#include <bits/stdc++.h> n const int MAXN = 1e5 * 5; n const int INF = 1e9 + 7; n using namespace std; n long long f[501][501]; n long long a[501][501]; n int n, p[501]; n long long ans[501]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int t = n; t >= 1; --t) { n     int u = p[t]; n     long long res = 0; n     for (int i = t + 1; i <= n; i++) { n       int x = p[i]; n       f[u][x] = a[u][x]; n       f[x][u] = a[x][u]; n       for (int j = t + 1; j <= n; j++) n         if (i != j) { n           int y = p[j]; n           f[u][x] = min(f[u][x], a[u][y] + f[y][x]); n           f[x][u] = min(f[x][u], f[x][y] + a[y][u]); n         } n     } n     for (int i = t + 1; i <= n; i++) n       for (int j = t + 1; j <= n; j++) { n         int x = p[i], y = p[j]; n         f[x][y] = min(f[x][y], f[x][u] + f[u][y]); n       } n     for (int i = t; i <= n; i++) n       for (int j = t; j <= n; j++) res += f[p[i]][p[j]]; n     ans[t] = res; n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 100010; n long long x[mn], y[mn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld %lld , &x[i], &y[i]); n   if (n <= 4) { n     printf( YES n ); n     return 0; n   } n   bool flag = 0; n   for (int i = 1; i <= 2; i++) { n     for (int j = i + 1; j <= 3; j++) { n       long long a = y[j] - y[i]; n       long long b = x[i] - x[j]; n       long long c = y[i] * x[j] - y[j] * x[i]; n       long long xa, ya, xb, yb; n       int t = 0; n       int k = 1; n       for (k = 1; k <= n; k++) { n         if (a * x[k] + b * y[k] + c == 0) n           continue; n         else { n           t++; n           if (t == 1) n             xa = x[k], ya = y[k]; n           else if (t == 2) n             xb = x[k], yb = y[k]; n           else n             break; n         } n       } n       if (t <= 2) { n         flag = 1; n         break; n       } n       long long ta = yb - ya; n       long long tb = xa - xb; n       long long tc = ya * xb - yb * xa; n       bool q = 0; n       for (int p = k; p <= n; p++) { n         if ((a * x[p] + b * y[p] + c == 0) || ta * x[p] + tb * y[p] + tc == 0) n           continue; n         else n           q = 1; n         if (q) break; n       } n       if (!q) { n         flag = 1; n         break; n       } n     } n     if (flag) break; n   } n   if (!flag) n     printf( NO n ); n   else n     printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n vector<int> divs[N]; n vector<int> mult[N]; n int mu[N], a[N], n; n int cnt[N]; n long long res = 0; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   mu[1] = 1; n   for (int i = 1; i < N; i++) { n     divs[i].push_back(i); n     for (int j = 2 * i; j < N; j += i) { n       divs[j].push_back(i); n       mu[j] -= mu[i]; n     } n   } n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     for (int e : divs[a[i]]) mult[e].push_back(a[i] / e); n   } n   for (int i = 1; i < N; i++) { n     sort(mult[i].begin(), mult[i].end(), n          [](int first, int second) { return first > second; }); n     stack<int> st; n     for (int e : mult[i]) { n       int ans = 0; n       for (int div : divs[e]) { n         ans -= mu[div] * cnt[div]; n       } n       while (ans != 0) { n         int u = st.top(); n         st.pop(); n         for (int div : divs[u]) { n           cnt[div]--; n           if (e % div == 0) ans += mu[div]; n         } n         if (ans == 0) res = max(res, 1LL * i * e * u); n       } n       st.push(e); n       for (int div : divs[e]) cnt[div]++; n     } n     for (int e : mult[i]) { n       for (int div : divs[e]) cnt[div] = 0; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n const double PI = acos(-1.0); n int main() { n   double n, r; n   scanf( %lf%lf , &n, &r); n   double p1 = PI / n, p2 = PI / (2 * n), p3 = PI - p1 - p2; n   double l3 = r, l2 = l3 / sin(p3) * sin(p2); n   double s = l3 * l2 * sin(p1); n   printf( %.15f n , n * s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int tas[101], cal[101]; n   int dp1[10001], dp2[10001]; n   int w1[101][3], w2[101][3]; n   while ((cin >> n >> k)) { n     int s = 0, t = 0; n     int sum1 = 0, sum2 = 0, sum = 0; n     for (int i = 1; i <= n; ++i) cin >> tas[i]; n     for (int j = 1; j <= n; ++j) cin >> cal[j]; n     for (int i = 1; i <= n; ++i) { n       int data; n       data = tas[i] - k * cal[i]; n       if (data > 0) { n         w1[++s][1] = data; n         w1[s][2] = tas[i]; n         sum1 += data; n       } else if (data < 0) { n         data = -data; n         w2[++t][1] = data; n         w2[t][2] = tas[i]; n         sum2 += data; n       } else n         sum += tas[i]; n     } n     int max = sum1 <= sum2 ? sum1 : sum2; n     int temp = 0; n     for (int i = 1; i <= max; ++i) { n       memset(dp1, -10000000, sizeof(dp1)); n       memset(dp2, -10000000, sizeof(dp2)); n       dp1[0] = dp2[0] = 0; n       for (int j = 1; j <= s; ++j) { n         for (int v = i; v >= w1[j][1]; --v) { n           if (dp1[v] < dp1[v - w1[j][1]] + w1[j][2]) n             dp1[v] = dp1[v - w1[j][1]] + w1[j][2]; n         } n       } n       for (int j = 1; j <= t; ++j) { n         for (int v = i; v >= w2[j][1]; --v) { n           if (dp2[v] < dp2[v - w2[j][1]] + w2[j][2]) n             dp2[v] = dp2[v - w2[j][1]] + w2[j][2]; n         } n       } n       if (dp1[i] > 0 && dp2[i] > 0) { n         if (dp1[i] + dp2[i] > temp) temp = dp1[i] + dp2[i]; n       } n     } n     sum += temp; n     if (sum == 0) n       cout <<  -1  << endl; n     else n       cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m, k, t, mx; n   cin >> t; n   while (t--) { n     cin >> n >> m >> k; n     mx = min(n / k, m); n     cout << (m >= mx ? mx - ceil((long double)(m - mx) / (k - 1)) : mx) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s =  FESTIVAL ; n long long dp[1010][10]; n int a[1010]; n int main(void) { n   int i, j, k; n   dp[0][0] = 1; n   for ((i) = 0; (i) < (int)(1000); (i)++) n     for ((j) = 0; (j) < (int)(8); (j)++) n       for ((k) = 0; (k) < (int)(8); (k)++) n         if (j <= k) dp[i + 1][k] = min(dp[i + 1][k] + dp[i][j], (1ll << 60)); n   long long K; n   cin >> K; n   for (i = 25; i >= 1; i--) { n     a[i] = K / dp[i][7]; n     K %= dp[i][7]; n   } n   string s; n   for ((i) = 0; (i) < (int)(25); (i)++) { n     s +=  FESTIVA ; n     for ((j) = 0; (j) < (int)(a[i + 1]); (j)++) s +=  L ; n   } n   cout << s <<     <<  FESTIVAL  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 , c = getchar(); n   } n   return x * f; n } n const int MAXN = 1e5 + 10; n const long long INF = 3e18; n int n; n long long x[MAXN], y[MAXN], z[MAXN], foo, bar, baz; n inline void Min(long long &x, long long y) { n   if (x > y) x = y; n } n inline void Max(long long &x, long long y) { n   if (x < y) x = y; n } n inline bool check(long long mid) { n   static long long L[5], R[5]; n   for (int i = 1; i <= 4; i++) L[i] = -INF, R[i] = INF; n   for (int i = 1; i <= n; i++) { n     long long A = x[i], B = y[i], C = z[i]; n     Max(L[1], A + B + C - mid); n     Min(R[1], A + B + C + mid); n     Max(L[2], -A + B + C - mid); n     Min(R[2], -A + B + C + mid); n     Max(L[3], A - B + C - mid); n     Min(R[3], A - B + C + mid); n     Max(L[4], A + B - C - mid); n     Min(R[4], A + B - C + mid); n   } n   for (int i = 0; i < 2; i++) { n     static long long l[5], r[5]; n     for (int j = 1; j <= 4; j++) n       l[j] = ((L[j] & 1) == i) ? L[j] : L[j] + 1, n       r[j] = ((R[j] & 1) == i) ? R[j] : R[j] - 1; n     long long low = l[2] + l[3] + l[4]; n     long long high = r[2] + r[3] + r[4]; n     for (int j = 1; j <= 4; j++) n       if (l[j] > r[j]) goto finish; n     if (low > r[1] || high < l[1]) goto finish; n     l[1] -= low, r[1] -= low; n     if (l[1] <= 0) { n       foo = l[2]; n       bar = l[3]; n       baz = l[4]; n       return 1; n     } n     for (int j = 2; j <= 4; j++) r[j] -= l[j]; n     if (l[1] <= r[2]) { n       foo = l[2] + l[1]; n       bar = l[3]; n       baz = l[4]; n       return 1; n     } n     l[1] -= r[2]; n     if (l[1] <= r[3]) { n       foo = l[2] + r[2]; n       bar = l[3] + l[1]; n       baz = l[4]; n       return 1; n     } n     l[1] -= r[3]; n     if (l[1] <= r[4]) { n       foo = l[2] + r[2]; n       bar = l[3] + r[3]; n       baz = l[4] + l[1]; n       return 1; n     } n   finish:; n   } n   return 0; n } n int main() { n   int t = read(); n   while (t--) { n     n = read(); n     for (int i = 1; i <= n; i++) x[i] = read(), y[i] = read(), z[i] = read(); n     long long l = 0, r = 3e18, ans = -1; n     while (l <= r) { n       long long mid = (l + r) >> 1; n       if (check(mid)) n         r = mid - 1, ans = mid; n       else n         l = mid + 1; n     } n     assert(ans != -1); n     printf( %lld %lld %lld n , (bar + baz) / 2, (foo + baz) / 2, n            (foo + bar) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf(101010101010101010LL); n int n; n struct uf { n   int par[1005]; n   uf() { n     for (int i(0); i < (n); i++) par[i] = i; n   } n   int ff(int x) { return par[x] == x ? x : par[x] = ff(par[x]); } n   void uu(int x, int y) { n     x = ff(x), y = ff(y); n     if (x - y) par[x] = y; n   } n }; n vector<pair<int, int> > edges; n vector<int> eA, eB; n char col[20]; n int main() { n   cin >> n; n   int m; n   cin >> m; n   edges.resize(m); n   uf fA, fB; n   for (int id(0); id < (m); id++) { n     int x, y; n     scanf( %d%d%s , &x, &y, col); n     x--, y--; n     edges[id] = make_pair(x, y); n     if (col[0] ==  S ) { n       if (fA.ff(x) == fA.ff(y)) continue; n       eA.push_back(id), fA.uu(x, y); n     } else { n       if (fB.ff(x) == fB.ff(y)) continue; n       eB.push_back(id), fB.uu(x, y); n     } n   } n   uf fAB_1; n   for (__typeof((eA).end()) it((eA).begin()); it != (eA).end(); ++it) { n     int id(*it), x(edges[id].first), y(edges[id].second); n     fAB_1.uu(x, y); n   } n   vector<int> eB_selected; n   for (__typeof((eB).end()) it((eB).begin()); it != (eB).end(); ++it) { n     int id(*it), x(edges[id].first), y(edges[id].second); n     if (fAB_1.ff(x) != fAB_1.ff(y)) { n       eB_selected.push_back(id); n       fAB_1.uu(x, y); n     } n   } n   uf fAB_2; n   int nA(0), nB(((int)(eB_selected).size())); n   for (__typeof((eB_selected).end()) it((eB_selected).begin()); n        it != (eB_selected).end(); ++it) { n     int id(*it), x(edges[id].first), y(edges[id].second); n     fAB_2.uu(x, y); n   } n   for (__typeof((eB).end()) it((eB).begin()); it != (eB).end(); ++it) n     if (nB < (n - 1) / 2) { n       int id(*it), x(edges[id].first), y(edges[id].second); n       if (fAB_2.ff(x) != fAB_2.ff(y)) { n         eB_selected.push_back(id); n         fAB_2.uu(x, y); n         nB++; n       } n     } n   vector<int> eA_selected; n   for (__typeof((eA).end()) it((eA).begin()); it != (eA).end(); ++it) { n     int id(*it), x(edges[id].first), y(edges[id].second); n     if (fAB_2.ff(x) != fAB_2.ff(y)) { n       eA_selected.push_back(id); n       fAB_2.uu(x, y); n       nA++; n     } n   } n   if (nA != (n - 1) / 2 || nB != (n - 1) / 2) { n     puts( -1 ); n     return 0; n   } n   cout << (n - 1) << endl; n   vector<int> selected; n   for (__typeof((eA_selected).end()) it((eA_selected).begin()); n        it != (eA_selected).end(); ++it) n     selected.push_back(*it); n   for (__typeof((eB_selected).end()) it((eB_selected).begin()); n        it != (eB_selected).end(); ++it) n     selected.push_back(*it); n   for (__typeof((selected).end()) it((selected).begin()); n        it != (selected).end(); ++it) { n     if (it != (selected).begin()) cout <<    ; n     cout << (*it) + 1; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n class SegmentTree { n  private: n   vector<long long> tree; n   vector<long long> lazy; n   vector<long long> num; n   long long build(long long ind, long long l, long long r) { n     if (l == r) { n       tree[ind] = num[l]; n       return tree[ind]; n     } n     tree[ind] = min(build(2 * ind, l, (l + r) / 2), n                     build(2 * ind + 1, (l + r) / 2 + 1, r)); n     return tree[ind]; n   } n   long long rngq(long long ind, long long l, long long r, long long ql, n                  long long qr) { n     if (lazy[ind] != 0) { n       tree[ind] += lazy[ind]; n       if (l != r) { n         lazy[2 * ind + 1] += lazy[ind]; n         lazy[2 * ind] += lazy[ind]; n       } n       lazy[ind] = 0; n     } n     if (ql > r || qr < l) return (long long)1e18; n     if (ql <= l && qr >= r) { n       return tree[ind]; n     } n     return min(rngq(2 * ind, l, (l + r) / 2, ql, qr), n                rngq(2 * ind + 1, (l + r) / 2 + 1, r, ql, qr)); n   } n   long long update(long long ind, long long l, long long r, long long ql, n                    long long qr, long long val) { n     if (lazy[ind] != 0) { n       tree[ind] += lazy[ind]; n       if (l != r) { n         lazy[2 * ind + 1] += lazy[ind]; n         lazy[2 * ind] += lazy[ind]; n       } n       lazy[ind] = 0; n     } n     if (ql > r || qr < l) return tree[ind]; n     if (ql <= l && qr >= r) { n       tree[ind] += val; n       if (l != r) { n         lazy[2 * ind + 1] += val; n         lazy[2 * ind] += val; n       } n       return tree[ind]; n     } n     return tree[ind] = n                min(update(2 * ind, l, (l + r) / 2, ql, qr, val), n                    update(2 * ind + 1, (l + r) / 2 + 1, r, ql, qr, val)); n   } n  n  public: n   void buildTree(vector<long long> arr) { n     long long n = arr.size(); n     tree.resize(4 * n); n     lazy.resize(4 * n, 0); n     num = arr; n     build(1, 0, n - 1); n   } n   long long query(long long l, long long r) { n     return rngq(1, 0, num.size() - 1, l, r); n   } n   void rngup(long long l, long long r, long long val) { n     update(1, 0, num.size() - 1, l, r, val); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   vector<long long> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   SegmentTree segtree; n   segtree.buildTree(arr); n   long long q; n   cin >> q; n   while (q--) { n     long long l, r; n     cin >> l >> r; n     if (cin.peek() ==   n ) { n       if (r < l) { n         cout << min(segtree.query(l, n - 1), segtree.query(0, r)) << endl; n       } else n         cout << segtree.query(l, r) << endl; n     } else { n       long long val; n       cin >> val; n       if (r < l) { n         segtree.rngup(l, n - 1, val); n         segtree.rngup(0, r, val); n       } else n         segtree.rngup(l, r, val); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n pair<int, int> t[500050]; n bitset<2020> P[2020], B[2020]; n void solve() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     t[i] = pair<int, int>(x, y); n     P[x][y] = 1; n   } n   for (int i = 1; i <= n; i++) B[i][i] = 1; n   for (int i = 1; i <= n; i++) { n     int f = -1; n     for (int j = i; j <= n; j++) n       if (P[j].test(i)) { n         f = j; n         break; n       } n     swap(P[f], P[i]); n     swap(B[f], B[i]); n     for (int j = 1; j <= n; j++) n       if (i != j && P[j][i]) { n         P[j] ^= P[i]; n         B[j] ^= B[i]; n       } n   } n   for (int i = 0; i < m; i++) { n     if (B[t[i].second][t[i].first]) n       puts( NO ); n     else n       puts( YES ); n   } n } n int main() { n   int Tc = 1; n   for (int tc = 1; tc <= Tc; tc++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n const int N = 100003; n int ld[N]; n int a[N]; n void sieve() { n   for (int i = 2; i * i < N; ++i) n     if (ld[i] == 0) n       for (int j = i * i; j < N; j += i) n         if (ld[j] == 0) ld[j] = i; n   for (int i = 2; i < N; ++i) n     if (ld[i] == 0) ld[i] = i; n } n vector<int> p; n vector<int> k; n vector<int> d; n void genDivisors(int i, int x) { n   if (i == (int)p.size()) { n     d.push_back(x); n     return; n   } n   for (int c = 0; c <= k[i]; ++c) { n     genDivisors(i + 1, x); n     x *= p[i]; n   } n } n int expMod(int a, int n) { n   int res = 1; n   while (n > 0) { n     if (n & 1) res = (long long)res * a % M; n     n >>= 1; n     a = (long long)a * a % M; n   } n   return res; n } n int main() { n   sieve(); n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   sort(a, a + n); n   int res = 0; n   for (int x = 1; x <= a[n - 1]; ++x) { n     p.clear(); n     k.clear(); n     d.clear(); n     int y = x; n     while (y > 1) { n       int z = ld[y]; n       p.push_back(ld[y]); n       k.push_back(1); n       y /= z; n       while (y % z == 0) { n         y /= z; n         ++k.back(); n       } n     } n     genDivisors(0, 1); n     sort(d.begin(), d.end()); n     k.clear(); n     k.resize(d.size()); n     for (int i = 0; i < (int)d.size(); ++i) n       k[i] = n - (lower_bound(a, a + n, d[i]) - a); n     y = 1; n     for (int i = 0; i < (int)d.size() - 1; ++i) { n       k[i] -= k[i + 1]; n       y = (long long)y * expMod(i + 1, k[i]) % M; n     } n     int z = n         (expMod(d.size(), k.back()) - expMod((int)d.size() - 1, k.back()) + M) % n         M; n     y = (long long)y * z % M; n     res = (res + y) % M; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n struct str { n   int len; n   bool vis; n   string pre, suf; n   void push_front(char ch) { n     if (!len) n       vis = 0; n     else if (ch ^ pre[0]) n       vis = ch < pre[0]; n     pre = ch + pre; n     if (len < 2) n       suf = ch + suf; n     else if (pre.length() > 10) n       pre.pop_back(); n     len++; n   } n } ans[100005]; n int main() { n   int i; n   cin >> s; n   n = s.length(); n   ans[n].len = ans[n].vis = 0; n   ans[n].pre = ans[n].suf =   ; n   s +=  # ; n   for (i = n - 1; i >= 0; i--) n     if (s[i] ^ s[i + 1]) n       ans[i] = ans[i + 1], ans[i].push_front(s[i]); n     else { n       ans[i] = ans[i + 2]; n       if (s[i] < ans[i].pre[0] || s[i] == ans[i].pre[0] && ans[i].vis) { n         ans[i].push_front(s[i]); n         ans[i].push_front(s[i]); n       } n     } n   for (i = 0; i < n; i++) { n     cout << ans[i].len <<    ; n     if (ans[i].len <= 10) n       cout << ans[i].pre << endl; n     else n       cout << ans[i].pre.substr(0, 5) <<  ...  << ans[i].suf << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n long long int n, k; n string s, s1; n void hi() { n   for (int i = k; i < s.length(); i++) { n     s[i] = s[i - k]; n   } n } n int main() { n   cout.precision(10); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k >> s; n   s1 = s; n   hi(); n   if (s >= s1) { n     cout << n << endl; n     cout << s; n   } else { n     bool ok = true; n     int i = k - 1; n     if (s[i] !=  9 ) { n       s[i] = s[i] + 1; n       hi(); n       cout << n << endl; n       cout << s; n     } else { n       s[i] =  0 ; n       for (int j = i - 1; j >= 0; j--) { n         if (s[j] !=  9 ) { n           s[j]++; n           ok = false; n           hi(); n           cout << n << endl; n           cout << s; n           break; n         } else { n           s[j] =  0 ; n         } n       } n       if (ok) { n         cout << n + 1 << endl; n         cout << s1[0] + 1; n         for (int i = 0; i < n; i++) { n           cout <<  0 ; n         } n       } n     } n   } n   return 0; n } n long long int nChooseR(long long int n, long long int k) { n   if (k > n) return 0; n   if (k * 2 > n) k = n - k; n   if (k == 0) return 1; n   long long int result = n; n   for (int i = 2; i <= k; ++i) { n     result *= (n - i + 1); n     result /= i; n   } n   return result; n } n long long int power(long long int a, long long int b) { n   if (b == 1) { n     return a % M; n   } else { n     if (b % 2 == 0) n       return (power(a, b / 2) % M * power(a, b / 2) % M) % M; n     else n       return (((power(a, b / 2) % M * power(a, b / 2) % M) % M) * a % M) % M; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E5 + 10; n const int inf = 1 << 30; n long long read() { n   long long a = 0; n   char b = 1, c; n   do n     if ((c = getchar()) == 45) b = -1; n   while (c < 48 || c > 57); n   do a = (a << 3) + (a << 1) + (c & 15); n   while ((c = getchar()) > 47 && c < 58); n   return a * b; n } n void write(long long x, char c) { n   if (x < 0) putchar(45), x = -x; n   char a[20], s = 0; n   do a[++s] = x % 10 | 48; n   while (x /= 10); n   do putchar(a[s]); n   while (--s); n   putchar(c); n } n int fa[N], _fa[N]; n int find(int x) { return fa[x] ^ x ? fa[x] = find(fa[x]) : x; } n int _find(int x) { return _fa[x] ^ x ? _fa[x] = _find(_fa[x]) : x; } n vector<int> q, _q; n int main() { n   int n = read(), y = read(), z = read(); n   write(n - 1 - max(y, z),   n ); n   for (int i = 1; i <= n; ++i) fa[i] = _fa[i] = i; n   while (y--) { n     int u = find(read()), v = find(read()); n     if (u > v) swap(u, v); n     fa[v] = u; n   } n   while (z--) { n     int u = _find(read()), v = _find(read()); n     if (u > v) swap(u, v); n     _fa[v] = u; n   } n   for (int i = 2; i <= n; ++i) { n     int u = find(i), v = _find(i); n     if (u ^ 1 && v ^ 1) { n       fa[u] = _fa[v] = 1; n       printf( %d 1 n , i); n     } n   } n   for (int i = 2; i <= n; ++i) { n     if (find(i) == i) q.push_back(i); n     if (_find(i) == i) _q.push_back(i); n   } n   for (int i = min(q.size(), _q.size()); i--;) printf( %d %d n , q[i], _q[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t = 0, k; n   cin >> n >> k; n   if (n > k) n = k; n   long long unsigned int sum = 1; n   while (n != 1) { n     sum *= n; n     n--; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   while (~scanf( %d %d %d , &a, &b, &c)) { n     int ans = a + b + c; n     ans = max(ans, a * b * c); n     ans = max((a + b) * c, ans); n     ans = max(a * (b + c), ans); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int res = 0, w = 1; n   char ch = getchar(); n   while (ch !=  -  && !isdigit(ch)) ch = getchar(); n   if (ch ==  - ) w = -1, ch = getchar(); n   while (isdigit(ch)) res = res * 10 + ch -  0 , ch = getchar(); n   return res * w; n } n const int MAX_N = 1e6 + 5; n const int V = 1e6 + 1; n int mn[MAX_N << 2], tag[MAX_N << 2], cnt[MAX_N << 2]; n void pushup(int o) { n   mn[o] = min(mn[(o << 1)], mn[(o << 1 | 1)]), cnt[o] = 0; n   if (mn[(o << 1)] == mn[o]) cnt[o] += cnt[(o << 1)]; n   if (mn[(o << 1 | 1)] == mn[o]) cnt[o] += cnt[(o << 1 | 1)]; n } n void puttag(int o, int v) { tag[o] += v, mn[o] += v; } n void pushdown(int o) { n   if (!tag[o]) return; n   puttag((o << 1), tag[o]), puttag((o << 1 | 1), tag[o]); n   tag[o] = 0; n } n void modify(int o, int l, int r, int ql, int qr, int v) { n   if (ql <= l && r <= qr) return puttag(o, v); n   pushdown(o); n   int mid = (l + r) >> 1; n   if (ql <= mid) modify((o << 1), l, mid, ql, qr, v); n   if (qr > mid) modify((o << 1 | 1), mid + 1, r, ql, qr, v); n   pushup(o); n } n void mdy(int o, int l, int r, int pos, int v) { n   if (l == r) return (void)(cnt[o] += v); n   pushdown(o); n   int mid = (l + r) >> 1; n   if (pos <= mid) n     mdy((o << 1), l, mid, pos, v); n   else n     mdy((o << 1 | 1), mid + 1, r, pos, v); n   pushup(o); n } n int query(int o, int l, int r, int ql, int qr) { n   if (ql <= l && r <= qr) return mn[o] == 1 ? cnt[o] : 0; n   pushdown(o); n   int mid = (l + r) >> 1, res = 0; n   if (ql <= mid) res = query((o << 1), l, mid, ql, qr); n   if (qr > mid) res += query((o << 1 | 1), mid + 1, r, ql, qr); n   return res; n } n void upd(int l, int r, int v) { n   if (l == r) return (void)puts( xgzc ak ioi ); n   if (l > r) swap(l, r); n   modify(1, 0, V, l, r - 1, v); n } n int N, Q, a[MAX_N]; n int main() { n   N = gi(), Q = gi(); n   for (int i = 1; i <= N; i++) a[i] = gi(); n   a[0] = V, a[N + 1] = 0; n   for (int i = 0; i <= N; i++) upd(a[i], a[i + 1], 1), mdy(1, 0, V, a[i], 1); n   while (Q--) { n     int p = gi(), v = gi(); n     upd(a[p - 1], a[p], -1), upd(a[p], a[p + 1], -1), mdy(1, 0, V, a[p], -1); n     a[p] = v; n     upd(a[p - 1], a[p], 1), upd(a[p], a[p + 1], 1), mdy(1, 0, V, a[p], 1); n     printf( %d n , query(1, 0, V, 1, V - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bef[1000100], af[1000100]; n long long calc(long long n) { return (n * (n + 1)) / 2; } n int main() { n   int k; n   string s; n   cin >> k >> s; n   memset(bef, 0, sizeof bef); n   memset(af, 0, sizeof af); n   long long last(1000010), cur(1), i(0); n   vector<int> ones; n   long long incase(0); n   while (i < s.size()) { n     while (s[i] ==  0 ) { n       ++i; n       ++cur; n     } n     incase += calc(cur - 1); n     if (i < s.size()) { n       bef[i] = cur; n       cur = 1; n       ones.push_back(i); n     } n     ++i; n   } n   if (!k) { n     cout << incase; n     return 0; n   } n   i = s.size() - 1; n   cur = 1; n   while (i >= 0) { n     while (s[i] ==  0 ) { n       --i; n       ++cur; n     } n     if (i >= 0) { n       af[i] = cur; n       cur = 1; n     } n     --i; n   } n   long long ans(0); n   for (int j = 0; j + k - 1 < ones.size(); ++j) { n     ans += bef[ones[j]] * af[ones[j + k - 1]]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b = 0, c = 0, n, t; n   cin >> t; n   for (int o = 1; o <= t; o++) { n     b = 0; n     c = 0; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a; n       if (a != 1) { n         c = 1; n       } else { n         if (c == 0) { n           b = b + 1; n         } n       } n     } n     if (n == 1) { n       cout <<  First ; n     } else { n       if (c == 0) { n         if (n % 2 == 0) { n           cout <<  Second ; n         } else { n           cout <<  First ; n         } n       } else { n         if (b % 2 == 0) { n           cout <<  First ; n         } else { n           cout <<  Second ; n         } n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > G[500005]; n int sz[500005]; n vector<int> *vec[500005]; n int cnt[(1 << 23) + 5]; n int lazy[500005]; n int len[500005]; n int res[500005]; n void get_ready(int u, int par, int mask, int hh) { n   sz[u] = 1; n   len[u] = hh; n   lazy[u] = mask; n   for (auto v : G[u]) n     if (v.first != par) n       get_ready(v.first, u, mask ^ v.second, hh + 1), sz[u] += sz[v.first]; n } n void DFS(int u, int par, int keep, int mask) { n   int node = -1, mx = 0, xr = 0; n   for (auto x : G[u]) { n     int v = x.first; n     if (par != v and sz[v] > mx) { n       mx = sz[v], node = v, xr = x.second; n     } n   } n   for (auto x : G[u]) { n     int v = x.first; n     if (v != par and v != node) DFS(v, u, 0, x.second); n   } n   if (node != -1) { n     DFS(node, u, 1, xr); n     vec[u] = vec[node]; n     res[u] = max(res[u], res[node]); n   } else n     vec[u] = new vector<int>; n   for (int i = 1; i <= (1 << 22); i <<= 1) { n     res[u] = max(res[u], cnt[lazy[u] ^ i] - len[u]); n   } n   res[u] = max(res[u], cnt[lazy[u]] - len[u]); n   cnt[lazy[u]] = max(cnt[lazy[u]], len[u]); n   vec[u]->push_back(u); n   for (auto x : G[u]) { n     int v = x.first; n     if (v != par and v != node) { n       res[u] = max(res[u], res[v]); n       for (auto vv : *vec[v]) { n         for (int i = 1; i <= (1 << 22); i <<= 1) { n           res[u] = max(res[u], len[vv] + cnt[lazy[vv] ^ i] - 2 * len[u]); n         } n         res[u] = max(res[u], len[vv] + cnt[lazy[vv]] - 2 * len[u]); n       } n       for (auto vv : *vec[v]) { n         int temp = lazy[vv]; n         cnt[temp] = max(cnt[temp], len[vv]); n         vec[u]->push_back(vv); n       } n       vec[v]->clear(); n     } n   } n   if (!keep) { n     for (auto v : *vec[u]) cnt[lazy[v]] = -1000000000; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   for (int i = 0; i < (1 << 23) + 5; i++) cnt[i] = -1000000000; n   int N; n   cin >> N; n   for (int i = 2; i <= N; i++) { n     string ch; n     int p; n     cin >> p >> ch; n     G[p].push_back({i, 1 << (ch[0] -  a )}); n     G[i].push_back({p, 1 << (ch[0] -  a )}); n   } n   get_ready(1, -1, 0, 0); n   DFS(1, -1, 1, 0); n   for (int i = 1; i <= N; i++) cout << res[i] <<    ; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long sum[500000 + 10]; n int data[500000 + 10]; n int main() { n   int n, l, r, ql, qr; n   scanf( %d%d%d%d%d , &n, &l, &r, &ql, &qr); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &data[i]); n     sum[i] += (sum[i - 1] + data[i] * r); n   } n   long long ans = 100000000000000; n   long long tem = 0; n   for (int i = 0; i <= n; ++i) { n     tem += data[i] * l; n     if (0 == i) n       ans = min(ans, sum[n] + (n - 1) * qr); n     else if (n == i) n       ans = min(ans, tem + (n - 1) * ql); n     else if (i == (n - i)) { n       ans = min(ans, tem + sum[n] - sum[i]); n     } else if (i < (n - i)) n       ans = min(ans, tem + sum[n] - sum[i] + (n - i - i - 1) * qr); n     else n       ans = min(ans, tem + sum[n] - sum[i] + (2 * i - n - 1) * ql); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n int n, x; n char s[3]; n int main() { n   cin >> n; n   while (n--) { n     cin >> s; n     if (s[0] ==  a ) { n       cin >> x; n       v.insert(lower_bound(v.begin(), v.end(), x), x); n     } n     if (s[0] ==  d ) { n       cin >> x; n       v.erase(lower_bound(v.begin(), v.end(), x)); n     } n     if (s[0] ==  s ) { n       long long ans = 0; n       for (int i = 2; i < v.size(); i += 5) { n         ans += v[i]; n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 1; n const long long M = 1e5 + 1; n vector<int> len; n vector<int> dist[N]; n vector<long long> pre[N]; n long long n, m; n void mer(long long v, long long l, long long addl, long long r, n          long long addr) { n   dist[v].push_back(0); n   long long sl = dist[l].size(); n   long long sr = dist[r].size(); n   long long pl = 0; n   long long pr = 0; n   while (pl < sl && pr < sr) { n     if (dist[l][pl] + addl <= dist[r][pr] + addr) { n       dist[v].push_back(dist[l][pl] + addl); n       pl++; n     } else { n       dist[v].push_back(dist[r][pr] + addr); n       pr++; n     } n   } n   while (pl < sl) { n     dist[v].push_back(dist[l][pl] + addl); n     pl++; n   } n   while (pr < sr) { n     dist[v].push_back(dist[r][pr] + addr); n     pr++; n   } n   return; n } n void bfs(long long v) { n   if (v > n) return; n   long long l = 2 * v; n   long long r = 2 * v + 1; n   bool cl = false, cr = false; n   if (l <= n) { n     bfs(l); n     cl = true; n   } n   if (r <= n) { n     bfs(r); n     cr = true; n   } n   if (cl && cr) { n     long long addl = len[2 * v - 1]; n     long long addr = len[2 * v]; n     mer(v, l, addl, r, addr); n   } else if (cl && !cr) { n     long long addl = len[2 * v - 1]; n     dist[v].push_back(0); n     for (long long x : dist[l]) dist[v].push_back(addl + x); n   } else if (cr && !cl) { n     long long addr = len[2 * v]; n     dist[v].push_back(0); n     for (long long x : dist[r]) dist[v].push_back(addr + x); n   } else { n     dist[v].push_back(0); n   } n   long long sze = dist[v].size(); n   pre[v].resize(sze); n   long long prev = 0ll; n   for (long long i = 0; i < sze; i++) { n     pre[v][i] = prev + dist[v][i]; n     prev = pre[v][i]; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   len.push_back(-1); n   cin >> n >> m; n   for (long long i = 0; i < n - 1; i++) { n     long long x; n     cin >> x; n     len.push_back(x); n   } n   bfs(1); n   for (long long i = 0; i < m; i++) { n     long long ans = 0ll; n     long long a, h; n     cin >> a >> h; n     long long opt = 3; n     while (a >= 1) { n       if (h < 0) break; n       if (opt == 3) { n         long long cnt = n             (upper_bound(dist[a].begin(), dist[a].end(), h) - dist[a].begin()); n         if (cnt > 0) ans += (cnt)*h - pre[a][cnt - 1]; n       } else if (opt == 2) { n         ans += h; n         if (2 * a <= n) { n           long long cnt = (upper_bound(dist[2 * a].begin(), dist[2 * a].end(), n                                        h - len[2 * a - 1]) - n                            dist[2 * a].begin()); n           if (cnt > 0) n             ans += (cnt) * (h - len[2 * a - 1]) - pre[2 * a][cnt - 1]; n         } n       } else { n         if (h > 0) ans += h; n         if (2 * a + 1 <= n) { n           long long cnt = (upper_bound(dist[2 * a + 1].begin(), n                                        dist[2 * a + 1].end(), h - len[2 * a]) - n                            dist[2 * a + 1].begin()); n           if (cnt > 0) n             ans += (cnt) * (h - len[2 * a]) - pre[2 * a + 1][cnt - 1]; n         } n       } n       if (a % 2) { n         h -= len[2 * (a / 2)]; n         opt = 2; n       } else { n         h -= len[2 * (a / 2) - 1]; n         opt = 1; n       } n       a /= 2; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int sum = 0; n   int d; n   for (int i = 0; i < n; i++) { n     cin >> d; n     sum += d; n   } n   int count = 5; n   for (int i = 1; i <= 5; i++) { n     if ((sum + i) % (n + 1) == 1) { n       count--; n     } n   } n   cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr1[1000001]; n template <typename T> n T myMax(T x, T y) { n   return (x > y) ? x : y; n } n template <typename T> n T myMin(T x, T y) { n   return (x < y) ? x : y; n } n template <typename T> n T sum(T x, T y) { n   return (x + y); n } n template <typename T> n T Mean(T x, T y) { n   return (x + ((y - x) / 2)); n } n void seive() { n   for (long long i = 0; i <= 1000000; i++) { n     arr1[i] = 1; n   } n   for (long long i = 2; i * i <= 1000000; i++) { n     if (arr1[i] == 1) { n       arr1[i] = 1; n       for (long long j = i * i; j <= 1000000; j = j + i) { n         arr1[j] = 0; n       } n     } n   } n } n long long find_occurances(string s) { n   string s1 =   ; n   long long c = 0; n   for (long long i = 0; i < 3; i++) { n     s1 = s1 + s[i]; n   } n   if (s1 ==  abc ) { n     c++; n   } n   long long n = s.size(); n   long long l = 0; n   long long r = 2; n   while (r < n - 1) { n     s1.erase(s1.begin()); n     l++; n     r++; n     s1 += s[r]; n     if (s1 ==  abc ) { n       c++; n     } n   } n   return c; n } n bool check_power_of_2(long long x) { n   if ((x & (x - 1)) == 0) { n     return true; n   } n   return false; n } n bool cmp(pair<long long, pair<long long, long long>> x, n          pair<long long, pair<long long, long long>> y) { n   if (x.first == y.first) { n     return x.second.second < y.second.second; n   } n   return x.first < y.first; n } n bool cmp1(pair<long long, long long> p, pair<long long, long long> q) { n   return p.first < q.second; n } n bool check(vector<long long>& v) { n   if (v[0] == 1) { n     bool flag1 = true; n     for (long long i = 1; i < v.size(); i++) { n       if (v[i] != v[i - 1]) { n         flag1 = false; n         break; n       } n     } n     return flag1; n   } else { n     return false; n   } n } n bool check_prime(long long x) { n   if (x == 2) { n     return true; n   } n   if (x == 3) { n     return true; n   } n   if ((x % 2) == 0) { n     return false; n   } n   if ((x % 3) == 0) { n     return false; n   } n   for (long long i = 5; i * i <= x; i = i + 6) { n     if ((x % i) == 0) { n       return false; n     } n     if ((x % (i + 2)) == 0) { n       return false; n     } n   } n   return true; n } n bool check_array(vector<long long>& v) { n   for (long long i = 1; i < v.size(); i++) { n     if (v[i] == v[i - 1]) return false; n   } n   return true; n } n bool is_vowel(char x) { n   if ((x ==  a ) || (x ==  e ) || (x ==  o ) || (x ==  i ) || (x ==  u )) { n     return true; n   } n   return false; n } n signed main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long arr[n]; n     long long sum = 0; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n       sum += arr[i]; n     } n     if ((sum % n) == 0) { n       cout << 0 <<   n ; n     } else { n       cout << 1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ini; n struct TrieNode { n   long long value; n   TrieNode *arr[2]; n }; n TrieNode *newNode() { n   TrieNode *temp = new TrieNode; n   temp->value = 0; n   temp->arr[0] = temp->arr[1] = NULL; n   return temp; n } n void insert(TrieNode *root, long long pre_xor) { n   TrieNode *temp = root; n   for (int i = 41 - 1; i >= 0; i--) { n     bool val = pre_xor & (1LL << i); n     if (temp->arr[val] == NULL) temp->arr[val] = newNode(); n     temp = temp->arr[val]; n   } n   temp->value = pre_xor; n } n long long query(TrieNode *root, long long pre_xor) { n   TrieNode *temp = root; n   for (int i = 41 - 1; i >= 0; i--) { n     bool val = pre_xor & (1LL << i); n     if (temp->arr[1 - val] != NULL) n       temp = temp->arr[1 - val]; n     else if (temp->arr[val] != NULL) n       temp = temp->arr[val]; n   } n   return pre_xor ^ (temp->value); n } n long long maxSubarrayXOR(long long arr[], long long n) { n   TrieNode *root = newNode(); n   insert(root, 0); n   long long result = ini, pre_xor = 0; n   for (int i = 0; i < n; i++) { n     pre_xor = pre_xor ^ arr[i]; n     insert(root, pre_xor); n     result = max(result, query(root, pre_xor ^ ini)); n   } n   return result; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long arr[100000], n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i], ini ^= arr[i]; n   cout << maxSubarrayXOR(arr, 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     long long number; n     cin >> number; n     if (number == 2) { n       cout << 0 << endl; n     } else if (number == 1) { n       cout << 0 << endl; n     } else { n       if (number % 2 == 0) { n         cout << number / 2 - 1 << endl; n       } else { n         cout << number / 2 << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, u[6], v[6], x; n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); i++) { n     scanf( %d , &x); n     u[x]++; n   } n   for (int i = (1); i <= (n); i++) { n     scanf( %d , &x); n     v[x]++; n   } n   int dif = 0; n   for (int i = (1); i <= (5); i++) n     if ((u[i] + v[i]) & 1) { n       printf( -1 n ); n       return 0; n     } else n       dif += max(0, (u[i] - v[i]) / 2); n   printf( %d n , dif); n   return 0; n } n


#include <bits/stdc++.h> n long long int min(long long int x, long long int y) { return (x < y) ? x : y; } n long long int max(long long int x, long long int y) { return (x > y) ? x : y; } n long long int fastexpo(long long int base, long long int power) { n   long long int res = 1; n   while (power) { n     if (power % 2 == 0) { n       base *= base; n       power /= 2; n     } else { n       res *= base; n       power--; n     } n   } n   return res; n } n long long int modexpo(long long int base, long long int power, n                       long long int mod) { n   long long int res = 1; n   while (power) { n     if (power % 2 == 0) { n       base = (base * base) % mod; n       power /= 2; n     } else { n       res = (base * res) % mod; n       power--; n     } n   } n   return res; n } n const long long int SN = 1e7; n bool prime[SN + 1]; n void SEIVE(int n) { 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   prime[0] = false; n   prime[1] = false; n } n using namespace std; n int x[26], par[200010]; n int find(int n) { n   if (par[n] == n) n     return n; n   else n     return par[n] = find(par[n]); n } n void solve() { n   int n; n   cin >> n; n   for (long long int i = 0; i < n + 1; i++) par[i] = i; n   for (long long int i = 1; i < n + 1; i++) { n     string s; n     cin >> s; n     for (long long int j = 0; j < s.size(); j++) { n       if (!x[s[j] -  a ]) n         x[s[j] -  a ] = i; n       else n         par[find(i)] = par[x[s[j] -  a ]]; n     } n   } n   int ans = 0; n   for (long long int i = 1; i < n + 1; i++) { n     if (par[i] == i) ans++; n   } n   cout << ans; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, x, y, c1 = 0, c2 = 0, m; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     if (x > 0) n       c1++; n     else n       c2++; n   } n   m = min(c1, c2); n   if (m <= 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   { solve(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const long long UNDEF = -1; n const long long INF = 1e18; n template <typename T> n inline bool chkmax(T &aa, T bb) { n   return aa < bb ? aa = bb, true : false; n } n template <typename T> n inline bool chkmin(T &aa, T bb) { n   return aa > bb ? aa = bb, true : false; n } n static char stdinBuffer[1024]; n static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); n static const char *stdinPos = stdinDataEnd; n void readAhead(size_t amount) { n   size_t remaining = stdinDataEnd - stdinPos; n   if (remaining < amount) { n     memmove(stdinBuffer, stdinPos, remaining); n     size_t sz = fread(stdinBuffer + remaining, 1, n                       sizeof(stdinBuffer) - remaining, stdin); n     stdinPos = stdinBuffer; n     stdinDataEnd = stdinBuffer + remaining + sz; n     if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; n   } n } n int rint() { n   readAhead(16); n   int x = 0; n   bool neg = false; n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   if (*stdinPos ==  - ) { n     ++stdinPos; n     neg = true; n   } n   while (*stdinPos >=  0  && *stdinPos <=  9 ) { n     x *= 10; n     x += *stdinPos -  0 ; n     ++stdinPos; n   } n   return neg ? -x : x; n } n char rch() { n   readAhead(16); n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   char ans = *stdinPos; n   ++stdinPos; n   return ans; n } n long long rlong() { n   readAhead(32); n   long long x = 0; n   bool neg = false; n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   if (*stdinPos ==  - ) { n     ++stdinPos; n     neg = true; n   } n   while (*stdinPos >=  0  && *stdinPos <=  9 ) { n     x *= 10; n     x += *stdinPos -  0 ; n     ++stdinPos; n   } n   return neg ? -x : x; n } n int nim(int x) { return x; } n map<int, map<int, int> > g[2]; n map<int, vector<pair<int, int> > > z[2]; n int lim[2]; n map<int, pair<int, int> > h; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   lim[0] = rint(), lim[1] = rint(); n   int k = rint(); n   for (int i = 0; i < k; i++) { n     int xb = rint(), yb = rint(), xe = rint(), ye = rint(); n     if (xb == xe) { n       if (yb > ye) swap(yb, ye); n       g[0][xb][yb]++; n       g[0][xb][ye]--; n     } else { n       if (xb > xe) swap(xb, xe); n       g[1][yb][xb]++; n       g[1][yb][xe]--; n     } n   } n   int nimber = 0; n   for (int t = 0; t < 2; t++) n     for (auto &o : g[t]) { n       int cnt = 0; n       int last = 0; n       int sumlen = 0; n       for (auto &w : o.second) { n         int oldcnt = cnt; n         int pos = w.first; n         cnt += w.second; n         if (oldcnt == 0 && cnt > 0) { n           int len = pos - last; n           if (len) { n             z[t][o.first].push_back(make_pair(last, pos)); n             sumlen += len; n           } n         } n         if (oldcnt > 0 && cnt == 0) { n           last = pos; n         } n       } n       { n         int pos = lim[t ^ 1]; n         int len = pos - last; n         if (len) { n           z[t][o.first].push_back(make_pair(last, pos)); n           sumlen += len; n         } n       } n       h[sumlen] = make_pair(t, o.first); n       nimber ^= sumlen; n     } n   for (int t = 0; t < 2; t++) { n     int unseen = lim[t] - 1 - g[t].size(); n     int num = lim[t ^ 1]; n     if (unseen & 1) nimber ^= num; n     if (unseen > 0) { n       int have = -1; n       int want = 1; n       for (auto &w : g[t]) { n         if (want != w.first) { n           have = want; n           break; n         } n         want++; n       } n       if (have == -1) have = lim[t] - 1; n       assert(g[t].count(have) == 0); n       h[num] = make_pair(t, have); n       z[t][have].push_back(make_pair(0, lim[t ^ 1])); n     } n   } n   if (nimber == 0) { n     printf( SECOND n ); n   } else { n     printf( FIRST n ); n     for (auto w : h) { n       int len = w.first; n       int go = len ^ nimber; n       if (go > len) continue; n       int rm = len - go; n       int t = w.second.first; n       int o = w.second.second; n       int solution = -1; n       for (auto &q : z[t][o]) { n         int start = q.first, end = q.second; n         if (rm <= end - start) { n           solution = start + rm; n           break; n         } else n           rm -= end - start; n       } n       assert(solution != -1); n       if (t == 0) { n         printf( %d %d %d %d n , o, 0, o, solution); n       } else { n         printf( %d %d %d %d n , 0, o, solution, o); n       } n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n static const int MOD = 1000000007; n using ll = long long; n using u32 = unsigned; n using u64 = unsigned long long; n using namespace std; n template <class T> n constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; n char buf[200001]; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     string s, t; n     cin >> s >> t; n     int a = 1, b = 0; n     for (int i = 0; i < n; ++i) { n       if (s[i] <=  2  && t[i] <=  2 ) { n       } else if (s[i] <=  2 ) n         b = 0; n       else if (t[i] <=  2 ) n         a = 0; n       else { n         swap(a, b); n       } n     } n     puts(b ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, r; n   cin >> k >> r; n   int count = 1; n   int aux = k; n   while (true) { n     if ((k % 10 == 0) || (abs(k - r) % 10 == 0)) { n       break; n     } else { n       k += aux; n       count++; n     } n   } n   cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, d; n   cin >> n >> d; n   vector<long long> delta(n); n   vector<long long> rs(n); n   for (int i = 0; i < n; ++i) cin >> delta[i]; n   rs[0] = delta[0]; n   for (int i = 1; i < n; ++i) rs[i] = rs[i - 1] + delta[i]; n   vector<long long> maxTotDeposit(n); n   long long mxSoFar = LLONG_MIN; n   for (int i = n - 1; i >= 0; --i) { n     mxSoFar = max(mxSoFar, rs[i]); n     maxTotDeposit[i] = d - mxSoFar; n     if (mxSoFar > d) { n       cout << -1; n       return 0; n     } n   } n   long long dep = 0; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     if (!delta[i] && rs[i] + maxTotDeposit[i] < 0) { n       cout << -1; n       return 0; n     } n     if (!delta[i] && rs[i] + dep < 0) { n       ++ans; n       dep = maxTotDeposit[i]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(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 const int N = 1e5 + 10; n int buc[N], L, R, a[N]; n long long ans; n void update(int c, int d) { ans += d * buc[c] * (long long)(buc[c] - 1) / 2; } n long long cal(int l, int r) { n   while (L < l) update(a[L], -1), buc[a[L]]--, update(a[L], 1), L++; n   while (L > l) L--, update(a[L], -1), buc[a[L]]++, update(a[L], 1); n   while (R < r) R++, update(a[R], -1), buc[a[R]]++, update(a[R], 1); n   while (R > r) update(a[R], -1), buc[a[R]]--, update(a[R], 1), R--; n   return ans; n } n long long dp[22][N]; n int cur; n void solve(int lb, int rb, int l, int r) { n   if (lb > rb || l > r) return; n   int mid = l + r >> 1; n   int d = 0; n   long long res = 1e18; n   for (int i = lb; i <= min(rb, mid - 1); ++i) { n     long long tmp = cal(i + 1, mid); n     if (res > dp[cur - 1][i] + tmp) res = dp[cur - 1][i] + tmp, d = i; n   } n   dp[cur][mid] = res; n   solve(lb, d, l, mid - 1), solve(d, rb, mid + 1, r); n } n int main() { n   memset(dp, 0x3f, sizeof(dp)); n   dp[0][0] = 0; n   int n, m; n   read(n), read(m); n   for (int i = 1; i <= n; ++i) read(a[i]); n   buc[a[1]]++, L = R = 1; n   for (cur = 1; cur <= m; ++cur) solve(0, n - 1, 1, n); n   printf( %lld , dp[m][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int arr[t]; n   for (int i = 0; i < t; i++) { n     int y; n     cin >> y; n     y = y + i; n     arr[i] = y; n   } n   sort(arr, arr + t); n   for (int i = 0; i < t; i++) { n     arr[i] -= i; n   } n   int check = 1; n   for (int i = 0; i < t - 1; i++) { n     if (arr[i] > arr[i + 1]) { n       check = 0; n       break; n     } n   } n   if (check == 0) n     cout <<  :(  n ; n   else { n     for (int i = 0; i < t; i++) { n       cout << arr[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int N = 1e5 + 15, M = 105, T = 1e7 + 5; n long long maxi[N], l[N], r[N]; n pair<long long, long long> f[N]; n pair<long long, int> bsd(pair<long long, int> a, pair<long long, int> b) { n   if (a.first > b.first || a.first == b.first && a.second < b.second) n     return a; n   else n     return b; n } n int get(long long n, long long target, long long j) { n   while (j <= n && target > r[j]) ++j; n   return j; n } n int main() { n   long long j = 1, g = 1, n, i, p, t, L, t0, x; n   cin >> L >> n >> p >> t; n   for (i = 1; i <= n; i++) cin >> l[i] >> r[i]; n   for (i = 1; i <= n; i++) { n     maxi[i] = max(maxi[i], maxi[i - 1]); n     f[i] = bsd(f[i], make_pair(maxi[i] + (r[i] - l[i]) / p, n                                l[i] + ((r[i] - l[i]) / p) * p)); n     t0 = f[i].second + t; n     j = get(n, t0, j); n     if (j > n) continue; n     if (l[j] >= t0) { n       maxi[j] = max(maxi[j], f[i].first); n       continue; n     } n     f[j] = bsd(f[j], make_pair(f[i].first + (r[j] - t0) / p, n                                t0 + ((r[j] - t0) / p) * p)); n     maxi[j + 1] = max(maxi[j + 1], f[i].first); n   } n   long long ret = 0; n   for (i = 1; i <= n; ++i) ret = max(ret, f[i].first); n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005, T = 100000; n int n, m, a[N], l[N], r[N]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) cin >> a[i]; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = m; i > 0; --i) { n     l[a[i] + T + i] = l[a[i] + T + i + 1] + 1; n     r[a[i] + T - i] = r[a[i] + T - i - 1] + 1; n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) n     ans += min(n, i + (m + 1) - r[i + T]) - max(1, i - (m + 1) + l[i + T]) + 1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int gcd(int x, int y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n int main() { n   int n, a; n   scanf( %d , &n); n   for (int i = 1; i <= n / 2; i++) { n     if (gcd(i, n - i) == 1) a = i; n   } n   printf( %d %d , a, n - a); n } n


#include <bits/stdc++.h> n int main() { n   char str[5010]; n   memset(str, 0, sizeof(str)); n   scanf( %s , str); n   int n; n   n = strlen(str); n   int res = 0; n   for (int i = 0; i < n; i++) { n     int l = 0, sum = 0; n     for (int j = i; j < n; j++) { n       if (str[j] ==  ( ) n         l++; n       else if (str[j] ==  ) ) n         l--; n       else n         l--, sum++; n       if (!l) res++; n       if (l < 0 && sum > 0) { n         l += 2; n         sum--; n       } else if (l < 0 && sum <= 0) { n         break; n       } n     } n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int r1, r2, c1, c2; n   cin >> r1 >> c1 >> r2 >> c2; n   if (r1 == r2 || c1 == c2) { n     cout << 1 <<    ; n   } else { n     cout << 2 <<    ; n   } n   if ((r1 + c1) % 2 != (r2 + c2) % 2) { n     cout << 0 <<    ; n   } else { n     if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2) { n       cout << 1 <<    ; n     } else { n       cout << 2 <<    ; n     } n   } n   cout << max(abs(r1 - r2), abs(c1 - c2)) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (d > 2 * c || 2 * d < c || d >= b) { n     printf( -1 n ); n     return 0; n   } n   int tmp = max(c, d); n   cout << max(d * 2 + 2, a) << endl; n   cout << max(d * 2 + 1, b) << endl; n   cout << tmp << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 20; n unsigned long long bit[N][20], ans, s; n int main() { n   int n, k, a; n   cin >> n >> k; n   for (int x = 1; x < N; x += x & (-x)) bit[x][0]++; n   for (int i = 0; i < n; i++) { n     cin >> a; n     for (int j = k + 1; j > 0; j--) { n       s = 0; n       for (int x = a; x > 0; x -= x & (-x)) s += bit[x][j - 1]; n       for (int x = a; x < N; x += x & (-x)) bit[x][j] += s; n     } n   } n   for (int x = n; x > 0; x -= x & (-x)) ans += bit[x][k + 1]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dr[] = {-1, 0, 1, 0}; n const int dc[] = {0, 1, 0, -1}; n string FILE_NAME =  testcase.C ; n string NAME; n string itos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n int main() { n   int n, k; n   cin >> n >> k; n   if (k >= n * (n - 1) / 2) { n     cout <<  no solution  << endl; n   } else { n     for (int i = 0; i < (n); i++) { n       cout << 0 <<     << i << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e4; n bitset<maxn * 25> f[50][25]; n int n; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   vector<int> a(n * 2); n   for (int i = 1; i <= n * 2; i++) cin >> a[i - 1]; n   sort(a.begin(), a.end()); n   vector<vector<int>> ans(2); n   ans[0].push_back(a[0]), ans[1].push_back(a[1]); n   int sum = 0; n   for (int i = 2; i <= n * 2 - 1; i++) sum += a[i]; n   int lim = sum / 2; n   f[1][0].set(0); n   for (int i = 2; i <= n * 2 - 1; i++) n     for (int j = 0; j <= min(i, n - 1); j++) { n       f[i][j] |= f[i - 1][j]; n       if (j) f[i][j] |= f[i - 1][j - 1] << a[i]; n     } n   int s1 = -1, cnt = n - 1, id = 2 * n - 1; n   for (int i = lim; i >= 0; i--) { n     if (f[id][cnt][i]) { n       s1 = i; n       break; n     } n   } n   assert(s1 >= 0); n   for (int i = id; i >= 2; i--) { n     assert(f[i][cnt][s1]); n     if (cnt >= 1 && s1 >= a[i] && f[i - 1][cnt - 1][s1 - a[i]]) { n       ans[0].push_back(a[i]); n       s1 -= a[i], cnt--; n     } else n       ans[1].push_back(a[i]); n   } n   sort(ans[0].begin(), ans[0].end()); n   sort(ans[1].begin(), ans[1].end(), greater<int>()); n   for (int i = 0; i <= 1; i++) { n     for (auto x : ans[i]) cout << x <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100100], b[100100]; n int n, x; n multiset<int> st; n multiset<int>::iterator it; n int main() { n   scanf( %d%d , &n, &x); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   for (int i = 0; i < n; i++) { n     int w; n     scanf( %d , &w); n     st.insert(w); n   } n   sort(a, a + n); n   reverse(a, a + n); n   int res = 1; n   for (int i = 0; i < n; i++) { n     int w = x - a[i]; n     it = st.lower_bound(w); n     if (it != st.end()) { n       st.erase(it); n       res = i + 1; n     } n   } n   printf( 1 %d , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 9; n long long n, a, b, val[MAX], incc[MAX], decc[MAX]; n vector<int> g[MAX]; n void dfs(int v, int p) { n   for (auto u : g[v]) n     if (u != p) n       dfs(u, v), decc[v] = max(decc[v], decc[u]), n                  incc[v] = max(incc[v], incc[u]); n   long long meg = val[v] + incc[v] - decc[v]; n   if (meg < 0) n     incc[v] += -meg; n   else n     decc[v] += meg; n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) n     cin >> a >> b, a--, b--, g[a].push_back(b), g[b].push_back(a); n   for (int i = 0; i < n; i++) cin >> val[i]; n   dfs(0, -1); n   cout << incc[0] + decc[0]; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ans, p; n int main() { n   scanf( %d , &n); n   p = 2; n   for (int i = 1; i <= n; i++) { n     cout << (long long)i * (i + 1) * (i + 1) - p / i << endl; n     p = (long long)i * (i + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int x1, x2, y1, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   if (x1 == x2) { n     int w = max(y1, y2) - min(y1, y2); n     cout << x1 + w <<     << y1 <<     << x2 + w <<     << y2 << endl; n   } else if (y1 == y2) { n     int w = max(x1, x2) - min(x1, x2); n     cout << x1 <<     << y1 + w <<     << x2 <<     << y2 + w << endl; n   } else if (abs(x1 - x2) == abs(y1 - y2)) { n     cout << x1 <<     << y2 <<     << x2 <<     << y1 << endl; n   } else { n     cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t = 0; n   string s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s[0] >=  0  && s[0] <=  9 ) { n       if (s.size() == 1 || (s.size() == 2 && s <  18 )) t++; n     } else if (s ==  ABSINTH  || s ==  CHAMPAGNE  || s ==  TEQUILA  || n                s ==  WINE  || s ==  WHISKEY  || s ==  VODKA  || s ==  SAKE  || n                s ==  RUM  || s ==  GIN  || s ==  BRANDY  || s ==  BEER ) n       t++; n   } n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double cooy1, cooy2, cooyw, cooxb, cooyb, r; n double s1, s2; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> cooy1 >> cooy2 >> cooyw >> cooxb >> cooyb >> r; n   cooy1 += r; n   cooy2 -= r; n   cooyw -= r; n   s1 = 2 * cooyw - cooy1; n   s2 = 2 * cooyw - cooy2; n   double xx1 = cooxb * (cooyw - s1) / (cooyb - s1); n   double xx2 = cooxb * (cooyw - s2) / (cooyb - s2); n   double dd1 = sqrt(cooxb * cooxb + (s1 - cooyb) * (s1 - cooyb)) * r / cooxb; n   double dd2 = sqrt(cooxb * cooxb + (s2 - cooyb) * (s2 - cooyb)) * r / cooxb; n   if (s1 - dd1 < s2 - r) { n     if (s2 - dd2 < s2 - r) { n       cout << -1; n       return 0; n     } n     cout.precision(30); n     cout << xx2; n     return 0; n   } else { n     cout.precision(30); n     cout << xx1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long power(long long a, long long b) { n   a = a % mod; n   long long ans = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       ans = (ans * a) % mod; n     } n     b = b / 2; n     a = (a * a) % mod; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long loc[n + 1]; n     long long a[n]; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       loc[a[i]] = i; n     } n     map<long long, long long> p; n     long long m = 0; n     for (long long i = 1; i <= n; i++) { n       for (long long j = loc[i]; j - 1 >= 0 && p[j - 1] == 0; j--) { n         if (m > (n - 1)) { n           break; n         } n         if (a[j - 1] > a[j]) { n           swap(loc[a[j]], loc[a[j - 1]]); n           swap(a[j], a[j - 1]); n           p[j - 1]++; n           m++; n         } else { n           break; n         } n       } n     } n     for (long long i = 0; i < n; i++) { n       cout << a[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 3e5 + 10; n long long ans; n long long n, k; n long long s[maxn]; n long long l[maxn]; n long long r[maxn]; n long long seg[4 * maxn]; n long long lazy[4 * maxn]; n vector<long long> bois[maxn]; n void in(); n void solve(); n long long doaz(long long); n long long get(long long, long long, long long, long long, long long); n void upd(long long, long long, long long, long long, long long); n void push(long long, long long, long long); n void out(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   in(); n   solve(); n   out(); n } n void in() { n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> s[i]; n   for (long long i = 0; i < k; i++) cin >> l[i] >> r[i]; n } n void solve() { n   sort(s, s + n); n   for (long long i = 0; i < k; i++) { n     l[i] = lower_bound(s, s + n, l[i]) - s; n     r[i] = upper_bound(s, s + n, r[i]) - s; n     bois[l[i]].push_back(i); n     bois[r[i]].push_back(i); n   } n   for (long long i = 0; i < n; i++) { n     l[k + i] = i; n     r[k + i] = i + 1; n     bois[i].push_back(k + i); n   } n   ans = n * (n - 1) * (n - 2) / 6; n   for (long long i = 0; i < n; i++) { n     for (auto j : bois[i]) upd(1, 0, n, l[j], r[j]); n     ans -= doaz(get(1, 0, n, 0, n) - get(1, 0, n, i, i + 1)); n   } n } n long long get(long long id, long long l, long long r, long long L, n               long long R) { n   if (r <= L || R <= l) return 0; n   if (L <= l && r <= R) return seg[id]; n   if (lazy[id]) push(id, l, r); n   long long mid = (l + r) >> 1; n   return get(id << 1, l, mid, L, R) + get(id << 1 | 1, mid, r, L, R); n } n void upd(long long id, long long l, long long r, long long L, long long R) { n   if (r <= L || R <= l) return; n   if (L <= l && r <= R) { n     seg[id] = r - l - seg[id]; n     lazy[id] = 1 - lazy[id]; n     return; n   } n   if (lazy[id]) push(id, l, r); n   long long mid = (l + r) >> 1; n   upd(id << 1, l, mid, L, R); n   upd(id << 1 | 1, mid, r, L, R); n   seg[id] = seg[id << 1] + seg[id << 1 | 1]; n } n void push(long long id, long long l, long long r) { n   long long mid = (l + r) >> 1; n   lazy[id] = 0; n   lazy[id << 1] = 1 - lazy[id << 1]; n   lazy[id << 1 | 1] = 1 - lazy[id << 1 | 1]; n   seg[id << 1] = mid - l - seg[id << 1]; n   seg[id << 1 | 1] = r - mid - seg[id << 1 | 1]; n } n bool cmp(long long i, long long j) { return s[i] < s[j]; } n long long doaz(long long x) { return x * (x - 1) / 2; } n void out() { cout << ans; } n


#include <bits/stdc++.h> n using namespace std; n void dbg() { std::cout <<    # n ; } n template <typename T, typename... Args> n void dbg(T a, Args... args) { n   std::cout << a <<    ; n   dbg(args...); n } n const long long maxn = 2e5 + 10; n const long long MAX = 1000; n const long long inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n long long n, d, m; n long long a[maxn]; n long long b[maxn]; n multiset<long long> small; n void solve() { n   cin >> n >> d >> m; n   for (long long i = (1); i <= (n); ++i) { n     cin >> a[i]; n   } n   sort(a + 1, a + 1 + n); n   long long pos = upper_bound(a + 1, a + 1 + n, m) - a; n   long long cans = 0; n   long long ans = 0; n   for (long long i = (1); i <= (pos - 1); ++i) small.insert(a[i]), cans += a[i]; n   long long forsmall = n - 1; n   ans = max(cans, ans); n   for (long long i = (1); i <= (n - pos + 1); ++i) { n     if (forsmall < 0) break; n     while (small.size() > forsmall) { n       cans -= *small.begin(); n       small.erase(small.begin()); n     } n     cans += a[n - i + 1]; n     ans = max(ans, cans); n     forsmall -= (d + 1); n   } n   cout << (ans) << endl; n } n signed main() { n   long long TestCase = 1; n   while (TestCase--) { n     solve(); n   } n   char EndFile = getchar(); n   EndFile = getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[101], n, imin = 0, imax = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     if (arr[i] == 1) n       imin = i; n     else if (arr[i] == n) n       imax = i; n   } n   int a; n   if (imax < imin) swap(imax, imin); n   int rsp = abs(imin - imax) + max(n - imax, imin - 1); n   cout << rsp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cout << fixed; n   setprecision(15); n   double x, y; n   cin >> n >> x >> y; n   long long p1 = 0, p2 = 0, ans = 0; n   set<double> s; n   for (long long i = 0; i < n; i++) { n     double a, b; n     cin >> a >> b; n     if (a == x) { n       if (s.find((double)1e7) == s.end()) { n         s.insert((double)1e7); n       } n       continue; n     } n     if (b == y) { n       if (s.find((double)1e6) == s.end()) { n         s.insert((double)1e6); n       } n       continue; n     } n     double slope = (double)(b - y) / (a - x); n     if (s.find(slope) == s.end()) { n       s.insert(slope); n     } n   } n   cout << (long long)s.size() <<   n ; n } n signed main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int par[200010]; n bool isLeaf[200010]; n vector<int> adj[200010]; n long long f[200010], dp[200010], s[200010], ans = 0, n; n void duyet(int u) { n   s[u] = 1; n   f[u] = 0; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (par[u] != v) { n       par[v] = u; n       duyet(v); n       s[u] += s[v]; n       f[u] += f[v]; n     } n   } n   f[u] += s[u]; n } n void cal(int u) { n   long long tong = 0; n   dp[u] = dp[par[u]] - 2 * s[u] + n; n   ans = max(ans, dp[u]); n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (par[u] != v) { n       cal(v); n     } n   } n } n void solve() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     par[i] = 0; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   par[n] = 0; n   duyet(1); n   dp[1] = f[1]; n   for (int i = 0; i < adj[1].size(); i++) { n     int v = adj[1][i]; n     cal(v); n   } n   cout << ans << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long maxn = 2e5 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7; n long long n, m, du[maxn], vis[maxn], f[maxn]; n long long res, ans = 1; n vector<long long> e[maxn]; n map<pair<long long, long long>, long long> mp; n inline long long ksm(long long a, long long b) { n   long long k = 1; n   while (b) { n     if (b & 1) k = k * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return k; n } n void topo() { n   queue<long long> q; n   for (long long i = 1; i <= n; i++) { n     if (du[i] == 1) { n       q.push(i); n       vis[i] = i; n     } n   } n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (auto v : e[u]) { n       du[v]--; n       if (du[v] <= 1 && !vis[v]) { n         vis[v] = v; n         q.push(v); n       } n     } n   } n } n void dfs(long long u, long long fa) { n   vis[u] = fa; n   f[fa]++; n   for (auto v : e[u]) { n     if (!vis[v] && du[v] > 1) { n       dfs(v, fa); n     } n   } n } n signed main() { n   cin >> n; n   long long flag = 0; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     e[i].emplace_back(x); n     e[x].emplace_back(i); n     du[i]++; n     du[x]++; n   } n   topo(); n   for (long long i = 1; i <= n; i++) { n     if (!vis[i] && du[i] > 1) { n       dfs(i, i); n     } n   } n   for (long long i = 1; i <= n; i++) { n     if (vis[i] == i && f[i] == 0) { n       res++; n     } else if (vis[i] == i && f[i] > 1) { n       ans = ans * (ksm(2, f[i]) - 2) % mod; n     } n   } n   cout << ans * ksm(2, res) % mod; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   long long int n, s, p, q; n   char d; n   map<long long int, long long int> p1, p2; n   cin >> n >> s; n   for (long long int i = 1; i <= n; i++) { n     cin >> d >> p >> q; n     if (d ==  B ) n       p1[p] += q; n     else if (d ==  S ) n       p2[p] += q; n   } n   map<long long int, long long int>::reverse_iterator rit; n   map<long long int, long long int>::iterator it; n   vector<pair<long long int, long long int> > p3; n   long long int cnt = 0; n   for (it = p2.begin(); it != p2.end(); it++) { n     if (cnt < s) { n       p3.push_back(make_pair(it->first, it->second)); n       cnt++; n     } n   } n   cnt = 0; n   reverse(p3.begin(), p3.end()); n   vector<pair<long long int, long long int> >::iterator it2; n   for (it2 = p3.begin(); it2 != p3.end(); ++it2) { n     if (cnt < s) { n       cout <<  S  n            <<     << it2->first <<     << it2->second << endl; n       cnt++; n     } n   } n   cnt = 0; n   for (rit = p1.rbegin(); rit != p1.rend(); ++rit) { n     if (cnt < s) { n       cout <<  B  n            <<     << rit->first <<     << rit->second << endl; n       cnt++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1005][1005]; n int sum[1005][1005]; n int mn = 1000000007, area = 0; n int X, Y, n, m; n int extend(int x, int y, int dx, int dy) { n   int mx = 0; n   if (x + dx <= n && y + dy - 1 <= m && n       sum[x + dx][y + dy - 1] - sum[x + dx][y - 1] - sum[x][y + dy - 1] + n               sum[x][y - 1] == n           dx * dy) n     mx = dy + extend(x + 1, y, dx, dy); n   else if (y + dy <= m && x + dx - 1 <= n && n            sum[x + dx - 1][y + dy] - sum[x + dx - 1][y] - sum[x - 1][y + dy] + n                    sum[x - 1][y] == n                dx * dy) n     mx = dx + extend(x, y + 1, dx, dy); n   return mx; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> (s[i] + 1); n   int x, y; n   for (x = 1; x <= n; x++) { n     for (y = 1; y <= m; y++) n       if (s[x][y] ==  X ) break; n     if (y < m + 1) break; n   } n   X = x, Y = y; n   int extX = 0, extY = 0; n   for (int i = x; i <= n; i++) n     if (s[i][y] ==  X ) extX++; n   for (int i = y; i <= m; i++) n     if (s[x][i] ==  X ) extY++; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       sum[i][j] = (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + n                    (s[i][j] ==  X )), n       area += (s[i][j] ==  X ); n     } n   for (int dx = 1; dx <= extX; dx++) { n     if (dx * extY > mn) break; n     if (sum[x + dx - 1][y + extY - 1] - sum[x + dx - 1][y - 1] - n             sum[x - 1][y + extY - 1] + sum[x - 1][y - 1] == n         dx * extY) { n       int dArea = dx * extY + extend(x, y, dx, extY); n       if (dArea == area && dx * extY < mn) mn = dx * extY; n     } n   } n   for (int dy = 1; dy <= extY; dy++) { n     if (dy * extX > mn) break; n     if (sum[x + extX - 1][y + dy - 1] - sum[x + extX - 1][y - 1] - n             sum[x - 1][y + dy - 1] + sum[x - 1][y - 1] == n         extX * dy) { n       int dArea = dy * extX + extend(x, y, extX, dy); n       if (dArea == area && extX * dy < mn) mn = extX * dy; n     } n   } n   if (mn == 1000000007) n     cout <<  -1  << endl; n   else n     cout << mn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   set<int> s; n   multiset<int> m; n   auto add = [&](int x) { n     auto it = s.insert(x).first; n     if (next(it) != s.end() && it != s.begin()) n       m.erase(m.find(*next(it) - *prev(it))); n     if (next(it) != s.end()) m.insert(*next(it) - x); n     if (it != s.begin()) m.insert(x - *prev(it)); n   }; n   auto rm = [&](int x) { n     s.erase(x); n     auto it = s.lower_bound(x); n     if (it != s.end()) m.erase(m.find(*it - x)); n     if (it != s.begin()) m.erase(m.find(x - *prev(it))); n     if (it != s.end() && it != s.begin()) m.insert(*it - *prev(it)); n   }; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     add(x); n   } n   auto solve = [&]() -> long long { n     if (s.size() <= 1) n       return 0; n     else n       return (*s.rbegin() - *s.begin() - *m.rbegin()); n   }; n   cout << solve() <<   n ; n   while (q--) { n     int t, x; n     cin >> t >> x; n     if (t == 0) n       rm(x); n     else n       add(x); n     cout << solve() <<   n ; n   } n   return 0; n } n


// author: sharad12arse n #include<bits/stdc++.h> n using namespace std; n  n #define ll long long n #define ull unsigned long long n #define gcd __gcd n #define INF LONG_LONG_MAX n  n void solve(){ n  int n; n  cin>>n; n  ll a1=0,a2=0,x=0; n  vector<ll> arr(n); n  for(int i=0;i<n;i++){ n   cin>>arr[i]; n   x^=arr[i]; n  } n  a2=x; n  for(int i=0;i<n-1;i++){ n   a1^=arr[i]; n   a2^=arr[i]; n   if(a1==a2){ n    cout<< YES n ; n    return; n   } n  } n  a1=0,a2=0; n  for(int i=0;i<n-1;i++){ n   a1^=arr[i]; n   a2=0; n   for(int j=n-1;j>i;j--){ n    a2^=arr[j]; n    if(a1==a2&&((x^a1)^a2)==a1){ n     cout<< YES n ; n     return; n    } n   } n  } n  cout<< NO n ; n } n  n int main(){ n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  int t=1; n  cin>>t; n  while(t--){ n   solve(); n  } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 200005; n const int MD = 998244353; n void add(int& a, int b) { n   a += b; n   if (a >= MD) a -= MD; n } n void sub(int& a, int b) { n   a -= b; n   if (a < 0) a += MD; n } n int mod_mult(long long a, long long b) { return (a * b) % MD; } n int h[MX]; n int fact[MX]; n int inv_fact[MX]; n int exp(int a, int b) { n   int res = 1; n   while (b) { n     if (b % 2 == 1) res = mod_mult(res, a); n     a = mod_mult(a, a); n     b /= 2; n   } n   return res; n } n int C(int n, int k) { n   int tmp = fact[n]; n   tmp = mod_mult(tmp, inv_fact[k]); n   tmp = mod_mult(tmp, inv_fact[n - k]); n   return tmp; n } n int f(int x) { n   int tmp = exp(2, x); n   if (x % 2 == 0) { n     sub(tmp, C(x, x / 2)); n   } n   tmp = mod_mult(tmp, inv_fact[2]); n   return tmp; n } n int main() { n   fact[0] = 1; n   for (int i = 1; i < MX; i++) { n     fact[i] = mod_mult(fact[i - 1], i); n   } n   inv_fact[MX - 1] = exp(fact[MX - 1], MD - 2); n   for (int i = MX - 2; i >= 0; i--) { n     inv_fact[i] = mod_mult(inv_fact[i + 1], i + 1); n   } n   int n, k; n   ignore = scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     ignore = scanf( %d , h + i); n   } n   int mult_at_end = 1; n   int B = 0; n   for (int i = 0; i < n; i++) { n     if (h[i] == h[(i + 1) % n]) { n       mult_at_end = mod_mult(mult_at_end, k); n     } else { n       B++; n     } n   } n   int ans = 0; n   for (int c = 0; c <= B; c++) { n     int tmp = exp(k - 2, c); n     tmp = mod_mult(tmp, C(B, c)); n     tmp = mod_mult(tmp, f(B - c)); n     add(ans, tmp); n   } n   ans = mod_mult(ans, mult_at_end); n   printf( %d n , ans); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n const int M = 2; n struct Node { n   int parent, rela; n   void set(int p, int r) { n     parent = p; n     rela = r; n   } n } node[maxn]; n int Find(int x) { n   int tmp_p; n   if (node[x].parent != x) { n     tmp_p = node[x].parent; n     node[x].parent = Find(node[x].parent); n     node[x].rela = (node[x].rela + node[tmp_p].rela) % M; n   } n   return node[x].parent; n } n void Merge(int a, int b, int p, int q, int d) { n   node[p].parent = q; n   node[p].rela = (node[b].rela - node[a].rela - d + M) % M; n } n int n, m; n const long long mod = 1e9 + 7; n void init() { n   for (int i = 0; i < maxn; ++i) { n     node[i].parent = i, node[i].rela = 0; n   } n } n long long powerMod(int x, int k) { n   long long res = 1; n   long long base = x; n   while (k) { n     if (k & 1) res = res * base % mod; n     base = base * base % mod; n     k >>= 1; n   } n   return res; n } n int main() { n   init(); n   int num = 0; n   cin >> n >> m; n   int res = 1; n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     int p = Find(a); n     int q = Find(b); n     if (p != q) n       Merge(a, b, p, q, c ^ 1); n     else { n       if ((node[a].rela - node[b].rela + 2) % 2 != 1 - c) res = 0; n     } n   } n   if (res == 0) n     cout << res << endl; n   else { n     for (int i = 1; i <= n; ++i) n       if (Find(i) == i) num++; n     cout << powerMod(2, num - 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e6 + 6; n const long long MOD = 1e9 + 7; n bool comparator(long long i, long long j) { return (i < j); } n struct setCompare { n   bool operator()(const int64_t& lhs, const int64_t& rhs) const {} n }; n long long power(long long x, long long i) { n   long long ans = 1; n   while (i > 0) { n     if (i & 1) ans = (ans * x) % MOD; n     i >>= 1; n     x = (x * x) % MOD; n   } n   return ans; n } n long long power(long long x, long long i, long long mod) { n   long long ans = 1; n   while (i > 0) { n     if (i & 1) ans = (ans * x) % mod; n     i >>= 1; n     x = (x * x) % mod; n   } n   return ans; n } n long long modInverse(long long x, long long mod) { n   return power(x, mod - 2, mod); n } n bool isPalindrome(string s) { n   long long limit = s.length() / 2; n   for (long long i = 0; i < limit; i++) n     if (s[i] != s[s.length() - i - 1]) return false; n   return true; n } n long long gcd(long long x, long long y) { n   long long t; n   while (y != 0) t = x % y, x = y, y = t; n   return x; n } n bool isVowel(char ch) { n   if (ch ==  a  || ch ==  i  || ch ==  e  || ch ==  u  || ch ==  o  || n       ch ==  y ) { n     return true; n   } n   return false; n } n bool isPrime(long long n) { n   long long root = sqrt(n); n   for (long long i = 2; i <= root; i++) n     if (n % i == 0) return 0; n   return 1; n } n long double getDisSquare(long double asdfg, long double qwert, n                          long double gfdsa, long double trewq) { n   return ((asdfg - gfdsa) * (asdfg - gfdsa) + n           (qwert - trewq) * (qwert - trewq)); n } n bool isSquareHelper(long long asdfg, long long qwert, long long gfdsa, n                     long long trewq, long long x3, long long y3, long long x4, n                     long long y4) { n   long double d1, d2, d3, d4, d5, d6; n   d1 = getDisSquare(asdfg, qwert, gfdsa, trewq); n   d2 = getDisSquare(gfdsa, trewq, x3, y3); n   d3 = getDisSquare(x3, y3, x4, y4); n   d4 = getDisSquare(x4, y4, asdfg, qwert); n   if (d1 == d2 && d1 == d3 && d1 == d4) { n     d5 = getDisSquare(asdfg, qwert, x3, y3); n     d6 = getDisSquare(gfdsa, trewq, x4, y4); n     if (d5 == d6) { n       return 1; n     } n   } n   return 0; n } n bool isSquare(long long asdfg, long long qwert, long long gfdsa, n               long long trewq, long long x3, long long y3, long long x4, n               long long y4) { n   if (isSquareHelper(asdfg, qwert, gfdsa, trewq, x3, y3, x4, y4) || n       isSquareHelper(asdfg, qwert, gfdsa, trewq, x4, y4, x3, y3) || n       isSquareHelper(asdfg, qwert, x3, y3, gfdsa, trewq, x4, y4)) n     return 1; n   else n     return 0; n } n bool isEqualateralTriangle(long long asdfg, long long qwert, long long gfdsa, n                            long long trewq, long long x3, long long y3) { n   long long d1 = getDisSquare(asdfg, qwert, gfdsa, trewq); n   long long d2 = getDisSquare(gfdsa, trewq, x3, y3); n   long long d3 = getDisSquare(x3, y3, asdfg, qwert); n   if (d1 == d2 && d1 == d3) { n     return 1; n   } else n     return 0; n } n bool isRightAngleTriangleHelper(long long asdfg, long long qwert, n                                 long long gfdsa, long long trewq, long long x3, n                                 long long y3) { n   long long d1 = getDisSquare(asdfg, qwert, gfdsa, trewq); n   long long d2 = getDisSquare(gfdsa, trewq, x3, y3); n   long long d3 = getDisSquare(x3, y3, asdfg, qwert); n   if (d2 == (d1 + d3)) { n     return 1; n   } n   return 0; n } n bool isRightAngleTriangle(long long asdfg, long long qwert, long long gfdsa, n                           long long trewq, long long x3, long long y3) { n   if (isRightAngleTriangleHelper(asdfg, qwert, gfdsa, trewq, x3, y3) || n       isRightAngleTriangleHelper(gfdsa, trewq, x3, y3, asdfg, qwert) || n       isRightAngleTriangleHelper(x3, y3, asdfg, qwert, gfdsa, trewq)) n     return 1; n   return 0; n } n bool areCollinear(long long asdfg, long long qwert, long long gfdsa, n                   long long trewq, long long x3, long long y3) { n   return ((x3 - asdfg) * (trewq - qwert) == (y3 - qwert) * (gfdsa - asdfg)); n } n long long n, a, b, c, t[maxn], k; n void getInput() { n   cin >> n >> a >> b >> c >> k; n   for (long long i = 0; i < n; i++) cin >> t[i]; n } n void getAnswer() {} n void solve() { n   sort(t, t + n); n   long long ans = 0; n   long long x, y; n   x = a / b; n   y = a % b; n   for (long long i = 0; i < n; i++) { n     if (b >= c) { n       ans += a; n     } else { n       long long tt = k - t[i]; n       ans += (a - b * tt + c * tt); n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) getInput(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long long MX = 1e9; n const long long INF = 1e9; n void solve() { n   string s; n   cin >> s; n   int n = s.length(), mx[n + 1], c = 0; n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] ==  1 ) n       ++c; n     else if (c) n       --c; n     mx[i] = c; n   } n   mx[n + 1] = c; n   for (int i = n - 1; i >= 0; --i) mx[i] = min(mx[i], mx[i + 1]); n   int nz = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  1 ) { n       if (nz < mx[i + 1]) { n         s[i] =  0 ; n         ++nz; n       } n     } n   } n   cout << s <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100000 + 10; n int mod; n int n, m; n vector<int> E[MAX_N]; n bool v[MAX_N]; n int cnt; n void dfs(int u) { n   if (v[u]) return; n   v[u] = true; n   ++cnt; n   for (vector<int>::iterator e = E[u].begin(); e != E[u].end(); ++e) { n     dfs(*e); n   } n } n int main() { n   cin >> n >> m >> mod; n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a, --b; n     E[a].push_back(b), E[b].push_back(a); n   } n   memset(v, 0, sizeof v); n   vector<int> comp; n   for (int i = 0; i < n; ++i) { n     if (!v[i]) { n       cnt = 0; n       dfs(i); n       comp.push_back(cnt); n     } n   } n   if (comp.size() == 1) { n     cout << 1 % mod << endl; n     return 0; n   } n   long long ans = 1, sum = 0; n   for (vector<int>::iterator e = comp.begin(); e != comp.end(); ++e) { n     ans = ans * (*e) % mod; n     sum += *e; n     sum %= mod; n   } n   for (int i = 0; i < comp.size() - 2; ++i) { n     ans = ans * sum % mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v; n   int w; n   Edge* next; n   void add(int v, int w, Edge*& head) { n     this->v = v; n     this->w = w; n     this->next = head; n     head = this; n   } n } edges[200100]; n Edge* h[100100]; n struct Q { n   int v; n   int t; n   bool operator<(const Q& q) const { return v < q.v || v == q.v && t < q.t; } n   void init(int v, int t) { n     this->v = v; n     this->t = t; n   } n } q[100100], q0; n void addEdge(int v1, int v2, int w) { n   static Edge* e = edges; n   (e++)->add(v2, w, h[v1]); n   (e++)->add(v1, w, h[v2]); n } n int d[100010]; n struct CMP { n   bool operator()(int a, int b) const { n     return d[a] < d[b] || d[a] == d[b] && a < b; n   } n }; n set<int, CMP> heap; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int v1, v2, w; n     scanf( %d %d %d , &v1, &v2, &w); n     v1--, v2--; n     addEdge(v1, v2, w); n   } n   int u = 0; n   for (int i = 0; i < n; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 0; j < k; j++) { n       int t; n       scanf( %d , &t); n       q[u++].init(i, t); n     } n   } n   memset(d, 127, sizeof(d)); n   d[0] = 0; n   heap.insert(0); n   while (!heap.empty()) { n     int v = *heap.begin(); n     heap.erase(heap.begin()); n     if (v == n - 1) { n       printf( %d n , d[v]); n       return 0; n     } n     int dist = d[v]; n     q0.init(v, dist); n     Q* qi = lower_bound(q, q + u, q0); n     while (qi < q + u && qi->v == v && qi->t == dist) { n       dist++; n       qi++; n     } n     for (Edge* e = h[v]; e; e = e->next) { n       if (d[e->v] > dist + e->w) { n         heap.erase(e->v); n         d[e->v] = dist + e->w; n         heap.insert(e->v); n       } n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long nCrModp_2(long long n, long long r, long long p) { n   if (r > n) { n     return 0; n   } n   if (n < 0 || r < 0) { n     return 0; n   } n   if (r == 0) return 1; n   long long fac[n + 1]; n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; n   return (fac[n] * power(fac[r], p - 2, p) % p * power(fac[n - r], p - 2, p) % n           p) % n          p; n } n int main() { n   long long n, m, k; n   cin >> n >> m >> k; n   long long ans = 0; n   long long mod = 998244353; n   ans = m; n   ans = ans * power(m - 1, k, mod); n   ans = ans % mod; n   ans = ans * nCrModp_2(n - 1, k, mod); n   ans = ans % mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int t, n, c_0, c_1, h; n   string s; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> n >> c_0 >> c_1 >> h; n     cin >> s; n     int sum = 0; n     for (int j = 0; j < n; j++) { n       sum += s[j] -  0 ; n     } n     if (c_0 - c_1 > h) { n       sum = c_1 * n + (n - sum) * h; n     } else if (c_1 - c_0 > h) { n       sum = c_0 * n + sum * h; n     } else n       sum = c_1 * sum + (n - sum) * c_0; n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[1010], vst[1010]; n char s[1010][1010]; n int main() { n   int n; n   scanf( %d , &n); n   int x = n, ed = 0, pos = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , p + i); n     if (p[i] != i) ed = i; n     for (int j = 1; j <= n; j++) s[i][j] =  . ; n   } n   for (int i = 1; i <= n; i++) n     if (p[i] == ed) pos = i; n   p[pos] = p[ed], p[ed] = ed; n   for (int i = 1; i <= ed; i++) n     if (!vst[i] && p[i] != i) { n       int u = i; n       char dir =  / ; n       while (!vst[u]) { n         vst[u] = 1; n         if (dir ==  / ) { n           s[x][u] =  / ; n           if (p[u] > u) n             s[x][p[u]] =  / ; n           else { n             s[x--][ed] =  / ; n             s[x][ed] = s[x][p[u]] = dir =     ; n           } n         } else { n           s[x][u] =     ; n           if (p[u] < u) n             s[x][p[u]] =     ; n           else { n             s[x--][i] =     ; n             s[x][i] = s[x][p[u]] = dir =  / ; n           } n         } n         u = p[u]; n       } n       x--; n     } n   if (ed) s[x][p[pos]] = s[x][ed] =  / ; n   printf( %d n , n - !!ed); n   for (int i = 1; i <= n; i++) printf( %s n , s[i] + 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 100; n const int maxK = 5000 + 10; n const long long INF = 1e18; n int a[maxn], n, k; n long long f[maxK][maxK]; n long long gao(int i, int j, int len) { n   int s = i * (n / k) + j * (n / k + 1) + 1; n   int t = s + len - 1; n   return a[t] - a[s]; n } n void solve() { n   sort(a + 1, a + 1 + n); n   int ni = k - n % k; n   int nj = n % k; n   f[0][0] = 0; n   for (int i = 0; i <= ni; i++) { n     for (int j = 0; j <= nj; j++) { n       if (i == 0 && j == 0) continue; n       f[i][j] = INF; n       if (i >= 1) { n         f[i][j] = min(f[i][j], f[i - 1][j] + gao(i - 1, j, n / k)); n       } n       if (j >= 1) { n         f[i][j] = min(f[i][j], f[i][j - 1] + gao(i, j - 1, n / k + 1)); n       } n     } n   } n   printf( %I64d n , f[ni][nj]); n } n int main() { n   while (scanf( %d%d , &n, &k) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long mx = 1e9 + 9, r = 0; n   for (int i = n - 1; i >= 0; i--) { n     a[i] = min(mx, a[i]); n     r += a[i]; n     mx = min(mx, max(0ll, a[i] - 1)); n   } n   cout << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010, M = 100010, SIZE = 26, LEN = 1000010; n int n, m, dfk = 0; n namespace BIT { n int c[LEN]; n void Add(int x, int k) { n   for (; x <= dfk; x += x & -x) c[x] += k; n   return; n } n int Sum(int x) { n   int sum = 0; n   for (; x; x -= x & -x) sum += c[x]; n   return sum; n } n }  // namespace BIT n struct Trie_Node; n struct Edge; n struct Trie_Node { n   Trie_Node() { n     dfn = size = 0; n     head = NULL; n     fail = NULL; n     for (int i = 0; i < SIZE; ++i) link[i] = NULL; n     return; n   } n   Trie_Node *fail, *link[SIZE]; n   Edge *head; n   int dfn, size; n } * node[M]; n struct Edge { n   Edge() { n     p = NULL; n     nxt = NULL; n     return; n   } n   Trie_Node *p; n   Edge *nxt; n }; n inline void Add_Edge(Trie_Node *u, Trie_Node *v) { n   Edge *w = new Edge(); n   w->p = v; n   w->nxt = u->head; n   u->head = w; n   return; n } n bool exist[M]; n char buff[LEN]; n queue<Trie_Node *> que; n void BFS(Trie_Node *root) { n   root->fail = root; n   for (int i = 0; i < SIZE; ++i) n     if (root->link[i]) { n       Add_Edge(root->link[i]->fail = root, root->link[i]); n       que.push(root->link[i]); n     } else n       root->link[i] = root; n   while (!que.empty()) { n     Trie_Node *u = que.front(); n     que.pop(); n     for (int i = 0; i < SIZE; ++i) n       if (u->link[i]) { n         Add_Edge(u->link[i]->fail = u->fail->link[i], u->link[i]); n         que.push(u->link[i]); n       } else n         u->link[i] = u->fail->link[i]; n   } n   return; n } n void DFS(Trie_Node *u) { n   u->dfn = ++dfk; n   u->size = 1; n   for (Edge *i = u->head; i; i = i->nxt) { n     Trie_Node *v = i->p; n     DFS(v); n     u->size += v->size; n   } n   return; n } n long long Query(Trie_Node *root, char *s) { n   long long res = 0; n   int j = 1, m = strlen(s + 1); n   for (Trie_Node *i = root; j <= m; ++j) { n     i = i->link[s[j] - 97]; n     res += BIT::Sum(i->dfn); n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   Trie_Node *root = new Trie_Node(); n   for (int i = 1; i <= m; ++i) { n     scanf( %s , buff + 1); n     int k = strlen(buff + 1); n     node[i] = root; n     for (int j = 1; j <= k; node[i] = node[i]->link[buff[j] - 97], ++j) n       if (!node[i]->link[buff[j] - 97]) n         node[i]->link[buff[j] - 97] = new Trie_Node(); n   } n   BFS(root); n   DFS(root); n   using namespace BIT; n   for (int i = 1; i <= m; ++i) { n     Add(node[i]->dfn, 1); n     Add(node[i]->dfn + node[i]->size, -1); n   } n   memset(exist, 1, sizeof(exist)); n   for (int i = 1; i <= n; ++i) { n     char t; n     cin >> t; n     if (t !=  ? ) { n       int x; n       scanf( %d , &x); n       if (t !=  - ) { n         if (!exist[x]) { n           exist[x] = 1; n           Add(node[x]->dfn, 1); n           Add(node[x]->dfn + node[x]->size, -1); n         } n       } else if (exist[x]) { n         exist[x] = 0; n         Add(node[x]->dfn, -1); n         Add(node[x]->dfn + node[x]->size, 1); n       } n     } else { n       scanf( %s , buff + 1); n       printf( %lld n , Query(root, buff)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 505, mod = 998244353; n int n, m; n vector<int> c, pos[MX]; n long long mem[2 * MX][2 * MX]; n bool isValid(int a, int b) { n   if (a != pos[c[a]][0] && a != pos[c[a]].back()) return 0; n   if (b != pos[c[b]][0] && b != pos[c[b]].back()) return 0; n   for (int i = 1; i <= n; i++) n     if ((a <= pos[i][0] && pos[i][0] <= b) != n         (a <= pos[i].back() && pos[i].back() <= b)) n       return 0; n   return 1; n } n long long dp(int a, int b) { n   if (a > b) return 1; n   long long &res = mem[a][b]; n   if (res != -1) return res; n   if (!isValid(a, b)) return res = 0; n   int mn = c[a]; n   for (int i = a; i <= b; i++) mn = min(mn, c[i]); n   int ind = pos[mn][0]; n   long long x = dp(a, ind - 1); n   for (int i = a; i < ind; i++) x += dp(a, i) * dp(i + 1, ind - 1) % mod; n   x %= mod; n   ind = pos[mn].back(); n   long long y = dp(ind + 1, b); n   for (int i = ind + 1; i <= b; i++) y += dp(ind + 1, i) * dp(i + 1, b) % mod; n   y %= mod; n   res = 1; n   for (int i = 0; i + 1 < pos[mn].size(); i++) n     (res *= dp(pos[mn][i] + 1, pos[mn][i + 1] - 1)) %= mod; n   (res *= x) %= mod; n   (res *= y) %= mod; n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   c.resize(m); n   for (int i = 0; i < m; i++) cin >> c[i]; n   c.erase(unique(c.begin(), c.end()), c.end()); n   if (c.size() > 2 * n) { n     cout << 0 <<   n ; n     return 0; n   } n   m = c.size(); n   for (int i = 0; i < m; i++) pos[c[i]].emplace_back(i); n   memset(mem, -1, sizeof(mem)); n   cout << dp(0, m - 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3; n vector<int> g[1000001]; n int p[1000001], c[1000001], a[1000001], sz[1000001], root, n; n char sub(string a, string b) { n   for (int i = 0; i < b.size(); i++) { n     if (a[i] != b[i]) return a[i]; n   } n   return a.back(); n } n vector<string> query(int l, int r) { n   vector<string> v; n   cout <<  ?   << l <<     << r << endl; n   fflush(stdout); n   for (int i = 0; i < (r - l + 1) * (r - l + 2) / 2; i++) { n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     v.push_back(s); n   } n   return v; n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     auto a = query(1, n); n     cout <<  !   << a[0] << endl; n     fflush(stdout); n     return 0; n   } n   auto a = query(1, n); n   auto b = query(1, n - 1); n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   vector<string> suf; n   int l = 0, r = 0; n   while (l < a.size() || r < b.size()) { n     if (r == b.size() || a[l] != b[r]) { n       suf.push_back(a[l]); n       l++; n     } else { n       l++, r++; n     } n   } n   sort(suf.begin(), suf.end(), n        [&](string& a, string& b) { return a.size() < b.size(); }); n   string ans; n   ans += suf[0]; n   for (int i = 1; i < suf.size(); i++) ans += sub(suf[i], suf[i - 1]); n   reverse(ans.begin(), ans.end()); n   cout <<  !   << ans << endl; n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n const int siz = 1000000 + 10; n char a[siz]; n int main() { n   int t, n, m = 0, i, l, k; n   scanf( %d , &t); n   scanf( %s , a); n   int len = strlen(a); n   int z = 0, o = 0; n   for (l = 0; l < len; l++) { n     if (a[l] ==  1 ) n       o++; n     else if (a[l] ==  0 ) n       z++; n   } n   m = min(o, z); n   len -= (2 * m); n   printf( %d , len); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, N, id[6100]; n bool judge(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 void work() { n   if (judge(N)) return; n   if (N % 2 && !judge(N - 2)) N -= 3, id[3] = 3; n   for (int i = 1; i <= n; i++) { n     if (judge(i) && judge(N - i)) { n       id[i] = 2; n       break; n     } n   } n } n int main() { n   cin >> n; n   N = n * (n + 1) / 2; n   for (int i = 1; i <= n; i++) id[i] = 1; n   if (n == 1) puts( -1 ); n   work(); n   for (int i = 1; i <= n; i++) cout << id[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(), cout.tie(); n   bool w = false; n   int cnt = 0; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     int zero = 0, one = 0; n     if (w && s[i] ==  0 ) { n       cout << 1 <<     << 1 << endl; n       w = false; n     } else if (s[i] ==  0 ) { n       cout << 3 <<     << 1 << endl; n       w = true; n     } else { n       cout << 4 - cnt <<     << 2 << endl; n       cnt++; n       if (cnt == 4) cnt = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double t[100001], v[100001], fin[100001]; n int main() { n   double n, a, d; n   cin >> n >> a >> d; n   for (int i = 0; i < n; ++i) { n     cin >> t[i] >> v[i]; n   } n   cout.precision(5); n   for (int i = 0; i < n; ++i) { n     fin[i] = 2 * d * a <= v[i] * v[i] ? sqrt(2.0 * d / a) n                                       : (d / v[i] + v[i] / (2 * a)); n     fin[i] += t[i]; n     if (i) fin[i] = max(fin[i], fin[i - 1]); n     cout << fixed << fin[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int a[N], p[N], d[N]; n int main(int argc, char const *argv[]) { n   int n, gain = 0; n   scanf( %d n , &n); n   for (int i = 2; i <= n; i++) scanf( %d , p + i); n   for (int i = 1; i <= n; i++) d[i] = d[p[i]] + 1; n   for (int i = 1; i <= n; i++) a[d[i]] += 1; n   for (int i = 1; i <= n; i++) gain += (a[i] % 2); n   printf( %d n , gain); n   return 0; n } n