#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long ar[1000001], dp[1001][1001]; n long long calc(long long idx, long long rem) { n   if (rem == 0 && idx != 0) return 1; n   if (idx >= n) return 0; n   long long &b = dp[idx][rem]; n   if (b != -1) n     return b; n   else n     b = 0; n   b |= calc(idx + 1, (rem + ar[idx]) % m); n   b |= calc(idx + 1, rem); n   return b; n } n void solve() { n   cin >> n >> m; n   for (long long i = 0; i < (n); ++i) { n     cin >> ar[i]; n     ar[i] %= m; n     if (ar[i] == 0) { n       cout <<  YES n ; n       return; n     } n   } n   if (n >= m) { n     cout <<  YES n ; n     return; n   } else { n     for (long long i = 0; i < (1001); ++i) n       for (long long j = 0; j < (1001); ++j) dp[i][j] = -1; n     if (calc(0, m)) { n       cout <<  YES n ; n     } else n       cout <<  NO n ; n   } n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pow(long long a, long long b, long long MOD) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y); n       if (x > MOD) x %= MOD; n     } n     y = (y * y); n     if (y > MOD) y %= MOD; n     b /= 2; n   } n   return x; n } n long long modInverse(long long a, long long m) { return pow(a, m - 2, m); } n long long arr[(int)1e5 + 1 + 1], val[(int)1e5 + 1]; n int main() { n   long long n; n   while (cin >> n) { n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       val[i] = 1; n     } n     long long ans = 1; n     for (int i = 1; i < n; i++) { n       if (arr[i] >= arr[i - 1]) { n         val[i] = max(val[i], val[i - 1] + 1); n       } n       ans = max(ans, val[i]); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> s(2 * n); n   for (int i = 0; i < 2 * n; i++) cin >> s[i]; n   sort(s.begin(), s.end()); n   if (s[0] == s[2 * n - 1]) { n     cout << -1 <<   n ; n     return; n   } n   for (int i = 0; i < 2 * n; i++) cout << s[i] <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long int t; n   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 N = 1e5 + 10, LG = 45; n long long n, ts, Mx, B[N], S[N]; n struct Node { n   int32_t l, r, c; n   Node() { l = r = c = 0; } n }; n vector<Node> A; n inline long long Creat() { return (ts++); } n inline void Add(long long val, long long tp) { n   long long id = 0; n   A[id].c += tp; n   for (long long i = LG - 1; ~i; i--) { n     bool b = (val >> i) & 1; n     if (!A[id].l) A[id].l = Creat(); n     if (!A[id].r) A[id].r = Creat(); n     if (!b) id = A[id].l; n     if (b) id = A[id].r; n     A[id].c += tp; n   } n } n inline long long Get(long long val) { n   long long id = 0, ret = 0; n   for (long long i = LG - 1; ~i; i--) { n     if (!A[id].l) A[id].l = Creat(); n     if (!A[id].r) A[id].r = Creat(); n     bool b = (val >> i) & 1, w = 0; n     if ((!b && A[A[id].r].c) || (b && !A[A[id].l].c)) n       id = A[id].r, ret |= (1ll << i), w = 1; n     if (!w) id = A[id].l; n   } n   return (val ^ ret); n } n int32_t main() { n   scanf( %lld , &n); n   A.resize(2e7, Node()); n   Creat(); n   Add(0, 2); n   for (long long i = 1; i <= n; i++) scanf( %lld , &B[i]); n   for (long long i = n; i; i--) S[i] = S[i + 1] ^ B[i], Add(S[i], 1); n   for (long long i = 0, r = 0; i <= n; i++, r ^= B[i]) n     Mx = max(Mx, Get(r)), Add(S[i + 1], -1); n   return !printf( %lld , Mx); n } n


#include <bits/stdc++.h> n using namespace std; n int fuck[2010], de[2010], n; n char mp[2010][2010]; n int check() { n   sort(fuck + 1, fuck + 1 + n); n   int sum = 0; n   for (int i = 1; i < n; i++) { n     sum += fuck[i]; n     if (sum == i * (i - 1) / 2) return 0; n   } n   return 1; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %s , mp[i] + 1); n     for (int j = 1; j <= n; j++) { n       if (mp[i][j] ==  1 ) de[i]++, fuck[i] = de[i]; n     } n   } n   if (check()) { n     cout << 0 <<     << 1; n     return 0; n   } n   if (n <= 6) { n     int minn = 1e9, res = 0; n     for (int i = 1; i < (1 << n); i++) { n       int now = 0; n       for (int j = 1; j <= n; j++) { n         if ((1 << (j - 1)) & i) { n           now++; n           de[j] = n - 1 - de[j]; n           for (int k = 1; k <= n; k++) { n             if (k == j) continue; n             if (mp[j][k] ==  1 ) { n               mp[j][k] =  0 ; n               mp[k][j] =  1 ; n               de[k]++; n             } else { n               mp[j][k] =  1 ; n               mp[k][j] =  0 ; n               de[k]--; n             } n           } n         } n       } n       for (int j = 1; j <= n; j++) fuck[j] = de[j]; n       for (int j = 1; j <= n; j++) { n         if ((1 << (j - 1)) & i) { n           de[j] = n - 1 - de[j]; n           for (int k = 1; k <= n; k++) { n             if (k == j) continue; n             if (mp[j][k] ==  1 ) { n               mp[j][k] =  0 ; n               mp[k][j] =  1 ; n               de[k]++; n             } else { n               mp[j][k] =  1 ; n               mp[k][j] =  0 ; n               de[k]--; n             } n           } n         } n       } n       if (now > minn) continue; n       if (check()) { n         if (now < minn) n           minn = now, res = 1; n         else n           res++; n       } n     } n     if (minn == 1e9) n       cout << -1; n     else { n       for (int i = 1; i <= minn; i++) res *= i; n       cout << minn <<     << res; n     } n   } else { n     int res = 0; n     for (int j = 1; j <= n; j++) { n       de[j] = n - 1 - de[j]; n       for (int k = 1; k <= n; k++) { n         if (k == j) continue; n         if (mp[j][k] ==  1 ) { n           mp[j][k] =  0 ; n           mp[k][j] =  1 ; n           de[k]++; n         } else { n           mp[j][k] =  1 ; n           mp[k][j] =  0 ; n           de[k]--; n         } n       } n       for (int j = 1; j <= n; j++) fuck[j] = de[j]; n       de[j] = n - 1 - de[j]; n       for (int k = 1; k <= n; k++) { n         if (k == j) continue; n         if (mp[j][k] ==  1 ) { n           mp[j][k] =  0 ; n           mp[k][j] =  1 ; n           de[k]++; n         } else { n           mp[j][k] =  1 ; n           mp[k][j] =  0 ; n           de[k]--; n         } n       } n       if (check()) res++; n     } n     cout << 1 <<     << res << endl; n   } n } n


#include <bits/stdc++.h> n int n, a, b, p; n char s[51]; n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %s , &a, s); n     b = 0; n     p = a / 2; n     for (int j = 0; j < a; j++) { n       if (s[j] ==  ( ) { n         b++; n       } else { n         if (b > 0) { n           b--; n           p--; n         } n       } n     } n     printf( %d n , p); n   } n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::pair; n using std::set; n using std::sort; n using std::string; n using std::vector; n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<pair<string, int>>> byreg(m); n   for (int i = 0; i < n; i++) { n     string a; n     int b, c; n     cin >> a >> b >> c; n     byreg[b - 1].push_back(make_pair(a, c)); n   } n   for (int i = 0; i < m; i++) { n     sort(byreg[i].begin(), byreg[i].end(), n          [](pair<string, int> &l, pair<string, int> &r) { n            return l.second > r.second; n          }); n   } n   for (int i = 0; i < m; i++) { n     if (byreg[i].size() > 2) { n       if (byreg[i][0].second == byreg[i][2].second || n           byreg[i][1].second == byreg[i][2].second) { n         cout <<  ? n ; n       } else n         cout << byreg[i][0].first <<     << byreg[i][1].first <<   n ; n     } else n       cout << byreg[i][0].first <<     << byreg[i][1].first <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010; n const int INF = 100000000; n string str[maxn]; n int dp[maxn][maxn]; n int n, m, ans; n int flag[maxn][maxn]; n int dir[4][2] = {0, -1, 0, 1, 1, 0, -1, 0}; n char ch[5] = { D ,  I ,  M ,  A }; n vector<pair<int, int> > q; n bool Ok(int ki, int kj) { n   if (ki >= 0 && ki < n && kj >= 0 && kj < m) return true; n   return false; n } n int dfs(int ki, int kj, int d, int f) { n   int Ans = 1; n   for (int i = 0; i < 4; i++) { n     int ti = ki + dir[i][0]; n     int tj = kj + dir[i][1]; n     if (Ok(ti, tj) && str[ti][tj] == ch[d + 1 < 4 ? d + 1 : 0]) { n       if (flag[ti][tj] < f) { n         int k = flag[ti][tj]; n         flag[ti][tj] = f; n         if (dp[ti][tj] > 0) n           Ans = max(Ans, dp[ti][tj] + 1); n         else n           Ans = max(Ans, dfs(ti, tj, d + 1 < 4 ? d + 1 : 0, f) + 1); n         flag[ti][tj] = k; n       } else { n         Ans = INF; n         break; n       } n     } n   } n   dp[ki][kj] = Ans; n   return Ans; n } n void solve() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     cin >> str[i]; n     for (int j = 0; j < m; j++) { n       if (str[i][j] ==  D ) q.push_back(pair<int, int>(i, j)); n     } n   } n   for (int i = 0; i < q.size(); i++) { n     if (dp[q[i].first][q[i].second] == 0) n       ans = max(ans, dfs(q[i].first, q[i].second, 0, i + 1) / 4); n   } n   if (ans == 0) n     cout <<  Poor Dima!  << endl; n   else if (ans >= 2500000) n     cout <<  Poor Inna!  << endl; n   else n     cout << ans << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n std::mt19937 rng( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n const int ms = 110000; n const int bs = 1000; n const int MOD = 998244353; n void add(int &a, int b) { a = a + b < MOD ? a + b : a + b - MOD; } n int *pivot; n bool comp(int x1, int x2) { return pivot[x1] < pivot[x2]; } n int tmp[2][bs]; n struct Bucket { n   Bucket() { dirty = size = 0; } n   void push_back(int x) { n     p[size] = size; n     lazy[size] = 0; n     dp[size] = x; n     values[size++] = 0; n   } n   void clean() { n     for (int i = size - 2; i >= 0; i--) { n       add(lazy[i], lazy[i + 1]); n     } n     for (int i = 0; i < size; i++) { n       add(dp[p[i]], lazy[i]); n       lazy[i] = 0; n     } n     if (dirty) { n       for (int i = 0; i < size; i++) { n         values[i] += dirty; n       } n       dirty = 0; n     } n   } n   void sort() { n     clean(); n     pivot = values; n     std::sort(p, p + size, comp); n   } n   int getID(int x) { n     if (x < values[p[0]] + dirty) { n       return -1; n     } n     int l = 0, r = size - 1; n     while (l != r) { n       int mid = (l + r + 1) / 2; n       if (values[p[mid]] + dirty <= x) { n         l = mid; n       } else { n         r = mid - 1; n       } n     } n     return l; n   } n   void upd(int x, int l, int r, int val) { n     if (l == 0 && r == size) { n       int id = getID(x); n       if (id != -1) { n         add(lazy[id], val); n       } n     } else { n       for (int i = l; i < r; i++) { n         if (values[i] + dirty <= x) { n           add(dp[i], val); n         } else { n         } n       } n     } n   } n   void upd2(int l, int r, int val) { n     if (l == 0 && r == size) { n       dirty += val; n     } else { n       for (int i = l; i < r; i++) { n         values[i] += val; n       } n       clean(); n       pivot = values; n       int s0 = 0, s1 = 0; n       for (int i = 0; i < size; i++) { n         if (l <= p[i] && p[i] < r) { n           tmp[0][s0++] = p[i]; n         } else { n           tmp[1][s1++] = p[i]; n         } n       } n       std::merge(tmp[0], tmp[0] + s0, tmp[1], tmp[1] + s1, p, comp); n     } n   } n   int values[bs], p[bs], lazy[bs], dp[bs]; n   int dirty; n   int size; n }; n Bucket b[ms / bs + 2]; n std::vector<int> pos[ms]; n int a[ms]; n int pt[ms]; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n, k; n   std::cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     pos[i].push_back(-1); n   } n   for (int i = 0; i < n; i++) { n     std::cin >> a[i]; n     pos[a[i]].push_back(i); n     b[i / bs].push_back(i == 0 ? 1 : 0); n   } n   b[n / bs].push_back(0); n   for (int i = 1; i <= n; i++) { n     pos[i].push_back(n); n     pos[i].push_back(n); n   } n   auto upd2 = [&](int l, int r, int val) { n     if (l >= r) return; n     int bl = l / bs, br = (r - 1) / bs; n     if (bl == br) { n       b[bl].upd2(l - bl * bs, r - bl * bs, val); n       return; n     } n     b[bl].upd2(l - bl * bs, bs, val); n     for (int i = bl + 1; i < br; i++) { n       b[i].upd2(0, bs, val); n     } n     b[br].upd2(0, r - br * bs, val); n   }; n   auto upd = [&](int l, int r, int val) { n     if (l >= r) return; n     int bl = l / bs, br = (r - 1) / bs; n     if (bl == br) { n       b[bl].upd(k, l - bl * bs, r - bl * bs, val); n       return; n     } n     b[bl].upd(k, l - bl * bs, bs, val); n     for (int i = bl + 1; i < br; i++) { n       b[i].upd(k, 0, bs, val); n     } n     b[br].upd(k, 0, r - br * bs, val); n   }; n   for (int i = 1; i <= n; i++) { n     upd2(pos[i][1] + 1, pos[i][2] + 1, 1); n   } n   for (int i = 0; i < n; i++) { n     b[i / bs].clean(); n     int dp = b[i / bs].dp[i % bs]; n     upd(i + 1, n + 1, dp); n     { n       int v = a[i]; n       upd2(pos[v][pt[v] + 1] + 1, pos[v][pt[v] + 2] + 1, -1); n       pt[v]++; n       upd2(pos[v][pt[v] + 1] + 1, pos[v][pt[v] + 2] + 1, 1); n     } n   } n   b[n / bs].clean(); n   std::cout << b[n / bs].dp[n % bs] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n long long int inverse(long long int i) { n   if (i == 1) return 1; n   return (MOD - ((MOD / i) * inverse(MOD % i)) % MOD + MOD) % MOD; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long m, n, x; n   cin >> m >> n; n   cout << flush; n   long long a[n]; n   for (int i = 0; i < n; i++) { n     cout << 1 << endl; n     cout << flush; n     cin >> x; n     if (x == 0) n       exit(0); n     else if (x == 1) n       a[i] = 1; n     else n       a[i] = 0; n   } n   long long l = 2, r = m, mid, j = 0; n   while (1) { n     mid = (l + r) / 2; n     cout << mid << endl; n     cout << flush; n     cin >> x; n     if (x == 0 || x == -2) n       exit(0); n     else { n       if (x == 1) { n         if (a[j] == 1) n           l = mid + 1; n         else n           r = mid - 1; n       } else if (x == -1) { n         if (a[j] == 1) n           r = mid - 1; n         else n           l = mid + 1; n       } n     } n     j++; n     j %= n; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-8; n const int inf = 1 << 30; n const int mod = int(1e9) + 7; n const int base = 257; n const int maxn = 2005; n int n, m, d[maxn][maxn], ans = -1, sx = -1, sy = -1; n char c[maxn][maxn]; n int get(int xa, int ya, int xb, int yb) { n   int res = d[xb][yb]; n   res -= d[xa - 1][yb]; n   res -= d[xb][ya - 1]; n   res += d[xa - 1][ya - 1]; n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> c[i][j]; n       d[i][j] = d[i][j - 1] + (c[i][j] ==  X ); n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (c[i][j] ==  X  && sx == -1) sx = i, sy = j; n       d[i][j] += d[i - 1][j]; n     } n   for (int x = 1; x <= n; x++) n     for (int y = 1; y <= m; y++) { n       if (get(sx, sy, sx + x - 1, sy + y - 1) == x * y) { n         int sxx = sx, syy = sy; n         vector<pair<int, int> > v; n         v.push_back(make_pair(sxx, syy)); n       go:; n         int sum1 = get(sxx + 1, syy, sxx + 1 + x - 1, syy + y - 1); n         int sum2 = get(sxx, syy + 1, sxx + x - 1, syy + 1 + y - 1); n         if (sum1 == x * y && sum2 == x * y) continue; n         if (sum1 == x * y) { n           sxx++; n           v.push_back(make_pair(sxx, syy)); n           goto go; n         } else if (sum2 == x * y) { n           syy++; n           v.push_back(make_pair(sxx, syy)); n           goto go; n         } else { n           int tot = 1ll * get(v[0].first, v[0].second, v[0].first + x - 1, n                               v[0].second + y - 1); n           for (int i = 1; i < v.size(); i++) { n             tot += get(v[i].first, v[i].second, v[i].first + x - 1, n                        v[i].second + y - 1); n             tot -= get(v[i].first, v[i].second, v[i - 1].first + x - 1, n                        v[i - 1].second + y - 1); n           } n           if (d[n][m] == tot) { n             if (ans == -1) n               ans = x * y; n             else n               ans = min(ans, x * y); n           } else n             continue; n         } n       } n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[205][205][205]; n long long int n, k; n string s, t; n char c1, c2; n long long int solve(long long int pos, long long int k, long long int cnt) { n   if (pos >= n) { n     return 0; n   } n   if (dp[pos][k][cnt] != -1) return dp[pos][k][cnt]; n   long long int ans = 0; n   ans = max(solve(pos + 1, k, cnt), ans); n   if (c1 == s[pos] && k >= 0) { n     ans = max(solve(pos + 1, k, cnt + 1), ans); n   } else if (c1 != s[pos] && k >= 1) { n     ans = max(solve(pos + 1, k - 1, cnt + 1), ans); n   } n   if (c2 == s[pos] && k >= 0) { n     ans = max(cnt + solve(pos + 1, k, cnt), ans); n   } else if (c2 != s[pos] && k >= 1) { n     ans = max(cnt + solve(pos + 1, k - 1, cnt), ans); n   } n   return dp[pos][k][cnt] = ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cin >> n >> k; n   cin >> s >> t; n   c1 = t[0]; n   c2 = t[1]; n   for (int i = 0; i < 205; i++) n     for (int j = 0; j < 205; j++) n       for (int p = 0; p < 205; p++) dp[i][j][p] = -1; n   if (c1 != c2) { n     cout << solve(0, k, 0) << endl; n   } else { n     long long int cnt = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] == c1) cnt++; n     } n     long long int ans = min(cnt + k, n); n     ans = ans * (ans - 1) / 2; n     cout << ans; n   } 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 add(long long a, long long b, long long m = 1000000007) { n   return (a % m + b % m + 2 * m) % m; n } n long long mul(long long a, long long b, long long m = 1000000007) { n   return ((a % m + m) * (b % m + m)) % m; n } n int main(void) { n   int n; n   scanf( %d , &(n)); n   if (n % 2 == 0) n     puts( Mahmoud ); n   else n     puts( Ehab ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Rd(int &res) { n   res = 0; n   char p; n   while (p = getchar(), p <  0 ) n     ; n   do { n     res = (res * 10) + (p ^ 48); n   } while (p = getchar(), p >=  0 ); n } n void Max(int &x, int y) { n   if (x < y) x = y; n } n struct W { n   int to, nx, d; n } Lis[100005 << 1]; n int Head[100005], tot = 1; n void Add(int x, int y, int d) { n   Lis[tot].to = y, Lis[tot].nx = Head[x], Lis[tot].d = d; n   Head[x] = tot++; n } n int n, P; n long long ans; n int q[100005], sz, cntson[100005], Mxson[100005], phi; n int fast(int x, int p) { n   int re = 1; n   while (p) { n     if (p & 1) re = (long long)re * x % P; n     x = (long long)x * x % P, p >>= 1; n   } n   return re; n } n bool vis[100005]; n void dfs_pre(int x, int f) { n   cntson[x] = 1, Mxson[x] = 0, q[sz++] = x; n   for (int i = Head[x], to; i && (to = Lis[i].to, 1); i = Lis[i].nx) n     if (to != f && !vis[to]) n       dfs_pre(to, x), cntson[x] += cntson[to], Max(Mxson[x], cntson[to]); n } n map<pair<int, int>, int> mp; n map<int, int> pm; n void dfs_pro(int x, int f, int v1, int c, int v2, int fac) { n   mp[pair<int, int>(v1, c)]++, pm[v2]++; n   for (int i = Head[x], to; i && (to = Lis[i].to, 1); i = Lis[i].nx) n     if (to != f && !vis[to]) n       dfs_pro(to, x, (v1 * 10LL + Lis[i].d) % P, c + 1, n               (v2 + (long long)fac * Lis[i].d) % P, fac * 10LL % P); n } n void dfs(int x) { n   sz = 0; n   dfs_pre(x, x); n   int pl = cntson[x], rt = x; n   for (int i = 0; i < sz; i++) { n     int p = q[i]; n     Max(Mxson[p], cntson[x] - cntson[p]); n     if (Mxson[p] < pl) pl = Mxson[p], rt = p; n   } n   mp.clear(), pm.clear(); n   dfs_pro(rt, rt, 0, 0, 0, 1); n   ans--; n   for (map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); n        it++) { n     int lp = (long long)(P - (it->first).first) * n              fast(fast(10, (it->first).second), phi) % P; n     map<int, int>::iterator it1 = pm.find(lp); n     if (it1 != pm.end()) ans += (long long)(it->second) * (it1->second); n   } n   for (int i = Head[rt], to; i && (to = Lis[i].to, 1); i = Lis[i].nx) n     if (!vis[to]) { n       mp.clear(), pm.clear(); n       dfs_pro(to, rt, Lis[i].d % P, 1, Lis[i].d % P, 10 % P); n       for (map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); n            it++) { n         int lp = (long long)(P - (it->first).first) * n                  fast(fast(10, (it->first).second), phi) % P; n         map<int, int>::iterator it1 = pm.find(lp); n         if (it1 != pm.end()) ans -= (long long)(it->second) * (it1->second); n       } n     } n   vis[rt] = 1; n   for (int i = Head[rt], to; i && (to = Lis[i].to, 1); i = Lis[i].nx) n     if (!vis[to]) dfs(to); n } n int main() { n   Rd(n), Rd(P); n   if (P == 1) { n     cout << (long long)n * (n - 1) << endl; n     return 0; n   } n   phi = P; n   int t = P; n   for (int i = 2; (long long)i * i <= phi; i++) n     if (t % i == 0) { n       phi = phi / i * (i - 1); n       while (t % i == 0) t /= i; n     } n   if (t > 1) phi = phi / t * (t - 1); n   phi--; n   for (int i = 1, x, y, z; i < n; i++) n     Rd(x), Rd(y), Rd(z), x++, y++, Add(x, y, z), Add(y, x, z); n   dfs(1); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 70, M = 11, MA = 1030; n long long int dp[M][N][M][MA], dps[M][N][MA], dpt[M][N], n, q, b, l, r, x; n vector<long long int> v; n long long int get(long long int a, long long int b) { n   long long int x = a, ans, mask = 0, l; n   v.resize(0); n   while (x > 0) { n     v.push_back(x % b); n     x /= b; n   } n   l = (long long int)v.size(); n   ans = dpt[b][l]; n   reverse(v.begin(), v.end()); n   for (auto y : v) { n     for (int j = b - 1; j > y; j--) ans -= dp[b][l][j][mask]; n     mask ^= (1 << y); n     l--; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   for (long long int bi = 2; bi <= 10; bi++) { n     for (int l = 2; l <= 65; l++) { n       for (int c = 0; c < bi; c++) { n         dp[bi][1][c][(1ll << c)] = 1; n         for (int mask = 0; mask < (1ll << bi); mask++) n           dps[bi][1][mask] += dp[bi][1][c][mask]; n       } n       dpt[bi][1] = 0; n     } n   } n   for (long long int bi = 2; bi <= 10; bi++) { n     for (int l = 2; l <= 65; l++) { n       dpt[bi][l] = dpt[bi][l - 1]; n       for (int c = 0; c < bi; c++) { n         for (int i = 0; i < bi; i++) n           for (int mask = 0; mask < (1ll << bi); mask++) { n             x = mask, x ^= (1ll << c); n             dp[bi][l][c][mask] += dp[bi][l - 1][i][x]; n           } n         for (int mask = 0; mask < (1ll << bi); mask++) n           dps[bi][l][mask] += dp[bi][l][c][mask]; n         if (c != 0) dpt[bi][l] += dp[bi][l][c][0]; n       } n     } n   } n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> b >> l >> r; n     l--; n     cout << get(r, b) - get(l, b) <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx ) n using namespace std; n template <class U, class V> n istream& operator>>(istream& is, pair<U, V>& p) { n   is >> p.first >> p.second; n   return is; n } n template <class T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& x : v) { n     is >> x; n   } n   return is; n } n template <class T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto& x : v) { n     os << x <<    ; n   } n   return os; n } n const int N = 1e5 + 5; n int n, x, y; n vector<int> b; n int freq[N]; n int match_indices[N]; n set<pair<int, int>> cnt; n vector<int> match_colors[N]; n void init() { n   cnt.clear(); n   for (int i = 0; i < n + 3; ++i) { n     freq[i] = 0; n     match_indices[i] = 0; n     match_colors[i].clear(); n   } n } n void buildFreq() { n   for (int x : b) { n     ++freq[x]; n   } n   for (int i = 1; i <= n + 1; ++i) { n     if (freq[i] != 0) { n       cnt.emplace(freq[i], i); n     } n   } n } n int getMissingElement() { n   for (int i = 1; i <= n + 1; ++i) { n     if (freq[i] == 0) return i; n   } n   assert(false); n   return -1; n } n void buildMatchIndices() { n   for (int i = 0; i < x; ++i) { n     auto it = cnt.end(); n     --it; n     auto curr = *it; n     cnt.erase(it); n     --curr.first; n     ++match_indices[curr.second]; n     if (curr.first != 0) { n       cnt.emplace(curr); n     } n   } n } n bool buildMatchColors() { n   if (y == 0) return true; n   if (cnt.empty()) return false; n   int y = ::y; n   vector<int> v1; n   for (auto p : cnt) { n     while (p.first--) { n       v1.emplace_back(p.second); n     } n   } n   auto v2 = v1; n   rotate(begin(v2), begin(v2) + ((int)(v1.size())) - cnt.rbegin()->first, n          end(v2)); n   for (int i = 0; i < ((int)(v1.size())); ++i) { n     if (v1[i] == v2[i]) continue; n     if (y == 0) break; n     --y; n     match_colors[v1[i]].emplace_back(v2[i]); n   } n   return y == 0; n } n void check(const vector<int>& a) { n   int x = ::x; n   int y = ::y; n   y += x; n   for (int i = 0; i < n; ++i) { n     x -= (a[i] == b[i]); n   } n   assert(x == 0); n   multiset<int> ms(begin(a), end(a)); n   for (int i = 0; i < n; ++i) { n     auto it = ms.find(b[i]); n     if (it == end(ms)) continue; n     ms.erase(it); n     --y; n   } n   assert(y == 0); n } n void solve() { n   if (y < x) { n     cout <<  NO  << endl; n     return; n   } n   y -= x; n   init(); n   buildFreq(); n   buildMatchIndices(); n   if (!buildMatchColors()) { n     cout <<  NO  << endl; n     return; n   } n   int missing_element = getMissingElement(); n   vector<int> res(n, missing_element); n   for (int i = 0; i < n; ++i) { n     if (match_indices[b[i]] > 0) { n       --match_indices[b[i]]; n       res[i] = b[i]; n       continue; n     } n     if (!match_colors[b[i]].empty()) { n       res[i] = match_colors[b[i]].back(); n       match_colors[b[i]].pop_back(); n     } n   } n   check(res); n   cout <<  YES  << endl; n   cout << res << endl; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> x >> y; n     b.resize(n); n     cin >> b; n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int A[1 << 21], B[1 << 21]; n long long level_index1[21]; n long long level_index2[21]; n long long mergesort(int begin, int end, int level) { n   if (begin == end) return 0; n   long long inv_index = 0; n   int middle = (begin + end) / 2; n   inv_index += mergesort(begin, middle, level + 1); n   inv_index += mergesort(middle + 1, end, level + 1); n   int left = begin; n   int right = middle + 1; n   long long ct = 0; n   long long ct2 = 0; n   for (int k = begin; k <= end; ++k) { n     if (right > end || (left <= middle && (A[left] <= A[right]))) { n       B[k] = A[left++]; n     } else { n       B[k] = A[right++]; n       ct += (middle - left + 1); n     } n   } n   left = begin, right = middle + 1; n   for (int k = begin; k <= end; ++k) { n     if (right > end || (left <= middle && (A[left] < A[right]))) { n       left++; n       ct2 += (end - right + 1); n     } else { n       right++; n     } n   } n   for (int k = begin; k <= end; ++k) A[k] = B[k]; n   level_index1[level] += ct; n   level_index2[level] += ct2; n   return inv_index + ct; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int fim = 1 << n; n   for (int i = 0; i < fim; ++i) cin >> A[i]; n   for (int lvl = 0; lvl <= n; ++lvl) { n     level_index1[lvl] = level_index2[lvl] = 0; n   } n   mergesort(0, fim - 1, 0); n   int m; n   cin >> m; n   while (m--) { n     int q; n     cin >> q; n     long long ans = 0; n     q = n - q; n     for (int lvl = 0; lvl < q; ++lvl) { n       ans += level_index1[lvl]; n     } n     for (int lvl = q; lvl < n; ++lvl) { n       swap(level_index1[lvl], level_index2[lvl]); n       ans += level_index1[lvl]; n     } n     cout << ans <<   n ; 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   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n const int maxn = 300 + 10; n char st[maxn][maxn]; n int n, m, p; n int main() { n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 1; i <= n; i++) scanf( %s , st[i] + 1); n   for (int k = 1; k <= n; k++) { n     for (int i = 1 + k; i + k <= n; i++) n       for (int j = 1 + k; j + k <= m; j++) n         if (st[i][j] ==  *  && st[i - k][j] ==  *  && st[i + k][j] ==  *  && n             st[i][j - k] ==  *  && st[i][j + k] ==  * ) { n           p--; n           if (!p) { n             printf( %d %d n%d %d n%d %d n%d %d n%d %d n , i, j, i - k, j, i + k, n                    j, i, j - k, i, j + k); n             return 0; n           } n         } n   } n   printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y) { n   int temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else n     return x * temp * temp; n } n long long int fun(long long int n) { n   if (n == 0) n     return 0; n   else { n     return n % 10 + fun(n / 10); n   } n } n int main() { n   long long int t; n   cin >> t; n   for (int _ = 0; _ < t; _++) { n     long long int n, k, a, b; n     cin >> k >> n >> a >> b; n     if (k - (n * b) <= 0) n       cout <<  -1 n ; n     else { n       long long int ans = 0; n       if ((k - (n * b)) % (a - b) == 0) { n         ans = ((k - (n * b)) / (a - b)) - 1; n       } else { n         ans = (k - (n * b)) / (a - b); n       } n       ans = min(n, ans); n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n template <typename T, size_t N> n int SIZE(const T (&t)[N]) { n   return N; n } n template <typename T> n int SIZE(const T &t) { n   return t.size(); n } n string to_string(const string s, int x1 = 0, int x2 = 1e9) { n   return     + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) :   ) +    ; n } n string to_string(const char *s) { return to_string((string)s); } n string to_string(const bool b) { return (b ?  true  :  false ); } n string to_string(const char c) { return string({c}); } n template <size_t N> n string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { n   string t =   ; n   for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); n        __iii__ <= __jjj__; ++__iii__) { n     t += b[__iii__] +  0 ; n   } n   return     + t +    ; n } n template <typename A, typename... C> n string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords); n int l_v_l_v_l = 0, t_a_b_s = 0; n template <typename A, typename B> n string to_string(const pair<A, B> &p) { n   l_v_l_v_l++; n   string res =  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n   l_v_l_v_l--; n   return res; n } n template <typename A, typename... C> n string to_string(const A(&v), int x1, int x2, C... coords) { n   int rnk = rank<A>::value; n   string tab(t_a_b_s,    ); n   string res =   ; n   bool first = true; n   if (l_v_l_v_l == 0) res +=   n ; n   res += tab +  [ ; n   x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); n   auto l = begin(v); n   advance(l, x1); n   auto r = l; n   advance(r, (x2 - x1) + (x2 < SIZE(v))); n   for (auto e = l; e != r; e = next(e)) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     l_v_l_v_l++; n     if (e != l) { n       if (rnk > 1) { n         res +=   n ; n         t_a_b_s = l_v_l_v_l; n       }; n     } else { n       t_a_b_s = 0; n     } n     res += to_string(*e, coords...); n     l_v_l_v_l--; n   } n   res +=  ] ; n   if (l_v_l_v_l == 0) res +=   n ; n   return res; n } n void dbgm() { ; } n template <typename Heads, typename... Tails> n void dbgm(Heads H, Tails... T) { n   cout << to_string(H) <<   |  ; n   dbgm(T...); n } n const long long MOD = (int)1e9 + 7; n string ascii_lowercase =  abcdefghijklmnopqrstuvwxyz ; n string ascii_uppercase =  ABCDEFGHIJKLMNOPQRSTUVWXYZ ; n long long inv(long long a, long long b) { n   return 1 < a ? b - inv(b % a, a) * b / a : 1; n } n const long long INF = 1e6; n unordered_map<int, int, custom_hash> mp; n long long gcd(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 = gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n long long distsq(long long x1, long long y1, long long x2, long long y2) { n   return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); n } n long long n, m; n double get(double x1, double y1, double x2, double y2, double x3, double y3) { n   if (x3 == x1) { n     return (x2 - x1) * (x2 - x1); n   } n   if (y3 == y1) { n     return (y2 - y1) * (y2 - y1); n   } n   double m = (y3 - y1) / (x3 - x1); n   double c = y1 - m * x1; n   double mm = -1 / m; n   double interx = (-m * c + m * y2 + x2) / (1 + m * m); n   double dx = interx - x2; n   double dy = mm * dx; n   return dx * dx + dy * dy; n } n double px[1005]; n double py[1005]; n double ans = -1; n int main() { n   cin >> n; n   for (int i = 0; i < (n); i++) { n     cin >> px[i] >> py[i]; n   } n   double curr; n   for (int i = 0; i < (n); i++) { n     curr = get(px[i], py[i], px[(i + 1) % n], py[(i + 1) % n], px[(i + 2) % n], n                py[(i + 2) % n]); n     if (ans < 0) ans = curr; n     ans = min(ans, curr); n   } n   printf( %.12f n , sqrt(ans) / 2.0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 7; n long long M = 1e9 + 7; n long long dp[N]; n long long g[N][30]; n int n; n vector<int> a; n int ch[N][N]; n int main() { n   ch[0][0] = 1; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       if (j == 0) { n         ch[j][i] = 1; n         continue; n       } n       ch[j][i] = (ch[j - 1][i - 1] + ch[j][i - 1]) % M; n     } n   } n   for (int i = 1; i <= n; i++) { n     char c; n     cin >> c; n     if (!a.size()) n       a.push_back(c -  a  + 1); n     else if (a.back() != c -  a  + 1) n       a.push_back(c -  a  + 1); n   } n   dp[0] = 1; n   for (int i = 0; i < a.size(); i++) { n     for (int j = a.size(); j >= 2; j--) { n       dp[j] += -g[j][a[i]] - g[j - 1][a[i]] + dp[j - 1]; n       g[j][a[i]] = dp[j - 1] - g[j - 1][a[i]]; n       dp[j] %= M; n       g[j][a[i]] %= M; n     } n     if (g[1][a[i]] == 0) dp[1]++; n     g[1][a[i]] = 1; n   } n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     dp[i] = (((dp[i] % M) + M) % M); n     res = (res + ch[i - 1][n - 1] * dp[i]) % M; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool a[2001][2001], w[2001][2001]; n int n, fi, fj, si, sj, dist, s, clr = 4; n inline bool val(int x, int y) { return (x > 0 && y > 0 && x < n && y < n); } n inline void clear(int x, int y) { n   s = 0; n   for (int i = 0; i < clr; i++) n     for (int j = 0; j < clr; j++) n       if (a[i + x][j + y]) s++; n   if (s <= clr) n     for (int i = 0; i < clr; i++) n       for (int j = 0; j < clr; j++) a[i + x][j + y] = 0; n } n void dfs(int x, int y) { n   w[x][y] = 1; n   s++; n   if (((x - fi) * (x - fi)) + ((y - fj) * (y - fj)) > dist) n     dist = ((x - fi) * (x - fi)) + ((y - fj) * (y - fj)); n   if (val(x - 1, y) && a[x - 1][y] && !w[x - 1][y]) dfs(x - 1, y); n   if (val(x + 1, y) && a[x + 1][y] && !w[x + 1][y]) dfs(x + 1, y); n   if (val(x, y - 1) && a[x][y - 1] && !w[x][y - 1]) dfs(x, y - 1); n   if (val(x, y + 1) && a[x][y + 1] && !w[x][y + 1]) dfs(x, y + 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   int i, j, x, as = 0, ac = 0; n   cin >> n; n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) cin >> a[i][j]; n   for (i = 0; i < n; i += clr) n     for (j = 0; j < n; j += clr) clear(i, j); n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) n       if (!w[i][j] && a[i][j]) { n         dist = s = 0; n         fi = i, fj = j; n         dfs(i, j); n         if (dist < 200.0) { n           a[i][j] = 0; n           continue; n         } n         s *= 1.2; n         double d = sqrt(dist), sq = ((d / sqrt(2.0)) * (d / sqrt(2.0))), n                circ = ((d / 2.0) * (d / 2.0)) * M_PI; n         if (abs(s - sq) < abs(s - circ)) n           as++; n         else n           ac++; n       } n   cout << ac <<     << as; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 999999999; n const long long INFL = 99999999999999999LL; n const double EPSILON = 0.00000001; n const long long MOD = 1000000007; n long long a[100005]; n int32_t main() { n   long long n, t; n   scanf( %I64d , &n); n   ; n   scanf( %I64d , &t); n   ; n   for (long long i = 0; i < n; i++) scanf( %I64d , &a[i]); n   ; n   long long ans = 0; n   long long l = 0, r = 0; n   long long cur = 0; n   long long s = 0; n   while (l < n) { n     while (r < n and s + a[r] <= t) { n       s += a[r]; n       r++; n       cur++; n       ans = ((ans) > (cur) ? (ans) : (cur)); n     } n     l++; n     if (l > r) n       r++; n     else { n       s -= a[l - 1]; n       cur--; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fr[200004]; n struct cmp { n   bool operator()(int a, int b) const { return a > b; } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   auto make = [](vector<int>& a, int pos) { n     swap(a[pos + 2], a[pos + 1]); n     swap(a[pos + 1], a[pos]); n   }; n   int t; n   cin >> t; n   while (t--) { n     int n, i, j, k, l; n     cin >> n; n     vector<int> v(n); n     vector<pair<int, int>> p(n); n     for (i = 0; i < n; i++) { n       cin >> v[i]; n       p[i] = {v[i], i}; n     } n     sort(p.begin(), p.end()); n     for (i = 0; i < n; i++) v[p[i].second] = i; n     int cnt = 0; n     for (i = 0; i < n - 1; i++) { n       for (j = i + 1; j < n; j++) n         if (v[j] < v[i]) cnt++; n     } n     if (cnt & 1) { n       for (i = 0; i < n - 1; i++) n         if (p[i].first == p[i + 1].first) { n           swap(v[p[i].second], v[p[i + 1].second]); n           break; n         } n     } n     vector<int> ans; n     for (i = 0; i < n - 2; i++) { n       int pos = min_element(v.begin() + i, v.end()) - v.begin(); n       while (pos > i + 1) { n         make(v, pos - 2); n         ans.push_back(pos - 2); n         pos = pos - 2; n       } n       if (pos != i) { n         make(v, i); n         make(v, i); n         ans.push_back(i); n         ans.push_back(i); n         pos = i; n       } n     } n     for (i = 0; i < 3; i++) { n       if (is_sorted(v.begin(), v.end())) { n         break; n       } n       make(v, n - 3); n       ans.push_back(n - 3); n     } n     if (!is_sorted(v.begin(), v.end())) { n       cout << -1 <<   n ; n       continue; n     } n     cout << ans.size() <<   n ; n     for (int x : ans) cout << x + 1 <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int x, y; n   cin >> x >> y; n   if (x == 1) { n     if (y <= 1) { n       cout <<  yes  << endl; n       return; n     } else { n       cout <<  no  << endl; n       return; n     } n   } else if (x == 2 || x == 3) { n     if (y <= 3) { n       cout <<  yes  << endl; n       return; n     } else { n       cout <<  no  << endl; n       return; n     } n   } else if (x > 3) { n     cout <<  yes  << endl; n     return; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int q; n   cin >> q; n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Bitset { n   int sz, len; n   vector<unsigned int> v; n   Bitset(int szz) { n     len = szz; n     sz = szz + 31 >> 5; n     v.clear(); n     v.resize(sz, 0); n   } n   Bitset operator<<=(int x) { n     int d = x >> 5, r = x & 31; n     for (int i = sz - 1; i >= d; i--) v[i] = v[i - d]; n     for (int i = d - 1; i >= 0; i--) v[i] = 0; n     if (r) { n       for (int i = sz - 1; i >= 1; i--) n         v[i] = ((v[i] & ((1u << 32 - r) - 1)) << r) | (v[i - 1] >> 32 - r); n       v[0] = (v[0] & ((1u << 32 - r) - 1)) << r; n     } n   } n   Bitset operator|(const Bitset &b) { n     Bitset rlt = Bitset(len); n     for (int i = 0; i < sz; i++) rlt.v[i] = v[i] | b.v[i]; n     return rlt; n   } n   Bitset operator^(const Bitset &b) { n     Bitset rlt = Bitset(len); n     for (int i = 0; i < sz; i++) rlt.v[i] = v[i] ^ b.v[i]; n     return rlt; n   } n   vector<int> get() { n     vector<int> rlt; n     for (int i = 0; i < sz; i++) n       if (v[i]) { n         for (int j = 0; j < 32; j++) n           if (v[i] & (1u << j)) rlt.push_back((i << 5) | j); n       } n     return rlt; n   } n   bool bit(int x) { return v[x >> 5] & (1u << (x & 31)); } n }; n int n, s, a[70707], id[70707], in[70707]; n bool vis[70707]; n set<int> st[70707]; n bool cmp(int i, int j) { n   return a[i] != a[j] ? a[i] < a[j] : !vis[i] && vis[j]; n } n int main() { n   Bitset val = Bitset(70707), nwb = Bitset(70707), tmp = Bitset(70707); n   scanf( %d %d , &n, &s); n   int I = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] > s) return puts( -1 ), 0; n     if (a[i] > a[I]) I = i; n   } n   vis[I] = 1; n   s -= a[I]; n   val.v[0] = 1; n   for (int i = 1; i <= n && s; i++) n     if (!vis[i] && a[i] <= s) { n       tmp = val; n       tmp <<= a[i]; n       nwb = val ^ (val | tmp); n       val = val | tmp; n       vector<int> v = nwb.get(); n       for (int j = 0; j < v.size(); j++) { n         if (v[j] <= s) st[i].insert(v[j]); n       } n     } n   if (!val.bit(s)) return puts( -1 ), 0; n   for (int i = n; i >= 1; i--) { n     if (st[i].find(s) != st[i].end()) { n       s -= a[i]; n       vis[i] = 1; n     } n   } n   for (int i = 1; i <= n; i++) id[i] = i; n   sort(id + 1, id + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     if (!vis[id[i]]) { n       in[id[i + 1]] = id[i]; n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , a[i] - a[in[i]]); n     if (in[i]) n       printf( 1 %d n , in[i]); n     else n       puts( 0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, t, i, j, k; n long long a[2000005]; n long long ans, sum; n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m * 2; i++) { n     scanf( %d , &t); n     a[t]++; n   } n   for (i = 1; i <= n; i++) ans += a[i] * (n - 1 - a[i]); n   printf( %lld n , (long long)n * (n - 1) * (n - 2) / 6 - ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[1010][1010]; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   if (a > 1 && b > 1) { n     puts( NO ); n     return (0); n   } n   if (n > 1 && n <= 3 && a == 1 && b == 1) { n     puts( NO ); n     return (0); n   } n   puts( YES ); n   if (a == 1 && b == 1) { n     for (int i = 1; i < n; i++) { n       mat[i][i + 1] = 1; n       mat[i + 1][i] = 1; n     } n   } else if (a == 1) { n     for (int i = 1; i < b; i++) { n       for (int j = 1; j <= n; j++) { n         if (i != j) { n           mat[i][j] = 1; n           mat[j][i] = 1; n         } n       } n     } n   } else if (b == 1) { n     for (int i = a; i < n; i++) { n       mat[i][i + 1] = 1; n       mat[i + 1][i] = 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) printf( %d , mat[i][j]); n     printf(  n ); n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long T, n, k; n   cin >> T; n   while (T--) { n     cin >> n >> k; n     long long ans = 0; n     while (n) { n       if (n % k != 0) n         ans += n % k, n -= n % k; n       else n         ans++, n /= k; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100][100]; n int row[100]; n vector<pair<pair<int, int>, pair<int, int> > > v; n int pos[30000][2]; n int main() { n   int n; n   int sum = 0; n   while (scanf(  %d , &n) == 1) { n     v.clear(); n     sum = 0; n     for (int i = 1; i <= n; i++) scanf(  %d , &row[i]); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= row[i]; j++) { n         scanf(  %d , &arr[i][j]); n         pos[arr[i][j]][0] = i; n         pos[arr[i][j]][1] = j; n       } n       sum += row[i]; n     } n     int cnt = 1; n     int swcnt = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= row[i]; j++) { n         if (arr[i][j] != cnt) { n           swcnt++; n           int r = pos[cnt][0]; n           int c = pos[cnt][1]; n           int val = arr[i][j]; n           arr[i][j] = cnt; n           arr[r][c] = val; n           pos[val][0] = r; n           pos[val][1] = c; n           pos[cnt][0] = i; n           pos[cnt][1] = j; n           v.push_back(make_pair(make_pair(i, j), make_pair(r, c))); n         } n         cnt++; n       } n     } n     printf( %d n , swcnt); n     for (int i = 0; i < ((int)v.size()); i++) n       printf( %d %d %d %d n , v[i].first.first, v[i].first.second, n              v[i].second.first, v[i].second.second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[3]; n int dp[310][310]; n string name[2] = { BitAryo ,  BitLGM }; n int mem(int x, int y) { n   if (dp[x][y] != -1) return dp[x][y]; n   int mn = 1; n   for (int i = 1; i <= x; i++) mn = min(mn, mem(x - i, y)); n   for (int i = 1; i <= y; i++) mn = min(mn, mem(x, y - i)); n   for (int i = 1; i <= min(x, y); i++) mn = min(mn, mem(x - i, y - i)); n   return dp[x][y] = 1 - mn; n } n int main() { n   memset(dp, -1, sizeof dp); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   if (n == 1) cout << name[(a[0] == 0 ? 0 : 1)] << endl; n   if (n == 2) cout << name[mem(a[0], a[1])] << endl; n   if (n == 3) cout << name[((a[0] ^ a[1] ^ a[2]) == 0 ? 0 : 1)] << endl; n   return 0; n } n


#include <bits/stdc++.h> n struct Point { n   double x, y; n }; n struct Polygon { n   Point p[100000]; n   int n; n   inline double get(int i, double x) { n     if (i < 0) return p[0].y; n     if (i >= n - 1) return p[n - 1].y; n     return p[i].y + n            (x - p[i].x) * (p[i + 1].y - p[i].y) / (p[i + 1].x - p[i].x); n   } n   double area() { n     double sum = 0; n     for (int i = 0; i < n - 1; i++) n       sum += (p[i + 1].x - p[i].x) * (p[i + 1].y + p[i].y) / 2; n     return sum; n   } n }; n Polygon poly[2]; n Polygon inp; n Polygon ta; n Polygon tb; n void intersect(Polygon& res, Polygon& a, Polygon& b) { n   ta.n = tb.n = res.n = 0; n   int ai = 0, bi = 0; n   while (ai < a.n || bi < b.n) { n     if (ai < a.n && (bi >= b.n || a.p[ai].x < b.p[bi].x)) { n       tb.p[tb.n].x = a.p[ai].x; n       tb.p[tb.n++].y = b.get(bi - 1, a.p[ai].x); n       ta.p[ta.n++] = a.p[ai++]; n     } else { n       ta.p[ta.n].x = b.p[bi].x; n       ta.p[ta.n++].y = a.get(ai - 1, b.p[bi].x); n       tb.p[tb.n++] = b.p[bi++]; n     } n   } n   for (int i = 0, next = 0; i < ta.n; i = next) { n     while (next < ta.n && ta.p[next].x - ta.p[i].x < 1e-6) next++; n     res.p[res.n].x = ta.p[i].x; n     res.p[res.n++].y = (ta.p[i].y > tb.p[i].y ? ta.p[i].y : tb.p[i].y); n     if (next < ta.n && n         (ta.p[i].y - tb.p[i].y) * (ta.p[next].y - tb.p[next].y) < -1e-6) { n       double t = (tb.p[i].y - ta.p[i].y) / n                  (ta.p[next].y - ta.p[i].y - tb.p[next].y + tb.p[i].y); n       res.p[res.n].x = ta.p[i].x + t * (ta.p[next].x - ta.p[i].x); n       res.p[res.n++].y = ta.p[i].y + t * (ta.p[next].y - ta.p[i].y); n     } n   } n } n int main() { n   int n; n   scanf( %d%d , &n, &inp.n); n   inp.n++; n   for (int i = 0; i < inp.n; i++) inp.p[i].x = i; n   int cur = 0; n   double prev = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < inp.n; j++) scanf( %lf , &inp.p[j].y); n     if (i == 0) n       memcpy(&poly[1 - cur], &inp, sizeof inp); n     else n       intersect(poly[1 - cur], poly[cur], inp); n     cur = 1 - cur; n     double area = poly[cur].area(); n     printf( %lf n , area - prev); n     prev = area; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int from, to, vis, next; n } e[400020]; n int top, head[200020]; n vector<int> g[200020]; n int in[200020], gg[200020]; n int n, m, k; n int ans[200020], cnt; n void add_edge(int from, int to) { n   e[++top].to = to; n   e[top].from = from; n   e[top].next = head[from]; n   head[from] = top; n } n void add(int from, int to) { n   add_edge(from, to); n   add_edge(to, from); n } n queue<int> q; n void del(int pos) { n   if (!gg[pos]) cnt--; n   q.push(pos); n   gg[pos] = 1; n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     for (int i = head[now]; ~i; i = e[i].next) { n       if (e[i].vis || gg[e[i].to]) continue; n       in[e[i].to]--; n       e[i].vis = 1; n       e[i ^ 1].vis = 1; n       if (in[e[i].to] < k) { n         cnt--; n         gg[e[i].to] = 1; n         q.push(e[i].to); n       } n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   cnt = n; n   top = -1; n   memset(head, -1, sizeof(head)); n   int from, to; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &from, &to); n     in[from]++, in[to]++; n     add(from, to); n   } n   for (int i = 1; i <= n; i++) { n     if (head[i] == -1) cnt--, gg[i] = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (in[i] < k && !gg[i]) del(i); n   } n   int ttt = m; n   for (int i = 2 * m - 1; i >= 1; i -= 2) { n     ans[ttt--] = cnt; n     if (e[i].vis) continue; n     in[e[i].from]--; n     in[e[i].to]--; n     e[i].vis = e[i ^ 1].vis = 1; n     if (!gg[e[i].from] && in[e[i].from] < k) del(e[i].from); n     if (!gg[e[i].to] && in[e[i].to] < k) del(e[i].to); n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> s; n vector<long long>::iterator it; n long long n, m, a; n long long sum = 0; n int main() { n   cin >> n >> m; n   for (long long i = 0; i < n; ++i) { n     cin >> a; n     s.push_back(a); n     sum += a; n   } n   if (m > sum) n     cout << -1 << endl; n   else { n     sort(s.begin(), s.end(), greater<long long>()); n     long long l = 1, r = n; n     while (l <= r) { n       long long mid = (l + r) / 2, ans = 0, temp = 0; n       it = s.begin(); n       while (it != s.end()) { n         long long cnt = 0; n         while (cnt < mid && it != s.end()) { n           ans += max((long long)0, *it - temp); n           ++it, ++cnt; n         } n         ++temp; n       } n       if (ans < m) n         l = mid + 1; n       else n         r = mid - 1; n     } n     cout << l << endl; n   } n } n


#include <bits/stdc++.h> n const double eps = (1e-9); n using namespace std; n int dcmp(long double a, long double b) { n   return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1; n } n int getBit(long long num, int idx) { return ((num >> idx) & 1ll) == 1; } n int setBit1(int num, int idx) { return num | (1 << idx); } n long long setBit0(long long num, int idx) { return num & ~(1ll << idx); } n long long flipBit(long long num, int idx) { return num ^ (1ll << idx); } n void M() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int countNumBit1(int mask) { n   int ret = 0; n   while (mask) { n     mask &= (mask - 1); n     ++ret; n   } n   return ret; n } n const int N = 300009; n int arr[N], key[N], n, power[32], ans[N]; n string ret; n struct trie { n   trie* child[3]; n   int isleaf; n   trie() { n     isleaf = 0; n     memset(child, 0, sizeof(child)); n   } n }; n string to_bin(int n) { n   string ret =   , ans =   ; n   while (n) { n     ret += (char)((n % 2) +  0 ); n     n /= 2; n   } n   int rem = 30 - ((int)(ret).size()); n   while (rem--) { n     ret +=  0 ; n   } n   for (int i = ((int)(ret).size()) - 1; i >= 0; i--) { n     ans += ret[i]; n   } n   return ans; n } n int to_dec(string s) { n   int ans = 0; n   for (int i = ((int)(s).size()) - 1; i >= 0; i--) { n     ans += power[((int)(s).size()) - 1 - i] * (s[i] -  0 ); n   } n   return ans; n } n void insertt(trie* cur, int id, string s) { n   if (id == ((int)(s).size())) { n     cur->isleaf++; n     return; n   } n   int ch = s[id] -  0 ; n   if (cur->child[ch] == 0) { n     cur->child[ch] = new trie(); n   } n   insertt(cur->child[ch], id + 1, s); n } n bool removee(trie* cur, int id, string s) { n   if (id == ((int)(s).size())) { n     cur->isleaf--; n     return (cur->isleaf == 0 && (cur->child[0] == 0 && cur->child[1] == 0)); n   } n   int ch = s[id] -  0 ; n   bool ret = removee(cur->child[ch], id + 1, s); n   if (ret) { n     cur->child[ch] = 0; n     return (cur->isleaf == 0 && cur->child[0] == 0 && cur->child[1] == 0); n   } n   return 0; n } n void findd(trie* cur, int id, string s) { n   if (id == ((int)(s).size())) { n     return; n   } n   int ch = s[id] -  0 ; n   int opch = (s[id] ==  0  ?  1  :  0 ) -  0 ; n   if (cur->child[ch] != 0) { n     ret += (char)(ch +  0 ); n     findd(cur->child[ch], id + 1, s); n   } else if (cur->child[opch] != 0) { n     ret += (char)(opch +  0 ); n     findd(cur->child[opch], id + 1, s); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   power[0] = 1; n   for (int i = 1; i <= 30; i++) power[i] = power[i - 1] * 2; n   trie* root = new trie(); n   int no; n   for (int i = 0; i < n; i++) { n     cin >> no; n     string st = to_bin(no); n     insertt(root, 0, st); n   } n   for (int i = 0; i < n; i++) { n     string st = to_bin(arr[i]); n     ret =   ; n     findd(root, 0, st); n     removee(root, 0, ret); n     ans[i] = arr[i] ^ (to_dec(ret)); n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int log2(int x) { return 31 - __builtin_clz(x); } n int main(void) { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n, q; n   cin >> n >> q; n   vector<long long> a(n); n   for (int i = 0; i < (int)(n); i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < (int)(n); i++) { n     int b; n     cin >> b; n     a[i] = b - a[i]; n     if (i) { n       a[i] += a[i - 1]; n     } n   } n   vector<vector<long long> > mn(20, vector<long long>(n)), n       mx(20, vector<long long>(n)); n   copy(a.begin(), a.end(), mn[0].begin()); n   copy(a.begin(), a.end(), mx[0].begin()); n   for (int k = 1; k < 20; k++) { n     for (int i = 0; i + (1 << (k - 1)) < n; i++) { n       mn[k][i] = min(mn[k - 1][i], mn[k - 1][i + (1 << (k - 1))]); n       mx[k][i] = max(mx[k - 1][i], mx[k - 1][i + (1 << (k - 1))]); n     } n   } n   int l, r; n   while (q--) { n     cin >> l >> r; n     --l; n     int k = log2(r - l); n     long long _min = min(mn[k][l], mn[k][r - (1 << k)]); n     long long _max = max(mx[k][l], mx[k][r - (1 << k)]); n     long long start = l ? a[l - 1] : 0; n     if (start != a[r - 1] || _min < start) { n       cout <<  -1 n ; n     } else { n       cout << _max - start <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int weight[11]; n int total = 0; n int bd2 = 0; n int mem[11][5001]; n int m; n bool wasal = false; n int arr[1001]; n int knapsack(int i, int prev, int totalPrev, int me, int all, int counter) { n   arr[counter - 1] = prev; n   if (i == m) { n     wasal = true; n     bd2 = all; n     return 0; n   } n   int choice2 = 0; n   if (!wasal) { n     for (int j = 0; j < total && !wasal; j++) { n       if (weight[j] != prev && me + weight[j] > totalPrev && !wasal) { n         if (totalPrev == -1) { n           totalPrev = 0; n         } n         choice2 += knapsack(i + 1, weight[j], me + weight[j], totalPrev, n                             me + weight[j] + totalPrev, counter + 1) + n                    1; n       } n     } n     return choice2; n   } n   return 0; n } n int main() { n   string c; n   cin >> c; n   for (int i = 0; i < c.size(); i++) { n     if (c.at(i) ==  1 ) { n       weight[total] = i + 1; n       total++; n     } n   } n   memset(arr, false, sizeof(arr)); n   cin >> m; n   int num = knapsack(0, 0, -1, 0, 0, 0); n   if (wasal) { n     cout <<  YES  << endl; n     for (int i = 0; i < m; i++) { n       if (i != 0) { n         cout <<     << arr[i]; n       } else { n         cout << arr[i]; n       } n     } n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T cross(complex<T> a, complex<T> b) { n   return imag(conj(a) * b); n } n template <typename T> n T dot(complex<T> a, complex<T> b) { n   return real(conj(a) * b); n } n struct __Query { n   static bool query; n }; n bool __Query::query = false; n template <typename T> n struct Point { n   complex<T> p; n   mutable function<const complex<T> *()> succ; n   bool operator<(const Point &rhs) const { n     const complex<T> &q = rhs.p; n     if (!__Query::query) { n       if (real(p) != real(q)) return real(p) < real(q); n       return imag(p) < imag(q); n     } n     const complex<T> *s = succ(); n     if (!s) return false; n     return dot(p - *s, q) < 0; n   } n }; n template <typename T, int turn> n struct half_hull : public set<Point<T>> { n   using set<Point<T>>::begin; n   using set<Point<T>>::insert; n   using set<Point<T>>::end; n   using set<Point<T>>::lower_bound; n   using set<Point<T>>::empty; n   using set<Point<T>>::erase; n   using typename set<Point<T>>::iterator; n   complex<T> extreme(const complex<T> &p) const { n     assert(!empty() && turn * imag(p) >= 0); n     __Query::query = true; n     auto pos = lower_bound(Point<T>{p}); n     __Query::query = false; n     assert(pos != end()); n     return pos->p; n   } n   void insert(const complex<T> &p) { n     auto y = insert(Point<T>{p}).first; n     if (y == end()) return; n     y->succ = [=] { return next(y) == end() ? nullptr : &next(y)->p; }; n     if (bad(y)) { n       erase(y); n       return; n     } n     while (y != begin() && bad(prev(y))) erase(prev(y)); n     while (next(y) != end() && bad(next(y))) erase(next(y)); n   } n  n  private: n   bool bad(iterator y) { n     if (y == begin() || y == end()) return false; n     auto x = prev(y), z = next(y); n     if (z == end()) return false; n     return cross(y->p - x->p, z->p - x->p) * turn >= 0; n   } n }; n template <typename T> n struct convex_hull_trick { n   void insert(const complex<T> &p) { n     lower_hull.insert(p); n     upper_hull.insert(p); n   } n   complex<T> extreme(const complex<T> &p) const { n     if (std::imag(p) < 0) return lower_hull.extreme(p); n     return upper_hull.extreme(p); n   } n   long long get(const complex<T> &p) const { n     if (upper_hull.empty()) return 0LL; n     return dot(p, extreme(p)); n   } n  n  private: n   half_hull<T, +1> upper_hull; n   half_hull<T, -1> lower_hull; n }; n long long solve(vector<vector<int>> &adj, int root = 0) { n   int n = adj.size(); n   vector<long long> sz(n), sz2(n), ssz(n); n   long long ans = 0; n   function<void(int, int)> dfs = [&](int u, int p) { n     sz[u] = 1; n     for (auto v : adj[u]) n       if (v != p) { n         dfs(v, u); n         sz[u] += sz[v]; n       } n   }; n   function<void(int, int)> dfs2 = [&](int u, int p) { n     sz2[u] += sz[u] * sz[u]; n     for (auto v : adj[u]) n       if (v != p) { n         sz2[v] += sz2[u]; n         ssz[v] += ssz[u] + sz[v] * sz[u]; n         dfs2(v, u); n       } n   }; n   function<long long(int, int)> dfs3 = [&](int u, int p) { n     convex_hull_trick<long long> cht; n     long long ret = -sz2[u] + ssz[u]; n     for (auto v : adj[u]) n       if (v != p) { n         long long temp = dfs3(v, u); n         ret = max(ret, temp); n         long long r = sz[v] * n + sz2[u] - ssz[v] + temp; n         ans = max(ans, r); n         ans = max(ans, r + cht.get({1LL, -sz[v]})); n         cht.insert({r, sz[v]}); n       } n     return ret; n   }; n   dfs(root, -1); n   dfs2(root, -1); n   dfs3(root, -1); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int n; n   cin >> n; n   vector<vector<int>> adj(n); n   for (int i = 1, u, v; i < n; ++i) { n     cin >> u >> v; n     --u, --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   long long ans = n * (n - 1LL) / 2 + solve(adj); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[100005], sum, en; n bool cheak(long long mid) { n   long long sum = 0, cnt = m; n   for (int i = 1; i <= n; ++i) { n     if (a[i]) { n       sum += a[i]; n       while (sum > mid - i) { n         sum -= (mid - i); n         cnt--; n         if (cnt < 0) return 0; n       } n     } n   } n   if (!cnt) return sum <= 0; n   return 1; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   long long l = 0, r = 1e17, ans; n   while (l < r) { n     long long mid = (l + r) / 2; n     if (cheak(mid)) n       ans = mid, r = mid; n     else n       l = mid + 1; n   } n   cout << ans << endl; n   return 0; n } n


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


#include<bits/stdc++.h> n using namespace std; n  n #define ll long long int n #define M 1000000007 n  n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(0),cout.tie(0); n     int t,n,x; n     cin>>t; n     string str= 2020 ; n     while(t--) n     { n         int a,b,c,d,e,c1,c2,c3; n         cin>>c1>>c2>>c3; n         cin>>a>>b>>c>>d>>e; n         bool flag=true; n         int te=min(c1,a); n         c1-=te; n         a-=te; n         te=min(c2,b); n         c2-=te; n         b-=te; n         te=min(c3,c); n         c3-=te; n         c-=te; n         te=min(c1,d); n         c1-=te; n         d-=te; n         te=min(c2,e); n         c2-=te; n         e-=te; n         te=min(c3,d); n         c3-=te; n         d-=te; n         te=min(c3,e); n         c3-=te; n         e-=te; n         if(a||b||c||d||e) n             cout<< no ; n         else n             cout<< yes ; n         cout<<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   string s[n]; n   for (int i = 0; i < n; i++) cin >> s[i]; n   vector<vector<int>> row(n), col(m); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  # ) { n         row[i].push_back(j); n         col[j].push_back(i); n       } n     } n   } n   deque<pair<int, int>> q; n   int rw[n]; n   int rvis[n]; n   for (int i = 0; i < n; i++) { n     rw[i] = INT_MAX; n     rvis[i] = 0; n   } n   int cw[m]; n   int cvis[m]; n   for (int i = 0; i < m; i++) { n     cw[i] = INT_MAX; n     cvis[i] = 0; n   } n   rw[n - 1] = 0; n   q.push_back(make_pair(0, n - 1)); n   while (q.size() > 0) { n     int type = q.front().first; n     int num = q.front().second; n     q.pop_front(); n     if (type == 0) { n       if (rvis[num] == 1) n         continue; n       else n         rvis[num] = 1; n       if (num == 0) break; n       for (int i = 0; i < row[num].size(); i++) { n         int cn = row[num][i]; n         if (cvis[cn] == 0 && cw[cn] > rw[num] + 1) { n           cw[cn] = rw[num] + 1; n           q.push_back(make_pair(1, cn)); n         } n       } n     } else if (type == 1) { n       if (cvis[num] == 1) n         continue; n       else n         cvis[num] = 1; n       for (int i = 0; i < col[num].size(); i++) { n         int rn = col[num][i]; n         if (rvis[rn] == 0 && rw[rn] > cw[num] + 1) { n           rw[rn] = cw[num] + 1; n           q.push_back(make_pair(0, rn)); n         } n       } n     } n   } n   if (rw[0] == INT_MAX) n     cout << -1; n   else n     cout << rw[0]; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   long long maximize(vector<int>& nums, int x) { n     int n = nums.size(); n     vector<vector<long long>> dp(3, vector<long long>(n + 1, 0)); n     long long res = 0; n     for (int i = 0; i < n; ++i) { n       long long num = nums[i]; n       dp[0][i + 1] = max(0LL, dp[0][i] + num); n       dp[1][i + 1] = max(0LL, max(dp[0][i], dp[1][i]) + num * x); n       dp[2][i + 1] = max(0LL, max(dp[2][i], dp[1][i]) + num); n     } n     for (int j = 0; j < 3; ++j) { n       for (int i = 0; i <= n; ++i) { n         res = max(res, dp[j][i]); n       } n     } n     return res; n   } n }; n int main(int argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   Solution sol; n   int n, x; n   cin >> n >> x; n   vector<int> nums(n); n   for (int i = 0; i < n; ++i) { n     cin >> nums[i]; n   } n   cout << sol.maximize(nums, x) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n int main() { n   int i, n, sum; n   while (scanf( %d , &n) != EOF) { n     sum = 0; n     while (n != 1) { n       sum += n; n       for (i = 2; i <= n; i++) { n         if (n % i == 0) { n           n /= i; n           break; n         } n       } n     } n     cout << sum + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long xa, ya, xb, yb, xc, yc, a, A; n   cin >> xa >> ya >> xb >> yb >> xc >> yc; n   if ((xa == xb) && (ya == yb)) n     cout <<  YES ; n   else if ((xb == ya) && (yb == -xa)) n     cout <<  YES ; n   else if ((xb == -xa) && (yb == -ya)) n     cout <<  YES ; n   else if ((xb == -ya) && (yb == xa)) n     cout <<  YES ; n   else if (((xc * xc) + (yc * yc)) == 0) n     cout <<  NO ; n   else { n     A = (xc * xc) + (yc * yc); n     a = (((xb - xa) * xc) + ((yb - ya) * yc)); n     if (a % A == 0) { n       a = a / A; n       if ((yc != 0) && ((xb - xa - (xc * a)) % yc == 0)) { n         cout <<  YES ; n         return 0; n       } else if ((xc != 0) && ((yb - ya - (yc * a)) % xc == 0)) { n         cout <<  YES ; n         return 0; n       } n     } n     a = (((xb + ya) * xc) + ((yb - xa) * yc)); n     if (a % A == 0) { n       a = a / A; n       if ((yc != 0) && ((xb + ya - (xc * a)) % yc == 0)) { n         cout <<  YES ; n         return 0; n       } else if ((xc != 0) && ((yb - xa - (yc * a)) % xc == 0)) { n         cout <<  YES ; n         return 0; n       } n     } n     a = (((xb + xa) * xc) + ((yb + ya) * yc)); n     if (a % A == 0) { n       a = a / A; n       if ((yc != 0) && ((xb + xa - (xc * a)) % yc == 0)) { n         cout <<  YES ; n         return 0; n       } else if ((xc != 0) && ((yb + ya - (yc * a)) % xc == 0)) { n         cout <<  YES ; n         return 0; n       } n     } n     a = (((xb - ya) * xc) + ((yb + xa) * yc)); n     if (a % A == 0) { n       a = a / A; n       if ((yc != 0) && ((xb - ya - (xc * a)) % yc == 0)) { n         cout <<  YES ; n         return 0; n       } else if ((xc != 0) && ((yb + xa - (yc * a)) % xc == 0)) { n         cout <<  YES ; n         return 0; n       } n     } n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long array1[n]; n   for (int i = 0; i < n; i++) { n     cin >> array1[i]; n   } n   long long a, b, c, d, e, sum = 0; n   cin >> a >> b >> c >> d >> e; n   map<long long, long long> mp; n   for (int i = 0; i < n; i++) { n     sum += array1[i]; n     mp[5] += sum / e; n     sum %= e; n     mp[4] += sum / d; n     sum %= d; n     mp[3] += sum / c; n     sum %= c; n     mp[2] += sum / b; n     sum %= b; n     mp[1] += sum / a; n     sum %= a; n   } n   for (int i = 1; i <= 5; i++) { n     cout << mp[i]; n     if (i != 5) cout <<    ; n   } n   cout << endl << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a1, a2, b1, b2, c1, c2, s; n   cin >> n; n   cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2; n   s = a1 + b1 + c1; n   n = n - s; n   if (n == 0) { n     cout << a1 <<     << b1 <<     << c1 << endl; n     return 0; n   } else { n     a2 = a2 - a1; n     b2 = b2 - b1; n     c2 = c2 - c1; n     if (n <= a2) n       a1 += n; n     else { n       a1 += a2; n       n = n - a2; n       if (n == 0) { n         cout << a1 <<     << b1 <<     << c1 << endl; n         return 0; n       } n       if (n <= b2) n         b1 += n; n       else { n         b1 += b2; n         n = n - b2; n         if (n == 0) { n           cout << a1 <<     << b1 <<     << c1 << endl; n           return 0; n         } n         if (n <= c2) { n           c1 += n; n         } else { n           c1 += c2; n         } n       } n     } n   } n   cout << a1 <<     << b1 <<     << c1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, m, i, x, p = 0, kk = 100000000000000000, sum = 0; n   cin >> n >> k >> m; n   string me[n]; n   map<string, long long> fahim; n   for (i = 0; i < n; i++) { n     cin >> me[i]; n   } n   for (i = 0; i < n; i++) { n     cin >> fahim[me[i]]; n   } n   for (i = 0; i < k; i++) { n     cin >> x; n     if (x == 1) { n       cin >> x; n       continue; n     } n     if (x != 1) { n       long long d[x]; n       for (p = 0; p < x; p++) { n         cin >> d[p]; n         kk = min(kk, fahim[me[d[p] - 1]]); n       } n       for (p = 0; p < x; p++) { n         fahim[me[d[p] - 1]] = kk; n       } n     } n     kk = 100000000000000000; n   } n   string word; n   for (i = 0; i < m; i++) { n     cin >> word; n     sum += fahim[word]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int sigma = maxn; n const int HASH_CNT = 2; n int n; n char s[maxn]; n unsigned long long Prime_Pool[] = {1998585857ul, 23333333333ul}; n unsigned long long Seed_Pool[] = {146527, 19260817}; n unsigned long long Mod_Pool[] = {1000000009, 998244353}; n unsigned long long has1[26], has2[26]; n struct Hash { n   unsigned long long Seed, Mod; n   unsigned long long bas[maxn]; n   unsigned long long sum[26][maxn]; n   int perm[sigma]; n   void init(int seedIndex, int modIndex) { n     Seed = Seed_Pool[seedIndex]; n     Mod = Mod_Pool[modIndex]; n     bas[0] = 1; n     for (int i = 1; i <= n; i++) { n       bas[i] = bas[i - 1] * Seed % Mod; n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < 26; j++) { n         sum[j][i] = (sum[j][i - 1] * Seed % Mod + (s[i] == j +  a )) % Mod; n       } n     } n   } n   unsigned long long getHash(int ch, int l, int r) { n     return (sum[ch][r] - sum[ch][l - 1] * bas[r - l + 1] % Mod + Mod) % Mod; n   } n } hasher[HASH_CNT]; n bool check(int l1, int r1, int l2, int r2) { n   for (int i = 0; i < HASH_CNT; i++) { n     for (int j = 0; j < 26; j++) { n       has1[j] = hasher[i].getHash(j, l1, r1); n       has2[j] = hasher[i].getHash(j, l2, r2); n     } n     sort(has1, has1 + 26); n     sort(has2, has2 + 26); n     for (int j = 0; j < 26; j++) n       if (has1[j] != has2[j]) { n         return false; n       } n   } n   return true; n } n int main() { n   int m, x, y, len; n   scanf( %d%d , &n, &m); n   scanf( %s , s + 1); n   for (int i = 0; i < HASH_CNT; i++) hasher[i].init(i, i); n   while (m--) { n     scanf( %d%d%d , &x, &y, &len); n     if (check(x, x + len - 1, y, y + len - 1)) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n long long f[200009], dp[200009][2]; n int main() { n   string s, t; n   cin >> s >> t; n   int m = t.size(); n   t +=  # ; n   t += s; n   for (int i = 1; i < t.size(); i++) { n     int j = f[i - 1]; n     while (j > 0 and t[i] != t[j]) j = f[j - 1]; n     j += (t[i] == t[j]); n     f[i] = j; n   } n   int d = 0, ind; n   for (int i = m + 1; i < t.size(); i++) { n     if (f[i] == m) d = 1, ind = i - m; n     if (d) { n       dp[i][0] = ((ind - m + 1) + dp[i - 1][0] + dp[ind][1]) % 1000000007; n       dp[i][1] = (dp[i][0] + dp[i - 1][1]) % 1000000007; n     } n   } n   cout << dp[t.size() - 1][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 21; n void solve() { n   int n, x; n   cin >> n; n   vector<int> cnt(N, 0); n   for (int i = 0; i < n; i++) { n     cin >> x; n     for (int j = 0; j < N; j++) { n       if (x >> j & 1) cnt[j]++; n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     x = 0; n     for (int j = N - 1; j >= 0; j--) { n       if (cnt[j]) { n         cnt[j]--; n         x |= (1 << j); n       } n     } n     ans += (x * 1LL * x); n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int b[n]; n   b[0] = 0; n   for (int i = 1; i < n; i++) b[i] = abs(a[i] - a[i - 1]); n   long long int dp[n], dd[n]; n   dp[0] = 0; n   dd[0] = 0; n   for (int i = 1; i < n; i++) { n     if (i % 2 == 1) { n       dp[i] = dp[i - 1] + b[i]; n       dd[i] = max(dd[i - 1] - b[i], 0LL); n     } else { n       dp[i] = max(0LL, dp[i - 1] - b[i]); n       dd[i] = dd[i - 1] + b[i]; n     } n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) ans = max(ans, dp[i]); n   for (int i = 0; i < n; i++) ans = max(ans, dd[i]); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char str[300005]; n vector<int> idx[27]; n int main() { n   int x; n   scanf( %s , str); n   int n = strlen(str); n   for (int i = 0; i < (int)(n); i++) { n     idx[str[i] -  a ].push_back(i); n   } n   long long int ans[27] = {0}; n   int ansmax = 0; n   for (int i = 0; i < (int)(n); i++) { n     set<int> fir; n     for (int j = 0; j < (int)(26); j++) { n       vector<int>::iterator it = lower_bound(idx[j].begin(), idx[j].end(), i); n       if (it != idx[j].end()) fir.insert(*it); n     } n     int p = i; n     int c = 0; n     for (__typeof((fir).begin()) it = (fir).begin(); it != (fir).end(); ++it) { n       ans[c++] += *it - p; n       p = *it; n     } n     ans[c] += n - p; n     ansmax = max(ansmax, c); n   } n   cout << ansmax << endl; n   for (int i = 0; i < (int)(ansmax); i++) cout << ans[i + 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(int a, int b) { return (a > b); } n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main(void) { n   long long int n, m, k; n   cin >> n >> m >> k; n   if (k > (n + m - 2)) { n     cout <<  -1 ; n     return 0; n   } n   long long int ans; n   if (m > n) swap(n, m); n   if (k <= m - 1) n     ans = max(m / (k + 1) * n, n / (k + 1) * m); n   else if (k <= n - 1) n     ans = n / (k + 1) * m; n   else n     ans = m / (k - n + 2); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const long long &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n int main() { n   long long n, a, b, k; n   R(n, a, b, k); n   vector<long long> h(n); n   for (int i = 0; i < (n); ++i) { n     R(h[i]); n   } n   long long res = 0; n   vector<long long> kk; n   for (int i = 0; i < (n); ++i) { n     if ((h[i] % (a + b)) <= a && (h[i] % (a + b)) > 0) { n       ++res; n     } else { n       long long rest = h[i] - (h[i] / (a + b)) * (a + b); n       rest -= a; n       if (h[i] % (a + b) == 0) { n         rest += a + b; n       } n       long long r2 = rest; n       rest /= a; n       if (r2 % a > 0) { n         rest += 1; n       } n       kk.push_back(rest); n     } n   } n   std::sort(kk.begin(), kk.end()); n   for (int i = 0; i < (kk.size()); ++i) { n     if (kk[i] <= k) { n       ++res; n       k -= kk[i]; n     } else { n       break; n     } n   } n   W(res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[200005], b[200005], s[200005], t[200005], dp[200005]; n vector<int> v[200005]; n bool flag; n void dfs(int x, int y) { n   s[x] = 1; n   dp[x] = t[x] = b[x]; n   int best = 0; n   for (int i = (0); i < (v[x].size()); i++) { n     int V = v[x][i]; n     if (V == y) continue; n     dfs(V, x); n     s[x] += s[V]; n     t[x] += t[V]; n     if (s[V] == t[V]) n       dp[x] += s[V]; n     else n       best = max(best, dp[V]); n   } n   dp[x] += best; n   if (!b[x]) dp[x] = 0; n } n void dfs2(int x, int y) { n   int best = 0, ch = -1, sec = 0; n   dp[x] = t[x] = b[x]; n   s[x] = 1; n   for (int i = (0); i < (v[x].size()); i++) { n     int V = v[x][i]; n     s[x] += s[V]; n     t[x] += t[V]; n     if (s[V] > t[V]) { n       if (best <= dp[V]) { n         sec = best; n         best = dp[V]; n         ch = V; n       } else if (dp[V] > sec) n         sec = dp[V]; n     } else n       dp[x] += s[V]; n   } n   dp[x] += best; n   if (!b[x]) dp[x] = 0; n   if (dp[x] >= m) flag = 1; n   for (int i = (0); i < (v[x].size()); i++) { n     int V = v[x][i]; n     if (V == y) continue; n     int ori = dp[x], ot = t[x], os = s[x]; n     if (s[V] == t[V]) n       dp[x] -= t[V]; n     else if (ch == V) n       dp[x] -= best - sec; n     if (!b[x]) dp[x] = 0; n     t[x] -= t[V]; n     s[x] -= s[V]; n     dfs2(V, x); n     dp[x] = ori; n     t[x] = ot; n     s[x] = os; n   } n } n bool ok(int l) { n   for (int i = (1); i <= (n); i++) b[i] = a[i] >= l; n   dfs(1, 0); n   flag = 0; n   dfs2(1, 0); n   return flag; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (int i = (1); i < (n); i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   int l = 1, h = 1000000; n   while (l < h) { n     int m = l + h + 1 >> 1; n     if (ok(m)) n       l = m; n     else n       h = m - 1; n   } n   printf( %d n , l); n } n


#include <bits/stdc++.h> n const int maxn = 5e5 + 5; n using namespace std; n int a[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   long long ans = 0, sum = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     int del = min((long long)a[i] >> 1, sum); n     ans += del; n     sum -= del; n     a[i] -= del << 1; n     ans += a[i] / 3; n     a[i] %= 3; n     sum += a[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000 + 10; n const int maxa = 10000000 + 10; n const int inf = (-1u) >> 1; n struct disjoint_set { n   int p[maxn]; n   void clear(int sz) { n     for (int i = (1); i <= (sz); ++i) { n       p[i] = i; n     } n   } n   int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } n   bool join(int x, int y) { n     x = find(x), y = find(y); n     if (x == y) return false; n     p[x] = y; n     return true; n   } n }; n int id[maxa], n; n disjoint_set ds; n bool check(int x, int y) { return id[x] && id[y] && ds.join(id[x], id[y]); } n int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) { n     int t; n     scanf( %d , &t); n     id[t] = i; n   } n   ds.clear(n); n   int ans = n; n   for (int s = 1; s <= 6000; ++s) { n     for (int t = (s & 1) + 1; t < s; t += 2) { n       if (gcd(s, t) != 1) continue; n       int x = 2 * s * t; n       int y = s * s - t * t; n       int z = s * s + t * t; n       if (x >= maxa || y >= maxa) { n         continue; n       } n       ans -= check(x, y); n       if (z < maxa) { n         ans -= check(x, z); n         ans -= check(y, z); n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1003; n const int xx[4] = {0, 1, 0, -1}; n const int yy[4] = {1, 0, -1, 0}; n int x[3], y[3]; n void pr(int x, int y, int fx, int fy) { n   while (x < fx) { n     ++x; n     printf( %d %d n , x, y); n   } n   while (x > fx) { n     --x; n     printf( %d %d n , x, y); n   } n   while (y < fy) { n     ++y; n     printf( %d %d n , x, y); n   } n   while (y > fy) { n     --y; n     printf( %d %d n , x, y); n   } n } n int main() { n   for (int i = 0; i < 3; ++i) scanf( %d%d , &x[i], &y[i]); n   int ans = N * N; n   int ansx, ansy; n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < N; ++j) { n       int yans = 0; n       for (int k = 0; k < 3; ++k) yans += (abs(i - x[k]) + abs(j - y[k]) + 1); n       yans -= 2; n       if (yans < ans) { n         ans = yans; n         ansx = i; n         ansy = j; n       } n     } n   } n   printf( %d n , ans); n   printf( %d %d n , ansx, ansy); n   for (int i = 0; i < 3; ++i) pr(ansx, ansy, x[i], y[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int noOfOne(int n) { n   int c = 0; n   while (n) { n     c += (n & 1); n     n >>= 1; n   } n   return c; n } n unsigned long long int f(unsigned long long int n, unsigned long long int r) { n   if (n - r > r) { n     unsigned long long int temp = n, temp2 = r; n     unsigned long long int ans = 1; n     while (temp > (n - r)) { n       ans *= temp; n       --temp; n     } n     while (temp2) { n       ans /= temp2; n       --temp2; n     } n     return ans; n   } n   unsigned long long int temp = n, temp2 = n - r; n   unsigned long long int ans = 1; n   while (temp > r) { n     ans *= temp; n     --temp; n   } n   while (temp2) { n     ans /= temp2; n     --temp2; n   } n   return ans; n } n const unsigned long long int N = 1e6 + 5; n const unsigned long long int MOD = 1e9 + 7; n unsigned long long int n, cnt[N], ans = 0; n int32_t main() { n   cin >> n; n   unsigned long long int num; n   cnt[0] = 1; n   for (int i = 1; i <= n; i++) { n     cin >> num; n     vector<unsigned long long int> cur; n     for (unsigned long long int j = 1; j * j <= num; j++) { n       if (num % j == 0) { n         cur.push_back(j); n         if (j != num / j) cur.push_back(num / j); n       } n     } n     sort(cur.begin(), cur.end()); n     for (int i = cur.size() - 1; i >= 0; i--) { n       cnt[cur[i]] += cnt[cur[i] - 1]; n       cnt[cur[i]] %= MOD; n     } n   } n   for (int i = 1; i <= n && cnt[i] != 0; i++) ans += cnt[i]; n   ans %= MOD; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const int mod = inf + 7; n const double eps = 1e-9; n const double pi = acos(-1.0); n long double a, b, c, d; n pair<long double, long double> get(pair<long double, long double> a, n                                    pair<long double, long double> b) { n   long double x[4] = {a.first * b.first, a.second * b.first, a.first * b.second, n                       a.second * b.second}; n   sort(x, x + 4); n   return make_pair(x[0], x[3]); n } n bool ok(pair<long double, long double> a, pair<long double, long double> b) { n   long double l = max(a.first, b.first); n   long double r = min(a.second, b.second); n   return l < r + 1e-21; n } n bool ok(long double ans) { n   pair<long double, long double> x = make_pair(a - ans, a + ans); n   pair<long double, long double> y = make_pair(b - ans, b + ans); n   pair<long double, long double> z = make_pair(c - ans, c + ans); n   pair<long double, long double> t = make_pair(d - ans, d + ans); n   pair<long double, long double> first = get(x, t); n   pair<long double, long double> second = get(y, z); n   return ok(first, second); n } n long double mx(long double a, long double b, long double c, long double d) { n   return max(abs(a), max(abs(b), max(abs(c), abs(d)))); n } n int main() { n   cin >> a >> b >> c >> d; n   long double l = 0, r = mx(a, b, c, d); n   for (int i = 0; i < 20000; i++) { n     long double mid = (l + r) / 2.0; n     if (ok(mid)) { n       r = mid; n     } else n       l = mid; n   } n   printf( %.10f n , (double)r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, p, k, pre[100005], f[100005][128]; n struct mem { n   long long p[7], a; n   bool operator<(const mem &v) const { return a > v.a; } n } c[100005]; n int main() { n   scanf( %lld%lld%lld , &n, &p, &k); n   for (int i = 1; i <= n; i++) scanf( %lld , &c[i].a); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < p; j++) scanf( %lld , &c[i].p[j]); n   sort(c + 1, c + n + 1); n   for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + c[i].a; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < 1 << p; j++) { n       int sz = 0; n       for (int k = 0; k < p; k++) sz += (j >> k) & 1; n       f[i][j] = f[i - 1][j]; n       if (k + sz >= i && sz <= i - 1) f[i][j] += c[i].a; n       for (int k = 0; k < p; k++) n         if ((j >> k) & 1) { n           int q = j ^ (1 << k); n           f[i][j] = max(f[i][j], f[i - 1][q] + c[i].p[k]); n         } n     } n   cout << f[n][(1 << p) - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __timestamper {}; n vector<string> solve(vector<int> a) { n   int n = a.size(); n   for (int i = 0; i < n - 1; i++) assert(a[i] >= a[i + 1]); n   if (n == 0) return vector<string>(); n   if (a[0] == 1) { n     for (int x : a) assert(x == 1); n     vector<string> res; n     res.push_back(string(n,  1 )); n     return res; n   } n   if (a.back() == n) { n     vector<string> res(n + 1); n     for (int i = 0; i <= n; i++) { n       res[i] = string(n,  1 ); n       if (i > 0) res[i][i - 1] =  0 ; n     } n     return res; n   } n   int x = a.back(); n   assert(x < n); n   a.pop_back(); n   string cur(n - 1,  0 ); n   for (int i = 0; i < n - 1; i++) { n     if (a[i] == n) { n       a[i]--; n       cur[i] =  1 ; n     } n   } n   auto res = solve(a); n   int left = x - 1; n   for (int i = 0; i < (int)res.size(); i++) { n     char c =  0 ; n     if (left > 0 && res[i] != cur) { n       left--; n       c =  1 ; n     } n     res[i] += c; n   } n   cur +=  1 ; n   res.push_back(cur); n   return res; n } n vector<string> fast(vector<int> a) { n   int n = a.size(); n   vector<pair<int, int>> b(n); n   for (int i = 0; i < n; i++) b[i] = {a[i], i}; n   sort(b.rbegin(), b.rend()); n   vector<int> c(n); n   for (int i = 0; i < n; i++) c[i] = b[i].first; n   auto s = solve(c); n   vector<string> res; n   for (auto o : s) { n     string t(n,  . ); n     for (int j = 0; j < n; j++) { n       t[b[j].second] = o[j]; n     } n     res.push_back(t); n   } n   sort(res.begin(), res.end()); n   for (int i = 0; i < (int)res.size() - 1; i++) assert(res[i] != res[i + 1]); n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = 0; j < (int)res.size(); j++) { n       cnt += res[j][i] ==  1 ; n     } n     assert(cnt == a[i]); n   } n   return res; n } n void stress() { n   for (int it = 59;; it++) { n     mt19937 rnd(it); n     cerr <<  it  n          <<   =   << it << endl; n     int n = 1e3; n     vector<int> a(n); n     for (int i = 0; i < n; i++) a[i] = rnd() % n + 1; n     cerr <<  a  n          <<   =  ; n     for (auto xxxx : a) cerr << xxxx <<    ; n     cerr << endl; n     fast(a); n   } n } n int main() { n   int n; n   while (scanf( %d , &n) == 1) { n     vector<int> a(n); n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     auto res = fast(a); n     printf( %d n , (int)res.size()); n     for (auto s : res) printf( %s n , s.c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const K = 12; n int const N = 5e4 + 123; n int n, k; n int s[N][K], maxi[N][K], mini[N][K]; n int ans[N]; n int cnt[N]; n struct Player { n   int id, v; n   Player(){}; n   Player(int id, int v) : id(id), v(v){}; n   bool operator<(const Player &a) const { n     if (v != a.v) return v < a.v; n     return id < a.id; n   }; n }; n set<Player> t[K]; n Player winner = Player(-1, -1); n void pushplayer(int id) { n   for (int i = 0; i < k; ++i) { n     t[i].insert(Player(id, maxi[id][i])); n   } n } n void remove(int id) { n   for (int i = 0; i < k; ++i) { n     t[i].erase(Player(id, maxi[id][i])); n   } n } n void merge(int ida, int idb) { n   int idc = min(ida, idb); n   winner.id = idc; n   cnt[idc] = cnt[ida] + cnt[idb]; n   for (int i = 0; i < k; ++i) { n     mini[idc][i] = min(mini[idb][i], mini[ida][i]); n     maxi[idc][i] = max(maxi[idb][i], maxi[ida][i]); n   } n } n bool canbeat(int ida, int idb) { n   for (int i = 0; i < k; ++i) n     if (maxi[ida][i] > mini[idb][i]) return true; n   return false; n } n void solve() { n   ans[0] = 1; n   winner = Player(0, -1); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < k; ++j) mini[i][j] = maxi[i][j] = s[i][j]; n   for (int i = 0; i < n; ++i) cnt[i] = 1; n   for (int i = 1; i < n; ++i) { n     bool f; n     if (canbeat(i, winner.id)) { n       if (canbeat(winner.id, i)) { n         merge(i, winner.id); n         f = true; n       } else { n         pushplayer(winner.id); n         winner = Player(i, -1); n         f = false; n       } n     } else { n       pushplayer(i); n       f = false; n     } n     while (f) { n       f = false; n       Player bf = Player(-1, -1); n       for (int j = 0; j < k; ++j) { n         if ((int)(t[j].size())) { n           set<Player>::iterator it = t[j].end(); n           --it; n           Player b = (*it); n           if (canbeat(b.id, winner.id)) { n             bf = b; n             f = true; n             break; n           } n         } n       } n       if (!f) break; n       remove(bf.id); n       merge(winner.id, bf.id); n     } n     ans[i] = cnt[winner.id]; n   } n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < k; ++j) scanf( %d , &s[i][j]); n   solve(); n   for (int i = 0; i < n; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n; n   cout << (((1ll << (n / 2)) - 1) * 2) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ara[505][505]; n bool row_check(long long val) { n   for (int i = 0; i < n; i++) { n     long long tmp = 0; n     for (int j = 0; j < n; j++) { n       tmp += ara[i][j]; n     } n     if (tmp != val) return false; n   } n   return true; n } n bool col_check(long long val) { n   for (int i = 0; i < n; i++) { n     long long tmp = 0; n     for (int j = 0; j < n; j++) { n       tmp += ara[j][i]; n     } n     if (tmp != val) return false; n   } n   return true; n } n bool diagonal_check(long long val) { n   long long tmp = 0; n   for (int i = 0; i < n; i++) tmp += ara[i][i]; n   if (tmp != val) return false; n   tmp = 0; n   for (int i = 0; i < n; i++) { n     tmp += ara[i][n - 1 - i]; n   } n   return (tmp == val); n } n bool check() { n   long long row = 0; n   for (int i = 0; i < n; i++) row += ara[i][0]; n   return row_check(row) & col_check(row) & diagonal_check(row); n } n int main() { n   ios_base::sync_with_stdio(false); n   while (cin >> n) { n     int r, c; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         cin >> ara[i][j]; n         if (ara[i][j] == 0) { n           r = i; n           c = j; n         } n       } n     } n     if (n > 1) { n       long long sum = 0; n       int x = 0; n       if (r == 0) x++; n       for (int i = 0; i < n; i++) sum += ara[x][i]; n       for (int i = 0; i < n; i++) sum -= ara[r][i]; n       if (sum > 0) { n         ara[r][c] = sum; n         if (check()) { n           cout << ara[r][c] << endl; n         } else { n           printf( -1 n ); n         } n       } else { n         printf( -1 n ); n       } n     } else n       printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int T, n, i, j, l, m, x, y, z, a, b, c, d, flag, count; n   cin >> a >> b >> c >> d; n   long long int sum1, sum2; n   sum1 = std::max((3 * a / 10), (a - ((a / 250) * c))); n   sum2 = std::max((3 * b / 10), (b - ((b / 250) * d))); n   if (sum1 > sum2) n     cout <<  Misha  << endl; n   else if (sum1 < sum2) n     cout <<  Vasya  << endl; n   else n     cout <<  Tie  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   string name; n   int id; n   int taxi, pizza, girl; n } arr[105]; n int cmp1(node a, node b) { n   if (a.taxi == b.taxi) return a.id < b.id; n   return a.taxi > b.taxi; n } n int cmp2(node a, node b) { n   if (a.pizza == b.pizza) return a.id < b.id; n   return a.pizza > b.pizza; n } n int cmp3(node a, node b) { n   if (a.girl == b.girl) return a.id < b.id; n   return a.girl > b.girl; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int m; n     cin >> m >> arr[i].name; n     arr[i].id = i; n     for (int j = 0; j < m; j++) { n       int a, b, c; n       scanf( %d-%d-%d , &a, &b, &c); n       if (a % 10 == a / 10 && a == b && b == c) n         arr[i].taxi++; n       else if (a / 10 > a % 10 && a % 10 > b / 10 && b / 10 > b % 10 && n                b % 10 > c / 10 && c / 10 > c % 10) n         arr[i].pizza++; n       else n         arr[i].girl++; n     } n   } n   sort(arr, arr + n, cmp1); n   printf( If you want to call a taxi, you should call:  ); n   int first = 1; n   for (int i = 0; i < n; i++) { n     if (arr[i].taxi != arr[0].taxi) break; n     if (!first) cout <<  ,  ; n     cout << arr[i].name; n     first = 0; n   } n   cout <<  .  << endl; n   sort(arr, arr + n, cmp2); n   printf( If you want to order a pizza, you should call:  ); n   first = 1; n   for (int i = 0; i < n; i++) { n     if (arr[i].pizza != arr[0].pizza) break; n     if (!first) cout <<  ,  ; n     cout << arr[i].name; n     first = 0; n   } n   cout <<  .  << endl; n   sort(arr, arr + n, cmp3); n   printf( n        If you want to go to a cafe with a wonderful girl, you should call:  ); n   first = 1; n   for (int i = 0; i < n; i++) { n     if (arr[i].girl != arr[0].girl) break; n     if (!first) cout <<  ,  ; n     cout << arr[i].name; n     first = 0; n   } n   cout <<  .  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 5; n const int MOD = 1e9 + 7; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n), b(m + 1), la(m + 1); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 1; i < m + 1; ++i) cin >> b[i]; n   int mid = 0, l = 0, r = n - 1, ans = -1; n   while (l <= r) { n     mid = (l + r) / 2; n     for (int i = 0; i < m + 1; ++i) la[i] = -1; n     for (int i = 0; i < mid + 1; ++i) { n       if (a[i] != 0) la[a[i]] = i; n     } n     int p = 0, ctr = 0; n     for (int i = 0; i < mid + 1; ++i) { n       if (la[a[i]] != i) n         ctr++; n       else if (ctr >= b[a[i]]) { n         ctr -= b[a[i]]; n         p++; n       } else n         break; n     } n     if (p == m) { n       ans = mid + 1; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   cout << ans; n } n int main() { n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   t = 1; n   while (t--) { n     stack<int> st; n     int n, x, ans = 0; n     scanf( %d , &n); n     for (int i = 0; i < n; ++i) { n       scanf( %d , &x); n       while (!st.empty()) { n         ans = max(ans, st.top() ^ x); n         if (st.top() > x) break; n         st.pop(); n       } n       st.push(x); n     } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n unsigned bactery_count(unsigned num) { n   unsigned res = 0; n   while (num > 0) { n     (num & true) ? ++res, --num : num >>= 1; n   } n   return res; n } n int main() { n   unsigned num; n   std::cin >> num; n   std::cout << bactery_count(num) << std::endl; n   return 0; n } n