#include <bits/stdc++.h> n using namespace std; n int n; n int a[300005] = {0}; n int c1 = 0, c2 = 0, p1 = 0, p2 = 0; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int maxn = -1; n   for (int i = 1; i <= n; i++) { n     if (c1 == 0) { n       p1 = i; n       c1 = a[i]; n     } else if (c2 == 0 && a[i] != c1) { n       p2 = i; n       c2 = a[i]; n       maxn = max(maxn, p2 - p1); n     } else if (a[i] == c1 && p2 != 0) { n       maxn = max(maxn, i - p2); n     } else if (a[i] == c2) { n       maxn = max(maxn, i - p1); n     } else if (a[i] != c1 && a[i] != c2) { n       maxn = max(maxn, i - p1); n     } n   } n   cout << maxn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <class T> n inline bool read(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n const int INF = 2000000009; n const int MX = 200005; n const double EPS = 1e-9; n const int MOD = 1000000007; n long long a[MX]; n vector<long long> sq, nsq; n int n, p, q; n long long solve1() { n   vector<long long> cost; n   for (int i = 0; i < q; i++) { n     long long t = sqrt(nsq[i]); n     cost.push_back(min(nsq[i] - t * t, (t + 1) * (t + 1) - nsq[i])); n   } n   sort(cost.begin(), cost.end()); n   long long ret = 0; n   int t = (n / 2) - p; n   for (int i = 0; i < t; i++) ret += cost[i]; n   return ret; n } n long long solve2() { n   long long ret = 0; n   vector<long long> cost; n   for (int i = 0; i < p; i++) { n     if (sq[i] == 0) n       cost.push_back(2LL); n     else n       cost.push_back(1LL); n   } n   sort(cost.begin(), cost.end()); n   int t = (n / 2) - q; n   for (int i = 0; i < t; i++) ret += cost[i]; n   return ret; n } n int main() { n   n = ({ n     int a; n     read(a); n     a; n   }); n   for (int i = 0; i < n; i++) { n     a[i] = ({ n       long long a; n       read(a); n       a; n     }); n     long long t = sqrt(a[i]); n     if (t * t == a[i]) n       sq.push_back(a[i]); n     else n       nsq.push_back(a[i]); n   } n   p = (int)sq.size(); n   q = (int)nsq.size(); n   if (p == q) { n     printf( 0 n ); n     return 0; n   } n   if (q > p) { n     printf( %lld n , solve1()); n   } else { n     printf( %lld n , solve2()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<int> E[200020], V; n int dis[2][200020]; n void solve() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     int x; n     scanf( %d , &x); n     V.push_back(x); n   } n   for (int i = 0; i < (m); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     E[x].push_back(y); n     E[y].push_back(x); n   } n   for (int v = 0; v < (2); v++) { n     int st = (v == 0 ? 1 : n); n     vector<int> q; n     auto D = dis[v]; n     for (int i = 1; i <= n; i++) D[i] = -1; n     D[st] = 0; n     q.push_back(st); n     for (int i = 0; i < ((int)(q).size()); i++) { n       int t = q[i]; n       for (int e : E[t]) n         if (D[e] == -1) { n           D[e] = D[t] + 1; n           q.push_back(e); n         } n     } n   } n   sort((V).begin(), (V).end(), n        [&](int x, int y) { return dis[0][x] > dis[0][y]; }); n   int mx = -1e9, ans = 0; n   for (int i = 0; i + 1 < (int)(V).size(); i++) { n     if (dis[0][V[i]] == dis[0][V[i + 1]]) { n       printf( %d n , dis[0][n]); n       return; n     } n   } n   for (int i = 0; i < ((int)(V).size()); i++) { n     int u = V[i]; n     int val = min({dis[0][n], dis[0][u] + dis[1][u], dis[0][u] + 1 + mx}); n     ans = max(ans, val); n     mx = max(mx, dis[1][u]); n   } n   printf( %d n , ans); n } n int main() { n   int T = 1; n   for (int t = 1; t <= T; t++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); } n double pi = 3.141592653589793238462643; n int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n inline int add(int a, int b, int m = 1000000007) { n   a += b; n   if (a >= m) a -= m; n   return a; n } n inline int mul(int a, int b, int m = 1000000007) { n   return (int)(((long long)a * (long long)b) % m); n } n inline bool ispalin(string &str) { n   int n = str.length(); n   for (int i = 0; i < n / 2; i++) n     if (str[i] != str[n - i - 1]) return false; n   return true; n } n long long expo(long long base, long long pow) { n   long long ans = 1; n   while (pow != 0) { n     if (pow & 1 == 1) { n       ans = ans * base; n       ans = ans % 1000000007; n     } n     base *= base; n     base %= 1000000007; n     pow /= 2; n   } n   return ans; n } n void swapp(int *a, int *b) { n   int temp = *a; n   *a = *b; n   *b = temp; n } n long long invt(long long x) { return expo(x, 1000000007 - 2); } n int expFactor(int n, int p) { n   int x = p; n   int exponent = 0; n   while ((n / x) > 0) { n     exponent += n / x; n     x *= p; n   } n   return exponent; n } n inline int countsetbit(int n) { n   unsigned int count = 0; n   while (n) { n     n &= (n - 1); n     count++; n   } n   return count; n } n inline int abs(int x) { n   if (x < 0) return -x; n   return x; n } n bool isPal(string ss) { n   int len = ss.length(); n   for (int i = 0; i < len / 2; i++) { n     int comp = len - i - 1; n     if (ss[i] != ss[comp]) return false; n   } n   return true; n } n vector<long long> fact, inv; n void calfal(int maxn) { n   fact.resize(maxn); n   inv.resize(maxn); n   fact[0] = inv[0] = inv[1] = 1; n   for (int i = (1); i <= (maxn - 1); ++i) n     fact[i] = i * fact[i - 1] % 1000000007; n   for (int i = (2); i <= (maxn - 1); ++i) n     inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007; n   for (int i = (1); i <= (maxn - 1); ++i) n     inv[i] = inv[i] * inv[i - 1] % 1000000007; n } n long long ncr(int n, int r) { n   if (r > n) return 0; n   return (fact[n] * inv[r] % 1000000007) * inv[n - r] % 1000000007; n } n long long _sieve_size; n bitset<10000010> bs; n vector<int> primes; n void sieve(long long upperbound) { n   _sieve_size = upperbound + 1; n   bs.set(); n   bs[0] = bs[1] = 0; n   for (long long i = 2; i <= _sieve_size; i++) n     if (bs[i]) { n       for (long long j = i * i; j <= _sieve_size; j += i) bs[j] = 0; n       primes.push_back((int)i); n     } n } n bool isPrime(long long N) { n   if (N <= _sieve_size) return bs[N]; n   for (int i = 0; i < (int)primes.size(); i++) n     if (N % primes[i] == 0) return false; n   return true; n } n double dpw[1001][1001][2], dpl1[1001][1001][2], dp[1001][1001][2]; n int w, b; n double fun(int turn, int ww, int bb) { n   if (ww >= w) { n     return 0; n   } n   if (bb > b) { n     return 0; n   } n   if (turn == 1 && bb >= b) { n     return 0; n   } n   if (dp[ww][bb][turn] != -1) { n     return dp[ww][bb][turn]; n   } n   if (turn) { n     double x, y; n     x = (w - ww) * 1.0 / (b - bb - 1 + w - ww); n     y = ((b - bb - 1) * 1.0 / (b - bb - 1 + w - ww)); n     if (bb == b - 1) { n       y = 0; n     } n     dp[ww][bb][turn] = (y * fun((turn + 1) % 2, ww, bb + 2) + n                         x * fun((turn + 1) % 2, ww + 1, bb + 1)) * n                        ((b - bb) * 1.0 / (b - bb + w - ww)); n   } else { n     dp[ww][bb][turn] = n         (w - ww) * 1.0 / (b - bb + w - ww) + n         fun((turn + 1) % 2, ww, bb + 1) * ((b - bb) * 1.0 / (b - bb + w - ww)); n   } n   return dp[ww][bb][turn]; n } n int main() { n   cin >> w >> b; n   if (!w) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = (0); i <= (1); ++i) { n     for (int j = (0); j <= (w + 1); ++j) { n       for (int k = (0); k <= (b + 1); ++k) { n         dp[j][k][i] = -1; n       } n     } n   } n   printf( %.10f , fun(0, 0, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int n; n int b[MAXN]; n set<pair<int, int>, greater<pair<int, int> > > s; n int ans[MAXN]; n int main() { n   scanf( %d , &n); n   for (auto i = 0; i != n; i++) scanf( %d , &b[i]); n   for (auto i = 0; i != n; i++) { n     int j = (i + 1) % n; n     int delta = abs(n - b[i] - b[j]); n     s.insert(make_pair(delta, i)); n   } n   int l = 0, r = n - 1; n   while (!s.empty()) { n     int pos = s.begin()->second; n     s.erase(s.begin()); n     int j = (pos + 1) % n; n     if (n - b[pos] > b[j]) { n       ans[pos] = l++; n     } else { n       ans[pos] = r--; n     } n   } n   for (auto i = 0; i != n; i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N = 0; n   cin >> N; n   for (int i = 0; i < N; i++) { n     int length, height; n     cin >> length >> height; n     int pixL, pixH; n     cin >> pixL >> pixH; n     int ans = max(max(height - pixH - 1, pixH) * length, n                   max(length - pixL - 1, pixL) * height); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > p, pt; n int n, m; n void getanswer(int l, int r) { n   if (l >= r) return; n   getanswer(l, (l + r) / 2); n   getanswer(1 + ((l + r) / 2), r); n   for (int i = int(l); i <= int(r); ++i) { n     m++; n     pt.push_back(make_pair(p[(l + r) / 2].first, p[i].second)); n   } n } n int main() { n   scanf( %I32d , &n); n   p.resize(n); n   pt.resize(n); n   for (int i = 0; i < int(n); i++) n     scanf( %I32d%I32d , &p[i].first, &p[i].second); n   stable_sort(p.begin(), p.end()); n   for (int i = 0; i < int(n); i++) pt[i] = p[i]; n   m = n; n   getanswer(0, n - 1); n   stable_sort(pt.begin(), pt.end()); n   p.clear(); n   for (int i = 0; i < int(m); i++) n     if ((i == 0) || (pt[i] != pt[i - 1])) p.push_back(pt[i]); n   printf( %I32d n , (int)p.size()); n   for (int i = 0; i < int(p.size()); i++) n     printf( %I32d %I32d n , p[i].first, p[i].second); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 8005; n const int inf = 0x3f3f3f3f; n const long long mod = 998244353; n using namespace std; n long long vis[111]; n int main() { n   long long x = 1ll, y; n   int n, q; n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &y); n     for (int j = 0; j <= 33; ++j) { n       if ((x << j) == y) { n         ++vis[j]; n         break; n       } n     } n   } n   for (int i = 1; i <= q; ++i) { n     scanf( %lld , &y); n     long long cnt = 0; n     for (int j = 33; j >= 0; --j) { n       cnt += min(y / (x << j), vis[j]); n       y -= (x << j) * min(y / (x << j), vis[j]); n     } n     if (y) cnt = -1; n     printf( %lld n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   char sym[] = { A ,  O ,  M ,  H ,  I ,  o ,  T ,  V , n                  W ,  U ,  X ,  Y ,  x ,  w ,  v }; n   map<char, char> mp; n   mp[ b ] =  d ; n   mp[ d ] =  b ; n   mp[ p ] =  q ; n   mp[ q ] =  p ; n   string s; n   cin >> s; n   int n = s.length(); n   int a = n / 2 - 1, b; n   if (n & 1) n     b = n / 2 + 1; n   else n     b = n / 2; n   if (n & 1 && count(sym, sym + 16, s[n / 2]) == 0) { n     cout <<  NIE ; n     return 0; n   } n   for (int i = 0, j = n - 1; i <= a && j >= b; i++, j--) { n     if (s[i] == s[j] && count(sym, sym + 16, s[i]) == 1) { n       continue; n     } n     if (mp[s[i]] == s[j] && mp[s[j]] == s[i]) { n       continue; n     } n     cout <<  NIE ; n     return 0; n   } n   cout <<  TAK ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9 + 7; n const pair<int, int> pinf = pair<int, int>(inf, inf); n int up(int n) { n   int ret = 1; n   while (ret < n) ret <<= 1; n   return ret; n } n template <class T> n class segtree { n   vector<T> arr; n   T min(int l, int r, int cl, int cr, int pos) { n     if (cr <= l || r <= cl) return pinf; n     if (l <= cl && cr <= r) return arr[pos]; n     int ct = (cl + cr) / 2; n     return std::min(min(l, r, cl, ct, 2 * pos), min(l, r, ct, cr, 2 * pos + 1)); n   } n   void pset(int pos, T x) { n     arr[pos] = x; n     while (pos /= 2) arr[pos] = std::min(arr[2 * pos], arr[2 * pos + 1]); n   } n  n  public: n   segtree(int n) : arr(2 * up(n)) {} n   T min(int l, int r) { return min(l, r, 0, arr.size() / 2, 1); } n   void set(int pos, T x) { pset(pos + arr.size() / 2, x); } n }; n class sumtree { n   vector<int> arr; n   int sum(int l, int r, int cl, int cr, int pos) { n     if (cr <= l || r <= cl) return 0; n     if (l <= cl && cr <= r) return arr[pos]; n     int ct = (cl + cr) / 2; n     return sum(l, r, cl, ct, 2 * pos) + sum(l, r, ct, cr, 2 * pos + 1); n   } n   void pset(int pos, int x) { n     arr[pos] = x; n     while (pos /= 2) arr[pos] = arr[2 * pos] + arr[2 * pos + 1]; n   } n  n  public: n   sumtree(int n) : arr(2 * up(n)) {} n   int sum(int l, int r) { return sum(l, r, 0, arr.size() / 2, 1); } n   void set(int p, int x) { pset(p + arr.size() / 2, x); } n }; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> arr(n); n   for (auto &it : arr) cin >> it; n   segtree<pair<int, int>> st(n); n   sumtree cards(n); n   for (int i = 0; i < arr.size(); i++) st.set(i, {arr[i], i}); n   long long cnt = 0; n   int pos = 0; n   sort(arr.begin(), arr.end()); n   for (int i = 0; i < n; i++) { n     auto q = st.min(pos, n); n     if (q.first == arr[i]) { n       cnt += q.second - pos - cards.sum(pos, q.second); n       pos = q.second; n       st.set(pos, pinf); n       cards.set(pos, 1); n     } else { n       cnt += n - pos - cards.sum(pos, n); n       i--; n       pos = 0; n     } n   } n   cout << cnt + n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int da[550]; n int dp[550][1050]; n int dp2[550][550]; n struct kutija { n   int in, out, w, s, v; n } K[505]; n bool cmp(const kutija &a, const kutija &b) { n   if (a.in != b.in) return a.in < b.in; n   if (a.out != b.out) return a.out > b.out; n   return a.s > b.s; n } n int rek(int x, int g) { n   if (g < 0) return -1e9; n   if (x == n + 1) return 0; n   if (dp[x][g] != -1) return dp[x][g]; n   memset(dp2[x], 0, sizeof dp2[x]); n   for (int i = x + 1; i <= n; i++) { n     dp2[x][i + 1] = max(dp2[x][i + 1], dp2[x][i]); n     if (K[i].in >= K[x].out || K[i].out > K[x].out) continue; n     dp2[x][da[i]] = max(dp2[x][da[i]], n                         dp2[x][i] + K[i].v + rek(i, min(K[i].s, g - K[i].w))); n   } n   dp[x][g] = dp2[x][n + 1]; n   return dp2[x][n + 1]; n } n int main(void) { n   scanf( %d%d , &n, &K[0].s); n   K[0].in = 0; n   K[0].out = 2 * n; n   K[0].w = 0; n   K[0].v = 0; n   for (int i = 1; i <= n; i++) n     scanf( %d%d%d%d%d , &K[i].in, &K[i].out, &K[i].w, &K[i].s, &K[i].v); n   sort(K, K + n + 1, cmp); n   memset(dp, -1, sizeof dp); n   for (int i = 0; i <= n; i++) { n     da[i] = n + 1; n     for (int j = 0; j < n + 1; j++) { n       if (K[i].out <= K[j].in) { n         da[i] = j; n         break; n       } n     } n   } n   printf( %d , rek(0, K[0].s)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[5][8]; n int vis[123124]; n struct Mp { n   int a[30]; n   Mp() { memset(a, 0, sizeof(a)); } n   bool operator<(const Mp &x) const { n     for (int i = 0; i < 25; ++i) n       if (x.a[i] != a[i]) return a[i] < x.a[i]; n     return 0; n   } n   void swap(int &a, int &b, int &c, int &d) { n     int t = a; n     a = b; n     b = c; n     c = d; n     d = t; n   } n   Mp Rotate1() { n     Mp res = *this; n     swap(res.a[1], res.a[3], res.a[4], res.a[2]); n     swap(res.a[13], res.a[5], res.a[17], res.a[21]); n     swap(res.a[14], res.a[6], res.a[18], res.a[22]); n     swap(res.a[15], res.a[7], res.a[19], res.a[23]); n     swap(res.a[16], res.a[8], res.a[20], res.a[24]); n     swap(res.a[9], res.a[10], res.a[12], res.a[11]); n     return res; n   } n   Mp Rotate2() { n     Mp res = *this; n     swap(res.a[1], res.a[3], res.a[4], res.a[2]); n     swap(res.a[13], res.a[5], res.a[17], res.a[21]); n     swap(res.a[14], res.a[6], res.a[18], res.a[22]); n     return res; n   } n   Mp Rotate3() { n     Mp res = *this; n     swap(res.a[2], res.a[23], res.a[10], res.a[6]); n     swap(res.a[4], res.a[21], res.a[12], res.a[8]); n     swap(res.a[17], res.a[18], res.a[20], res.a[19]); n     return res; n   } n   bool check() { n     for (int i = 1; i <= 24; i += 4) { n       for (int j = i + 1; j <= i + 3; ++j) n         if (a[j] != a[i]) return 0; n     } n     return 1; n   } n   void OUT() { n     for (int i = 1; i <= 24; ++i) cout << a[i] <<    ; n     cout << endl; n   } n }; n map<Mp, bool> hs; n int main() { n   Mp init; n   for (int i = 1; i <= 24; ++i) cin >> init.a[i]; n   queue<Mp> Q; n   Q.push(init); n   while (!Q.empty()) { n     Mp now = Q.front(); n     Q.pop(); n     Mp news; n     news = now.Rotate1(); n     if (!hs[news]) { n       hs[news] = 1; n       Q.push(news); n     } n     news = now.Rotate2(); n     if (news.check()) { n       puts( yes ); n       return 0; n     } n     news = now.Rotate2().Rotate2().Rotate2(); n     if (news.check()) { n       puts( yes ); n       return 0; n     } n     news = now.Rotate3(); n     if (news.check()) { n       puts( yes ); n       return 0; n     } n     news = now.Rotate3().Rotate3().Rotate3(); n     if (news.check()) { n       puts( yes ); n       return 0; n     } n   } n   puts( no ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1011; n int n, m; n char a[N][N]; n int b[N][N], ts; n const int dx[] = {-1, -1, 0, 0, 1, 1}; n const int dy[] = {1, -1, 2, -2, 1, -1}; n void output() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       int x = i, y = j; n       if (a[i][j] ==  . ) { n         printf( . ); n         continue; n       } n       if (a[i][j] ==  < ) n         ++y; n       else if (a[i][j] ==  > ) n         --y; n       else if (a[i][j] ==  A ) n         ++x; n       else if (a[i][j] ==  V ) n         --x; n       else if (a[i][j] !=  -  && a[i][j] !=  | ) { n         printf( %c , a[i][j]); n         continue; n       } n       char k =  a ; n       if (x % 2 == 1) k =  c ; n       if (x / 2 % 2 != y / 2 % 2) ++k; n       putchar(k); n     } n     puts(  ); n   } n } n bool dfs(int x, int y) { n   b[x][y] = ts; n   if (x > 0 && x < n - 1 && a[x - 1][y] ==  w  && a[x + 1][y] ==  w ) { n     a[x - 1][y] =  A ; n     a[x][y] =  | ; n     a[x + 1][y] =  V ; n     int nc = 0; n     for (int d = 0; d < 6; ++d) { n       int nx = x + dy[d]; n       int ny = y + dx[d]; n       if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] ==  b ) { n         ++nc; n         if (dfs(nx, ny)) n           return true; n         else n           break; n       } n     } n     if (!nc) return true; n     a[x - 1][y] =  w ; n     a[x][y] =  b ; n     a[x + 1][y] =  w ; n   } n   if (y > 0 && y < m - 1 && a[x][y - 1] ==  w  && a[x][y + 1] ==  w ) { n     a[x][y - 1] =  < ; n     a[x][y] =  - ; n     a[x][y + 1] =  > ; n     int nc = 0; n     for (int d = 0; d < 6; ++d) { n       int nx = x + dx[d]; n       int ny = y + dy[d]; n       if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] ==  b ) { n         ++nc; n         if (dfs(nx, ny)) n           return true; n         else n           break; n       } n     } n     if (!nc) return true; n     a[x][y - 1] =  w ; n     a[x][y] =  b ; n     a[x][y + 1] =  w ; n   } n   return false; n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %s , a[i]); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       if (a[i][j] ==  b ) { n         ts = i * m + j + 1; n         if (!dfs(i, j)) { n           puts( NO ); n           return 0; n         } n       } n     } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (a[i][j] ==  b  || a[i][j] ==  w ) { n         puts( NO ); n         return 0; n       } n   puts( YES ); n   output(); n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long a[n], b[n], c[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) cin >> b[i]; n   for (long long i = 0; i < n; i++) { n     c[i] = (a[i] - b[i]); n   } n   sort(c, c + n); n   long long answer = 0, answer1 = 0; n   for (long long i = 0; i < n; i++) { n     long long ans = a[i] - b[i]; n     long long sum = 1 - ans; n     if (ans > 0) answer1++; n     long long ss = lower_bound(c, c + n, sum) - c; n     answer += (n - ss); n   } n   answer -= answer1; n   answer /= 2; n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int oo = 2139063143; n const int N = 101000; n const int P = 1000000007; n using namespace std; n inline void sc(int &x) { n   x = 0; n   static int p; n   p = 1; n   static char c; n   c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) p = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c - 48); n     c = getchar(); n   } n   x *= p; n } n inline void print(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n inline void pr(int x) { print(x), putchar(  n ); } n int n, k; n int vis[N], cl = 1, cr = 0, ans = 0; n int a[N]; n void add(int w, int d) { n   if (!vis[w] && d == 1) n     ++ans; n   else if (vis[w] == 1 && d == -1) n     --ans; n   vis[w] += d; n } n int calc(int l, int r) { n   while (cr < r) ++cr, add(a[cr], 1); n   while (cl > l) --cl, add(a[cl], 1); n   while (cr > r) add(a[cr], -1), --cr; n   while (cl < l) add(a[cl], -1), ++cl; n   return ans; n } n int f[N], g[N]; n void solve(int wl, int wr, int l, int r) { n   int mid = (l + r) >> 1, pos = 0; n   f[mid] = 0; n   for (int i = wl; i <= min(wr, mid - 1); i++) { n     int sb = g[i] + calc(i + 1, mid); n     if (sb > f[mid]) f[mid] = sb, pos = i; n   } n   if (l == r) return; n   if (l < mid) solve(wl, pos, l, mid - 1); n   if (r > mid) solve(pos, wr, mid + 1, r); n } n int las[N]; n int main() { n   sc(n), sc(k); n   for (int i = 1; i <= n; i++) sc(a[i]); n   for (int i = 1; i <= k; i++) { n     solve(0, n, 1, n); n     memcpy(g, f, sizeof(int) * (n + 1)); n   } n   pr(f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, V; n pair<int, int> a[111111]; n int x[111111], t[111111], b[111111], f[111111]; n int ans1, ans2; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &x[i], &t[i]); n   cin >> V; n   for (int i = 1; i <= n; ++i) n     t[i] *= V, a[i] = make_pair(t[i] + x[i], t[i] - x[i]); n   sort(a + 1, a + 1 + n); n   b[0] = -2000000000; n   for (int i = 1; i <= n; ++i) b[i] = 2000000000; n   for (int i = 1; i <= n; ++i) { n     f[i] = lower_bound(b, b + 1 + n, a[i].second + 1) - b; n     b[f[i]] = min(b[f[i]], a[i].second); n     ans2 = max(ans2, f[i]); n   } n   int fd = 0; n   for (int i = 1; i <= n; ++i) n     if (a[i].first == 0 && a[i].second == 0) fd = i; n   if (!fd) a[++n] = make_pair(0, 0); n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; ++i) n     if (a[i].first == 0 && a[i].second == 0) fd = i; n   for (int i = 0; i <= n; ++i) b[i] = 2000000000; n   b[(ans1 = f[fd] = 0)] = a[fd].second; n   for (int i = fd + 1; i <= n; ++i) { n     if (a[i].second >= 0) { n       f[i] = lower_bound(b, b + 1 + n, a[i].second + 1) - b; n       b[f[i]] = min(b[f[i]], a[i].second); n       ans1 = max(ans1, f[i]); n     } n   } n   printf( %d %d n , ans1, ans2); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans; n int x[100010], y[100010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x[i]); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &y[i]); n   } n   long long sum1 = 0, sum2 = 0; n   for (int r1 = 0, r2 = 0; r1 <= n, r2 <= m;) { n     while (sum1 < sum2 || r1 == 0) { n       r1++; n       sum1 += x[r1]; n     } n     while (sum1 > sum2 || r2 == 0) { n       r2++; n       sum2 += y[r2]; n     } n     if (sum1 == sum2) { n       ans++; n       r1++; n       r2++; n       sum1 = x[r1]; n       sum2 = y[r2]; n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   char ch; n   int arr[5001], brr[5001], mdays[5001], fdays[5001]; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ch >> arr[i] >> brr[i]; n     if (ch ==  M ) { n       for (int j = arr[i]; j <= brr[i]; j++) mdays[j]++; n     } else { n       for (int j = arr[i]; j <= brr[i]; j++) fdays[j]++; n     } n   } n   int mx = 0; n   for (int i = 1; i <= 366; i++) { n     mx = max(mx, min(mdays[i], fdays[i])); n   } n   mx *= 2; n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sq = (int)sqrt((int)1e6); n const int maxnb = (int)1e9 + 7; n const double pi = acos(-1.0); n const int maxnl = 100500; n const double eps = 1e-9; n set<pair<long long, int> > se; n int n, pos[50500]; n char c[50500]; n long long price; n string s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> s; n   int n = s.size(), bal = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ( ) bal++; n     if (s[i] ==  ) ) bal--; n     if (s[i] ==  ? ) { n       long long a, b; n       cin >> a >> b; n       se.insert(make_pair(a - b, i)); n       price += b; n       s[i] =  ) ; n       bal--; n     } n     while (bal < 0) { n       if (!se.size()) { n         cout << -1; n         return 0; n       } n       pair<long long, int> p = *se.begin(); n       price += p.first; n       s[p.second] =  ( ; n       bal += 2; n       se.erase(se.begin()); n     } n   } n   if (bal != 0) { n     cout << -1; n     return 0; n   } n   cout << price <<   n ; n   for (int i = 0; i < n; i++) { n     cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n string s; n int n, len; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> s; n   len = s.size(); n   for (int i = 0; i < len; i++) n     if (s[i] ==  a ) n++; n   if (len / 2 < n) { n     cout << len; n     return 0; n   } n   cout << 2 * n - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, ans(0); n   bool flg = true; n   cin >> n >> m >> k; n   int ar[n * m]; n   for (int a = 0; a < n * m; a++) cin >> ar[a]; n   sort(ar, ar + n * m); n   int median = (n * m % 2) ? ar[n * m / 2] : ar[(n * m + 1) / 2]; n   for (int a = 0; a < n * m; a++) { n     if (abs(median - ar[a]) % k != 0) flg = false; n     ans += abs(median - ar[a]) / k; n   } n   if (flg) n     cout << ans; n   else n     cout << -1; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<string> a(n + 1), b(n + 1); n   vector<int> c(n + 1); n   vector<long long> f1(n + 1), f2(n + 1); n   for (int i = 1; i <= n; ++i) { n     cin >> c[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     b[i] = a[i]; n     reverse(b[i].begin(), b[i].end()); n   } n   for (int i = 1; i <= n; ++i) { n     f1[i] = f2[i] = LLONG_MAX; n     if (f1[i - 1] != LLONG_MAX) { n       if (a[i] >= a[i - 1]) { n         f1[i] = min(f1[i], f1[i - 1]); n       } n       if (b[i] >= a[i - 1]) { n         f2[i] = min(f2[i], f1[i - 1] + c[i]); n       } n     } n     if (f2[i - 1] != LLONG_MAX) { n       if (a[i] >= b[i - 1]) { n         f1[i] = min(f1[i], f2[i - 1]); n       } n       if (b[i] >= b[i - 1]) { n         f2[i] = min(f2[i], f2[i - 1] + c[i]); n       } n     } n   } n   if (f1[n] == LLONG_MAX && f2[n] == LLONG_MAX) { n     cout << -1; n   } else { n     cout << min(f1[n], f2[n]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = 1010; n const int MM = 101010; n struct Node { n   int id; n   int x, y; n } Q[MM]; n int a[NN][NN]; n vector<int> adj[MM]; n int ans[MM], f[MM]; n int n, m, k; n void dfs(int u, int fa = 0, int crr = 0) { n   if (Q[u].id == 1) { n     int x = Q[u].x, y = Q[u].y; n     if (!a[x][y]) { n       a[x][y] = 1; n       crr++; n       f[u] = 1; n     } n   } else if (Q[u].id == 2) { n     int x = Q[u].x, y = Q[u].y; n     if (a[x][y]) { n       a[x][y] = 0; n       crr--; n       f[u] = 1; n     } n   } else if (Q[u].id == 3) { n     int x = Q[u].x; n     for (int i = 1; i <= m; i++) { n       if (a[x][i]) n         crr--; n       else n         crr++; n       a[x][i] ^= 1; n     } n   } n   ans[u] = crr; n   for (int v : adj[u]) { n     if (v == fa) continue; n     dfs(v, u, ans[u]); n   } n   if (Q[u].id == 1) { n     int x = Q[u].x, y = Q[u].y; n     if (f[u]) a[x][y] = 0; n   } else if (Q[u].id == 2) { n     int x = Q[u].x, y = Q[u].y; n     if (f[u]) a[x][y] = 1; n   } else if (Q[u].id == 3) { n     int x = Q[u].x; n     for (int i = 1; i <= m; i++) a[x][i] ^= 1; n   } n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= k; i++) { n     int id, x, y; n     scanf( %d , &id); n     if (id < 3) { n       scanf( %d %d , &x, &y); n       Q[i] = {id, x, y}; n       adj[i - 1].push_back(i); n       adj[i].push_back(i - 1); n       continue; n     } n     scanf( %d , &x); n     if (id == 4) { n       adj[x].push_back(i); n       adj[i].push_back(x); n     } else { n       adj[i - 1].push_back(i); n       adj[i].push_back(i - 1); n     } n     Q[i] = {id, x, 0}; n   } n   dfs(0); n   for (int i = 1; i <= k; i++) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n vector<int> gr[300001]; n int par[300001], comp[300001]; n int mx[300001][2]; n int dfs(int s, int k) { n   comp[s] = k; n   mx[s][0] = mx[s][1] = 0; n   for (int i = 0; i < gr[s].size(); i++) { n     if (comp[gr[s][i]] == -1) { n       int p = dfs(gr[s][i], k) + 1; n       par[gr[s][i]] = s; n       if (p > mx[s][0]) { n         mx[s][1] = mx[s][0]; n         mx[s][0] = p; n       } else if (p > mx[s][1]) n         mx[s][1] = p; n     } n   } n   return mx[s][0]; n } n int mark[300001]; n void dfs2(int s, int d) { n   mark[s] = 1; n   if (d > mx[s][0]) { n     mx[s][1] = mx[s][0]; n     mx[s][0] = d; n   } else if (d > mx[s][1]) n     mx[s][1] = d; n   for (int i = 0; i < gr[s].size(); i++) { n     if (!mark[gr[s][i]]) { n       if (mx[s][0] == (mx[gr[s][i]][0] + 1)) { n         ; n         dfs2(gr[s][i], mx[s][1] + 1); n       } else n         dfs2(gr[s][i], mx[s][0] + 1); n     } n   } n } n int mlen[300001]; n int mpar[300001]; n int get(int id) { n   if (par[id] == id) return id; n   return par[id] = get(par[id]); n } n void merge(int x, int y) { par[x] = get(par[y]); } n int main() { n   cin >> n >> m >> q; n   int a, b; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     gr[a - 1].push_back(b - 1); n     gr[b - 1].push_back(a - 1); n   } n   memset(comp, -1, sizeof comp); n   int c = 0; n   for (int i = 0; i < n; i++) { n     if (comp[i] == -1) { n       dfs(i, i); n     } n   } n   memset(mark, 0, sizeof mark); n   for (int i = 0; i < n; i++) n     if (!mark[i]) dfs2(i, 0); n   memset(mlen, -1, sizeof mlen); n   for (int i = 0; i < n; i++) { n     mlen[i] = mx[i][0] + mx[i][1]; n     mlen[comp[i]] = max(mlen[i], mlen[comp[i]]); n     mpar[i] = mpar[comp[i]]; n     if (i == comp[i]) n       mpar[i] = i; n     else if (mx[i][0] < mx[mpar[i]][0]) n       mpar[comp[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     mlen[i] = mlen[comp[i]]; n     mpar[i] = mpar[comp[i]]; n     par[i] = mpar[i]; n   } n   for (int i = 0; i < q; i++) { n     scanf( %d , &c); n     if (c == 1) { n       scanf( %d , &a); n       printf( %d n , mlen[get(a - 1)]); n     } else { n       scanf( %d%d , &a, &b); n       a = get(a - 1); n       b = get(b - 1); n       if (a != b) { n         if (mx[a][0] >= mx[b][0]) { n           merge(b, a); n           mlen[a] = max(mlen[a], mx[a][0] + mx[b][0] + 1); n           mx[a][0] = max(mx[a][0], mx[b][0] + 1); n         } else { n           merge(a, b); n           mlen[b] = max(mlen[b], mx[b][0] + mx[a][0] + 1); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int p = 1000000007; n int c[12][15], e[12][15], g[12][5000], h[12][5000]; n int edge1[3000], next1[3000], first1[3000], deg1[3000], sum_edge1; n int edge2[30], next2[30], first2[30], deg2[30], sum_edge2; n int i, n1, n2, r, s, t, x, y; n inline int power(int x, int y) { n   int s = 1; n   for (int i = 30; i >= 0; i--) { n     s = 1LL * s * s % p; n     if (y & (1 << i)) s = 1LL * s * x % p; n   } n   return s; n } n inline void addedge1(int x, int y) { n   sum_edge1++, edge1[sum_edge1] = y, next1[sum_edge1] = first1[x], n                first1[x] = sum_edge1, deg1[x]++; n   return; n } n inline void addedge2(int x, int y) { n   sum_edge2++, edge2[sum_edge2] = y, next2[sum_edge2] = first2[x], n                first2[x] = sum_edge2, deg2[x]++; n   return; n } n inline int dfs1(int d, int x, int fx, int y, int fy) { n   if (deg1[x] < deg2[y]) return 0; n   int l = 0; n   for (int j = 1; j <= n2; j++) e[d][j] = 0; n   for (int j = first2[y]; j != 0; j = next2[j]) n     if (edge2[j] != fy) l++, e[d][edge2[j]] = l; n   for (int i = 0; i < (1 << l); i++) g[d][i] = 0; n   g[d][0] = 1; n   for (int i = first1[x]; i != 0; i = next1[i]) n     if (edge1[i] != fx) { n       for (int j = 0; j < (1 << l); j++) h[d][j] = g[d][j]; n       for (int j = first2[y]; j != 0; j = next2[j]) n         if (edge2[j] != fy) n           c[d][e[d][edge2[j]]] = dfs1(d + 1, edge1[i], x, edge2[j], y); n       for (int k = 1; k <= l; k++) n         if (c[d][k]) n           for (int j = 0; j < (1 << l); j++) n             if (!(j & (1 << (k - 1)))) n               g[d][j | (1 << (k - 1))] = n                   (g[d][j | (1 << (k - 1))] + 1LL * h[d][j] * c[d][k]) % p; n     } n   return g[d][(1 << l) - 1]; n } n inline int dfs2(int d, int x, int fx, int y, int fy) { n   if (deg2[x] < deg2[y]) return 0; n   int l = 0; n   for (int j = 1; j <= n2; j++) e[d][j] = 0; n   for (int j = first2[y]; j != 0; j = next2[j]) n     if (edge2[j] != fy) l++, e[d][edge2[j]] = l; n   for (int i = 0; i < (1 << l); i++) g[d][i] = 0; n   g[d][0] = 1; n   for (int i = first2[x]; i != 0; i = next2[i]) n     if (edge2[i] != fx) { n       for (int j = 0; j < (1 << l); j++) h[d][j] = g[d][j]; n       for (int j = first2[y]; j != 0; j = next2[j]) n         if (edge2[j] != fy) n           c[d][e[d][edge2[j]]] = dfs2(d + 1, edge2[i], x, edge2[j], y); n       for (int k = 1; k <= l; k++) n         if (c[d][k]) n           for (int j = 0; j < (1 << l); j++) n             if (!(j & (1 << (k - 1)))) n               g[d][j | (1 << (k - 1))] = n                   (g[d][j | (1 << (k - 1))] + 1LL * h[d][j] * c[d][k]) % p; n     } n   return g[d][(1 << l) - 1]; n } n inline void input() { n   scanf( %d , &n1); n   for (i = 1; i < n1; i++) { n     scanf( %d%d , &x, &y); n     addedge1(x, y), addedge1(y, x); n   } n   scanf( %d , &n2); n   for (i = 1; i < n2; i++) { n     scanf( %d%d , &x, &y); n     addedge2(x, y), addedge2(y, x); n   } n   for (i = 1; i <= n2; i++) n     if (deg2[i] > deg2[r]) r = i; n   return; n } n int main() { n   input(); n   for (i = 1; i <= n1; i++) s = (s + dfs1(0, i, 0, r, 0)) % p; n   for (i = 1; i <= n2; i++) t = t + dfs2(0, i, 0, r, 0); n   printf( %d , 1LL * s * power(t, p - 2) % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int i, n, d, e; n   cin >> n >> d >> e; n   e *= 5; n   int min = n % e; n   for (i = 0; i <= n / d; i++) { n     if ((n - (i * d)) % e < min) min = (n - (i * d)) % e; n   } n   if ((n % e) % d < min) min = (n % e) % d; n   cout << min; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 30; n struct edge { n   int to, next; n } G[maxn << 1]; n int h[maxn], si; n void add(int u, int v) { n   G[si].to = v; n   G[si].next = h[u]; n   h[u] = si++; n } n vector<int> p; n void dfs(int u, int f, int d) { n   int is = 0; n   for (int i = h[u]; ~i; i = G[i].next) { n     int v = G[i].to; n     if (v ^ f) { n       dfs(v, u, d + 1); n       is = 1; n     } n   } n   if (!is) p.push_back(d); n } n int solve(int u) { n   p.clear(); n   dfs(u, 1, 1); n   int cur = -1; n   sort(p.begin(), p.end()); n   for (int i = 0; i < p.size(); i++) { n     if (cur < p[i]) n       cur = p[i]; n     else n       cur++; n   } n   return cur; n } n int main() { n   int n; n   while (~scanf( %d , &n)) { n     memset(h, -1, sizeof h); n     si = 0; n     int u, v; n     for (int i = 0; i < n - 1; i++) { n       scanf( %d%d , &u, &v); n       add(u, v); n       add(v, u); n     } n     int ans = 0; n     for (int i = h[1]; ~i; i = G[i].next) ans = max(ans, solve(G[i].to)); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int K[3]; n long long T[3]; n queue<long long> waiting[3]; n queue<pair<long long, long long> > events[3]; n long long res = 0; n void ServeSomeone(long long t) { n   for (int i = 0; i < 3; i++) n     if (K[i] && !waiting[i].empty()) { n       K[i]--; n       events[i].push(make_pair(waiting[i].front(), t + T[i])); n       waiting[i].pop(); n     } n } n void ProcessEvents(long long max_time) { n   while (true) { n     int mini = -1; n     for (int i = 0; i < 3; i++) n       if (!events[i].empty() && (mini == -1 || events[i].front().second < n                                                    events[mini].front().second)) n         mini = i; n     if (mini == -1) break; n     if (events[mini].front().second > max_time) break; n     pair<long long, long long> m = events[mini].front(); n     events[mini].pop(); n     K[mini]++; n     if (mini == 2) { n       res = max(res, m.second - m.first); n     } else { n       waiting[mini + 1].push(m.first); n     } n     ServeSomeone(m.second); n   } n } n int main() { n   cin >> K[0] >> K[1] >> K[2]; n   cin >> T[0] >> T[1] >> T[2]; n   int N; n   cin >> N; n   for (int i = 0; i < N; i++) { n     long long next_arrival; n     cin >> next_arrival; n     ProcessEvents(next_arrival); n     waiting[0].push(next_arrival); n     ServeSomeone(next_arrival); n   } n   ProcessEvents(1000000000000000000LL); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int trie[64 * 100005][2], uni; n inline long long int bin_to_int(vector<long long int> vt) { n   long long int ans = 0, hudai = 1; n   while (vt.size()) { n     ans += (hudai * vt[vt.size() - 1]); n     hudai *= 2; n     vt.pop_back(); n   } n   return ans; n } n inline vector<long long int> int_to_bin(long long int n) { n   vector<long long int> ans(64), tem; n   while (n > 0) { n     tem.push_back(n % 2); n     n /= 2; n   } n   for (int i = 0; i < (tem.size()); i++) { n     ans[63 - i] = tem[i]; n   } n   return ans; n } n inline long long int search(vector<long long int> vt) { n   int cur = 1; n   vector<long long int> ans; n   for (int i = 0; i < (64); i++) { n     int id = (vt[i] + 1) % 2; n     if (trie[cur][id]) { n       ans.push_back(1); n       cur = trie[cur][id]; n     } else { n       ans.push_back(0); n       cur = trie[cur][vt[i]]; n     } n   } n   return bin_to_int(ans); n } n inline void insert(vector<long long int> vt) { n   int cur = 1; n   for (int i = 0; i < (64); i++) { n     int id = vt[i]; n     if (!trie[cur][id]) trie[cur][id] = ++uni; n     cur = trie[cur][id]; n   } n   return; n } n int main() { n   int n; n   scanf( %d , &n); n   vector<long long int> vt; n   uni = 1; n   for (int i = 0; i < (n); i++) { n     long long int xx; n     scanf( %lld , &xx); n     vt.push_back(xx); n   } n   vector<long long int> hudai; n   hudai.push_back(0); n   vt.push_back(0); n   for (int i = 0; i < (n); i++) n     hudai.push_back(hudai[hudai.size() - 1] ^ vt[i]); n   long long int maximum = 0; n   long long int xr = 0; n   for (int i = hudai.size() - 1; i >= 0; i--) { n     long long int xx = hudai[i]; n     xr = xr ^ vt[i]; n     vector<long long int> tem = int_to_bin(xr); n     insert(tem); n     tem = int_to_bin(xx); n     maximum = max(maximum, search(tem)); n   } n   printf( %lld n , maximum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, st, dr, mij, r, l12, l11, c12, c11; n int l21, l22, c21, c22; n int nr; n void getldrept(int li, int ls, int ci, int cs) { n   int st, dr; n   int l1, c1, l2, c2; n   st = li; n   dr = ls; n   while (st < dr) { n     mij = (st + dr) / 2; n     cout <<  ?   << li <<     << ci <<     << mij <<     << cs <<   n ; n     cin >> r; n     if (r) n       dr = mij; n     else n       st = mij + 1; n   } n   l2 = st; n   st = li; n   dr = l2; n   while (st < dr) { n     mij = (st + dr + 1) / 2; n     cout <<  ?   << mij <<     << ci <<     << l2 <<     << cs <<   n ; n     cin >> r; n     if (r) n       st = mij; n     else n       dr = mij - 1; n   } n   l1 = st; n   st = ci; n   dr = cs; n   while (st < dr) { n     mij = (st + dr) / 2; n     cout <<  ?   << l1 <<     << ci <<     << l2 <<     << mij <<   n ; n     cin >> r; n     if (r) n       dr = mij; n     else n       st = mij + 1; n   } n   c2 = st; n   st = ci; n   dr = c2; n   while (st < dr) { n     mij = (st + dr + 1) / 2; n     cout <<  ?   << l1 <<     << mij <<     << l2 <<     << c2 <<   n ; n     cin >> r; n     if (r) n       st = mij; n     else n       dr = mij - 1; n   } n   c1 = st; n   if (nr == 1) { n     l11 = l1; n     l12 = l2; n     c11 = c1; n     c12 = c2; n   } else { n     l21 = l1; n     l22 = l2; n     c21 = c1; n     c22 = c2; n   } n } n int main() { n   cin >> n; n   nr = 1; n   getldrept(1, n, 1, n); n   nr = 2; n   if (l11 > 1) { n     cout <<  ?   << 1 <<     << 1 <<     << l11 - 1 <<     << n <<   n ; n     cin >> r; n     if (r) { n       getldrept(1, l11 - 1, 1, n); n       cout <<  !   << l11 <<     << c11 <<     << l12 <<     << c12; n       cout <<     << l21 <<     << c21 <<     << l22 <<     << c22; n       return 0; n     } n   } n   if (l12 < n) { n     cout <<  ?   << l12 + 1 <<     << 1 <<     << n <<     << n <<   n ; n     cin >> r; n     if (r) { n       getldrept(l12 + 1, n, 1, n); n       cout <<  !   << l11 <<     << c11 <<     << l12 <<     << c12; n       cout <<     << l21 <<     << c21 <<     << l22 <<     << c22; n       return 0; n     } n   } n   if (c11 > 1) { n     cout <<  ?   << 1 <<     << 1 <<     << n <<     << c11 - 1 <<   n ; n     cin >> r; n     if (r) { n       getldrept(1, n, 1, c11 - 1); n       cout <<  !   << l11 <<     << c11 <<     << l12 <<     << c12; n       cout <<     << l21 <<     << c21 <<     << l22 <<     << c22; n       return 0; n     } n   } n   if (c12 < n) { n     cout <<  ?   << 1 <<     << c12 + 1 <<     << n <<     << n <<   n ; n     cin >> r; n     if (r) { n       getldrept(1, n, c12 + 1, n); n       cout <<  !   << l11 <<     << c11 <<     << l12 <<     << c12; n       cout <<     << l21 <<     << c21 <<     << l22 <<     << c22; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int rev(int a) { n   bitset<8> bs(a); n   string s = bs.to_string(); n   reverse((s).begin(), (s).end()); n   return bitset<32>(s).to_ulong(); n } n int main() { n   string line; n   getline(cin, line); n   for (int i = 0; i < (int)(line.length()); i++) { n     int p = i == 0 ? 0 : line[i - 1]; n     int c = line[i]; n     int x = (rev(p) - rev(c) + 256) % 256; n     ; n     printf( %d n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx ) n using namespace std; n template <class U, class V> n istream& operator>>(istream& is, pair<U, V>& p) { n   is >> p.first >> p.second; n   return is; n } n template <class T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& x : v) { n     is >> x; n   } n   return is; n } n template <class T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto& x : v) { n     os << x <<    ; n   } n   return os; n } n const int N = 2e5 + 5; n vector<int> adj[N]; n vector<int> parents[N]; n int cnt_parents[N]; n bool removed[N]; n int n; n void init() { n   for (int i = 1; i <= n; ++i) { n     adj[i].clear(); n     parents[i].clear(); n     cnt_parents[i] = 0; n     removed[i] = false; n   } n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n vector<int> solve() { n   static vector<int> res; n   static vector<int> perm; n   perm.clear(); n   res.clear(); n   for (int i = 1; i <= n; ++i) { n     removed[i] = false; n     perm.emplace_back(i); n   } n   shuffle(begin(perm), end(perm), rng); n   for (int i : perm) { n     if (removed[i]) continue; n     for (int child : adj[i]) { n       if (removed[child]) continue; n       for (int grand : adj[child]) { n         if (removed[grand]) continue; n         res.emplace_back(grand); n         removed[grand] = true; n       } n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   vector<int> res; n   while (t--) { n     res.clear(); n     int m; n     cin >> n >> m; n     init(); n     while (m--) { n       int u, v; n       cin >> u >> v; n       adj[u].emplace_back(v); n       ++cnt_parents[v]; n       parents[v].emplace_back(u); n     } n     for (int i = 1; i <= n; ++i) { n       bool to_remove = false; n       for (int parent : parents[i]) { n         if (cnt_parents[parent] == 0 || removed[parent]) continue; n         to_remove = true; n         break; n       } n       if (!to_remove) continue; n       res.emplace_back(i); n       removed[i] = true; n       for (int v : adj[i]) { n         --cnt_parents[v]; n       } n     } n     for (int i = 0; i < 30; ++i) { n       auto tmp = solve(); n       if (tmp.size() < res.size()) { n         swap(res, tmp); n       } n     } n     cout << res.size() <<   n ; n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pr(int n) { n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return 0; n   return 1; n } n int near(int n) { n   int i = 0; n   for (i = n - 2; i >= 2; i--) { n     if (pr(i)) break; n   } n   return i; n } n int main() { n   int i, n, s1, j = 0, s2 = 0, counter = 0, x = 0; n   int ara[10] = {0}; n   cin >> n; n   if (pr(n)) { n     cout << 1 << endl; n     cout << n << endl; n     return 0; n   } else { n     s1 = near(n); n     ara[0] = s1; n     j++; n     s2 = n - s1; n     if (pr(s2) == 1) { n       printf( 2 n ); n       printf( %d %d , s1, s2); n       return 0; n     } n     x = near(s2); n     while (pr(s2 - x) == 0) { n       x = near(x - 1); n     } n     printf( 3 n ); n     printf( %d %d %d , s1, x, n - s1 - x); n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long n, ans = 0, a[30], cur, deg = 20, countOfPair = 1; n   cin >> n; n   memset(a, 0, sizeof(unsigned long long) * 30); n   for (int i = 0; i < n; ++i) { n     cin >> cur; n     ++a[cur + 10]; n   } n   for (int i = 0; i < 10; ++i) { n     ans += a[i] * a[deg + i]; n     deg -= 2; n   } n   if (a[10] != 0 && a[10] != 1) { n     if (a[10] == 2) n       ans += 1; n     else { n       for (unsigned int i = 3; i <= a[10]; ++i) { n         countOfPair = countOfPair + i - 1; n       } n       ans += countOfPair; n     } n   } n   cout << ans; n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   map<int, int> mp; n   for (int i = 0; i < n; i++) { n     int curr; n     cin >> curr; n     mp[curr]++; n   } n   vector<int> nums(mp.size()); n   int counter = 0; n   vector<pair<int, int> > sorted(mp.size()); n   for (auto i : mp) { n     sorted[counter].first = i.second; n     sorted[counter].second = i.first; n     nums[counter] = i.second; n     counter++; n   } n   sort(sorted.begin(), sorted.end()); n   sort(nums.begin(), nums.end()); n   vector<int> sums(nums.size()); n   sums[nums.size() - 1] = nums.back(); n   for (int i = sums.size() - 2; i >= 0; i--) { n     sums[i] = sums[i + 1] + nums[i]; n   } n   int sqr = (int)sqrt(n); n   int mx = 0; n   pair<int, int> dimensions; n   for (int i = sqr; i > 0; i--) { n     int low = 0; n     int high = nums.size() - 1; n     while (low <= high) { n       int mid = (low + high) >> 1; n       if (nums[mid] <= i) { n         low = mid + 1; n       } else { n         high = mid - 1; n       } n     } n     int res = n; n     if (i < nums.back()) { n       res -= sums[low] - (nums.size() - low) * i; n     } n     low = i; n     high = n; n     if (res < i * i) { n       continue; n     } n     while (low <= high) { n       int mid = (low + high) >> 1; n       if (mid * i < res) { n         low = mid + 1; n       } else if (mid * i > res) { n         high = mid - 1; n       } else { n         high = mid; n         break; n       } n     } n     if (mx < i * high) { n       mx = i * high; n       dimensions.first = i; n       dimensions.second = high; n     } n   } n   cout << mx <<   n ; n   cout << dimensions.first <<     << dimensions.second <<   n ; n   int arr[dimensions.first][dimensions.second]; n   auto itr = sorted.rbegin(); n   counter = min((*itr).first, dimensions.first); n   for (int i = 0; i < dimensions.second; i++) { n     int start = i; n     for (int a = 0; a < dimensions.first; a++) { n       if (counter == 0) { n         itr++; n         counter = min((*itr).first, dimensions.first); n       } n       arr[a][start] = (*itr).second; n       counter--; n       start = (start + 1) % dimensions.second; n     } n   } n   for (int i = 0; i < dimensions.first; i++) { n     for (int a = 0; a < dimensions.second - 1; a++) { n       cout << arr[i][a] <<    ; n     } n     cout << arr[i][dimensions.second - 1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int u; n   int v; n   Edge() {} n   Edge(int u, int v) : u(u), v(v) {} n }; n struct WeightedEdge : Edge { n   int w; n }; n struct EdgeGroup { n   int q; n   vector<Edge> edges; n   EdgeGroup() { q = -1; } n   EdgeGroup(int q) : q(q) {} n }; n const int MAX_M = 500005; n const int MAX_W = 500005; n const int MAX_N = 500005; n const int MAX_Q = 500005; n WeightedEdge raw_edge[MAX_M]; n int vertex_set[MAX_N]; n int cycle_set[MAX_N]; n vector<Edge> edge[MAX_W]; n vector<EdgeGroup> edge_group[MAX_W]; n bool ans[MAX_Q]; n int get_canonical(int disjoint_set[], int v) { n   if (disjoint_set[v] == v) return v; n   return disjoint_set[v] = get_canonical(disjoint_set, disjoint_set[v]); n } n bool join(int disjoint_set[], int u, int v) { n   u = get_canonical(disjoint_set, u); n   v = get_canonical(disjoint_set, v); n   if (u == v) { n     return false; n   } n   disjoint_set[v] = u; n   return true; n } n bool check(const vector<Edge> &edges) { n   vector<Edge> canonic_edges; n   for (auto e : edges) { n     int v = get_canonical(vertex_set, e.v); n     int u = get_canonical(vertex_set, e.u); n     cycle_set[v] = v; n     cycle_set[u] = u; n     canonic_edges.emplace_back(u, v); n   } n   for (auto e : canonic_edges) { n     if (!join(cycle_set, e.v, e.u)) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     vertex_set[i] = i; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> raw_edge[i].u >> raw_edge[i].v >> raw_edge[i].w; n     edge[raw_edge[i].w].emplace_back(raw_edge[i].v, raw_edge[i].u); n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     ans[i] = true; n     int k; n     cin >> k; n     for (int j = 0; j < k; ++j) { n       int edge_id; n       cin >> edge_id; n       int w = raw_edge[edge_id].w; n       if (edge_group[w].empty() || edge_group[w].back().q != i) { n         edge_group[w].emplace_back(i); n       } n       edge_group[w].back().edges.push_back(raw_edge[edge_id]); n     } n   } n   for (int w = 1; w < MAX_W; ++w) { n     for (auto group : edge_group[w]) { n       ans[group.q] &= check(group.edges); n     } n     for (auto e : edge[w]) { n       join(vertex_set, e.v, e.u); n     } n   } n   for (int i = 0; i < q; ++i) { n     cout << (ans[i] ?  YES  :  NO ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n struct Edge { n   int to, next, v, c; n } a[40001 << 7]; n int h[40001], cnt = 1; n void add(int x, int y, int z, int w) { n   a[++cnt] = (Edge){y, h[x], z, w}, h[x] = cnt, n   a[++cnt] = (Edge){x, h[y], 0, -w}, h[y] = cnt; n } n int S, T, n, m, p[40001], d[40001]; n int flow, val; n bool v[40001]; n bool Spfa() { n   std::queue<int> q; n   memset(v, 0, sizeof(v)); n   memset(p, 0, sizeof(p)); n   memset(d, 0x3f, sizeof(d)); n   q.push(S), d[S] = 0, v[S] = 1; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     v[x] = 0; n     for (int i = h[x]; i; i = a[i].next) n       if (a[i].v && d[x] + a[i].c < d[a[i].to]) { n         d[a[i].to] = d[x] + a[i].c, p[a[i].to] = i; n         if (!v[a[i].to]) v[a[i].to] = 1, q.push(a[i].to); n       } n   } n   if (d[T] == 1061109567) return 0; n   return 1; n } n void Adjust() { n   int minf = 0x7fffffff, x = T; n   for (int i = T; i != S; i = a[p[i] ^ 1].to) minf = min(minf, a[p[i]].v); n   for (int i = T; i != S; i = a[p[i] ^ 1].to) n     a[p[i]].v -= minf, a[p[i] ^ 1].v += minf; n   flow += minf, val += d[T] * minf; n } n int Edmonds_Karp() { n   flow = val = 0; n   while (Spfa()) Adjust(); n   return val; n } n int du[110]; n signed main() { n   int x, y, c, f, ans = 0; n   n = read(), m = read(), add(n, 1, 0x7fffffff, 0), S = n + 1, T = n + 2; n   for (int i = 1; i <= m; i++) { n     x = read(), y = read(), c = read(), f = read(); n     if (c < f) n       ans += f - c, add(y, x, f - c, 0), add(y, x, c, 1), n           add(x, y, 0x7fffffff, 2); n     else n       add(x, y, c - f, 1), add(x, y, 0x7fffffff, 2), add(y, x, f, 1); n     du[x] -= f, du[y] += f; n   } n   for (int i = 1; i <= n; i++) n     if (du[i] > 0) n       add(S, i, du[i], 0); n     else n       add(i, T, -du[i], 0); n   cout << ans + Edmonds_Karp(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100 * 1000 + 5; n const int mod = 1000 * 1000 * 1000; n int a[maxN]; n int n; n string make(string s, int t) { n   string res; n   res.resize(n); n   for (int i = t; i < n; i++) res[i] = s[i - t]; n   for (int i = 0; i < t; i++) res[i] = s[i + n - t]; n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   string s; n   cin >> s; n   vector<string> res; n   for (int j = 0; j < 10; j++) { n     for (int i = 0; i < n; i++) n       if (s[i] ==  9 ) n         s[i] =  0 ; n       else n         s[i]++; n     for (int i = 0; i < n; i++) res.push_back(make(s, i)); n   } n   sort(res.begin(), res.end()); n   cout << res[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int M = 10005; n inline int read() { n   int ret = 0, f = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     ret = ret * 10 + c - 48; n     c = getchar(); n   } n   if (f) return -ret; n   return ret; n } n int T, mx, s, t, cnt, size, hh, n, m, x, y, sum, k, f[M], ha[M], v[M], n     a[105][2005]; n struct node { n   int x, y; n } xu[N]; n bool cmp(node a, node b) { return a.x < b.x; } n signed main() { n   int xjh = 0; n   T = read(); n   while (T--) { n     cnt = 0; n     n = read(); n     m = read(); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         xjh++; n         a[i][j] = read(); n         xjh++; n         xu[++cnt].x = a[i][j]; n         xu[cnt].y = j; n         xjh++; n         a[i + n][j] = a[i][j]; n       } n     size = 0; n     sort(xu + 1, xu + cnt + 1, cmp); n     for (int i = cnt; i; i--) { n       if (v[xu[i].y] == 0) { n         size++; n         xjh++; n         v[xu[i].y] = 1; n         xjh++; n       } n       if (size >= m) break; n     } n     mx = 1 << n; n     for (int j = 1; j <= m; j++) n       if (v[j] != 0) { n         for (int s = 0; s < mx; s++) { n           xjh++; n           xjh++; n           xjh--; n           ha[s] = 0; n           for (int i = 1; i <= n; i++) { n             sum = 0; n             xjh++; n             for (int k = i; k < n + i; k++) { n               x = k - i; n               if (s & (1 << x)) sum = sum + a[k][j]; n             } n             xjh++; n             xjh++; n             xjh--; n             ha[s] = max(ha[s], sum); n             xjh++; n             xjh++; n             xjh--; n           } n         } n         for (int s = mx - 1; s >= 0; s--) { n           for (int t = s; t; t = (t - 1) & s) { n             xjh++; n             f[s] = max(f[s], f[s ^ t] + ha[t]); n             xjh++; n             xjh++; n             xjh--; n           } n         } n       } n     xjh++; n     xjh++; n     xjh--; n     printf( %d n , f[mx - 1]); n     xjh++; n     xjh++; n     xjh--; n     for (int i = 0; i < mx; i++) f[i] = 0; n     for (int j = 1; j <= m; j++) v[j] = 0; n     xjh++; n     xjh++; n     xjh--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double n, m, k, r; n double f(int x, int y) { n   x++; n   y++; n   return (min(n + 1, x + r) - max((double)x, r)) * n          (min(m + 1, y + r) - max((double)y, r)); n } n int main() { n   cin >> n >> m >> r >> k; n   double ans = 0; n   double nbpos = (n - r + 1) * (m - r + 1); n   int tot = 0; n   priority_queue<pair<double, pair<int, int> > > q; n   pair<double, pair<int, int> > cur; n   q.push(make_pair(f(n / 2, m / 2), make_pair(n / 2, m / 2))); n   set<pair<int, int> > done; n   done.insert(make_pair(n / 2, m / 2)); n   pair<int, int> pos; n   while (tot < k) { n     cur = q.top(); n     pos = cur.second; n     q.pop(); n     ans += cur.first; n     if (pos.first - 1 >= 0 && n         done.find(make_pair(pos.first - 1, pos.second)) == done.end()) { n       q.push(make_pair(f(pos.first - 1, pos.second), n                        make_pair(pos.first - 1, pos.second))); n       done.insert(make_pair(pos.first - 1, pos.second)); n     } n     if (pos.first + 1 < n && n         done.find(make_pair(pos.first + 1, pos.second)) == done.end()) { n       q.push(make_pair(f(pos.first + 1, pos.second), n                        make_pair(pos.first + 1, pos.second))); n       done.insert(make_pair(pos.first + 1, pos.second)); n     } n     if (pos.second - 1 >= 0 && n         done.find(make_pair(pos.first, pos.second - 1)) == done.end()) { n       q.push(make_pair(f(pos.first, pos.second - 1), n                        make_pair(pos.first, pos.second - 1))); n       done.insert(make_pair(pos.first, pos.second - 1)); n     } n     if (pos.second + 1 < m && n         done.find(make_pair(pos.first, pos.second + 1)) == done.end()) { n       q.push(make_pair(f(pos.first, pos.second + 1), n                        make_pair(pos.first, pos.second + 1))); n       done.insert(make_pair(pos.first, pos.second + 1)); n     } n     tot++; n   } n   cout << setprecision(11) << ans / nbpos << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, c, e; n   char s[100]; n   scanf( %d%d%*d.%d , &n, &m, &c); n   map<string, int> u; n   for (int i = 0; i < n; i++) { n     scanf( %s%d , s, &e); n     if (e * c / 100 >= 100) u[s] = e * c / 100; n   } n   for (int i = 0; i < m; i++) { n     scanf( %s , s); n     u[s]; n   } n   printf( %d n , int(u.size())); n   for (auto& x : u) printf( %s %d n , x.first.c_str(), x.second); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[1000100], b[1000100], ans; n stack<int> st; n int n, m; n bool del[1000100], used[1001000]; n int main() { n   scanf( %d%d , &n, &m); n   int i; n   for (i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     a[u].push_back(v); n     b[v].push_back(u); n   } n   memset(del, 0, sizeof(del)); n   for (i = 1; i <= n; i++) { n     if (del[i]) continue; n     del[i] = 1; n     st.push(i); n     int len = a[i].size(), j; n     for (j = 0; j < len; j++) del[a[i][j]] = 1; n   } n   memset(used, 0, sizeof(used)); n   while (!st.empty()) { n     int u = st.top(); n     st.pop(); n     used[u] = 1; n     int j, len = b[u].size(); n     for (j = 0; j < len; j++) n       if (used[b[u][j]]) { n         used[u] = 0; n         break; n       } n     if (used[u]) ans.push_back(u); n   } n   int len = ans.size(); n   printf( %d n , ans.size()); n   for (i = 0; i < len; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n long long i, j, n; n int ar[N]; n int main() { n   for (int i = 2; i < N; i++) { n     for (int j = i + i; j < N; j += i) { n       ar[j] += j / i; n     } n   } n   cin >> n; n   long long sum = 0; n   for (int i = 2; i <= n; i++) sum += ar[i]; n   cout << 4 * sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int trie[5000005][2]; n int n, cnt; n inline int read() { n   int t = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     t = (t << 3) + (t << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return t; n } n inline void write(int t) { n   if (t > 9) { n     write(t / 10); n   } n   putchar(t % 10 +  0 ); n } n inline void insert(int t) { n   int pos = 0; n   for (int i = 29; i >= 0; i--) { n     int x = (t >> i) & 1; n     if (!trie[pos][x]) { n       trie[pos][x] = ++cnt; n     } n     pos = trie[pos][x]; n   } n } n inline int solve(int t, int cur) { n   if (t == -1) { n     return 0; n   } n   if (!trie[cur][0]) { n     return solve(t - 1, trie[cur][1]); n   } else if (!trie[cur][1]) { n     return solve(t - 1, trie[cur][0]); n   } else { n     return (1 << t) + n            min(solve(t - 1, trie[cur][0]), solve(t - 1, trie[cur][1])); n   } n } n int main() { n   n = read(); n   while (n--) { n     insert(read()); n   } n   write(solve(29, 0)); n   putchar(10); n   return 0; n } n


#include <bits/stdc++.h> n inline long long read() { n   long long x = 0; n   char ch = getchar(), w = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * w; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   write(x); n   puts(  ); n } n using namespace std; n const int N = 3e5 + 666; n long long a[N], n; n const long long mo = 998244353; n long long ksm(long long x, long long y) { n   long long res = 1; n   for (; y; y >>= 1, x = x * x % mo) n     if (y & 1) res = res * x % mo; n   return res; n } n int main() { n   n = read(); n   long long inv = ksm(100, mo - 2); n   for (int i = 1; i <= n; ++i) a[i] = read() * inv % mo; n   long long e1 = 0, s = 1; n   for (int i = 1; i <= n; ++i) { n     e1 += s; n     s *= a[i] % mo; n     s %= mo; n     e1 %= mo; n   } n   e1 = e1 * ksm(s, mo - 2) % mo; n   long long ans = 0; n   for (int i = n; i >= 1; --i) { n     ans = ans * a[i] % mo + (1 - a[i] + mo) * e1 % mo; n     ans += 1; n     ans %= mo; n   } n   writeln(ans); n   return 0; n } n


#include <bits/stdc++.h> n #define mid (tl+tr)/2 n #define mod 1000000007 n #define fain(arr) for(ll i=0;i<n;i++)cin>>arr[i]; n #define all(x) x.begin(),x.end() n #define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define bugv(n1) if(DEBUG)cout<<#n1<<  is  <<n1<<  n ; n #define buga(A) cout<<#A<<  is : <<endl ; for(auto x:A)cout<<x<<   ; cout<<endl; n #define FILE freopen( input.txt , r ,stdin); n #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) n #define endl   n  n #define ll long long  n #define pii pair<ll,ll> n #define pll pair<long long ,long long > n #define pi acos(-1) n #define sz(x) ((ll)x.size()) n #define clr(x) memset(x, 0, sizeof(x)) n #define init(x, a) memset(x, a, sizeof(x)) n #define DEBUG true n using namespace std; n  n int main(){ n  // FILE; n  SPEED; n  ll t; n  cin>>t; n  while(t--){ n        int n,m; n        cin>>n>>m; n  n        char arr[n][m]; n  n        for(int i=0;i<n;i++){ n         for(int j=0;j<m;j++){ n          cin>>arr[i][j]; n         } n        } n  n  n        int left[n][m]; n        int right[n][m]; n        int up[n][m]; n  n        for(int i=0;i<n;i++){ n          for(int j=m-1;j>=0;j--){ n            if(j==m-1){ n              right[i][j]=arr[i][j]== * ?1:0; n            } n            else{ n             if(arr[i][j]== * ) n             right[i][j]=right[i][j+1]+1; n             else n             right[i][j]=0; n            } n          } n        } n  n         for(int i=0;i<n;i++){ n          for(int j=0;j<m;j++){ n            if(j==0){ n              left[i][j]=arr[i][j]== * ?1:0; n            } n            else{ n             if(arr[i][j]== * ) n             left[i][j]=left[i][j-1]+1; n             else n             left[i][j]=0; n            } n          } n        } n        int ans=0; n        for(int i=0;i<n;i++){ n          for(int j=0;j<m;j++){ n            if(i==0){ n              up[i][j]=arr[i][j]== * ?1:0; n            } n            else{ n             up[i][j]=min({left[i][j],right[i][j],1+up[i-1][j]}); n            } n            ans+=up[i][j]; n          } n        } n  n        cout<<ans<<endl; n  n  } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n int n; n int t[2][N]; n vector<int> vals[N]; n vector<pair<int, int>> G[N]; n bool vis[N]; n int color[N]; n vector<int> C[2]; n bool cycle; n void dfs(int v) { n   vis[v] = true; n   C[color[v]].push_back(v); n   for (auto it : G[v]) { n     if (vis[it.first] && (color[v] ^ it.second) != color[it.first]) n       cycle = true; n     if (!vis[it.first]) { n       color[it.first] = color[v] ^ it.second; n       dfs(it.first); n     } n   } n } n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) G[i].clear(), vals[i].clear(); n   for (int j = 0; j <= 1; ++j) n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &t[j][i]); n       vals[t[j][i]].push_back(i); n     } n   for (int i = 1; i <= n; ++i) { n     if ((int)vals[i].size() != 2) { n       printf( -1 n ); n       return; n     } n     int u = vals[i][0], v = vals[i][1], w; n     if (u == v) continue; n     w = ((t[0][u] == t[0][v] && t[0][u] == i) || n          (t[1][u] == t[1][v] && t[1][u] == i)); n     G[u].push_back(make_pair(v, w)); n     G[v].push_back(make_pair(u, w)); n   } n   cycle = false; n   for (int i = 1; i <= n; ++i) vis[i] = false; n   vector<int> ans; n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) { n       for (int j = 0; j <= 1; ++j) C[j].clear(); n       dfs(i); n       if ((int)C[0].size() > (int)C[1].size()) swap(C[0], C[1]); n       for (auto it : C[0]) ans.push_back(it); n     } n   if (cycle) printf( -1 n ); n   printf( %d n , (int)ans.size()); n   for (auto it : ans) printf( %d  , it); n   printf(  n ); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double PI = 2.0 * acos(0.0); n const int MAXN = 33; n int n, m; n string s; n bool d1[26]; n bool d2[700]; n int encode(char x, char y) { return (x -  a ) * 26 + (y -  a ); } n int main() { n   memset(d1, false, sizeof(d1)); n   memset(d2, false, sizeof(d2)); n   cin >> n; n   for (int i = 0, _n = (n); i < _n; i++) { n     cin >> s; n     m = s.length(); n     for (int j = 0, _n = (m); j < _n; j++) { n       d1[s[j] -  a ] = true; n       if (j + 1 < m) d2[encode(s[j], s[j + 1])] = true; n     } n   } n   char r1, r2; n   int x; n   for (int i = 0, _n = (26); i < _n; i++) { n     if (!d1[i]) { n       r1 =  a  + i; n       cout << r1 << endl; n       return 0; n     } n   } n   for (int i = 0, _n = (700); i < _n; i++) { n     if (!d2[i]) { n       r1 =  a  + (i / 26); n       r2 =  a  + (i % 26); n       cout << r1 << r2 << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   if (k > n) n     cout << k; n   else n     for (int i = 2;; i++) { n       if (k * i > n) { n         cout << k * i; n         break; n       } n     } n   return 0; n } n


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


#pragma GCC optimize(2) n #include <bits/stdc++.h> n #define all(n) (n).begin(), (n).end() n #define se second n #define fi first n #define pb push_back n #define mp make_pair n #define sqr(n) (n) * (n) n #define rep(i, a, b) for (int i = (a); i <= (b); ++i) n #define per(i, a, b) for (int i = (a); i >= (b); --i) n #define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr) n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef pair<int, int> PII; n typedef pair<ll, ll> PLL; n typedef vector<int> VI; n typedef double db; n   n template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; } n template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; } n template<class T> void clear(T& a) { T().swap(a); } n   n const int N = 2e5 + 5; n   n int n, m, _, k; n ll a[N]; n   n int main() { n  IOS; n  for (cin >> _; _; --_) { n   cin >> n; ll mx = -1, s = 0; n         rep (i, 1, n) cin >> a[i], umax(mx, a[i]), s += a[i]; n         ll ans = mx * (n - 1ll) - s; n         if (ans < 0) ans = (ans % (n - 1) + (n - 1)) % (n - 1); n         cout << ans <<   n ; n     } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n using namespace std; n int n, m; n string s; n int prime(int x) { n   if (x == 1) return 0; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return 0; n   return 1; n } n int check(int x, int y) { n   if (x > n || x < 1 || y >= m || y < 0) return 0; n   return 1; n } n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int main() { n   cin >> s; n   if (s.size() <= 0) { n     string t = s; n     reverse(t.begin(), t.end()); n     string k = s + t; n     cout << k; n     return 0; n   } n   for (int i = 0; i < s.size(); i++) { n     string t =   , k =   ; n     for (int j = 0; j < i; j++) t += s[j]; n     for (int j = i; j < s.size(); j++) k += s[j]; n     string t1 =   ; n     if (k.size() >= t.size()) { n       reverse(k.begin(), k.end()); n       for (int j = k.size() - t.size(); j < k.size(); j++) t1 += k[j]; n       if (t1 == t) { n         string ans = k; n         reverse(k.begin(), k.end()); n         ans += k; n         cout << ans; n         return 0; n       } n     } else if (t.size() >= k.size()) { n       reverse(t.begin(), t.end()); n       for (int j = 0; j < t.size() - k.size(); j++) t1 += t[j]; n       if (t1 == k) { n         string ans = t; n         reverse(t.begin(), t.end()); n         ans += t; n         cout << ans; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long int n, m, i, j, x; n   cin >> n >> m; n   long long int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (i = 0; i < m; i++) { n     cin >> x; n     cout << upper_bound(a, a + n, x) - a <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s = 86400; n   long long t, a; n   cin >> n >> t; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (s - a >= t) { n       cout << i; n       break; n     } else n       t -= s - a; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  -O3  n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using namespace std; n const int MX = 7007; n ll f[MX]; n bool better(ll x, ll y) { return ((x ^ y) & x) != 0; } n ll x[MX], y[MX]; n bool ok[MX]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<pair<ll, ll>> a; n   map<ll, int> cnt; n   map<ll, ll> sum; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> y[i]; n   } n   for (int i = 0; i < n; i++) { n     a.emplace_back(x[i], y[i]); n     cnt[x[i]]++; n     sum[x[i]] += y[i]; n   } n   sort(a.begin(), a.end()); n   for (int i = n - 1; i >= 0; i--) { n     if (cnt[a[i].first] >= 2) { n       ok[i] = true; n     } n     for (int j = i + 1; j < n; j++) { n       if (!better(a[i].first, a[j].first) && ok[j]) { n         ok[i] = true; n       } n     } n   } n   ll ans = 0; n   for (int i = 0; i < n; i++) { n     if (ok[i]) { n       ans += a[i].second; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int32_t> dp(1, 0); n void solve() { n   int64_t n; n   cin >> n; n   int64_t x3 = 0, x2 = 0; n   while (n % 3 == 0 && n != 1) { n     n /= 3; n     x3++; n   } n   while (n % 2 == 0 && n != 1) { n     n /= 2; n     x2++; n   } n   if (x3 >= x2 && n == 1) { n     cout << x3 + (x3 - x2) <<   n ; n   } else { n     cout << -1 <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t_; n   cin >> t_; n   while (t_--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 20010; n int lf[max_n]; n int rf[max_n]; n int main() { n   long long sum = 0; n   int n; n   int x, y; n   memset(lf, 0, sizeof(lf)); n   memset(rf, 0, sizeof(rf)); n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &x, &y); n     ++lf[y - x + 1000]; n     ++rf[x + y]; n   } n   for (int i = 0; i <= 2050; ++i) { n     sum += (lf[i] * (lf[i] - 1)) / 2; n   } n   for (int i = 0; i <= 2050; ++i) { n     sum += (rf[i] * (rf[i] - 1)) / 2; n   } n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int r, j, l, x, y, m, c, n, s, f, q, i, z, p, k, d, t, u, e, g, w; n string s1, s2, s3, s4, str; n vector<long long int> v; n int main() { n   cin >> n >> x; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     v.push_back(a[i]); n   } n   k = 0; n   double ans = 0.000; n   for (long long int i = 0; i < n - 2; i++) { n     p = a[i] + x; n     l = lower_bound(v.begin(), v.end(), p) - v.begin(); n     if (v[l] != p) l--; n     if (l <= i) continue; n     double ans2 = n         (((v[l] - a[i + 1]) * 1.000) / ((v[l] - a[i]) * 1.000) * 1.000); n     if (ans2 - ans >= 1e-9) { n       k = 1; n       ans = ans2; n     } n   } n   if (k == 0) n     cout << -1; n   else n     printf( %.9f , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long maxn = 2001; n long long h[maxn], dp[maxn][2 * maxn]; n signed main() { n   long long n, k; n   scanf( %lld%lld , &n, &k); n   for (long long i = 1; i <= n; i++) scanf( %lld , &h[i]); n   dp[0][2001] = 1; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j < 2 * maxn; j++) n       if (h[i] == h[i % n + 1]) n         dp[i][j] = k * dp[i - 1][j] % mod; n       else n         dp[i][j] = (dp[i - 1][j + 1] + dp[i - 1][j - 1] + n                     (k - 2) * dp[i - 1][j] % mod) % n                    mod; n   } n   long long ans = 0; n   for (long long i = 1; i < maxn; i++) ans = (ans + dp[n][maxn + i]) % mod; n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 900; n struct edge { n   int v, w, b; n   double c; n }; n vector<edge> adj[maxn]; n int n, k; n int s, t; n double dis[maxn]; n int bk[maxn], bki[maxn]; n bool inq[maxn]; n long long x[maxn], y[maxn]; n inline void ae(int u, int v, int w, double c) { n   edge nu, nv; n   nu.v = v; n   nu.w = w; n   nu.c = c; n   nu.b = adj[v].size(); n   nv.v = u; n   nv.w = 0; n   nv.c = -c; n   nv.b = adj[u].size(); n   adj[u].push_back(nu); n   adj[v].push_back(nv); n } n inline pair<int, double> spfa() { n   for (int i = 0; i < maxn; i++) dis[i] = 1e30; n   memset(inq, 0, sizeof(inq)); n   dis[s] = 0; n   queue<int> q; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     inq[u] = 0; n     for (int i = adj[u].size() - 1; i >= 0; i--) { n       int v = adj[u][i].v; n       int w = adj[u][i].w; n       double c = adj[u][i].c; n       if (dis[u] + c < dis[v] && w != 0) { n         dis[v] = dis[u] + c; n         bk[v] = u; n         bki[v] = i; n         if (!inq[v]) { n           q.push(v); n           inq[v] = 1; n         } n       } n     } n   } n   if (dis[t] > 1e20) return make_pair(-1, 0.0); n   int cur = t; n   int flw = 0x3f3f3f3f; n   while (cur != s) { n     flw = min(flw, adj[bk[cur]][bki[cur]].w); n     cur = bk[cur]; n   } n   cur = t; n   double cst = 0; n   while (cur != s) { n     adj[bk[cur]][bki[cur]].w -= flw; n     adj[cur][adj[bk[cur]][bki[cur]].b].w += flw; n     cst += flw * adj[bk[cur]][bki[cur]].c; n     cur = bk[cur]; n   } n   return make_pair(flw, cst); n } n inline pair<int, double> ek() { n   pair<int, double> ans = make_pair(0ll, 0.0); n   while (1) { n     pair<int, double> cr = spfa(); n     if (cr.first <= 0) return ans; n     ans.first += cr.first; n     ans.second += cr.second; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n   } n   s = n + n + 1; n   t = n + n + 3; n   for (int i = 1; i <= n; i++) { n     ae(s, i, 2, 0.0); n     ae(i + n, t, 1, 0.0); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       if (y[i] > y[j]) n         ae(i, j + n, 1, n            sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]))); n     } n   } n   pair<int, double> ans = ek(); n   if (ans.first != n - 1) n     cout << -1; n   else n     printf( %.15lf , ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int st = 0; n   cin >> s; n   int cnt[4] = {0}; n   for (int i = 0; i < s.size(); i++) { n     if ( 1  <= s[i] && s[i] <=  3 ) { n       ++cnt[s[i] -  0 ]; n     } n   } n   for (int i = 1; i <= 3; i++) { n     for (int j = 0; j < cnt[i]; j++) { n       if (st) { n         cout <<  + ; n       } else { n         st = 1; n       } n       cout << i; n     } n   } n   return 0; n } n


#include <iostream> n #include <cstdio> n #include <algorithm> n #include <cstring> n #include <cmath> n #include <vector> n #include <map> n #include <queue> n #define int long long n #define mod 998244353 n using namespace std; n int n,c[505],a[505],f[505][505],pos,ans; n char s[15]; n int dfs(int now,int nowd){ n  if (now>n)return 1; n  if (now==pos)return dfs(now+1,nowd+1); n  if (f[now][nowd]!=-1)return f[now][nowd]; n  int ans=0; n  if (now<pos)ans=dfs(now+1,nowd)+dfs(now+1,max(0ll,nowd+a[now])); n  else{ n   ans=dfs(now+1,nowd); n   if (nowd+a[now]>0)ans+=dfs(now+1,nowd+a[now]); n  } n  ans%=mod; n  return f[now][nowd]=ans; n } n signed main(){ n  cin>>n; n  for (int i=1;i<=n;i++){ n   scanf( %s ,s+1); n   if (s[1]== - )c[i]=-1; n   else scanf( %lld ,&c[i]); n  } n  for (int i=1;i<=n;i++){ n   if (c[i]==-1)continue; n   for (int j=1;j<=n;j++){ n    if (c[j]==-1)a[j]=-1; n    else if (c[j]<c[i]||(c[j]==c[i]&&j<i))a[j]=1; n    else a[j]=0; n   } n   memset(f,-1,sizeof(f)); n   pos=i; n   ans=(ans+c[i]*dfs(1,0))%mod; n  } n  cout<<ans<<endl; n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long x[300009]; n int main() { n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> x[i]; n   sort(x, x + n); n   long long a = x[0], b = 1, ans = 0; n   for (int i = 1; i < n; i++) { n     ans = (ans + x[i] * (2 * b - 1) - a) % 1000000007; n     a = (2 * a + x[i]) % 1000000007; n     b = 2 * b % 1000000007; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int N, arr[1000000]; n set<int> st; n int main() { n   cin >> N; n   if (N % 2 == 0) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 1, b = 1, e = N * 2; i <= N; b += 2, e -= 2) n     arr[i] = b, i++, arr[i] = e, i++; n   for (int i = N + 1; i <= N * 2;) { n     arr[i] = arr[i - N] + 1; n     i++; n     arr[i] = arr[i - N] - 1; n     i++; n   } n   cout <<  YES n ; n   for (int i = 1; i <= 2 * N; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const double eps = 1e-8; n int N; n long long K; n double pi; n int X[maxn], Y[maxn]; n struct Seg { n   double radl, radr; n   void correct() { n     if (radl > 2 * pi) radl -= 2 * pi; n     if (radr > 2 * pi) radr -= 2 * pi; n     if (radl < 0) radl += 2 * pi; n     if (radr < 0) radr += 2 * pi; n     if (radl > radr) swap(radl, radr); n   } n   bool operator<(const Seg& rhs) const { return radl < rhs.radl; } n } ses[maxn]; n double rads[maxn * 2]; n unordered_map<double, int> mp; n int c[maxn * 2]; n void add(int pos, int val) { n   while (pos <= N * 2) { n     c[pos] += val; n     pos += (pos & (-pos)); n   } n } n int sum(int pos) { n   int res = 0; n   while (pos) { n     res += c[pos]; n     pos -= (pos & (-pos)); n   } n   return res; n } n int main() { n   scanf( %d%lld , &N, &K); n   long long ALL = 1ll * N * (N - 1) / 2; n   pi = acos(-1); n   for (int i = 1; i <= N; i++) scanf( %d%d , &X[i], &Y[i]); n   double mxr = 1e5, mir = 0; n   while (mxr - mir > eps) { n     double mid = (mxr + mir) / 2.0; n     int rcnt = 0, rscnt = 0; n     mp.clear(); n     memset(c, 0, sizeof(c)); n     for (int i = 1; i <= N; i++) { n       double dis = sqrt(X[i] * X[i] + Y[i] * Y[i]); n       if (dis <= mid) continue; n       double ang = atan2(Y[i], X[i]) + pi; n       double dan = acos(mid / dis); n       ses[++rcnt] = Seg{ang + dan, ang - dan}; n       ses[rcnt].correct(); n       rads[++rscnt] = ses[rcnt].radl; n       rads[++rscnt] = ses[rcnt].radr; n     } n     sort(ses + 1, ses + rcnt + 1); n     sort(rads + 1, rads + rscnt + 1); n     for (int i = 1; i <= rscnt; i++) mp[rads[i]] = i; n     long long cnt = 0; n     for (int i = 1; i <= rcnt; i++) { n       cnt += sum(mp[ses[i].radr] - 1) - sum(mp[ses[i].radl]); n       add(mp[ses[i].radr], 1); n     } n     if (ALL - cnt >= K) n       mxr = mid; n     else n       mir = mid; n   } n   printf( %.8lf n , mir); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int a[maxn], b[maxn]; n int n, k; n bool check(int x, int state) { n   if (state == 1) { n     int c = 0, right = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i] <= x) { n         right = i; n         c++; n         i++; n       } n     } n     if (c == k - k / 2 && right == n && k % 2 == 0) return false; n     return c >= k - k / 2; n   } else { n     int c = 0, right = 0; n     for (int i = 2; i <= n; i++) { n       if (a[i] <= x) { n         right = i; n         c++; n         i++; n       } n     } n     if (c == k / 2 && right == n && k % 2 != 0) return false; n     return c >= k / 2; n   } n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   int l = 1, r = n; n   while (l < r) { n     int mid = r + l >> 1; n     if (check(b[mid], 0) || check(b[mid], 1)) n       r = mid; n     else n       l = mid + 1; n   } n   cout << b[l]; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <class T> n inline void amin(T &x, const T &y) { n   if (y < x) x = y; n } n template <class T> n inline void amax(T &x, const T &y) { n   if (x < y) x = y; n } n template <class Iter> n void rprintf(const char *fmt, Iter begin, Iter end) { n   for (bool sp = 0; begin != end; ++begin) { n     if (sp) n       putchar(   ); n     else n       sp = true; n     printf(fmt, *begin); n   } n   putchar(  n ); n } n int N; n int dp[2011]; n int prv[2011]; n bool tar[1011]; n bool cur[1011]; n int calc(int a) { n   memset(dp, 0xc0, sizeof dp); n   dp[0] = 0; n   for (int i = 0, i_len = (N); i < i_len; ++i) { n     for (int j = 1; j < a; j++) { n       if (dp[i + j + 1] < dp[i] + j) { n         dp[i + j + 1] = dp[i] + j; n         prv[i + j + 1] = i; n       } n     } n   } n   return dp[N] - a + 1; n } n void query(vector<int> v) { n   printf( %d , (int)v.size()); n   for (int i = 0, i_len = (v.size()); i < i_len; ++i) printf(  %d , v[i] + 1); n   printf(  n ); n   fflush(stdout); n   for (__typeof((v).begin()) e = (v).begin(), e_end = (v).end(); e != e_end; n        ++e) n     cur[*e] = true; n   int x; n   scanf( %d , &x); n   x--; n   for (int i = 0, i_len = (v.size()); i < i_len; ++i) cur[(x + i) % N] = false; n } n void solve() { n   int ma = 0, a = 0; n   for (int b = 1; b < N; b++) { n     int tmp = calc(b); n     if (ma < tmp) { n       ma = tmp; n       a = b; n     } n   } n   if (ma == 0) { n     puts( 0 ); n     fflush(stdout); n   } else { n     calc(a); n     int x = N; n     memset(tar, 0, sizeof tar); n     memset(cur, 0, sizeof cur); n     while (x) { n       for (int k = prv[x]; k < x - 1; k++) tar[k] = true; n       x = prv[x]; n     } n     while (1) { n       vector<int> v; n       for (int i = 0, i_len = (N); i < i_len; ++i) n         if (tar[i] && !cur[i]) { n           v.push_back(i); n         } n       if ((int)v.size() <= a) { n         for (int i = 0, i_len = (N); i < i_len; ++i) n           if (tar[i] && cur[i]) { n             if ((int)v.size() == a) break; n             v.push_back(i); n           } n         query(v); n         break; n       } else { n         v.resize(a); n         query(v); n       } n     } n     puts( 0 ); n     fflush(stdout); n   } n } n void MAIN() { n   scanf( %d , &N); n   solve(); n } n int main() { n   int TC = 1; n   for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 5; n long long dp[105], cnt[105], b[N], a[N]; n void add(long long &a, long long b) { n   a += b; n   if (a >= mod) a -= mod; n } n struct Mat { n   long long t[101][101]; n   Mat() { memset(t, 0, sizeof t); } n } I; n int m; n Mat operator*(Mat a, Mat b) { n   Mat c; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < m; j++) { n       long long t = 0; n       for (int k = 0; k < m; k++) { n         add(c.t[i][j], a.t[i][k] * b.t[k][j] % mod); n       } n     } n   return c; n } n Mat Pow(Mat a, int b) { n   Mat c = I; n   while (b) { n     if (b & 1) c = c * a; n     a = a * a; n     b >>= 1; n   } n   return c; n } n int main() { n   int n, l, x; n   cin >> n >> l >> m; n   for (int i = 0; i < m; i++) dp[i] = 0, cnt[i] = 0; n   for (int i = 1; i <= n; i++) scanf( %d , &x), dp[x % m]++; n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), cnt[a[i] % m]++; n   for (int i = 1; i <= n; i++) scanf( %lld , &b[i]); n   for (int i = 0; i < m; i++) I.t[0][i] = dp[i]; n   Mat pi; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < m; j++) pi.t[i][j] = cnt[(m - i + j) % m]; n   Mat ans = Pow(pi, l - 2); n   for (int i = 0; i < m; i++) cnt[i] = 0, dp[i] = 0; n   for (int i = 1; i <= n; i++) { n     cnt[(a[i] + b[i]) % m]++; n   } n   for (int i = 0; i < m; i++) n     for (int j = 0; j < m; j++) n       add(dp[(j + i) % m], ans.t[0][j] * cnt[i] % mod); n   printf( %lld n , dp[0]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int variables[3001][3001]; n long long int cost[3001][3001]; n int n; n long long int a[3001]; n void inicjuj() { n   for (int i = 0; i < n; ++i) { n     variables[0][i] = a[i] - i; n   } n   sort(variables[0], variables[0] + n); n   for (int i = 0; i < n; ++i) cost[0][i] = abs(variables[0][i] - a[0]); n } n void __built_column(int X) { n   long long int minpref = 1e18; n   for (int j = 0; j < n; ++j) { n     minpref = min(minpref, cost[X - 1][j]); n     variables[X][j] = variables[X - 1][j] + 1; n     cost[X][j] = minpref + (long long int)abs(a[X] - variables[X][j]); n   } n } n long long int oblicz() { n   for (int i = 1; i < n; ++i) { n     __built_column(i); n   } n   long long int wyn = 1e18; n   for (int j = 0; j < n; ++j) { n     wyn = min(wyn, cost[n - 1][j]); n   } n   return wyn; n } n void wczytaj() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n } n int main() { n   wczytaj(); n   inicjuj(); n   cout << oblicz() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n template <typename T> n inline void read(T &x) { n   x = 0; n   long long f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x = x * f; n } n }  // namespace IO n using namespace IO; n const long long maxn = 200000; n long long dp[maxn + 10][4]; n long long a[maxn + 10]; n void DP(long long &x, long long y) { x = max(x, y); } n signed main() { n   long long _; n   read(_); n   while (_--) { n     long long n; n     read(n); n     for (long long i = 0; i <= n; i++) { n       dp[i][0] = dp[i][1] = dp[i][2] = 0; n       a[i] = 0; n     } n     for (long long i = 0; i < n; i++) { n       read(a[i]); n     } n     for (long long i = 0; i < n; i++) { n       if (i % 2 == 1) { n         DP(dp[i + 1][0], dp[i][0]); n         if (i + 2 <= n) DP(dp[i + 2][1], max(dp[i][1] + a[i], dp[i][0] + a[i])); n         DP(dp[i + 1][2], max(dp[i][1], max(dp[i][0], dp[i][2]))); n       } else { n         DP(dp[i + 1][0], dp[i][0] + a[i]); n         if (i + 2 <= n) DP(dp[i + 2][1], max(dp[i][1], dp[i][0]) + a[i + 1]); n         DP(dp[i + 1][2], max(dp[i][1], max(dp[i][0], dp[i][2])) + a[i]); n       } n     } n     cout << max(dp[n][1], max(dp[n][2], dp[n][0])) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char a[11]; n bool ok = true; n bool check(char a[]) { n   for (int i = 0; i < 7; i++) n     if (a[i] == a[i + 1]) return false; n   return true; n } n int main() { n   for (int i = 1; i <= 8; i++) { n     scanf( %s , a); n     ok &= check(a); n   } n   if (ok) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100050; n const int M = 10050; n long long mn[M], h; n int ord[M], tmp[M], que[M], k, n, m; n bool was[M]; n long long a[N]; n int c[N]; n stack<int> stk[M]; n struct cmp { n   bool operator()(int i, int j) { n     return c[i] > c[j] || (c[i] == c[j] && i < j); n   } n }; n set<int, cmp> pts; n bool in[N]; n void Cns(int ost) { n   while (stk[ost].size() && a[stk[ost].top()] >= mn[ost]) { n     int i = stk[ost].top(); n     stk[ost].pop(); n     pts.insert(i); n     in[i] = 1; n   } n } n void Upd(long long x) { n   int i = 1, j = 1, xb = 0, xz = 0; n   for (int i = 0; i < k; i++) was[i] = 0; n   for (; i <= k || j <= xb;) { n     int u; n     if (i > k || (j <= xb && mn[que[j]] < mn[ord[i]])) n       u = que[j++]; n     else n       u = ord[i++]; n     if (was[u]) continue; n     was[u] = 1; n     tmp[++xz] = u; n     int v = (u + x) % k; n     if (mn[v] > mn[u] + x) { n       mn[v] = mn[u] + x; n       Cns(v); n       que[++xb] = v; n     } n   } n   assert(xz == k); n   for (int i = 1; i <= k; i++) ord[i] = tmp[i]; n } n int main() { n   scanf( %lld %i %i %i , &h, &n, &m, &k); n   for (int i = 1; i < k; i++) mn[i] = h; n   for (int i = 1; i <= k; i++) ord[i] = i - 1; n   vector<int> ord; n   for (int i = 1; i <= n; i++) n     scanf( %lld %i , &a[i], &c[i]), a[i]--, ord.push_back(i); n   sort(ord.begin(), ord.end(), [&](int i, int j) { return a[i] < a[j]; }); n   for (int i : ord) stk[a[i] % k].push(i); n   Cns(0); n   while (m--) { n     int t; n     scanf( %i , &t); n     if (t == 1) { n       long long x; n       scanf( %lld , &x); n       Upd(x); n     } else if (t == 2) { n       int x, y; n       scanf( %i %i , &x, &y); n       if (in[x]) pts.erase(x); n       c[x] -= y; n       if (in[x]) pts.insert(x); n     } else { n       if (pts.empty()) n         printf( 0 n ); n       else { n         int i = *pts.begin(); n         printf( %i n , c[i]); n         pts.erase(i); n         c[i] = 0; n         in[i] = 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n long long n, k; n long long a[N]; n priority_queue<tuple<long long, long long, long long> > pq; n inline long long sq(long long x) { return x * x; } n long long cost(long long len, long long pieces) { n   long long each = len / pieces; n   long long extra = len % pieces; n   long long res = extra * sq(each + 1) + sq(each) * (pieces - extra); n   return res; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> k; n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     ans += sq(a[i]); n     long long val = cost(a[i], 1) - cost(a[i], 2); n     long long len = a[i]; n     long long pieces = 2; n     pq.push(make_tuple(val, len, pieces)); n   } n   for (long long i = 1; i <= k - n; i++) { n     long long val, len, pieces; n     tie(val, len, pieces) = pq.top(); n     pq.pop(); n     ans -= val; n     long long new_val = cost(len, pieces) - cost(len, pieces + 1); n     pieces++; n     pq.push(make_tuple(new_val, len, pieces)); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, h, m, data[55]; n int main() { n   cin >> n >> h >> m; n   for (int i = 1; i <= n; i++) { n     data[i] = h; n   } n   for (int i = 1; i <= m; i++) { n     long long l, r, x; n     cin >> l >> r >> x; n     for (int j = l; j <= r; j++) { n       data[j] = min(data[j], x); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) ans += data[i] * data[i]; n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long n, m, l, r, T, mid; n bool check(long long x) { n   long long p = x * m, s = 1, d = n, t; n   for (long i = 0; i <= x && p >= 0 && d; i++) { n     d -= (t = min(s, d)); n     p -= t * i; n     s = s * (x - i) / (i + 1); n   } n   return p >= 0 && !d; n } n int main() { n   for (scanf( %ld , &T); T; T--) { n     scanf( %ld%ld , &n, &m); n     m = min(m, n / 2); n     l = 0; n     r = n; n     while (l < r) { n       mid = (l + r) / 2; n       if (check(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     printf( %ld n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct Vec { n   long long x, y; n   Vec operator+(const Vec &o) const { return (Vec){x + o.x, y + o.y}; } n   Vec operator-(const Vec &o) const { return (Vec){x - o.x, y - o.y}; } n   long long operator*(const Vec &o) const { n     return 1ll * x * o.y - 1ll * y * o.x; n   } n   long long operator|(const Vec &o) const { n     return 1ll * x * o.x + 1ll * y * o.y; n   } n } mon[1010], arr[11]; n vector<int> ir[11][1010]; n bool book[11]; n int a[11]; n int die[1010], clk; n int now; n bool chk(int p) { n   if (die[p] == clk) return 1; n   now++; n   if (now > m) return 0; n   int ap = a[now]; n   int ma = ir[ap][p].size(); n   if (!ma) { n     die[p] = clk; n     return 1; n   } n   for (int i = 0; i < ma; ++i) n     if (!chk(ir[ap][p][i])) return 0; n   die[p] = clk; n   return 1; n } n bool check(int x) { n   clk++; n   now = 0; n   return chk(x); n } n bool dfs(int x, int p) { n   if (x > m) { n     if (check(p)) return 1; n     return 0; n   } n   for (int i = 1; i <= m; ++i) n     if (!book[i]) { n       a[x] = i; n       book[i] = 1; n       if (dfs(x + 1, p)) return 1; n       book[i] = 0; n     } n   return 0; n } n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= m; ++i) scanf( %lld%lld , &arr[i].x, &arr[i].y); n   for (int i = 1; i <= n; ++i) scanf( %lld%lld , &mon[i].x, &mon[i].y); n   Vec vn, v1; n   for (int i = 1; i <= m; ++i) n     for (int j = 1; j <= n; ++j) { n       vn = mon[j] - arr[i]; n       for (int k = 1; k <= n; ++k) { n         if (k == j) continue; n         v1 = mon[k] - arr[i]; n         if (v1 * vn != 0 || (v1 | vn) <= 0) continue; n         if (abs(v1.x) > abs(vn.x) || abs(v1.y) > abs(vn.y)) continue; n         ir[i][j].push_back(k); n       } n     } n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     memset(book, 0, sizeof(book)); n     if (dfs(1, i)) ans++; n   } n   printf( %d n , ans); n   return 0; n } n