#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, i, count = 0; n   vector<long long int> v; n   string s1, s2, s3, reverses1, reverses2, reverses3; n   cin >> s1 >> s2 >> s3; n   reverses1 = s1; n   reverses2 = s2; n   reverses3 = s3; n   reverse(s1.begin(), s1.end()); n   reverse(s2.begin(), s2.end()); n   reverse(s3.begin(), s3.end()); n   if (reverses1 == s3 && reverses2 == s2 && reverses3 == s1) n     cout <<  YES  n          <<   n ; n   else n     cout <<  NO  n          <<   n ; n } n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   fast(); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 3e18 + 100; n const int MOD = 1e9 + 7; n const int N = 100; n int32_t main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(cin.failbit); n   int n; n   cin >> n; n   set<int> s, tot; n   vector<int> a(n + 5, 0), cnt(n + 5, 0), last(n + 5); n   vector<bool> ok(n + 4, true); n   for (int i = 1; i <= n; ++i) { n     s.insert(i); n   } n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     cnt[a[i]]++; n     tot.insert(a[i]); n     if (s.find(a[i]) != s.end()) { n       s.erase(a[i]); n     } n   } n   cout << n - tot.size() << endl; n   for (int i = 1; i <= n; ++i) { n     if (cnt[a[i]] == 1) continue; n     if (ok[a[i]] and *s.begin() > a[i]) { n       ok[a[i]] = false; n     } else { n       cnt[a[i]]--; n       a[i] = *s.begin(); n       s.erase(s.begin()); n     } n   } n   for (int i = 1; i <= n; ++i) { n     cout << a[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, cnt; n   string s; n   map<int, pair<int, int> > mapp; n   cin >> n; n   cin >> s; n   cnt = 0; n   int maxm = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  0 ) n       cnt--; n     else n       cnt++; n     if (mapp.find(cnt) == mapp.end()) { n       mapp[cnt].first = i; n       mapp[cnt].second = i; n     } else { n       mapp[cnt].second = i; n       int len = mapp[cnt].second - mapp[cnt].first; n       if (len > maxm) maxm = len; n     } n   } n   if (mapp.find(0) != mapp.end()) maxm = max(maxm, mapp[0].second + 1); n   cout << maxm << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const vector<int> mods = {998244353, 1000000007, 1000000009}; n const long long inf = 3e18; n const double pi = acos(-1.0); n const double eps = 0.0000001; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <class... K> n using umap = unordered_map<K...>; n template <class... K> n using uset = unordered_set<K...>; n const int N = 1000100; n int n, l[N]; n vector<vector<int> > a, ord; n string s; n umap<int, long long> dp; n long long res; n void build(int x) { n   vector<int> cur = {}; n   vector<int> bk; n   auto ad = [&](vector<int>& to) { n     while (((int)(cur).size())) { n       to.push_back(cur.back()); n       cur.pop_back(); n     } n   }; n   for (long long i = (0); i < (l[x] - 1); ++i) { n     cur.push_back(i); n     if (a[x][i] < a[x][i + 1]) ad(ord[x]); n     if (a[x][i] > a[x][i + 1]) ad(bk); n   } n   cur.push_back(l[x] - 1); n   ad(ord[x]); n   for (int i = ((int)(bk).size()) - 1; i >= 0; i--) ord[x].push_back(bk[i]); n } n int p(int i, int si) { return i + (i >= si); } n bool gt(int i, int si, int j, int sj) { n   int pp = 0; n   while (p(pp, si) < l[i] && p(pp, sj) < l[j]) { n     int ppi = p(pp, si), ppj = p(pp, sj); n     if (a[i][ppi] < a[j][ppj]) return 0; n     if (a[i][ppi] > a[j][ppj]) return 1; n     pp++; n   } n   long long il = l[i] - (si != l[i] - 1); n   long long jl = l[j] - (sj != l[j] - 1); n   if (il > jl) n     return 1; n   else n     return 0; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout.precision(13); n   cin >> n; n   a.resize(n + 1); n   ord.resize(n + 1); n   for (long long i = (0); i < (n); ++i) { n     cin >> s; n     l[i] = s.length() + 1; n     for (long long j = (0); j < (l[i] - 1); ++j) a[i].push_back(s[j] -  a ); n     a[i].push_back(-1); n   } n   a[n] = {27, -1}; n   l[n] = 2; n   for (long long i = (0); i < (n + 1); ++i) build(i); n   dp[0] = 1; n   dp[1] = 0; n   for (int i = n; i >= 1; i--) { n     umap<int, long long> nxdp; n     int j = i - 1; n     int pi = 0, pj = 0; n     long long pre = 0; n     while (pi < l[i] || pj < l[j]) { n       if (pi >= l[i] || pj < l[j] && gt(j, ord[j][pj], i, ord[i][pi])) n         nxdp[pj++] = pre; n       else n         pre = (pre + dp[pi++]) % mod; n     } n     dp = move(nxdp); n   } n   for (auto& d_ : dp) res = (res + d_.second) % mod; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2200000; n int chk[N]; n int main() { n   ios_base::sync_with_stdio(0); n   int cnt = 0, n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     if (!chk[i]) { n       for (int j = i; n            (j < n && chk[j] < 2) || (j >= n && chk[n + n - 2 - j] < 2); n            j = (j + m + m - 2) % (n + n - 2)) { n         if (j < n) n           chk[j]++; n         else n           chk[n + n - 2 - j]++; n       } n       cnt++; n     } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[55][55]; n const int MAX = 1e3 + 5; n const int INF = 0x3f3f3f3f; n int color; n int A[MAX][MAX]; n int A1[MAX][MAX], A2[MAX][MAX]; n int N; n int DX[2] = {0, 1}, DY[2] = {1, 0}; n int NOW1[4], NOW2[4]; n int X1, Y1, X2, Y2; n bool FLAG = false; n bool VIS[MAX][MAX]; n void dfs(int x, int y, int count) { n   if (FLAG) return; n   if (count == 4) { n     if (NOW1[1] == NOW1[2] && NOW1[0] == NOW1[3]) { n       if (NOW2[1] != NOW2[2] || NOW2[0] != NOW2[3]) { n         FLAG = true; n         X2 = x, Y2 = y; n         color = 1; n         return; n       } n     } n     if (NOW2[1] == NOW2[2] && NOW2[0] == NOW2[3]) { n       if (NOW1[1] != NOW1[2] || NOW1[0] != NOW1[3]) { n         FLAG = true; n         X2 = x, Y2 = y; n         color = 2; n         return; n       } n     } n     return; n   } n   for (int i = 0; i < 2; ++i) { n     int numX = x + DX[i]; n     int numY = y + DY[i]; n     if (numX >= 1 && numX <= N && numY >= 1 && numY <= N && !VIS[numX][numY]) { n       NOW1[count] = A1[numX][numY]; n       NOW2[count] = A2[numX][numY]; n       VIS[numX][numY] = true; n       dfs(numX, numY, count + 1); n       VIS[numX][numY] = false; n     } n   } n } n void solve() { n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= N; ++j) { n       A[i][j] = a[i][j]; n       if ((i + j) & 1) { n         if (A[i][j] == 0) n           A1[i][j] = 1, A2[i][j] = 0; n         else n           A1[i][j] = 0, A2[i][j] = 1; n       } else n         A1[i][j] = A2[i][j] = A[i][j]; n     } n   } n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= N; ++j) { n       NOW1[0] = A1[i][j]; n       NOW2[0] = A2[i][j]; n       VIS[i][j] = true; n       dfs(i, j, 1); n       VIS[i][j] = false; n       if (FLAG) { n         X1 = i, Y1 = j; n         return; n       } n     } n   } n } n int main() { n   int n, t; n   scanf( %d , &n); n   N = n; n   a[1][1] = 1; n   a[1][2] = 0; n   a[n][n] = 0; n   printf( ? %d %d %d %d n , 1, 2, 2, 3); n   fflush(stdout); n   scanf( %d , &t); n   if (t == 1) n     a[2][3] = a[1][2]; n   else n     a[2][3] = a[1][2] ^ 1; n   printf( ? %d %d %d %d n , 2, 1, 2, 3); n   fflush(stdout); n   scanf( %d , &t); n   if (t == 1) n     a[2][1] = a[2][3]; n   else n     a[2][1] = a[2][3] ^ 1; n   for (int i = 3; i <= n; i++) { n     printf( ? %d %d %d %d n , i - 2, 1, i, 1); n     fflush(stdout); n     scanf( %d , &t); n     if (t == 1) n       a[i][1] = a[i - 2][1]; n     else n       a[i][1] = a[i - 2][1] ^ 1; n     printf( ? %d %d %d %d n , 1, i - 2, 1, i); n     fflush(stdout); n     scanf( %d , &t); n     if (t == 1) n       a[1][i] = a[1][i - 2]; n     else n       a[1][i] = a[1][i - 2] ^ 1; n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 2; j <= n; j++) { n       if (i == j && j == n) continue; n       if (i == 2 && j == 3) continue; n       printf( ? %d %d %d %d n , i - 1, j - 1, i, j); n       fflush(stdout); n       scanf( %d , &t); n       if (t == 1) n         a[i][j] = a[i - 1][j - 1]; n       else n         a[i][j] = a[i - 1][j - 1] ^ 1; n     } n   } n   solve(); n   printf( ? %d %d %d %d n , X1, Y1, X2, Y2); n   fflush(stdout); n   scanf( %d , &t); n   printf( ! n ); n   if (t == 1) { n     if (color == 1) { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) { n           if ((i + j) % 2 == 1) n             printf( %d , a[i][j] ^ 1); n           else n             printf( %d , a[i][j]); n         } n         printf(  n ); n       } n     } else { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) printf( %d , a[i][j]); n         printf(  n ); n       } n     } n   } else { n     if (color == 2) { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) { n           if ((i + j) % 2 == 1) n             printf( %d , a[i][j] ^ 1); n           else n             printf( %d , a[i][j]); n         } n         printf(  n ); n       } n     } else { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) printf( %d , a[i][j]); n         printf(  n ); n       } n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, i, b; n   while (scanf( %d , &n) != EOF) { n     int j = 0, str[10001] = {0}, total = 0; n     for (i = 0; i < n; i++) { n       scanf( %d , &b); n       if (str[b] == 0) ++total; n       ++str[b]; n       if (str[b] > j) j = str[b]; n     } n     printf( %d %d n , j, total); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double eps = 1e-6; n double pi = acos(0.0) * 2; n double dinf = 1e250; n long long INF = static_cast<long long>(2e18); n long long inf = static_cast<long long>(1e9 + 7); n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <class T> n void OUT(T a) { n   cout << a; n   exit(0); n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long extgcd(long long a, long long b, long long& x, long long& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = extgcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long x, y, k; n     cin >> x >> y >> k; n     if (x < 0) x = -x; n     if (y < 0) y = -y; n     if (x < y) swap(x, y); n     if (x > k) n       cout << -1 << endl; n     else { n       if ((x & 1) != (y & 1)) { n         k -= 1; n         x -= 1; n       } else if ((x & 1) != (k & 1)) { n         x -= 1; n         y -= 1; n         k -= 2; n       } n       cout << k << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, x, y, a, b, c, t, q; n   string s; n   while (cin >> n >> q) { n     if ((n / q) % 2 == 0) { n       cout <<  NO  << endl; n     } else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[100001], l = 1; n   for (int i = 1; i <= n; i++) { n     int c, t; n     cin >> c >> t; n     int x = c * t; n     a[l] = c * t; n     l++; n   } n   int d = 1, s = 0; n   for (int i = 0; i < m; i++) { n     int k; n     cin >> k; n     if (s + a[d] >= k) { n       cout << d << endl; n     } else { n       while (1) { n         s += a[d]; n         d++; n         if (s + a[d] >= k) { n           cout << d << endl; n           break; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void OJ() {} n long long n, m, x, k, y; n long long a[200000 + 5], b[200000 + 5]; n void imp() { cout << -1 <<   n , exit(0); } n long long remove(long long start, long long end) { n   if (start > end) return 0; n   long long sz = end - start + 1; n   long long maxneb = max(a[start - 1], a[end + 1]); n   long long m = *max_element(a + start, a + end + 1); n   long long a1 = (sz / k) * x + (sz % k) * y; n   if (maxneb > m) return min(a1, y * sz); n   if (sz < k) imp(); n   return min(a1, (sz - k) * y + x); n } n void solve() { n   cin >> n >> m >> x >> k >> y; n   for (long long i = 1; i <= n; ++i) cin >> a[i]; n   for (long long i = 1; i <= m; ++i) cin >> b[i]; n   long long ans = 0; n   long long j = 1; n   for (long long i = 1; i <= m; ++i) { n     long long start = j; n     while (j <= n && a[j] != b[i]) j++; n     if (j > n) imp(); n     ans += remove(start, j - 1); n     j++; n   } n   ans += remove(j, n); n   cout << ans <<   n ; n } n signed main() { n   OJ(); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n const int N = 100005; n void solve() { n   long long n; n   cin >> n; n   long long ans = 1; n   for (long long i = 2; i <= n; i++) { n     ans = ans + 4 * (i - 1); n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     solve(); n   } n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ton { n   vector<pair<long long, long long> > v[1919810 + 3]; n   long long& operator()(long long a, int b, int c, int d) { n     a = ((a * 4000ll + b) * 4000ll + c) * 3ll + (d == 1); n     int k = a % 1919810; n     for (auto u = v[k].begin(); u != v[k].end(); u++) n       if (u->first == a) return u->second; n     v[k].push_back(make_pair(a, -1e17)); n     return v[k].back().second; n   } n } f; n long long s[100010]; n long long F(int l, int r, int k, int p) { n   if (r - l + 1 < k) return 0; n   if (r - l + 1 == k) return p * (s[r] - s[l - 1]); n   long long& u = f(l, r, k, p); n   if (u != -1e17) return u; n   if (p == -1) n     u = min(F(l, r - k, k, 1) - (s[r] - s[r - k]), n             F(l, r - k - 1, k + 1, 1) - (s[r] - s[r - k - 1])); n   else n     u = max(F(l + k, r, k, -1) + (s[l + k - 1] - s[l - 1]), n             F(l + k + 1, r, k + 1, -1) + (s[l + k] - s[l - 1])); n   return u; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &s[i]), s[i] += s[i - 1]; n   printf( %lld n , F(1, n, 1, 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char dirs[100001]; n int todo[4]; n int main() { n   int n, fx, fy, tx, ty; n   string wind; n   cin >> n >> fx >> fy >> tx >> ty >> wind; n   memcpy(dirs, wind.c_str(), n); n   memset(todo, 0, sizeof(todo)); n   if (fx < tx) todo[0] = tx - fx; n   if (fy < ty) todo[3] = ty - fy; n   if (fx > tx) todo[2] = fx - tx; n   if (fy > ty) todo[1] = fy - ty; n   for (int i = 0; i < n; i++) { n     switch (dirs[i]) { n       case  E : n         if (todo[0]) todo[0]--; n         break; n       case  S : n         if (todo[1]) todo[1]--; n         break; n       case  W : n         if (todo[2]) todo[2]--; n         break; n       case  N : n         if (todo[3]) todo[3]--; n         break; n     } n     if (todo[0] == 0 && todo[1] == 0 && todo[2] == 0 && todo[3] == 0) { n       cout << i + 1 << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, p; n   cin >> t; n   for (; t > 0; t--) { n     cin >> n >> p; n     int br = 0; n     for (int r = 1; r <= n; r++) { n       for (int i = 1; i <= n; i++) { n         cout << i <<     << (i + r - 1) % n + 1 << endl; n         br++; n         if (br == 2 * n + p) goto st; n       } n     } n   st:; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int div_floor(const long long int &a, const long long int &b) { n   return a / b - (((a ^ b) < 0) and a % b); n } n long long int div_ceil(const long long int &a, const long long int &b) { n   return a / b + (((a ^ b) >= 0) and a % b); n } n long long int diff(long long int a, long long int cnt) { n   long long int rem = a % cnt; n   return a * a - ((a / cnt + 1) * (a / cnt + 1) * rem + n                   (a / cnt) * (a / cnt) * (cnt - rem)); n } n void solve() { n   long long int n, k; n   cin >> n >> k; n   long long int sum = 0; n   multiset<pair<long long int, pair<long long int, long long int> > > mst; n   vector<long long int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i] * a[i]; n     mst.insert({diff(a[i], 2), {a[i], 2}}); n   } n   while (n++ < k) { n     auto it = mst.end(); n     it--; n     auto el = *it; n     mst.erase(it); n     sum -= el.first; n     long long int x = el.second.first, ct = el.second.second; n     mst.insert({diff(x, ct + 1) - diff(x, ct), {x, ct + 1}}); n   } n   cout << sum <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[111]; n int vv(int k) { n   int i, j; n   j = 0; n   for (i = 1; i < k; i++) n     if (a[i] == 0) j++; n   for (i = k; i <= n; i++) n     if (a[i] == 1) j++; n   return j; n } n int main() { n   cin >> n; n   int i, j, k; n   for (i = 1; i <= n; i++) cin >> a[i]; n   a[n + 1] = 1; n   int d = 1; n   for (i = 0; i <= n + 1; i++) { n     d = max(vv(i), d); n   } n   cout << d; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long a[30]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   long long now = a[n - 1]; n   long long ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     now = min(now, a[i]); n     ans += now; n     now--; n     if (now < 0) break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int num_e = 1e9 + 10, num_m, ans, w, q; n string answer; n void read_input() { cin >> s; } n void solve() { n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  e ) num_e = i; n     if (s[i] ==  . ) num_m = i; n   } n   string s2 =   ; n   for (int i = 0; i < num_e; i++) n     if (s[i] !=  . ) s2 += s[i]; n   for (int i = num_e + 1; i < s.size(); i++) { n     int q = s[i] -  0 ; n     w = w * 10 + q; n   } n   w -= (num_e - num_m - 1); n   if (w >= 0) { n     answer += s2; n     for (int i = 0; i < w; i++) answer +=  0 ; n   } else { n     for (int i = 0; i < s2.size(); i++) { n       if (s2.size() - i == -w) answer +=  . ; n       answer += s2[i]; n     } n   } n } n void write_output() { n   bool check = false; n   for (int i = 0; i < answer.size(); i++) n     if (answer[i] !=  .  && answer[i] !=  0 ) check = true; n   if (!check) n     cout << 0; n   else { n     int start; n     for (int i = 0; i < answer.size(); i++) { n       if (answer[i] ==  . ) { n         start = i - 1; n         break; n       } n       if (answer[i] !=  0 ) { n         start = i; n         break; n       } n     } n     int num_m = -1; n     for (int i = 0; i < answer.size(); i++) n       if (answer[i] ==  . ) num_m = i; n     check = false; n     for (int i = num_m + 1; i < answer.size(); i++) n       if (answer[i] !=  0 ) check = true; n     if (check || num_m == -1) n       for (int i = start; i < answer.size(); i++) cout << answer[i]; n     else n       for (int i = start; i < num_m; i++) cout << answer[i]; n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   read_input(), solve(), write_output(); n } n


#include <bits/stdc++.h> n using namespace std; n const bool online_judge = true; n const long long inf = 1LL << 60; n long long toInt(string s) { n   long long res; n   stringstream ss; n   ss << s; n   ss >> res; n   return res; n } n string toString(long long n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n long long dx[3] = {1, 0, -1}; n long long dy[3] = {1, 0, -1}; n vector<pair<long long, long long> > statue; n bool valid(long long x, long long y, long long turn) { n   if (!(((x) >= (0) && (x) <= (7)) && ((y) >= (0) && (y) <= (7)))) return false; n   for (long long i = 0; i < ((long long)((statue).size())); i++) { n     long long sx = statue[i].first + turn; n     long long sy = statue[i].second; n     if ((sx == x && sy == y) || (sx + 1 == x && sy == y)) return false; n   } n   return true; n } n bool solve(long long x, long long y, long long turn) { n   if (turn == 8) return true; n   for (long long i = 0; i < 3; i++) { n     for (long long j = 0; j < 3; j++) { n       long long nx = x + dx[i]; n       long long ny = y + dy[j]; n       if (valid(nx, ny, turn) && solve(nx, ny, turn + 1)) { n         return true; n       } n     } n   } n   return false; n } n void run() { n   statue.clear(); n   for (long long i = 0; i < 8; i++) { n     string s; n     cin >> s; n     for (long long j = 0; j < 8; j++) { n       if (s[j] ==  S ) statue.emplace_back(i, j); n     } n   } n   bool ans = solve(7, 0, 0); n   if (ans) n     cout <<  WIN  << endl; n   else n     cout <<  LOSE  << endl; n } n int main(int argc, char *argv[]) { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n int const M = 1010000; n int const N = 10100; n int n; n int len[N]; n int x[N]; n int y[N]; n int sq[M]; n int perm[N]; n double angle[N]; n int resx[N]; n int resy[N]; n void filllens() { n   std::fill(sq, sq + M, -1); n   for (int i = 0; i * i < M; ++i) { n     sq[i * i] = i; n   } n   int last = 0; n   int sum = 0; n   for (int i = n - 1; i >= 0; --i) { n     while (x[i] == 0 && y[i] == 0) { n       len[i] = ++last; n       x[i] = 0; n       y[i] = 0; n       for (int q = 0; q * q <= len[i]; ++q) { n         if (sq[len[i] - q * q] != -1) { n           x[i] = q; n           y[i] = sq[len[i] - q * q]; n           break; n         } n       } n     } n     sum += len[i]; n   } n   if (sum % 2 == 1) { n     int xx = 0; n     int yy = 0; n     while (xx == 0 && yy == 0) { n       ++last; n       for (int q = 0; q * q <= last; ++q) { n         if (sq[last - q * q] != -1) { n           xx = q; n           yy = sq[last - q * q]; n           break; n         } n       } n     } n     int i = 0; n     while (len[i] % 2 == last % 2) ++i; n     len[i] = last; n     x[i] = xx; n     y[i] = yy; n     for (; i > 0; --i) { n       std::swap(len[i], len[i - 1]); n       std::swap(x[i], x[i - 1]); n       std::swap(y[i], y[i - 1]); n     } n   } n } n void solve() { n   filllens(); n   int sumx = 0; n   int sumy = 0; n   std::function<int(int, int)> len2 = [](int x, int y) { n     return x * x + y * y; n   }; n   for (int i = 0; i < n; ++i) { n     int ax = x[i]; n     int ay = y[i]; n     for (int it = 0; it < 2; ++it) { n       for (int px : {-1, 1}) { n         for (int py : {-1, 1}) { n           if (len2(sumx + ax, sumy + ay) > n               len2(sumx + px * x[i], sumy + py * y[i])) { n             ax = px * x[i]; n             ay = py * y[i]; n           } n         } n       } n       std::swap(x[i], y[i]); n     } n     x[i] = ax; n     y[i] = ay; n     sumx += ax; n     sumy += ay; n   } n   assert(sumx == 0 && sumy == 0); n   for (int i = 0; i < n; ++i) { n     perm[i] = i; n     angle[i] = atan2(x[i], y[i]); n   } n   std::sort(perm, perm + n, [](int i, int j) { return angle[i] > angle[j]; }); n   sumx = 0; n   sumy = 0; n   for (int i = 0; i < n; ++i) { n     resx[i] = sumx; n     resy[i] = sumy; n     sumx += x[perm[i]]; n     sumy += y[perm[i]]; n   } n } n void out() { n   puts( YES ); n   for (int i = 0; i < n; ++i) { n     printf( %d %d n , resx[i], resy[i]); n   } n } n int main() { n   scanf( %d , &n); n   solve(); n   out(); n } n


#include <bits/stdc++.h> n using namespace std; n int _; n long long a1, a2, b1, b2, c1, c2; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2; n   if ((c2 - b2) * (c1 - a1) == (c2 - a2) * (c1 - b1)) { n     cout <<  No ; n   } else if ((a1 - b1) * (a1 - b1) + (a2 - b2) * (a2 - b2) == n              (b1 - c1) * (b1 - c1) + (b2 - c2) * (b2 - c2)) { n     cout <<  Yes ; n   } else n     cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = ~0u >> 1; n int n, m, a, ans; n int b[100015], p[100015]; n bool check(int x) { n   int sum = 0; n   ans = 0; n   for (int i = 0; i < x; i++) { n     sum += max(p[x - i - 1] - b[i], 0), ans += p[x - i - 1]; n     if (sum > a) break; n   } n   if (sum <= a) return true; n   return false; n } n int main() { n   scanf( %d %d %d , &n, &m, &a); n   for (int i = 0; i < n; i++) scanf( %d , &b[i]); n   for (int i = 0; i < m; i++) scanf( %d , &p[i]); n   sort(b, b + n, greater<int>()); n   sort(p, p + m); n   int l = 0, r = min(n, m), res = 0, anser = 0; n   while (l <= r) { n     int mid = l + (r - l) / 2; n     if (check(mid)) { n       res = mid, anser = ans; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   printf( %d %d n , res, max(anser - a, 0)); n   return 0; n } n


#if !defined(ONLINE_JUDGE) n     #include  prettyprint.hpp   n     #include  debug.h  n #else n     #include <bits/stdc++.h> n     using namespace std; n     #define deb(...) 1729 n     #define dbg(...) 29 n     #define cer(x) 13 n #endif n const bool testcase = 1; n typedef long long int ll; n const ll mod1 = 1000000007; n const ll mod2 = 998244353; n  n #define fast ios_base::sync_with_stdio(0);cin.tie(NULL)  n #define test int T_T = 1; if(testcase){cin>>T_T;}while(T_T--) n #define all(ve) ve.begin(),ve.end() n  n const ll N = 1e7 + 7; n std::vector<ll> spf, prime; n std::vector<bool> isp; n void sieve(ll MAXN) {  n     spf.resize(MAXN); n     spf[1] = 1;  n     for (int i=2; i<MAXN; i++)  n         spf[i] = i;  n     for (int i=4; i<MAXN; i+=2)  n         spf[i] = 2;  n  n     for (int i=3; i*i<MAXN; i++)  n     {  n         if (spf[i] == i)  n         {  n             for (int j=i*i; j<MAXN; j+=i)  n             { n                 if (spf[j]==j)  n                     spf[j] = i;  n             } n         }  n     }  n }  n void linearSieve(ll n) { n     spf.resize(n); n     isp.resize(n,true); n     isp[0] = isp[1] = 0; n     isp[2] = 1; n     for (int i = 2; i <= n; ++i) { n         if(isp[i]) { n             prime.push_back(i); n             spf[i] = i; n         } n         for (int j = 0; j < prime.size() && prime[j] <= spf[i] && prime[j] * i <= N; ++j) { n             isp[i*prime[j]] = 0; n             spf[i*prime[j]] = prime[j]; n         } n     } n } n map<ll,ll> getFactor(ll x) {  n     map<ll,ll> ret; n     while (x != 1) {  n         ret[spf[x]]++; n         x = x / spf[x];  n     }  n     return ret;  n }  n // SOLVE n void solve() { n     // cout << N <<   n ; n     // dbg(spf); n     ll n, k; n     cin >> n >> k; n     std::vector<ll> a(n); n     for (int i = 0; i < n; ++i) { n         cin >> a[i]; n         // dbg(a[i]); n         map<ll,ll> pf = getFactor(a[i]); n         // dbg(pf); n         ll cur = 1; n         for(auto [u, v]: pf) { n             if(v & 1) { n                cur = cur * u;  n             } n         } n         a[i] = cur; n     } n     set<ll> st; n     ll ans = 0; n     for (int i = 0; i < n; ++i) { n         if(st.find(a[i]) != st.end()) { n             ans++; n             st.clear(); n         } n         st.insert(a[i]); n         // while(!st.count()) n     } n     if(st.size()) { n         ans++; n     } n     cout << ans <<   n ; n } n  n //MAIN n int main(){ n  n     fast; n     #ifndef ONLINE_JUDGE n         // for getting input from input.txt n         freopen( input.txt ,  r , stdin); n         // for writing output to output.txt n         freopen( output.txt ,  w , stdout); n         // for debug in error.txt n         freopen( error.txt ,  w , stderr); n     #endif n  n     sieve(N); n     test { n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int question, r, speed, st, en, len, did; n double lft, rht, middle, dist, p; n inline double euclid_dist(double a, double b) { return sqrt(a * a + b * b); } n inline bool check(double dis) { n   p = 1.0 * dis / r; n   return r * euclid_dist(1 - cos(p), sin(p)) >= len - dis; n } n int main() { n   scanf( %d%d%d , &question, &r, &speed); n   for (register int i = 1; i <= question; i++) { n     scanf( %d%d , &st, &en); n     len = en - st; n     lft = 0; n     rht = len; n     did = 0; n     while (lft < rht && did <= 50) { n       middle = (lft + rht) / 2.0; n       switch (check(middle)) { n         case true: n           dist = middle; n           rht = middle; n           break; n         case false: n           lft = middle; n           break; n       } n       did++; n     } n     dist /= speed; n     printf( %0.12lf n , dist); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXP = 18; n const int MAXN = 1000001; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int n, p; n string cad; n int freq[MAXP]; n int val[1 << MAXP]; n int vis[1 << MAXP]; n vector<pair<int, int> > lista[1 << MAXP]; n bool mat[MAXP][MAXP]; n bool invalid[1 << MAXP]; n bool marked[MAXP][MAXP][1 << MAXP]; n int mapea(int i) { return cad[i] -  a ; } n int dfs(int u) { n   if (vis[u]) return val[u]; n   vis[u] = 1; n   int ans = 0; n   for (int j = 0; j < p; j++) { n     if (u & 1 << j) continue; n     int v = u | 1 << j; n     if (!invalid[v]) ans = max(ans, dfs(v) + freq[j]); n   } n   return val[u] = ans; n } n void compute(int a, int b) { n   int id = 0, last = -1, mask; n   while (id < n) { n     if (mapea(id) == b && last != -1 && mask) { n       lista[mask].push_back(pair<int, int>(a, b)); n       mask = 0; n       last = -1; n     } n     if (mapea(id) == a) { n       last = id; n       mask = 0; n     } n     if (mapea(id) != a && mapea(id) != b) mask |= 1 << mapea(id); n     id++; n   } n } n void bfs(int mask, int a, int b) { n   queue<int> q; n   q.push(mask); n   invalid[mask] = 1; n   marked[a][b][mask] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < p; i++) { n       if (i == a || i == b) continue; n       int v = u | 1 << i; n       if (!marked[a][b][v]) { n         invalid[v] = 1; n         marked[a][b][v] = 1; n         q.push(v); n       } n     } n   } n   invalid[(1 << p) - 1] = 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> p; n   cin >> cad; n   for (int i = 0; i < n; i++) freq[mapea(i)]++; n   for (int i = 0; i < p; i++) n     for (int j = 0; j < p; j++) { n       cin >> mat[i][j]; n       if (!mat[i][j]) compute(i, j); n     } n   for (int i = 0; i < 1 << p; i++) n     for (int j = 0; j < lista[i].size(); j++) n       bfs(i, lista[i][j].first, lista[i][j].second); n   cout << n - dfs(0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ch[101]; n int main() { n   int n, pos; n   cin >> n; n   for (int i = 0; i < n / 2; i++) { n     cin >> ch[i]; n   } n   sort(ch, ch + (n / 2)); n   int black = 0; n   pos = 1; n   for (int i = 0; i < n / 2; i++) { n     black += abs(ch[i] - pos); n     pos += 2; n   } n   int white = 0; n   pos = 2; n   for (int i = 0; i < n / 2; i++) { n     white += abs(ch[i] - pos); n     pos += 2; n   } n   cout << min(white, black) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   char s[200000], c[200000]; n   cin >> s; n   int x = k / 2, r = k / 2; n   if (k == n) { n     cout << s; n     return 0; n   } n   for (int i = 0, j = 0; i < n; ++i) { n     if (s[i] ==  ( ) { n       if (x > 0) c[j++] =  ( ; n       --x; n     } else { n       if (r > 0) c[j++] =  ) ; n       --r; n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long re() { n   long long x; n   scanf( %lld , &x); n   return x; n } n int main() { n   long long y1 = re(), x1 = re(), y2 = re(), x2 = re(); n   long long h = (x2 - x1) / 2 + 1; n   cout << h * (((y2 - y1 + 1) + 1) / 2) + (h - 1) * ((y2 - y1 + 1) / 2) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using li = long long; n int get_num(string s) { n   istringstream is(s); n   int x; n   if ((is >> x) && to_string(x) == s) return x; n   return -1; n } n void solve(bool read) { n   int n; n   cin >> n; n   vector<pair<string, int>> s(n); n   int n_e = 0; n   for (int i = 0; i < n; i++) { n     cin >> s[i].first >> s[i].second; n     if (s[i].second) n_e++; n   } n   set<string> non_e, non_t, non_num_e, non_num_t; n   set<int> e_slots, t_slots; n   for (int i = 1; i <= n_e; i++) e_slots.insert(i); n   for (int i = n_e + 1; i <= n; i++) t_slots.insert(i); n   set<string> all_s; n   for (int i = 0; i < n; i++) { n     int num = get_num(s[i].first); n     all_s.insert(s[i].first); n     if (s[i].second == 1) { n       if (num >= 1 && num <= n_e) { n         e_slots.erase(num); n         continue; n       } n       if (num > n_e && num <= n) { n         non_e.insert(s[i].first); n         t_slots.erase(num); n       } else n         non_num_e.insert(s[i].first); n     } else { n       if (num > n_e && num <= n) { n         t_slots.erase(num); n         continue; n       } n       if (num >= 1 && num <= n_e) { n         non_t.insert(s[i].first); n         e_slots.erase(num); n       } else n         non_num_t.insert(s[i].first); n     } n   } n   string rnd =   ; n   while (true) { n     rnd =   ; n     for (int i = 0; i < 6; i++) rnd +=  a  + rand() % 26; n     if (!all_s.count(rnd)) break; n   } n   vector<string> answer; n   if (e_slots.empty() && t_slots.empty()) { n     assert(non_num_t.empty() && non_num_e.empty()); n     if (!non_t.empty()) { n       assert(!non_e.empty()); n       string test = *non_t.begin(); n       non_t.erase(non_t.begin()); n       e_slots.insert(get_num(test)); n       answer.push_back( move   + test +     + rnd); n       non_num_t.insert(rnd); n     } n   } n   if (!e_slots.empty() || !t_slots.empty()) { n     while (!e_slots.empty() || !t_slots.empty()) { n       if (!e_slots.empty()) { n         if (!non_e.empty()) { n           answer.push_back( move   + *non_e.begin() +     + n                            to_string(*e_slots.begin())); n           t_slots.insert(get_num(*non_e.begin())); n           non_e.erase(non_e.begin()); n           e_slots.erase(e_slots.begin()); n           continue; n         } n         if (!non_num_e.empty()) { n           answer.push_back( move   + *non_num_e.begin() +     + n                            to_string(*e_slots.begin())); n           non_num_e.erase(non_num_e.begin()); n           e_slots.erase(e_slots.begin()); n           continue; n         } n       } n       if (!t_slots.empty()) { n         if (!non_t.empty()) { n           answer.push_back( move   + *non_t.begin() +     + n                            to_string(*t_slots.begin())); n           e_slots.insert(get_num(*non_t.begin())); n           non_t.erase(non_t.begin()); n           t_slots.erase(t_slots.begin()); n           continue; n         } n         if (!non_num_t.empty()) { n           answer.push_back( move   + *non_num_t.begin() +     + n                            to_string(*t_slots.begin())); n           non_num_t.erase(non_num_t.begin()); n           t_slots.erase(t_slots.begin()); n           continue; n         } n       } n     } n     assert(non_num_e.empty() && non_num_t.empty() && non_t.empty() && n            non_e.empty()); n   } n   printf( %d n , (int)answer.size()); n   for (string s : answer) printf( %s n , s.c_str()); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int nt = 1; n   for (int i = 0; i < nt; i++) solve(true); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tot, n, q, u, v, z, a, b, c, p, k; n long long dep[200005], f[200005][25], fa[200005], va[200005]; n struct edge { n   long long from, head, nxt, val, go; n } e[200005 * 4]; n long long find(long long x) { n   long long o = x; n   while (x != fa[x]) x = fa[x] = fa[fa[x]]; n   while (o != x) { n     long long j = fa[o]; n     fa[o] = x; n     o = j; n   } n   return x; n } n void add(long long u, long long v, long long z) { n   e[++tot].nxt = e[u].head; n   e[u].head = tot; n   e[tot].from = u; n   e[tot].go = v; n   e[tot].val = z; n } n void deal_first(long long u, long long father) { n   dep[u] = dep[father] + 1; n   for (int i = 0; i <= 19; i++) f[u][i + 1] = f[f[u][i]][i]; n   for (long long i = e[u].head; i; i = e[i].nxt) { n     long long v = e[i].go, w = e[i].val; n     if (v == father) continue; n     if (w == 1) fa[v] = find(u); n     va[v] = w; n     f[v][0] = u; n     deal_first(v, u); n   } n } n long long lca(long long u, long long v) { n   if (dep[u] < dep[v]) swap(u, v); n   for (long long i = 17; i >= 0; i--) { n     if (dep[f[u][i]] >= dep[v]) u = f[u][i]; n     if (u == v) return u; n   } n   for (long long i = 17; i >= 0; i--) { n     if (f[u][i] != f[v][i]) { n       u = f[u][i]; n       v = f[v][i]; n     } n   } n   return f[u][0]; n } n int main() { n   scanf( %lld%lld , &n, &q); n   for (int i = 1; i < n; i++) { n     scanf( %lld%lld%lld , &u, &v, &z); n     add(u, v, z); n     add(v, u, z); n   } n   for (int i = 1; i <= n; i++) fa[i] = i; n   deal_first(1, 0); n   while (q--) { n     scanf( %lld , &k); n     if (k == 1) { n       scanf( %lld%lld%lld , &a, &b, &c); n       long long finish = lca(a, b); n       while (dep[a] > dep[finish] && c) { n         c = c / va[a]; n         a = find(f[a][0]); n       } n       while (dep[b] > dep[finish] && c) { n         c = c / va[b]; n         b = find(f[b][0]); n       } n       printf( %lld n , c); n     } else { n       scanf( %lld%lld , &p, &c); n       long long u = e[p << 1].from, v = e[p << 1].go; n       if (f[u][0] == v) n         z = u; n       else n         z = v; n       va[z] = c; n       if (va[z] == 1) fa[z] = find(f[z][0]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, p, c = 0; n   cin >> n >> k; n   vector<int> v; n   map<int, int> m; n   for (int i = 1; i <= n; i++) { n     cin >> p; n     v.push_back(p); n     m[p]++; n   } n   if (m[k] == 0) v.push_back(k), c++, n++; n   sort(v.begin(), v.end()); n   while (1) { n     int x = (n + 1) / 2; n     if (v[x - 1] == k) { n       cout << c; n       return 0; n     } else if (v[x - 1] < k) { n       v.push_back(100000); n       n++; n       c++; n     } else n       v.push_back(1), n++, c++; n     sort(v.begin(), v.end()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int grid[500][500]; n int srow[500][500]; n int scircle[500][500]; n int rmaxcircle[500][500 + 1]; n int cmaxcircle[500][500 + 1]; n int width[((500 - 1) / 2) + 1]; n int separation[2 * ((500 - 1) / 2) + 1]; n int skip_whitespace() { n   int ch; n   while (true) { n     ch = getchar_unlocked(); n     if (ch !=     && ch !=   n ) break; n   } n   return ch; n } n template <typename T> n T read_unsigned_integer() { n   T result = (T)0; n   int ch = skip_whitespace(); n   while (ch >=  0  && ch <=  9 ) { n     result = 10 * result + (ch -  0 ); n     ch = getchar_unlocked(); n   } n   return result; n } n template <typename T> n T read_signed_integer() { n   T result = (T)0; n   bool flip = false; n   int ch = skip_whitespace(); n   if (ch ==  - ) { n     flip = true; n     ch = skip_whitespace(); n   } n   while (ch >=  0  && ch <=  9 ) { n     result = 10 * result + (ch -  0 ); n     ch = getchar_unlocked(); n   } n   return flip ? -result : result; n } n int partial_row_sum(int row, int left, int right) { n   return srow[row][right + 1] - srow[row][left]; n } n long long mul_ll(long long x, long long y) { return x * y; } n int solve_problem() { n   int n, m, r; n   n = read_unsigned_integer<int>(); n   m = read_unsigned_integer<int>(); n   r = read_unsigned_integer<int>(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) grid[i][j] = read_unsigned_integer<int>(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) srow[i][j + 1] = srow[i][j] + grid[i][j]; n   width[0] = r; n   for (int i = 1; i <= r; i++) { n     int w = width[i - 1]; n     while (i * i + w * w > r * r) --w; n     width[i] = w; n   } n   for (int i = 0; i <= 2 * r; i++) { n     int limit = min(i / 2, r); n     for (int j = max(0, i - r); j <= limit; j++) { n       int cand = width[j] + width[i - j] + 1; n       if (cand > separation[i]) separation[i] = cand; n     } n   } n   for (int i = r; i < n - r; i++) n     for (int j = r; j < m - r; j++) n       for (int k = 0; k <= r; k++) { n         scircle[i][j] += partial_row_sum(i + k, j - width[k], j + width[k]); n         if (k > 0) n           scircle[i][j] += partial_row_sum(i - k, j - width[k], j + width[k]); n       } n   for (int i = r; i < n - r; i++) n     for (int j = m - r - 1; j >= r; j--) n       if (scircle[i][j] <= rmaxcircle[i][j + 1]) { n         rmaxcircle[i][j] = rmaxcircle[i][j + 1]; n         cmaxcircle[i][j] = cmaxcircle[i][j + 1]; n         if (rmaxcircle[i][j] == scircle[i][j]) ++cmaxcircle[i][j]; n       } else { n         rmaxcircle[i][j] = scircle[i][j]; n         cmaxcircle[i][j] = 1; n       } n   int result = 0; n   long long mult = 0; n   for (int i = r; i < n - r; i++) n     for (int j = i + 2 * r + 1; j < n - r; j++) { n       int cand = rmaxcircle[i][r] + rmaxcircle[j][r]; n       if (cand > result) { n         result = cand; n         mult = mul_ll(cmaxcircle[i][r], cmaxcircle[j][r]); n       } else if (cand == result) { n         mult += mul_ll(cmaxcircle[i][r], cmaxcircle[j][r]); n       } n     } n   for (int i = r; i < n - r; i++) n     for (int j = r; j < m - r; j++) { n       int limit = min(i + 2 * r + 1, n - r); n       for (int k = max(i - 2 * r, r); k < limit; k++) { n         int delta = k - i; n         if (delta < 0) delta = -delta; n         int l = j + separation[delta]; n         if (l < m - r) { n           int cand = scircle[i][j] + rmaxcircle[k][l]; n           if (cand > result) { n             result = cand; n             mult = cmaxcircle[k][l]; n           } else if (cand == result) { n             mult += cmaxcircle[k][l]; n           } n         } n       } n     } n   cout << result <<     << mult << endl; n   return 0; n } n int main() { n   solve_problem(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n int n, l[N], t[N]; n long long r, dv, res, tcur; n vector<long long> ans; n void solve(int l, int t) { n   if (2 * l <= t) { n     tcur += 2 * l; n     return; n   } n   long long x = t - l; n   long long tmp = (1ll * l - x + r - 1) / r; n   res += tmp; n   dv = x + tmp * r - l; n   tcur += 2 * x; n   long long t1 = tcur; n   while (ans.size() < 1e5 + 10 && tmp) { n     ans.push_back(t1); n     tmp--; n     t1 += r; n   } n   tcur += l - x; n } n int main() { n   cin >> n >> r; n   for (int i = 1; i <= n; i++) cin >> l[i]; n   for (int i = 1; i <= n; i++) cin >> t[i]; n   res = 0; n   dv = 0; n   tcur = 0; n   for (int i = 1; i <= n; i++) { n     if (l[i] > t[i]) { n       res = -1; n       break; n     } n     if (l[i] <= dv) { n       dv -= l[i]; n       tcur += l[i]; n       continue; n     } n     l[i] -= dv; n     t[i] -= dv; n     tcur += dv; n     dv = 0; n     solve(l[i], t[i]); n   } n   cout << res << endl; n   if (res != -1 && ans.size() <= 1e5) { n     assert(res == ans.size()); n     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 228; n const int big = 2e9; n const int md = 1e9 + 7; n struct node { n   int l, r, s; n }; n int n, m, a[N], pr[N], root[N], k, tex, jj; n node t[N * 40]; n bool ok; n void build(int v, int l, int r) { n   if (l == r) return; n   t[v].l = ++k; n   t[v].r = ++k; n   int d = (l + r) >> 1; n   build(t[v].l, l, d); n   build(t[v].r, d + 1, r); n } n void update(int v, int l, int r, int ps, int vl) { n   if (l == r) { n     t[++k].s = vl; n     return; n   } n   int d = (l + r) >> 1; n   if (ps <= d) { n     update(t[v].l, l, d, ps, vl); n     t[++k].l = k - 1; n     t[k].r = t[v].r; n   } else { n     update(t[v].r, d + 1, r, ps, vl); n     t[++k].r = k - 1; n     t[k].l = t[v].l; n   } n   t[k].s = t[t[k].l].s + t[t[k].r].s; n } n void mod(int v, int l, int r, int ps, int vl) { n   if (l == r) { n     t[v].s = vl; n     return; n   } n   int d = (l + r) >> 1; n   if (ps <= d) n     mod(t[v].l, l, d, ps, vl); n   else n     mod(t[v].r, d + 1, r, ps, vl); n   t[v].s = t[t[v].l].s + t[t[v].r].s; n } n int go(int v, int l, int r) { n   if (l == r) return l; n   int d = (l + r) >> 1; n   if (t[t[v].l].s >= tex) n     return go(t[v].l, l, d); n   else { n     tex -= t[t[v].l].s; n     return go(t[v].r, d + 1, r); n   } n } n void nx(int v, int tl, int tr, int l, int r) { n   if (l > r || ok) return; n   if (tl == l && tr == r) { n     if (tex <= t[v].s) { n       ok = 1; n       jj = go(v, l, r); n     } else n       tex -= t[v].s; n     return; n   } n   int d = (tl + tr) >> 1; n   nx(t[v].l, tl, d, l, min(r, d)); n   nx(t[v].r, d + 1, tr, max(l, d + 1), r); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   k = 1; n   root[n + 1] = 1; n   build(1, 1, n); n   for (int i = n; i >= 1; i--) { n     update(root[i + 1], 1, n, i, 1); n     if (pr[a[i]] != 0) update(k, 1, n, pr[a[i]], 0); n     root[i] = k; n     pr[a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     int j = 1, ans = 0; n     ok = 1; n     while (j <= n && ok) { n       ans++; n       tex = i + 1; n       ok = 0; n       nx(root[j], 1, n, j, n); n       j = jj; n     } n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int test; n   cin >> test; n   while (test--) { n     int n; n     cin >> n; n     for (int i = 2 * n, j = 0; j < n; i += 2, ++j) { n       cout << i <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5 + 10; n int le[MAXN], ri[MAXN]; n int a[MAXN]; n struct num { n   int val, pos; n }; n num data[MAXN]; n bool cmp(num a, num b) { return a.val < b.val; } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     data[i].val = a[i]; n     data[i].pos = i; n     le[i] = i - 1; n     ri[i] = i + 1; n   } n   sort(data, data + n, cmp); n   long long ans = 0; n   int low = 0; n   int hi = n - 1; n   for (int i = 0; i < n - 2; i++) { n     int pos = data[i].pos; n     if (low == pos) { n       ans += a[pos]; n       low = ri[pos]; n     } else if (hi == pos) { n       ans += a[pos]; n       hi = le[pos]; n     } else { n       ans += min(a[le[pos]], a[ri[pos]]); n       ri[le[pos]] = ri[pos]; n       le[ri[pos]] = le[pos]; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SZ = 1000010; n const long long INF = 1e18 + 10; n const int mod = 1e9 + 7; n const long double eps = 1e-12; n long long read() { n   long long n = 0; n   char a = getchar(); n   bool flag = 0; n   while (a >  9  || a <  0 ) { n     if (a ==  - ) flag = 1; n     a = getchar(); n   } n   while (a <=  9  && a >=  0 ) { n     n = n * 10 + a -  0 , a = getchar(); n   } n   if (flag) n = -n; n   return n; n } n int a[SZ], s[SZ], b[SZ]; n int main() { n   int n = read(), m = read(), sum = 0; n   for (int i = 1; i <= m; i++) a[i] = read(), b[i] = (sum += a[i]) % n; n   printf( %d n , (sum + n - 1) / n); n   b[m] = n; n   sort(b + 1, b + 1 + m); n   for (int i = 1; i <= m; i++) s[i] = b[i] - b[i - 1]; n   for (int i = 1; i <= m; i++) printf( %d  , s[i]); n   puts(  ); n   int k = 1; n   for (int i = 1, j = 1; i <= m; i++) n     while (a[i] > 0) { n       a[i] -= s[j]; n       printf( %d  , i); n       if (k % m == 0) puts(  ); n       k++; n       j++, j = (j - 1) % m + 1; n     } n   while ((k - 1) % m) k++, printf( 1  ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int INF = (int)1e9; n const long long LINF = (long long)1e18; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return a > val ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline long long isqrt(long long k) { n   long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n const int maxn = 100010; n const int magic = 320; n int fac[maxn]; n int rfac[maxn]; n int f[maxn]; n int p25[maxn]; n int binomial(int a, int b) { n   if (!a || a == b) { n     return 1; n   } n   return mult(fac[b], mult(rfac[a], rfac[b - a])); n } n void solve() { n   fac[0] = 1; n   for (int i = (1); i < (maxn); i++) fac[i] = mult(fac[i - 1], i); n   for (int i = (0); i < (maxn); i++) rfac[i] = inv(fac[i]); n   p25[0] = 1; n   for (int i = (1); i < (maxn); i++) p25[i] = mult(p25[i - 1], 25); n   int m, k; n   cin >> m; n   string s; n   cin >> s; n   if ((k = int((s).size())) > magic) memset(f, -1, sizeof(f)); n   while (m--) { n     int op; n     cin >> op; n     if (op == 1) { n       string s; n       cin >> s; n       if ((k = int((s).size())) > magic) memset(f, -1, sizeof(f)); n     } else { n       int n; n       cin >> n; n       if (n < k) { n         cout << 0 <<   n ; n         continue; n       } n       if (k > magic && ~f[n]) { n         cout << f[n] <<   n ; n         continue; n       } n       int res = fpow(26, n); n       for (int i = (0); i < (k); i++) { n         submod(res, mult(binomial(i, n), p25[n - i])); n       } n       if (k > magic) f[n] = res; n       cout << res <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int MXN = 100010; n bool alive[MXN * 5]; n int L[MXN], R[MXN], T[MXN]; n struct heap_t { n   int idx; n   heap_t(int nidx) : idx(nidx) {} n   bool operator<(const heap_t& cmp) const { return T[idx] > T[cmp.idx]; } n }; n struct ele_t { n   int idx, val; n   bool operator<(const ele_t& cmp) const { return val < cmp.val; } n } ele[MXN * 2], ele2[MXN * 5]; n int ans[MXN]; n int main() { n   int n, m; n   scanf( %d %d , &m, &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d %d , &L[i], &R[i], &T[i]); n     ele[i].idx = i, ele[i].val = L[i]; n     ele[i + n].idx = i, ele[i + n].val = R[i]; n   } n   int cele2 = m; n   for (int i = 1; i <= m; i++) { n     ele2[i].idx = i; n     scanf( %d , &ele2[i].val); n   } n   int lstPos = -1; n   std::priority_queue<heap_t> heap; n   std::sort(ele + 1, ele + n * 2 + 1); n   for (int i = 1; i <= n * 2; i++) { n     while (!heap.empty() && !alive[heap.top().idx]) heap.pop(); n     if (!heap.empty()) { n       ++cele2; n       ele2[cele2].idx = cele2; n       ele2[cele2 + 1].idx = cele2; n       ele2[cele2].val = T[heap.top().idx] - lstPos; n       ele2[cele2 + 1].val = T[heap.top().idx] - ele[i].val; n       ++cele2; n     } n     lstPos = ele[i].val; n     alive[ele[i].idx] = !alive[ele[i].idx]; n     if (alive[ele[i].idx]) heap.push(ele[i].idx); n   } n   int sum = 0, cnt = 0; n   std::sort(ele2 + 1, ele2 + cele2 + 1); n   for (int i = 1; i <= cele2; i++) { n     sum += cnt * (ele2[i].val - lstPos); n     lstPos = ele2[i].val; n     if (ele2[i].idx <= m) n       ans[ele2[i].idx] = sum; n     else { n       alive[ele2[i].idx] = !alive[ele2[i].idx]; n       cnt += (alive[ele2[i].idx]) ? 1 : -1; n     } n   } n   for (int i = 1; i <= m; i++) printf( %d%c , ans[i], i == m ?   n  :    ); n } n


#include <bits/stdc++.h> n const long long INF = (long long)1e18 + 18; n const long long maxn = (long long)1e6 + 6; n using namespace std; n long long n; n signed main() { n   cin >> n; n   long long a[n], c[n], b[maxn]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     long long inp; n     cin >> inp; n     b[inp] = i + 1; n   } n   for (long long i = 0; i < n; i++) { n     c[i] = b[a[i]]; n   } n   long long fine = 0, maxx = c[0]; n   for (long long i = 0; i < n; i++) { n     if (c[i] < maxx) { n       fine++; n     } n     maxx = max(maxx, c[i]); n   } n   cout << fine; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T>; n template <typename T> n using dpair = pair<T, T>; n template <class T1, class T2> n istream &operator>>(istream &is, pair<T1, T2> &p) { n   return is >> p.first >> p.second; n } n template <class T1, class T2> n void operator+=(vector<T1> &v, const T2 &obj) { n   v.push_back(obj); n } n template <class T1> n void operator+=(vector<T1> &v, const T1 &obj) { n   v.push_back(obj); n } n long long get_millis() { n   using namespace std::chrono; n   return duration_cast<milliseconds>(system_clock::now().time_since_epoch()) n       .count(); n } n const int INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n class xrange { n  public: n   xrange(int end) : _start(0), _end(end), _step(1) {} n   xrange(int start, int end) : _start(start), _end(end), _step(1) {} n   xrange(int start, int end, int step) n       : _start(start), _end(end), _step(step) {} n   class iterator { n    public: n     iterator(int v, xrange &par) : val(v), parent(par){}; n     int operator*() const { return val; } n     int operator++() { return (val += parent._step); } n     bool operator==(const xrange::iterator &iter) const { n       if (&parent != &iter.parent) return false; n       if (val == iter.val) return true; n       if (parent._step > 0) n         return val >= parent._end && iter.val >= parent._end; n       else n         return val <= parent._end && iter.val <= parent._end; n     } n     bool operator!=(const xrange::iterator &iter) const { n       return !(this->operator==(iter)); n     } n  n    private: n     int val; n     xrange &parent; n   }; n   xrange::iterator begin() { return {_start, *this}; } n   xrange::iterator end() { return {_end, *this}; } n  n  protected: n   int _start, _end, _step; n  n  private: n }; n struct irange : public xrange { n   irange(int i) : xrange(1, i + 1) {} n   irange(int i, int j) : xrange(i, j + 1) {} n }; n const int MAXN = 1e4 + 5; n namespace std { n template <> n struct hash<dpair<int>> { n   size_t operator()(dpair<int> p) const { n     return p.first * 123456789 + p.second; n   } n }; n }  // namespace std n unordered_map<dpair<int>, int> ump; n int N, M, K; n struct dir { n   bool lit; n   int r, c; n   int dist; n   dir(int _l, int _r, int _c, int _d) { n     lit = _l; n     r = _r; n     c = _c; n     dist = _d; n   } n   bool operator>(const dir &d) const { return dist > d.dist; } n }; n minheap<dir> pq; n set<int> availR[MAXN], availC[MAXN]; n int best[MAXN]; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cin >> N >> M >> K; n   for (int a = 0; a < K; a++) { n     int r, c; n     cin >> r >> c; n     availR[r].insert(c); n     availC[c].insert(r); n     ump[{r, c}] = a; n   } n   pq.push({false, 1, 1, 0}); n   pq.push({true, 1, 1, 1}); n   availR[1].erase(1); n   availC[1].erase(1); n   memset(best, 0x3f, sizeof(best)); n   best[ump[{1, 1}]] = 0; n   while (!pq.empty()) { n     dir top = pq.top(); n     pq.pop(); n     if (top.lit) { n       for (int r = max(1, top.r - 2); r <= top.r + 2; r++) { n         for (int i : availR[r]) { n           int id = ump[{r, i}]; n           best[id] = min(best[id], top.dist); n           pq.push(dir{false, r, i, top.dist}); n           pq.push(dir{true, r, i, top.dist + 1}); n         } n         availR[r].clear(); n       } n       for (int c = max(1, top.c - 2); c <= top.c + 2; c++) { n         for (int i : availC[c]) { n           int id = ump[{i, c}]; n           best[id] = min(best[id], top.dist); n           pq.push(dir{false, i, c, top.dist}); n           pq.push(dir{true, i, c, top.dist + 1}); n         } n         availC[c].clear(); n       } n     } else { n       int dr = 1, dc = 0; n       if (ump.count({top.r + dr, top.c + dc}) && n           best[ump[{top.r + dr, top.c + dc}]] > top.dist) { n         best[ump[{top.r + dr, top.c + dc}]] = top.dist; n         pq.push(dir{false, top.r + dr, top.c + dc, top.dist}); n         pq.push(dir{true, top.r + dr, top.c + dc, top.dist + 1}); n         availR[top.r + dr].erase(top.c + dc); n         availC[top.c + dc].erase(top.r + dr); n       } n       dr = -1; n       dc = 0; n       if (ump.count({top.r + dr, top.c + dc}) && n           best[ump[{top.r + dr, top.c + dc}]] > top.dist) { n         best[ump[{top.r + dr, top.c + dc}]] = top.dist; n         pq.push(dir{false, top.r + dr, top.c + dc, top.dist}); n         pq.push(dir{true, top.r + dr, top.c + dc, top.dist + 1}); n         availR[top.r + dr].erase(top.c + dc); n         availC[top.c + dc].erase(top.r + dr); n       } n       dr = 0; n       dc = 1; n       if (ump.count({top.r + dr, top.c + dc}) && n           best[ump[{top.r + dr, top.c + dc}]] > top.dist) { n         best[ump[{top.r + dr, top.c + dc}]] = top.dist; n         pq.push(dir{false, top.r + dr, top.c + dc, top.dist}); n         pq.push(dir{true, top.r + dr, top.c + dc, top.dist + 1}); n         availR[top.r + dr].erase(top.c + dc); n         availC[top.c + dc].erase(top.r + dr); n       } n       dr = 0; n       dc = -1; n       if (ump.count({top.r + dr, top.c + dc}) && n           best[ump[{top.r + dr, top.c + dc}]] > top.dist) { n         best[ump[{top.r + dr, top.c + dc}]] = top.dist; n         pq.push(dir{false, top.r + dr, top.c + dc, top.dist}); n         pq.push(dir{true, top.r + dr, top.c + dc, top.dist + 1}); n         availR[top.r + dr].erase(top.c + dc); n         availC[top.c + dc].erase(top.r + dr); n       } n     } n   } n   int ans = INF; n   for (auto au : ump) { n     if (au.first == dpair<int>{N, M}) { n       ans = min(ans, best[au.second]); n     } n     if (abs(au.first.first - N) <= 1 || abs(au.first.second - M) <= 1) { n       ans = min(ans, best[au.second] + 1); n     } n   } n   if (ans > 10000000) { n     cout << -1; n   } else { n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 10010; n int f[2][N], g[2][N], a[N], ans[N], b[N], pr[N]; n inline int qpow(long long a, int b, const int p) { n   int ret = 1; n   a %= p; n   while (b) { n     ret = ret * 1ll * (b & 1 ? a : 1) % p; n     a = a * a % p, b >>= 1; n   } n   return ret; n } n int main() { n   auto _add = [&](int &x, int y) { n     x += y; n     if (x >= mod) x -= mod; n   }; n   int n; n   scanf( %d , &n); n   a[1] = 1; n   b[1] = 1; n   const int inv_2 = mod - mod / 2; n   for (int i = 2; i <= n; i++) scanf( %d , &a[i]), b[i] = a[i]; n   for (int i = 2; i <= n; i++) b[i] = b[i - 1] * 1ll * b[i] % mod; n   for (int i = n; i >= 2; i--) pr[i] = (pr[i + 1] + b[i]) % mod; n   for (int i = 1; i <= n; i++) { n     f[i & 1][0] = g[i & 1][0] = 1; n     for (int j = 1; j <= n * 2 - 2; j++) { n       f[i & 1][j] = f[i & 1][j - 1] * 1ll * a[i + j] % mod; n       g[i & 1][j] = 0; n       if (j >= 2) n         _add(g[i & 1][j], f[i & 1][j - 2] * 1ll * (a[i] - 1 + mod) % mod); n       _add(g[i & 1][j], g[(i - 1) & 1][j - 1]); n       _add(ans[j], g[i & 1][j] * 1ll * b[i] % mod); n     } n   } n   for (int i = 1; i <= 2 * n - 2; i++) { n     if (i <= n - 1) { n       ans[i] = n           ((ans[i] - pr[1 + i] + mod) % mod * 1ll * inv_2 % mod + pr[1 + i]) % n           mod; n     } else n       ans[i] = ans[i] * 1ll * inv_2 % mod; n   } n   for (int i = 1; i <= 2 * n - 2; i++) n     printf( %d%c , ans[i], i == 2 * n - 2 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9 + 1; n const int MOD = 1e9 + 7; n const int N = 1e6 + 1; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m; n   cin >> n >> m; n   if (m == 1) { n     cout <<  100003 100003  << endl; n     cout <<  1 2 100003  << endl; n     return 0; n   } n   cout <<  100003 100003  << endl; n   for (int i = 1; i < n - 1; i++) { n     cout << i <<     << i + 1 <<   1  << endl; n     m--; n   } n   cout << n - 1 <<     << n <<     << 100003 - n + 2 << endl; n   m--; n   long long int z = 1000000000; n   for (int i = 1; i < n && m > 0; i++) { n     for (int j = i + 2; j <= n && m > 0; j++) { n       cout << i <<     << j <<     << z << endl; n       m--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double memo[2002][2002]; n double p; n int main() { n   int n, t; n   cin >> n >> p >> t; n   double ans = 0; n   memo[0][0] = 1.0; n   for (int per = 1; per <= n; per++) n     for (int s = 1; s <= t; s++) n       memo[per][s] = memo[per - 1][s - 1] * p + memo[per][s - 1] * (1.0 - p); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= t; j++) ans += memo[i][j]; n   printf( %lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n void *wmem; n char memarr[10485760]; n template <class T> n inline void walloc1d(T **arr, int x, void **mem = &wmem) { n   static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; n   (*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]); n   (*arr) = (T *)(*mem); n   (*mem) = ((*arr) + x); n } n inline void rd(int &x) { n   int k; n   int m = 0; n   x = 0; n   for (;;) { n     k = getchar(); n     if (k ==  - ) { n       m = 1; n       break; n     } n     if ( 0  <= k && k <=  9 ) { n       x = k -  0 ; n       break; n     } n   } n   for (;;) { n     k = getchar(); n     if (k <  0  || k >  9 ) { n       break; n     } n     x = x * 10 + k -  0 ; n   } n   if (m) { n     x = -x; n   } n } n inline void wt_L(char a) { putchar(a); } n inline void wt_L(int x) { n   int s = 0; n   int m = 0; n   char f[10]; n   if (x < 0) { n     m = 1; n     x = -x; n   } n   while (x) { n     f[s++] = x % 10; n     x /= 10; n   } n   if (!s) { n     f[s++] = 0; n   } n   if (m) { n     putchar( - ); n   } n   while (s--) { n     putchar(f[s] +  0 ); n   } n } n struct unionFind { n   int *d; n   int N; n   int M; n   inline void malloc(const int n) { n     d = (int *)std::malloc(n * sizeof(int)); n     M = n; n   } n   inline void free(void) { std::free(d); } n   inline void walloc(const int n, void **mem = &wmem) { n     walloc1d(&d, n, mem); n     M = n; n   } n   inline void init(const int n) { n     int i; n     N = n; n     for (i = (0); i < (n); i++) { n       d[i] = -1; n     } n   } n   inline void init(void) { init(M); } n   inline int get(int a) { n     int t = a; n     int k; n     while (d[t] >= 0) { n       t = d[t]; n     } n     while (d[a] >= 0) { n       k = d[a]; n       d[a] = t; n       a = k; n     } n     return a; n   } n   inline int connect(int a, int b) { n     if (d[a] >= 0) { n       a = get(a); n     } n     if (d[b] >= 0) { n       b = get(b); n     } n     if (a == b) { n       return 0; n     } n     if (d[a] < d[b]) { n       d[a] += d[b]; n       d[b] = a; n     } else { n       d[b] += d[a]; n       d[a] = b; n     } n     return 1; n   } n   inline int operator()(int a) { return get(a); } n   inline int operator()(int a, int b) { return connect(a, b); } n   inline int &operator[](const int a) { return d[a]; } n   inline int size(int a) { n     a = get(a); n     return -d[a]; n   } n   inline int sizeList(int res[]) { n     int i; n     int sz = 0; n     for (i = (0); i < (N); i++) { n       if (d[i] < 0) { n         res[sz++] = -d[i]; n       } n     } n     return sz; n   } n }; n int N; n int M; n int Q; n int X; n int Y; n int C; n int res[2000000 + 2]; n int mp[300][300]; n vector<int> tmm[2000000 + 1]; n vector<int> type[2000000 + 1]; n vector<int> place[2000000 + 1]; n int main() { n   wmem = memarr; n   int i; n   int j; n   int k; n   int c; n   int t; n   int x; n   int y; n   int nx; n   int ny; n   int d; n   int dx[4] = {-1, 1, 0, 0}; n   int dy[4] = {0, 0, -1, 1}; n   unionFind uf; n   rd(N); n   rd(M); n   rd(Q); n   for (i = (0); i < (N * M); i++) { n     tmm[0].push_back(0); n     type[0].push_back(1); n     place[0].push_back(i); n   } n   for (i = (0); i < (Q); i++) { n     rd(X); n     X += (-1); n     rd(Y); n     Y += (-1); n     rd(C); n     if (mp[X][Y] == C) { n       continue; n     } n     k = mp[X][Y]; n     tmm[k].push_back(i + 1); n     type[k].push_back(-1); n     place[k].push_back(X * M + Y); n     k = mp[X][Y] = C; n     tmm[k].push_back(i + 1); n     type[k].push_back(1); n     place[k].push_back(X * M + Y); n   } n   for (i = (0); i < (N * M); i++) { n     k = mp[i / M][i % M]; n     tmm[k].push_back(Q + 1); n     type[k].push_back(-1); n     place[k].push_back(i); n   } n   uf.walloc(N * M); n   for (k = (0); k < (2000000 + 1); k++) { n     if (tmm[k].size()) { n       for (i = (0); i < (N); i++) { n         for (j = (0); j < (M); j++) { n           mp[i][j] = 0; n         } n       } n       uf.init(N * M); n       t = c = 0; n       for (i = (0); i < (tmm[k].size()); i++) { n         if (tmm[k][i] != t) { n           res[t] += c; n           t = tmm[k][i]; n           res[t] -= c; n         } n         if (type[k][i] == -1) { n           break; n         } n         x = place[k][i] / M; n         y = place[k][i] % M; n         mp[x][y] = 1; n         c++; n         for (d = (0); d < (4); d++) { n           nx = x + dx[d]; n           ny = y + dy[d]; n           if (nx < 0 || ny < 0 || nx >= N || ny >= M) { n             continue; n           } n           if (mp[nx][ny] == 1) { n             c -= uf(x * M + y, nx * M + ny); n           } n         } n       } n       for (i = (0); i < (N); i++) { n         for (j = (0); j < (M); j++) { n           mp[i][j] = 0; n         } n       } n       uf.init(N * M); n       t = Q + 1; n       c = 0; n       for (i = (tmm[k].size()) - 1; i >= (0); i--) { n         if (type[k][i] == 1) { n           break; n         } n         if (tmm[k][i] != t) { n           res[t] -= c; n           t = tmm[k][i]; n           res[t] += c; n         } n         x = place[k][i] / M; n         y = place[k][i] % M; n         mp[x][y] = 1; n         c++; n         for (d = (0); d < (4); d++) { n           nx = x + dx[d]; n           ny = y + dy[d]; n           if (nx < 0 || ny < 0 || nx >= N || ny >= M) { n             continue; n           } n           if (mp[nx][ny] == 1) { n             c -= uf(x * M + y, nx * M + ny); n           } n         } n       } n     } n   } n   for (i = (1); i < (Q + 1); i++) { n     res[i] += res[i - 1]; n     wt_L(res[i]); n     wt_L(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, val; n   int arr[3] = {0}, count = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> val; n     arr[val]++; n   } n   if (arr[2] <= arr[1]) { n     count += arr[2]; n     arr[1] -= arr[2]; n     count += (arr[1] / 3); n   } else { n     count += arr[1]; n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     set<int> se; n     for (int i = 0; i < n; i++) { n       int va; n       cin >> va; n       se.insert(va & 1); n     } n     if (se.size() == 1) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010, mod = 998244353; n inline long long read() { n   char ch = getchar(); n   long long x = 0, f = 0; n   while (ch <  0  || ch >  9 ) f |= ch ==  - , ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n int n, m, dfn[maxn], low[maxn], stk[maxn], tp, cnt, id[maxn], deg[maxn], scnt, n     el, head[maxn], to[maxn], nxt[maxn], ans, q[maxn], h = 1, r; n bool ins[maxn]; n vector<int> vec[maxn]; n inline int ask(int u, int v) { n   printf( ? %d %d n , u, v); n   fflush(stdout); n   return read(); n } n inline void add(int u, int v) { n   to[++el] = v; n   nxt[el] = head[u]; n   head[u] = el; n } n void dfs(int u) { n   dfn[u] = low[u] = ++cnt; n   stk[++tp] = u; n   ins[u] = true; n   for (int i = head[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (!dfn[v]) n       dfs(v), low[u] = min(low[u], low[v]); n     else if (ins[v]) n       low[u] = min(low[u], dfn[v]); n   } n   if (low[u] == dfn[u]) { n     scnt++; n     do { n       int v = stk[tp]; n       ins[v] = false; n       id[v] = scnt; n       vec[scnt].push_back(v); n     } while (stk[tp--] != u); n   } n } n int main() { n   n = read(); n   m = read(); n   for (int i = (1); i <= (m); i++) { n     int u = read(), v = read(); n     add(u, v); n   } n   for (int i = (1); i <= (n); i++) n     if (!dfn[i]) dfs(i); n   for (int u = (1); u <= (n); u++) n     for (int i = head[u]; i; i = nxt[i]) n       if (id[u] != id[to[i]]) deg[id[to[i]]]++; n   for (int i = (1); i <= (scnt); i++) n     if (!deg[i]) q[++r] = i; n   ans = vec[q[h]].back(); n   while (h != r) { n     int &at = q[h + 1]; n     int u = vec[at].back(); n     if (!ask(ans, u)) swap(u, ans), swap(q[h], q[h + 1]); n     for (int i = head[u]; i; i = nxt[i]) n       if (id[u] != id[to[i]] && !--deg[id[to[i]]]) q[++r] = id[to[i]]; n     vec[at].pop_back(); n     if (vec[at].empty()) swap(q[h], q[h + 1]), h++; n   } n   printf( ! %d n , ans); n   fflush(stdout); n } n


#include<bits/stdc++.h> n using namespace std; n const int N=1e5+5; n int tot; n int head[N],to[N*2],next1[N*2]; n void lian(int x,int y) n { n     to[++tot]=y; n     next1[tot]=head[x]; n     head[x]=tot; n } n int d[N],k; n struct qw{ n     int a,b,c,d; n }e[N]; n struct er{ n     int a,b; n }jie[N]; n void dfs(int x,int fa) n { n     jie[x].a=x,jie[x].b=0; n     for(int i=head[x];i;i=next1[i]) n     { n         int v=to[i]; n         if(v==fa) continue; n         d[x]++; n         dfs(v,x); n     } n     if(d[x]==0) return; n     int v1=-1; n     for(int i=head[x];i;i=next1[i]) n     { n         int v=to[i]; n         if(v==fa) continue; n         if(jie[v].b==0) n         { n             if(v1==-1) jie[x].a=jie[v].a; n             else if(jie[x].b==0) jie[x].b=jie[v].a; n             else e[++k]=(qw){v,x,v,jie[x].a},jie[x].a=jie[v].a; n             v1=0; n              n         } n     } n     if(v1==-1) n     { n         for(int i=head[x];i;i=next1[i]) n         { n             int v=to[i]; n             if(v==fa) continue; n             v1=v; n             e[++k]=(qw){v,x,x,jie[v].a}; n             jie[x].a=jie[v].b; n             break; n         } n     } n     for(int i=head[x];i;i=next1[i]) n     { n         int v=to[i]; n         if(v==fa||jie[v].b==0||v==v1) continue; n         if(jie[v].a&&jie[v].b) e[++k]=(qw){v,x,jie[v].a,jie[x].a},jie[x].a=jie[v].b; n         else e[++k]=(qw){v,x,jie[x].a,v},jie[x].a=jie[v].a; n     } n } n int main() n { n     int T; n     cin>>T; n     while(T--) n     { n         int n; n         cin>>n; n         int x,y; n         for(int i=1;i<=n-1;i++) n         { n             scanf( %d %d ,&x,&y); n             lian(x,y),lian(y,x); n         } n         k=0; n         dfs(1,0); n         printf( %d n ,k); n         for(int i=1;i<=k;i++) n         printf( %d %d %d %d n ,e[i].a,e[i].b,e[i].c,e[i].d); n         tot=0; n         for(int i=1;i<=n;i++) head[i]=d[i]=0; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a[100100]; n int main() { n   scanf( %lld %lld , &n, &k); n   int temp = 0, t = 0; n   vector<int> v; n   for (int i = 1; i <= n; i++) { n     long long x; n     scanf( %lld , &x); n     if (temp + (i - n) * x * 1ll * (i - t - 1) < k) { n       v.push_back(i); n       t++; n     } else n       temp = temp + x * 1ll * (i - t - 1); n   } n   for (int i = 0; i < v.size(); i++) printf( %d n , v[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[1001]; n   int b[1001] = {0}; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[a[i]] = 1; n   } n   sort(a, a + n); n   int ans = 0; n   int sum = a[n - 1] - a[0] + 1; n   cout << sum - n << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010; n int n, m, k, u, v, x, y, t, a, b, ans; n string A[5]; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   for (int i = 0; i < 5; i++) { n     cin >> A[i]; n     if (A[i][0] == s[0] || A[i][1] == s[1]) return cout <<  YES  <<   n , 0; n   } n   return cout <<  NO  <<   n , 0; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n int n, a, b, c, ans = 0; n int main() { n   cin >> n >> a >> b >> c; n   for (int i = 0; i <= a; i += 2) n     for (int j = 0; j < (int)(b + 1); j++) { n       int k = n - (i * 0.5 + j); n       if (k < 0) break; n       if (!k) { n         ans++; n         continue; n       } n       if (k % 2 == 0) { n         int l = k / 2; n         if (l <= c) ans++; n       } n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int M = 1e6 + 10; n const long long INF = 0x3f3f3f3f3f3f3f; n const int inf = 0x3f3f3f; n const double esp = 1e-6; n long long read() { n   long long x = 0; n   long long k = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) k = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + (c -  0 ); n     c = getchar(); n   } n   return x * k; n } n int n; n int a[N]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if (a == 1) { n       if (b <= 1) { n         cout <<  YES  << endl; n       } else n         cout <<  NO  << endl; n     } else if (a == 2) { n       if (b <= 3) { n         cout <<  YES  << endl; n       } else n         cout <<  NO  << endl; n     } else if (a == 3) { n       if (b <= 3) { n         cout <<  YES  << endl; n       } else n         cout <<  NO  << endl; n     } else n       cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Ask(int x, int y) { n   printf( ? %d %d n , x, y); n   fflush(stdout); n   int t; n   scanf( %d , &t); n   return t; n } n int vis[105], t[105], n; n struct bt { n   bitset<105> A; n   int id; n   int operator[](int x) { return A[x]; } n   friend bool operator<(bt x, bt y) { n     for (int i = n; ~i; i--) n       if (x[i] ^ y[i]) return x[i] < y[i]; n     return 0; n   } n   int mx() { n     for (int i = n; ~i; i--) n       if (A[i]) return i; n     return -1; n   } n } dp[105]; n map<pair<int, int>, int> val; n priority_queue<bt> q; n vector<int> e[105]; n bool chk(vector<bt> f) { n   for (auto x : f) q.push(x); n   if (q.empty()) return 1; n   for (int i = n; ~i; i--) n     if (t[i]) { n       bt x = q.top(); n       q.pop(); n       int v = x.mx(); n       if (v > i) n         break; n       else if (v == i) n         x.A[v] = 0, q.emplace(x); n       if (!q.size()) return 1; n     } n   while (q.size()) q.pop(); n   return 0; n } n void produce(int u, vector<bt> f) { n   for (auto x : f) q.push(x); n   if (q.empty()) return; n   for (int i = n; ~i; i--) n     if (t[i]) { n       bt x = q.top(); n       q.pop(); n       int v = x.mx(); n       if (v == i) n         x.A[v] = 0, q.emplace(x); n       else n         val[pair<int, int>(x.id, u)] = val[pair<int, int>(u, x.id)] = i; n       if (!q.size()) break; n     } n   while (q.size()) q.pop(); n } n void dfs1(int u, int fa) { n   vector<bt> g; n   dp[u].id = u; n   for (int v : e[u]) n     if (v != fa) dfs1(v, u), g.push_back(dp[v]); n   fill(t, t + n, 1); n   t[n] = 0; n   for (int i = n; ~i; i--, t[i] = 0) n     if (!chk(g)) t[i] = 1; n   for (int i = 0; i <= n; i++) dp[u].A[i] = t[i]; n   produce(u, g); n } n pair<int, int> id; n void dfs2(int u, int fa, int &mx) { n   for (int v : e[u]) n     if (!vis[v] && v != fa) { n       int k = val[pair<int, int>(u, v)]; n       if (k > mx) mx = k, id = pair<int, int>(u, v); n       dfs2(v, u, mx); n     } n } n void solve(int u, int fa) { n   int mx = -1; n   dfs2(u, fa, mx); n   if (mx == -1) printf( ! %d n , u), fflush(stdout), exit(0); n   int res = Ask(id.first, id.second); n   if (res == id.first) n     vis[id.second] = 1, solve(id.first, id.second); n   else n     vis[id.first] = 1, solve(id.second, id.first); n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; i++) n     scanf( %d%d , &u, &v), e[u].emplace_back(v), e[v].emplace_back(u); n   dfs1(1, 0); n   solve(1, 0); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... T> n inline void inp(T &...args) { n   ((cin >> args), ...); n } n template <typename T> n inline istream &operator>>(istream &in, vector<T> &a) { n   for (auto &x : a) in >> x; n   return in; n } n template <typename T, typename U> n inline istream &operator>>(istream &in, pair<T, U> &a) { n   in >> a.first >> a.second; n   return in; n } n const long long int MX = 1e6 + 10; n const long long int Mod = 998244353; n inline long long int md(long long int x, long long int M = Mod) { n   x %= M; n   return ((x < 0) ? (x + M) : x); n } n vector<long long int> fact(MX), invfact(MX), inv(MX); n void init_fact(long long int m = Mod) { n   inv[0] = 0; n   inv[1] = fact[0] = fact[1] = invfact[0] = invfact[1] = 1; n   for (long long int i = 2; i < MX; i++) { n     fact[i] = md(fact[i - 1] * i, m); n     inv[i] = m - (m / i) * inv[m % i] % m; n     invfact[i] = md(inv[i] * invfact[i - 1], m); n   } n } n long long int NPR(long long int n, long long int r) { n   if (0 <= r and r <= n) { n     return md(fact[n] * invfact[n - r]); n   } n   return 0; n } n long long int NCR(long long int n, long long int r) { n   if (0 <= r and r <= n) { n     return md(fact[n] * md(invfact[r] * invfact[n - r])); n   } n   return 0; n } n void solve(int &T) { n   long long int n, k; n   cin >> n >> k; n   vector<pair<long long int, long long int>> a; n   for (long long int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) { n     long long int x, y; n     cin >> x >> y; n     a.push_back({x, 1}); n     a.push_back({y + 1, -1}); n   } n   sort((a).begin(), (a).end()); n   long long int cnt = 0, ans = 0; n   for (long long int i = (0); ((1) > 0 ? i < (2 * n) : i > (2 * n)); i += (1)) { n     if (a[i].second == 1) { n       if (cnt >= k - 1) { n         ans = md(ans + NCR(cnt, k - 1)); n       } n       cnt++; n     } else { n       cnt--; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   init_fact(); n   int t = 1; n   for (int i = 1; i <= t; i++) { n     if (0) cerr <<  Case #  << i <<   n ; n     solve(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long a[100005]; n long long sum1[100005], sum2[100005]; n int main() { n   memset(sum1, 0, sizeof(sum1)); n   memset(sum2, 0, sizeof(sum2)); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     sum1[i] = sum1[i - 1] + a[i]; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; ++i) { n     sum2[i] = sum2[i - 1] + a[i]; n   } n   cin >> m; n   for (int i = 0; i < m; ++i) { n     int q, l, r; n     cin >> q >> l >> r; n     if (q == 1) { n       cout << sum1[r] - sum1[l - 1] << endl; n     } else { n       cout << sum2[r] - sum2[l - 1] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n bool fun(long long a); n using namespace std; n int main() { n   long long num, num1, num2; n   cin >> num; n   if (num % 2 == 0) { n     num1 = num / 2; n     num2 = num / 2; n     while (!fun(num1) || !fun(num2)) { n       num1 -= 1; n       num2 += 1; n     } n   } else { n     num1 = num / 2; n     num2 = num / 2 + 1; n     while (!fun(num1) || !fun(num2)) { n       num1 -= 1; n       num2 += 1; n     } n   } n   cout << num1 <<     << num2; n } n bool fun(long long a) { n   int c = 0; n   for (int i = 2; i < a; i++) { n     if (a % i == 0) { n       c = 1; n       break; n     } n   } n   return c == 1; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tab[1000001]; n void draw() { n   for (int i = 0; i < n; i++) { n     cout << tab[i] <<    ; n   } n   cout << endl; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> tab[i]; n   } n   sort(tab, tab + n); n   if (n % 2 == 1) { n     cout << tab[n / 2] << endl; n   } else { n     cout << tab[n / 2 - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read(T& x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x = f ? -x : x; n } n const int N = 3e5 + 10; n const int INF = 2000000000; n int n, k, i, ans, minx, maxx, miny, maxy, x[N], y[N]; n int main() { n   read(n); n   minx = miny = INF, maxx = maxy = -INF; n   for (i = 0; i < n; ++i) { n     read(x[i]), read(y[i]); n     minx = min(minx, x[i]); n     miny = min(miny, y[i]); n     maxx = max(maxx, x[i]); n     maxy = max(maxy, y[i]); n   } n   for (ans = -INF, i = 0; i < n; ++i) { n     ans = max(ans, 2 * (maxx - x[i]) + 2 * (maxy - y[i])); n     ans = max(ans, 2 * (maxx - x[i]) + 2 * (y[i] - miny)); n     ans = max(ans, 2 * (x[i] - minx) + 2 * (maxy - y[i])); n     ans = max(ans, 2 * (x[i] - minx) + 2 * (y[i] - miny)); n   } n   printf( %d%c , ans, n == 3 ?   n  :    ); n   for (i = 4; i <= n; ++i) n     printf( %d%c , 2 * (maxx - minx + maxy - miny), i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 1; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n long long ln, lk, lm; n vector<int> mp[100005]; n int r1, r2, fa[100005], s[100005], no[100005], cnt, d[100005], w[100005]; n bool vis[100005]; n int bfs(int x) { n   queue<int> q; n   q.push(x); n   fa[x] = 0; n   vis[x] = 1; n   while (!q.empty()) { n     x = q.front(); n     q.pop(); n     for (int c : mp[x]) n       if (!vis[c]) { n         fa[c] = x; n         vis[c] = 1; n         q.push(c); n       } n   } n   for (int(i) = 0; (i) < (int)(100005); (i)++) vis[i] = 0; n   return x; n } n void dfs(int x, int dp, int fa, int col) { n   d[x] = dp; n   no[x] = col; n   w[col] = max(w[col], dp); n   for (int c : mp[x]) n     if (c != fa && !vis[c]) { n       dfs(c, dp + 1, x, col); n     } n } n int T[2][100005][20]; n void init() { n   for (int(i) = 1; (i) <= (int)(cnt); (i)++) { n     T[0][i][0] = w[i] + i; n     T[1][i][0] = w[i] - i; n   } n   for (int j = 1; (1 << j) <= cnt; j++) { n     for (int i = 1; i + (1 << j) - 1 <= cnt; i++) { n       for (int(x) = 0; (x) < (int)(2); (x)++) n         T[x][i][j] = max(T[x][i][j - 1], T[x][i + (1 << (j - 1))][j - 1]); n     } n   } n } n int qym(int l, int r, int p) { n   if (l > r) return -(1 << 30); n   int x = log(r - l + 1) / log(2); n   int y = max(T[p][l][x], T[p][r - (1 << x) + 1][x]); n   return y; n } n int main() { n   scanf( %d , &n); n   for (int i = 0, u, v; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     mp[u].push_back(v); n     mp[v].push_back(u); n   } n   r1 = bfs(1); n   r2 = bfs(r1); n   for (int(i) = 0; (i) < (int)(100005); (i)++) vis[i] = 0; n   for (int i = r2; i; i = fa[i]) { n     vis[i] = 1; n     s[++cnt] = i; n     no[i] = cnt; n   } n   for (int(i) = 1; (i) <= (int)(cnt); (i)++) dfs(s[i], 0, 0, i); n   init(); n   scanf( %d , &m); n   for (int i = 0, x, y, ans; i < m; i++) { n     scanf( %d%d , &x, &y); n     int l = no[x], r = no[y]; n     if (r < l) { n       swap(l, r); n       swap(x, y); n     } n     if (l == r) { n       ans = max(l - 1, cnt - r) + min(d[x], d[y]); n     } else { n       int mid2 = l + r - d[x] + d[y]; n       if (mid2 <= l * 2) { n         ans = d[y] + max(r - 1, cnt - r); n       } else if (mid2 >= r * 2) { n         ans = d[x] + max(l - 1, cnt - l); n       } else { n         int mid = mid2 / 2; n         int L = max(l - 1, qym(l + 1, mid, 0) - l) + d[x]; n         int R = max(cnt - r, qym(mid + 1, r - 1, 1) + r) + d[y]; n         ans = max(L, R); n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace fio { n streambuf* in = cin.rdbuf(); n char bb[1000000], *s = bb, *t = bb; n inline long long read() { n   long long x = 0; n   char ch = n       (s == t && (t = (s = bb) + in->sgetn(bb, 1000000), s == t) ? EOF : *s++); n   while (ch < 48) n     ch = (s == t && (t = (s = bb) + in->sgetn(bb, 1000000), s == t) ? EOF n                                                                     : *s++); n   while (ch >= 48) n     x = x * 10 + ch - 48, n     ch = (s == t && (t = (s = bb) + in->sgetn(bb, 1000000), s == t) ? EOF n                                                                     : *s++); n   return x; n } n }  // namespace fio n using fio::read; n long long n; n int m, llm; n int lim[26]; n vector<int> road[26]; n map<array<int, 26>, int> huaji; n array<int, 26> pm; n class mat { n  public: n   int val[125][125]; n   int* operator[](int x) { return val[x]; } n   mat operator*(mat _) { n     mat ans; n     for (int i = 1; i <= llm; i++) n       for (int u = 1; u <= llm; u++) { n         ans[i][u] = 0; n         for (int j = 1; j <= llm; j++) n           ans[i][u] = (ans[i][u] + val[i][j] * _[j][u]) % 12345; n       } n     return ans; n   } n }; n mat ksm(mat x, long long y) { n   mat ans = x; n   y--; n   for (; y; y >>= 1, x = x * x) n     if (y & 1) ans = ans * x; n   return ans; n } n mat base; n int ans; n int main() { n   n = read(), m = read(); n   if (!n) return cout << 1, 0; n   if (!m) return cout << 0, 0; n   for (int i = 1, x, y; i <= m; i++) n     x = read() +  0  -  A , road[x].emplace_back(y = read()), n     lim[x] = max(lim[x], 1) * road[x].back(); n   while (pm[25] < max(lim[25], 1)) { n     huaji[pm] = ++llm, pm[0]++; n     for (int i = 0; i < 25; i++) n       if (lim[i]) n         pm[i + 1] += pm[i] / lim[i], pm[i] %= lim[i]; n       else n         pm[i + 1] += pm[i], pm[i] = 0; n   } n   for (pair<array<int, 26>, int> it : huaji) n     for (int i = 0; i < 26; i++) n       if (lim[i]) n         it.first[i] = (it.first[i] + 1) % lim[i], n         base[it.second][huaji[it.first]]++, n         it.first[i] = (it.first[i] - 1 + lim[i]) % lim[i]; n   base = ksm(base, n); n   for (pair<array<int, 26>, int> it : huaji) { n     bool flag = 1; n     for (int i = 0, fx = 0; flag && i < 26; i++, flag &= fx, fx = 0) n       if (!lim[i]) n         fx = 1; n       else n         for (int p : road[i]) fx |= !(it.first[i] % p); n     flag ? ans = (ans + base[1][it.second]) % 12345 : ans; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<set<int> > > setgrid; n vector<string> grid; n int k; n int ro[4] = {1, -1, 0, 0}; n int co[4] = {0, 0, -1, 1}; n struct s { n   s() : r(-1), c(-1), set(-1), path(  ), let( A ), d(0) {} n   s(int nr, int nc, unsigned int nset, string npath, char nlet, int nd) n       : r(nr), c(nc), set(nset), path(npath), let(nlet), d(nd) {} n   int r, c, d; n   unsigned int set; n   string path; n   char let; n }; n bool operator<(const s &lhs, const s &rhs) { n   if (lhs.d == rhs.d) n     if (lhs.path == rhs.path) n       return lhs.let >= rhs.let; n     else n       return lhs.path >= rhs.path; n   else n     return lhs.d >= rhs.d; n } n bool visited(int r, int c, unsigned int set) { n   unsigned int subset = set; n   do { n     subset = (subset - 1) & set; n     if (setgrid[r][c].find(subset) != setgrid[r][c].end()) { n       return true; n     } n   } while (subset != set); n   return false; n } n int main(void) { n   s qf; n   int rs, cs, nr, nc; n   unsigned int nset, one = 0x00000001; n   priority_queue<s> q; n   vector<s> pv, tv; n   cin >> rs >> cs >> k; n   grid.resize(rs); n   for (int i = 0; i < rs; i++) cin >> grid[i]; n   setgrid.resize(rs); n   for (int i = 0; i < rs; i++) setgrid[i].resize(cs); n   int sr, sc; n   for (int i = 0; i < rs; i++) n     for (int j = 0; j < cs; j++) n       if (grid[i][j] ==  S ) { n         sr = i; n         sc = j; n         break; n       } n   q.push(s(sr, sc, 0,   ,  a , 0)); n   setgrid[sr][sc].insert(0); n   while (!q.empty()) { n     pv.clear(); n     qf = q.top(); n     for (int i = 0; i < 4; i++) { n       nr = qf.r + ro[i]; n       nc = qf.c + co[i]; n       if (nr < 0 || nr >= rs || nc < 0 || nc >= cs) continue; n       if (grid[nr][nc] ==  T ) goto success; n       nset = qf.set | (one << int(grid[nr][nc] -  a )); n       if ((__builtin_popcount(nset) > k) || visited(nr, nc, nset)) continue; n       pv.push_back( n           s(nr, nc, nset, qf.path + grid[nr][nc], grid[nr][nc], qf.d + 1)); n       setgrid[nr][nc].insert(nset); n     } n     sort(pv.begin(), pv.end()); n     for (int i = 0; i < pv.size(); i++) q.push(pv[i]); n     q.pop(); n   } n   cout << -1 <<   n ; n   return 0; n success: n   if (qf.path !=   ) cout << qf.path <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y; n bool a[401][401]; n bool viz[401]; n int dist[401]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cerr.tie(0); n   cout.tie(0); n   cin >> n >> m; n   while (m--) { n     cin >> x >> y; n     a[x][y] = a[y][x] = 1; n   } n   queue<int> q; n   q.push(1); n   viz[1] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 1; i <= n; i++) n       if (!viz[i] && (a[u][i] ^ a[1][n])) { n         q.push(i); n         viz[i] = 1; n         dist[i] = dist[u] + 1; n       } n   } n   return cout << ((!viz[n]) ? -1 : max(1, dist[n])), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   char s[3][3]; n   bool is = false; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       cin >> s[i][j]; n       if (s[i][j] ==  X ) is = true; n     } n   } n   int i = 0, j = 0; n   bool isY = true; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) n       if (s[i][j] != s[2 - i][2 - j]) isY = false; n   } n   if (isY) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p1, p2; n double fastPow(double b, int n) { n   if (n == 0) return 1.0; n   double po = fastPow(b, n / 2); n   po = po * po; n   if (n & 1) return po * b; n   return po; n } n double proba(int turno) { n   double ans = 1; n   ans = ans * fastPow(1 - p1, turno / 2); n   ans = ans * fastPow(1 - p2, turno / 2); n   return ans * p1; n   return ans * p1; n } n int main() { n   double a, b, c, d; n   while (cin >> a >> b >> c >> d) { n     double prob = 0; n     p1 = a / b; n     p2 = c / d; n     for (int i = 1; i < 100000; i += 2) prob = prob + proba(i); n     cout << prob << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 3e5 + 5; n map<pair<int, int>, int> mp; n int counts[maxN]; n set<int> s[maxN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int N, P; n   cin >> N >> P; n   for (int i = 0; i < N; i++) { n     pair<int, int> xy; n     cin >> xy.first >> xy.second; n     xy = {min(xy.first, xy.second), max(xy.first, xy.second)}; n     if (mp.count(xy) > 0) n       mp[xy]++; n     else n       mp[xy] = 1; n     counts[xy.first]++; n     counts[xy.second]++; n     s[xy.first].insert(xy.second); n     s[xy.second].insert(xy.first); n   } n   vector<int> ordered; n   for (int i = 1; i <= N; i++) { n     ordered.push_back(counts[i]); n   } n   sort(ordered.begin(), ordered.end()); n   long long ans = 0; n   for (int i = 1; i <= N; i++) { n     int rem = P - counts[i]; n     int k = lower_bound(ordered.begin(), ordered.end(), rem) - ordered.begin(); n     int tot = N - k; n     if (counts[i] >= rem) tot--; n     for (auto it = s[i].begin(); it != s[i].end(); it++) { n       pair<int, int> ordp = {min(i, *it), max(i, *it)}; n       if (counts[i] + counts[*it] >= P && n           counts[i] + counts[*it] - mp[ordp] < P) { n         tot--; n       } n     } n     ans += tot; n   } n   cout << ans / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string second, a =  1968 ; n   string rem[10]; n   cin >> second; n   int zero = 0; n   bool one = 0, six = 0, eight = 0, nine = 0; n   string alt =   ; n   for (int i = 0; i < second.size(); i++) { n     if (second[i] ==  0 ) zero++; n     if (second[i] ==  1  && one) alt += second[i]; n     if (second[i] ==  6  && six) alt += second[i]; n     if (second[i] ==  8  && eight) alt += second[i]; n     if (second[i] ==  9  && nine) alt += second[i]; n     if (second[i] ==  1 ) n       one = 1; n     else if (second[i] ==  6 ) n       six = 1; n     else if (second[i] ==  8 ) n       eight = 1; n     else if (second[i] ==  9 ) n       nine = 1; n     else if (second[i] !=  0 ) n       alt += second[i]; n   } n   int t = 1, mo = 0; n   for (int i = 0; i < zero; i++) { n     t = (t * 10) % 7; n   } n   int ft = t; n   do { n     int m = 0; n     t = ft; n     for (int i = 4 - 1; i >= 0; i--) { n       m = (m + t * (a[i] -  0 )) % 7; n       t = (t * 10) % 7; n     } n     rem[m] = a; n   } while (next_permutation(a.begin(), a.end())); n   for (int i = alt.size() - 1; i >= 0; i--) { n     mo = (mo + (alt[i] -  0 ) * t) % 7; n     t = (t * 10) % 7; n   } n   rem[7] = rem[0]; n   cout << alt << rem[7 - mo]; n   for (int i = 0; i < zero; i++) cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5, OO = 1e14; n const int di[] = {1, -1, 0, 0}; n const int dj[] = {0, 0, 1, -1}; n long long TC, n, m, k, a, b, c, cnt, u, mx, mn, ans, arr[N]; n bool f, vis[N], mark[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> TC; n   while (TC--) { n     cin >> n >> m; n     cnt = f = 0; n     m += (2 * n); n     for (int i = 1; i <= n && !f; i++) { n       for (int j = i + 1; j <= n; j++) { n         cout << i <<     << j <<   n ; n         if (++cnt == m) { n           f = 1; n           break; n         } n       } n     } n   } n   return 0; n } n


#pragma GCC optimize( O3 ) n #pragma GCC target( avx2 ) n #include<bits/stdc++.h> n  n using namespace std; n  n #define int long long n  n signed main() { n     int t; n     cin >> t; n     for (int u = 0; u < t; ++u) { n         string s; n         cin >> s; n         int b1 = 0, b2 = 0; n         int ans = 0; n         for (char c : s) { n             if (c ==  ( ) { n                 ++b1; n             } else if (c ==  [ ) { n                 ++b2; n             } else if (c ==  )  && b1) { n                 --b1; n                 ++ans; n             } else if (c ==  ]  && b2) { n                 --b2; n                 ++ans; n             } n         } n         cout << ans <<   n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n string toString(long long n) { n   string str =   ; n   while (n) { n     str += n % 10 +  0 ; n     n /= 10; n   } n   reverse(str.begin(), str.end()); n   return str; n } n int main() { n   string s =   ; n   for (auto i = (1); i <= (10000); ++i) { n     string t = toString(i); n     s += t; n   } n   long long n; n   scanf( %lld , &n); n   if (n <= (int)s.size()) n     printf( %c n , s[n - 1]); n   else { n     n -= 9; n     long long cur = 180LL; n     int digit = 2; n     long long power = 10; n     while (n > cur) { n       n -= cur; n       power *= 10LL; n       ++digit; n       cur = 9LL * digit * power; n     } n     power += n / digit - 1; n     n %= digit; n     if (n) ++power; n     string str = toString(power); n     if (n == 0) n       cout << str[(int)str.size() - 1] << endl; n     else n       cout << str[n - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, b; n   cin >> n >> b; n   long long int a[n], i, j, k, l, t1, t2, sum = 0, ad[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     ad[i] = a[i]; n   } n   vector<pair<long long int, long long int>> v; n   map<long long int, long long int> mp; n   for (i = 0; i < b; i++) { n     cin >> t1 >> t2; n     v.push_back(make_pair(t1, t2)); n   } n   sort(v.begin(), v.end()); n   sort(a, a + n); n   i = j = sum = 0; n   for (i = 0; i < n; i++) { n     while (a[i] >= v[j].first && j < b) { n       sum = sum + v[j].second; n       j++; n     } n     mp[a[i]] = sum; n   } n   for (i = 0; i < n; i++) { n     cout << mp[ad[i]] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n; n double p[maxn], dp[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lf , &p[i]); n   double sol = 0.; n   dp[0] = 0; n   for (int i = 1; i < n; i++) { n     dp[i] = (dp[i - 1] + p[i - 1]) * p[i]; n     sol += 2. * dp[i]; n   } n   for (int i = 0; i < n; i++) sol += p[i]; n   printf( %.9lf n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int numTest; n   cin >> numTest; n   for (int num = 0; num < numTest; ++num) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; ++i) { n       cin >> arr[i]; n     } n     sort(arr, arr + n); n     int temp = arr[0], temp2 = 0; n     for (int i = 1; i < n; ++i) { n       temp2 += arr[i - 1]; n       arr[i] -= temp; n       temp += arr[i]; n       if (arr[0] < arr[i]) arr[0] = arr[i]; n     } n     cout << arr[0] << endl; n   } n } n