#include <bits/stdc++.h> n using namespace std; n string dp[5001]; n vector<int> win[2001]; n int n; n string pow2[2001]; n void multi2(string &ress, const string &p) { n   int flag = 0; n   ress =   ; n   for (int i = 0; i < p.size(); i++) { n     ress += ((p[i] -  0 ) * 2 + flag) % 10 +  0 ; n     flag = ((p[i] -  0 ) * 2 + flag) / 10; n   } n   if (flag) ress +=  1 ; n   int pos = ress.size() - 1; n   for (; pos >= 0 && ress[pos] ==  0 ; pos--) n     ; n   ress = ress.substr(0, pos + 1); n   if (ress ==   ) ress =  0 ; n   return; n } n void initPow2(void) { n   pow2[0] =  1 ; n   for (int i = 1; i <= 2000; i++) multi2(pow2[i], pow2[i - 1]); n   return; n } n void add(string &ress, string num) { n   while (ress.size() < num.size()) ress +=  0 ; n   while (num.size() < ress.size()) num +=  0 ; n   int flag = 0; n   for (int i = 0; i < ress.size(); i++) { n     char tRes = (ress[i] -  0  + num[i] -  0  + flag) % 10 +  0 ; n     flag = (ress[i] -  0  + num[i] -  0  + flag) / 10; n     ress[i] = tRes; n   } n   if (flag) ress +=  1 ; n   int pos = ress.size() - 1; n   for (; pos >= 0 && ress[pos] ==  0 ; pos--) n     ; n   ress = ress.substr(0, pos + 1); n   if (ress ==   ) ress =  0 ; n   return; n } n bool larger(const string &str1, const string &str2) { n   if (str1.size() > str2.size()) return true; n   if (str1.size() < str2.size()) return false; n   for (int i = str1.size() - 1; i >= 0; i--) { n     if (str1[i] > str2[i]) return true; n     if (str1[i] < str2[i]) return false; n   } n   return false; n } n int main(void) { n   char enter; n   char input[10]; n   int t; n   dp[0] =  0 ; n   initPow2(); n   for (int i = 0; i <= 2000; i++) win[i].clear(); n   scanf( %d%c , &n, &enter); n   for (int i = 1; i <= n; i++) { n     scanf( %s%c%d%c , input, &enter, &t, &enter); n     if (input[0] ==  w ) { n       win[t].push_back(i); n       dp[i] = dp[i - 1]; n     } else { n       dp[i] = dp[i - 1]; n       if (win[t].size() != 0) { n         for (int j = 0; j < win[t].size(); j++) { n           string temp = pow2[t]; n           add(temp, dp[win[t][j] - 1]); n           if (larger(temp, dp[i])) dp[i] = temp; n         } n       } n     } n   } n   reverse(dp[n].begin(), dp[n].end()); n   printf( %s n , dp[n].c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s, ans; n int ar[5005]; n int cnt[5005]; n int ar2[5005]; n vector<pair<pair<int, int>, pair<int, int> > > v; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) ar[i] = 1000000000; n   for (int i = 0; i < m; i++) { n     int t, l, r, x; n     scanf( %d%d%d%d , &t, &l, &r, &x); n     if (t == 1) n       for (int j = l; j <= r; j++) cnt[j] += x; n     else { n       for (int j = l; j <= r; j++) ar[j] = min(ar[j], x - cnt[j]); n     } n     v.push_back(pair<pair<int, int>, pair<int, int> >(pair<int, int>(t, x), n                                                       pair<int, int>(l, r))); n   } n   for (int i = 1; i <= n; i++) n     if (max(ar[i], -ar[i]) > 1000000000) { n       printf( NO n ); n       exit(0); n     } n   for (int i = 1; i <= n; i++) ar2[i] = ar[i]; n   for (int i = 0; i < m; i++) { n     int t = v[i].first.first; n     int x = v[i].first.second; n     int l = v[i].second.first; n     int r = v[i].second.second; n     if (t == 1) { n       for (int j = l; j <= r; j++) ar2[j] += x; n     } else { n       int mx = -1000000000; n       for (int j = l; j <= r; j++) mx = max(mx, ar2[j]); n       if (mx != x) { n         printf( NO n ); n         exit(0); n       } n     } n   } n   printf( YES n ); n   for (int i = 1; i <= n; i++) printf( %d  , ar[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pot(int n) { n   if (n == (n & -n)) return n; n   return (n - (n & -n)); n } n int main() { n   int n, b, p; n   cin >> n >> b >> p; n   int ans1 = 0, ans2 = p * n; n   while (n != 1) { n     int d = pot(n); n     int e = n - d; n     n -= (d / 2); n     ans1 += (d * b + (d / 2)); n   } n   cout << ans1 <<     << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   char name[21]; n   int initials[26]; n   cin >> n; n   memset(initials, 0, 26 * sizeof(int)); n   for (int i = 0; i < n; ++i) { n     cin >> name; n     initials[name[0] -  a ]++; n   } n   int total = 0; n   for (int i = 0; i < 26; ++i) { n     int k = initials[i]; n     if (k % 2) n       total += ((k - 1) / 2) * ((k - 1) / 2); n     else n       total += k / 2 * (k / 2 - 1); n   } n   cout << total << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n long long ans, dp[N]; n int ed, n, m; n struct node { n   int w, c; n   double val; n   node(int a = 0, int b = 0) : w(a), c(b) {} n   bool operator<(const node &b) const { return val > b.val; } n } a[N], tmp[N]; n long long DP() { n   for (int i = 1; i <= ed; i++) { n     for (int j = m; j >= tmp[i].w; j--) { n       dp[j] = max(dp[j], dp[j - tmp[i].w] + tmp[i].c); n     } n   } n   return dp[m]; n } n int main() { n   scanf( %d%d , &n, &m); n   ans = 0; n   for (int i = 1; i <= n; i++) n     scanf( %d%d , &a[i].w, &a[i].c), a[i].val = 1.0 * a[i].c / a[i].w; n   sort(a + 1, a + 1 + n); n   int tp[4]; n   memset(tp, -1, sizeof(tp)); n   for (int i = 1; i <= n; i++) { n     if (m < 30) { n       ed = 0; n       while (i <= n) tmp[++ed] = a[i], i++; n       for (int ii = 1; ii <= 3; ii++) n         if (~tp[ii]) tmp[++ed] = node(ii, tp[ii]), m += ii, ans -= tp[ii]; n       ans += DP(); n       break; n     } n     ans += a[i].c; n     m -= a[i].w; n     tp[a[i].w] = a[i].c; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n template <class T> n inline T biton(T n, T pos) { n   return n | ((T)1 << pos); n } n template <class T> n inline T bitoff(T n, T pos) { n   return n & ~((T)1 << pos); n } n template <class T> n inline T ison(T n, T pos) { n   return (bool)(n & ((T)1 << pos)); n } n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n template <class T> n inline T bigmod(T p, T e, T m) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n   } n   return (T)ret; n }; n void solve() { n   string s, c; n   cin >> s >> c; n   if (s < c) { n     cout << s << endl; n     return; n   } n   int n = (int)s.size(); n   map<char, vector<int>> mn; n   map<char, set<char>> st; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (s[i] >= s[j]) { n         if (st[s[i]].count(s[j]) == 0) { n           mn[s[i]].push_back({j}); n           st[s[i]].insert(s[j]); n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (auto m : mn) { n       for (auto j : m.second) { n         swap(s[i], s[j]); n         if (s < c) { n           cout << s << endl; n           return; n         } n         swap(s[i], s[j]); n       } n     } n   } n   cout <<  ---  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.setf(ios::fixed); n   cout.precision(10); n   cout.tie(nullptr); n   cin.tie(nullptr); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s; n   for (int i = s.length() - 1; i >= 0; i--) { n     if (t ==   ) { n       t.push_back(s[i]); n     } else if (t.back() <= s[i]) { n       t.push_back(s[i]); n     } n   } n   reverse(t.begin(), t.end()); n   cout << t; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010, LOG = 20; n int n, m, k, q, u, v, x, y, t, a, b, ans; n int stt[MAXN], fnt[MAXN], ras[MAXN], timer = 1; n int sz[MAXN], par[MAXN], head[MAXN], h[MAXN]; n pair<long long, int> seg[MAXN << 2]; n long long lazy[MAXN << 2]; n set<int> st[MAXN]; n vector<int> G[MAXN]; n int dfs1(int node) { n   h[node] = h[par[node]] + 1; n   for (int v : G[node]) n     if (v != par[node]) { n       par[v] = node; n       sz[node] += dfs1(v); n     } n   return ++sz[node]; n } n void dfs2(int node, int hd) { n   head[node] = hd; n   stt[node] = timer++; n   ras[stt[node]] = node; n   int big = 0; n   for (int v : G[node]) n     if (v != par[node] && sz[v] > sz[big]) big = v; n   if (big) dfs2(big, hd); n   for (int v : G[node]) n     if (v != par[node] && v != big) dfs2(v, v); n   fnt[node] = timer; n } n pair<long long, int> Build(int id, int tl, int tr) { n   if (tr - tl == 1) return seg[id] = {INF, ras[tl]}; n   int mid = (tl + tr) >> 1; n   return seg[id] = min(Build(id << 1, tl, mid), Build(id << 1 | 1, mid, tr)); n } n void add_lazy(int id, long long val) { n   seg[id].first += val; n   lazy[id] += val; n } n void shift(int id) { n   if (!lazy[id]) return; n   add_lazy(id << 1, lazy[id]); n   add_lazy(id << 1 | 1, lazy[id]); n   lazy[id] = 0; n } n void Add(int id, int tl, int tr, int l, int r, long long val) { n   if (r <= tl || tr <= l) return; n   if (l <= tl && tr <= r) { n     add_lazy(id, val); n     return; n   } n   shift(id); n   int mid = (tl + tr) >> 1; n   Add(id << 1, tl, mid, l, r, val); n   Add(id << 1 | 1, mid, tr, l, r, val); n   seg[id] = min(seg[id << 1], seg[id << 1 | 1]); n } n pair<long long, int> Get(int id, int tl, int tr, int l, int r) { n   if (r <= tl || tr <= l) return seg[0]; n   if (l <= tl && tr <= r) return seg[id]; n   shift(id); n   int mid = (tl + tr) >> 1; n   return min(Get(id << 1, tl, mid, l, r), Get(id << 1 | 1, mid, tr, l, r)); n } n void Set(int id, int tl, int tr, int pos, long long val) { n   if (pos < tl || tr <= pos) return; n   if (tr - tl == 1) { n     seg[id].first = val; n     return; n   } n   shift(id); n   int mid = (tl + tr) >> 1; n   Set(id << 1, tl, mid, pos, val); n   Set(id << 1 | 1, mid, tr, pos, val); n   seg[id] = min(seg[id << 1], seg[id << 1 | 1]); n } n pair<long long, int> GetPath(int u, int v) { n   pair<long long, int> res = {INF, -1}; n   while (head[u] != head[v]) { n     if (h[head[u]] > h[head[v]]) swap(u, v); n     res = min(res, Get(1, 1, n + 1, stt[head[v]], stt[v] + 1)); n     v = par[head[v]]; n   } n   if (h[u] > h[v]) swap(u, v); n   res = min(res, Get(1, 1, n + 1, stt[u], stt[v] + 1)); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= m; i++) cin >> x, st[x].insert(i); n   dfs1(1); n   dfs2(1, 1); n   Build(1, 1, n + 1); n   seg[0] = {INF, 0}; n   for (int i = 1; i <= n; i++) n     if (st[i].size()) Set(1, 1, n + 1, stt[i], *st[i].begin()); n   while (q--) { n     cin >> t; n     if (t == 2) { n       cin >> v >> x; n       Add(1, 1, n + 1, stt[v], fnt[v], x); n       continue; n     } n     vector<int> out; n     cin >> u >> v >> k; n     while (k--) { n       pair<long long, int> p = GetPath(u, v); n       if (p.first >= INF) break; n       int v = p.second, id = *st[v].begin(); n       out.push_back(id); n       st[v].erase(id); n       if (st[v].empty()) n         Set(1, 1, n + 1, stt[v], INF); n       else n         Add(1, 1, n + 1, stt[v], stt[v] + 1, *st[v].begin() - id); n     } n     cout << out.size() <<    ; n     for (int i : out) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int h = 17, N = 1 << h; n long long t[2 * N]; n long long k; n long long product(int a, int b) { n   if (a > b) return 1; n   if (a % 2 == 1) return (t[a] * product(a + 1, b)) % k; n   if (b % 2 == 0) return (t[b] * product(a, b - 1)) % k; n   return product(a / 2, b / 2); n } n int main() { n   int n; n   scanf( %d%lld , &n, &k); n   for (int i = 1; i < 2 * N; ++i) t[i] = 1; n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &t[N + i]); n     t[N + i] = t[N + i] % k; n   } n   for (int i = N - 1; i >= 1; --i) t[i] = (t[2 * i] * t[2 * i + 1]) % k; n   long long m = 0; n   for (int i = 0; i < n; ++i) { n     int lo = i, hi = n - 1; n     while (lo < hi) { n       int mid = lo + (hi - lo) / 2; n       if (product(N + i, N + mid) == 0) n         hi = mid; n       else n         lo = mid + 1; n     } n     if (lo >= n || product(N + i, N + lo) != 0) break; n     m += static_cast<long long>(n) - lo; n   } n   printf( %lld n , m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, mod = 1e9 + 7; n int head[N], nxt[N << 1], to[N << 1], num = 0, n, b[N], fa[N], q[N], DFN = 0, n                                       k[N], in[N]; n inline void link(int x, int y) { n   nxt[++num] = head[x]; n   to[num] = y; n   head[x] = num; n } n inline int qm(int x, int k) { n   int sum = 1; n   while (k) { n     if (k & 1) sum = 1ll * sum * x % mod; n     x = 1ll * x * x % mod; n     k >>= 1; n   } n   return sum; n } n inline int inv(int x) { return qm(x, mod - 2); } n inline void dfs(int x, int last) { n   q[++DFN] = x; n   for (int i = head[x]; i; i = nxt[i]) n     if (to[i] != last) fa[to[i]] = x, dfs(to[i], x); n } n int main() { n   int x, y, z; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     x++; n     y++; n     link(x, y); n     link(y, x); n     k[x]++; n     k[y]++; n     b[x] += z; n     b[y] += z; n   } n   for (int i = 1; i <= n; i++) in[i] = k[i]; n   dfs(1, 1); n   for (int i = n; i >= 1; i--) { n     int x = q[i]; n     if (in[x] == 1) continue; n     k[fa[x]] = (k[fa[x]] - inv(k[x])) % mod; n     b[fa[x]] = (b[fa[x]] + 1ll * inv(k[x]) * b[x]) % mod; n   } n   b[1] = 1ll * b[1] * inv(k[1]) % mod; n   if (b[1] < 0) b[1] += mod; n   printf( %d n , b[1]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pr = pair<int, int>; n using prll = pair<long, long>; n using V = vector<int>; n using Vll = vector<long long>; n template <typename T, typename TT> n pr operator+(pair<T, TT> a, pair<T, TT> b) { n   return {a.first + b.first, a.second + b.second}; n } n template <typename T, typename TT> n ostream& operator<<(ostream& s, pair<T, TT> t) { n   return s <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& s, vector<T> t) { n   for (auto itr : t) s << itr <<    ; n   return s; n } n int n, m; n vector<pair<int, int>> a(200000); n vector<long long> c(100000); n int binSearch(int x) { n   int beg = 0; n   int last = m - 1; n   while (beg < last) { n     int mid = beg + (last - beg + 1) / 2; n     if (a[mid].first < x) n       last = mid - 1; n     else n       beg = mid; n   } n   if (a[beg].first < x) return -1; n   return beg; n } n bool cmp(pr a, pr b) { n   if (a.first == b.first) return a.second > b.second; n   return a.first > b.first; n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(0); n   cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 0; i < int(m); i++) { n       int x, y; n       cin >> x >> y; n       a[i] = {x, y}; n     } n     sort(a.begin(), a.begin() + m, cmp); n     c[0] = a[0].first; n     for (int i = 0; i < int(m - 1); i++) c[i + 1] = c[i] + a[i + 1].first; n     ll ans = INT_MIN; n     ans = c[min(n, m) - 1]; n     for (int i = 0; i < int(m); i++) { n       int beg = binSearch(a[i].second); n       int t = min(beg, n - 3); n       if (beg >= i) t = min(beg, n - 2); n       if (t < 0) n         ans = max(ans, a[i].first + (n - 1) * 1LL * a[i].second); n       else if (t >= i) n         ans = max(ans, c[t] + (n - t - 1) * 1LL * a[i].second); n       else n         ans = max(ans, c[t] + (n - t - 2) * 1LL * a[i].second + a[i].first); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, Q, ans[100005], t[265000]; n vector<int> v[100005]; n vector<pair<int, int> > vq[100005]; n void build(int k, int l, int r) { n   t[k] = 1e9; n   if (l == r) return; n   int mid = (l + r) / 2; n   build(k * 2, l, mid); n   build(k * 2 + 1, mid + 1, r); n } n void pushdown(int k) { n   t[k * 2] = min(t[k * 2], t[k]); n   t[k * 2 + 1] = min(t[k * 2 + 1], t[k]); n } n void change(int k, int l, int r, int x, int y, int v) { n   if (t[k] <= v) return; n   if (l == x && r == y) { n     t[k] = v; n     return; n   } n   int mid = (l + r) / 2; n   pushdown(k); n   if (y <= mid) n     change(k * 2, l, mid, x, y, v); n   else if (x > mid) n     change(k * 2 + 1, mid + 1, r, x, y, v); n   else n     change(k * 2, l, mid, x, mid, v), n         change(k * 2 + 1, mid + 1, r, mid + 1, y, v); n   t[k] = max(t[k * 2], t[k * 2 + 1]); n } n int ask(int k, int l, int r, int p, int v) { n   if (t[k] <= v) return -1; n   if (p <= l) { n     for (; l < r;) { n       int mid = (l + r) / 2; n       pushdown(k); n       k *= 2; n       if (t[k] > v) n         r = mid; n       else n         l = mid + 1, k++; n     } n     return l; n   } n   int mid = (l + r) / 2; n   int tmp = -1; n   if (p <= mid) tmp = ask(k * 2, l, mid, p, v); n   if (tmp != -1) return tmp; n   return ask(k * 2 + 1, mid + 1, r, p, v); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x < y) v[x].push_back(y); n   } n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     vq[x].push_back(make_pair(y, i)); n   } n   build(1, 1, n); n   for (int i = n; i; i--) { n     for (int j = 0; j < v[i].size(); j++) n       change(1, 1, n, i, v[i][j] - 1, v[i][j]); n     for (int j = 0; j < vq[i].size(); j++) n       ans[vq[i][j].second] = ask(1, 1, n, i, vq[i][j].first); n   } n   for (int i = 1; i <= Q; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m; n vector<long long int> ar[100001]; n vector<long long int> res[2]; n long long int vis[100001]; n long long int col[100001]; n bool dfs(long long int node, long long int c) { n   vis[node] = 1; n   res[c].push_back(node); n   col[node] = c; n   for (long long int child : ar[node]) { n     if (vis[child] == 0) { n       if (dfs(child, c ^ 1) == false) return false; n     } else if (col[node] == col[child]) n       return false; n   } n   return true; n } n void solve() { n   long long int test = 1; n   while (test--) { n     cin >> n >> m; n     while (m--) { n       long long int a, b; n       cin >> a >> b; n       ar[a].push_back(b); n       ar[b].push_back(a); n     } n     long long int count = 1; n     vector<long long int> von; n     memset(vis, 0, sizeof(vis)); n     for (long long int i = 1; i <= n; i++) { n       if (vis[i] == 0) { n         if (ar[i].empty()) continue; n         if (dfs(i, 0) == false) { n           cout << -1 << endl; n           return; n         } n       } n     } n     for (long long int i = 0; i < 2; i++) { n       cout << res[i].size() << endl; n       for (auto x : res[i]) cout << x <<    ; n       cout << endl; n     } n   } n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0 || b == 0) return a + b; n   if (a < b) return gcd(b % a, a); n   return gcd(a % b, b); n } n int a[111]; n int main() { n   int n; n   cin >> n; n   int g = 0, max = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     g = gcd(g, a[i]); n     if (max < a[i]) max = a[i]; n   } n   for (int i = 0; i < n; i++) { n     a[i] /= g; n   } n   max /= g; n   int moves = max - n; n   if (moves % 2) n     cout <<  Alice ; n   else n     cout <<  Bob ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n vector<long long> p; n int main() { n   int n, q; n   cin >> n >> q; n   string s; n   cin >> s; n   p.resize(1e5 + 1); n   p[0] = 1; n   for (int i = 1; i <= 1e5; i++) { n     p[i] = (p[i - 1] * 2) % MOD; n   } n   vector<long long> kum(n + 1); n   kum[0] = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       kum[i + 1] = kum[i] + 1; n     } else { n       kum[i + 1] = kum[i]; n     } n   } n   for (int i = 0; i < q; i++) { n     int l, r; n     cin >> l >> r; n     long long kol_1 = kum[r] - kum[l - 1]; n     long long kol_0 = r - l + 1 - kol_1; n     long long ans_1 = p[kol_1] - 1; n     long long ans_0 = ans_1 * (p[kol_0] - 1); n     cout << (ans_1 + ans_0) % MOD <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<int> a, b; n int c = 0; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     a.push(x); n   } n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     b.push(x); n   } n   while (!b.empty() && !a.empty()) { n     if (abs(a.top() - b.top()) < 2) { n       c++; n       a.pop(); n       b.pop(); n       continue; n     } n     if (a.top() > b.top()) n       a.pop(); n     else n       b.pop(); n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n int n, k; n bool check(long long m) { n   long long sum = 0; n   for (int i = n / 2; i < n; i++) { n     sum += max(m - v[i], 0ll); n   } n   if (sum <= k) { n     return true; n   } n   return false; n } n int main() { n   cin >> n >> k; n   v.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   long long l = v[n / 2]; n   long long r = 1e10; n   while (l + 1 < r) { n     long long m = (l + r) / 2; n     if (check(m)) { n       l = m; n     } else { n       r = m; n     } n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reverseStr(string& str); n void reverseQueue(queue<int>& Queue); n void print_queue(queue<int>& q); n int n, a[250], sum, num; n int main() { n   cin >> n; n   for (int i = 0; i < n * 2; i++) cin >> a[i]; n   for (int i = 0; i < n * 2; i++) { n     for (int j = i + 2; j < n * 2; j++) { n       if (a[i] == a[j]) { n         sum += j - i - 1; n         for (int l = j; l > i; l--) { n           swap(a[l], a[l - 1]); n         } n       } n     } n   } n   cout << sum; n } n void reverseStr(string& str) { n   int n = str.length(); n   for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); n } n void reverseQueue(queue<int>& Queue) { n   stack<int> Stack; n   while (!Queue.empty()) { n     Stack.push(Queue.front()); n     Queue.pop(); n   } n   while (!Stack.empty()) { n     Queue.push(Stack.top()); n     Stack.pop(); n   } n } n void print_queue(queue<int>& q) { n   while (!q.empty()) { n     cout << q.front() <<    ; n     q.pop(); n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1e5 + 7; n vector<long long> v[MX]; n bool vis[MX]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   pair<long long, long long> p[n]; n   for (long long i = 0; i < n; i++) cin >> p[i].first >> p[i].second; n   long double l = 0, r = 1e10; n   for (long long i = 0; i < 100; i++) { n     long long cnt; n     long double mid = (l + r) / 2, d = -1e20; n     for (cnt = 0; cnt < n; cnt++) { n       if (p[cnt].second > 0) n         d = max(d, p[cnt].first + mid * p[cnt].second); n       else if (p[cnt].first + mid * p[cnt].second <= d) n         break; n     } n     if (cnt < n) n       r = mid; n     else n       l = mid; n   } n   if (r == 1e10) n     cout << -1; n   else n     cout << fixed << setprecision(18) << l; n } n


#include <bits/stdc++.h> n using namespace std; n int t, ans, tot, pus, a, b, c, d; n int main() { n   register int i; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     ans = a * (b - d - 1); n     ans = ((ans) > (a * d) ? (ans) : (a * d)); n     ans = ((ans) > (b * (a - c - 1)) ? (ans) : (b * (a - c - 1))); n     ans = ((ans) > (b * c) ? (ans) : (b * c)); n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> factor[100001]; n void precal() { n   for (int i = (1); i < (100001); ++i) n     for (int j = (i); j < (100001); j += (i)) factor[j].push_back(i); n } n int expo(int a, int b) { n   if (b == 0) return 1; n   if (b == 1) return a % 1000000007; n   int c = expo(a, b >> 1); n   c = ((long long)c * c) % 1000000007; n   if (b & 1) c = ((long long)c * a) % 1000000007; n   return c; n } n int main(int argc, char **argv) { n   int N; n   scanf( %d , &N); n   vector<int> A(N); n   for (int i = (0); i < (N); ++i) scanf( %d , &A[i]); n   sort(A.begin(), A.end()); n   precal(); n   int res = 0; n   for (int f = (1); f < (100001); ++f) { n     int numFactor = factor[f].size(); n     int bound[numFactor]; n     int cur = 0; n     for (int i = (0); i < (numFactor); ++i) { n       int lo = cur, hi = N - 1; n       int nextFactor = (i + 1 == numFactor) ? (1 << 29) : factor[f][i + 1]; n       while (lo <= hi) { n         int mi = (lo + hi) / 2; n         if (A[mi] >= nextFactor) n           hi = mi - 1; n         else n           lo = mi + 1; n       } n       bound[i] = lo - cur; n       cur = lo; n     } n     if (!bound[numFactor - 1]) continue; n     int countWays = 1; n     for (int i = (0); i < (numFactor - 1); ++i) n       countWays = ((long long)countWays * expo(i + 1, bound[i])) % 1000000007; n     countWays = ((long long)countWays * n                  (((long long)expo(numFactor, bound[numFactor - 1]) + n                    1000000007 - expo(numFactor - 1, bound[numFactor - 1])) % n                   1000000007)) % n                 1000000007; n     res = (res + countWays) % 1000000007; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct inf { n   long long t, w; n }; n struct djk { n   long long n, v; n   bool operator>(const djk &i) const { return v < i.v; } n   bool operator<(const djk &i) const { return v > i.v; } n } n; n struct edge { n   long long n1, n2, t; n   bool operator<(const edge &i) const { return t < i.t; } n }; n long long nodes, edges, n1, n2, weight, nn, totes, p1, p2, ports; n long long isp[100005], cp[100005], pd[100005], rnk[100005], head[100005]; n edge elist[100005]; n vector<inf> nlist[100005]; n priority_queue<djk> viser; n long long ufind(long long n) { n   if (head[n] != n) head[n] = ufind(head[n]); n   return head[n]; n } n void uni(long long n1, long long n2) { n   p1 = ufind(n1); n   p2 = ufind(n2); n   if (rnk[p1] > rnk[p2]) n     head[p1] = p2; n   else if (rnk[p2] > rnk[p1]) n     head[p2] = p1; n   else n     head[p1] = p2, rnk[p1]++; n } n int main() { n   scanf( %I64d%I64d , &nodes, &edges); n   for (int i = 1; i <= nodes; i++) pd[i] = -1, head[i] = i, rnk[i] = 1; n   for (int i = 1; i <= edges; i++) { n     scanf( %I64d%I64d%I64d , &n1, &n2, &weight); n     nlist[n1].push_back({n2, weight}); n     nlist[n2].push_back({n1, weight}); n     elist[i] = {n1, n2, weight}; n   } n   scanf( %I64d , &ports); n   for (int i = 1; i <= ports; i++) { n     scanf( %I64d , &n1); n     isp[n1] = 1; n     viser.push({n1, 0}); n     pd[n1] = 0; n     cp[n1] = n1; n   } n   while (!viser.empty()) { n     n = viser.top(); n     viser.pop(); n     if (pd[n.n] > n.v) continue; n     for (int i = 0; i < nlist[n.n].size(); i++) { n       nn = nlist[n.n][i].t; n       if (pd[nn] == -1 || pd[nn] > nlist[n.n][i].w + pd[n.n]) { n         pd[nn] = nlist[n.n][i].w + pd[n.n]; n         cp[nn] = cp[n.n]; n         viser.push({nn, pd[nn]}); n       } n     } n   } n   for (int i = 1; i <= edges; i++) n     elist[i].t += pd[elist[i].n1] + pd[elist[i].n2]; n   sort(elist + 1, elist + 1 + edges); n   for (int i = 1; i <= edges; i++) { n     if (ufind(cp[elist[i].n1]) != ufind(cp[elist[i].n2])) { n       uni(cp[elist[i].n1], cp[elist[i].n2]); n       totes += elist[i].t; n     } n   } n   if (!isp[1] && pd[1] != -1) totes += pd[1]; n   printf( %I64d n , totes); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 55; n const int INF = 1 << 20; n bool dp1[MAXN][MAXN][26] = {false}; n bool dp2[MAXN][MAXN][26] = {false}; n int dp[MAXN][MAXN]; n char s1[MAXN], s2[MAXN]; n string tr[MAXN]; n int n; n void cal(char* s, bool f[MAXN][MAXN][26]) { n   int len = strlen(s); n   for (int i = 0; i < len; i++) { n     f[i][i][s[i] -  a ] = true; n   } n   for (int L = 1; L <= len; L++) { n     for (int i = 0; i + L - 1 < len; i++) { n       int j = i + L - 1; n       for (int k = i; k < j; k++) { n         for (int c = 0; c < n; c++) { n           if (f[i][k][tr[c][3] -  a ] && f[k + 1][j][tr[c][4] -  a ]) { n             f[i][j][tr[c][0] -  a ] = true; n           } n         } n       } n     } n   } n } n int main() { n   cin >> s1 >> s2; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> tr[i]; n   } n   cal(s1, dp1); n   cal(s2, dp2); n   int len1 = strlen(s1); n   int len2 = strlen(s2); n   for (int i = 0; i <= len1; i++) n     for (int j = 0; j <= len2; j++) dp[i][j] = INF; n   dp[0][0] = 0; n   for (int i = 0; i < len1; i++) n     for (int ii = i; ii < len1; ii++) n       for (int c = 0; c < 26; c++) n         if (dp1[i][ii][c]) n           for (int j = 0; j < len2; j++) n             for (int jj = j; jj < len2; jj++) n               if (dp2[j][jj][c]) n                 dp[ii + 1][jj + 1] = min(dp[ii + 1][jj + 1], dp[i][j] + 1); n   int ans = (dp[len1][len2] == INF ? -1 : dp[len1][len2]); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long me(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   c_p_c(); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     long long b[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     for (long long i = 0; i < n; i++) cin >> b[i]; n     long long c[n]; n     multiset<long long> s; n     long long l = 0; n     vector<long long> v; n     long long g = 0; n     long long temp = 0; n     long long g1 = 0; n     for (long long i = 0; i < n; i++) { n       c[i] = b[i] - a[i]; n       if (c[i] == 0 && g1 == 1) { n         break; n       } n       if (c[i] < 0) { n         cout <<  NO n ; n         l = 1; n         break; n       } n       if (c[i] != 0 && (temp == c[i] || g == 0)) { n         a[i] = a[i] + c[i]; n         temp = c[i]; n         g = 1; n         g1 = 1; n       } n     } n     if (l == 1) { n       continue; n     } n     long long flg = 0; n     for (long long i = 0; i < n; i++) { n       if (a[i] != b[i]) { n         flg = 1; n         break; n       } n     } n     if (flg == 1) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n     } n   } 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 t, n, k, c = 0, d = 0; n   cin >> n >> k; n   map<long long, long long> m; n   for (int i = 0; i < n; ++i) { n     cin >> t; n     m[t]++; n   } n   auto it2 = m.begin(); n   for (auto it = m.begin(); it != m.end(); ++it) { n     it2 = it; n     it2++; n     t = it->first; n     if (it2 == m.end()) { n       c += it->second; n       break; n     } n     d = it2->first; n     if (t + k < d) c += it->second; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T a) { n   return a * a; n } n int n; n int a[21000]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int best = -1000000000; n   for (int i = 3; i <= n; i++) n     if (n % i == 0) { n       int g = n / i; n       for (int j = 0; j < g; j++) { n         int cur = 0; n         for (int k = j; k < n; k += g) cur += a[k]; n         best = max(cur, best); n       } n     } n   cout << best; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n struct Edge { n   int src, dst; n   int weight; n   Edge(int src_, int dst_, int weight_) n       : src(src_), dst(dst_), weight(weight_) {} n   Edge(int dst_, int weight_) : src(-2), dst(dst_), weight(weight_) {} n }; n bool operator<(const Edge &e, const Edge &f) { n   return e.weight != f.weight ? e.weight > f.weight : e.dst < f.dst; n } n void dijkstra(const vector<vector<Edge> > &g, int start, vector<int> &dist) { n   int n = g.size(); n   dist.assign(n, INF); n   dist[start] = 0; n   vector<int> prev(n, -1); n   priority_queue<Edge> q; n   for (q.push(Edge(-2, start, 0)); !q.empty();) { n     Edge e = q.top(); n     q.pop(); n     if (prev[e.dst] != -1) continue; n     prev[e.dst] = e.src; n     for (typeof((g[e.dst]).begin()) f = ((g[e.dst]).begin()); n          f != (g[e.dst]).end(); ++f) { n       if (dist[f->dst] > e.weight + f->weight) { n         dist[f->dst] = e.weight + f->weight; n         q.push(Edge(f->src, f->dst, e.weight + f->weight)); n       } n     } n   } n } n int main() { n   int n; n   int x0, y0, x1, y1; n   vector<pair<int, pair<int, int> > > v; n   cin >> y0 >> x0 >> y1 >> x1; n   cin >> n; n   for (int(i) = 0; (i) < (int)(n); ++(i)) { n     int o, a, b; n     cin >> o >> a >> b; n     v.push_back(make_pair((o), (make_pair((a), (b))))); n   } n   sort((v).begin(), (v).end()); n   map<pair<int, int>, int> m; n   int t = 0; n   static const int dy[4] = {0, -1, -1, -1}, dx[4] = {-1, 0, -1, 1}; n   vector<vector<Edge> > g; n   for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) { n     for (int(j) = (int)(i->second.first); (j) <= (int)(i->second.second); n          ++(j)) { n       if (m.count(make_pair((i->first), (j)))) continue; n       int q = m[make_pair((i->first), (j))] = t++; n       g.push_back(vector<Edge>()); n       for (int(d) = 0; (d) < (int)(4); ++(d)) n         if (m.count(make_pair((i->first + dy[d]), (j + dx[d])))) { n           g[q].push_back( n               Edge(q, m[make_pair((i->first + dy[d]), (j + dx[d]))], 1)); n         } n     } n   } n   vector<vector<Edge> > gg(t); n   for (int(i) = 0; (i) < (int)(t); ++(i)) n     for (typeof((g[i]).begin()) j = ((g[i]).begin()); j != (g[i]).end(); ++j) { n       gg[j->src].push_back(Edge(j->src, j->dst, 1)); n       gg[j->dst].push_back(Edge(j->dst, j->src, 1)); n     } n   int r = -1; n   if (!m.count(make_pair((y0), (x0)))) n     r = -999; n   else if (!m.count(make_pair((y1), (x1)))) n     r = -999; n   else { n     vector<int> d; n     dijkstra(gg, m[make_pair((y0), (x0))], d); n     r = d[m[make_pair((y1), (x1))]]; n     if (r == INF) r = -1; n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr <<  [   << *it <<   =   << a <<   ]  ; n   err(++it, args...); n } n template <typename Tk, typename Tv> n ostream& operator<<(ostream& os, const pair<Tk, Tv>& p) { n   os <<  {  << p.first <<  ,  << p.second <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [  ; n   for (T x : p) os << x <<    ; n   os <<  ]  <<   n ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  {  ; n   for (T x : p) os << x <<    ; n   os <<  }  <<   n ; n   return os; n } n template <typename Tk, typename Tv> n ostream& operator<<(ostream& os, const map<Tk, Tv>& p) { n   os <<  {  ; n   for (pair<Tk, Tv> x : p) os << x <<    ; n   os <<  }  <<   n ; n   return os; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int n, k, l; n     cin >> n >> k >> l; n     vector<long long int> depth(n), time(2 * k); n     for (long long int i = 0; i <= k; i++) { n       time[i] = i; n     } n     long long int ct = 1; n     for (long long int i = 0; i < n; i++) { n       cin >> depth[i]; n     } n     for (long long int i = k - 1; i > 0; i--) { n       time[k + (ct++)] = i; n     } n     bool dp[2 * k][n]; n     memset(dp, false, sizeof(dp)); n     for (long long int i = 0; i < 2 * k; i++) { n       if (depth[0] + time[i] <= l) { n         dp[i][0] = true; n       } n     } n     for (long long int i = 0; i < n - 1; i++) { n       if (depth[i] + k <= l) { n         if (i == 0) { n           for (long long int j = 0; j < 2 * k; j++) { n             dp[j][i] = true; n           } n         } else { n           bool find_true = false; n           for (long long int j = 0; j < 2 * k; j++) { n             if (dp[j][i]) { n               find_true = true; n             } n           } n           if (find_true) { n             for (long long int j = 0; j < 2 * k; j++) { n               dp[j][i] = true; n             } n           } n         } n       } n       for (long long int j = 0; j < 2 * k; j++) { n         if (dp[j][i] && n             (depth[i + 1] + (long long int)time[(j + 1) % (2 * k)] <= l)) { n           dp[(j + 1) % (2 * k)][i + 1] = true; n         } n       } n     } n     bool pass = false; n     for (long long int i = 0; i < 2 * k; i++) { n       if (dp[i][n - 1]) { n         pass = true; n         break; n       } n     } n     cout << (pass ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int max(int a, int b) { n   if (a > b) return a; n   return b; n } n int min(int a, int b) { n   if (a < b) n     return a; n   else n     return b; n } n char s1[600000], s2[600000]; n int z[600000], y[600000]; n struct student { n   int l; n   int r; n } a[600000]; n int main() { n   int i; n   int n, m, p; n   int j; n   scanf( %d%d%d , &n, &m, &p); n   for (i = 0; i <= n + 1; i++) { n     a[i].l = i - 1; n     a[i].r = i + 1; n   } n   getchar(); n   gets(s1 + 1); n   gets(s2 + 1); n   int num = 0; n   int len = strlen(s1 + 1); n   for (i = 1; i <= len; i++) { n     if (s1[i] ==  ( ) n       z[num++] = i; n     else { n       y[z[--num]] = i; n       y[i] = z[num]; n     } n   } n   len = strlen(s2 + 1); n   for (i = 1; i <= len; i++) { n     if (s2[i] ==  R ) n       p = a[p].r; n     else if (s2[i] ==  L ) n       p = a[p].l; n     else { n       a[a[max(p, y[p])].r].l = a[min(p, y[p])].l; n       a[a[min(p, y[p])].l].r = a[max(p, y[p])].r; n       a[min(p, y[p])].r = a[max(p, y[p])].r; n       p = a[p].r; n     } n     if (p < 1) p = a[p].r; n     if (p > n) p = a[p].l; n   } n   for (i = a[0].r; i <= n; i = a[i].r) { n     printf( %c , s1[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n const int sieve_size = 5e6; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int &x : a) cin >> x; n   vector<int> primes; n   vector<bool> sieve(sieve_size); n   for (int i = 2; i < int((sieve).size()); i++) { n     if (sieve[i]) continue; n     primes.push_back(i); n     for (int j = i + i; j < int((sieve).size()); j += i) { n       sieve[j] = true; n     } n   } n   auto get_decomp = [](int x) { n     vector<int> decomp; n     for (int j = 2; j * j <= x; j++) { n       if (x % j == 0) { n         decomp.push_back(j); n         x /= j; n         while (x % j == 0) x /= j; n       } n     } n     if (x != 1) decomp.push_back(x); n     return decomp; n   }; n   set<int> occupied_primes; n   vector<int> b(n); n   bool a_coprime = true; n   for (int i = 0, primes_idx = 0; i < n; i++) { n     if (a_coprime) { n       vector<int> decomp = get_decomp(a[i]); n       for (int x : decomp) { n         if (occupied_primes.count(x)) { n           a_coprime = false; n           break; n         } n       } n       if (!a_coprime) { n         for (a[i]++;; a[i]++) { n           decomp = get_decomp(a[i]); n           bool ok = true; n           for (int x : decomp) { n             if (occupied_primes.count(x)) { n               ok = false; n               break; n             } n           } n           if (ok) break; n         } n       } n       for (int x : decomp) occupied_primes.insert(x); n       b[i] = a[i]; n     } else { n       while (occupied_primes.count(primes[primes_idx])) primes_idx++; n       b[i] = primes[primes_idx++]; n     } n   } n   for (int x : b) cout << x <<    ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int tcs = 1; n   for (int tc = 1; tc <= tcs; tc++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, h; n long long l[200010], r[200010]; n long long dsum[200010], rsum[200010]; n int main() { n   scanf( %lld%lld , &n, &h); n   for (long long i = (long long)(0); i < (long long)(n); i++) n     scanf( %lld%lld , l + i, r + i); n   for (long long i = (long long)(0); i < (long long)(n - 1); i++) n     dsum[i + 1] = dsum[i] + l[i + 1] - r[i]; n   for (long long i = (long long)(0); i < (long long)(n); i++) n     rsum[i + 1] = rsum[i] + r[i] - l[i]; n   long long res = h; n   for (long long i = (long long)(0); i < (long long)(n - 1); i++) { n     long long lb = 0, ub = n; n     while (ub - lb > 1) { n       long long mid = (lb + ub) / 2; n       if (dsum[mid] - dsum[i] < h) n         lb = mid; n       else n         ub = mid; n     } n     res = (res > h + rsum[ub] - rsum[i] ? res : h + rsum[ub] - rsum[i]); n   } n   res = (res > h + r[n - 1] - l[n - 1] ? res : h + r[n - 1] - l[n - 1]); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Act { n   int l, r; n   int mx; n   double pb; n   vector<Act *> son; n   static int dp_size; n   double dp[5005]; n   bool operator<(const Act &act) const { n     if (l < act.l) { n       return true; n     } else if (l > act.l) { n       return false; n     } else if (r > act.r) { n       return true; n     } n     return false; n   } n   bool contains(Act &act) { return act.l >= l && act.r <= r; } n   double p(int profit) { n     if (profit < 0) return 0.0; n     if (profit > dp_size) return 1.0; n     return dp[profit]; n   } n }; n int Act::dp_size = 0; n int n, q; n int *a; n int **m; n int *lg; n Act act[5005]; n void dfs(Act &act) { n   for (int i = 0; i < act.son.size(); i++) { n     dfs(*act.son[i]); n   } n   for (int i = 0; i <= Act::dp_size; i++) { n     act.dp[i] = 0.0; n   } n   for (int i = 0; i <= Act::dp_size; i++) { n     double p = (i == 0 ? 0.0 : act.pb); n     double np = 1.0 - act.pb; n     for (int j = 0; j < act.son.size(); j++) { n       Act *son = act.son[j]; n       p *= son->p(act.mx - son->mx + i - 1); n       np *= son->p(act.mx - son->mx + i); n     } n     act.dp[i] = p + np; n   } n } n int main() { n   cin >> n >> q; n   a = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   lg = new int[n + 1]; n   lg[1] = 0; n   for (int i = 2; i <= n; i++) { n     lg[i] = lg[i / 2] + 1; n   } n   m = new int *[lg[n] + 1]; n   for (int i = 0; i <= lg[n]; i++) { n     m[i] = new int[n]; n   } n   for (int i = 0; i < n; i++) { n     m[0][i] = a[i]; n   } n   for (int i = 1; i <= lg[n]; i++) { n     int psz = 1 << (i - 1); n     int nsz = 1 << i; n     for (int j = 0; j + nsz <= n; j++) { n       m[i][j] = max(m[i - 1][j], m[i - 1][j + psz]); n     } n   } n   q++; n   for (int i = 0; i < q; i++) { n     if (i == q - 1) { n       act[i].l = 1; n       act[i].r = n; n       act[i].pb = 0.0; n     } else { n       cin >> act[i].l >> act[i].r >> act[i].pb; n     } n     act[i].l--, act[i].r--; n     int tlg = lg[act[i].r - act[i].l + 1]; n     int tsz = 1 << tlg; n     act[i].mx = max(m[tlg][act[i].l], m[tlg][act[i].r - tsz + 1]); n   } n   sort(act, act + q); n   Act::dp_size = q; n   for (int i = 1; i < q; i++) { n     int j = i - 1; n     while (!act[j].contains(act[i])) j--; n     act[j].son.push_back(act + i); n   } n   dfs(act[0]); n   double ans = 0.0; n   for (int i = 0; i <= Act::dp_size; i++) { n     ans += (act[0].mx + i) * (act[0].p(i) - act[0].p(i - 1)); n   } n   printf( %.8lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, l, n, a, b, ans = 0; n int gcd(int a, int b) { n   if (a < b) swap(a, b); n   return (b == 0) ? a : gcd(b, a % b); n } n struct st { n   int first; n   long double second; n }; n int main() { n   std::ios_base::sync_with_stdio(0); n   string s; n   cin >> s; n   n = s.size(); n   list<int> left, right; n   j = 0; n   for (auto i : s) { n     if (i ==  l ) { n       left.push_front(++j); n     } else { n       right.push_front(++j); n     } n   } n   reverse(right.begin(), right.end()); n   for (auto i : right) cout << i <<   n ; n   for (auto i : left) cout << i <<   n ; n   fflush(stdin); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class _T> n inline _T sqr(const _T &x) { n   return x * x; n } n template <class _T> n inline string tostr(const _T &a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795; n const long double EPS = 1e-11; n const int dx[4] = {1, 0, -1, 0}; n const int dy[4] = {0, -1, 0, 1}; n int n, m; n int a[2024][2024]; n bool u[2024][2024]; n int u2[2024][2024]; n bool u3[2024][2024]; n int d[2024][2024]; n int q[2024 * 2024][2]; n int e[1024000]; n void bfs(int x, int y, signed long long &sx, signed long long &sy, int &s) { n   int qb = 0, qe = 0; n   s = 1; n   sx = x; n   sy = y; n   u[x][y] = true; n   q[qe][0] = x; n   q[qe][1] = y; n   qe++; n   while (qb != qe) { n     x = q[qb][0]; n     y = q[qb][1]; n     qb++; n     for (int l = 0; l < (int)(4); l++) { n       int xn = x + dx[l]; n       int yn = y + dy[l]; n       if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || u[xn][yn]) n         continue; n       u[xn][yn] = true; n       q[qe][0] = xn; n       q[qe][1] = yn; n       sx += xn; n       sy += yn; n       s++; n       qe++; n     } n   } n } n int bfs2(int x, int y, int col) { n   if (!a[x][y]) n     for (;;) n       ; n   int qb = 0, qe = 0; n   u2[x][y] = col; n   d[x][y] = 0; n   q[qe][0] = x; n   q[qe][1] = y; n   qe++; n   while (qb != qe) { n     x = q[qb][0]; n     y = q[qb][1]; n     qb++; n     for (int l = 0; l < (int)(4); l++) { n       int xn = x + dx[l]; n       int yn = y + dy[l]; n       if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || u2[xn][yn]) n         continue; n       d[xn][yn] = d[x][y] + 1; n       u2[xn][yn] = col; n       q[qe][0] = xn; n       q[qe][1] = yn; n       qe++; n     } n   } n   return qe; n } n int main() { n   cout << setiosflags(ios::fixed) << setprecision(10); n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       scanf( %d , &a[i][j]); n     } n   } n   int cnt = 0; n   memset((u), 0, sizeof(u)); n   memset((u2), 0, sizeof(u2)); n   memset((u3), 0, sizeof(u3)); n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       if (a[i][j] && !u[i][j]) { n         signed long long xs, ys; n         int s; n         bfs(i, j, xs, ys, s); n         int t = bfs2(xs / s, ys / s, cnt + 1); n         int z = 0; n         for (int k = 0; k < (int)(t); k++) { n           int x = q[k][0]; n           int y = q[k][1]; n           bool bb = false; n           for (int l = 0; l < (int)(4); l++) { n             int xn = x + dx[l]; n             int yn = y + dy[l]; n             if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn]) continue; n             if (d[xn][yn] > d[x][y]) bb = true; n           } n           if (!u3[x][y] && bb) { n             int w = 0; n             for (int di = (-4); di <= (int)(4); di++) { n               for (int dj = (-4); dj <= (int)(4); dj++) { n                 int xn = x + di; n                 int yn = y + dj; n                 if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || n                     u2[xn][yn] != cnt + 1) n                   continue; n                 w++; n               } n             } n             if (w < 27) { n               z++; n               for (int di = (-4); di <= (int)(4); di++) { n                 for (int dj = (-4); dj <= (int)(4); dj++) { n                   int xn = x + di; n                   int yn = y + dj; n                   if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || n                       u3[xn][yn]) n                     continue; n                   u3[xn][yn] = true; n                 } n               } n             } n           } n         } n         e[cnt] = z; n         cnt++; n       } n     } n   } n   printf( %d n , cnt); n   sort(e, e + cnt); n   for (int i = 0; i < (int)(cnt); i++) { n     if (i) putchar(   ); n     printf( %d , e[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[105], ans, n; n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     ans += a[i] * i; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, q; n   std::cin >> n >> q; n   int ans[q], servers[n], server; n   for (server = 0; server < n; ++server) { n     servers[server] = 0; n   } n   int index, t, k, d, planner[n]; n   for (int i = 0; i < q; ++i) { n     server = 0; n     index = 0; n     std::cin >> t >> k >> d; n     while (index < k && server < n) { n       if (servers[server] <= t) { n         planner[index++] = server; n         servers[server] = t; n       } n       ++server; n     } n     if (index == k) { n       ans[i] = k; n       for (index = 0; index < k; ++index) { n         ans[i] += planner[index]; n         servers[planner[index]] += d; n       } n     } else { n       ans[i] = -1; n     } n   } n   for (int a : ans) { n     std::cout << a << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     vector<int> sml, big; n     for (int i = 1, j = n; i < j; i++, j--) { n       printf( ? %d %d n , i, j); n       fflush(stdout); n       char c[5]; n       scanf( %s , c); n       if (c[0] ==  < ) { n         sml.push_back(i); n         big.push_back(j); n       } n       if (c[0] ==  > ) { n         sml.push_back(j); n         big.push_back(i); n       } n       if (c[0] ==  = ) { n         sml.push_back(i); n         big.push_back(j); n       } n     } n     if (n % 2) { n       big.push_back(ceil(n / 2.0)); n       sml.push_back(ceil(n / 2.0)); n     } n     int mn = sml[0]; n     for (int i = 1; i < sml.size(); i++) { n       printf( ? %d %d n , mn, sml[i]); n       fflush(stdout); n       char c[5]; n       scanf( %s , c); n       if (c[0] ==  > ) { n         mn = sml[i]; n       } n     } n     int mx = big[0]; n     for (int i = 1; i < big.size(); i++) { n       printf( ? %d %d n , mx, big[i]); n       fflush(stdout); n       char c[5]; n       scanf( %s , c); n       if (c[0] ==  < ) { n         mx = big[i]; n       } n     } n     printf( ! %d %d n , mn, mx); n     fflush(stdout); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int mpow(int base, int exp); n void ipgraph(int m); n void dfs(int u, int par); n const long long mod = 1000000007 * 1LL * 1000000007; n const int N = 1e6 + 3, M = N; n vector<int> g[N]; n int a[N]; n long long h[N], val[N]; n vector<pair<int, int> > E; n long long r() { return rand() * 32000 + rand(); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int i, n, k, j, m; n   cin >> n >> m; n   srand(1234); n   for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) n     h[i] = (r() * r() + r() + r()) % mod; n   ipgraph(m); n   long long ans = 0; n   for (pair<int, int> e : E) { n     int u = e.first, v = e.second; n     val[u] = (val[u] + mod - h[v]) % mod; n     val[v] = (val[v] + mod - h[u]) % mod; n     ans += val[u] == val[v]; n     val[u] = (val[u] + h[v]) % mod; n     val[v] = (val[v] + h[u]) % mod; n   } n   vector<long long> fin; n   for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) n     fin.push_back(val[i]); n   sort(fin.begin(), fin.end()); n   long long c = 1; n   for (i = 1; 1 < fin.size() ? i < fin.size() : i > fin.size(); n        1 < fin.size() ? i += 1 : i -= 1) { n     if (fin[i] == fin[i - 1]) n       c++; n     else n       ans += c * (c - 1) / 2, c = 1; n   } n   ans += c * (c - 1) / 2; n   cout << ans << endl; n   return 0; n } n int mpow(int base, int exp) { n   base %= mod; n   int result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long)result * base) % mod; n     base = ((long long)base * base) % mod; n     exp >>= 1; n   } n   return result; n } n void ipgraph(int m) { n   int i, u, v; n   for (i = 0; i < m; i++) { n     cin >> u >> v; n     val[u] = (val[u] + h[v]) % mod; n     val[v] = (h[u] + val[v]) % mod; n     E.push_back({u, v}); n     u++, v++; n     g[u - 1].push_back(v - 1); n     g[v - 1].push_back(u - 1); n   } n } n void dfs(int u, int par) { n   for (int v : g[u]) { n     if (v == par) continue; n     dfs(v, u); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     int card_to_person = n / k; n     if (card_to_person >= m) { n       cout << m <<   n ; n       continue; n     } n     int tmp = (m - card_to_person) / (k - 1); n     if ((m - card_to_person) % (k - 1) != 0) { n       tmp++; n     } n     cout << card_to_person - tmp <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1000005; n int N; n vector<int> G[MAX_N]; n int cyid[MAX_N], Q[MAX_N]; n vector<vector<int>> cycles, newcycles; n void find_cycle(int s, int curid) { n   cyid[s] = curid; n   cycles.back().push_back(s); n   for (int i = 0; i < G[s].size(); i++) { n     if (cyid[G[s][i]] == -1) { n       find_cycle(G[s][i], curid); n     } n   } n } n void get_cycles() { n   fill(cyid + 1, cyid + N + 1, -1); n   int nCycles = 0; n   for (int i = 1; i <= N; i++) { n     if (cyid[i] == -1) { n       cycles.push_back(vector<int>()); n       find_cycle(i, ++nCycles); n     } n   } n } n void construct_root_permutation() { n   int numEven = 0, numOdd = 0; n   for (vector<int> p : cycles) { n     if (p.size() % 2 == 0) n       numEven++; n     else n       numOdd++; n   } n   if (numEven % 2 == 1) { n     printf( -1 n ); n     return; n   } n   sort(cycles.begin(), cycles.end(), n        [](vector<int> a, vector<int> b) { return a.size() < b.size(); }); n   for (int i = 0; i < cycles.size();) { n     if (cycles[i].size() % 2 == 1) { n       if (cycles[i].size() == 1) n         newcycles.push_back({cycles[i][0]}); n       else { n         vector<int> newcyk; n         int ptr1 = 0, ptr2 = (cycles[i].size() - 1) / 2 + 1; n         for (int j = 0; j < cycles[i].size() / 2; j++) { n           newcyk.push_back(cycles[i][ptr1++]); n           newcyk.push_back(cycles[i][ptr2++]); n         } n         newcyk.push_back(cycles[i][ptr1]); n         newcycles.emplace_back(newcyk); n       } n       i++; n     } else { n       if (i + 1 < cycles.size()) { n         if (cycles[i].size() != cycles[i + 1].size()) { n           printf( -1 n ); n           return; n         } else { n           vector<int> merged; n           for (int j = 0; j < cycles[i].size(); j++) { n             merged.push_back(cycles[i][j]); n             merged.push_back(cycles[i + 1][j]); n           } n           newcycles.emplace_back(merged); n           i += 2; n         } n       } else { n         printf( -1 n ); n         return; n       } n     } n   } n   for (int i = 0; i < newcycles.size(); i++) { n     if (newcycles[i].size() == 1) n       Q[newcycles[i][0]] = newcycles[i][0]; n     else { n       newcycles[i].push_back(newcycles[i][0]); n       for (int j = 0; j < newcycles[i].size() - 1; j++) { n         int next = newcycles[i][j + 1]; n         Q[newcycles[i][j]] = next; n       } n     } n   } n   for (int i = 1; i <= N; i++) printf( %d  , Q[i]); n   printf(  n ); n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     int pi; n     scanf( %d , &pi); n     G[i].push_back(pi); n   } n   get_cycles(); n   construct_root_permutation(); 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 const double EPS = 1e-8; n const int MOD = 998244353; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n int mod = MOD; n struct ModInt { n   unsigned val; n   ModInt() : val(0) {} n   ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} n   ModInt pow(long long exponent) { n     ModInt tmp = *this, res = 1; n     while (exponent > 0) { n       if (exponent & 1) res *= tmp; n       tmp *= tmp; n       exponent >>= 1; n     } n     return res; n   } n   ModInt &operator+=(const ModInt &rhs) { n     if ((val += rhs.val) >= mod) val -= mod; n     return *this; n   } n   ModInt &operator-=(const ModInt &rhs) { n     if ((val += mod - rhs.val) >= mod) val -= mod; n     return *this; n   } n   ModInt &operator*=(const ModInt &rhs) { n     val = (unsigned long long)val * rhs.val % mod; n     return *this; n   } n   ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } n   bool operator==(const ModInt &rhs) const { return val == rhs.val; } n   bool operator!=(const ModInt &rhs) const { return val != rhs.val; } n   bool operator<(const ModInt &rhs) const { return val < rhs.val; } n   bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } n   bool operator>(const ModInt &rhs) const { return val > rhs.val; } n   bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } n   ModInt operator-() const { return ModInt(-val); } n   ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } n   ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } n   ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } n   ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } n   friend ostream &operator<<(ostream &os, const ModInt &rhs) { n     return os << rhs.val; n   } n   friend istream &operator>>(istream &is, ModInt &rhs) { n     long long x; n     is >> x; n     rhs = ModInt(x); n     return is; n   } n  n  private: n   ModInt inv() const { n     unsigned a = val, b = mod; n     int x = 1, y = 0; n     while (b) { n       unsigned tmp = a / b; n       a -= tmp * b; n       swap(a, b); n       x -= tmp * y; n       swap(x, y); n     } n     return ModInt(x); n   } n }; n ModInt abs(const ModInt &x) { return x.val; } n struct Combinatorics { n   Combinatorics(int MAX = 5000000) { n     MAX <<= 1; n     fact.resize(MAX + 1); n     fact_inv.resize(MAX + 1); n     fact[0] = 1; n     for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i; n     fact_inv[MAX] = ModInt(1) / fact[MAX]; n     for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; n   } n   ModInt nCk(int n, int k) { n     if (n < 0 || n < k || k < 0) return ModInt(0); n     return fact[n] * fact_inv[k] * fact_inv[n - k]; n   } n   ModInt nPk(int n, int k) { n     if (n < 0 || n < k || k < 0) return ModInt(0); n     return fact[n] * fact_inv[n - k]; n   } n   ModInt nHk(int n, int k) { n     if (n < 0 || k < 0) return ModInt(0); n     return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); n   } n  n  private: n   vector<ModInt> fact, fact_inv; n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = (0); i < (n); ++i) cin >> a[i]; n   vector<int> w(n); n   for (int i = (0); i < (n); ++i) cin >> w[i]; n   int dl = 0, l = 0; n   for (int i = (0); i < (n); ++i) (a[i] == 0 ? dl : l) += w[i]; n   vector<vector<ModInt> > dp(m + 1, vector<ModInt>(m + 1, 0)); n   dp[0][0] = 1; n   for (int i = (0); i < (m); ++i) n     for (int j = (0); j < (m); ++j) { n       int sum = l + dl - j + (i - j); n       if (j > dl || j > i || sum <= 0) continue; n       dp[i + 1][j + 1] += dp[i][j] * (dl - j) / sum; n       dp[i + 1][j] += dp[i][j] * (l + (i - j)) / sum; n     } n   vector<ModInt> ans(2, 0); n   ModInt tmp = 1; n   for (int i = (0); i < (dl + 1); ++i) { n     if (i > m) break; n     ans[0] += tmp * dp[m][i]; n     if (i < dl) tmp *= (ModInt(1) - ModInt(1) / (dl - i)); n   } n   tmp = 1; n   for (int i = (0); i < (m + 1); ++i) { n     ans[1] += tmp * dp[m][m - i]; n     tmp *= (ModInt(1) + ModInt(1) / (l + i)); n   } n   for (int i = (0); i < (n); ++i) cout << ans[a[i]] * w[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using vv = vector<vector<T>>; n template <class T> n inline bool MX(T &l, const T &r) { n   return l < r ? l = r, 1 : 0; n } n template <class T> n inline bool MN(T &l, const T &r) { n   return l > r ? l = r, 1 : 0; n } n const long long MOD = 1e9 + 7; n int N; n long long C[114514]; n long long T[114514]; n vector<long long> ds, es; n int main() { n   scanf( %d , &N); n   for (int(i) = 0; (i) < (N); ++(i)) { n     scanf( %lld , &C[i]); n     if (i > 0) ds.emplace_back(C[i] - C[i - 1]); n   } n   for (int(i) = 0; (i) < (N); ++(i)) { n     scanf( %lld , &T[i]); n     if (i > 0) es.emplace_back(T[i] - T[i - 1]); n   } n   sort((ds).begin(), (ds).end()); n   sort((es).begin(), (es).end()); n   if (C[0] != T[0]) { n     puts( No ); n     return 0; n   } n   assert((int)ds.size() == N - 1); n   assert((int)es.size() == N - 1); n   for (int(i) = 0; (i) < (N - 1); ++(i)) { n     if (ds[i] != es[i]) { n       puts( No ); n       return 0; n     } n   } n   puts( Yes ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, M, x, y; n   cin >> N >> M >> x >> y; n   x--; n   y--; n   cout << x + 1 <<     << y + 1 << endl; n   for (int i = 1; i < x; i++) cout << i + 1 <<     << y + 1 << endl; n   for (int i = x + 1; i < N; i++) cout << i + 1 <<     << y + 1 << endl; n   cout << 1 <<     << y + 1 << endl; n   int uneven = 0; n   for (int j = 0; j < M; j++) { n     if (j == y) { n       uneven = 1; n       continue; n     } n     if (j % 2 == uneven) { n       for (int i = 0; i < N; i++) { n         cout << i + 1 <<     << j + 1 << endl; n       } n     } n     if (j % 2 != uneven) { n       for (int i = N - 1; i > -1; i--) { n         cout << i + 1 <<     << j + 1 << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int a[10]; n int main() { n   int T, N, ans; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &N); n     ans = 1; n     for (int i = 0; i <= 30; i++) n       if (N & (1 << i)) ans *= 2; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, m; n     scanf( %d%d , &n, &m); n     int x, ans = 0; n     for (int i = 0; i < n; i++) { n       scanf( %d , &x); n       ans += x; n     } n     if (ans == m) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2000 * 100 + 10; n map<int, int> val; n int a[maxN]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     val[a[i]]++; n   } n   sort(a, a + n); n   n = unique(a, a + n) - a; n   cout << a[n - 1] - a[0] <<    ; n   if (n == 1) n     cout << 1LL * val[a[0]] * (val[a[0]] - 1) / 2 << endl; n   else n     cout << 1LL * val[a[0]] * val[a[n - 1]] << endl; n } n


#include <bits/stdc++.h> n #define ff first n #define ss second n #define endl   n  n using namespace std; n const long long INF = (long long) 1e18; n const int mod = (int) 1e9+7; n const int MAXN = (int) 3.5e4+5; n  n typedef long long ll; n typedef unsigned long long ull; n typedef pair<int,int> pii; n typedef pair<ll,ll> pll; n ll n, lst[MAXN], nx[MAXN], pr[MAXN], a[MAXN]; n set<ll> idx[MAXN]; n ll dp[105][MAXN]; n ll t[101][4*MAXN], lazy[101][4*MAXN]; n int x; n void push(int v){ n     if(lazy[x][v] == 0) n         return; n     lazy[x][2*v] += lazy[x][v]; n     lazy[x][2*v + 1] += lazy[x][v]; n     t[x][2*v] += lazy[x][v]; n     t[x][2*v + 1] += lazy[x][v]; n     lazy[x][v] = 0; n } n  n void upd(int v, int tl, int tr, int l, int r, ll val){ n     if(l > r) n         return; n     if(tl == l && tr == r){ n         t[x][v] += val; n         lazy[x][v] += val; n     }else{ n         push(v); n         int tm = (tl + tr) / 2; n         upd(2*v, tl, tm, l, min(tm, r), val); n         upd(2*v+1, tm + 1, tr, max(tm + 1, l), r, val); n         t[x][v] = max(t[x][2*v], t[x][2*v + 1]); n     } n } n  n ll que(int v, int tl, int tr, int l, int r){ n     if(l > r) n         return -INF; n     if(tl == l && tr == r) n         return t[x][v]; n     else { n         push(v); n         int tm = (tl + tr) / 2; n         return max(que(2*v, tl, tm, l, min(tm, r)), que(2*v+1, tm + 1, tr, max(tm + 1, l), r)); n     } n } n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(nullptr);cout.tie(nullptr); n  n     #ifdef Local n         freopen( C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt , r ,stdin); n         freopen( C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt , w ,stdout); n     #endif n     ll k; n     cin>>n>>k; n     ll top = 0; n     for(ll i = 1; i <= n; i++){ n         cin>>a[i]; n         if(lst[a[i]] != 0) n             top += i - lst[a[i]]; n         pr[i] = lst[a[i]]; n         lst[a[i]] = i; n     } n     memset(lst, 0x7f, sizeof(lst)); n  n     for(int i = n; i >= 1; i--){ n         nx[i] = min(n + 1, lst[a[i]]); n         lst[a[i]] = i; n     } n  n     memset(dp, 0xf7, sizeof(dp)); n     dp[0][0] = 0; n     ll mx = 0; n     for(int i = 1; i < k; i++){ n         x = i - 1; n         ll cev = 0; n         for(ll j = 1; j <= n; j++){ n             if(j != 1) n                 if(pr[j - 1] != 0) n                     upd(1, 0, n, pr[j - 1] + 1, j - 1, j - 1 - pr[j - 1]); n             dp[i][j] = que(1, 0, n, 0, j - 1) + cev; n             upd(1, 0, n, j, j, dp[i - 1][j] - cev); n             //cerr<<dp[i][j]<<   ; n             mx = max(mx, dp[i][j]); n  n             if(pr[j] != 0) n                 cev -= j - pr[j]; n             if(nx[j] != n + 1) n                 cev += nx[j] - j; n         } n     } n  n     cout<<top - mx<<endl; n  n  n     #ifdef Local n         cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<<   milliseconds  ; n     #endif n }


#include <bits/stdc++.h> n using namespace std; n void sort(int temp[2000], int start, int end) { n   int flag; n   for (int i = start; i < end - 1; i++) { n     for (int j = start; j < end - i - 1; j++) { n       if (temp[j] < temp[j + 1]) { n         flag = temp[j]; n         temp[j] = temp[j + 1]; n         temp[j + 1] = flag; n       } n     } n   } n } n int main() { n   int n, k, arr[2000], count = 0; n   cin >> n; n   cin >> k; n   cout <<   n ; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, 0, n); n   for (int i = 0; i < n; i += k) count = count + (2 * (arr[i] - 1)); n   cout <<   n  << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int m, n, t, ans; n pair<int, int> a[N]; n vector<pair<int, int>> v; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &m); n     ans = 0; n     v.clear(); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i].first), a[i].second = i; n     sort(a + 1, a + n + 1); n     for (int i = 1; i < n; i++) { n       v.push_back(make_pair(a[i].second, a[i + 1].second)); n       ans += a[i].first + a[i + 1].first; n     } n     v.push_back(make_pair(a[n].second, a[1].second)); n     ans += a[n].first + a[1].first; n     if (v.size() > m || n == 2) { n       puts( -1 ); n       continue; n     } n     for (int i = v.size() + 1; i <= m; i++) { n       v.push_back(make_pair(a[1].second, a[2].second)); n       ans += a[1].first + a[2].first; n     } n     printf( %d n , ans); n     for (int i = 0; i < v.size(); i++) n       printf( %d %d n , v[i].first, v[i].second); n   } 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; n     cin >> a >> b; n     cout << (a ^ b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string A, B; n bool is_divisor(const string &a, const string &b) { n   int sa = a.size(), sb = b.size(); n   for (int i = 0; i < sb; i += sa) { n     if (b.substr(i, sa) != a) return false; n   } n   return true; n } n int process(const string &a, const string &b) { n   int sz = a.size(); n   vector<int> divi; n   for (int i = 1; i < sqrt(sz) + 1; ++i) { n     if (sz % i == 0) { n       divi.push_back(i); n       if (i * i != sz) divi.push_back(sz / i); n     } n   } n   set<string> s; n   for (int i = 0; i < divi.size(); ++i) n     if (is_divisor(a.substr(0, divi[i]), a)) s.insert(a.substr(0, divi[i])); n   sz = b.size(); n   divi.clear(); n   for (int i = 1; i < sqrt(sz) + 1; ++i) { n     if (sz % i == 0) { n       divi.push_back(i); n       if (i * i != sz) divi.push_back(sz / i); n     } n   } n   int ans = 0; n   set<string> ss; n   for (int i = 0; i < divi.size(); ++i) n     if (is_divisor(b.substr(0, divi[i]), b)) ss.insert(b.substr(0, divi[i])); n   for (string a : s) n     if (ss.find(a) != ss.end()) ++ans; n   return ans; n } n int main() { n   char a[100001]; n   scanf(  %s , a); n   A = string(a); n   scanf(  %s , a); n   B = string(a); n   cout << process(A, B) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   scanf( %lld , &n); n   printf( 1  ); n   if ((n - 1) % 3 == 1) n     printf( 2 %lld , n - 3); n   else n     printf( 1 %lld , n - 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> mmap; n map<string, string>::iterator it; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string a; n     string b; n     cin >> a >> b; n     if (mmap[b] !=   ) { n       for (it = mmap.begin(); it != mmap.end(); it++) { n         if (it->first == b) { n           mmap.erase(it); n           break; n         } n       } n     } n     int flag = 0; n     for (it = mmap.begin(); it != mmap.end(); it++) { n       if (it->second == a) { n         it->second = b; n         flag = 1; n         break; n       } n     } n     if (!flag) mmap[a] = b; n   } n   int flag = 0; n   for (it = mmap.begin(); it != mmap.end(); it++) n     if (it->second ==   ) flag++; n   cout << mmap.size() - flag << endl; n   for (it = mmap.begin(); it != mmap.end(); it++) n     if (it->second !=   ) cout << it->first <<     << it->second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100010]; n long long int weight[100010], add[100010], sub[100010]; n int n; n void dfs(int mom, int u) { n   for (vector<int>::iterator v = g[u].begin(); v != g[u].end(); v++) n     if (*v != mom) { n       dfs(u, *v); n       add[u] = max(add[*v], add[u]); n       sub[u] = max(sub[u], sub[*v]); n     } n   weight[u] += add[u] - sub[u]; n   if (weight[u] < 0) n     add[u] -= weight[u]; n   else n     sub[u] += weight[u]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     g[u].push_back(v), g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) scanf( %I64d , weight + i); n   dfs(-1, 1); n   printf( %I64d n , add[1] + sub[1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const long long llinf = (long long)3e18; n const int N = (int)1e5 + 111; n const long double PI = (long double)acos(-1); n int main() { n   int n, cur, num, ans = 0; n   scanf( %d , &n); n   vector<int> a(n); n   vector<char> us(n, 0); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; i++) n     if (us[i] == 0) { n       cur = i; n       num = 0; n       while (cur < n) { n         if (us[cur] == 1 || a[cur] < num) n           cur++; n         else { n           num++; n           us[cur] = 1; n         } n       } n       ans++; n     } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n int x[10]; n int cp, dp; n using namespace std; n void print() { n   for (int i = 0; i < 10; ++i) { n     cout << x[i]; n     if (i == 9) n       cout <<   n ; n     else n       cout <<    ; n   } n } n int pp(char a, int &dp) { n   if (a ==  < ) { n     dp = -1; n     return -2; n   } else if (a ==  > ) { n     dp = 1; n     return -2; n   } else n     return a -  0 ; n } n void work(int l, int r, string ch1) { n   cp = l; n   dp = 1; n   int temp; n   bool ins = true; n   while (cp >= l && cp <= r) { n     temp = pp(ch1[cp], dp); n     if (temp >= 0) { n       x[temp]++; n       ch1[cp]--; n       cp += dp; n     } else { n       if (temp == -1) cp += dp; n       if (temp == -2) { n         int cpt = cp; n         cp += dp; n         if (cp < l || cp > r) break; n         int temp1 = pp(ch1[cp], dp); n         while (temp1 == -1) { n           cp += dp; n           temp1 = pp(ch1[cp], dp); n         } n         if (cp < l || cp > r) break; n         if (temp1 == -2) ch1[cpt] =  0  - 1; n         if (temp1 >= 0) x[temp1]++, ch1[cp]--, cp += dp; n       } n     } n   } n   print(); n } n int main() { n   int n, q; n   string ch; n   cin >> n >> q; n   cin >> ch; n   for (int i = 0; i < q; ++i) { n     int l, r; n     cin >> l >> r; n     l--, r--; n     memset(x, 0, sizeof(x)); n     if (l == r) { n       int pk = ch[l]; n       if (pk ==  <  || pk ==  > ) n         print(); n       else n         x[pk -  0 ]++, print(); n     } else n       work(l, r, ch); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Swap(int &a, int &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n int Max(int a, int b) { n   if (a > b) return a; n   return b; n } n int Scan() { n   int res = 0; n   char ch; n   ch = getchar(); n   if (ch >=  0  && ch <=  9 ) res = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) res = res * 10 + (ch -  0 ); n   return res; n } n int main() { n   int T = Scan(); n   int X = 0, Y = 0; n   while (T--) { n     char c; n     cin >> c; n     int a = Scan(); n     int b = Scan(); n     if (a < b) Swap(a, b); n     if (c ==  + ) n       X = Max(X, a), Y = Max(Y, b); n     else if (a >= X && b >= Y) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   cout << s[0]; n   for (int i = 1; i < s.size() - 1; i += 2) { n     cout << s[i]; n   } n   cout << s.back(); n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t_; n   cin >> t_; n   while (t_--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n int pre[1000010]; n int GetN(int n) { n   if (pre[n] > 0) return pre[n]; n   if (n < 10) { n     pre[n] = n; n     return n; n   } n   int temp = n; n   int product = 1; n   while (n > 0) { n     int rem = n % 10; n     product *= rem == 0 ? 1 : rem; n     n = n / 10; n   } n   return pre[temp] = GetN(product); n } n int dp[1000010][10]; n void Precal() { n   for (int i = 1; i < 1000010; i++) { n     int res = GetN(i); n     dp[i][res] = 1; n     for (int k = 1; k <= 9; k++) { n       dp[i][k] = dp[i][k] + dp[i - 1][k]; n       if (dp[i][0] == 1) { n         cout << i <<     << k; n         int v; n         cin >> v; n         exit(0); n       } n     } n   } n } n int main() { n   Precal(); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int l, r, k; n     cin >> l >> r >> k; n     cout << dp[r][k] - dp[l - 1][k] << endl; n     ; n   } n   int v; n   cin >> v; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(3e5) + 228, mod = int(1e9) + 7; n const long long inf = (long long)(1e16); n int n; n pair<int, int> p[N]; n pair<long long, long long> t[2 * N], dp[N]; n pair<long long, long long> merg(pair<long long, long long> a, n                                 pair<long long, long long> b) { n   if (a.first > b.first) return b; n   if (a.first < b.first) return a; n   a.second += b.second; n   if (a.second >= mod) a.second -= mod; n   return a; n } n void upd(int v, pair<long long, long long> a) { n   v += n - 1; n   t[v] = merg(t[v], a); n   while (v > 1) { n     v >>= 1; n     t[v] = merg(t[v + v], t[v + v + 1]); n   } n } n pair<long long, long long> get(int l, int r) { n   l += n - 1; n   r += n - 1; n   pair<long long, long long> res = make_pair(inf, 0); n   while (l <= r) { n     if (l & 1) res = merg(res, t[l]); n     if (!(r & 1)) res = merg(res, t[r]); n     l = (l + 1) >> 1; n     r = (r - 1) >> 1; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &p[i].first, &p[i].second); n   } n   for (int i = 1; i <= 2 * n; i++) { n     t[i] = make_pair(inf, 0); n   } n   sort(p + 1, p + n + 1); n   for (int i = 1; i <= n; i++) { n     int l = 0, r = i; n     while (r - l > 1) { n       int mid = (l + r) >> 1; n       if (p[mid].first <= p[i].second) n         l = mid; n       else n         r = mid; n     } n     if (l == 0) { n       dp[i] = make_pair(p[i].second, 1); n     } else { n       dp[i] = get(1, l); n       dp[i].first += p[i].second; n     } n     upd(i, make_pair(dp[i].first - p[i].first, dp[i].second)); n   } n   int mx = 0; n   pair<long long, long long> ans = make_pair(inf, 0); n   for (int i = n; i > 0; i--) { n     if (mx < p[i].first) ans = merg(ans, dp[i]); n     mx = max(mx, p[i].second); n   } n   printf( %lld , ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     while (a[i] % 2 == 0) a[i] /= 2; n     while (a[i] % 3 == 0) a[i] /= 3; n   } n   int flag = 1; n   int val = a[0]; n   for (int i = 1; i < n; i++) { n     if (a[i] != val) flag = 0; n   } n   if (flag) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mx = INT_MIN, n, a[10]; n char x; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= 7; j++) { n       cin >> x; n       a[j] += x - 48; n     } n   } n   for (int i = 1; i <= 7; i++) n     if (mx < a[i]) mx = a[i]; n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a, b; n   int t = n / 2; n   if (n % 2 == 1) { n     cout << t <<     << t + 1; n   } else if (n % 4 == 2) { n     cout << t - 2 <<     << t + 2; n   } else if (n % 4 == 0) { n     cout << t - 1 <<     << t + 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int as[10001]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) as[i] = 1; n   as[n] = 0; n   for (int j = 0; j < k; ++j) { n     for (int i = 1; i <= n - (1 << (j + 1)); ++i) { n       if (as[i] != n - i) { n         cout << n - (1 << j) <<    ; n         as[i] += as[n - (1 << j)]; n       } else { n         cout << n <<    ; n       } n     } n     int x = 1; n     if (n - (1 << (j + 1)) + 1 > 0) { n       for (int i = n - (1 << (j + 1)) + 1; i <= n; ++i) { n         if (as[i] != n - i) { n           cout << n - (1 << j) + x <<    ; n           as[i] += as[n - (1 << j) + x]; n           ++x; n         } else { n           cout << n <<    ; n         } n       } n     } else { n       for (int i = 1; i <= n; ++i) { n         if (as[i] != n - i) { n           cout << i + as[i] <<    ; n           as[i] += as[i + as[i]]; n         } else { n           cout << n <<    ; n         } n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   long long c[n], s[n]; n   for (int i = 0; i < n; i++) { n     cin >> c[i] >> s[i]; n   } n   long long dp[m + 1]; n   dp[m] = 0; n   for (int i = m - 1; i >= 0; i--) { n     dp[i] = m - i; n     for (int j = 0; j < n; j++) { n       long long l = c[j] - s[j]; n       l = max(0LL, l); n       long long r = c[j] + s[j]; n       r = min(r, m); n       if (i + 1 >= l && i + 1 <= r) { n         dp[i] = min(dp[i], dp[i + 1]); n         break; n       } n       if (i < l) { n         long long gap = l - i - 1; n         long long rr = r + gap; n         rr = min(m, rr); n         dp[i] = min(dp[i], gap + dp[rr]); n       } n     } n   } n   cout << dp[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[] = {1, 2, 4, 3}; n int b[] = {1, 3, 4, 2}; n int c[] = {1, 4, 1, 4}; n int d[] = {1, 1, 1, 1}; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   int rem = 0; n   for (int i = 0; i < n; i++) { n     rem = (rem * 10 + s[i] -  0 ) % 4; n   } n   int ans = a[rem] + b[rem] + c[rem] + d[rem]; n   ans %= 5; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1), eps = 1e-6, INF = 1e16; n bool isEqual(long double x, long double y) { return fabs(x - y) < eps; } n int sgn(long double sc) { n   if (isEqual(sc, 0)) return 0; n   if (sc < eps) return -1; n   return 1; n } n struct pnt { n   long double x, y; n   pnt() {} n   pnt(long double _x, long double _y) { n     x = _x; n     y = _y; n   } n   pnt(pnt A, pnt B) { n     x = B.x - A.x; n     y = B.y - A.y; n   } n   pnt operator+(pnt p) { return pnt(x + p.x, y + p.y); } n   pnt operator-(pnt p) { return pnt(x - p.x, y - p.y); } n   void operator+=(pnt p) { x += p.x, y += p.y; } n   void operator-=(pnt p) { x -= p.x, y -= p.y; } n   pnt operator*(long double sc) { return pnt(x * sc, y * sc); } n   pnt operator/(long double sc) { return pnt(x / sc, y / sc); } n   long double operator*(pnt p) { return x * p.x + y * p.y; } n   long double operator^(pnt p) { return x * p.y - p.x * y; } n   bool operator==(pnt p) { return isEqual(x, p.x) && isEqual(y, p.y); } n   bool operator!=(pnt p) { return !isEqual(x, p.x) || !isEqual(y, p.y); } n   void normalize() { n     long double sq = sqrt(x * x + y * y); n     x /= sq; n     y /= sq; n     return; n   } n   long double dist2(pnt p) { n     return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y); n   } n   long double dist(pnt p) { return sqrt(dist2(p)); } n   bool isParallel(pnt p) { return isEqual(pnt(x, y) ^ p, 0); } n }; n struct line { n   long double a, b, c; n   line() {} n   line(long double _a, long double _b, long double _c) { n     a = _a, b = _b, c = _c; n   } n   line(pnt p1, pnt p2) { n     a = p1.y - p2.y; n     b = p2.x - p1.x; n     c = -(a * p1.x + b * p1.y); n   } n   pnt getPerpendicularVector() { return pnt(a, b); } n   pnt getParallelVector() { return pnt(-b, a); } n   line getPerpendicularLine() { return line(-b, a, c); } n   bool isParallel(line l) { n     return getParallelVector().isParallel(l.getParallelVector()); n   } n   pnt intersect(line l) { n     if (isParallel(l)) { n       return pnt(INF, INF); n     } n     long double q = a * l.b - b * l.a; n     return pnt((b * l.c - c * l.b) / q, (c * l.a - a * l.c) / q); n   } n   long double dist(pnt p) { n     return fabs(a * p.x + b * p.y + c) / sqrt(a * a + b * b); n   } n   pnt basePerp(pnt p) { n     return intersect(line(p, p + getPerpendicularVector())); n   } n   bool isOnLine(pnt p) { return isEqual(a * p.x + b * p.y + c, 0); } n }; n int main() { n   int n, d; n   cin >> n >> d; n   pnt A(0, d), B(d, 0), C(n, n - d), D(n - d, n); n   pnt BA(B, A), BC(B, C), CD(C, D), DA(D, A), AC(A, C), DC(D, C); n   line BAl(B, A), BCl(B, C), CDl(C, D), DAl(D, A), ACl(A, C); n   int m, x, y; n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     pnt kuz(x, y), Bkuz(B, kuz), Dkuz(D, kuz); n     if ((((BA ^ Bkuz) < 0 && (BC ^ Bkuz) > 0) && n          (((DA ^ Dkuz) > 0) && ((DC ^ Dkuz) < 0))) || n         BAl.isOnLine(kuz) || BCl.isOnLine(kuz) || CDl.isOnLine(kuz) || n         DAl.isOnLine(kuz)) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n long long ans; n int son[100010][2], fa[100010], cnt[100010], val[100010]; n int is[100010]; n long long sumVal[100010], sumCnt[100010], sum[100010], all[100010], n     lazy[100010]; n void up(int p) { n   sumCnt[p] = sumCnt[son[p][0]] + sumCnt[son[p][1]] + cnt[p]; n   sumVal[p] = sumVal[son[p][0]] + sumVal[son[p][1]] + val[p]; n   sum[p] = sum[son[p][0]] + sum[son[p][1]] + 1LL * cnt[p] * val[p]; n } n void func(int p, long long v) { n   if (!p) return; n   all[p] += v * cnt[p]; n   lazy[p] += v; n } n void down(int p) { n   if (lazy[p]) { n     func(son[p][0], lazy[p]); n     func(son[p][1], lazy[p]); n     lazy[p] = 0; n   } n } n void Rotate(int x) { n   int y = fa[x], z = fa[y]; n   int o = son[y][0] == x; n   down(y), down(x); n   son[y][!o] = son[x][o], fa[son[x][o]] = y; n   son[x][o] = y, fa[y] = x; n   fa[x] = z; n   if (is[y]) n     is[y] = 0, is[x] = 1; n   else n     son[z][son[z][1] == y] = x; n   up(y); n } n void Splay(int x) { n   int y, z; n   down(x); n   while (!is[x]) { n     y = fa[x], z = fa[y]; n     if (is[y]) n       Rotate(x); n     else if ((son[y][0] == x) ^ (son[z][0] == y)) n       Rotate(x), Rotate(x); n     else n       Rotate(y), Rotate(x); n   } n   up(x); n } n int Access(int x) { n   int y = 0; n   for (; x; x = fa[x]) { n     Splay(x); n     is[son[x][1]] = 1; n     if (son[x][1]) { n       cnt[x] += sumCnt[son[x][1]]; n     } n     son[x][1] = y; n     if (y) { n       cnt[x] -= sumCnt[y]; n     } n     is[y] = 0; n     y = x; n     up(x); n   } n   return y; n } n int vv[100010], nxt[100010], h[100010], e; n void add(int u, int v) { vv[e] = v, nxt[e] = h[u], h[u] = e++; } n void dfs(int u, int f) { n   fa[u] = f, is[u] = 1, cnt[u] = 1; n   son[u][0] = son[u][1] = 0; n   all[u] = 1; n   for (int i = h[u]; i + 1; i = nxt[i]) { n     int v = vv[i]; n     dfs(v, u); n     all[u] += 1LL * cnt[v] * cnt[u] * 2; n     cnt[u] += cnt[v]; n   } n   ans += all[u] * val[u]; n   up(u); n } n void change(int x, long long v) { n   Splay(x); n   ans += (v - val[x]) * all[x]; n   val[x] = v; n   up(x); n } n int getFa(int x) { n   Splay(x); n   if (!son[x][0]) return fa[x]; n   int y = son[x][0]; n   while (son[y][1]) y = son[y][1]; n   return y; n } n void gao(int u, int v) { n   Access(u); n   Splay(v); n   if (!fa[v]) swap(u, v); n   int f = getFa(v); n   if (f == u) return; n   Access(f), Splay(f); n   cnt[f] -= sumCnt[v], up(f); n   fa[v] = 0; n   ans -= 2 * sum[f] * sumCnt[v]; n   func(f, -2 * sumCnt[v]); n   Access(u), Splay(u); n   ans += 2 * sum[u] * sumCnt[v]; n   func(u, 2 * sumCnt[v]); n   cnt[u] += sumCnt[v], up(u); n   fa[v] = u; n } n int main() { n   int n; n   int u; n   scanf( %d , &n); n   memset(h, -1, sizeof(h)), e = 0; n   for (int i = 2; i <= n; ++i) { n     scanf( %d , &u); n     add(u, i); n   } n   for (int i = 1; i <= n; ++i) scanf( %d , val + i); n   dfs(1, 0); n   int m, v; n   scanf( %d , &m); n   char c[3]; n   double mu = 1.0 * n * n; n   printf( %.10lf n , ans / mu); n   while (m--) { n     scanf( %s%d%d , c, &u, &v); n     if (c[0] ==  P ) n       gao(v, u); n     else n       change(u, v); n     printf( %.10lf n , ans / mu); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, day, dep[1000010], arr[1000010], cost[1000010], cel, from, n     coss; n long long mini[1000010], wylot[1000010], dolot[1000010], sum, inf = 1e7; n vector<pair<long long, int> > tab; n int ptr; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   inf *= inf; n   for (int a = 1; a <= m; a++) { n     cin >> day >> dep[a] >> arr[a] >> cost[a]; n     tab.push_back(make_pair(day, a)); n   } n   sort(tab.begin(), tab.end()); n   ptr = 0; n   for (int a = 1; a <= n; a++) mini[a] = inf; n   sum = n * inf; n   dolot[0] = n * inf; n   for (int a = 1; a <= 1e6; a++) { n     while (ptr < tab.size() && tab[ptr].first <= a) { n       cel = arr[tab[ptr].second]; n       from = dep[tab[ptr].second]; n       coss = cost[tab[ptr].second]; n       if (cel != 0) { n         ptr++; n         continue; n       } n       if (coss < mini[from]) { n         sum -= mini[from]; n         mini[from] = coss; n         sum += coss; n       } n       ptr++; n     } n     dolot[a] = sum; n   } n   reverse(tab.begin(), tab.end()); n   ptr = 0; n   for (int a = 1; a <= n; a++) mini[a] = inf; n   sum = n * inf; n   wylot[(int)1e6 + 1] = n * inf; n   for (int a = 1e6; a >= 0; a--) { n     while (ptr < tab.size() && tab[ptr].first >= a) { n       cel = arr[tab[ptr].second]; n       from = dep[tab[ptr].second]; n       coss = cost[tab[ptr].second]; n       if (cel == 0) { n         ptr++; n         continue; n       } n       if (coss < mini[cel]) { n         sum -= mini[cel]; n         mini[cel] = coss; n         sum += coss; n       } n       ptr++; n     } n     wylot[a] = sum; n   } n   long long ans = inf; n   for (int a = 1; a <= 1e6 - k; a++) { n     if (dolot[a - 1] < n * inf && wylot[a + k] < n * inf) n       ans = min(ans, dolot[a - 1] + wylot[a + k]); n   } n   if (ans == inf) n     cout << -1; n   else n     cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[3][11]; n int sum[11]; n int x, len, i; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) n     for (int j = 0; j < 6; j++) { n       scanf( %d , &x); n       a[i][x]++; n       sum[x]++; n     } n   for (i = 1; i < 1000; i++) { n     if (i < 10) { n       if (sum[i]) n         continue; n       else n         break; n     } else if (i < 100) { n       int flag = 0; n       for (int j = 0; j < 3; j++) n         for (int k = 0; k < 3; k++) { n           if (k == j) continue; n           if (a[j][i % 10] && a[k][i / 10]) flag = 1; n         } n       if (flag == 0) break; n     } else { n       int flag = 0; n       for (int j = 0; j < 3; j++) { n         for (int k = 0; k < 3; k++) { n           if (k == j) continue; n           for (int l = 0; l < 3; l++) { n             if (l == k || l == j) continue; n             if (a[j][i % 10] && a[k][i / 10 % 10] && a[l][i / 100]) flag = 1; n           } n         } n       } n       if (flag == 0) break; n     } n   } n   cout << i - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, mem[100004][28]; n char x, y; n string s; n map<pair<int, int>, int> m; n int dp(int i = 0, int j = 27) { n   if (i == (int)s.size()) return 0; n   int& ret = mem[i][j]; n   if (~ret) return ret; n   if (m[{(int)(s[i] -  a ), j}] != 1) { n     return ret = max(dp(i + 1, (int)(s[i] -  a )) + 1, dp(i + 1, j)); n   } else n     return ret = dp(i + 1, j); n } n int main() { n   cin >> s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     m[{(int)(x -  a ), (int)(y -  a )}] = 1; n     m[{(int)(y -  a ), (int)(x -  a )}] = 1; n   } n   memset(mem, -1, sizeof mem); n   cout << (int)s.size() - dp(); n   return 0; n } n


#include <bits/stdc++.h> n void answer(unsigned long long v) { std::cout << v <<   n ; } n void solve(unsigned n, unsigned k) { n   const auto s = n * (n - 1ull) / 2; n   if (k > n / 2) return answer(s); n   const auto m = n - 2 * k; n   const auto d = m * (m - 1ull) / 2; n   answer(s - d); n } n int main() { n   unsigned n, k; n   std::cin >> n >> k; n   solve(n, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a > b) return gcd(b, a % b); n   return gcd(a, b % a); n } n int main() { n   long long n, x, y; n   cin >> n >> x >> y; n   long long d = gcd(x, y); n   long long xd = x / d; n   long long yd = y / d; n   long long nok = x / d * y; n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> a; n     long long l = 0; n     long long r = (a / (x + y) + 2) * nok; n     while ((r - l) > 1) { n       long long m = (r + l) / 2; n       long long hits = m / yd + m / xd; n       if (hits >= a) n         r = m; n       else n         l = m; n     } n     if (((r % yd) == 0) && ((r % xd) == 0)) n       cout <<  Both n ; n     else if (r % yd == 0) n       cout <<  Vanya n ; n     else n       cout <<  Vova n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int ans, tot, head[N], ver[N], Next[N], val[N], n, m, deg[N]; n queue<int> q[2]; n bool vis[N]; n inline void add(int x, int y) { n   ver[++tot] = y; n   Next[tot] = head[x]; n   head[x] = tot; n   deg[y]++; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x++; n     y++; n     add(y, x); n   } n   for (int i = 1; i <= n; i++) n     if (!deg[i]) q[val[i]].push(i); n   while ((!q[0].empty()) || (!q[1].empty())) { n     while (!q[0].empty()) { n       int x = q[0].front(); n       q[0].pop(); n       vis[x] = true; n       for (int i = head[x]; i; i = Next[i]) { n         int y = ver[i]; n         if (vis[y]) continue; n         if (!(--deg[y])) q[val[y]].push(y); n       } n     } n     if (!q[1].empty()) { n       ans++; n       while (!q[1].empty()) { n         int x = q[1].front(); n         q[1].pop(); n         vis[x] = true; n         for (int i = head[x]; i; i = Next[i]) { n           int y = ver[i]; n           if (vis[y]) continue; n           if (!(--deg[y])) q[val[y]].push(y); n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 7050; n int dp[2][maxn]; n vector<int> g[2]; n int state[2][maxn]; n void dfs(int player, int cur, int n) { n   if (state[player][cur] == 1) return; n   state[player][cur] = 1; n   for (int i = 0; i < g[player].size(); i++) { n     int v = (cur + g[player][i]) % n; n     if (v == 0) { n       dp[player][cur] = 1; n       return; n     } n   } n   int cnt = 0; n   for (int i = 0; i < g[player].size(); i++) { n     int v = (cur + g[player][i]) % n; n     dfs(player ^ 1, v, n); n     if (dp[player ^ 1][v] == (-1)) { n       dp[player][cur] = 1; n       return; n     } n     if (dp[player ^ 1][v] == 1) { n       cnt++; n     } n   } n   if (cnt == g[player].size()) dp[player][cur] = (-1); n   return; n } n void WORK(int n) { n   for (int i = 0; i <= n; i++) dp[0][i] = dp[1][i] = 2; n   for (int k = 1; k <= 5; k++) { n     memset(state, 0, sizeof(state)); n     for (int i = n - 1; i >= 0; i--) { n       dfs(0, i % n, n); n       dfs(1, i % n, n); n     } n   } n } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < 2; i++) { n       g[i].clear(); n       int cnt; n       scanf( %d , &cnt); n       while (cnt--) { n         int x; n         scanf( %d , &x); n         g[i].push_back(x); n       } n     } n     WORK(n); n     for (int i = 0; i < 2; i++) { n       for (int j = 1; j < n; j++) { n         if (dp[i][j] == 1) n           printf( Win  ); n         else if (dp[i][j] == (-1)) n           printf( Lose  ); n         else n           printf( Loop  ); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n const int maxn = 1e6 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   int m; n   cin >> n >> m; n   int need = (n + 4 - 1) / 4; n   string a =  5 ; n   int tota = 5; n   while ((int)a.size() < need) { n     a +=  5 ; n     tota += 5; n   } n   string b =  5 ; n   int totb = 5; n   while ((int)b.size() < need) { n     b +=  4 ; n     totb += 4; n   } n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   cout << a <<   n ; n   cout << b <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, cnt; n map<int, int> g[200005]; n set<int> s, ns; n void dfs(int x) { n   vector<int> v; n   v.clear(); n   ns.clear(); n   for (set<int>::iterator it = s.begin(); it != s.end(); it++) n     if (!g[x][*it]) n       v.push_back(*it); n     else n       ns.insert(*it); n   s = ns; n   for (int i : v) dfs(i); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, u, v; i <= m; ++i) n     scanf( %d%d , &u, &v), g[u][v] = 1, g[v][u] = 1; n   for (int i = 1; i <= n; ++i) s.insert(i); n   for (; s.size();) { n     ++cnt; n     int t = *s.begin(); n     s.erase(t); n     dfs(t); n   } n   printf( %d n , cnt - 1); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int t, a, b, c, d, k; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c >> d >> k; n     int x = a / c; n     int y = b / d; n     if (a % c) x++; n     if (b % d) y++; n     if (x + y <= k) { n       cout << x <<     << k - x << endl; n     } else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, a[300000], b[300000]; n long long s; n map<long, long> x, y; n map<pair<long, long>, long> t; n int main() { n   cin >> n; n   for (long i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     x[a[i]]++; n     y[b[i]]++; n     pair<long, long> p; n     p.first = a[i]; n     p.second = b[i]; n     t[p]++; n   } n   for (long i = 0; i < n; i++) { n     pair<long, long> p; n     p.first = a[i]; n     p.second = b[i]; n     s += (x[a[i]] + y[b[i]] - 1 - t[p]); n   } n   s /= 2; n   cout << s; n } n