#include <bits/stdc++.h> n using namespace std; n long long ans; n vector<long long> all, aa, bb; n int main() { n   aa.clear(); n   bb.clear(); n   all.clear(); n   long long a, b; n   cin >> a >> b; n   ans = 2 * (a + b + 1); n   for (long long i = 1; i * i <= (a + b); i++) n     if ((a + b) % i == 0) all.push_back(i); n   for (long long i = 1; i * i <= a; i++) n     if (a % i == 0) aa.push_back(i); n   for (long long i = 1; i * i <= b; i++) n     if (b % i == 0) bb.push_back(i); n   int now1 = 0, now2 = 0; n   while (now1 < aa.size() && now2 < all.size()) { n     if (aa[now1] <= all[now2] && (a / aa[now1]) <= (a + b) / (all[now2])) { n       ans = min(ans, 2 * (all[now2] + (a + b) / all[now2])); n       now2++; n     } else if (aa[now1] > all[now2]) n       now2++; n     else n       now1++; n   } n   now1 = 0, now2 = 0; n   while (now1 < bb.size() && now2 < all.size()) { n     if (bb[now1] <= all[now2] && (b / bb[now1]) <= (a + b) / (all[now2])) { n       ans = min(ans, 2 * (all[now2] + (a + b) / all[now2])); n       now2++; n     } else if (bb[now1] > all[now2]) n       now2++; n     else n       now1++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d; n int v[50 + 5]; n bool F[5000000 + 5]; n int main() { n   scanf( %d%d , &n, &d); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n   F[0] = true; n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     sum += v[i]; n     for (int j = sum; j >= v[i]; j--) F[j] |= F[j - v[i]]; n   } n   int ans = 0, day = 0; n   bool found = false; n   while (1) { n     for (int i = d; i >= 1; i--) { n       if (F[ans + i]) { n         ans += i; n         day++; n         found = true; n         break; n       } n     } n     if (!found) n       break; n     else n       found = false; n   } n   printf( %d %d n , ans, day); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000010; n unordered_map<int, vector<pair<int, int> > > rows; n unordered_map<int, vector<pair<int, int> > > cols; n int n, m; n int cnum; n map<pair<int, int>, int> id; n map<pair<int, int>, vector<pair<int, int> > > adj1; n unordered_map<int, vector<int> > adj2; n int dp[MAXN]; n void fgreat(const vector<pair<int, int> >& v, int row, int col) { n   int sz = v.size(); n   for (int i = 0; i < (sz - 1); ++i) n     if (v[i].first < v[i + 1].first) { n       if (row == -1) { n         adj2[id[make_pair(v[i + 1].second, col)]].push_back( n             id[make_pair(v[i].second, col)]); n       } else { n         adj2[id[{row, v[i + 1].second}]].push_back( n             id[make_pair(row, v[i].second)]); n       } n     } n } n void fequal(const vector<pair<int, int> >& v, int row, int col) { n   int sz = v.size(); n   for (int i = 0; i < (sz - 1); ++i) n     if (v[i].first == v[i + 1].first) { n       if (row == -1) { n         adj1[{v[i].second, col}].push_back(make_pair(v[i + 1].second, col)); n         adj1[{v[i + 1].second, col}].push_back(make_pair(v[i].second, col)); n       } else { n         adj1[{row, v[i].second}].push_back(make_pair(row, v[i + 1].second)); n         adj1[{row, v[i + 1].second}].push_back(make_pair(row, v[i].second)); n       } n     } n } n void dfs(int sx, int sy) { n   id[{sx, sy}] = cnum; n   const auto& v = adj1[{sx, sy}]; n   for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) n     if (!id.count(*it)) { n       dfs(it->first, it->second); n     } n } n int solve(int id) { n   int& ans = dp[id]; n   if (ans != -1) return ans; n   ans = 0; n   const auto& v = adj2[id]; n   for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) n     ans = max(ans, solve(*it)); n   return ++ans; n } n int main() { n   scanf(  %d %d , &(n), &(m)); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (m); ++j) { n       int x; n       scanf(  %d , &(x)); n       rows[i].push_back(make_pair(x, j)); n       cols[j].push_back(make_pair(x, i)); n     } n   for (__typeof((rows).begin()) it = (rows).begin(); it != (rows).end(); ++it) { n     auto& v = it->second; n     sort(v.begin(), v.end()); n   } n   for (__typeof((cols).begin()) it = (cols).begin(); it != (cols).end(); ++it) { n     auto& v = it->second; n     sort(v.begin(), v.end()); n   } n   for (__typeof((rows).begin()) it = (rows).begin(); it != (rows).end(); ++it) { n     fequal(it->second, it->first, -1); n   } n   for (__typeof((cols).begin()) it = (cols).begin(); it != (cols).end(); ++it) { n     fequal(it->second, -1, it->first); n   } n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (m); ++j) n       if (!id.count({i, j})) { n         dfs(i, j); n         ++cnum; n       } n   for (__typeof((rows).begin()) it = (rows).begin(); it != (rows).end(); ++it) { n     fgreat(it->second, it->first, -1); n   } n   for (__typeof((cols).begin()) it = (cols).begin(); it != (cols).end(); ++it) { n     fgreat(it->second, -1, it->first); n   } n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) { n       if (j) printf(   ); n       printf( %d , solve(id[{i, j}])); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string c1, c2; n int main() { n   int x; n   cin >> c1 >> c2; n   for (int i = 0; i < c1.size(); i++) { n     if (c1[i] < 92) { n       c1[i] += 32; n     } n     if (c2[i] < 92) { n       c2[i] += 32; n     } n   } n   if (c1 < c2) { n     x = -1; n   } else if (c1 > c2) { n     x = 1; n   } else { n     x = 0; n   } n   printf( %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 10010100; n const int MAZN = 301010; n int ch[MAXN][3]; n int n, cnt, cout[MAXN]; n int arr[MAZN]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n   } n   for (int i = 1; i <= n; i++) { n     int tem, p = 0; n     scanf( %d , &tem); n     for (int k = 29; k >= 0; k--) { n       int tmp = (tem >> k) & 1; n       if (ch[p][tmp]) n         p = ch[p][tmp]; n       else n         p = ch[p][tmp] = ++cnt; n       cout[p]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     int x = arr[i]; n     int ans = 0, p = 0; n     for (int j = 29; j >= 0; j--) { n       int tmp = (x >> j) & 1; n       if (ch[p][tmp] && cout[ch[p][tmp]]) { n         p = ch[p][tmp]; n       } else { n         p = ch[p][!tmp]; n         ans += (1 << j); n       } n       cout[p]--; n     } n     printf( %d  , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, ans = 0; n   string s; n   cin >> n; n   while (cin >> s) { n     int cnt = 0; n     for (int i = 0; i < s.length(); ++i) { n       if (s[i] >=  A  && s[i] <=  Z ) { n         ++cnt; n       } n     } n     ans = max(ans, cnt); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   char cc = getchar(); n   int cn = 0, flus = 1; n   while (cc <  0  || cc >  9 ) { n     if (cc ==  - ) flus = -flus; n     cc = getchar(); n   } n   while (cc >=  0  && cc <=  9 ) cn = cn * 10 + cc -  0 , cc = getchar(); n   return cn * flus; n } n const int N = 1e5 + 5; n int n, m, q, L[N]; n struct SegmentBeats { n   int bef, mx, se, tot, tag; n } tr[N << 2]; n void cmax(int &x, int y) { x = (x < y) ? y : x; } n inline void pushup(int x) { n   tr[x].tot = tr[x].se = 0, n   tr[x].mx = max(tr[(x << 1)].mx, tr[(x << 1 | 1)].mx); n   if (tr[x].mx == tr[(x << 1)].mx) n     tr[x].tot += tr[(x << 1)].tot, cmax(tr[x].se, tr[(x << 1)].se); n   else n     cmax(tr[x].se, tr[(x << 1)].mx); n   if (tr[x].mx == tr[(x << 1 | 1)].mx) n     tr[x].tot += tr[(x << 1 | 1)].tot, cmax(tr[x].se, tr[(x << 1 | 1)].se); n   else n     cmax(tr[x].se, tr[(x << 1 | 1)].mx); n   tr[x].bef = tr[x].mx; n } n inline void push(int x, int k) { tr[x].mx = k, tr[x].tag = k; } n inline void pushmark(int x) { n   if (!tr[x].tag) return; n   if (tr[(x << 1)].mx == tr[x].bef) push((x << 1), tr[x].tag); n   if (tr[(x << 1 | 1)].mx == tr[x].bef) push((x << 1 | 1), tr[x].tag); n   tr[x].tag = 0, tr[x].bef = tr[x].mx; n } n int query(int x, int l, int r, int k) { n   if (l == r) return tr[x].mx; n   int mid = (l + r) >> 1; n   pushmark(x); n   if (k <= mid) n     return query((x << 1), l, mid, k); n   else n     return query((x << 1 | 1), mid + 1, r, k); n } n void update(int x, int l, int r, int k, int d) { n   if (l == r) return tr[x].mx = d, tr[x].se = 0, tr[x].tot = 1, void(); n   int mid = (l + r) >> 1; n   pushmark(x); n   if (k <= mid) n     update((x << 1), l, mid, k, d); n   else n     update((x << 1 | 1), mid + 1, r, k, d); n   pushup(x); n } n void modify(int x, int l, int r, int ql, int qr, int y, int z) { n   if (tr[x].mx < y || r < ql || qr < l) return; n   if ((ql <= l && r <= qr) && (tr[x].se < y && tr[x].mx >= y)) n     return push(x, z); n   int mid = (l + r) >> 1; n   pushmark(x); n   modify((x << 1), l, mid, ql, qr, y, z), n       modify((x << 1 | 1), mid + 1, r, ql, qr, y, z), pushup(x); n } n vector<pair<int, int> > p[N]; n int ans[N]; n signed main() { n   n = gi(), m = gi(); n   int x, y; n   for (int i = (1); i <= (m); ++i) x = gi(), y = gi(), L[y] = x; n   q = gi(); n   for (int i = (1); i <= (q); ++i) n     x = gi(), y = gi(), p[y].push_back(make_pair(x, i)); n   for (int i = (1); i <= (n); ++i) { n     update(1, 1, n, i, i); n     if (L[i]) modify(1, 1, n, 1, L[i], L[i], i); n     for (auto u : p[i]) ans[u.second] = query(1, 1, n, u.first); n   } n   for (int i = (1); i <= (q); ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 3003; n const long long mod = 998244353; n using namespace std; n int n; n int c; n int a[N]; n int p[N]; n int res[N]; n int d[N][N]; n int g[N][N]; n int f1[N][1 << 12]; n void upd(int &x, int y) { n   x += y; n   if (x >= mod) { n     x -= mod; n   } else if (x < 0) { n     x += mod; n   } n } n void solve1() { n   int nj, nk, last = (1 << c) - 1; n   for (int i = 1; i <= n; i++) { n     for (int j = (i - 1) / c; j >= 0; j--) { n       for (int k = last - 1; k >= 0; k--) { n         nj = j; n         nk = k | (1 << a[i]); n         if (nk == last) { n           nj += 1; n           nk = 0; n           upd(d[i][nj], f1[j][k]); n         } n         upd(f1[nj][nk], f1[j][k]); n       } n     } n     if (c == 1) { n       upd(d[i][1], 1); n       upd(f1[1][0], 1); n     } else { n       upd(f1[0][1 << a[i]], 1); n     } n   } n } n long long pw(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) { n       res = res * x % mod; n     } n     x = x * x % mod; n     n /= 2; n   } n   return res; n } n int t[N]; n int inv[N]; n void solve2() { n   for (int i = 1; i < n; i++) { n     inv[i] = pw(p[i] - 1, mod - 2); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < c; j++) { n       t[j] = 0; n     } n     int j = i - 1; n     int sad = c; n     while (j < n && sad) { n       j += 1; n       t[a[j]] += 1; n       sad -= t[a[j]] == 1; n     } n     if (sad) { n       break; n     } n     long long res = 1; n     for (int h = 0; h < c; h++) { n       res = res * (p[t[h]] - 1) % mod; n     } n     sad = 1; n     for (; j <= n; j++) { n       res = res * inv[t[a[j]]] % mod; n       g[i][j] = res; n       if (sad) { n         sad = 0; n       } else { n         t[a[j]] += 1; n       } n       res = res * (p[t[a[j]]] - 1) % mod; n     } n   } n   d[0][0] = 1; n   int k = n / c; n   d[0][0] = 1; n   long long sum, MOD = mod, MOD2 = mod * mod; n   for (int r = 1; r <= k; r++) { n     d[0][r] = 0; n     for (int i = r * c - 1; i < n; i++) { n       sum = 0; n       for (int j = i; j >= (r - 1) * c; j--) { n         sum += 1LL * g[j + 1][i + 1] * d[j][r - 1]; n         if (sum >= MOD2) sum -= MOD2; n       } n       d[i + 1][r] = (sum % MOD); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> c; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] -= 1; n   } n   p[0] = 1; n   for (int i = 1; i <= n; i++) { n     p[i] = p[i - 1]; n     upd(p[i], p[i]); n   } n   if (c <= 10) { n     solve1(); n   } else { n     solve2(); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       upd(res[j], 1ll * d[i][j] * p[n - i] % mod); n     } n   } n   res[0] = p[n]; n   upd(res[0], -1); n   for (int i = 0; i <= n; i++) { n     upd(res[i], -res[i + 1]); n     cout << res[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n const long long inf = 0x3f3f3f3f3f3f3f3f; n string s; n int a[maxn]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int flag = 0; n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     for (int i = 1; i < n; i++) { n       if (a[i] - a[i - 1] == 1) { n         flag = 1; n         break; n       } n     } n     if (flag) n       cout << 2 << endl; n     else n       cout << 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int MOD = 1000000007; n void solve() { n   int n; n   cin >> n; n   vector<vector<int>> dp(4, vector<int>(n + 1, 0)); n   dp[3][0] = 1; n   for (int i = 1; i < n + 1; i++) n     for (int j = 0; j < 4; j++) n       for (int k = 0; k < 4; k++) n         if (k != j) dp[j][i] = (dp[j][i] + dp[k][i - 1]) % MOD; n   cout << dp[3][n] <<   n ; 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   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long l, r, n, k; n string Bad; n int ask(long long l, long long r) { n   cout << l <<     << r << endl; n   cout.flush(); n   cin >> Bad; n   if (Bad[0] ==  B ) exit(0); n   if (Bad[0] ==  Y ) { n     if (l == r) exit(0); n     return 1; n   } n   return 0; n } n int main() { n   srand(2109888); n   cin >> n >> k; n   l = 1; n   r = n; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (ask(l, mid)) n       r = mid; n     else n       l = mid + 1; n     l = max(1ll, l - k); n     r = min(n, r + k); n     mid = l + rand() % (r - l + 1); n     ask(mid, mid); n     l = max(1ll, l - k); n     r = min(n, r + k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nn = 100005; n const double eps = 1e-10; n struct point { n   double x, y; n } a[nn], b[nn]; n int n, m, t, l[nn], r[nn]; n int h[nn], p[nn]; n double R, c[nn]; n inline double cp(const point &x, const point &y, const point &z) { n   return (x.x - z.x) * (y.y - z.y) - (x.y - z.y) * (y.x - z.x); n } n inline double dis(const point &x, const point &y) { n   return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y)); n } n inline bool cmp(const point &x, const point &y) { n   double z = cp(x, y, a[1]); n   if (z > eps) return 1; n   if (z < -eps) return 0; n   return dis(x, a[1]) < dis(y, a[1]); n } n void init() { n   scanf( %d%lf , &n, &R); n   for (int i = 1; i <= n; ++i) { n     scanf( %lf%lf , &a[i].x, &a[i].y); n     if (a[i].y < a[1].y || (a[i].y < a[1].y + eps && a[i].x < a[1].x)) n       swap(a[i], a[1]); n   } n   sort(a + 2, a + n + 1, cmp); n   b[m = 1] = a[1]; n   for (int i = 2; i <= n; ++i) { n     for (; m > 1 && cp(a[i], b[m], b[m - 1]) > -eps; --m) n       ; n     b[++m] = a[i]; n   } n } n inline void change(int x, int y) { n   swap(p[h[x]], p[h[y]]); n   swap(h[x], h[y]); n } n void up(int x) { n   for (; x > 1 && c[h[x]] > c[h[x >> 1]]; x >>= 1) change(x, x >> 1); n } n void down(int x) { n   for (int y = x << 1; y <= t; x = y, y = x << 1) { n     if (y < t && c[h[y]] < c[h[y + 1]]) ++y; n     if (c[h[x]] < c[h[y]]) n       change(x, y); n     else n       break; n   } n } n inline void calc(int x) { n   point y = b[l[x]], z = b[r[x]]; n   c[x] = dis(y, z) * dis(b[x], y) * dis(b[x], z) / (2 * abs(cp(y, z, b[x]))); n   up(p[x]); n } n inline double count(int x, int y) { n   double z = 2 * asin(dis(b[x], b[y]) / (2 * R)); n   return R * R / 2 * (z - sin(z)); n } n void work() { n   for (int i = 1; i <= m; ++i) { n     h[i] = p[i] = i; n     l[i] = i - 1, r[i] = i + 1; n   } n   t = m; n   l[1] = m; n   r[m] = 1; n   for (int i = 1; i <= m; ++i) calc(i); n   while (c[h[1]] > R) { n     int i = h[1]; n     l[r[i]] = l[i]; n     r[l[i]] = r[i]; n     change(1, t--); n     down(1); n     calc(l[i]); n     calc(r[i]); n   } n   int j = h[1]; n   double s = count(j, l[j]) + count(j, r[j]); n   for (int i = r[j]; r[i] != j; i = r[i]) n     s += abs(cp(b[i], b[r[i]], b[j])) / 2 + count(i, r[i]); n   printf( %.10lf n , s); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int k; n     cin >> k; n     int days[14]; n     int ones = 0; n     for (int i = 0; i < 7; i++) { n       cin >> days[i]; n       if (days[i]) ones++; n       days[i + 7] = days[i]; n     } n     int ans = 0; n     int left = 0; n     if (k % ones == 0) { n       ans = 7 * (k / ones - 1); n       left = ones; n     } else { n       ans = 7 * (k / ones); n       left = k % ones; n     } n     int c = 0, mini = INT_MAX; n     for (int i = 0; i < 14; i++) { n       if (!days[i]) continue; n       int c = 0, c2 = 0; n       for (int j = i; j < 14; j++) { n         if (days[j] == 1) c++; n         c2++; n         if (c == left) { n           mini = min(mini, c2); n           break; n         } n       } n     } n     cout << mini + ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int c[10000007]; n vector<pair<int, int> > x; n int temp; n vector<int> vt; n int main() { n   scanf( %d %d %d , &m, &k, &n); n   for (int i = 0; i < m; i++) { n     scanf( %d , &temp); n     c[temp]++; n     if (c[temp] > n) { n       printf( -1 n ); n       return 0; n     } n   } n   for (int i = 0; i < k; i++) { n     scanf( %d , &temp); n     x.push_back(make_pair(temp, i)); n   } n   sort(x.begin(), x.end()); n   int cur = 0; n   int remain = 0; n   for (int i = 0; i < k; i++) { n     while (cur <= x[i].first) { n       remain += n - c[cur]; n       cur++; n     } n     if (remain) { n       remain--; n       vt.push_back(x[i].second); n     } n   } n   printf( %d n , vt.size()); n   for (int i = 0; i < vt.size(); i++) printf( %d  , vt[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[55][55]; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n int n, T, m; n inline int valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n int cyc[55][55][4][2] = {}; n int main(void) { n   scanf( %d%d , &n, &T); n   for (int i = 0; i < n; i++) scanf( %s , a[i]); n   m = strlen(a[0]); n   int x = 0, y = 0, DP = 0, CP = 0; n   int Cx = -dy[DP] * (CP ? -1 : 1), Cy = dx[DP] * (CP ? -1 : 1); n   int Dx = dx[DP], Dy = dy[DP]; n   for (int step = 1; step <= T; step++) { n     while (valid(x + Dx, y + Dy) && a[x + Dx][y + Dy] == a[x][y]) n       x += Dx, y += Dy; n     while (valid(x + Cx, y + Cy) && a[x + Cx][y + Cy] == a[x][y]) n       x += Cx, y += Cy; n     if (cyc[x][y][DP][CP]) { n       int ii = step - cyc[x][y][DP][CP]; n       while (step + ii <= T) step += ii; n     } else n       cyc[x][y][DP][CP] = step; n     if (!valid(x + Dx, y + Dy) || a[x + Dx][y + Dy] ==  0 ) { n       if (CP == 0) n         CP = 1; n       else if (CP == 1) { n         DP = (DP + 1) % 4; n         CP = 0; n       } n       Cx = -dy[DP] * (CP ? -1 : 1), Cy = dx[DP] * (CP ? -1 : 1); n       Dx = dx[DP], Dy = dy[DP]; n     } else n       x += Dx, y += Dy; n   } n   printf( %c n , a[x][y]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int f = 1, r = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f ^= ch ==  - , ch = getchar(); n   while (isdigit(ch)) r = (r << 1) + (r << 3) + (ch ^ 48), ch = getchar(); n   return f ? r : -r; n } n const int N = 3005, M = 305; n int n, m, cnt[N]; n double f[M][N], g[N], d[N], sv[N], siv[N], val[N]; n double ans, p[N][M]; n inline void upd(int x) { n   memcpy(g, f[x], sizeof(g)); n   f[x][0] = 0; n   for (int i = 1; i <= n; i++) n     f[x][i] = f[x][i - 1] * (1 - p[i][x]) + g[i - 1] * p[i][x]; n   cnt[x]++, sv[x] += f[x][n]; n   siv[x] += f[x][n] * cnt[x]; n   double E = siv[x] + cnt[x] * (1 - sv[x]); n   d[x] = E - val[x], val[x] = E; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) p[i][j] = read() / 1000.; n   for (int i = 1; i <= m; i++) { n     f[i][0] = 1; n     for (int j = 1; j <= n; j++) f[i][j] = f[i][j - 1] * (1 - p[j][i]); n     sv[i] = f[i][n], upd(i); n   } n   for (int i = 1; i <= n; i++) { n     int k = 0; n     for (int j = 1; j <= m; j++) n       if (d[k] < d[j]) k = j; n     if (!k) break; n     ans += d[k], upd(k); n   } n   return printf( %.12lf n , ans), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[101], b[101], z[101], x, s, k, i, j; n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= m; i++) { n     cin >> b[i]; n     s += b[i]; n   } n   for (i = 1; i <= n - s + 1; i++) { n     for (k = i; k <= i + s - 1; k++) z[a[k]]++; n     for (j = 1; j <= 100; j++) n       if (z[j] != b[j]) { n         x = 1; n         break; n       } n     if (x == 0) { n       cout <<  YES ; n       return 0; n     } n     x = 0; n     memset(z, 0, 101 * sizeof(int)); n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n long long cacu(long long l, long long r) { n   return (r + l) / 2 * ((r - l) / 2 + 1); n } n int main() { n   int q, l, r; n   scanf( %d , &q); n   while (q--) { n     long long ans; n     scanf( %d%d , &l, &r); n     ans = cacu(l + (l & 1), r - (r & 1)); n     ans -= cacu(l | 1, r - !(r & 1)); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n template <typename T> n void chmin(T &x, const T &y) { n   if (x > y) x = y; n } n template <typename T> n void chmax(T &x, const T &y) { n   if (x < y) x = y; n } n template <typename T> n T gcd(T x, T y) { n   return y ? gcd(y, x % y) : x; n } n const int mod = 998244353; n template <typename T> n void Mod(T &x) { n   while (x >= mod) x -= mod; n } n long long qk(long long x, long long y) { n   long long r = 1; n   for (; y; y >>= 1) { n     if (y & 1) r = (r * x) % mod; n     x = (x * x) % mod; n   } n   return r; n } n map<int, vector<int>> dps; n int cal(int a, int b, int c, int d) { n   int x = d + b - a; n   if (x < 0) return 0; n   int k = d + c; n   chmin(x, k); n   if (dps.count(k)) return dps[k][x]; n   auto &dp = dps[k]; n   dp.resize(k + 1); n   int t = 0, w = 1; n   for (int i = 0; i <= k; ++i) { n     Mod(t += w); n     dp[i] = t; n     w = (long long)w * (k - i) % mod; n     w = (long long)w * qk(i + 1, mod - 2) % mod; n   } n   return dp[x]; n } n void solve(int times) { n   string s; n   cin >> s; n   int n = s.length(); n   int a = 0, b = 0, c = 0, d = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ) ) n       ++b; n     else if (s[i] ==  ? ) n       ++d; n   } n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ( ) { n       ++a; n     } else if (s[i] ==  ? ) { n       ++c; n       --d; n     } else if (s[i] ==  ) ) { n       --b; n     } n     if (s[i] ==  ( ) { n       Mod(ans += cal(a, b, c, d)); n     } else if (s[i] ==  ? ) { n       ++a; n       --c; n       Mod(ans += cal(a, b, c, d)); n       ++c; n       --a; n     } n   } n   cout << ans << endl; n } n void Init() {} n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int times = 1; n   { n     solve(times++); n     Init(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n const double EPS = 1e-10; n const double PI = acos(-1.0); n const int INF = 1 << 29; n const int mod = 1000000007; n const int MAX_V = 1000 + 10; n const int MAX_M = 10000 + 10; n const int maxm = 1 + 5; n const int maxn = 50000 + 5; n int used[20]; n int n; n int main() { n   scanf( %d , &n); n   int i, j, sum = 0, a; n   memset(used, 0, sizeof(used)); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     sum += a; n     used[a]++; n   } n   if (used[0] == 0) n     cout << -1 << endl; n   else { n     int flag = 0; n     if (sum % 3 == 1) { n       if (used[1] + used[4] + used[7]) { n         for (int i = 1; i <= 7; i += 3) { n           if (used[i]) { n             used[i]--, sum -= i; n             break; n           } n         } n       } else if (used[2] + used[5] + used[8] >= 2) { n         for (j = 1, i = 2; i <= 8; i += 3) { n           while (used[i] && j <= 2) used[i]--, sum -= i, j++; n         } n       } n     } n     if (sum % 3 == 2) { n       if (used[2] + used[5] + used[8]) { n         for (i = 2; i <= 8; i += 3) n           if (used[i]) { n             used[i]--, sum -= i; n             break; n           } n       } else if (used[1] + used[4] + used[7] >= 2) { n         for (j = 1, i = 1; i <= 7; i += 3) n           while (used[i] && j <= 2) used[i]--, sum -= i, j++; n       } else n         flag = 1; n     } n     if (!flag) { n       if (used[1] + used[2] + used[3] + used[4] + used[5] + used[6] + used[7] + n           used[8] + used[9]) { n         for (i = 9; i >= 0; i--) n           while (used[i]--) printf( %d , i); n         printf(  n ); n       } else n         printf( 0 n ); n     } else n       printf( 0 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n list<int> adj[1000005]; n int T[1000005]; n bool visited[1000005]; n bool uniq[1000005]; n int rev[10000005]; n void dfs(int v) { n   visited[v] = true; n   for (list<int>::iterator it = adj[v].begin(); it != adj[v].end(); it++) { n     if (!visited[*it]) dfs(*it); n   } n } 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() { n   scanf( %d , &N); n   for (int i = 0; i < (N); i++) scanf( %d , &T[i]); n   sort(T, T + N); n   T[N] = -1; n   for (int i = 0; i < (10000001); i++) rev[i] = N; n   for (int i = 0; i < (N); i++) rev[T[i]] = i; n   int temp[3]; n   int v[3]; n   for (int i = 1; i <= 5000; i += 2) n     for (int j = 2; i * i + j * j <= T[N - 1]; j += 2) { n       if (gcd(i, j) > 1) continue; n       temp[0] = abs(i * i - j * j); n       temp[1] = i * i + j * j; n       temp[2] = 2 * i * j; n       for (int t = 0; t < (3); t++) v[t] = rev[temp[t]]; n       for (int u = 0; u < (3); u++) n         for (int s = 0; s < (u); s++) { n           if ((T[v[u]] == temp[u]) && (T[v[s]] == temp[s])) { n             adj[v[u]].push_back(v[s]); n             adj[v[s]].push_back(v[u]); n           } n         } n     } n   for (int i = 1; i <= T[N - 1]; i += 2) n     for (int j = 2; i * j <= T[N - 1]; j += 2) { n       if (gcd(i, j) > 1) continue; n       long long int L1 = abs(i * (long long int)i - j * (long long int)j); n       long long int L2 = 2 * i * j; n       if (L1 > 10000000 || L2 > 10000000) continue; n       int v1 = rev[L1]; n       int v2 = rev[L2]; n       if ((T[v1] == L1) && (T[v2] == L2)) { n         adj[v1].push_back(v2); n         adj[v2].push_back(v1); n       } n     } n   for (int i = 0; i < (N); i++) visited[i] = false; n   int result = 0; n   for (int i = 0; i < (N); i++) n     if (!visited[i]) { n       result++; n       dfs(i); n     } n   printf( %d n , result); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void solve() { n   if (n <= 3) n     printf( NO n ); n   else if (n == 4) { n     printf( YES n ); n     printf( 2 * 3 = 6 n ); n     printf( 6 * 4 = 24 n ); n     printf( 24 * 1 = 24 n ); n   } else if (n == 5) { n     printf( YES n ); n     printf( 1 + 5 = 6 n ); n     printf( 3 - 2 = 1 n ); n     printf( 6 * 1 = 6 n ); n     printf( 6 * 4 = 24 n ); n   } else if (n >= 6) { n     printf( YES n ); n     printf( 3 - 2 = 1 n ); n     printf( 1 - 1 = 0 n ); n     printf( 4 * 6 = 24 n ); n     printf( 5 * 0 = 0 n ); n     int i = 7; n     while (i <= n) { n       printf( %d * 0 = 0 n , i); n       i++; n     } n     printf( 24 + 0 = 24 n ); n   } else n     printf( NO n ); n } n int main() { n   while (scanf( %d , &n) != EOF) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = LONG_LONG_MAX; n const long long NINF = LONG_LONG_MIN; n const long long mmod = 1000000007; n int32_t main() { n   long long k; n   cin >> k; n   while (k-- > 0) { n     long long n; n     cin >> n; n     string s, t; n     cin >> s >> t; n     vector<long long> cnt(26, 0); n     for (long long i = 0; i < n; i++) { n       cnt[s[i] -  a ]++; n       cnt[t[i] -  a ]++; n     } n     bool found = true; n     for (long long i = 0; i < 26; i++) n       if (cnt[i] & 1) found = false; n     if (found) { n       vector<pair<long long, long long>> ans; n       for (long long i = 0; i < n - 1; i++) { n         if (s[i] != t[i]) { n           bool f = false; n           for (long long j = i + 1; j < n; j++) { n             if (t[j] == t[i]) { n               swap(s[i], t[j]); n               f = true; n               ans.emplace_back(i + 1, j + 1); n               break; n             } n           } n           if (!f) { n             for (long long j = i + 1; j < n; j++) { n               if (s[j] == t[i]) { n                 swap(s[j], t[j]); n                 swap(s[i], t[j]); n                 ans.emplace_back(j + 1, j + 1); n                 ans.emplace_back(i + 1, j + 1); n                 break; n               } n             } n           } n         } n       } n       cout <<  YES  << endl; n       cout << (long long)ans.size() << endl; n       for (pair<long long, long long> x : ans) n         cout << x.first <<     << x.second << endl; n     } else { n       cout <<  No  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long p, k; n vector<int> v; n int main() { n   cin >> p >> k; n   v.push_back(p % k); n   long long a0 = -p / k; n   while (1) { n     long long a1 = -a0 / k; n     while (a1 * k + a0 < 0) a1++; n     while (a1 * k + a0 >= k) a1--; n     if (a1 < k && a1 >= 0) { n       v.push_back(a0 + k * a1); n       v.push_back(a1); n       break; n     } n     v.push_back(a0 + k * a1); n     a0 = a1; n   } n   while (v.back() == 0) v.pop_back(); n   printf( %d n , v.size()); n   for (int i = 0; i < v.size(); ++i) printf( %d  , v[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200010]; n long long vis[200010]; n long long cnt[200010]; n long long n; n long long max_n = 0; n void solve(long long pos) { n   vis[pos]++; n   long long temp1 = pos; n   long long temp2 = pos; n   long long step1 = 0; n   long long step2 = 0; n   while (temp1 <= max_n) { n     temp1 <<= 1; n     step1++; n     vis[temp1]++; n     cnt[temp1] += step1; n   } n   while (temp2) { n     if ((temp2 & 1) && (temp2 != 1)) { n       temp2 >>= 1; n       step2++; n       vis[temp2]++; n       cnt[temp2] += step2; n       long long temp3 = temp2; n       long long step3 = step2; n       while (temp3 <= max_n) { n         temp3 <<= 1; n         step3++; n         vis[temp3]++; n         cnt[temp3] += step3; n       } n     } else { n       temp2 >>= 1; n       step2++; n       vis[temp2]++; n       cnt[temp2] += step2; n     } n   } n } n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     max_n = max(a[i], max_n); n   } n   memset(vis, 0, sizeof(vis)); n   memset(cnt, 0, sizeof(cnt)); n   for (long long i = 1; i <= n; i++) solve(a[i]); n   long long ans = 1 << 29; n   for (long long i = 1; i <= 2 * max_n - 1; i++) { n     if (vis[i] == n) ans = min(ans, cnt[i]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int read() { n   int x = 0, y = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) y = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return x * y; n } n void print(int x) { n   if (x < 0) { n     putchar( 0 ); n     return; n   } n   if (x < 10) { n     putchar(x +  0 ); n     return; n   } n   print(x / 10); n   putchar(x % 10 +  0 ); n } n int n, x, maxx; n int main() { n   n = read(); n   for (int _ = 0; _ < n; _++) x = read(), maxx = maxx > x ? maxx : x; n   print(maxx - 25); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int x, y, tx, ty, vis[N]; n int chk(int x, int y) { n   for (int i = 0; i < 7; i++) vis[i] = 0; n   for (int i = 1; i <= tx; i++) ++vis[x % 7], x /= 7; n   for (int i = 1; i <= ty; i++) ++vis[y % 7], y /= 7; n   for (int i = 0; i < 7; i++) n     if (vis[i] > 1) return 0; n   return 1; n } n int main() { n   scanf( %d%d , &x, &y), --x, --y; n   if ((long long)x * y > 6543210) return puts( 0 ), 0; n   for (int i = x; i; i /= 7) ++tx; n   for (int i = y; i; i /= 7) ++ty; n   tx = max(tx, 1), ty = max(ty, 1); n   int ans = 0; n   for (int i = 0; i <= x; i++) n     for (int j = 0; j <= y; j++) ans += chk(i, j); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 207; n const int M = 1507; n const int inf = INT_MAX; n int nx[M], to[M], head[N], cap[M], fl[M], who[N][N], vis[N], es = 2; n int a[N], b[N]; n int n, m, s, t, turn = 1; n int dfs(int u, int mini) { n   vis[u] = turn; n   if (u == t) return mini; n   for (int e = head[u]; e; e = nx[e]) { n     int v = to[e]; n     if (vis[v] != turn && cap[e] - fl[e]) { n       int k = dfs(v, min(mini, cap[e] - fl[e])); n       if (k != inf) { n         fl[e] += k; n         fl[e ^ 1] -= k; n         return k; n       } n     } n   } n   return inf; n } n int maxflow() { n   int mf = 0, k; n   while ((k = dfs(s, inf)) != inf) { n     turn++; n     mf += k; n   } n   return mf; n } n int addedge(int u, int v, int _cap) { n   who[u][v] = es; n   to[es] = v; n   nx[es] = head[u]; n   head[u] = es; n   cap[es] = _cap; n   fl[es++] = 0; n   to[es] = u; n   nx[es] = head[v]; n   head[v] = es; n   cap[es] = 0; n   fl[es++] = 0; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   for (int i = 0; i < n; i++) scanf( %d , b + i); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--; n     v--; n     addedge(u, v + n, inf); n     addedge(v, u + n, inf); n   } n   s = 2 * n; n   t = 2 * n + 1; n   int sa = 0, sb = 0; n   for (int i = 0; i < n; i++) { n     sa += a[i]; n     sb += b[i]; n     addedge(i, i + n, inf); n     addedge(s, i, a[i]); n     addedge(i + n, t, b[i]); n   } n   int res = maxflow(); n   if (res == sa && res == sb) { n     printf( YES n ); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         printf( %d%c , fl[who[i][j + n]],    n [j == n - 1]); n   } else n     puts( NO ); n } n


#include <bits/stdc++.h> n int main() { n   int n, t, pos = 0; n   std::cin >> n >> t; n   std::vector<std::pair<int, int> > vec; n   for (int i = 0; i < n; i++) { n     int x, y; n     std::cin >> x >> y; n     vec.push_back(std::make_pair(x, y)); n   } n   std::sort(vec.begin(), vec.end()); n   for (int i = 0; i < n - 1; i++) { n     int temp = 2 * vec[i + 1].first - 2 * vec[i].first - vec[i + 1].second - n                vec[i].second; n     if (temp >= 2 * t) { n       pos++; n     } n     if (temp > 2 * t) { n       pos++; n     } n   } n   std::cout << pos + 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long MOD = 1e8 + 7; n const unsigned long long base = 71; n const unsigned long long max_n = 2e5 + 400; n unsigned long long n, m, k, q; n string s; n unsigned long long pw[2 * max_n + 200]; n unsigned long long save[20][2 * max_n + 200]; n unsigned long long seg[10 * max_n], lazy[10 * max_n]; n void init(unsigned long long v, unsigned long long b, unsigned long long e) { n   lazy[v] = -1; n   if (b == e) { n     seg[v] = s[b]; n     return; n   } n   unsigned long long mid = (b + e) / 2; n   init(v << 1, b, mid); n   init(v << 1 ^ 1, mid + 1, e); n   seg[v] = (seg[v << 1] * pw[e - mid] + seg[v << 1 ^ 1]) % MOD; n } n void shift(unsigned long long v, unsigned long long b, unsigned long long e, n            unsigned long long mid) { n   if (lazy[v] == -1) return; n   lazy[v << 1] = lazy[v << 1 ^ 1] = lazy[v]; n   seg[v << 1] = save[lazy[v]][mid - b]; n   seg[v << 1 ^ 1] = save[lazy[v]][e - mid - 1]; n   lazy[v] = -1; n } n void update(unsigned long long v, unsigned long long b, unsigned long long e, n             unsigned long long l, unsigned long long r, n             unsigned long long val) { n   if (b > r || l > e) return; n   if (l <= b && e <= r) { n     seg[v] = save[val][e - b]; n     lazy[v] = val; n     return; n   } n   unsigned long long mid = (b + e) / 2; n   shift(v, b, e, mid); n   update(v << 1, b, mid, l, r, val); n   update(v << 1 ^ 1, mid + 1, e, l, r, val); n   seg[v] = (seg[v << 1] * pw[e - mid] + seg[v << 1 ^ 1]) % MOD; n } n unsigned long long get(unsigned long long v, unsigned long long b, n                        unsigned long long e, unsigned long long l, n                        unsigned long long r) { n   if (b > r || l > e) return 0; n   if (l <= b && e <= r) return seg[v]; n   unsigned long long mid = (b + e) / 2; n   shift(v, b, e, mid); n   unsigned long long first = get(v << 1, b, mid, l, r); n   unsigned long long second = get(v << 1 ^ 1, mid + 1, e, l, r); n   first = (first * pw[max(0, int(min(r, e) - mid))] + second) % MOD; n   return first; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   cin >> n >> m >> k; n   q = m + k; n   cin >> s; n   pw[0] = 1; n   for (unsigned long long i = 1; i < max_n; i++) n     pw[i] = (pw[i - 1] * base) % MOD; n   for (unsigned long long i = 0; i < 10; i++) { n     save[i][0] = i + 48; n     for (unsigned long long j = 1; j < max_n; j++) n       save[i][j] = (save[i][j - 1] * base + (i + 48)) % MOD; n   } n   init(1, 0, n - 1); n   while (q--) { n     unsigned long long type, l, r; n     cin >> type >> l >> r; n     l--, r--; n     if (type == 1) { n       unsigned long long c; n       cin >> c; n       update(1, 0, n - 1, l, r, c); n       continue; n     } n     bool fl = 0; n     unsigned long long d; n     cin >> d; n     if (d == r - l + 1) fl = 1; n     if (!fl) fl = get(1, 0, n - 1, l, r - d) == get(1, 0, n - 1, l + d, r); n     if (fl) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2E5 + 77; n void putint(int x) { n   static bool first = true; n   if (first) { n     printf( %d , x); n     first = false; n   } else n     printf(  %d , x); n } n struct QUERY { n   int t, r; n } Q[maxn]; n int cnt = 0; n int N, M, A[maxn], B[maxn], pb = 0; n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; ++i) scanf( %d , A + i); n   for (int i = 0; i < M; ++i) { n     int t, r; n     scanf( %d%d , &t, &r); n     while (cnt && r >= Q[cnt - 1].r) --cnt; n     if (!cnt || t != Q[cnt - 1].t) Q[cnt++] = (QUERY){t, r}; n   } n   Q[cnt] = (QUERY){0, 0}; n   sort(A, A + Q[0].r); n   int ptr[2] = {0, Q[0].r - 1}, d = Q[0].t == 1; n   for (int i = 0; i < cnt; ++i) { n     for (int j = Q[i].r; j > Q[i + 1].r; --j) { n       B[pb++] = A[ptr[d]]; n       ptr[d] += d ? -1 : 1; n     } n     d ^= 1; n   } n   for (int i = Q[0].r - 1; i >= 0; --i) putint(B[i]); n   for (int i = Q[0].r; i < N; ++i) putint(A[i]); n   printf(  n ); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:136777216 ) n using namespace std; n int bits(int x) { return x == 0 ? 0 : 1 + bits(x & (x - 1)); } n const double PI = acos(-1.0); n const double eps = 1e-9; n const int INF = 1000000000; n string nextString() { n   char buf[1000000]; n   scanf( %s , buf); n   return buf; n } n int dx[] = {0, 1, 0, -1}; n int dy[] = {1, 0, -1, 0}; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n string month[] = { January ,    February ,  March ,     April , n                    May ,        June ,      July ,      August , n                    September ,  October ,   November ,  December }; n int main() { n   string s = nextString(); n   int start; n   for (int(i) = 0; (i) < (12); (i)++) n     if (s == month[i]) start = i; n   int n; n   scanf( %d , &n); n   int end = (n + start) % 12; n   printf( %s n , month[end].c_str()); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int mod = (int)1e9 + 7; n const long long INF = 1000000000000000000LL; n const int WASTE = (int)2e5; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int x, y, p, q; n     cin >> x >> y >> p >> q; n     if (!p) { n       cout << (x == 0 ? 0 : -1) <<   n ; n       continue; n     } n     if (p == q) { n       if (x == y) n         cout << 0 <<   n ; n       else n         cout << -1 <<   n ; n       continue; n     } n     int i = (x + p - 1) / p; n     int i1 = (y - x - p + q - 1) / (q - p); n     cout << (q * 1LL * max(i, i1) - y) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   for (int i = 0; i < T; i++) { n     long long int n, x; n     cin >> n >> x; n     cout << 2 * x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3; n int n, x[N], d[N], i, j; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) cin >> x[i] >> d[i]; n   for (i = 0; i < n; i++) n     for (j = i + 1; j < n; j++) n       if (x[i] + d[i] == x[j] && x[j] + d[j] == x[i]) return cout <<  YES , 0; n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n int n; n char s[N]; n bool ck(int l, int r) { n   int a = 0, b = 0, c = 0, d = 0; n   for (int i = l; i <= r; i++) { n     if (s[i] ==  U ) n       a++; n     else if (s[i] ==  D ) n       b++; n     else if (s[i] ==  L ) n       c++; n     else n       d++; n   } n   if (a == b && c == d) n     return 1; n   else n     return 0; n } n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) ans += ck(i, j); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i; n   scanf( %d , &n); n   char s[100]; n   int a[n], b[n], c[n]; n   scanf( %s , s); n   for (i = 0; i < n; i++) { n     int h = (int)s[i]; n     a[i] = h - 48; n   } n   for (i = 0; i < n; i++) { n     b[n - i - 1] = a[i]; n     c[n - i - 1] = a[i]; n   } n   i = n - 1; n   int c1 = 1; n   while (c1 == 1) { n     if (b[i] == 1) { n       c[i] = 0; n       c1 = 1; n       i--; n     } else if (b[i] == 0) { n       c[i] = 1; n       c1 = 0; n       i--; n     } n     if (i < 0) break; n   } n   int count = 0; n   for (i = 0; i < n; i++) { n     if (b[i] != c[i]) count++; n   } n   printf( %d n , count); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   cin >> x; n   string ans, in, dec, exp; n   int flag = 0; n   for (int i = 0; i < x.length(); i++) { n     if (x[i] ==  . ) n       flag = 1; n     else if (x[i] ==  e ) n       flag = 2; n     else if (flag == 0) n       in += x[i]; n     else if (flag == 1) n       dec += x[i]; n     else if (flag == 2) n       exp += x[i]; n   } n   int e = stoi(exp); n   ans += in; n   if (e <= dec.length()) { n     ans += dec.substr(0, e); n     if (e < dec.length()) { n       ans +=  . ; n       ans += dec.substr(e, dec.length()); n     } n   } else { n     ans += dec; n     ans += string(e - dec.length(),  0 ); n   } n   flag = 0; n   if (dec.length() < 2 && stoi(dec) == 0 && e == 0) ans = in; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int limN = 2e5 + 5; n char s[limN]; n char arr[2000]; n int bu[400]; n int main() { n   int t; n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     scanf( %s , s); n     for (int j = 0; j < 2000; j++) arr[j] =  . ; n     for (int j = 0; j < 400; j++) bu[j] = 0; n     int n = strlen(s); n     int ini = 500; n     int fin = 500; n     bool f = true; n     arr[ini] = s[0]; n     bu[s[0]]++; n     int pos = ini; n     for (int j = 1; j < n; j++) { n       if (j == 1) { n         fin++; n         arr[fin] = s[j]; n         bu[s[j]]++; n         pos = fin; n         continue; n       } n       if (arr[pos - 1] == s[j]) { n         pos--; n         continue; n       } n       if (arr[pos + 1] == s[j]) { n         pos++; n         continue; n       } n       if (bu[s[j]] > 0) { n         f = false; n         break; n       } else { n         if (pos == ini) { n           ini--; n           arr[ini] = s[j]; n           bu[s[j]]++; n           pos--; n         } else if (pos == fin) { n           fin++; n           arr[fin] = s[j]; n           bu[s[j]]++; n           pos++; n         } else { n           f = false; n           break; n         } n       } n     } n     if (!f) { n       printf( NO n ); n     } else { n       printf( YES n ); n       for (int j = ini; j <= fin; j++) { n         printf( %c , arr[j]); n       } n       for (int j =  a ; j <=  z ; j++) { n         if (bu[j] == 0) { n           printf( %c , j); n         } n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxv = 110; n int n, m; n int val[Maxv][Maxv * Maxv]; n int org[Maxv][Maxv]; n int dp[Maxv][Maxv * Maxv]; n int Len[Maxv]; n int main() { n   int i, j, k; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     scanf( %d , &Len[i]); n     for (j = 1; j <= Len[i]; j++) scanf( %d , &org[i][j]); n     int sum1 = 0; n     for (j = 0; j <= Len[i]; j++) { n       int sum2 = 0; n       sum1 += org[i][j]; n       for (k = Len[i] + 1; k > j; k--) { n         sum2 += org[i][k]; n         int tcost = j + Len[i] + 1 - k; n         val[i][tcost] = max(sum1 + sum2, val[i][tcost]); n       } n     } n     for (k = m; k >= 1; k--) { n       for (j = 0; j <= Len[i] && j <= k; j++) { n         dp[i][k] = max(dp[i][k], dp[i - 1][k - j] + val[i][j]); n       } n     } n   } n   printf( %d n , dp[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, x, a[100005], ct[100005]; n int main() { n   cin >> n >> x; n   for (long long int i = 1; i <= n; ++i) cin >> a[i], ct[a[i]]++; n   sort(a + 1, a + n + 1); n   long long int ans = 0; n   for (long long int i = 1; i <= n; ++i) { n     long long int ch = x ^ a[i]; n     if (ch == a[i]) { n       ans += (ct[ch] - 1); n     } else if (ch <= 100000) n       ans += ct[ch]; n   } n   cout << ans / 2; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   if (a < b) n     puts( -1 ); n   else n     printf( %.12f n , (a + b) / (2. * ((a + b) / (2 * b)))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = (int)1e9 + 7; n const int MX = 1073741822; n const long long MXLL = 4e18; n const int Sz = 1110111; n inline void Read_rap() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n inline void randomizer3000() { n   unsigned int seed; n   asm( rdtsc  :  =A (seed)); n   srand(seed); n } n void files(string problem) { n   if (fopen((problem +  .in ).c_str(),  r )) { n     freopen((problem +  .in ).c_str(),  r , stdin); n     freopen((problem +  .out ).c_str(),  w , stdout); n   } n } n void localInput(const char in[] =  s ) { n   if (fopen(in,  r )) { n     freopen(in,  r , stdin); n   } else n     cerr <<  Warning: Input file not found  << endl; n } n int n; n const int N = 302; n int pref[N][N]; n int dp[N][N]; n int a[N]; n int plen[N], len[N]; n int main() { n   Read_rap(); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     static string s; n     cin >> s; n     static map<string, int> id; n     if (!id.count(s)) id[s] = (int)(id.size()); n     a[i] = id[s]; n     len[i] = (int)(s.size()); n   } n   for (int i = n; i >= 1; i--) n     for (int j = n; j >= 1; j--) n       if (a[i] == a[j]) pref[i][j] = pref[i + 1][j + 1] + 1; n   for (int i = 1; i <= n; i++) plen[i] = plen[i - 1] + len[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i; j++) { n       int len = i - j + 1; n       for (int p = len; p < j; p++) { n         int q = p - len + 1; n         if (pref[j][q] >= len) dp[i][j] = max(dp[i][j], dp[p][q] + 1); n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i; j++) { n       int len = i - j + 1; n       if (dp[i][j]) { n         int cost = len - 1 + plen[i] - plen[j - 1] - len; n         ans = max(ans, cost * (dp[i][j] + 1)); n       } n     } n   } n   int sum = accumulate(len + 1, len + n + 1, 0) + n - 1; n   cout << sum - ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long ask(long long a, long long b, long long c, long long d) { n   long long res; n   cout <<  ?   << a <<     << b <<     << c <<     << d << endl; n   cout.flush(); n   cin >> res; n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   long long x1 = -1; n   long long x2 = -1; n   long long y1 = -1; n   long long y2 = -1; n   for (int i = 1; i < n; i++) { n     if (ask(i, 1, i, n) % 2 == 1) { n       if (x1 == -1) n         x1 = i; n       else { n         x2 = i; n         break; n       } n     } n   } n   if (x1 == -1) { n     for (int i = 1; i < n; i++) { n       if (ask(1, i, n, i) % 2 == 1) { n         if (y1 == -1) n           y1 = i; n         else { n           y2 = i; n           break; n         } n       } n     } n     if (y2 == -1) y2 = n; n     long long low = 1; n     long long high = n; n     x1 = n; n     while (low <= high) { n       long long mid = (low + high) / 2; n       if (ask(low, y1, mid, y1) % 2 == 1) { n         high = mid - 1; n         x1 = min(x1, mid); n       } else { n         low = mid + 1; n       } n     } n     x2 = x1; n   } else { n     if (x2 == -1) x2 = n; n     long long low = 1; n     long long high = n; n     y1 = n; n     while (low <= high) { n       long long mid = (low + high) / 2; n       if (ask(x1, low, x1, mid) % 2 == 1) { n         y1 = min(y1, mid); n         high = mid - 1; n       } else { n         low = mid + 1; n       } n     } n     low = 1; n     high = n; n     y2 = n; n     while (low <= high) { n       long long mid = (low + high) / 2; n       if (ask(x2, low, x2, mid) % 2 == 1) { n         y2 = min(y2, mid); n         high = mid - 1; n       } else { n         low = mid + 1; n       } n     } n   } n   cout <<  !   << x1 <<     << y1 <<     << x2 <<     << y2 << endl; n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[111111]; n int y[111111]; n int main() { n   int i, n, m, t; n   long long xx = 0, yy = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &t); n     x[t] = i; n     y[t] = n - i + 1; n   } n   scanf( %d , &m); n   for (i = 1; i <= m; i++) { n     scanf( %d , &t); n     xx += x[t]; n     yy += y[t]; n   } n   cout << xx <<     << yy << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T abs(T t) { n   return t < 0 ? -t : t; n } n const unsigned long long modn = 1000000007; n inline unsigned long long mod(unsigned long long x) { return x % modn; } n const int MAX = 100009; n int deg[MAX], ok[MAX]; n vector<int> adj[MAX]; n void dfs(int u, int p) { n   if (deg[u] > 3) return; n   ok[u]++; n   if (deg[u] == 3) return; n   for (int v : adj[u]) n     if (v != p) dfs(v, u); n } n int main() { n   int i, n, a, b; n   scanf( %d , &n); n   for (i = 0; i < n - 1; i++) { n     scanf( %d %d , &a, &b); n     adj[--a].push_back(--b); n     adj[b].push_back(a); n     deg[a]++; n     deg[b]++; n   } n   for (i = 0; i < n; i++) n     if (deg[i] == 1) dfs(i, -1); n   for (i = 0; i < n; i++) n     if (deg[i] == 3 && ok[i] <= 1) ok[i] = 0; n   for (i = 0; i < n; i++) { n     if (ok[i]) continue; n     int ct = 0; n     for (int v : adj[i]) n       if (!ok[v]) ct++; n     if (ct > 2) break; n   } n   if (i < n) n     puts( No ); n   else n     puts( Yes ); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n int n; n pair<long long, long long> a[maxn + 1]; n pair<long long, long long> b; n priority_queue<long long, vector<long long>, greater<long long> > q; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return a > b; n } n int main() { n   int ans; n   scanf( %d n , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &a[i - 1].first, &a[i - 1].second); n   } n   b = a[0]; n   sort(a + 1, a + n, cmp); n   int i = 1; n   for (; i <= n && a[i].first > b.first; i++) n     q.push(a[i].second - a[i].first + 1); n   ans = i; n   int lans = 0; n   while (!q.empty()) { n     long long k = q.top(); n     q.pop(); n     if (k > b.first) break; n     b.first -= k; n     lans++; n     for (; i <= n && a[i].first > b.first; i++) n       q.push(a[i].second - a[i].first + 1); n     ans = min(ans, i - lans); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int acc[1002], s[1002]; n int main() { n   int n, t; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> t; n     s[t]++; n   } n   for (int i = 1; i < 1002; i++) { n     acc[i] = acc[i - 1] + s[i]; n   } n   int d; n   cin >> d; n   int total = 0; n   for (int i = d + 1; i < 1002; i++) { n     if (acc[i] - acc[i - d - 1] > total) total = acc[i] - acc[i - d - 1]; n   } n   cout << total << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::map<long, long> map; n   long q, id, leftMAX = 0, rightMAX = 0, firstTime = 0; n   char c; n   std::cin >> q; n   while (q--) { n     std::cin >> c >> id; n     if (!firstTime--) { n       map.insert(std::pair<long, long>(id, 0)); n     } else { n       if (c ==  ? ) n         std::cout << std::min(map[id] - leftMAX, rightMAX - map[id]) n                   << std::endl; n       else if (c ==  R ) n         map.insert(std::pair<long, long>(id, ++rightMAX)); n       else n         map.insert(std::pair<long, long>(id, --leftMAX)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n vector<int> up, down; n int main() { n   int T, n; n   char ch; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     up.clear(); n     down.clear(); n     for (int i = 1; i < n; i += 2) { n       printf( ? %d %d n , i, i + 1); n       fflush(stdout); n       cin >> ch; n       if (ch ==  < ) n         down.push_back(i), up.push_back(i + 1); n       else n         down.push_back(i + 1), up.push_back(i); n     } n     if (n & 1) down.push_back(n), up.push_back(n); n     int Min = down[0], Max = up[0]; n     for (int i = 1; i < down.size(); i++) { n       printf( ? %d %d n , Min, down[i]); n       fflush(stdout); n       cin >> ch; n       if (ch ==  > ) Min = down[i]; n     } n     for (int i = 1; i < up.size(); i++) { n       printf( ? %d %d n , Max, up[i]); n       fflush(stdout); n       cin >> ch; n       if (ch ==  < ) Max = up[i]; n     } n     printf( ! %d %d n , Min, Max); n     fflush(stdout); 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 lcm(long a, long b) { return ((a * b) / gcd(a, b)); } n long long a[100000]; n int main() { n   long long i, j, k, l, m, n, maxx = 0, g = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     while (a[i] % 2 == 0) { n       a[i] /= 2; n     } n     while (a[i] % 3 == 0) { n       a[i] /= 3; n     } n   } n   sort(a, a + n); n   if (a[0] == a[n - 1]) n     cout <<  Yes  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   long long a, b, c; n   cin >> a >> b >> c; n   if (a > b) swap(a, b); n   cout << a + min(c, a + b) + min(b, a + c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(long long &x) { scanf( %lld , &x); } n void read(long long &x, long long &y) { scanf( %lld%lld , &x, &y); } n void read(long long &x, long long &y, long long &z) { n   scanf( %lld%lld%lld , &x, &y, &z); n } n void print(long long x) { printf( %lld  , x); } n void println(long long x) { printf( %lld n , x); } n const long long N = 300100; n long long q, n, a[N], ans, b[N], sz; n int main() { n   cin >> q; n   for (; q--;) { n     read(n); n     for (long long i = 1; i <= n; i++) read(a[i]); n     sort(a + 1, a + n + 1); n     sz = 0; n     for (long long i = 1; i <= n; i++) n       if (i == 1 || a[i] != a[i - 1]) b[++sz] = a[i]; n     reverse(b + 1, b + sz + 1); n     for (long long i = 1; i <= sz; i++) a[i] = b[i]; n     n = sz; n     ans = a[1]; n     for (long long i = 1; i <= min(59LL, n); i++) n       for (long long j = i + 1; j <= (i + 59LL, n); j++) n         if (a[i] % a[j] != 0) ans = max(ans, a[i] + a[j]), j = n; n     for (long long i = 1; i <= min(59LL, n); i++) { n       if (i + 2 <= n && a[i] + a[i + 1] + a[i + 2] <= ans) break; n       for (long long j = i + 1; j <= (i + 59LL, n); j++) n         if (a[i] % a[j] != 0) { n           if (j < n && a[i] + a[j] + a[j + 1] <= ans) break; n           for (long long k = j + 1; k <= min(j + 59LL, n); k++) n             if (a[i] % a[k] != 0 && a[j] % a[k] != 0) n               ans = max(ans, a[i] + a[j] + a[k]), k = n; n         } n     } n     println(ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v); n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { n   return cout <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) { n   cout <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<  ,  ; n     cout << v[i]; n   } n   return cout <<  ] ; n } n template <typename A, typename B> n istream &operator>>(istream &cin, pair<A, B> &p) { n   cin >> p.first; n   return cin >> p.second; n } n mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); n void usaco(string filename) { n   freopen((filename +  .in ).c_str(),  r , stdin); n   freopen((filename +  .out ).c_str(),  w , stdout); n } n const long double pi = 3.14159265358979323846; n long long n, m, q, k, l, r, x, y, z; n const long long template_array_size = 1e6 + 16318; n long long a[template_array_size]; n long long b[template_array_size]; n long long c[template_array_size]; n string second, t; n long long ans = 0; n string op[5005]; n string val[5005]; n pair<long long, long long> dep[5005]; n map<string, long long> rev; n long long dp[5005]; n long long check(long long x, long long p, long long t) { n   if (x == -1) return t; n   if (dp[x] != -1) return dp[x]; n   if (op[x] ==  = ) n     return dp[x] = val[x][p] -  0 ; n   else if (op[x] ==  OR ) n     return dp[x] = check(dep[x].first, p, t) | check(dep[x].second, p, t); n   else if (op[x] ==  AND ) n     return dp[x] = check(dep[x].first, p, t) & check(dep[x].second, p, t); n   else n     return dp[x] = check(dep[x].first, p, t) ^ check(dep[x].second, p, t); n } n void solve(int tc = 0) { n   cin >> n >> m; n   string v; n   getline(cin, v); n   rev[ ? ] = -1; n   for (long long i = 0; i < n; i++) { n     vector<string> x; n     getline(cin, v); n     istringstream is(v); n     while (is >> v) { n       x.push_back(v); n     } n     rev[x[0]] = i; n     if (x.size() == 3) { n       op[i] =  = ; n       val[i] = x[2]; n     } else { n       op[i] = x[3]; n       dep[i] = {rev[x[2]], rev[x[4]]}; n     } n   } n   string rmin =   , rmax =   ; n   for (long long i = 0; i < m; i++) { n     long long c0 = 0, c1 = 0; n     memset(dp, -1, sizeof(dp)); n     for (long long j = 0; j < n; j++) { n       c0 += check(j, i, 0); n     } n     memset(dp, -1, sizeof(dp)); n     for (long long j = 0; j < n; j++) { n       c1 += check(j, i, 1); n     } n     if (c0 == c1) { n       rmin +=  0 ; n       rmax +=  0 ; n     } else if (c0 < c1) { n       rmin +=  0 ; n       rmax +=  1 ; n     } else { n       rmin +=  1 ; n       rmax +=  0 ; n     } n   } n   cout << rmin <<   n  << rmax <<   n ; n } n int main() { n   { ios_base::sync_with_stdio(false); } n   { cin.tie(NULL); } n   cout << setprecision(15) << fixed; n   int tc = 1; n   for (int t = 0; t < tc; t++) solve(t); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, pos, n, cnt, sz, dummy; n   vector<int> ans; n   long long k, tot = 0, examine[100005], temp; n   scanf( %d %I64d , &n, &k); n   for (i = 0; i < n; ++i) { n     scanf( %I64d , &examine[i]); n     tot += examine[i]; n   } n   if (tot < k) n     printf( -1 n ); n   else if (tot == k) n     printf(  n ); n   else { n     dummy = n; n     while (k > (long long)n) { n       temp = k / (long long)n; n       k = k % (long long)n; n       cnt = 0; n       for (i = 0; i < dummy; ++i) { n         if (examine[i] != 0) { n           if ((long long)examine[i] <= temp) { n             k += (long long)(temp - (long long)examine[i]); n             examine[i] = 0; n             ++cnt; n           } else n             examine[i] -= temp; n         } n       } n       n -= cnt; n     } n     pos = 0; n     if (k != 0) { n       while (k != 0) { n         if (examine[pos] > 0) { n           --examine[pos]; n           --k; n         } n         ++pos; n       } n     } n     for (i = pos; i < dummy; ++i) n       if (examine[i] > 0) ans.push_back(i + 1); n     for (i = 0; i < pos; ++i) n       if (examine[i] > 0) ans.push_back(i + 1); n     sz = ans.size(); n     for (i = 0; i < (sz - 1); ++i) printf( %d  , ans[i]); n     printf( %d n , ans[sz - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   string s; n   cin >> s; n   long long l = s.length(); n   bool visit[256]; n   memset(visit, false, sizeof(visit)); n   int dis = 0, i; n   for (i = 0; i < l; i++) { n     if (!visit[s[i]]) { n       visit[s[i]] = true; n       dis++; n     } n   } n   if (dis == 1) { n     cout << ((l * (l + 1)) / 2) % 998244353; n     return 0; n   } n   int s_cnt = 1, e_cnt = 1; n   i = 1; n   int j = l - 2; n   for (; i < l; i++) { n     if (s[i] == s[0]) n       s_cnt++; n     else n       break; n   } n   for (; j >= 0; j--) { n     if (s[j] == s[n - 1]) n       e_cnt++; n     else n       break; n   } n   if (s[0] != s[l - 1]) { n     cout << (e_cnt + s_cnt + 1) % 998244353; n   } else { n     cout << (long long)((long long)(s_cnt + 1) % 998244353 * n                         (long long)(e_cnt + 1) % 998244353) % n                 998244353; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int Nmax = 100005; n int count_one(vector<int> s) { n   int ct = 0; n   int i = 0; n   while (i < s.size()) { n     if (s[i] == 1) ++ct; n     ++i; n   } n   return ct; n } n int count_zero(string s, int index) { n   int i = 0; n   int ct = 0; n   while (s[i] !=  1  && i < s.length()) { n     ++ct; n     ++i; n   } n } n int gcd(int a, int b) { n   if (a == 0) { n     return b; n   } n   return gcd(b % a, a); n } n bool isPrime(int n) { n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return 0; n   return 1; n } n void binaryRepresentation(int x) { n   for (int i = 31; i >= 0; i--) { n     if (x & (1 << i)) n       cout <<  1 ; n     else n       cout <<  0 ; n   } n } n void DFS(vector<vector<int> > &adj, int u, vector<bool> &visit, n          vector<int> &dist, vector<int> &freq) { n   visit[u] = true; n   for (int i = 0; i < adj[u].size(); i++) { n     int x = adj[u][i]; n     if (!visit[x]) { n       dist[x] = dist[u] + 1; n       freq[u]++; n       DFS(adj, adj[u][i], visit, dist, freq); n     } n   } n } n bool check(string a, string b) { n   vector<int> first(26, 0); n   vector<int> second(26, 0); n   for (int i = 0; i < a.length(); i++) { n     first[a[i] -  a ]++; n     second[b[i] -  a ]++; n   } n   if (first != second) n     return false; n   else n     return true; n } n long long combination(long long n, long long r) { n   if (r == n || r == 0) { n     return 1; n   } n   return combination(n - 1, r - 1) + combination(n - 1, r); n } n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) return a.first < b.first; n   return a.second > b.second; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long n, k; n   cin >> n >> k; n   vector<int> ar(n); n   map<long long, long long> right; n   map<long long, long long> left; n   for (long long i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   long long ans = 0; n   for (long long i = n - 1; i >= 0; i--) { n     ans += right[ar[i] * k]; n     right[ar[i]] += left[ar[i] * k]; n     left[ar[i]]++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long base = 133; n unsigned long long hash1[200005][26], hash2[200005][26], Pow[200005]; n int n, m; n char str1[200005], str2[200005]; n int main() { n   scanf( %d%d , &n, &m); n   scanf( %s%s , str1 + 1, str2 + 1); n   Pow[0] = 1; n   for (int i = 1; i <= n; i++) { n     Pow[i] = Pow[i - 1] * base; n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 1; j <= n; j++) { n       hash1[j][i] = hash1[j - 1][i] * base + (str1[j] -  a  == i); n     } n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 1; j <= m; j++) { n       hash2[j][i] = hash2[j - 1][i] * base + (str2[j] -  a  == i); n     } n   } n   vector<int> vec; n   for (int i = 1; i <= n - m + 1; i++) { n     int cnt = 0; n     for (int x = 0; x < 26; x++) { n       if (cnt != x) break; n       for (int y = 0; y < 26; y++) { n         if (hash2[m][x] == hash1[i + m - 1][y] - hash1[i - 1][y] * Pow[m]) { n           if (hash2[m][y] == hash1[i + m - 1][x] - hash1[i - 1][x] * Pow[m]) { n             cnt++; n             break; n           } n         } n       } n     } n     if (cnt == 26) vec.push_back(i); n   } n   int sz = vec.size(); n   printf( %d n , sz); n   for (int i = 0; i < sz; i++) { n     printf( %d  , vec[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5, INF = -1e9; n long long down[N], mx[N], up[N], dp[N], sum[N]; n unordered_map<long long, vector<long long>> mp[N]; n long long n, m, pre[N], suf[N], ans[N]; n vector<pair<long long, long long>> adj[N]; n bool mark[N]; n void dfs_down(long long u, long long par) { n   down[u] = up[u] = mark[u] ? 0 : INF; n   for (auto x : adj[u]) { n     long long v = x.first, w = x.second; n     if (v == par) continue; n     dfs_down(v, u); n     mp[u][down[v] + w].push_back(v); n     down[u] = max(down[u], down[v] + w); n   } n   pre[0] = suf[adj[u].size() - 1] = INF; n   for (long long i = 0; i < adj[u].size(); i++) { n     long long v = adj[u][i].first, w = adj[u][i].second; n     pre[i + 1] = v ^ par ? max(pre[i], down[v] + w) : pre[i]; n   } n   for (long long i = adj[u].size() - 1; i; i--) { n     long long v = adj[u][i].first, w = adj[u][i].second; n     suf[i - 1] = v ^ par ? max(suf[i], down[v] + w) : suf[i]; n   } n   for (long long i = 0; i < adj[u].size(); i++) n     if (adj[u][i].first ^ par) mx[adj[u][i].first] = max(suf[i], pre[i]); n } n void dfs_up(long long u, long long par) { n   for (auto x : adj[u]) { n     long long v = x.first, w = x.second; n     if (v == par) continue; n     up[v] = max(up[v], max(up[u], mx[v]) + w); n     dfs_up(v, u); n   } n } n void dfs1(long long u, long long par) { n   if (mark[u] && up[u] > down[u]) dp[u]++; n   for (auto x : adj[u]) { n     long long v = x.first, w = x.second; n     if (v == par) continue; n     dfs1(v, u); n     ans[u] += dp[v]; n     if (up[u] > mx[v]) dp[u] += dp[v]; n     if (up[u] < mx[v]) { n       long long sz = mp[u][mx[v]].size(); n       if (down[v] + w == mx[v]) sz--; n       if (sz ^ 1) continue; n       for (long long s : mp[u][mx[v]]) n         if (s ^ v) sum[s] += dp[v]; n     } n   } n } n void dfs2(long long u, long long par) { n   if (mark[u] && down[u] > up[u]) sum[u]++; n   if (mp[u][down[u]].size() == 1) sum[mp[u][down[u]][0]] += sum[u]; n   for (auto x : adj[u]) n     if (x.first ^ par) dfs2(x.first, u); n   ans[u] += sum[u]; n } n inline void read_input() { n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long v; n     cin >> v; n     mark[--v] = true; n   } n   for (long long i = 1; i < n; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     adj[--u].push_back({--v, w}); n     adj[v].push_back({u, w}); n   } n } n inline void solve() { n   dfs_down(0, 0); n   dfs_up(0, 0); n   dfs1(0, 0); n   dfs2(0, 0); n } n inline void write_output() { n   long long mx = 0, cnt = 0; n   for (long long u = 0; u < n; u++) { n     if (mark[u]) continue; n     if (mx < ans[u]) { n       mx = ans[u]; n       cnt = 0; n     } n     if (mx == ans[u]) cnt++; n   } n   cout << mx <<     << cnt << endl; n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   read_input(), solve(), write_output(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O2 ) n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   t ; n   err(++it, args...); n } n template <typename T1, typename T2> n ostream& operator<<(ostream& c, pair<T1, T2>& v) { n   c <<  (  << v.first <<  ,  << v.second <<  ) ; n   return c; n } n template <template <class...> class TT, class... T> n ostream& operator<<(ostream& out, TT<T...>& c) { n   out <<  {  ; n   for (auto& x : c) out << x <<    ; n   out <<  } ; n   return out; n } n const int LIM = 1e5 + 5, MOD = 1e9 + 7; n int t, n, m, k, x, y; n struct Gaussbase2 { n   int numofbits = 31; n   int rk = 0; n   vector<int> Base; n   Gaussbase2() { Base.assign(numofbits, 0); } n   Gaussbase2& operator=(Gaussbase2& g) { n     for (int i = 0; i < numofbits; i++) Base[i] = g.Base[i]; n     rk = g.rk; n   } n   bool canbemade(int x) { n     for (int i = numofbits - 1; i >= 0; i--) x = min(x, x ^ Base[i]); n     return x == 0; n   } n   void Add(int x) { n     for (int i = numofbits; i >= 0; i--) { n       if ((x >> i) & 1) { n         if (!Base[i]) { n           Base[i] = x; n           rk++; n           return; n         } else n           x ^= Base[i]; n       } n     } n   } n } g; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   int tot = 0; n   for (int i = 0; i < n; i++) { n     cin >> x; n     tot ^= x; n     g.Add(x); n   } n   if (tot == 0) n     cout << -1 <<   n ; n   else n     cout << g.rk <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 2e5 + 555; n int n, m, q, z; n pair<long long, int> a[N]; n int qt[N]; n long long ql[N], qr[N]; n long long ans[N]; n vector<pair<int, int> > ed[N]; n long long bl[N], br[N], arin[N]; n int f[N * 20]; n const int iu = 18; n int lg[N * 2]; n long long st[N][iu], nid[N][iu]; n long long sh[N][iu]; n void adde(int u, int v, int t) { ed[t].push_back({u, v}); } n void add(int id, int l, int r) { n   int duh = lg[r - l + 1]; n   int e = r - (1 << duh) + 1; n   st[l][duh] = max(st[l][duh], min(arin[id], sh[l][duh])); n   st[e][duh] = max(st[e][duh], min(arin[id], sh[e][duh])); n   adde(id, nid[l][duh], min(arin[id], sh[l][duh])); n   adde(id, nid[e][duh], min(arin[id], sh[e][duh])); n } n int find(int x) { n   if (f[x] != x) f[x] = find(f[x]); n   return f[x]; n } n void join(int x, int y) { n   x = find(x); n   y = find(y); n   f[x] = y; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 2; i < 262144; i++) lg[i] = lg[i / 2] + 1; n   a[++m] = {-1, -q - 1}; n   a[++m] = {1LL << n, q + 1}; n   for (int i = 1; i <= q; i++) { n     string s; n     cin >> s >> ql[i] >> qr[i]; n     if (s[0] ==  b ) n       qt[i] = 1; n     else n       qt[i] = 2; n     if (qt[i] == 1) { n       a[++m] = {ql[i], -i}; n       a[++m] = {qr[i], i}; n     } n   } n   sort(a + 1, a + m + 1); n   for (int i = 1; i < m; i++) { n     long long cl = a[i].first + (i % 2); n     long long cr = a[i + 1].first - (i % 2); n     int cc; n     if (i % 2 == 1) n       cc = q + 1; n     else n       cc = -a[i].second; n     if (cl > cr) continue; n     if (cl == cr) { n       bl[++z] = cl; n       br[z] = cr; n       arin[z] = cc; n     } else { n       long long fk = cl ^ cr; n       while (fk != (fk & -fk)) fk ^= fk & -fk; n       long long mid = cl | (fk - 1); n       bl[++z] = cl; n       br[z] = mid; n       arin[z] = cc; n       bl[++z] = mid + 1; n       br[z] = cr; n       arin[z] = cc; n     } n   } n   int ptr = z; n   for (int j = 0; j < iu; j++) { n     for (int i = 1; i <= z; i++) { n       nid[i][j] = ++ptr; n       if (j == 0) n         sh[i][j] = arin[i]; n       else if (i + (1 << (j - 1)) <= z) { n         sh[i][j] = max(sh[i][j - 1], sh[i + (1 << (j - 1))][j - 1]); n       } n     } n   } n   for (int i = 1; i <= z; i++) { n     for (int k = n - 1; k >= 0; k--) { n       long long cl, cr; n       bool die = false; n       if (br[i] - bl[i] + 1 > (1LL << k)) { n         cr = bl[i] | ((1LL << (k + 1)) - 1); n         cl = cr ^ ((1LL << (k + 1)) - 1); n         die = true; n       } else { n         cl = bl[i] ^ (1LL << k); n         cr = br[i] ^ (1LL << k); n       } n       int zl = lower_bound(br + 1, br + z + 1, cl) - br; n       int zr = lower_bound(bl + 1, bl + z + 1, cr + 1) - bl - 1; n       add(i, zl, zr); n       if (die) break; n     } n     adde(i, nid[i][0], arin[i]); n   } n   for (int j = iu - 2; j >= 0; j--) { n     for (int i = 1; i <= z; i++) { n       st[i][j] = max(st[i][j], st[i][j + 1]); n       adde(nid[i][j], nid[i][j + 1], min(st[i][j + 1], sh[i][j])); n       if (i > (1 << j)) { n         st[i][j] = max(st[i][j], st[i - (1 << j)][j + 1]); n         adde(nid[i][j], nid[i - (1 << j)][j + 1], n              min(st[i - (1 << j)][j + 1], sh[i][j])); n       } n     } n   } n   for (int i = 1; i <= ptr; i++) f[i] = i; n   for (int i = q + 1; i >= 1; i--) { n     for (auto c : ed[i]) join(c.first, c.second); n     if (qt[i] == 2) { n       int cl = lower_bound(br + 1, br + z + 1, ql[i]) - br; n       int cr = lower_bound(br + 1, br + z + 1, qr[i]) - br; n       ans[i] = find(cl) == find(cr); n     } n   } n   for (int i = 1; i <= q; i++) { n     if (qt[i] == 2) cout << ans[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     int i = (n - 1) / 2, j = i + 1; n     while (i >= 0 || j <= n - 1) { n       if (i >= 0) cout << a[i--] <<    ; n       if (j <= n - 1) cout << a[j++] <<    ; n     } 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   int aray[100007]; n   while (scanf( %d %d , &n, &m) != EOF) { n     for (int i = 1; i < n; i++) { n       scanf( %d , &aray[i]); n     } n     int sum = 0, i = 1; n     bool flag = false; n     while (true) { n       sum = (i + aray[i]); n       i = sum; n       if (sum == m) { n         flag = true; n         break; n       } n       if (sum > m) { n         break; n       } n     } n     (flag) ? printf( YES n ) : printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 333; n const long long linf = 1e18 + inf; n const int LEN = 6e5 + 5; n int n, m, q, len; n char str[LEN], s[LEN]; n int a[LEN], st[LEN], nd[LEN], suf[LEN], ord[LEN], lcp[LEN], w[LEN], root[LEN], n     L[LEN], R[LEN]; n pair<pair<int, int>, int> C[LEN]; n long long cnt[LEN]; n void add(int x, char str[]) { n   int m = strlen(str); n   s[++len] =  # ; n   st[x] = len + 1; n   for (int i = 0; i < m; i++) s[++len] = str[i]; n   nd[x] = len; n } n int f(int x) { n   if (x == root[x]) return x; n   return root[x] = f(root[x]); n } n void merge(int x, int y) { n   x = f(x); n   y = f(y); n   if (x == y) return; n   root[y] = x; n   cnt[x] += cnt[y]; n   L[x] = min(L[x], L[y]); n   R[x] = max(R[x], R[y]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , str); n     add(i, str); n   } n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= len; i++) suf[i] = s[i]; n   for (int it = 1; it < len; it *= 2) { n     for (int j = 1; j <= len; j++) { n       C[j] = make_pair(pair<int, int>(suf[j], suf[min(j + it, len + 1)]), j); n     } n     sort(C + 1, C + len + 1); n     for (int j = 1; j <= len; j++) { n       suf[C[j].second] = suf[C[j - 1].second] + (C[j].first != C[j - 1].first); n     } n   } n   for (int i = 1; i <= len; i++) ord[suf[i]] = i; n   for (int i = 1; i <= len; i++) { n   } n   int j = 0; n   for (int i = 1; i <= len; i++) { n     if (suf[i] == 1) continue; n     while (i + j <= len and ord[suf[i] - 1] + j <= len and n            s[i + j] == s[ord[suf[i] - 1] + j] and s[i + j] !=  # ) n       j++; n     lcp[suf[i]] = j; n     if (j) j--; n   } n   for (int i = 1; i <= len; i++) { n     assert(s[ord[i] + lcp[i]] != s[ord[i - 1] + lcp[i]] or n            s[ord[i] + lcp[i]] ==  # ); n   } n   for (int i = 1; i <= len; i++) { n   } n   memset(w, -1, sizeof(w)); n   for (int i = 1; i <= n; i++) { n     for (int j = st[i]; j <= nd[i]; j++) { n       w[suf[j]] = i; n     } n   } n   for (int i = 1; i <= len; i++) { n     if (w[i] != -1) cnt[i] = a[w[i]]; n     L[i] = R[i] = root[i] = i; n   } n   vector<pair<int, int> > vs; n   for (int i = 1; i <= len; i++) { n     vs.push_back(pair<int, int>(lcp[i], i)); n   } n   sort(vs.begin(), vs.end()); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int x = suf[st[i]]; n     if (nd[i] - st[i] + 1 > lcp[x] and nd[i] - st[i] + 1 > lcp[x + 1]) { n       ans = max(ans, (long long)a[i] * (nd[i] - st[i] + 1)); n     } n   } n   for (int it = len - 1; it >= 0; it--) { n     int l = vs[it].first, x = vs[it].second; n     merge(x, x - 1); n     x = f(x); n     if (lcp[L[x]] < l and lcp[R[x] + 1] < l) ans = max(ans, cnt[f(x)] * l); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, ll, v[100011]; n int dp[100011]; n const int oo = 1 << 28; n int seg[400001]; n int at, l, r; n void add(int n, int s, int e) { n   if (at < s || at > e || s > e) return; n   if (s == e) { n     seg[n] = dp[s]; n     return; n   } n   add(n * 2, s, (s + e) / 2); n   add(n * 2 + 1, (s + e) / 2 + 1, e); n   seg[n] = min(seg[n * 2], seg[n * 2 + 1]); n } n int get(int n, int s, int e) { n   if (l > e || r < s || l > r || s > e) return oo; n   if (s >= l && e <= r) return seg[n]; n   return min(get(n * 2, s, (s + e) / 2), get(n * 2 + 1, (s + e) / 2 + 1, e)); n } n int main() { n   scanf( %d%d%d , &n, &s, &ll); n   for (int i = 1; i <= n; ++i) scanf( %d , v + i); n   set<pair<int, int> > q; n   int E = n; n   for (int i = n; i > 0; --i) { n     dp[i] = oo; n     q.insert(make_pair(v[i], i)); n     while ((--q.end())->first - q.begin()->first > s) { n       q.erase(make_pair(v[E], E)); n       --E; n     } n     l = i + ll; n     r = E + 1; n     if (r >= l) { n       if (r == n + 1) n         dp[i] = 1; n       else n         dp[i] = min(dp[i], 1 + get(1, 1, n)); n     } n     at = i; n     add(1, 1, n); n   } n   if (dp[1] > 500000) dp[1] = -1; n   printf( %d n , dp[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10; n const int mod = 1e9 + 7; n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = (x << 1) + (x << 3) + c -  0 , c = getchar(); n   return x * f; n } n int inc(int x, int y) { return (x + y >= mod) ? (x + y - mod) : (x + y); } n int del(int x, int y) { return (x - y < 0) ? (x - y + mod) : (x - y); } n int qpow(int a, int x, int mo) { n   int res = 1; n   while (x) { n     if (x & 1) res = 1ll * res * a % mo; n     a = 1ll * a * a % mod; n     x >>= 1; n   } n   return res; n } n vector<pair<int, int> > g[N]; n long long dp[N][N]; n map<int, long long> mp; n int q[N]; n int main() { n   int n = read(), m = read(), Q = read(); n   for (int i = 0; i < m; i++) { n     int u = read(), v = read(), w = read(); n     g[u].push_back(make_pair(v, w)); n     g[v].push_back(make_pair(u, w)); n   } n   for (int i = 0; i <= m; i++) n     for (int j = 1; j <= n; j++) dp[i][j] = -2e18; n   dp[0][1] = 0; n   int ans = 0; n   for (int i = 1; i <= m; i++) { n     long long res = 0; n     for (int j = 1; j <= n; j++) n       for (int t = 0; t < g[j].size(); t++) { n         pair<int, int> now = g[j][t]; n         int u = now.first, v = j; n         dp[i][u] = max(dp[i - 1][v] + now.second, dp[i][u]); n         if (i == m) n           mp[now.second] = max(dp[i - 1][v] + now.second, mp[now.second]); n         res = max(res, dp[i][u]); n       } n     ans = inc(ans, res % mod); n   } n   vector<pair<int, int> > p; n   for (auto it : mp) { n     p.push_back(it); n   } n   int head = 1, tail = 0; n   int Inv2 = qpow(2, mod - 2, mod); n   for (int i = 0; i < p.size(); i++) { n     while (head <= tail && p[q[tail]].second <= p[i].second) tail--; n     q[++tail] = i; n   } n   Q -= m; n   int cnt = 0; n   int pre = 1; n   for (int i = 2; i <= tail; i++) { n     pair<int, int> now = p[q[pre]]; n     pair<int, int> nxt = p[q[i]]; n     bool ok = 1; n     long long k = (now.second - nxt.second) / (nxt.first - now.first); n     for (int j = i + 1; j <= tail; j++) { n       pair<int, int> o = p[q[j]]; n       if ((now.second - o.second) / (o.first - now.first) <= k) { n         ok = 0; n         break; n       } n     } n     k -= cnt; n     if (k <= 0 || ok == 0) continue; n     if (Q <= k) k = Q; n     ans = inc(ans, inc(1ll * k * (1 + k) % mod * Inv2 % mod * now.first % mod, n                        1ll * (now.second + 1ll * cnt * now.first % mod) % mod * n                            k % mod)); n     Q -= k; n     cnt += k; n     pre = i; n     if (Q == 0) break; n   } n   pair<int, int> now = p[q[tail]]; n   if (Q > 0) n     ans = inc(ans, inc(1ll * Q * (1 + Q) % mod * Inv2 % mod * now.first % mod, n                        1ll * (now.second + 1ll * cnt * now.first % mod) % mod * n                            Q % mod)); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1e6 + 5; n long long int power(long long int base, long long int exp) { n   long long int res = 1; n   while (exp) { n     if (exp % 2) res *= base; n     base *= base; n     res %= mod; n     base %= mod; n     exp /= 2; n   } n   return res; n } n long long int x[2], y[2], val[2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, n, a, b, c; n   for (long long int j = 0; j < 2; j++) cin >> x[j] >> y[j]; n   cin >> n; n   long long int ans = 0; n   for (i = 1; i <= n; i++) { n     cin >> a >> b >> c; n     for (long long int j = 0; j < 2; j++) { n       val[j] = -1; n       if (a * x[j] + b * y[j] + c > 0) val[j] = 1; n     } n     if (val[1] > 0 && val[0] < 0) ans++; n     if (val[1] < 0 && val[0] > 0) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const bound = 1e6 + 2, N = 2e5 + 1; n long long OO = 1e18; n double eps = 1e-6; n int oo = 1e9, mod = 1e9 + 7; n int n, m, s, app[N][2]; n pair<int, int> par[N][2]; n vector<int> adj[N], rev[N]; n queue<pair<int, int> > q; n int vis[N]; n void buildpath() { n   puts( Win ); n   int cur = s, c = 1; n   while (cur != -1) { n     printf( %d  , cur); n     pair<int, int> ne = par[cur][c]; n     cur = ne.first; n     c = ne.second; n   } n } n bool cyc(int u) { n   vis[u] = 2; n   for (auto v : rev[u]) { n     if (vis[v] == 2) return 1; n     if (!vis[v] && cyc(v)) return 1; n   } n   vis[u] = 1; n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int(i) = 1; (i) <= (n); (i)++) { n     int sz; n     scanf( %d , &sz); n     if (!sz) { n       q.push({i, 0}); n       app[i][0] = 1; n       par[i][0] = {-1, -1}; n     } n     while (sz--) { n       int a; n       scanf( %d , &a); n       adj[a].push_back(i); n       rev[i].push_back(a); n     } n   } n   scanf( %d , &s); n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     q.pop(); n     for (auto v : adj[cur.first]) { n       int cost = cur.second + 1; n       int md = cost % 2; n       if (app[v][md]) continue; n       app[v][md] = 1; n       par[v][md] = {cur.first, cur.second}; n       if (v == s && md) { n         buildpath(); n         return 0; n       } n       q.push({v, cost % 2}); n     } n   } n   if (cyc(s)) { n     puts( Draw ); n     return 0; n   } n   puts( Lose ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[25][25][5], ans, n, t; n int main() { n   cin >> n >> t; n   for (int i = 1; i <= 4; i++) n     for (int j = i + 1; j <= 4; j++) dp[2][1][j]++; n   for (int i = 3; i <= n; i++) n     for (int j = 1; j <= t * 2; j++) n       for (int k = 1; k <= 4; k++) { n         if (j & 1) n           for (int l = 1; l < k; l++) n             dp[i][j][k] += dp[i - 1][j][l] + dp[i - 1][j - 1][l]; n         else n           for (int l = k + 1; l <= 4; l++) n             dp[i][j][k] += dp[i - 1][j][l] + dp[i - 1][j - 1][l]; n       } n   for (int i = 1; i <= 4; i++) ans += dp[n][t * 2][i]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     int a = 0, b = 0, k = 2; n     for (int i = 0; i < n / 2 - 1; i++) { n       a += k; n       k *= 2; n     } n     for (int i = 0; i < n / 2; i++) { n       b += k; n       k *= 2; n     } n     a += k; n     cout << abs(a - b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int k, t, state[16]; n unordered_map<unsigned long long, long long> M[10]; n unsigned long long tobeull(int state[]) { n   int tmp[16] = { n       0, n   }; n   unsigned long long ret = 0; n   for (int i = 0; i < 16; i++) tmp[i] = state[i]; n   sort(tmp, tmp + 16); n   for (int i = 0; i < 16; i++) { n     ret |= (tmp[i] << (i * 4)); n   } n   return ret; n } n long long dp(int len, int state[]) { n   if (len == 0) { n     return 1; n   } n   auto it = M[len].emplace(tobeull(state), 0); n   if (it.second) { n     long long ret = 0; n     for (int i = 0; i < 16; i++) { n       if (state[i] == t) continue; n       state[i]++; n       ret += dp(len - 1, state); n       state[i]--; n     } n     it.first->second = ret; n     return it.first->second; n   } else n     return it.first->second; n } n int main() { n   scanf( %d %d , &k, &t); n   if (k <= 15) { n     printf( %lx , k); n     return 0; n   } n   int mylen = 1; n   state[15]++; n   long long rem = 0; n   while (1) { n     long long key = dp(mylen - 1, state) * (long long)15; n     if (rem + key >= k) { n       break; n     } else n       rem += key; n     ++mylen; n   } n   int st = 1; n   state[15] = 0; n   for (int digit = 1; digit <= mylen; digit++) { n     long long rem = 0; n     if (st == 1) { n       state[15] = 1; n       for (int ite = mylen - 2; ite >= 0; ite--) n         rem += dp(ite, state) * (long long)15; n       state[15] = 0; n     } n     for (int num = st; num < 16; num++) { n       if (state[num] == t) continue; n       state[num]++; n       long long cnt = dp(mylen - digit, state); n       if (rem + cnt >= k) { n         printf( %x , num); n         break; n       } else { n         rem += cnt; n         state[num]--; n       } n     } n     k -= rem; n     st = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, ans = -2; n   cin >> n; n   vector<int> v(n); n   for (auto &a : v) cin >> a; n   vector<int> max_ele_index; n   int maxx = 0; n   for (auto a : v) maxx = max(a, maxx); n   for (int i = 0; i < n; i++) n     if (v[i] == maxx) max_ele_index.push_back(i); n   if (v[0] == maxx and v[1] != maxx) { n     cout << 1 <<   n ; n     return; n   } n   if (v[0] != maxx and v[1] == maxx) { n     cout << 2 <<   n ; n     return; n   } n   if (v[n - 1] == maxx and v[n - 2] != maxx) { n     cout << n <<   n ; n     return; n   } n   for (int i = 1; i < n - 1; i++) n     if (v[i] == maxx and (v[i - 1] != maxx or v[i + 1] != maxx)) { n       ans = i; n       break; n     } n   cout << ans + 1 <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[5100], b[5100]; n long long c[5100]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(b, b + n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       int d = b[j] - a[i]; n       if (d > 0) n         c[j] = min(c[j - 1], d + c[j]); n       else n         c[j] -= d; n     } n   long long ans = c[0]; n   while (--n) n     if (ans > c[n]) ans = c[n]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000 * 1000 * 1000 + 7; n const int INF = 2000 * 1000 * 1000; n const double EPS = 1e-9; n const double pi = acos(-1.0); n template <typename T> n inline T sqr(T n) { n   return (n * n); n } n long long gcd(int a, int b) { n   while (a && b) { n     a %= b; n     swap(a, b); n   } n   return a + b; n } n struct Point { n   long long x, y; n   Point() {} n   Point(int x, int y) : x(x), y(y) {} n   Point(const Point& a) { *this = a; } n   Point operator-(const Point& a) const { return Point(x - a.x, y - a.y); } n   inline long long dist() { return sqr(x) + sqr(y); } n   bool operator<(const Point& p1) const { n     return (x < p1.x || (x == p1.x && y < p1.y)); n   } n }; n struct Line { n   long long a, b, c; n   Line() {} n   inline void normalize() { n     if (a < 0 || (a == 0 && b < 0)) { n       a *= -1; n       b *= -1; n       c *= -1; n     } n     long long g = gcd(gcd(abs(a), abs(b)), abs(c)); n     if (g > 1) { n       a /= g; n       b /= g; n       c /= g; n     } n   } n   Line(int aa, int bb, int cc) { n     a = aa, b = bb, c = cc; n     normalize(); n   } n   Line(const Point& p1, const Point& p2) { n     a = p2.y - p1.y; n     b = p1.x - p2.x; n     c = p1.y * p2.x - p1.x * p2.y; n     normalize(); n   } n }; n const int maxn = 300010; n vector<pair<Point, int> > v[maxn]; n vector<int> vec[maxn]; n map<long long, int> position; n vector<Point> tmp[maxn]; n Point center[2000]; n long long rad[2000]; n long long get_hash(const Line& l) { n   long long a = l.a + 50000; n   long long b = l.b + 50000; n   long long c = l.c + 2000000000; n   long long ret = c * 2000000000LL; n   ret += a * 100000; n   ret += b; n   return ret; n } n void prepare(int pos) { n   vector<pair<Point, int> >& cur = v[pos]; n   sort((cur).begin(), (cur).end()); n   vec[pos].resize(cur.size()); n   int cnt = 0; n   for (size_t i = 0; i < cur.size(); i++) { n     if (cur[i].second == 0) n       cnt++; n     else n       cnt--; n     vec[pos][i] = cnt; n   } n } n long long get_ans(const Point& p1, const Point& p2) { n   Line cur(p1, p2); n   Point mid((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); n   Point norm(cur.a, cur.b); n   norm.x += mid.x; n   norm.y += mid.y; n   cur = Line(mid, norm); n   long long h = get_hash(cur); n   if (!position.count(h)) return 0; n   int pos = position[h]; n   int q = upper_bound((v[pos]).begin(), (v[pos]).end(), make_pair(mid, 1)) - n           v[pos].begin() - 1; n   if (q != -1) return vec[pos][q]; n   return 0; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   Point p1, p2; n   int sz = 0; n   for (int i = 0; i < n; i++) { n     scanf( %I64d%I64d%I64d%I64d , &p1.x, &p1.y, &p2.x, &p2.y); n     p1.x *= 2, p1.y *= 2; n     p2.x *= 2, p2.y *= 2; n     Line cur(p1, p2); n     long long h = get_hash(cur); n     int pos = 0; n     if (position.count(h)) n       pos = position[h]; n     else { n       pos = sz++; n       position[h] = pos; n     } n     v[pos].push_back(make_pair(p1, (p1 < p2) ? 0 : 2)); n     v[pos].push_back(make_pair(p2, (p1 < p2) ? 2 : 0)); n   } n   for (int i = 0; i < sz; i++) prepare(i); n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     scanf( %I64d%I64d%I64d , &center[i].x, &center[i].y, &rad[i]); n     center[i].x *= 2; n     center[i].y *= 2; n     rad[i] *= 2; n     for (size_t j = 0; j < tmp[rad[i]].size(); j++) { n       if ((center[i] - tmp[rad[i]][j]).dist() > 4 * sqr(rad[i])) n         ans += get_ans(center[i], tmp[rad[i]][j]); n     } n     tmp[rad[i]].push_back(center[i]); n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma hdrstop n #pragma argsused n using namespace std; n int main(int argc, char* argv[]) { n   int i, j, resp, resn, resv, resl, res, n, a, b; n   vector<int> x, y; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d %d , &a, &b); n     x.push_back(a); n     y.push_back(b); n   } n   res = 0; n   for (i = 0; i < n; i++) { n     resv = 0; n     resn = 0; n     resp = 0; n     resl = 0; n     for (j = 0; j < n; j++) { n       if (i != j) { n         if (x[i] == x[j] && y[i] > y[j]) n           resn++; n         else if (x[i] == x[j] && y[i] < y[j]) n           resv++; n         else if (x[i] > x[j] && y[i] == y[j]) n           resl++; n         else if (x[i] < x[j] && y[i] == y[j]) n           resp++; n       } n       if (resp != 0 && resl != 0 && resv != 0 && resn != 0) { n         res++; n         break; n       } n     } n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long d; n   cin >> n >> d; n   vector<long long> v; n   long long x; n   for (int i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n   } n   int count = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (v[i] - v[j] >= 0 && v[i] - v[j] <= d) n         count++; n       else if (v[j] - v[i] >= 0 && v[j] - v[i] <= d) n         count++; n     } n   } n   cout << count * 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   vector<int> c(n); n   for (int &x : c) cin >> x; n   long long cur = 1; n   for (int &x : c) { n     cur = gcd(k, lcm(cur, x)); n   } n   if (cur == k) { n     cout <<  Yes n ; n   } else { n     cout <<  No n ; n   } n   return 0; n } n