#include <bits/stdc++.h> n using namespace std; n char s[100005]; n struct node { n   int x, op; n } a[100005]; n bool cmp(node q, node w) { return q.x < w.x; } n int cnt; n bool vis[100005]; n int main() { n   int now, len, i; n   scanf( %d , &now); n   scanf( %s , s + 1); n   len = strlen(s + 1); n   s[0] =  + ; n   for (i = 1; i <= len; i++) { n     if (s[i] ==  a ) { n       cnt++; n       vis[i] = 1; n       if (i > 2 && s[i - 1] ==  +  && s[i - 2] ==  +  && !vis[i - 1] && n           !vis[i - 2]) { n         a[cnt].op = 1; n         vis[i - 1] = 1; n         vis[i - 2] = 1; n         if (s[i - 3] ==  + ) n           a[cnt].x = 1; n         else if (s[i - 3] ==  - ) n           a[cnt].x = -1; n         else if (s[i - 3] ==  * ) { n           a[cnt].x = 0; n           int j, flg = 1; n           for (j = i - 4; j >= 1; j--) n             if (s[j] <  0  || s[j] >  9 ) { n               if (s[j] ==  - ) flg = -1; n               break; n             } n           for (j++; j <= i - 4; j++) a[cnt].x = 10 * a[cnt].x + s[j] - 48; n           a[cnt].x *= flg; n         } n       } else if (s[i + 1] ==  +  && s[i + 2] ==  +  && !vis[i + 1] && n                  !vis[i + 2]) { n         a[cnt].op = 0; n         vis[i + 1] = 1; n         vis[i + 2] = 1; n         if (s[i - 1] ==  + ) n           a[cnt].x = 1; n         else if (s[i - 1] ==  - ) n           a[cnt].x = -1; n         else if (s[i - 1] ==  * ) { n           a[cnt].x = 0; n           int j, flg = 1; n           for (j = i - 2; j >= 1; j--) n             if (s[j] <  0  || s[j] >  9 ) { n               if (s[j] ==  - ) flg = -1; n               break; n             } n           for (j++; j <= i - 2; j++) a[cnt].x = 10 * a[cnt].x + s[j] - 48; n           a[cnt].x *= flg; n         } n       } n     } n   } n   sort(a + 1, a + cnt + 1, cmp); n   long long ans = 0; n   for (i = 1; i <= cnt; i++) { n     if (a[i].op == 1) { n       now++; n       ans = 1ll * ans + 1ll * now * a[i].x; n     } else { n       ans = 1ll * ans + 1ll * now * a[i].x; n       now++; n     } n   } n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s1, s2, s3; n   cin >> n; n   while (n--) { n     cin >> s1 >> s2 >> s3; n     for (int i = 0; i < s1.length(); i++) { n       if (s3.at(i) != s2.at(i) && s3.at(i) != s1.at(i)) { n         cout <<  NO n ; n         break; n       } n       if (i == s1.length() - 1) cout <<  YES n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod1 = 1000000007; n long long int mod2 = 67280421310721; n long long int mod3 = 998244353; n long long int INF = 1e18; n long long int pow1(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n void sieve() { n   long long int n; n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n long long int pow2(long long int a, long long int b, long long int m) { n   a %= m; n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long int ncr(long long int n, long long int k) { n   long long int res = 1; n   if (k > n - k) k = n - k; n   for (int i = 0; i < k; ++i) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   long long int a[n + 1][m + 1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   } n   long long int dp[n + 1][m + 1]; n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= m; i++) dp[1][i] = 1; n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i][j] >= a[i - 1][j]) n         dp[i][j] = dp[i - 1][j] + 1; n       else n         dp[i][j] = 1; n     } n   } n   long long int best[n + 1]; n   memset(best, 0, sizeof(best)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       best[i] = max(best[i], dp[i][j]); n     } n   } n   long long int q; n   cin >> q; n   while (q--) { n     long long int l, r; n     cin >> l >> r; n     if (best[r] <= (r - l)) n       cout <<  No n ; n     else n       cout <<  Yes n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> a; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, x, ct, i; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     while ((x % 2) == 0) x = x / 2; n     while ((x % 3) == 0) x = x / 3; n     a.push_back(x); n   } n   ct = 0; n   for (i = 1; i < n; i++) n     if (a[i] == a[0]) ct++; n   if (ct == (n - 1)) n     cout <<  Yes n ; n   else n     cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n const long long INF = 2e9; n int n, m, dp[N], d[N], og[N], son[N], pr[N], top[N], num[N], sz, p[N], ans[N], n     a[N], b[N]; n int t[5 * N], ob[5 * N]; n vector<int> g[N]; n void build(int v, int l, int r) { n   ob[v] = -1; n   if (l == r) { n     if (l == 1) { n       t[v] = INF; n     } n     return; n   } n   int mid = (r + l) >> 1; n   build(v + v, l, mid); n   build(v + v + 1, mid + 1, r); n   t[v] = max(t[v + v], t[v + v + 1]); n } n inline void push(int v, int l, int r) { n   if (ob[v] != -1) { n     ob[v + v + 1] = t[v + v + 1] = ob[v]; n     int mid = (r + l) >> 1; n     ob[v + v] = t[v + v] = ob[v] - 2 * (r - mid); n     ob[v] = -1; n   } n } n int mx(int v, int l, int r, int tl, int tr) { n   push(v, l, r); n   if (l > r || l > tr || tl > r) { n     return -INF; n   } n   if (tl <= l && r <= tr) { n     return t[v]; n   } n   int mid = (r + l) >> 1; n   return max(mx(v + v, l, mid, tl, tr), mx(v + v + 1, mid + 1, r, tl, tr)); n } n int get(int v, int l, int r, int tl, int tr, int val) { n   push(v, l, r); n   if (t[v] <= val || l > r || l > tr || tl > r) { n     return -1; n   } n   if (l == r) { n     return og[l]; n   } n   int mid = (r + l) >> 1; n   int y = get(v + v + 1, mid + 1, r, tl, tr, val); n   return y == -1 ? get(v + v, l, mid, tl, tr, val) : y; n } n void update(int v, int l, int r, int tl, int tr, int val) { n   push(v, l, r); n   if (l > r || l > tr || tl > r) { n     return; n   } n   if (tl <= l && r <= tr) { n     t[v] = ob[v] = val - 2 * (tr - r); n     return; n   } n   int mid = (r + l) >> 1; n   update(v + v, l, mid, tl, tr, val); n   update(v + v + 1, mid + 1, r, tl, tr, val); n   t[v] = max(t[v + v], t[v + v + 1]); n } n void dfs(int v, int depth = 0) { n   dp[v] = 1; n   d[v] = depth; n   for (int to : g[v]) { n     dfs(to, depth + 1); n     pr[to] = v; n     dp[v] += dp[to]; n     if (!son[v] || (dp[to] > dp[son[v]])) { n       son[v] = to; n     } n   } n } n void hld(int v) { n   sz++; n   og[sz] = v; n   num[v] = sz; n   if (son[v]) { n     top[son[v]] = top[v]; n     hld(son[v]); n   } n   for (int to : g[v]) { n     if (to != son[v]) { n       top[to] = to; n       hld(to); n     } n   } n } n inline bool cmp(int x, int y) { n   return d[a[x]] + b[x] < d[a[y]] + b[y] || n          (d[a[x]] + b[x] == d[a[y]] + b[y] && a[x] < a[y]); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     g[x].push_back(i); n   } n   dfs(0); n   hld(0); n   n++; n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &a[i], &b[i]); n     p[i] = i; n   } n   sort(p + 1, p + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     int x = a[p[i]], t = b[p[i]], v; n     while (true) { n       if (mx(1, 1, n, num[top[x]], num[x]) > t + d[a[p[i]]]) { n         v = get(1, 1, n, num[top[x]], num[x], t + d[a[p[i]]]); n         break; n       } n       x = pr[top[x]]; n     } n     ans[p[i]] = t + 2 * (d[a[p[i]]] - d[v]); n     x = a[p[i]]; n     while (true) { n       if (mx(1, 1, n, num[top[x]], num[x]) > t + d[a[p[i]]]) { n         v = get(1, 1, n, num[top[x]], num[x], t + d[a[p[i]]]); n         if (v != x) { n           update(1, 1, n, num[v] + 1, num[x], n                  ans[p[i]] - (d[a[p[i]]] - d[x]) + d[x]); n         } n         break; n       } else { n         update(1, 1, n, num[top[x]], num[x], n                ans[p[i]] - (d[a[p[i]]] - d[x]) + d[x]); n       } n       x = pr[top[x]]; n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d  , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q = 1; n   while (q--) { n     double n, r; n     cin >> n >> r; n     double ans; n     ans = (r) / (1 / sin(3.14159265 / n) - 1); n     cout << setprecision(9) << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6; n const int MAX = 1e7 + 7; n int n, k; n int a[N]; n int dp[MAX]; n bool can(int x) { n   memset(dp, 0, sizeof(dp)); n   for (int i = x; i <= MAX; i++) { n     dp[i] = max(1, dp[i / 2] + dp[(i + 1) / 2]); n   } n   int kk = k; n   for (int i = 0; i < n; i++) { n     kk -= a[i] < x ? 0 : dp[a[i]]; n     if (kk <= 0) { n       return true; n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int lo = 0, hi = 1e7 + 100; n   while (lo + 1 < hi) { n     int mid = (lo + hi) / 2; n     if (!can(mid)) { n       hi = mid; n     } else { n       lo = mid; n     } n   } n   cout << (lo == 0 ? -1 : lo) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200011], b[200011], q[200011][2], l, r; n long long sum[2]; n char s[200011]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) b[i] = (s[i] ==  L  ? -1 : 1); n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; ++i) sum[n - i + 1 & 1] += a[i]; n   l = 1; n   r = n; n   int cur = n; n   while (l <= r) { n     if (b[cur] == b[cur - 1]) { n       q[cur][0] = a[l]; n       q[cur][1] = (r - l + 1 & 1 ? b[cur] : -b[cur]); n       ++l; n     } else { n       swap(sum[1], sum[0]); n       q[cur][0] = a[r]; n       q[cur][1] = b[cur]; n       --r; n     } n     --cur; n   } n   for (int i = 1; i <= n; ++i) n     if (q[i][1] == -1) n       printf( %d L n , q[i][0]); n     else n       printf( %d R n , q[i][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n bool mod(long double a, long double b) { return a / b - floor(a / b); } n int occurs(vector<int> v, int n) { n   auto it = lower_bound(((v)).begin(), ((v)).end(), (n)); n   auto it1 = upper_bound(((v)).begin(), ((v)).end(), (n)); n   int x = it1 - it; n   return x; n } n long double max(long double a, long double b) { return a < b ? b : a; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     string s; n     cin >> s; n     int a[n]; n     int c = 0; n     for (int(i) = 0; (i) < (n); ++(i)) { n       if (s[i] ==  0 ) n         c++; n       else n         c--; n       a[i] = c; n     } n     sort(a, a + n); n     int ans = 0; n     if (c == 0) { n       for (int(i) = 0; (i) < (n); ++(i)) { n         if (a[i] == x) { n           ans = -1; n           break; n         } n       } n       if (x == 0 && ans == 0) ans = 1; n       cout << ans <<   n ; n     } else { n       if ((x >= 0 && c > 0) || (x <= 0 && c < 0)) { n         if (c > 0) { n           for (int(i) = 0; (i) < (n); ++(i)) { n             if ((x - a[i]) % c == 0 && (x - a[i]) >= 0) ans++; n           } n         } else { n           for (int(i) = 0; (i) < (n); ++(i)) { n             if ((x - a[i]) % c == 0 && (x - a[i]) <= 0) ans++; n           } n         } n         if (x == 0) ans++; n         cout << ans <<   n ; n       } else { n         if (c > 0) { n           for (int(i) = 0; (i) < (n); ++(i)) { n             if ((x - a[i]) % c == 0 && (x - a[i]) >= 0) ans++; n           } n         } else { n           for (int(i) = 0; (i) < (n); ++(i)) { n             if ((x - a[i]) % c == 0 && (x - a[i]) <= 0) ans++; n           } n         } n         if (x == 0) ans++; n         cout << ans <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void rd(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 -  0  + c, c = getchar(); n   x *= f; n } n int curtest; n void FAIL() { n   printf( NO ); n   exit(0); n } n long long Abs(long long x) { return x > 0 ? x : -x; } n long double eps = 1e-20; n int dcmp(long double x) { return x > -eps ? (x > eps) : -1; } n const int N = 2e6 + 10; n vector<int> pos; n struct node { n   long long t, x, y; n } v[N]; n long long val[N], ti[N]; n int vis[N]; n int m, n; n long double lb, rb; n vector<char> S; n void modify(int p, long long x, long long delt) { n   if (Abs((delt * m + p) % 2) ^ Abs(x % 2)) FAIL(); n   if (delt == 0) return; n   long long l = -p - x, r = p - x; n   long double dt = -delt; n   if (dt > 0) n     lb = max(lb, l / dt), rb = min(rb, r / dt); n   else n     lb = max(lb, r / dt), rb = min(rb, l / dt); n } n void sol(vector<int> &ans) { n   lb = -m, rb = m; n   for (int i = 0; i + 1 < pos.size(); ++i) { n     if (i + 2 == pos.size()) n       modify(m - pos[i], -val[pos[i]], -ti[pos[i]] - 1); n     else n       modify(pos[i + 1] - pos[i], val[pos[i + 1]] - val[pos[i]], n              ti[pos[i + 1]] - ti[pos[i]]); n     if (ti[pos[i]] == 0) { n       lb = max(lb, (long double)val[pos[i]] - (m - pos[i])); n       rb = min(rb, (long double)val[pos[i]] + (m - pos[i])); n     } n   } n   long long dx = floor(rb); n   if (Abs(dx % 2) ^ (m % 2)) dx--; n   if (dcmp(dx - lb) < 0) FAIL(); n   for (int i = 0; i + 1 < pos.size(); ++i) { n     int l = pos[i], r = pos[i + 1], len = r - l; n     long long x = (r == m ? dx : val[r]) - val[l] - (ti[r] - ti[l]) * dx; n     int d = x > 0 ? 1 : -1; n     x = Abs(x); n     len -= x; n     while (x) x--, ans.push_back(d); n     if (len < 0 || (len & 1)) FAIL(); n     while (len) ans.push_back(1), ans.push_back(-1), len -= 2; n   } n } n long long dx, dy; n void solve(int l, int r, long long x, long long y, long long t) { n   x -= t * dx, y -= t * dy; n   char xx = x > 0 ?  R  :  L ; n   char yy = y > 0 ?  U  :  D ; n   x = Abs(x), y = Abs(y); n   while (x--) S.push_back(xx), r--; n   while (y--) S.push_back(yy), r--; n   if (((r - l) & 1) || r - l < 0) FAIL(); n   while (r - l) S.push_back( U ), S.push_back( D ), r -= 2; n } n vector<int> ax, ay; n int main() { n   rd(n), rd(m); n   int flg = 0; n   vis[0] = 1, v[0].x = v[0].y = v[0].t = 0; n   for (int i = 1; i <= n; ++i) { n     node A; n     rd(A.t), rd(A.x), rd(A.y); n     int p = A.t % m; n     A.t /= m; n     if (vis[p]) { n       if (A.t == v[p].t) { n         if (A.x != v[p].x || A.y != v[p].y) FAIL(); n         continue; n       } n       long long dt = A.t - v[p].t; n       if (flg) { n         if (v[p].x + dt * dx == A.x && v[p].y + dt * dy == A.y) n           continue; n         else n           FAIL(); n       } n       dx = A.x - v[p].x, dy = A.y - v[p].y; n       if (dx % dt || dy % dt) FAIL(); n       dx /= dt, dy /= dt; n       flg = 1; n     } else n       v[p] = A, vis[p] = 1; n   } n   if (flg) { n     int p1 = 0, p2; n     while (true) { n       p2 = p1 + 1; n       while (!vis[p2] && p2 < m) p2++; n       if (p2 >= m) break; n       solve(p1, p2, v[p2].x - v[p1].x, v[p2].y - v[p1].y, v[p2].t - v[p1].t); n       p1 = p2; n     } n     solve(p1, m, dx - v[p1].x, dy - v[p1].y, -v[p1].t); n     for (int i = 0; i < S.size(); ++i) putchar(S[i]); n   } else { n     for (int i = 0; i < m; ++i) n       if (vis[i]) ti[i] = v[i].t, pos.push_back(i); n     pos.push_back(m), ti[m] = 0; n     for (int i = 0; i < m; ++i) n       if (vis[i]) val[i] = v[i].x + v[i].y; n     sol(ax); n     for (int i = 0; i < m; ++i) n       if (vis[i]) val[i] = v[i].x - v[i].y; n     sol(ay); n     for (int i = 0; i < m; ++i) { n       if (ax[i] == ay[i]) n         putchar(ax[i] > 0 ?  R  :  L ); n       else n         putchar(ax[i] > 0 ?  U  :  D ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int d[MAXN], s[MAXN]; n vector<pair<int, int> > ans; n set<pair<int, pair<int, int> > > ver; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> d[i] >> s[i]; n     ver.insert({d[i], {s[i], i}}); n   } n   while (ver.size()) { n     int deg = ver.begin()->first, adj = ver.begin()->second.first, n         v = ver.begin()->second.second; n     ver.erase(ver.begin()); n     if (deg == 0) continue; n     ver.erase({d[adj], {s[adj], adj}}); n     ans.push_back({v, adj}); n     s[adj] ^= v; n     d[adj]--; n     ver.insert({d[adj], {s[adj], adj}}); n   } n   cout << ans.size() << endl; n   for (auto u : ans) cout << u.first <<     << u.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxx = 1000001; n int x[maxx], y[maxx]; n unsigned long long hash[maxx], vl[maxx], count = 0, check = 1, mul = 1; n bool compare(unsigned long long a, unsigned long long b) { return a > b; } n unsigned long long sum1ton(unsigned long long n) { return n * (n - 1) / 2; } n int main() { n   int a, b; n   scanf( %d %d , &a, &b); n   hash[0] = 1; n   for (int i = 1; i <= a; i++) { n     hash[i] = hash[i - 1] * mul + 31; n     mul = mul * 13 + 23; n   } n   for (int i = 0; i < b; i++) { n     scanf( %d %d , &x[i], &y[i]); n     vl[x[i]] -= hash[y[i]]; n     vl[y[i]] -= hash[x[i]]; n   } n   for (int i = 0; i < b; i++) { n     if (vl[x[i]] + hash[y[i]] == vl[y[i]] + hash[x[i]]) { n       count++; n     } n   } n   std::sort(vl + 1, vl + a + 1, compare); n   for (int i = 1; i < a; i++) { n     if (vl[i] == vl[i + 1]) n       check++; n     else { n       count += sum1ton(check); n       check = 1; n     } n   } n   count += sum1ton(check); n   printf( %I64d , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int d[N]; n int a[N][N], b[N][N], c[N][N], row[N], col[N]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       d[j] = a[i][j]; n     } n     sort(d + 1, d + m + 1); n     int k = unique(d + 1, d + m + 1) - d - 1; n     for (int j = 1; j <= m; j++) n       b[i][j] = lower_bound(d + 1, d + k + 1, a[i][j]) - d; n     row[i] = k; n   } n   for (int j = 1; j <= m; j++) { n     for (int i = 1; i <= n; i++) d[i] = a[i][j]; n     sort(d + 1, d + n + 1); n     int k = unique(d + 1, d + n + 1) - d - 1; n     for (int i = 1; i <= n; i++) n       c[i][j] = lower_bound(d + 1, d + k + 1, a[i][j]) - d; n     col[j] = k; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int ans = 0; n       if (b[i][j] >= c[i][j]) { n         ans = max(ans, row[i]); n         ans = max(ans, col[j] + b[i][j] - c[i][j]); n       } else { n         ans = max(ans, col[j]); n         ans = max(ans, row[i] + c[i][j] - b[i][j]); n       } n       printf( %d  , ans); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T_key, typename T_value, bool maximum_mode = false> n struct online_prefix_max { n   map<T_key, T_value> optimal; n   bool better(T_value a, T_value b) { return (a < b) ^ maximum_mode; } n   pair<T_key, T_value> query(T_key key_limit) const { n     auto it = optimal.lower_bound(key_limit); n     if (it == optimal.begin()) return {0, -1}; n     it--; n     return *it; n   } n   void insert(T_key key, T_value value) { n     auto it = optimal.lower_bound(key); n     if (it != optimal.end() && it->first == key) { n       if (!better(value, it->second)) return; n     } else if (it != optimal.begin()) { n       auto prev_it = it; n       prev_it--; n       if (!better(value, prev_it->second)) return; n     } n     while (it != optimal.end() && !better(it->second, value)) n       it = optimal.erase(it); n     optimal.insert(it, {key, value}); n   } n }; n int main() { n   int n, i, j, t; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   online_prefix_max<long long int, int> ds; n   map<pair<int, long long int>, pair<int, int> > mp; n   long long int l, r; n   cin >> n >> t; n   for (i = 0; i < n; i++) { n     cin >> l >> r; n     r += l; n     mp[{l, r}] = {i, 0}; n     ds.insert(-r, l); n   } n   int pos, siz; n   pair<long long int, int> p; n   multiset<pair<int, int> > not_eaten; n   while (t--) { n     cin >> pos >> siz; n     p = ds.query(-pos + 1); n     if (p.second == -1 || p.second > pos) { n       not_eaten.insert({pos, siz}); n     } else { n       l = p.second; n       r = -p.first; n       pair<int, int> val = mp[{l, r}]; n       mp.erase({l, r}); n       r += siz; n       val.second++; n       while (1) { n         auto it = not_eaten.lower_bound({l, -1}); n         if (it == not_eaten.end() || it->first > r) break; n         r += it->second; n         val.second++; n         not_eaten.erase(it); n       } n       ds.insert(-r, l); n       mp[{l, r}] = val; n     } n   } n   pair<int, int> ans[n]; n   for (auto &z : mp) { n     ans[z.second.first] = {z.second.second, z.first.second - z.first.first}; n   } n   for (i = 0; i < n; i++) cout << ans[i].first <<     << ans[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n], vis[200005] = {0}; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       if (arr[i] <= n) vis[arr[i]]++; n     } n     sort(arr, arr + n); n     vector<int> v; n     for (int i = 1; i <= n; i++) { n       if (!vis[i]) v.push_back(i); n     } n     int m = v.size(), l = 0; n     int ans = 0; n     for (int i = 0; i < n; i++) { n       int x = arr[i]; n       if (x <= n && vis[x] > 1) { n         int r = vis[x], k = 1; n         while (r > 1) { n           if (l < m && (2 * v[l]) < x) { n             ans++; n             l++; n           } else { n             ans = -1; n             k = 0; n             break; n           } n           r--; n           i++; n         } n         if (!k) break; n       } n       if (x > n) { n         if (l < m && (2 * v[l]) < x) { n           ans++; n           l++; n         } else { n           ans = -1; n           break; n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<long long> V; n   for (long long i = 1; i * i <= n; ++i) { n     if (!(n % i)) { n       V.push_back(i); n       V.push_back(n / i); n     } n   } n   sort(V.begin(), V.end()); n   for (long long i = V.size() - 1; i >= 0; --i) n     if (i == V.size() - 1 || V[i + 1] != V[i]) n       cout << n * (2 + V[i] * (n / V[i] - 1)) / 2 / V[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int main() { n   cin >> n >> m >> k; n   if (m >= n && k >= n) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n int n, h, a[100005]; n pair<int, int> A[100005]; n void solve() { n   sort(A, A + n); n   for (int i = (0); i < (n); i++) a[i] = A[i].first; n   int res = a[n - 1] + a[n - 2] - a[0] - a[1], ind = n - 1; n   if (n >= 3) { n     int v = max(a[n - 1] + a[n - 2], a[n - 1] + a[0] + h) - n             min(a[1] + a[2], a[1] + a[0] + h); n     if (res > v) { n       res = v; n       ind = 0; n     } n     v = max(a[n - 3] + a[n - 2], a[n - 1] + a[n - 2] + h) - (a[0] + a[1]); n     if (res > v) { n       res = v; n       ind = n - 2; n     } n   } n   for (int i = 1; i + 2 < n; i++) { n     int v = max(a[n - 1] + a[n - 2], a[n - 1] + a[i] + h) - (a[0] + a[1]); n     if (res > v) { n       res = v; n       ind = i; n     } n   } n   cout << res << endl; n   set<int> L; n   for (int i = (0); i < (ind + 1); i++) L.insert(A[i].second); n   for (int i = (0); i < (n); i++) { n     printf( %d  , (L.find(i) == L.end()) + 1); n   } n   cout << endl; n } n int main() { n   while (cin >> n >> h) { n     for (int i = (0); i < (n); i++) { n       scanf( %d , &A[i].first); n       A[i].second = i; n     } n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxx = 1e6 + 7; n const long long maxn = 5000 + 7; n const long long mod = 998244853; n const long long maxs = 5 * (1E5) + 7; n const long long maxm = 4e5 + 7; n const long long maxk = 1008 + 7; n const long long maxw = 100 + 7; n const long long maxh = 100 + 7; n const double pi = 3.1415926; n const long long ten = 10; n const int ooi = 1234567; n const long long ool = 1e18 + 7; n const long double eps = 1e-8; n const int apsz = 26; n vector<int> can[2][2]; n int a[maxn]; n int c[maxn]; n int n; n void init() { n   cin >> n; n   for (long long i = (0); i < (n); i++) { n     char ch; n     cin >> ch; n     c[i] = ch -  0 ; n   } n   for (long long i = (0); i < (n); i++) { n     char ch; n     cin >> ch; n     a[i] = ch -  0 ; n   } n   for (long long i = (0); i < (n); i++) { n     if (a[i] == 1 && c[i] == 1) can[1][1].push_back(i); n     if (c[i] == 1 && a[i] == 0) can[1][0].push_back(i); n     if (c[i] == 0 && a[i] == 0) can[0][0].push_back(i); n     if (c[i] == 0 && a[i] == 1) can[0][1].push_back(i); n   } n } n void solve() { n   int nn = can[0][0].size(); n   int nc = can[1][0].size(); n   int na = can[0][1].size(); n   int nb = can[1][1].size(); n   int ans = -1; n   int a, b, c, d; n   for (b = 0; b <= nc; b++) { n     for (c = 0; c <= na; c++) { n       if ((nb + na - c - b) % 2) continue; n       d = (nb + na - c - b) / 2; n       a = n / 2 - b - c - d; n       if (a <= nn && d <= nb && a >= 0 && d >= 0) { n         ans = 1; n         goto ED; n       } n     } n   } n ED: n   if (ans == -1) { n     cout << ans << endl; n     return; n   } n   vector<int> ta; n   for (long long i = (0); i < (a); i++) ta.push_back(can[0][0][i]); n   for (long long i = (0); i < (b); i++) ta.push_back(can[1][0][i]); n   for (long long i = (0); i < (c); i++) ta.push_back(can[0][1][i]); n   for (long long i = (0); i < (d); i++) ta.push_back(can[1][1][i]); n   for (long long i = (0); i < (n / 2); i++) { n     if (i) cout <<    ; n     cout << ta[i] + 1; n   } n   cout << endl; n } n int main(void) { n   ios::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int n, u, v, cnt = 0, h[N] = {-1}, in[N], out[N], ans[N], val[N], pa[N]; n bool chk[N]; n vector<int> cur; n vector<int> adj[N]; n vector<pair<int, int>> ve; n multiset<int, greater<int>> second, ed, ch[N]; n struct segment_tree { n   int tr[4 * N], lz[4 * N]; n   void apply(int i, int v) { n     tr[i] += v; n     lz[i] += v; n   } n   void down(int i) { n     apply(i * 2, lz[i]); n     apply(i * 2 + 1, lz[i]); n     lz[i] = 0; n   } n   void update(int l, int r, int i, int L, int R, int v) { n     if (l > R || r < L || L > R) { n       return; n     } else if (L <= l && r <= R) { n       apply(i, v); n       return; n     } else { n       down(i); n       update(l, (l + r) / 2, i * 2, L, R, v); n       update((l + r) / 2 + 1, r, i * 2 + 1, L, R, v); n       tr[i] = max(tr[i * 2], tr[i * 2 + 1]); n     } n   } n   int query(int l, int r, int i, int L, int R) { n     if (l > R || r < L || L > R) { n       return 0; n     } else if (L <= l && r <= R) { n       return tr[i]; n     } else { n       down(i); n       return max(query(l, (l + r) / 2, i * 2, L, R), n                  query((l + r) / 2 + 1, r, i * 2 + 1, L, R)); n     } n   } n } seg; n void DFS(int u, int p = 0) { n   h[u] = h[p] + 1; n   pa[u] = p; n   in[u] = ++cnt; n   seg.update(1, n, 1, in[u], in[u], h[u]); n   for (int &v : adj[u]) { n     if (v != p) { n       DFS(v, u); n     } n   } n   out[u] = cnt; n } n void DFS_2(int u, int p = 0) { n   int pa = n       max(seg.query(1, n, 1, 1, in[u] - 1), seg.query(1, n, 1, out[u] + 1, n)); n   ve.push_back({0, u}); n   if (u != 1) { n     ve.push_back({pa, u}); n   } n   for (int &v : adj[u]) { n     if (v != p) { n       int pa = seg.query(1, n, 1, in[v], out[v]); n       ve.push_back({pa, u}); n       seg.update(1, n, 1, 1, n, 1); n       seg.update(1, n, 1, in[v], out[v], -2); n       DFS_2(v, u); n       seg.update(1, n, 1, 1, n, -1); n       seg.update(1, n, 1, in[v], out[v], 2); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   DFS(1); n   DFS_2(1); n   for (int i = 1; i <= n; i++) { n     second.insert(val[i] = adj[i].size() + 1); n   } n   for (int i = 1; i <= n; i++) { n     for (int &v : adj[i]) { n       if (v != pa[i]) { n         ch[i].insert(val[v]); n       } n     } n     ed.insert(val[i] + *ch[i].begin() - 2); n   } n   sort(ve.begin(), ve.end(), greater<pair<int, int>>()); n   for (int i = 0; i <= n; i += 2) { n     ans[i] = max(*second.begin(), *ed.begin()); n     while (!ve.empty() && ve.back().first == i / 2) { n       int u = ve.back().second; n       if (!chk[u]) { n         chk[u] = true; n         cur.push_back(u); n       } n       ve.pop_back(); n       second.erase(second.find(val[u])); n       second.insert(val[u] - 1); n       ed.erase(ed.find(val[u] + *ch[u].begin() - 2)); n       ed.insert(val[u] - 1 + *ch[u].begin() - 2); n       if (u != 1) { n         ed.erase(ed.find(val[pa[u]] + *ch[pa[u]].begin() - 2)); n         ch[pa[u]].erase(ch[pa[u]].find(val[u])); n         ch[pa[u]].insert(val[u] - 1); n         ed.insert(val[pa[u]] + *ch[pa[u]].begin() - 2); n       } n       val[u]--; n     } n     ans[i + 1] = *second.begin(); n     while (!cur.empty()) { n       int u = cur.back(); n       cur.pop_back(); n       chk[u] = false; n       ans[i + 1] = max(ans[i + 1], val[u] + 1); n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << max(ans[i], 1) <<    ; n   } n } n


#include <bits/stdc++.h> n int n; n int tot; n int top; n int tot1; n int a[2005][2005]; n int stack[2005]; n int instack[2005]; n int visit[2005]; n int dfn[2005]; n int low[2005]; n inline void tarjan(int u) { n   if (visit[u]) return; n   int v; n   dfn[u] = low[u] = ++tot; n   stack[++top] = u; n   instack[u] = 1; n   visit[u] = 1; n   int i; n   for (i = 1; i <= n; i++) { n     if (a[u][i]) { n       if (!visit[i]) { n         tarjan(i); n         if (low[i] < low[u]) low[u] = low[i]; n       } else { n         if (instack[i]) n           if (dfn[i] < low[u]) low[u] = dfn[i]; n       }; n     }; n   }; n   if (dfn[u] == low[u]) { n     ++tot1; n     while (1) { n       v = stack[top--]; n       instack[v] = 0; n       if (v == u) break; n     }; n   }; n }; n int main() { n   scanf( %d , &n); n   int i, j; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       scanf( %d , a[i] + j); n     }; n   }; n   for (i = 1; i <= n; i++) tarjan(i); n   puts(tot1 == 1 ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n #pragma gcc optimize(O3) n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const double dinf = 1e250; n const long long INF = static_cast<long long>(2e18); n const unsigned long long inf = static_cast<unsigned long long>(1e9 + 7); n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <class T> n void OUT(T a) { n   cout << a; n   exit(0); n } n template <typename T> n bool amax(T& a, T b) { n   if (b > a) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n bool amin(T& a, T b) { n   if (b < a) { n     a = b; n     return true; n   } n   return false; n } n mt19937 rng(static_cast<uint32_t>( n     chrono::steady_clock::now().time_since_epoch().count())); n chrono::time_point<chrono::steady_clock> cl; n double time_since_start() { n   return (chrono::steady_clock::now() - cl).count() / 1e9; n } n void get_time(); n vector<long long> precalc; n void rec(unsigned long long n, unsigned long long cur, unsigned long long& k, n          map<unsigned long long, unsigned long long>& s, n          unsigned long long& w) { n   w = n; n   if (!n) return; n   if (!k) return; n   if (k >= 2 * cur - 1) { n     k -= 2 * cur - 1; n     rec(n - 1, cur * 2, k, s, w); n     s[n - 1] += 2 * cur - 3; n   } n } n void solve() { n   map<unsigned long long, unsigned long long> m; n   unsigned long long n, k; n   cin >> n >> k; n   unsigned long long cur = 1; n   while (k >= cur * cur && n) { n     k -= cur * cur; n     cur *= 2; n     n -= 1; n   } n   if (!n && k) { n     cout <<  NO  << endl; n     return; n   } n   unsigned long long w; n   rec(n, cur, k, m, w); n   m[n] += (cur - 1) * (cur - 1); n   for (unsigned long long i = n; i > 0; --i) { n     if (k >= m[i]) { n       k -= m[i]; n       m[i - 1] += 4 * m[i]; n     } n     if (k < m[i]) { n       cout <<  YES   << w << endl; n       return; n     } n   } n   if (!k) { n     cout <<  YES   << n << endl; n     return; n   } n   if (k) { n     cout <<  NO  << endl; n     return; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cout << fixed << setprecision(10); n   cl = chrono::steady_clock::now(); n   cin.tie(0); n   unsigned long long cur = 1; n   for (unsigned long long i = 0; i < 32; ++i) { n     precalc.push_back(cur); n     cur *= 4; n   } n   for (unsigned long long i = 1; i < 32; ++i) { n     precalc[i] += precalc[i - 1]; n   } n   unsigned long long q; n   cin >> q; n   while (q--) { n     solve(); n   } n   return 0; n } n void get_time() {} n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2010; n const int MAXM = 4100000; n int F[MAXN]; n struct node { n   long long x, y, c; n } a[MAXN]; n struct Edge { n   long long u, v, w; n } edge[MAXM]; n int tol; n vector<long long> vec1; n vector<pair<long long, long long> > vec2; n void addedge(long long u, long long v, long long w) { n   edge[tol].u = u; n   edge[tol].v = v; n   edge[tol++].w = w; n } n bool cmp(Edge a, Edge b) { return a.w < b.w; } n long long Find(long long x) { n   if (F[x] == -1) n     return x; n   else n     return F[x] = Find(F[x]); n } n long long Kruskal(int n) { n   memset(F, -1, sizeof(F)); n   sort(edge, edge + tol, cmp); n   long long cnt = 0; n   long long ans = 0; n   for (int i = 0; i < tol; i++) { n     long long u = edge[i].u; n     long long v = edge[i].v; n     long long w = edge[i].w; n     long long t1 = Find(u); n     long long t2 = Find(v); n     if (t1 != t2) { n       if (u == 0) n         vec1.push_back(v); n       else if (v == 0) n         vec1.push_back(u); n       else n         vec2.push_back(make_pair(u, v)); n       ans += w; n       F[t1] = t2; n       cnt++; n     } n     if (cnt == n) break; n   } n   if (cnt < n) n     return -1; n   else n     return ans; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; n   for (int i = 1; i <= n; i++) { n     long long c; n     cin >> c; n     addedge(0, i, c); n     addedge(i, 0, c); n   } n   for (int i = 1; i <= n; i++) cin >> a[i].c; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (j == i) continue; n       long long ww = n           (a[i].c + a[j].c) * (abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y)); n       addedge(i, j, ww); n     } n   } n   long long ans = Kruskal(n); n   cout << ans << endl; n   cout << vec1.size() << endl; n   for (int i = 0; i < vec1.size(); i++) { n     if (i != 0) cout <<    ; n     cout << vec1[i]; n   } n   cout << endl; n   cout << vec2.size() << endl; n   for (int i = 0; i < vec2.size(); i++) { n     cout << vec2[i].first <<     << vec2[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 30; n const int M = 110; n const int LIT = 2500; n const int INF = 1 << 28; n string s; n int n, m; n int val[N][N]; n void init() { n   cin >> n; n   memset(val, 0, sizeof(val)); n   for (int i = 1; i <= n; i++) { n     string a, b; n     int v; n     cin >> a >> b >> v; n     int aa = a[0] -  a ; n     int bb = b[0] -  a ; n     val[aa][bb] = v; n   } n } n int dp[M][M][N]; n void solve() { n   memset(dp, 0, sizeof(dp)); n   for (int i = 0; i < 26; i++) { n     int ch = s[0] -  a ; n     dp[0][1][i] = 0; n     dp[0][0][i] = -INF; n     if (i == ch) dp[0][0][i] = 0; n   } n   for (int i = 1; i < s.size(); i++) { n     int ch = s[i] -  a ; n     for (int j = 0; j <= i + 1; j++) { n       for (int k = 0; k < 26; k++) { n         int Max = -INF; n         if (ch == k && j <= i) { n           for (int p = 0; p < 26; p++) { n             int tmp = dp[i - 1][j][p] + val[p][k]; n             Max = max(Max, tmp); n           } n         } else { n           for (int p = 0; j && p < 26; p++) { n             int tmp = dp[i - 1][j - 1][p] + val[p][k]; n             Max = max(Max, tmp); n           } n         } n         dp[i][j][k] = Max; n       } n     } n   } n   int out = s.size() - 1, res = -INF; n   for (int i = 0; i <= min(out + 1, m); i++) { n     for (int k = 0; k < 26; k++) { n       res = max(res, dp[out][i][k]); n     } n   } n   cout << res << endl; n } n int main() { n   while (cin >> s >> m) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200000 + 1], b[200000 + 1]; n int c[200000 + 1], t[200000 + 1]; n int n, m, k, s; n vector<pair<long long, int> > v; n inline int check(int p) { n   int i; n   v.clear(); n   long long mna = (1LL << 62), mnb = (1LL << 62); n   for (i = 1; i <= p; i++) { n     mna = min(mna, a[i]); n     mnb = min(mnb, b[i]); n   } n   for (i = 1; i <= m; i++) { n     if (t[i] == 1) n       v.push_back({1LL * mna * c[i], i}); n     else n       v.push_back({1LL * mnb * c[i], i}); n   } n   sort(v.begin(), v.end()); n   int cnt = 0; n   long long aux = s; n   for (i = 0; i < m; i++) { n     if (aux >= v[i].first) { n       aux -= v[i].first; n       cnt++; n     } n   } n   return cnt; n } n int main() { n   int i; n   ios::sync_with_stdio(false); n   cin >> n >> m >> k >> s; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) cin >> b[i]; n   for (i = 1; i <= m; i++) cin >> t[i] >> c[i]; n   int rez = 0; n   for (int pas = 1 << 17; pas; pas >>= 1) n     if (rez + pas <= n && check(rez + pas) < k) rez += pas; n   if (rez == n) { n     cout << -1; n     return 0; n   } n   rez++; n   cout << rez << endl; n   v.clear(); n   long long mna = (1LL << 62), mnb = (1LL << 62); n   int pa, pb; n   for (i = 1; i <= rez; i++) { n     mna = min(mna, a[i]); n     if (mna == a[i]) pa = i; n     mnb = min(mnb, b[i]); n     if (mnb == b[i]) pb = i; n   } n   for (i = 1; i <= m; i++) { n     if (t[i] == 1) n       v.push_back({1LL * mna * c[i], i}); n     else n       v.push_back({1LL * mnb * c[i], i}); n   } n   sort(v.begin(), v.end()); n   int cnt = 0; n   for (i = 0; i < m && cnt < k; i++) { n     if (s >= v[i].first) { n       s -= v[i].first; n       cnt++; n       if (t[v[i].second] == 1) n         cout << v[i].second <<     << pa << endl; n       else n         cout << v[i].second <<     << pb << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, zz; n string s; n int a[28]; n int main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int ntest; n   cin >> ntest; n   while (ntest--) { n     cin >> n >> m >> zz >> s; n     for (int i = 0; i < 28; ++i) a[i] = 0; n     for (int i = 0; i < s.size(); ++i) ++a[s[i] -  A ]; n     int ans = 1e9; n     for (int i = 0; i < 27; ++i) { n       vector<bool> can(zz - a[i] + 1, 0); n       can[0] = 1; n       for (int j = 0; j < 26; ++j) n         if (j != i) { n           for (int k = zz - a[i]; k >= 0; --k) n             if (can[k]) can[k + a[j]] = 1; n         } n       for (int j = 0; j <= zz - a[i]; ++j) n         if (can[j]) { n           int need_n = max(0, n - j); n           int need_m = max(0, m - (zz - a[i] - j)); n           if (need_n + need_m > a[i]) continue; n           ans = min(ans, need_n * need_m); n         } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[5], sum = 0; n   for (int i = 0; i < 5; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum % 5 == 0 && sum != 0) n     cout << (sum / 5); n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int LIM = 1e+5 + 100; n int n, m, k; n int get_rd(int i, int j) { n   if (i == j) { n     return n; n   } else if (i < j) { n     return n + (j - i); n   } else { n     return n - (i - j); n   } n } n int get_ld(int i, int j) { n   if (i + j == n) { n     return n; n   } else if (i + j < n) { n     return i + j; n   } else { n     return i + j; n   } n } n struct detector { n   int i, j, id; n   detector(int i, int j, int id) : i(i), j(j), id(id) {} n   int get_rd() { return ::get_rd(i, j); } n   int get_ld() { return ::get_ld(i, j); } n }; n vector<bool> used(LIM, false); n vector<long long> ans(LIM, -1); n vector<vector<detector>> ld(2 * LIM); n vector<vector<detector>> rd(2 * LIM); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m >> k; n   for (int i = 0; i < k; ++i) { n     int pi, pj; n     cin >> pi >> pj; n     detector newbie(pi, pj, i); n     rd[newbie.get_rd()].push_back(newbie); n     ld[newbie.get_ld()].push_back(newbie); n   } n   int i = 0, j = 0; n   int di = 1, dj = 1; n   long long time = 0; n   while (true) { n     int d = -1; n     if (di == dj) { n       d = get_rd(i, j); n       for (const detector& det : rd[d]) { n         int id = det.id; n         if (!used[id]) { n           ans[id] = time + abs(i - det.i); n           used[id] = true; n         } n       } n     } else { n       d = get_ld(i, j); n       for (const detector& det : ld[d]) { n         int id = det.id; n         if (!used[id]) { n           ans[id] = time + abs(i - det.i); n           used[id] = true; n         } n       } n     } n     if (di == 1 && dj == 1) { n       int delta = min(n - i, m - j); n       time += delta; n       i += delta; n       j += delta; n       if (i == n && j == m) { n         break; n       } n       if (i == n) n         di = -1; n       else n         dj = -1; n     } else if (di == -1 && dj == -1) { n       int delta = min(i, j); n       time += delta; n       i -= delta; n       j -= delta; n       if (i == 0 && j == 0) break; n       if (i == 0) n         di = 1; n       else n         dj = 1; n     } else if (di == 1 && dj == -1) { n       int delta = min(n - i, j); n       time += delta; n       i += delta; n       j -= delta; n       if (i == n && j == 0) break; n       if (i == n) n         di = -1; n       else n         dj = 1; n     } else { n       int delta = min(i, m - j); n       time += delta; n       i -= delta; n       j += delta; n       if (i == 0 && j == m) break; n       if (i == 0) n         di = 1; n       else n         dj = -1; n     } n   } n   for (int i = 0; i < k; ++i) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[100001]; n int acum[100001]; n long long cnt = 0, ans[100001]; n struct Q { n   int i, j, p; n   Q(int a, int b, int c) : i(a), j(b), p(c){}; n   Q() {} n   bool operator<(const Q &other) const { n     if (i / 300 == other.i / 300) return j < other.j; n     return i / 300 < other.i / 300; n   } n }; n vector<Q> queries; n int freq[(1 << 20) - 1 + 1]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , v + i); n     acum[i] = v[i] ^ acum[i - 1]; n   } n   for (int k = 0; k < m; k++) { n     int i, j; n     scanf( %d %d , &i, &j); n     queries.push_back(Q(i - 1, j, k)); n   } n   sort(queries.begin(), queries.end()); n   int l = 0, r = 0; n   freq[0] = 1; n   for (int q = 0; q < m; q++) { n     int i = queries[q].i, j = queries[q].j, p = queries[q].p; n     while (l < i) { n       freq[acum[l]]--; n       cnt -= freq[acum[l] ^ k]; n       l++; n     } n     while (l > i) { n       --l; n       cnt += freq[acum[l] ^ k]; n       freq[acum[l]]++; n     } n     while (r < j) { n       r++; n       cnt += freq[acum[r] ^ k]; n       freq[acum[r]]++; n     } n     while (r > j) { n       freq[acum[r]]--; n       cnt -= freq[acum[r] ^ k]; n       r--; n     } n     ans[p] = cnt; n   } n   for (int i = 0; i < m; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n const int inf = 1000000009; n const long long INF = 1000000000000000009; n const long long big = 1000000000000000; n const long double eps = 0.0000000001; n using namespace std; n int DP[2005][2005], T[2005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int n, h; n   cin >> n >> h; n   for (int i = 1; i <= n; i++) cin >> T[i]; n   DP[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     int j = h - T[i]; n     if (j < 0) continue; n     DP[i][j] = (DP[i][j] + DP[i - 1][j]) % mod; n     DP[i][j] %= mod; n     if (j > 0) { n       DP[i][j] = (DP[i][j] + DP[i - 1][j - 1]) % mod; n       DP[i][j - 1] = ((long long int)DP[i][j - 1] + DP[i - 1][j - 1] + n                       (long long int)DP[i - 1][j] * j) % n                      mod; n       DP[i][j - 1] = n           (DP[i][j - 1] + (long long int)DP[i - 1][j - 1] * (j - 1)) % mod; n     } n   } n   cout << DP[n][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n int a[maxn], b[maxn]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int MIN1 = 0x7fffffff; n     int MIN2 = 0x7fffffff; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       MIN1 = min(MIN1, a[i]); n     } n     for (int i = 1; i <= n; i++) { n       scanf( %d , &b[i]); n       MIN2 = min(MIN2, b[i]); n     } n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i] - MIN1 == 0 && b[i] - MIN2 != 0) { n         ans += (b[i] - MIN2); n         continue; n       } n       if (a[i] - MIN1 != 0 && b[i] - MIN2 == 0) { n         ans += (a[i] - MIN1); n         continue; n       } n       if (a[i] - MIN1 > b[i] - MIN2) { n         ans += (b[i] - MIN2); n         a[i] -= (b[i] - MIN2); n         ans += (a[i] - MIN1); n       } else { n         ans += (a[i] - MIN1); n         b[i] -= (a[i] - MIN1); n         ans += (b[i] - MIN2); n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m, x, y, i; n   cin >> n >> m >> x >> y; n   cout << x <<     << y << endl; n   int k = x + 1; n   int p = x - 1; n   for (i = 1; i < m + 1; i++) { n     if (i != y) cout << x <<     << i << endl; n   } n   int jj = 1; n   while (k <= n) { n     if (jj == 0) { n       for (i = 1; i < m + 1; i++) { n         cout << k <<     << i << endl; n       } n       jj = 1; n     } else { n       for (i = m; i >= 1; i--) { n         cout << k <<     << i << endl; n       } n       jj = 0; n     } n     k++; n   } n   while (p >= 1) { n     if (jj == 0) { n       for (i = 1; i < m + 1; i++) { n         cout << p <<     << i << endl; n       } n       jj = 1; n     } else { n       for (i = m; i >= 1; i--) { n         cout << p <<     << i << endl; n       } n       jj = 0; n     } n     p--; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int wall[110][110] = {0}; n   int r, c; n   scanf( %d %d%*c , &r, &c); n   char a[110]; n   for (int i = 1; i < r; i++) gets(a); n   int flag = 0; n   int seg = 0; n   for (int i = 0; i < c; i++) { n     char x; n     scanf( %c , &x); n     if (flag == 0) { n       if (x ==  B ) { n         flag++; n         seg++; n       } else n         continue; n     } else { n       if (x ==  B ) n         continue; n       else n         flag = 0; n     } n   } n   printf( %d n , seg); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2012; n int f[N][N][2]; n int n, m, k, flag; n int ex, ey; n inline int find(int x, int y, int p) { n   if (y < 0) return y; n   return f[x][y][p] == y ? y : f[x][y][p] = find(x, f[x][y][p], p); n } n int dis(int x, int y) { return abs(ex - x) + abs(ey - y); } n inline void chk(int &ans, int nx, int ny, int &x, int &y) { n   int d = dis(nx, ny); n   if (d < ans) { n     ans = d, x = nx, y = ny; n   } else if (ans == d) { n     if (!flag) { n       if (nx < x) { n         x = nx, y = ny; n       } else if (nx == x && ny < y) n         y = ny; n     } else { n       if (ny < y) { n         x = nx, y = ny; n       } else if (ny == y && nx < x) n         x = nx; n     } n   } n } n void update(int r, int &ans, int &x, int &y) { n   int nx = r, ny = find(r, ey, 0); n   if (ny > 0) chk(ans, nx, ny, x, y); n   ny = find(r, ey, 1); n   if (ny <= m) chk(ans, nx, ny, x, y); n } n int main() { n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) f[i][j][0] = f[i][j][1] = j; n   int x, y; n   scanf( %d%d%d , &n, &m, &k); n   if (n > m) swap(n, m), flag = 1; n   while (k--) { n     scanf( %d%d , &x, &y); n     if (flag) swap(x, y); n     ex = x, ey = y; n     int d = 0, ans = ~0u >> 2, nx, ny; n     while (d <= ans) { n       int pre = ans; n       int r = x - d; n       if (r > 0 && r <= n) update(r, ans, nx, ny); n       r = x + d; n       if (r > 0 && r <= n) update(r, ans, nx, ny); n       d++; n     } n     f[nx][ny][0] = ny - 1; n     f[nx][ny][1] = ny + 1; n     if (flag) swap(nx, ny); n     printf( %d %d n , nx, ny); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int INF = 1e9 + 10; n int n, current; n pair<int, int> best; n int ans[N], block[N], sz[N]; n vector<int> tree[N], tree_c[N]; n int dfs1(int i, int parent) { n   sz[i] = 1; n   for (int k = 0; k < tree[i].size(); ++k) { n     int j = tree[i][k]; n     if (j != parent and !block[j]) sz[i] += dfs1(j, i); n   } n   return sz[i]; n } n void dfs2(int i, int parent) { n   int highest = current - sz[i]; n   for (int k = 0; k < tree[i].size(); ++k) { n     int j = tree[i][k]; n     if (j == parent or block[j]) continue; n     dfs2(j, i); n     highest = max(highest, sz[j]); n   } n   if (highest < best.first) best = make_pair(highest, i); n } n int find_centroid(int i) { n   current = dfs1(i, 0); n   best = make_pair(INF, 0); n   dfs2(i, 0); n   return best.second; n } n int build(int i, int deep) { n   int centroid = find_centroid(i); n   block[centroid] = 1; n   for (int k = 0; k < tree[centroid].size(); ++k) { n     int j = tree[centroid][k]; n     if (block[j]) continue; n     j = build(j, deep + 1); n     tree_c[centroid].push_back(j); n     tree_c[j].push_back(centroid); n   } n   return centroid; n } n bool solve(int root) { n   ans[root] = 0; n   queue<pair<int, int> > q; n   q.push(make_pair(root, -1)); n   while (!q.empty()) { n     int i = q.front().first; n     int parent = q.front().second; n     q.pop(); n     for (int k = 0; k < tree_c[i].size(); ++k) { n       int j = tree_c[i][k]; n       if (j == parent) continue; n       if (ans[i] + 1 > 25) return false; n       ans[j] = ans[i] + 1; n       q.push(make_pair(j, i)); n     } n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     --x, --y; n     tree[x].push_back(y); n     tree[y].push_back(x); n   } n   int root = build(0, 0); n   if (!solve(root)) n     printf( Impossible! n ); n   else { n     for (int i = 0; i < n; ++i) { n       if (i) printf(   ); n       printf( %c , ans[i] +  A ); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace fastIO { n bool IOerror = 0; n inline char nc() { n   static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; n   if (p1 == pend) { n     p1 = buf; n     pend = buf + fread(buf, 1, 100000, stdin); n     if (pend == p1) { n       IOerror = 1; n       return -1; n     } n   } n   return *p1++; n } n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n inline long long read() { n   bool sign = 0; n   char ch = nc(); n   long long x = 0; n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return 0; n   if (ch ==  - ) sign = 1, ch = nc(); n   for (; ch >=  0  && ch <=  9 ; ch = nc()) x = x * 10 + ch -  0 ; n   if (sign) x = -x; n   return x; n } n };  // namespace fastIO n using namespace fastIO; n long long qpow(long long n, long long k) { n   long long ans = 1; n   while (k) { n     if (k & 1) ans *= n, ans %= 998244353; n     n *= n, n %= 998244353; n     k >>= 1; n   } n   return ans; n } n char s[3000010]; n long long fac[3000010], ifac[3000010], l[3000010], r[3000010], x[3000010], n     y[3000010]; n long long c(long long x, long long y) { n   return fac[x] * ifac[x - y] % 998244353 * ifac[y] % 998244353; n } n signed main() { n   cin >> s + 1; n   long long n = strlen(s + 1); n   fac[0] = 1; n   for (long long i = 1; i <= n; i++) { n     fac[i] = fac[i - 1] * i % 998244353; n   } n   ifac[n] = qpow(fac[n], 998244353 - 2); n   for (long long i = n - 1; i >= 0; i--) n     ifac[i] = ifac[i + 1] * (i + 1) % 998244353; n   for (long long i = 1; i <= n; i++) { n     l[i] = l[i - 1] + (s[i] ==  ( ); n     x[i] = x[i - 1] + (s[i] ==  ? ); n   } n   for (long long i = n; i >= 1; i--) { n     r[i] = r[i + 1] + (s[i] ==  ) ); n     y[i] = y[i + 1] + (s[i] ==  ? ); n   } n   long long ans = 0; n   for (long long i = 1; i <= n - 1; i++) { n     ans += l[i] * c(x[i] + y[i + 1], y[i + 1] - l[i] + r[i + 1]) + n            x[i] * c(x[i] + y[i + 1] - 1, y[i + 1] - l[i] + r[i + 1] - 1), n         ans %= 998244353; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(long long ar[], long long n) { n   bool flag = true; n   for (long long i = 1; i < n; i++) { n     if (ar[i] >= (ar[i - 1] + ar[(i + 1) % n])) { n       flag = false; n       break; n     } n   } n   return flag; n } n int32_t main() { n   long long n; n   cin >> n; n   long long temp[n]; n   for (long long i = 0; i < n; i++) { n     cin >> temp[i]; n   } n   sort(temp, temp + n); n   long long ar[n]; n   long long k = 0; n   for (long long i = n - 3; i >= 0; i--, k++) ar[k] = temp[i]; n   ar[k++] = temp[n - 2]; n   ar[k++] = temp[n - 1]; n   if (check(ar, n)) { n     cout <<  YES n ; n     for (auto k : ar) cout << k <<    ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n void sort(int *a, int n) { n   int isSorted = 0; n   int i; n   int tmp; n   while (isSorted == 0) { n     isSorted = 1; n     for (i = 0; i < n - 1; i++) { n       if (a[i] > a[i + 1]) { n         tmp = a[i]; n         a[i] = a[i + 1]; n         a[i + 1] = tmp; n         isSorted = 0; n       } n     } n   } n } n int main() { n   int n, m; n   int p[101]; n   int i; n   int numeven, numodd; n   scanf( %d , &n); n   m = n / 2; n   for (i = 0; i < m; i++) scanf( %d , p + i); n   sort(p, m); n   numeven = 0; n   numodd = 0; n   for (i = 0; i < m; i++) { n     numeven += ((n - i * 2) - p[m - 1 - i]) > 0 ? ((n - i * 2) - p[m - 1 - i]) n                                                 : -((n - i * 2) - p[m - 1 - i]); n   } n   for (i = 0; i < m; i++) { n     numodd += (((i * 2 + 1) - p[i])) > 0 ? (((i * 2 + 1) - p[i])) n                                          : -(((i * 2 + 1) - p[i])); n   } n   if (numeven > numodd) n     printf( %d n , numodd); n   else n     printf( %d n , numeven); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int sum = 0, ff = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) ff = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); n   return sum * ff; n } n int Q, n, a[10005], cnt; n int main() { n   Q = read(); n   for (; Q--;) { n     n = read(); n     cnt = 0; n     if (n == 4) { n       puts( 3 1 4 2 ); n       continue; n     } n     if (n <= 3) n       printf( -1 ); n     else { n       if (n & 1) { n         for (int j = n; j >= 1; j--) n           if (j & 1) a[++cnt] = j; n         a[++cnt] = 4, a[++cnt] = 2; n         if (n >= 6) n           for (int j = 6; j <= n; j++) n             if (j % 2 == 0) a[++cnt] = j; n       } else { n         for (int j = n; j >= 1; j--) n           if (j % 2 == 0) a[++cnt] = j; n         a[++cnt] = 5, a[++cnt] = 1, a[++cnt] = 3; n         if (n >= 7) n           for (int j = 7; j <= n; j++) n             if (j & 1) a[++cnt] = j; n       } n       for (int i = 1; i <= cnt; i++) printf( %d  , a[i]); n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using std::cerr; n using std::cin; n using std::cos; n using std::cout; n using std::endl; n using std::make_pair; n using std::max; n using std::min; n using std::next_permutation; n using std::pair; n using std::priority_queue; n using std::queue; n using std::random_shuffle; n using std::set; n using std::sin; n using std::sort; n using std::sqrt; n using std::stack; n using std::string; n using std::stringstream; n using std::swap; n using std::vector; n const long double PI = 3.14159265358979323846; n int main() { n   vector<string> taxi, pizza, girls; n   int mtaxi, mpizza, mgirls; n   mtaxi = mpizza = mgirls = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int s; n     string name; n     cin >> s >> name; n     int ntaxi, npizza, ngirls; n     ntaxi = npizza = ngirls = 0; n     for (int j = 0; j < s; ++j) { n       string number; n       cin >> number; n       string nomer = number.substr(0, 2); n       nomer += number.substr(3, 2); n       nomer += number.substr(6, 2); n       bool t = true; n       for (int k = 1; k < 6; ++k) t = t && (nomer[k] == nomer[0]); n       if (t) { n         ++ntaxi; n         continue; n       } n       bool p = true; n       for (int k = 1; k < 6; ++k) p = p && (nomer[k] < nomer[k - 1]); n       if (p) n         ++npizza; n       else n         ++ngirls; n     } n     if (ntaxi == mtaxi) { n       taxi.push_back(name); n     } n     if (ntaxi > mtaxi) { n       mtaxi = ntaxi; n       taxi.resize(0); n       taxi.push_back(name); n     } n     if (npizza == mpizza) { n       pizza.push_back(name); n     } n     if (npizza > mpizza) { n       mpizza = npizza; n       pizza.resize(0); n       pizza.push_back(name); n     } n     if (ngirls == mgirls) { n       girls.push_back(name); n     } n     if (ngirls > mgirls) { n       mgirls = ngirls; n       girls.resize(0); n       girls.push_back(name); n     } n   } n   cout <<  If you want to call a taxi, you should call: ; n   for (int i = 0; i < taxi.size() - 1; ++i) cout <<     << taxi[i] <<  , ; n   cout <<     << taxi.back() <<  .  << endl; n   cout <<  If you want to order a pizza, you should call: ; n   for (int i = 0; i < pizza.size() - 1; ++i) cout <<     << pizza[i] <<  , ; n   cout <<     << pizza.back() <<  .  << endl; n   cout <<  If you want to go to a cafe with a wonderful girl, you should call: ; n   for (int i = 0; i < girls.size() - 1; ++i) cout <<     << girls[i] <<  , ; n   cout <<     << girls.back() <<  .  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, k, l, w[200005], h[200005], a, b, m, m1; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &w[i], &h[i]); n     if (h[m] <= h[i]) { n       m1 = m; n       m = i; n     } else if (h[m1] <= h[i]) { n       m1 = i; n     } n     a += w[i]; n   } n   for (i = 1; i <= n; i++) { n     if (i == m) { n       printf( %d  , (a - w[i]) * h[m1]); n     } else n       printf( %d  , (a - w[i]) * h[m]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string rl; n   cin >> rl; n   vector<int> A(n, 0); n   for (int i = 0; i < n; i++) cin >> A[i]; n   int diff = 2147483647; n   int t; n   for (int i = 0; i < n - 1; i++) n     if (rl[i] ==  R  && rl[i + 1] ==  L ) { n       t = (A[i + 1] - A[i]) / 2; n       diff = min(diff, t); n     } n   if (diff == 2147483647) diff = -1; n   cout << diff <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(int n) { cout << n << endl; } n int main() { n   int n, l, sum = 0; n   cin >> n; n   string s; n   s = to_string(n); n   l = s.size(); n   for (int i = l; i > 0; i--) sum += pow(2, i - 1); n   reverse(s.begin(), s.begin() + l); n   for (int i = 0; i < l; i++) n     if (s[i] ==  7 ) sum += pow(2, i); n   print(sum); n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1e18 + 5; n const int mod = (int)1e9 + 7; n const int logN = 17; n const int inf = 1e9; n const int N = 2e5 + 5; n int s, n, m, h1[N], h, t, x, y, z; n vector<int> v[N]; n pair<int, int> e[N]; n void print(int x, int y) { n   cout <<  YES n ; n   vector<int> com, h, t; n   for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) n     h1[*it]++; n   for (__typeof(v[y].begin()) it = v[y].begin(); it != v[y].end(); it++) n     h1[*it]++; n   for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) { n     if (*it == y) continue; n     if (h1[*it] == 1 && h.size() < ::h) h.push_back(*it); n     if (h1[*it] == 2) com.push_back(*it); n   } n   for (__typeof(v[y].begin()) it = v[y].begin(); it != v[y].end(); it++) { n     if (*it == x) continue; n     if (h1[*it] == 1 && t.size() < ::t) t.push_back(*it); n   } n   while (h.size() < ::h) { n     h.push_back(com.back()); n     com.pop_back(); n   } n   while (t.size() < ::t) { n     t.push_back(com.back()); n     com.pop_back(); n   } n   cout << x <<     << y <<   n ; n   for (__typeof(h.begin()) it = h.begin(); it != h.end(); it++) n     cout << *it <<    ; n   cout <<   n ; n   for (__typeof(t.begin()) it = t.begin(); it != t.end(); it++) n     cout << *it <<    ; n   cout <<   n ; n } n int main() { n   scanf( %d %d %d %d , &n, &m, &h, &t); n   int s = 0; n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &x, &y); n     e[++s] = make_pair(x, y); n     e[++s] = make_pair(y, x); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     sort(v[i].begin(), v[i].end()); n   } n   for (int i = 1; i <= s; i++) { n     x = e[i].first; n     y = e[i].second; n     if (v[x].size() >= h + 1 && v[y].size() >= h + t + 1) { n       print(x, y); n       return 0; n     } n     if (v[x].size() >= h + t + 1 && v[y].size() >= t + 1) { n       print(x, y); n       return 0; n     } n     if (v[x].size() <= h || v[y].size() <= t) continue; n     int j = 0; n     int s1 = v[x].size() - 1, s2 = v[y].size() - 1, com = 0; n     for (int tt = 0; tt <= s1; tt++) { n       while (j < v[y].size() && v[y][j] < v[x][tt]) j++; n       if (j < v[y].size() && v[y][j] == v[x][tt]) com++; n     } n     if (s1 + s2 - com >= h + t) { n       print(x, y); n       return 0; n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 2e3 + 505; n struct node { n   double x, y; n } a[mx]; n double pw(double x) { return x * x; } n double dist(node a, node b) { return sqrt(pw(a.x - b.x) + pw(a.y - b.y)); } n double f[2][mx], g[2][mx]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lf%lf , &a[i].x, &a[i].y); n   for (int i = 1; i < n; i++) n     for (int j = 0; j < n; j++) { n       g[i % 2][j] = n           max(g[(i + 1) % 2][(j - 1 + n) % n] + dist(a[j], a[(j - 1 + n) % n]), n               f[(i + 1) % 2][(j - i + n) % n] + dist(a[j], a[(j - i + n) % n])); n       f[i % 2][j] = n           max(f[(i + 1) % 2][(j + 1) % n] + dist(a[j], a[(j + 1) % n]), n               g[(i + 1) % 2][(i + j) % n] + dist(a[j], a[(i + j) % n])); n     } n   double ans = 0; n   for (int i = 0; i < n; i++) n     ans = max(ans, max(f[(n - 1) % 2][i], g[(n - 1) % 2][i])); n   printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 10; n int n; n const int nn = 65536 * 4; n long long sha[maxn]; n long long tree[nn * 2 + 10], tag[nn * 2 + 10]; n long long fix; n long long og[maxn]; n void _down(int id) { n   tree[id * 2] += tag[id], tag[id * 2] += tag[id]; n   tree[id * 2 + 1] += tag[id], tag[id * 2 + 1] += tag[id]; n   tag[id] = 0; n } n void _update(int a, int b, int l, int r, int id) { n   if (l >= a and r <= b) { n     tree[id] += fix; n     tag[id] += fix; n     return; n   } n   _down(id); n   int mid = (l + r) / 2; n   if (a <= mid) _update(a, b, l, mid, id << 1); n   if (b > mid) _update(a, b, mid + 1, r, id << 1 | 1); n   tree[id] = max(tree[id << 1], tree[id << 1 | 1]); n } n long long _query(int a, int b, int l, int r, int id) { n   if (l >= a and r <= b) return tree[id]; n   _down(id); n   int mid = (l + r) / 2; n   if (a <= mid) n     return _query(a, b, l, (l + r) / 2, id << 1); n   else n     return _query(a, b, (l + r) / 2 + 1, r, id << 1 | 1); n } n int _nxt(int l, int r, int id) { n   if (l == r) return l; n   _down(id); n   if (tree[id << 1] >= fix) n     return _nxt(l, (l + r) / 2, id << 1); n   else n     return _nxt((l + r) / 2 + 1, r, id << 1 | 1); n } n int main() { n   int Q; n   scanf( %d%d , &n, &Q); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &sha[i]); n     og[i] = sha[i]; n     sha[i] += sha[i - 1]; n     fix = sha[i]; n     _update(i, i, 1, nn, 1); n   } n   while (Q--) { n     int x; n     long long y; n     scanf( %d%I64d , &x, &y); n     fix = y - og[x]; n     og[x] = y; n     _update(x, n, 1, nn, 1); n     int now = 1; n     long long op; n     int ui = 0; n     while (1) { n       op = _query(now, now, 1, nn, 1); n       if (op == (now != 1 ? _query(now - 1, now - 1, 1, nn, 1) * 2 : 0)) { n         ui = 1; n         printf( %d n , now); n         break; n       } n       fix = op * 2; n       if (fix > tree[1]) break; n       now = _nxt(1, nn, 1); n     } n     if (!ui) puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename A, typename B> n inline char smax(A &a, const B &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename A, typename B> n inline char smin(A &a, const B &b) { n   return b < a ? a = b, 1 : 0; n } n template <typename I> n inline void read(I &x) { n   int f = 0, c; n   while (!isdigit(c = getchar())) c ==  -  ? f = 1 : 0; n   x = c & 15; n   while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); n   f ? x = -x : 0; n } n const int N = 1e5 + 7; n int n; n int b[N], s[N], id[N << 1], pre[N << 1]; n struct wph { n   int d, id; n } a[N]; n inline void add(int x, int fa, int tag = 1) { n   if (tag) s[x] = s[fa] + 1, b[s[x]] = x; n   printf( %d %d n , x, fa); n } n inline void work() { n   std::sort(a + 1, a + n + 1, n             [](const wph &a, const wph &b) { return a.d > b.d; }); n   for (int i = 1; i < n; ++i) n     printf( %d %d n , a[i].id * 2 - 1, a[i + 1].id * 2 - 1); n   for (int i = 1; i <= n; ++i) id[i] = a[i].id * 2 - 1; n   int nn = n; n   for (int i = 1; i <= n; ++i) n     if (i + a[i].d - 1 < nn) n       printf( %d %d n , id[i] + 1, id[i + a[i].d - 1]); n     else n       id[++nn] = id[i] + 1, printf( %d %d n , id[nn], id[nn - 1]); n } n inline void init() { n   read(n); n   for (int i = 1; i <= n; ++i) read(a[i].d), a[i].id = i; n } n int main() { n   init(); n   work(); n   fclose(stdin), fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n void print_mat(vector<vector<long long> > mat) { n   for (long long y = 0; y < (long long)((long long)((mat).size())); ++y) { n     for (long long x = 0; x < (long long)((long long)((mat[y]).size())); ++x) n       printf( %lld  , mat[y][x]); n     puts(  ); n   } n } n vector<vector<long long> > I; n vector<vector<long long> > add(vector<vector<long long> > mat1, n                                vector<vector<long long> > mat2) { n   int h = (long long)((mat1).size()), w = (long long)((mat1[0]).size()); n   assert((long long)((mat2).size()) == h && (long long)((mat2[0]).size()) == w); n   vector<vector<long long> > ret(h, vector<long long>(w, 0)); n   for (long long y = 0; y < (long long)(h); ++y) n     for (long long x = 0; x < (long long)(w); ++x) n       ret[y][x] = mat1[y][x] + mat2[y][x]; n   return ret; n } n vector<long long> mul(vector<vector<long long> > mat, vector<long long> vec) { n   int h = (long long)((mat).size()), w = (long long)((mat[0]).size()); n   assert((long long)((vec).size()) == w); n   vector<long long> ret(h, 0); n   for (long long y = 0; y < (long long)(h); ++y) n     for (long long x = 0; x < (long long)(w); ++x) n       ret[y] = (ret[y] + mat[y][x] * vec[x] % MOD) % MOD; n   return ret; n } n vector<vector<long long> > mul(vector<vector<long long> > mat1, n                                vector<vector<long long> > mat2) { n   int h = (long long)((mat1).size()), in = (long long)((mat1[0]).size()), n       w = (long long)((mat2[0]).size()); n   assert((long long)((mat2).size()) == in); n   vector<vector<long long> > ret(h, vector<long long>(w, 0)); n   for (long long y = 0; y < (long long)(h); ++y) n     for (long long x = 0; x < (long long)(w); ++x) n       for (long long i = 0; i < (long long)(in); ++i) { n         ret[y][x] = (ret[y][x] + mat1[y][i] * mat2[i][x] % MOD) % MOD; n       } n   return ret; n } n vector<vector<long long> > pow(vector<vector<long long> > mat, n                                long long power) { n   int n = (long long)((mat).size()); n   vector<vector<long long> > ret(n, vector<long long>(n, 0)); n   for (long long i = 0; i < (long long)(n); ++i) ret[i][i] = 1; n   while (power > 0) { n     if (power & 1) ret = mul(ret, mat); n     mat = mul(mat, mat); n     power >>= 1; n   } n   return ret; n } n vector<vector<long long> > powadd(vector<vector<long long> > mat, n                                   long long power) { n   int n = (long long)((mat).size()); n   vector<vector<long long> > ret(n, vector<long long>(n, 0)); n   for (long long i = 0; i < (long long)(n); ++i) ret[i][i] = 1; n   if (power == 0) return ret; n   if (power % 2 == 1) { n     ret = powadd(mat, power / 2); n     ret = mul(ret, add(I, pow(mat, power / 2 + 1))); n   } else { n     ret = add(pow(mat, power), powadd(mat, power - 1)); n   } n   return ret; n } n long long solve(long long L) { n   if (L == 0) return 0LL; n   if (L == 1) return 4; n   vector<vector<long long> > mat = n       vector<vector<long long> >(5, vector<long long>(5, 0)); n   for (long long i = 0; i < (long long)(5); ++i) n     for (long long j = 0; j < (long long)(5); ++j) n       if (abs(i - j) == 1) mat[i][j] = 1; n   vector<long long> vec = vector<long long>(5, 0); n   vec[0] = vec[4] = 1; n   vec[1] = vec[2] = vec[3] = 2; n   vector<long long> piyo = mul(powadd(mat, L - 2), vec); n   long long res = 4; n   for (long long i = 0; i < (long long)(5); ++i) res = (res + piyo[i]) % MOD; n   return res; n } n long long solve2(long long L) { n   return (solve(L) + solve((L + 1) / 2)) * ((MOD + 1) / 2) % MOD; n } n int main() { n   I = vector<vector<long long> >(5, vector<long long>(5, 0)); n   for (long long i = 0; i < (long long)(5); ++i) I[i][i] = 1; n   long long L, R; n   while (cin >> L >> R) { n     cout << (solve2(R) - solve2(L - 1) + MOD) % MOD << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int pc; n void PrintCase() { printf( Case %lli:  , ++pc); } n long long int GCD(long long int a, long long int b) { n   if (a < b) swap(a, b); n   if (b == 1) return 1; n   if (!b) return a; n   if (!(a % b)) n     return b; n   else n     return (a, a % b); n } n long long int LCM(long long int a, long long int b) { n   return (a * b) / GCD(a, b); n } n long long int bigmod(long long int n, long long int p) { n   if (p == 0) return 1; n   if (p == 1) return (n + 1000000007) % 1000000007; n   if (p % 2) n     return (bigmod(n, p - 1) * n + 1000000007) % 1000000007; n   else { n     long long int x = bigmod(n, p / 2); n     return (x * x + 1000000007) % 1000000007; n   } n } n long long int modinverse(long long int n) { n   return bigmod(n, 1000000007 - 2) % 1000000007; n } n bool sc[100007]; n vector<long long int> pr; n void seieve() { n   for (long long int i = 2; i <= 100007; i++) { n     if (!sc[i]) { n       pr.push_back(i); n       for (long long int j = i * i; j <= 100007; j += i) sc[j] = 1; n     } n   } n } n pair<long long int, long long int> pf[100007]; n void pri_fact(long long int n) { n   for (long long int i = 0; i < pr.size() && n != 1; i++) { n     pf[i].first = pr[i]; n     long long int c = 0; n     while (!(n % pr[i])) { n       n /= pr[i]; n       c++; n     } n     pf[i].second = c; n   } n } n vector<long long int> de; n void devisors(long long int n) { n   long long int i; n   for (i = 1; i <= sqrt(n); i++) { n     if (!(n % i) && i != sqrt(n)) { n       de.push_back(i); n       de.push_back(n % i); n     } n   } n   if ((i - 1) * (i - 1) == n) de.push_back(i - 1); n   sort(de.begin(), de.end()); n } n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, h, a, b, k, i, p, q, u, v; n   cin >> n >> h >> a >> b >> k; n   for (i = 0; i < k; i++) { n     cin >> p >> q >> u >> v; n     long long int x = abs(p - u); n     if (!x) { n       cout << abs(q - v) << endl; n     } else if (q < a && v < a) { n       cout << x + (a - q) + (a - v) << endl; n     } else if (q > b && v > b) { n       cout << x + (q - b) + (v - b) << endl; n     } else { n       cout << x + abs(q - v) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-10; n template <typename T, typename S> n vector<T>& operator<<(vector<T>& a, S b) { n   a.push_back(b); n   return a; n } n template <typename T> n void operator>>(vector<T>& a, int b) { n   while (b--) n     if (!a.empty()) a.pop_back(); n } n bool isprime(int n) { n   if (n < 2) return false; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n long long b_pow(long long x, long long n) { n   return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; n } n string itos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n vector<int> pos[10]; n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   int n, m; n   cin >> n >> m; n   cin >> s; n   int e[10] = {0}; n   for (int i = 0; (int)i < (int)s.size(); i++) { n     e[s[i] -  0 ]++; n     pos[s[i] -  0 ].push_back(i); n   } n   string ans; n   vector<int> can; n   int mi = INT_MAX; n   pair<int, string> W = make_pair(INT_MAX,  ~ ); n   for (int X = 0; X < 10; X++) { n     if (e[X] < m) { n       string mur = s; n       int sum = 0; n       int t = m - e[X]; n       for (int i = 1; i <= 9; i++) { n         int x = X - i; n         int y = X + i; n         if (y <= 9) { n           int j = 0; n           while (j < pos[y].size() && t) { n             mur[pos[y][j]] = X +  0 ; n             sum += i; n             t--; n             j++; n           } n         } n         if (x >= 0) { n           int j = pos[x].size() - 1; n           while (j >= 0 && t) { n             mur[pos[x][j]] = X +  0 ; n             sum += i; n             t--; n             j--; n           } n         } n       } n       W = min(W, make_pair(sum, mur)); n     } else if (e[X] >= m) { n       cout << 0 << endl; n       cout << s << endl; n       return 0; n     } n   } n   cout << W.first << endl; n   cout << W.second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long mod = 1000000007; n const long long INF18 = 1000000000000000000; n long long n; n long long a[1010]; n bool ok = false; n void rec(long long depth, long long cnt, long long sum) { n   if (ok) return; n   if (depth >= n) { n     if (cnt < 1) return; n     if (sum == 0) ok = true; n     return; n   } n   rec(depth + 1, cnt, sum); n   rec(depth + 1, cnt + 1, sum + a[depth]); n   rec(depth + 1, cnt + 1, sum - a[depth]); n } n void solve() { n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) { n     if (a[i] == 0) { n       cout <<  YES n ; n       return; n     } n     for (long long j = 0; j < n; j++) { n       if (i == j) continue; n       if (a[i] == a[j]) { n         cout <<  YES n ; n         return; n       } n     } n   } n   ok = false; n   rec(0, 0, 0); n   if (ok) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int FuN(long long n) { n   string s; n   while (n != 0) { n     int ch = (n % 2); n     s += ch +  0 ; n     n /= 2; n   } n   int x = -1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) x = i + 1; n   } n   return x; n } n int main() { n   int t, n, i, j; n   t = 1; n   cin >> t; n   while (t--) { n     long long ans = 0, fg = 1, a[200005], cnt = 0, x; n     cin >> n; n     map<long long, long long> m; n     for (i = 1; i <= n; i++) { n       cin >> a[i]; n       x = FuN(a[i]); n       m[x]++; n     } n     for (auto it : m) { n       ans += (it.second * (it.second - 1)) / 2; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n const double EPS = 1e-9; n const double PI = acos(-1.); n const int INF = 1e9; n const int MAXN = 2e5 + 5; n const long long MOD = 1e9 + 7; n void output_path(int n, int a, int b, int c, int d) { n   cout << a <<     << c <<    ; n   for (int i = (1); i <= (int)(n); i++) { n     if (i != a && i != b && i != c && i != d) { n       cout << i <<    ; n     } n   } n   cout << d <<     << b << endl; n } n void solve() { n   int n, k, a, b, c, d; n   cin >> n >> k >> a >> b >> c >> d; n   if (n == 4 || k <= n) { n     cout << -1 << endl; n     return; n   } n   output_path(n, a, b, c, d); n   output_path(n, c, d, a, b); n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n bool is_valid_streak(map<ll, ll>& unique_counts) { n   if (unique_counts.size() == 2 && unique_counts.begin()->first == 1 && n       unique_counts.begin()->second == 1) { n     return true; n   } else if (unique_counts.size() == 2 && n              unique_counts.begin()->first + 1 == n                  unique_counts.rbegin()->first && n              unique_counts.rbegin()->second == 1) { n     return true; n   } n   return false; n } n int main() { n   ll n; n   cin >> n; n   vector<ll> a(n); n   map<ll, ll> counts; n   map<ll, ll> unique_counts; n   set<ll> unique_values; n   for (ll i = 0; i < n; i++) { n     cin >> a[i]; n   } n   ll ans = 0; n   for (ll i = 0; i < n; i++) { n     if (counts[a[i]] != 0) { n       unique_counts[counts[a[i]]]--; n       if (unique_counts[counts[a[i]]] == 0) { n         unique_counts.erase(counts[a[i]]); n       } n     } n     counts[a[i]]++; n     unique_counts[counts[a[i]]]++; n     unique_values.insert(a[i]); n     if (unique_values.size() == 1) { n       ans = i + 1; n     } else if (unique_values.size() == i + 1) { n       ans = i + 1; n     } else if (is_valid_streak(unique_counts)) { n       ans = i + 1; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int num = 0, w = 0; n   char ch = 0; n   while (!isdigit(ch)) { n     w |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = (num << 3) + (num << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return w ? -num : num; n } n int n, m; n vector<int> c[1005], r[1005]; n char ch[1005][1005]; n bool vis[1005], vis2[1005]; n int main() { n   n = read(), m = read(); n   int numj = 0, numi = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> ch[i][j]; n       if (ch[i][j] ==  # ) { n         c[i].push_back(j); n         r[j].push_back(i); n         if (!vis[j]) { n           numj++; n           vis[j] = true; n         } n         if (!vis2[i]) { n           vis2[i] = true; n           numi++; n         } n       } n     } n   } n   int tag; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (ch[i][j] ==  . ) { n         if (c[i].size()) { n           int len = c[i].size(); n           if (j > c[i][len - 1] || j < c[i][0]) n             tag = 1; n           else { n             cout << -1; n             return 0; n           } n         } n         if (r[j].size()) { n           int len = r[j].size(); n           if (i > r[j][len - 1] || i < r[j][0]) n             tag = 1; n           else { n             cout << -1; n             return 0; n           } n         } n       } n     } n   } n   int pre, aft, minl = 1005, maxl = 0, ans = 0, rest = 0; n   for (int i = 1; i <= n; i++) { n     pre = 0; n     for (int j = 1; j <= m; j++) { n       if (ch[i][j] ==  # ) { n         if (!pre) pre = j; n         if (j == m || ch[i][j + 1] ==  . ) aft = j; n       } n     } n     if (!pre) { n       rest++; n       continue; n     } n     if (minl == 0) { n       minl = pre; n       maxl = aft; n       ans++; n     } else if (pre > maxl) { n       ans++; n       minl = pre; n       maxl = aft; n     } else if (aft < minl) { n       minl = pre; n       maxl = aft; n       ans++; n     } else { n       maxl = max(maxl, aft); n       minl = min(minl, pre); n     } n   } n   if (numj == m && rest) { n     cout << -1; n     return 0; n   } n   rest = 0; n   for (int i = 1; i <= m; i++) { n     int flag = 0; n     for (int j = 1; j <= n; j++) { n       if (ch[j][i] ==  # ) { n         flag = 1; n         break; n       } n     } n     if (!flag && numi == n) { n       cout << -1; n       return 0; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, max = 0, count1 = 0; n     cin >> n; n     vector<int> v; n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       v.push_back(x); n       if (max < x) { n         max = x; n       } n     } n     int c = v[0]; n     for (int i = 0; i < v.size(); i++) { n       if (v[i] == c) { n         count1++; n       } n       c = v[i]; n     } n     if (count1 == n) { n       cout <<  0  << endl; n     } else { n       vector<int> v1(10000); n       map<int, int> mp; n       long long int sum; n       for (int i = 0; i < n; i++) { n         sum = v[i]; n         for (int j = i + 1; j < n; j++) { n           sum = sum + v[j]; n           if (sum <= 8000) { n             v1[sum] = 1; n           } n         } n       } n       int count = 0; n       for (int i = 0; i < n; i++) { n         if (v1[v[i]]) { n           count++; n         } n       } n       cout << count << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n namespace jumpmelon { n long long divfloor(long long a, long long b) { n   long long k = a / b; n   if (k * b > a) k--; n   return k; n } n long long divceil(long long a, long long b) { n   long long k = a / b; n   if (k * b < a) k++; n   return k; n } n void work() { n   long long n, i_l, i_r, i_k; n   scanf( %lld%lld%lld%lld , &n, &i_l, &i_r, &i_k); n   long long d = i_l <= i_r ? i_r - i_l + 1 : i_r + n - i_l + 1; n   long long s = i_k - d, v = n - d + 1, ans = -1; n   if (n <= 1000000) { n     for (long long r = 0; r <= n; r++) { n       long long a = max(0LL, r - v), b = min(d, r); n       long long mink = max(0LL, divceil(s - b, n + r)); n       long long maxk = divfloor(s - a, n + r); n       if (mink <= maxk) ans = max(ans, r); n     } n   } else { n     for (long long k = 0; k * n <= s; k++) { n       long long t = s - k * n; n       long long minr = 0, maxr = n; n       if (k) maxr = min(maxr, divfloor(t, k)); n       maxr = min(maxr, divfloor(t + v, k + 1)); n       if (k) n         minr = max(minr, divceil(t - d, k)); n       else if (t > d) n         continue; n       minr = max(minr, divceil(t, k + 1)); n       if (minr <= maxr) ans = max(ans, maxr); n     } n   } n   printf( %lld n , ans); n } n }  // namespace jumpmelon n int main() { n   jumpmelon::work(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 1e5 + 10; n multiset<int> ms; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     ms.insert(x); n   } n   int cur = 1; n   while (!ms.empty()) { n     if (cur <= *ms.begin()) { n       ms.erase(ms.find(*ms.begin())); n       cur++; n     } else { n       ms.erase(ms.find(*ms.begin())); n     } n   } n   cout << cur << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[100][100]; n struct point { n  public: n   int f, t; n   point() {} n   point(int a, int b) { f = a, t = b; } n }; n int main() { n   int X, k; n   cin >> X >> k; n   int val, sum = 0; n   vector<pair<int, int>> a, b; n   for (int i = 0; i < (X); i++) { n     cin >> val; n     sum += val; n     a.push_back(make_pair(val, i + 1)); n   } n   sort((a).begin(), (a).end()); n   int cnt = 0; n   while (k--) { n     int di = a[X - 1].first - a[0].first; n     if (di > 1) { n       a[X - 1].first--; n       a[0].first++; n       b.push_back(make_pair(a[X - 1].second, a[0].second)); n       sort((a).begin(), (a).end()); n       cnt++; n     } else n       break; n   } n   cout << a[X - 1].first - a[0].first <<     << cnt << endl; n   for (int j = 0; j < (cnt); j++) n     cout << b[j].first <<     << b[j].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n const int maxm = 5005; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int n; n   scanf( %d , &n); n   int a[maxn]; n   bool exist[maxm]; n   for (int i = 0; i < maxm; ++i) exist[i] = true; n   int i = 2; n   while (i < maxm) { n     int j = i + i; n     if (exist[i]) { n       while (j < maxm) { n         exist[j] = false; n         j += i; n       } n     } n     i += 1; n   } n   vector<int> primes; n   for (int i = 2; i < maxm; ++i) n     if (exist[i]) { n       primes.push_back(i); n     } n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   int cur_gcd = gcd(a[0], a[1]); n   for (int i = 2; i < n; ++i) n     if (cur_gcd != 1) cur_gcd = gcd(cur_gcd, a[i]); n   for (int i = 0; i < n; ++i) a[i] = a[i] / cur_gcd; n   vector<int> nums; n   for (int i = 0; i < n; ++i) { n     int cur = a[i]; n     for (int j = 0; j < primes.size(); ++j) n       if (primes[j] > cur) n         break; n       else if (cur % primes[j] == 0) { n         nums.push_back(primes[j]); n         while (cur % primes[j] == 0) { n           cur = cur / primes[j]; n         } n       } n     if (cur > 1) nums.push_back(cur); n   } n   sort(nums.begin(), nums.end()); n   int ans = 0; n   int left = 0, right = 0; n   while (right < nums.size()) { n     while (right < nums.size() && nums[right] == nums[left]) right++; n     if (right - left > ans) { n       ans = right - left; n     } n     left = right; n   } n   if (ans == 0 || ans == n) n     cout << -1 << endl; n   else n     cout << n - ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[300010]; n int main() { n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     int prvi = -1, br_jednakih = 1, maxi = -1; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       maxi = max(maxi, a[i]); n       if (i == 0) { n         prvi = a[i]; n         continue; n       } n       if (a[i] == prvi) { n         br_jednakih++; n       } n     } n     if (br_jednakih == n) { n       cout << -1 << endl; n       continue; n     } n     if (a[0] == maxi && a[1] != a[0]) { n       cout << 1 << endl; n       continue; n     } n     bool da = false; n     for (int i = 1; i < n - 1; i++) { n       if (a[i] == maxi && (a[i + 1] != maxi || a[i - 1] != maxi) && !da) { n         cout << i + 1 << endl; n         da = true; n         break; n       } n     } n     if (da) continue; n     if (a[n - 1] == maxi && a[n - 2] != maxi) { n       cout << n << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n void parse(int size, std::string& input, int& pos, int depth, int& max_depth, n            std::vector<std::vector<std::string> >& comments) { n   if (size <= pos) { n     return; n   } n   if (depth > max_depth) { n     max_depth = depth; n     comments.push_back(std::vector<std::string>()); n   } n   int end = pos; n   while (input[end++] !=  , ) { n   } n   comments[depth].push_back(input.substr(pos, end - pos - 1)); n   pos = end; n   while (end < size && input[end++] !=  , ) { n   } n   if (end == size) { n     ++end; n   } n   int children = std::stoi(input.substr(pos, end - pos - 1)); n   pos = end; n   for (int i = 0; i < children; ++i) { n     parse(size, input, pos, depth + 1, max_depth, comments); n   } n } n int main() { n   std::string input; n   std::cin >> input; n   std::vector<std::vector<std::string> > comments; n   int max_depth = -1, pos = 0, size = input.size(); n   while (pos < size) { n     parse(size, input, pos, 0, max_depth, comments); n   } n   std::cout << max_depth + 1 << std::endl; n   bool not_first; n   for (std::vector<std::string>& level : comments) { n     not_first = false; n     for (std::string& comment : level) { n       if (not_first) { n         std::cout <<    ; n       } else { n         not_first = true; n       } n       std::cout << comment; n     } n     std::cout << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, CA, CB; n struct point { n   int x, y; n   bool operator<(const point &p) const { return x != p.x ? x < p.x : y < p.y; } n } st[201000]; n int BX[201000], EX[201000], BY[201000], EY[201000]; n long long res; n void Pro(vector<point> &w, int *B, int *E) { n   int i; n   int top = 0; n   for (auto &tp : w) { n     while (top && st[top].x + st[top].y >= tp.x + tp.y) top--; n     if (top && st[top].y - st[top].x <= tp.y - tp.x) continue; n     st[++top] = tp; n   } n   int pv = 1; n   for (i = 1; i <= 200020; i++) { n     while (pv < top && n            st[pv].y + abs(i - st[pv].x) >= st[pv + 1].y + abs(i - st[pv + 1].x)) n       pv++; n     B[i] = st[pv].y + abs(i - st[pv].x); n   } n   top = 0; n   for (auto &tp : w) { n     while (top && st[top].y - st[top].x <= tp.y - tp.x) top--; n     if (top && st[top].y + st[top].x >= tp.y + tp.x) continue; n     st[++top] = tp; n   } n   pv = 1; n   for (i = 1; i <= 200020; i++) { n     while (pv < top && n            st[pv].y - abs(i - st[pv].x) <= st[pv + 1].y - abs(i - st[pv + 1].x)) n       pv++; n     E[i] = st[pv].y - abs(i - st[pv].x); n     if (B[i] > E[i]) B[i] = 1, E[i] = 0; n   } n } n struct AA { n   int t, y, ck; n   bool operator<(const AA &p) const { return t < p.t; } n } T[401000]; n int BIT[201000][2]; n void Add(int a, int ck, int b) { n   while (a <= 200020) { n     BIT[a][ck] += b; n     a += (a & -a); n   } n } n int Sum(int a, int ck) { n   if (a < 0) return 0; n   int r = 0; n   while (a) { n     r += BIT[a][ck]; n     a -= (a & -a); n   } n   return r; n } n void Do(vector<point> w, int ck) { n   if (w.empty()) return; n   int i; n   sort(w.begin(), w.end()); n   Pro(w, BX, EX); n   for (i = 0; i < w.size(); i++) swap(w[i].x, w[i].y); n   sort(w.begin(), w.end()); n   Pro(w, BY, EY); n   int cnt = 0; n   for (i = 1; i <= 200020; i++) { n     BIT[i][0] = BIT[i][1] = 0; n     T[cnt++] = {BY[i], i, 1}; n     T[cnt++] = {EY[i] + 1, i, -1}; n   } n   sort(T, T + cnt); n   int pv = 0; n   for (i = 1; i <= 200020; i++) { n     while (pv < cnt && T[pv].t <= i) { n       Add(T[pv].y, T[pv].y % 2, T[pv].ck); n       pv++; n     } n     if (BX[i] > EX[i]) continue; n     res += Sum(EX[i], (ck + i) % 2) - Sum(BX[i] - 1, (ck + i) % 2); n   } n } n int main() { n   int i, x, y; n   scanf( %d , &n); n   vector<point> A, B; n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     x += 1e5 + 10; n     y += 1e5 + 10; n     if ((x + y) % 2) { n       A.push_back({x, y}); n     } else { n       B.push_back({x, y}); n     } n   } n   Do(A, 0); n   Do(B, 1); n   printf( %lld n , res); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, h; n long long r(long long l, long long s, long long e, long long d) { n   if (s + 1 == e) return 0; n   long long m = 1 + (s + e) >> 1; n   long long k = n       (n < m ? (d ? (1ll << (h - l)) : 1) : (d ? 1 : (1ll << (h - l)))); n   if (n < m) return r(l + 1, s, m, 1) + k; n   return r(l + 1, m, e, 0) + k; n } n int main() { n   cin >> h >> n; n   --n; n   cout << r(0, 0, 1ll << h, 0); n } n


#include <bits/stdc++.h> n using namespace std; n int ar[200010]; n vector<int> ans; n void solve() { n   int n, a, b, k, cnt = 0; n   cin >> n >> a >> b >> k; n   string s; n   cin >> s; n   int nxt = n + 1; n   for (int i = n; i > 0; i--) { n     if (s[i - 1] ==  1 ) { n       cnt += (nxt - i - 1) / b; n       nxt = i; n     } n     ar[i] = nxt; n   } n   cnt += (nxt - 1) / b; n   int last = 0; n   if (cnt < a) { n     printf( 0 n ); n     return; n   } n   for (int i = 1; i <= n; i++) { n     if (s[i - 1] ==  1 ) { n       last = i; n       continue; n     } n     if (i - last == b) { n       cnt--; n       ans.push_back(i); n       last = i; n       if (cnt < a) break; n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   int n, d, h; n   cin >> n >> d >> h; n   if (d > 2 * h || (d == 1 && (n - 1 > d))) { n     cout << -1 << endl; n   } else { n     for (int i = 1; i <= h; i++) { n       cout << i <<     << i + 1 << endl; n     } n     if (d > h) { n       cout << 1 <<     << h + 2 << endl; n       for (int i = h + 2; i <= d; i++) { n         cout << i <<     << i + 1 << endl; n       } n     } n     if (n - 1 > d) { n       if (h > 1) { n         for (int i = d + 2; i <= n; i++) { n           cout << 2 <<     << i << endl; n         } n       } else { n         for (int i = d + 2; i <= n; i++) { n           cout << 1 <<     << i << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int a[105]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int c = 0; n   for (int i = 0; i < n;) { n     if (a[i] == 1) { n       while (a[i] == 1 || a[i + 1] == 1) { n         c++; n         i++; n         if (i == n) break; n       } n     } n     i++; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n int diru[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int dirv[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n using namespace std; n template <class T> n T sq(T n) { n   return n * n; n } n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <class T> n bool inside(T a, T b, T c) { n   return a <= b && b <= c; n } n template <class T> n void setmax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n void setmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n T power(T N, T P) { n   return (P == 0) ? 1 : N * power(N, P - 1); n } n pair<int, int> in[110]; n int main() { n   int n, T, t = 1, m, i, j, k; n   while (scanf( %d , &n) == 1) { n     scanf( %d , &k); n     double ans = 0; n     for (i = 0; i < n; i++) { n       scanf( %d , &in[i].first); n       scanf( %d , &in[i].second); n     } n     for (i = 1; i < n; i++) { n       ans += sqrt((double)(( n           ((in[i].first - in[i - 1].first) * (in[i].first - in[i - 1].first)) + n           ((in[i].second - in[i - 1].second) * n            (in[i].second - in[i - 1].second))))); n     } n     ans /= (double)50.0; n     printf( %.15lf n , ans * (double)k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double m1 = 200.0, m2 = 0.0, DV; n int card, n, k, Q, getcard; n int a[110], vs[110], b[110], len = 0; n int sum = 0, tmp; n int main() { n   memset(vs, 0, sizeof(vs)); n   scanf( %d%d , &n, &k); n   card = (int)(n / k); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   scanf( %d , &Q); n   for (int i = 1; i <= Q; ++i) { n     sum = 0; n     for (int j = 1; j <= card; ++j) { n       scanf( %d , &tmp); n       sum += a[tmp]; n       vs[tmp] = 1; n     } n     DV = double(sum) / double(card); n     if (DV < m1) m1 = DV; n     if (DV > m2) m2 = DV; n   } n   for (int i = 1; i <= n; ++i) n     if (!vs[i]) b[++len] = a[i]; n   getcard = (n - len) / card; n   if (len >= card && getcard < k) { n     sort(b + 1, b + len + 1); n     sum = 0; n     for (int i = 1; i <= card; ++i) sum += b[i]; n     DV = double(sum) / double(card); n     if (DV < m1) m1 = DV; n     if (DV > m2) m2 = DV; n     sum = 0; n     for (int i = len - card + 1; i <= len; ++i) sum += b[i]; n     DV = double(sum) / double(card); n     if (DV < m1) m1 = DV; n     if (DV > m2) m2 = DV; n   } n   printf( %.10lf %.10lf n , m1, m2); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int a = 0, b = 0, r = 0; n   while (true) { n     a = (int)n / 36; n     r = n % 36; n     b = (int)r / 3; n     if (r % 3 == 2) b += 1; n     if (b >= 12) n       n = b * 3 + a * 36; n     else n       break; n   } n   cout << a <<     << b << endl; n   ; n   return 0; n } n