#include <bits/stdc++.h> n using namespace std; n long long n, len, lcm, g, mod; n long long a[266666], b[266666], c[266666], d[266666], e[266666], f[266666]; n long long pw[266666], tmp[266666], w[2][266666], rev[266666]; n long long mul(long long a, long long b) { n   return (a * b - (long long)(((long double)a * b + 0.5) / mod) * mod + mod) % n          mod; n } n long long qp(long long a, long long b) { n   long long c = 1; n   for (; b; b >>= 1, a = mul(a, a)) n     if (b & 1) c = mul(c, a); n   return c; n } n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n void init() { n   for (len = 1; len <= 2 * n; len <<= 1) n     ; n   lcm = len / gcd(len, n) * n; n   mod = lcm + 1; n   while (mod < 5000000) mod += lcm; n   for (;; mod += lcm) { n     long long flag = 0; n     for (long long i = 2; i * i <= mod; i++) n       if (mod % i == 0) { n         flag = 1; n         break; n       } n     if (!flag) break; n   } n   for (g = 3;; g++) { n     long long flag = 0; n     for (long long i = 2; i * i <= mod - 1; i++) n       if ((mod - 1) % i == 0) { n         if (qp(g, i) == 1) { n           flag = 1; n           break; n         } n         if (qp(g, (mod - 1) / i) == 1) { n           flag = 1; n           break; n         } n       } n     if (!flag) break; n   } n   long long dan = qp(g, (mod - 1) / n); n   w[0][0] = w[1][0] = 1; n   for (long long i = 1; i < n; i++) w[0][i] = mul(w[0][i - 1], dan); n   for (long long i = 1; i < n; i++) w[1][i] = w[0][n - i]; n   for (long long i = 0; i < len; i++) { n     if (i & 1) n       rev[i] = (rev[i >> 1] >> 1) | (len >> 1); n     else n       rev[i] = rev[i >> 1] >> 1; n   } n } n void fft(long long *a, long long o) { n   for (long long i = 0; i < len; i++) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (long long k = 2, dan, now, t; k <= len; k <<= 1) { n     dan = qp(g, (o ? (mod - 1 - (mod - 1) / k) : (mod - 1) / k)); n     for (long long i = 0; i < len; i += k) { n       now = 1; n       for (long long j = 0; j < (k >> 1); j++, now = mul(now, dan)) { n         t = mul(a[i + j + (k >> 1)], now); n         a[i + j + (k >> 1)] = (a[i + j] - t + mod) % mod; n         a[i + j] = (a[i + j] + t) % mod; n       } n     } n   } n   if (o) { n     long long inv = qp(len, mod - 2); n     for (long long i = 0; i < len; i++) a[i] = mul(a[i], inv); n   } n } n void init(long long o) { n   for (long long i = 0; i < 2 * n; i++) pw[i] = w[o][(i * (i - 1) / 2) % n]; n   for (long long i = 2 * n; i < len; i++) pw[i] = 0; n   reverse(pw, pw + 2 * n); n   fft(pw, 0); n } n void dft(long long *a, long long o) { n   for (long long i = 0; i < n; i++) n     tmp[i] = mul(a[i], w[o][(n - (i * (i - 1) / 2) % n) % n]); n   for (long long i = n; i < len; i++) tmp[i] = 0; n   fft(tmp, 0); n   for (long long i = 0; i < len; i++) tmp[i] = mul(tmp[i], pw[i]); n   fft(tmp, 1); n   reverse(tmp, tmp + 2 * n); n   for (long long i = 0; i < n; i++) n     a[i] = mul(tmp[i], w[o][(n - (i * (i - 1) / 2) % n) % n]); n   if (o) { n     long long inv = qp(n, mod - 2); n     for (long long i = 0; i < n; i++) a[i] = mul(a[i], inv); n   } n } n long long ans[5], ans_cnt; n signed main() { n   scanf( %lld , &n); n   init(); n   for (long long i = 0; i < n; i++) scanf( %lld , &b[i]); n   for (long long i = 0; i < n; i++) scanf( %lld , &c[i]); n   for (long long i = 0, inv = qp(mod - 2, mod - 2); i < n; i++) n     d[i] = mul((c[(i + 1) % n] - c[i] + mod) % mod, inv), e[i] = b[i]; n   init(0); n   dft(d, 0); n   reverse(e + 1, e + n); n   dft(e, 0); n   for (long long i = 0; i < n; i++) f[i] = mul(d[i], qp(e[i], mod - 2)); n   init(1); n   dft(f, 1); n   long long x2 = 0, x1 = 0, x0 = -c[0]; n   for (long long i = 0, now = 0, k; i < n; i++) { n     k = b[i] - now; n     x2++; n     x1 -= 2 * k; n     x0 += k * k; n     a[i] = ((f[i] < mod - f[i]) ? f[i] : f[i] - mod); n     if (a[i] > 1000000) { n       puts( 0 ); n       return 0; n     } n     now = now + a[i]; n   } n   long long det = x1 * x1 - 4 * x2 * x0, sq = sqrt(det); n   if (det < 0 || sq * sq != det) { n     puts( 0 ); n     return 0; n   } n   if (((-x1 - sq) % (2 * x2)) == 0) ans[++ans_cnt] = (-x1 - sq) / (2 * x2); n   if (((-x1 + sq) % (2 * x2)) == 0 && (!ans_cnt || sq)) n     ans[++ans_cnt] = (-x1 + sq) / (2 * x2); n   printf( %lld n , ans_cnt); n   if (!ans_cnt) return 0; n   for (long long t = 1, now; t <= ans_cnt; t++) { n     now = ans[t]; n     printf( %lld  , now); n     for (long long i = 0; i < n - 1; i++) { n       now = now + a[i]; n       printf( %lld  , now); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n const int maxe = maxn * 2; n int fir[maxn]; n struct Edge { n   int v, c, nxt; n   Edge(int v = 0, int c = 0, int nxt = 0) : v(v), c(c), nxt(nxt) {} n } edge[maxe]; n int cnt; n bool cir[maxn]; n void init() { n   memset(fir, -1, sizeof(fir)); n   cnt = 0; n } n void add_edge(int u, int v, int c) { n   edge[cnt] = Edge(v, c, fir[u]); n   fir[u] = cnt++; n } n bool dfs(int from, int to, int c) { n   cir[from] = true; n   if (from == to) return true; n   for (int e = fir[from]; e != -1; e = edge[e].nxt) n     if (!cir[edge[e].v]) { n       if (edge[e].c != c) continue; n       if (dfs(edge[e].v, to, c)) { n         return true; n       } n     } n   return false; n } n int solve(int from, int to) { n   int ans = 0; n   bool vis[maxn]; n   memset(vis, false, sizeof(vis)); n   for (int e = fir[from]; e != -1; e = edge[e].nxt) n     if (!vis[edge[e].c]) { n       memset(cir, false, sizeof(cir)); n       if (dfs(from, to, edge[e].c)) { n         ans++; n       } n       vis[edge[e].c] = true; n     } n   return ans; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   init(); n   for (int i = 0; i < m; i++) { n     int u, v, c; n     scanf( %d%d%d , &u, &v, &c); n     add_edge(u, v, c); n     add_edge(v, u, c); n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int u, v; n     scanf( %d%d , &u, &v); n     printf( %d n , solve(u, v)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e17; n struct item { n   long long t, d, p; n }; n long long n, dp[105][2100][2], ans; n item arr[200], arrc[200]; n long long solve(long long pos, long long tim) { n   if (pos > n) return 0; n   if (dp[pos][tim][0]) return max(dp[pos][tim][0], dp[pos][tim][1]); n   long long ts = tim + arr[pos].t; n   dp[pos][tim][0] = solve(pos + 1, tim); n   if (ts < arr[pos].d) dp[pos][tim][1] = solve(pos + 1, ts) + arr[pos].p; n   return max(dp[pos][tim][0], dp[pos][tim][1]); n } n bool comp(item l, item r) { n   if (l.d == r.d) { n     if (l.t == r.t) return l.p > r.p; n     return l.t < r.t; n   } n   return l.d < r.d; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i].t >> arr[i].d >> arr[i].p; n     arrc[i] = {arr[i].t, arr[i].d, arr[i].p}; n   } n   sort(arr + 1, arr + n + 1, comp); n   ans = solve(1, 0); n   cout << ans << endl; n   long long ansar[300], m = 0; n   long long tim = 0; n   for (int i = 1; i <= n && ans; i++) { n     if (dp[i][tim][1] == ans) { n       ansar[++m] = i; n       tim += arr[i].t; n       ans -= arr[i].p; n     } n   } n   cout << m << endl; n   for (int i = 1; i <= m; i++) { n     long long first = ansar[i]; n     for (int j = 1; j <= n; j++) { n       if (arr[first].t == arrc[j].t && arr[first].d == arrc[j].d && n           arr[first].p == arrc[j].p) { n         cout << j <<    ; n         arrc[j].t = 1000000000000; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> ve; n const int MAX = 51; n long long C[MAX][MAX]; n void build() { n   for (int i = 0; i < MAX; ++i) n     for (int j = 0; j < MAX; ++j) n       C[i][j] = (j == 0) ? 1 : ((i == 0) ? 0 : C[i - 1][j - 1] + C[i - 1][j]); n } n double memo[MAX][MAX][MAX]; n double solve(int i, int last, int mx) { n   if (i == m) { n     if (last != n) return 0; n     return mx; n   } n   double &ret = memo[i][last][mx]; n   if (ret == ret) return ret; n   ret = 0; n   for (int j = 0; j <= n - last; j++) { n     ret += C[n - last][j] * 1LL * n            solve(i + 1, last + j, max(mx, (j / ve[i]) + (j % ve[i] != 0))); n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   build(); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     ve.push_back(x); n   } n   memset(memo, -1, sizeof memo); n   cout << fixed << setprecision(20) << powl(1.0 / m, n) * solve(0, 0, 0) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[100005]; n int bas[100005]; n const int INF = 0x7fffffff; n bool cmp(int a, int b) { return a > b; } n int main() { n   int n, m; n   while (scanf( %d , &m) != EOF) { n     memset(s, 0, sizeof(s)); n     memset(bas, 0, sizeof(bas)); n     int i, j; n     int minbas = INF; n     for (i = 0; i < m; i++) { n       scanf( %d , &bas[i]); n       if (minbas > bas[i]) minbas = bas[i]; n     } n     scanf( %d , &n); n     for (i = 0; i < n; i++) { n       scanf( %d , &s[i]); n     } n     sort(s, s + n, cmp); n     int sum = 0; n     int tot = 0; n     int pan = 0; n     for (i = 0; i < n;) { n       for (j = 0; j < minbas; j++) { n         sum += s[i]; n         i++; n         if (i >= n) { n           pan = 1; n           break; n         } n       } n       i += 2; n     } n     printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, arr[55] = {}, sm = 0, dp[55][2] = {}; n long long rec(int ind, bool dt) { n   if (ind == n - 1) { n     if (dt) n       return 0; n     else n       return arr[ind]; n   } n   if (dp[ind][dt] != -1) return dp[ind][dt]; n   if (dt) n     return dp[ind][dt] = min(rec(ind + 1, dt) + arr[ind], rec(ind + 1, !dt)); n   else n     return dp[ind][dt] = max(rec(ind + 1, dt), rec(ind + 1, !dt) + arr[ind]); n } n int main() { n   cin >> n; n   for (int i = 0; i < 55; i++) { n     dp[i][0] = -1; n     dp[i][1] = -1; n   } n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     sm += arr[i]; n   } n   long long ans = rec(0, 0); n   cout << (sm - ans) <<     << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[251][251][51]; n bool check(int n, int ans, int t) { n   if (t == 0) { n     if (ans == n) return 1; n     return 0; n   } n   bool ret = 0; n   if (arr[n][ans + 3][t - 1] == -1) n     arr[n][ans + 3][t - 1] = check(n, ans + 3, t - 1); n   ret |= arr[n][ans + 3][t - 1]; n   if (arr[n][ans + 4][t - 1] == -1) n     arr[n][ans + 4][t - 1] = check(n, ans + 4, t - 1); n   ret |= arr[n][ans + 4][t - 1]; n   if (arr[n][ans + 5][t - 1] == -1) n     arr[n][ans + 5][t - 1] = check(n, ans + 5, t - 1); n   ret |= arr[n][ans + 5][t - 1]; n   return ret; n } n int main() { n   for (int i = 0; i < 251; i++) n     for (int ii = 0; ii < 251; ii++) n       for (int iii = 0; iii < 51; iii++) arr[i][ii][iii] = -1; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i <= n; i++) { n     if (check(k - i * 2, 0, n - i)) { n       cout << i; n       return 0; n     } n   } n   cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1000010; n struct Point { n   long long x, y; n   Point() {} n   Point(long long x, long long y) : x(x), y(y) {} n   bool operator<(const Point &rhs) const { n     if (x == rhs.x) return y < rhs.y; n     return x < rhs.x; n   } n } p[5000]; n long long fac[maxn]; n long long powmod(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long C(long long n, long long m) { n   if (m > n || m < 0) return 0; n   long long s1 = fac[n], s2 = fac[m] * fac[n - m] % mod; n   return s1 * powmod(s2, mod - 2) % mod; n } n long long dp[maxn]; n int n, m, k; n int main() { n   fac[0] = 1LL; n   for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * i % mod; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     scanf( %lld%lld , &p[i].x, &p[i].y); n     p[i].x--, p[i].y--; n   } n   p[++k].x = n - 1; n   p[k].y = m - 1; n   sort(p + 1, p + k + 1); n   for (int i = 1; i <= k; i++) { n     dp[i] = C(p[i].x + p[i].y, p[i].x); n     for (int j = 1; j < i; j++) { n       if (p[j].y <= p[i].y) { n         dp[i] += (mod - dp[j] * C(p[i].x - p[j].x + p[i].y - p[j].y, n                                   p[i].x - p[j].x)); n         dp[i] %= mod; n       } n     } n   } n   printf( %lld n , (dp[k] + mod) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   long long int mx = 0; n   for (long long int i = 0; i < a.size(); i++) { n     long long int g = (a[i] -  0 ); n     mx = max(mx, g); n   } n   for (long long int i = 0; i < b.size(); i++) { n     long long int g = (b[i] -  0 ); n     mx = max(mx, g); n   } n   mx++; n   string h =   ; n   long long int c = 0, s = 0; n   for (long long int i = 0; i < 20; i++) { n     if (a.size() < i) a =  0  + a; n     if (b.size() < i) b =  0  + b; n   } n   for (long long int i = 18; i >= 0; i--) { n     long long int q = (a[i] -  0 ), w = (b[i] -  0 ); n     s = (q + w + c) % mx; n     char ch = ( 0  + s); n     h = ch + h; n     c = (q + w + c) / mx; n   } n   long long int k = 0; n   while (h[k] ==  0 ) k++; n   cout << 19 - k << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e6; n const long long int inf = 1e14; n const long long int mod = 1e9 + 7; n const double PI = acos(-1); n long long int powm(long long int a, long long int b) { n   a = a % mod; n   long long int res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n long long int n, a[N]; n long double f(long double xn) { n   set<long double> b = {0}; n   long double su = 0, ans = 0; n   long double ma = 0, mi = 0; n   for (int i = 0; i < n; i++) { n     su += (long double)a[i] - xn; n     ans = max({ans, abs(su - mi), abs(su - ma)}); n     ma = max(ma, su); n     mi = min(mi, su); n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cout << fixed << std::setprecision(10); n   while (t--) { n     cin >> n; n     long double B = -100000; n     long double A = 100000; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       B = max(B, (long double)a[i]); n       A = min(A, (long double)a[i]); n     } n     for (int i = 0; i < 100; i++) { n       long double m1 = (A * 2 + B) / 3.0; n       long double m2 = (A + 2 * B) / 3.0; n       if (f(m1) > f(m2)) n         A = m1; n       else n         B = m2; n     } n     cout << f(A) <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const long long mod = 1e9 + 7; n long long a[200010]; n int main() { n   long long n, k; n   cin >> n >> k; n   long long i; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   long long sum = 0; n   for (i = 1; i <= n; i++) { n     long long temp = min(i, n - i + 1); n     temp = min(temp, n - k + 1); n     temp = min(temp, k); n     sum += temp * a[i]; n   } n   double ans = double(sum) / (n - k + 1); n   printf( %.10lf , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     int m = n % 4; n     cout << (m == 0 || m == 3 ? 0 : 1) << endl << n / 2 <<    ; n     for (int i = n; i >= 4; i -= 4) cout << i - 1 <<     << i - 2 <<    ; n     if (m == 2) n       cout <<  1   << endl; n     else if (m == 3) n       cout <<  3   << endl; n     else n       cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n int n, m, k; n struct node { n   int x, y, step; n }; n char s[maxn][maxn]; n bool vis[maxn][maxn][4]; n int go[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; n int bfs(int x, int y, int tx, int ty) { n   for (int i = 0; i < 4; ++i) vis[x][y][i] = 1; n   queue<node> q; n   node tmp, tem; n   tem.x = x, tem.y = y, tem.step = 0; n   q.push(tem); n   int xx, yy; n   while (!q.empty()) { n     tmp = q.front(); n     q.pop(); n     if (tmp.x == tx && tmp.y == ty) return tmp.step; n     for (int i = 0; i < 4; i++) { n       tem = tmp; n       tem.step++; n       for (int j = 1; j <= k; j++) { n         xx = tem.x + go[i][0]; n         yy = tem.y + go[i][1]; n         if (xx < 0 || xx > n) break; n         if (yy < 0 || yy > m) break; n         if (s[xx][yy] !=  . ) break; n         if (vis[xx][yy][i]) break; n         tem.x = xx; n         tem.y = yy; n         vis[xx][yy][i] = 1; n         q.push(tem); n       } n     } n   } n   return -1; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   int x, xx, y, yy; n   scanf( %d%d%d%d , &x, &y, &xx, &yy); n   printf( %d n , bfs(x, y, xx, yy)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[100005]; n long long D[100005]; n long long qzh[100005]; n long long hzh[100005]; n long long ans[100005]; n long long dp[100005]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) scanf( %lld , &A[i]); n   for (int i = 1; i <= n; i++) scanf( %lld , &D[i]); n   for (int i = n; i >= 1; i--) { n     hzh[i] = A[i] + hzh[i + 1]; n     dp[i] = max(dp[i + 1], hzh[i] - D[i]); n   } n   long long MIN = D[1]; n   for (int i = 1; i <= n; i++) { n     MIN = min(MIN, D[i]); n     qzh[i] = qzh[i - 1] + A[i]; n     ans[i] = max(dp[i + 1], qzh[i] - MIN + dp[i + 1]); n   } n   if (k == 0) { n     long long res = 0; n     for (int i = 1; i <= n; i++) { n       res = max(res, dp[i]); n     } n     cout << res << endl; n   } else if (k == 1) { n     long long res = 0; n     for (int i = 1; i < n; i++) { n       res = max(res, ans[i]); n     } n     MIN = min(A[n], D[n]); n     for (int i = n - 1; i >= 1; i--) { n       dp[i] = hzh[i] - D[i]; n       res = max(res, dp[i] - MIN); n       MIN = min(MIN, min(A[i], D[i])); n     } n     cout << res << endl; n   } else { n     long long res = 0; n     for (int i = 1; i < n; i++) { n       res = max(res, qzh[n] - D[i]); n     } n     res = max(res, A[n] - D[n]); n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 50; n int mod = 1e9 + 7; n long long add(long long a, long long b) { n   return (a % mod + b % mod + mod) % mod; n } n long long mul(long long a, long long b) { return (a % mod * (b % mod)) % mod; } n int dp[N][3]; n int solve(int rem, int val) { n   if (rem == 0) return val == 1; n   int &ans = dp[rem][val]; n   if (~ans) return ans; n   ans = solve(rem - 1, val); n   ans = add(ans, solve(rem - 1, val * -1)); n   return ans; n } n int dp2[N][20]; n int solve2(int remp, int rem) { n   if (remp == 0) return rem == 0; n   int &ans = dp2[remp][rem]; n   if (~ans) return ans; n   ans = solve2(remp - 1, rem); n   if (rem) ans = add(ans, solve2(remp, rem - 1)); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(dp, -1, sizeof(dp)); n   memset(dp2, -1, sizeof(dp2)); n   int t; n   cin >> t; n   while (t--) { n     int x, y; n     cin >> x >> y; n     map<int, int> co; n     for (int i = 2; i * i <= x; i++) { n       while (x % i == 0) { n         x /= i; n         co[i]++; n       } n     } n     if (x != 1) co[x]++; n     long long ans = solve(y, 1); n     for (auto it : co) ans = mul(ans, solve2(y, it.second)); n     cout << ans <<   n ; n   } n   return 0; n } n


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


#include<cstdio> n #include<iomanip> n #include<iostream> n #include<cstring> n #include<string> n #include<cmath> n #include<map> n #include<algorithm> n #include<vector> n #include<set> n #include<queue> n #include<unordered_map> n #define Quick ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) n #define ll long long n #define P pair<int,int> n #define inf 0x3f3f3f3f n #define endl   n  n #define PI acos(-1) n #define mem(a, b) memset(a, b, sizeof a) n using namespace std; n const int N =2007; n const int mod = 1e9 + 7; n ll a[N]; n ll dp[N][N]; n int main() n { n     Quick; n  n     //重新排序使其在每一位上的（max-min）总和最小 n  n     int n; cin >> n; n     for (int i = 0; i < n; i++)cin >> a[i]; n     sort(a, a + n); n     for (int i = 0; i < n; i++) { n         for (int j = 0; j < n; j++) { n             dp[i][j] = 1e12;//初始dp数组保留最大值。 n         } n     } n     //初始化dp数组。 n     dp[0][0] = a[n - 1] - a[0]; n     //减小这个差值，可以删掉一个最小的或者最大的 n     for (int i = 1; i < n; i++) n     { n         //只删除最大值i次 n         dp[i][0] = dp[i - 1][0] + a[n - i - 1] - a[0]; //删除最大值从右边删除，那么删除之后当前最大值就是a[n-i-1]; n         //只删除最小值i次 n         dp[0][i] = dp[0][i - 1] + a[n - 1] - a[i];     //删除最小值从左边删除，那么删除之后当前最小值就是a[i]。 n     } n  n     //接下来开始进行状态转移取最优。 n     for (int i = 1; i < n; i++) { n         for (int j = 1; i + j < n; j++) { n             //i+j不得超过n次。 n             dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[n - i - 1] - a[j]);//选择删除最大值 n             dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[n - i - 1] - a[j]);//选择删除最小值 n         } n     } n     ll res = 1e12; n     for (int i = 0; i < n; i++) { n         res = min(res, dp[i][n - i - 1]); n     } n     cout << res << endl; n }


#include<bits/stdc++.h>  n using namespace std; n void solve() n { n  long long int n,s=0,sum=0,k; n  cin>>n; n  long long int arr[n+1]; n  for(int i=1;i<=n;i++) n  { n  cin>>arr[i]; n  if(i%2==1) n  sum=sum+arr[i]; n  else n  s=s+arr[i];} n  if(sum>=s) n  { n   for(int i=1;i<=n;i++) n      { n       if(i%2==1) n       cout<<arr[i]<<   ; n       else n       cout<< 1 <<   ; n   } n  }  n  else n  { n   for(int i=1;i<=n;i++) n      { n       if(i%2==0) n       cout<<arr[i]<<   ; n       else n       cout<< 1 <<   ; n   } n  } n   cout<<endl; n } n int main() { n  int t=1; n  cin >> t; n  while(t--) { n   solve(); n  } n } n  n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n struct point { n   double x, y; n   point() {} n   point(double _x, double _y) { n     x = _x; n     y = _y; n   } n   point operator+(const point &t) { return point(x + t.x, y + t.y); } n   point operator-(const point &t) { return point(x - t.x, y - t.y); } n   double dist(const point &t) { n     return ((x - t.x) * (x - t.x) + (y - t.y) * (y - t.y)); n   } n }; n struct line { n   double a, b, c; n   line() {} n   line(double _a, double _b, double _c) { n     a = _a; n     b = _b; n     c = _c; n   } n   void build(const point &p, const point &q) { n     a = p.y - q.y; n     b = q.x - p.x; n     c = -(p.x * (p.y - q.y) + p.y * (q.x - p.x)); n   } n }; n int n, lt, rt; n point p[2000]; n double mx = 1e+15, mn = -(1e+15); n bool intersect(const line &a, point &res) { n   if (fabs(a.a) <= eps) return false; n   res.y = p[0].y; n   res.x = (-a.c - (a.b * res.y)) / a.a; n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lf%lf , &p[i].x, &p[i].y); n   p[n] = p[0]; n   lt = int(min(p[0].x, p[1].x)); n   rt = int(max(p[0].x, p[1].x)); n   p[n + 1] = p[1]; n   for (int i = 1; i < n; i++) { n     line l; n     l.build(p[i], p[i + 1]); n     point res, tt = p[i + 1] - p[i]; n     if (intersect(l, res)) { n       if (tt.y < eps) n         mx = min(mx, res.x); n       else n         mn = max(mn, res.x); n     } else if ((p[1].x - p[0].x) * tt.x > eps) { n       printf( 0 n ); n       return 0; n     } n   } n   int x = ceil(mn - eps), y = floor(mx + eps); n   printf( %d n , max(0, min(y, rt) - max(x, lt) + 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long min(long long a, long long b) { return (a < b) ? a : b; } n int main() { n   long long n, m, b_max_1 = 0, b_max_2 = 0, g_min = 1e9; n   long long sum_b = 0, sum_g = 0; n   cin >> n >> m; n   long long b[n], g[m]; n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n     sum_b += b[i]; n     if (b[i] > b_max_1) { n       b_max_2 = b_max_1; n       b_max_1 = b[i]; n     } else if (b[i] > b_max_2) { n       b_max_2 = b[i]; n     } n   } n   sum_b = sum_b * m; n   for (int i = 0; i < m; ++i) { n     cin >> g[i]; n     sum_g += g[i]; n     g_min = min(g_min, g[i]); n   } n   if (b_max_1 > g_min) { n     cout << -1 <<   n ; n     return 0; n   } n   if (g_min > b_max_1) n     sum_b = sum_b + sum_g - (m - 1) * b_max_1 - b_max_2; n   else n     sum_b = sum_b - m * b_max_1 + sum_g; n   cout << sum_b <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010]; n struct seg { n   int val, idx; n } tree1[600010], tree2[600010]; n void build(int node, int s, int e) { n   if (s > e) return; n   if (s == e) { n     tree1[node].val = a[s]; n     tree1[node].idx = s; n     tree2[node].val = a[s]; n     tree2[node].idx = s; n     return; n   } n   int mid = (s + e) / 2; n   build(2 * node, s, mid); n   build(2 * node + 1, mid + 1, e); n   if (tree1[2 * node].val > tree1[2 * node + 1].val) n     tree1[node].idx = tree1[2 * node + 1].idx; n   else n     tree1[node].idx = tree1[2 * node].idx; n   if (tree2[2 * node].val > tree2[2 * node + 1].val) n     tree2[node].idx = tree2[2 * node].idx; n   else n     tree2[node].idx = tree2[2 * node + 1].idx; n   tree1[node].val = min(tree1[2 * node].val, tree1[2 * node + 1].val); n   tree2[node].val = max(tree2[2 * node].val, tree2[2 * node + 1].val); n } n seg query1(int node, int s, int end, int l, int r) { n   if (s > end || s > r || end < l) { n     seg temp; n     temp.val = 1000000000; n     temp.idx = -1; n     return temp; n   } n   if (s >= l && end <= r) return tree1[node]; n   seg res1 = query1(2 * node, s, (s + end) / 2, l, r); n   seg res2 = query1(2 * node + 1, (s + end) / 2 + 1, end, l, r); n   seg res; n   if (res1.val < res2.val) { n     res.val = res1.val; n     res.idx = res1.idx; n   } else { n     res.val = res2.val; n     res.idx = res2.idx; n   } n   return res; n } n seg query2(int node, int s, int end, int l, int r) { n   if (s > end || s > r || end < l) { n     seg temp; n     temp.val = -1000000000; n     temp.idx = -1; n     return temp; n   } n   if (s >= l && end <= r) return tree2[node]; n   seg res1 = query2(2 * node, s, (s + end) / 2, l, r); n   seg res2 = query2(2 * node + 1, (s + end) / 2 + 1, end, l, r); n   seg res; n   if (res1.val > res2.val) { n     res.val = res1.val; n     res.idx = res1.idx; n   } else { n     res.val = res2.val; n     res.idx = res2.idx; n   } n   return res; n } n int main() { n   int n; n   cin >> n; n   int q; n   cin >> q; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   build(1, 0, n - 1); n   while (q--) { n     int l, r, x; n     scanf( %d %d %d , &l, &r, &x); n     l--; n     r--; n     seg mx = query1(1, 0, n - 1, l, r); n     seg mi = query2(1, 0, n - 1, l, r); n     if (mx.val == x && mi.val == x) { n       printf( -1 n ); n     } else if (mx.val == x) { n       printf( %d n , mi.idx + 1); n     } else n       printf( %d n , mx.idx + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 10; n const int inf = 1e9 + 10; n const int N = 1e6 + 10; n vector<int> t[N * 4]; n void build(int v, int tl, int tr, vector<int> &a) { n   if (tl == tr) { n     t[v].push_back(a[tl]); n   } else { n     int tm = (tl + tr) / 2; n     build(v * 2, tl, tm, a); n     build(v * 2 + 1, tm + 1, tr, a); n     t[v] = t[v * 2]; n     for (auto i : t[v * 2 + 1]) { n       t[v].push_back(i); n     } n     sort(t[v].begin(), t[v].end()); n   } n } n int get(int v, int tl, int tr, int l, int r, int x) { n   if (l > r) { n     return 0; n   } n   if (tl == l && tr == r) { n     int ind = lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin(); n     if (ind != t[v].size() && t[v][ind] == x) { n       return 1; n     } else { n       return 0; n     } n   } n   int tm = (tl + tr) / 2; n   int ql = get(v * 2, tl, tm, l, min(r, tm), x); n   int qr = get(v * 2 + 1, tm + 1, r, max(l, tm + 1), r, x); n   return (ql + qr); n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int n, k; n   cin >> n >> k; n   vector<int> a(k), last(n + 1, k); n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n     if (last[a[i]] == k) { n       last[a[i]] = i; n     } n   } n   build(1, 0, k - 1, a); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (last[i] == k) { n       ans++; n     } n     if (i > 1 && get(1, 0, k - 1, last[i], k - 1, i - 1) == 0) { n       ans++; n     } n     if (i < n && get(1, 0, k - 1, last[i], k - 1, i + 1) == 0) { n       ans++; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool As; n char c; n int n, m, k; n inline void read(int& a) { n   a = 0; n   do c = getchar(); n   while (c <  0  || c >  9 ); n   while (c <=  9  && c >=  0 ) a = (a << 3) + (a << 1) + c -  0 , c = getchar(); n } n int Min[1211]; n double A[11][11]; n bool Has[11][11]; n const double eps = 1e-7; n int Matrix(int n) { n   int o = 1; n   double res = 1; n   if (n < 0) return 0; n   for (int i = 0; i < n; i++) { n     int j = -1; n     for (int t = i; t < n; t++) n       if (((A[t][i]) > 0 ? (A[t][i]) : -(A[t][i])) > eps) { n         j = t; n         break; n       } n     if (j ^ i) o ^= 1; n     if (j == -1) return 0; n     for (int t = 0; t < n; t++) swap(A[j][t], A[i][t]); n     for (int j = i + 1; j < n; j++) n       if (((A[j][i]) > 0 ? (A[j][i]) : -(A[j][i])) > eps) { n         double U = A[j][i] / A[i][i]; n         for (int k = 0; k < n; k++) A[j][k] -= U * A[i][k]; n       } n     res *= A[i][i]; n   } n   if (o) n     return res + 0.1; n   else n     return -res + 0.1; n } n int Build(int S) { n   vector<int> Q; n   Q.clear(); n   memset(A, 0, sizeof(A)); n   while (S) Q.push_back(Min[S]), S ^= 1 << Min[S]; n   for (int i = 0; i < Q.size(); i++) n     for (int j = 0; j < i; j++) n       if (Has[Q[i]][Q[j]]) A[i][j]--, A[j][i]--, A[j][j]++, A[i][i]++; n   return Q.size() - 1; n } n int Cnt[2002]; n int Pow(int a, int x) { n   int res = 1; n   for (x; x; x >>= 1, a = a * a) n     if (x & 1) res = res * a; n   return res; n } n int F[2002]; n int G[2002]; n int main() { n   Min[0] = -1; n   for (int i = 1; i < 1024; i++) n     Cnt[i] = (i & 1) ? Cnt[i >> 1] + 1 : Cnt[i >> 1], n     Min[i] = (i & 1) ? 0 : (Min[i >> 1] + 1); n   read(n), read(m), read(k); n   while (m--) { n     int a, b; n     read(a), read(b); n     a--, b--; n     Has[b][a] = Has[a][b] = true; n   } n   int t = 1 << n; n   t--; n   for (int i = 1; i <= t; i++) { n     int W = 1; n     for (int j = 0; j < n; j++) n       if ((1 << j) & i) { n         int f = 0; n         for (int k = 0; k < n; k++) n           if (!(i & (1 << k))) n             if (Has[j][k]) f++; n         W *= f; n       } n     int df; n     G[i] = F[i] = W * (df = Matrix(Build(t ^ i))); n   } n   int Ans = 0; n   for (int i = t; ~i; i--) { n     if (Cnt[i] == k) Ans += G[i]; n     int j = (i - 1) & i; n     while (j) { n       G[j] += (Cnt[j ^ i] & 1 ? -1 : 1) * F[i]; n       j = i & (j - 1); n     } n   } n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans; n float x, dis, t; n struct ponto { n   float ini, fim; n   bool operator<(const ponto &lhs) const { return lhs.ini > ini; } n } p[1010]; n int main() { n   scanf( %d%f , &n, &t); n   ans = 2; n   for (int i = 0; i < n; i++) { n     scanf( %f%f , &x, &dis); n     dis /= 2.0; n     p[i].ini = x - dis; n     p[i].fim = x + dis; n   } n   sort(p, p + n); n   for (int i = 1; i < n; i++) { n     if (p[i - 1].fim + t < p[i].ini) n       ans += 2; n     else if (p[i - 1].fim + t == p[i].ini) n       ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n long long int inverse(long long int i) { n   if (i == 1) return 1; n   return (MOD - ((MOD / i) * inverse(MOD % i)) % MOD + MOD) % MOD; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long x = 0, y = 0, ans = 0, len = 0, last; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s[i] ==  U ) n       y++; n     else if (s[i] ==  D ) n       y--; n     else if (s[i] ==  R ) n       x++; n     else n       x--; n     len++; n     if (len > (abs(x) + abs(y))) { n       x = 0; n       y = 0; n       len = 0; n       ans++; n       i--; n       last = i; n     } n   } n   if (last != (n - 1)) ans++; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n bool has(int s, int x) { n   int i = s / 50 % 475; n   for (int ii = (0); ii < int(25); ++ii) { n     i = (i * 96 + 42) % 475; n     if (i + 26 == x) { n       return true; n     } n   } n   return false; n } n void solve(istream& cin, ostream& cout) { n   int p, x, y; n   cin >> p >> x >> y; n   for (int i = 0; x - i * 50 >= y; i++) { n     if (has(x - i * 50, p)) { n       cout << 0; n       return; n     } n   } n   for (int i = 1;; i++) { n     if (has(x + i * 100, p) || has(x + i * 100 - 50, p)) { n       cout << i; n       return; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct vec { n   int to; n   int fro; n }; n int fa[200010], son[200010][26], mx[200010]; n int rt, lst, tot; n int n, m; n char s[200010]; n vec mp[200010]; n int tai[200010], cnt; n int P[200010]; n int ans[200010]; n vector<int> qs[200010]; n int av[200010]; n int len[200010]; n int Rt[200010], Son[4000010][2]; n int TOT; n inline void be(int x, int y) { n   mp[++cnt].to = y; n   mp[cnt].fro = tai[x]; n   tai[x] = cnt; n } n int merge(int x, int y) { n   if (!x || !y) { n     return x + y; n   } n   Son[x][0] = merge(Son[x][0], Son[y][0]); n   Son[x][1] = merge(Son[x][1], Son[y][1]); n   return x; n } n void change(int &x, int y, int z, int p) { n   if (!x) { n     x = ++TOT; n   } n   if (y == z) { n     return; n   } n   int mid = y + z >> 1; n   if (p <= mid) { n     change(Son[x][0], y, mid, p); n   } else { n     change(Son[x][1], mid + 1, z, p); n   } n } n void ins(int x) { n   int np = ++tot, p = lst; n   mx[np] = mx[p] + 1; n   while (p && !son[p][x]) { n     son[p][x] = np; n     p = fa[p]; n   } n   if (!p) { n     fa[np] = rt; n   } else { n     int q = son[p][x]; n     if (mx[q] == mx[p] + 1) { n       fa[np] = q; n     } else { n       int nq = ++tot; n       mx[nq] = mx[p] + 1; n       memcpy(son[nq], son[q], sizeof(son[q])); n       fa[nq] = fa[q]; n       fa[np] = fa[q] = nq; n       while (p && son[p][x] == q) { n         son[p][x] = nq; n         p = fa[p]; n       } n     } n   } n   lst = np; n } n int L; n int t[200010]; n void DFS(int x, int y, int z) { n   if (y == z) { n     t[++L] = y; n     return; n   } n   int mid = y + z >> 1; n   if (Son[x][0]) { n     DFS(Son[x][0], y, mid); n   } n   if (Son[x][1]) { n     DFS(Son[x][1], mid + 1, z); n   } n } n void dfs(int x) { n   int i, y; n   for (i = tai[x]; i; i = mp[i].fro) { n     y = mp[i].to; n     dfs(y); n   } n   for (i = tai[x]; i; i = mp[i].fro) { n     y = mp[i].to; n     Rt[x] = merge(Rt[x], Rt[y]); n   } n   L = 0; n   if (qs[x].size()) { n     L = 0; n     DFS(Rt[x], 1, n); n     for (i = 0; i < qs[x].size(); i++) { n       int wzh = 1; n       int ti = qs[x][i]; n       int tav = av[qs[x][i]]; n       if (L < tav) { n         ans[ti] = -1; n       } else { n         ans[ti] = 1000000000; n         for (int j = tav; j <= L; j++) { n           ans[ti] = min(ans[ti], t[j] - t[j - tav + 1] + len[ti]); n         } n       } n     } n   } n } n int main() { n   int i, x; n   lst = rt = tot = 1; n   scanf( %s , s + 1); n   n = strlen(s + 1); n   int p = rt; n   for (i = 1; i <= n; i++) { n     ins(s[i] -  a ); n     p = son[p][s[i] -  a ]; n     P[i] = p; n     change(Rt[p], 1, n, i); n   } n   for (i = 2; i <= tot; i++) { n     be(fa[i], i); n   } n   scanf( %d , &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%s , &av[i], s + 1); n     int p = rt; n     for (int j = 1; s[j]; j++) { n       p = son[p][s[j] -  a ]; n     } n     len[i] = strlen(s + 1); n     if (!p) { n       ans[i] = -1; n     } else { n       qs[p].push_back(i); n     } n   } n   dfs(1); n   for (i = 1; i <= m; i++) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool iguales(vector<string>& x, vector<string>& y) { n   for (int i = (int)0; i < (int)x.size(); i++) { n     if (x[i] ==  T  || y[i] ==  T ) continue; n     if (!(x[i] == y[i])) return false; n   } n   return true; n } n int main() { n   map<string, vector<vector<string> > > mapa; n   map<string, string> peso; n   int n, m, k; n   scanf( %d n , &n); n   string linea, nom, alfa, beta; n   while (n--) { n     getline(cin, linea); n     for (int i = (int)0; i < (int)linea.size(); i++) n       if (linea[i] ==  (  || linea[i] ==  )  || linea[i] ==  , ) linea[i] =    ; n     vector<string> x; n     stringstream flujo(linea); n     while (flujo >> linea) x.push_back(linea); n     nom = x[1]; n     x.erase(x.begin()); n     x.erase(x.begin()); n     mapa[nom].push_back(x); n   } n   cin >> m; n   while (m--) { n     cin >> alfa >> beta; n     peso[beta] = alfa; n   } n   scanf( %d n , &k); n   while (k--) { n     getline(cin, linea); n     for (int i = (int)0; i < (int)linea.size(); i++) n       if (linea[i] ==  (  || linea[i] ==  )  || linea[i] ==  , ) linea[i] =    ; n     vector<string> x; n     stringstream flujo(linea); n     int cc = 0; n     while (flujo >> linea) { n       if (cc == 0) { n         nom = linea; n         cc = 1; n       } else n         x.push_back(peso[linea]); n     } n     map<string, vector<vector<string> > >::iterator iter; n     iter = mapa.find(nom); n     if (iter != mapa.end()) { n       vector<vector<string> > r = iter->second; n       int tot = 0; n       for (int i = (int)0; i < (int)r.size(); i++) { n         vector<string> second = r[i]; n         if (x.size() == second.size()) { n           if (iguales(x, second)) tot++; n         } n       } n       cout << tot << endl; n     } else n       cout << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int n, k; n int a[200]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   int e = 0, s = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] == 1) n       e++; n     else n       s++; n   } n   int ee = e, ss = s; n   int ans = -1; n   for (int b = 1; b <= k; b++) { n     ee = e; n     ss = s; n     for (int i = 0; b + i * k <= n; i++) { n       int c = b + i * k; n       if (a[c] == 1) n         ee--; n       else n         ss--; n     } n     ans = max(ans, abs(ee - ss)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, c; n   cin >> n >> k >> c; n   int p = ((n * k * c) / 100); n   int k1 = p / k; n   int k2 = p % k; n   for (int i = 0; i < n; i++) { n     if (i != 0) cout <<    ; n     if (i < k1) n       cout << k; n     else if (i == k1) n       cout << k2; n     else n       cout << 0; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, i, j, a[100005], b[100005]; n int main() { n   j = 2; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) b[i] = b[i - 1] + a[i]; n   for (i = n; i >= 1; i--) n     if (b[i + 1] + a[i] < b[i]) { n       j = i; n       b[i] = b[i + 1] + a[i]; n     } else n       break; n   cout << j - 1 <<     << n - j + 1; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 2e5 + 10; n const long long maxB = 500; n const long long INF = 4557430888798830399; n const int INF_I = 2139062143; n const long long maxA = 1e6 + 10; n const long long MOD = 1e9 + 7; n const long long mINF = -4557430888798830400; n const int mINF_I = -2139062144; n template <typename T> n T binaryExpo(T n, T p) { n   if (p == 0) { n     return 1LL; n   } n   if (p & 1) { n     return n * binaryExpo(n, p - 1); n   } n   T ret = binaryExpo(n, p / 2); n   return ret * ret; n } n template <typename T> n T bigMod(T n, T p, T m) { n   if (p == 0) { n     return 1LL % m; n   } n   if (p & 1) { n     return (n % m * bigMod(n, p - 1, m)) % m; n   } n   T ret = bigMod(n, p / 2, m) % m; n   return (ret * ret) % m; n } n template <typename T> n T modInv(T n, T m) { n   return bigMod(n, m - 2, m); n } n bool FILE_PRINT = 0; n int marked[+2]; n vector<long long> prime; n void sieve(long long n); n bool is_prime(long long num); n int arr[maxN + 10], ans[maxN + 10], idx[maxN + 10]; n long long cum[maxN + 10]; n pair<int, int> query[maxN + 10]; n vector<int> divisors[maxN + 10]; n long long spd[maxA + 10]; n long long mp[maxN + 10]; n void calc_spd(long long n) { n   memset(spd, -1, sizeof spd); n   for (long long i = 2; i <= n; i++) { n     if (spd[i] == -1) { n       prime.push_back(i); n       mp[prime.size() - 1] = ((i - 1) * modInv(i, MOD)) % MOD; n       for (long long j = i; j <= n; j += i) { n         if (spd[j] == -1) spd[j] = prime.size() - 1; n       } n     } n   } n } n inline bool compare(long long x, long long y) { n   return make_pair(query[x].first / maxB, query[x].second) < n          make_pair(query[y].first / maxB, query[y].second); n } n signed cnt[maxN + 10]; n void solve(long long n, long long q) { n   long long a, b = -1, l, r, ans = 1; n   for (long long curr_block = -1, i = 0; i < q; i++) { n     tie(l, r) = query[idx[i]]; n     if (l / maxB != curr_block) { n       curr_block = l / maxB; n       ans = 1; n       memset(cnt, 0, sizeof cnt); n       b = (curr_block + 1) * maxB - 1; n     } n     while (b + 1 <= r) { n       b++; n       for (auto p : divisors[b]) { n         if (cnt[p] == 0) ans = (ans * mp[p]) % MOD; n         cnt[p]++; n       } n     } n     long long temp = ans; n     for (a = l; a <= min(r, (curr_block + 1) * maxB - 1); a++) { n       for (auto p : divisors[a]) { n         if (cnt[p] == 0) ans = (ans * mp[p]) % MOD; n         cnt[p]++; n       } n     } n     if (ans < 0) ans += MOD; n     ::ans[idx[i]] = (::ans[idx[i]] * ans) % MOD; n     for (a = l; a <= min(r, (curr_block + 1) * maxB - 1); a++) { n       for (auto p : divisors[a]) { n         cnt[p]--; n       } n     } n     ans = temp; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(15); n   cout << fixed; n   calc_spd(maxA + 5); n   long long number_of_test_cases = 1, test_case; n   for (test_case = 1; test_case <= number_of_test_cases; test_case++) { n     long long i, j, k, l, r, m, n, query_type, q; n     long long a, b, c, x, y, z; n     cin >> n; n     for (i = (0); i < (n); ++i) { n       cin >> arr[i]; n       x = arr[i]; n       while (x > 1) { n         long long p = spd[x]; n         while (x % prime[p] == 0) { n           x /= prime[p]; n         } n         divisors[i].push_back(p); n       } n     } n     cum[0] = arr[0]; n     for (i = (1); i <= (n - 1); ++i) { n       cum[i] = (cum[i - 1] * arr[i]) % MOD; n     } n     cin >> q; n     for (i = (0); i < (q); ++i) { n       cin >> l >> r; n       l--; n       r--; n       query[i] = make_pair(l, r); n       ans[i] = cum[r]; n       if (l > 0) ans[i] = (ans[i] * modInv((long long)cum[l - 1], MOD)) % MOD; n     } n     for (i = (0); i < (q); ++i) idx[i] = i; n     sort(idx, idx + q, compare); n     solve(n, q); n     for (i = (0); i < (q); ++i) cout << ans[i] <<   n ; n   } n   return 0; n } n bool is_prime(long long num) { n   return (num > 1 && n           (num == 2 || ((((num)&1LL) ? (1) : (0)) && n                         !(marked[(num) >> 6] & (1L << (((num) % 64) >> 1)))))); n } n void sieve(long long n) { n   long long i, j; n   (marked[(1) >> 6] |= (1L << (((1) % 64) >> 1))); n   for (i = 3; i * i <= n; i += 2) { n     if (!(marked[(i) >> 6] & (1L << (((i) % 64) >> 1)))) { n       for (j = i * i; j <= n; j += i + i) n         (marked[(j) >> 6] |= (1L << (((j) % 64) >> 1))); n     } n   } n   prime.clear(); n   prime.push_back(2); n   for (i = 3; i <= n; i += 2) { n     if (is_prime(i)) prime.push_back(i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e9 + 7; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int lcm(long long int a, long long int b) { n   return (a / gcd(a, b)) * b; n } n long long int sq(long long int x) { return x * x; } n void solve() { n   long long int n, x1, x2, y1, y2; n   cin >> n >> x1 >> y1 >> x2 >> y2; n   long long int ans = 8e18; n   vector<pair<long long int, long long int>> v(n); n   for (long long int i = 0; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     v[i].first = sq(x - x1) + sq(y - y1); n     v[i].second = sq(x - x2) + sq(y - y2); n   } n   v.push_back({0, 0}); n   for (auto i : v) { n     long long int r1 = i.first; n     long long int r2 = 0; n     for (auto j : v) { n       if (j.first > r1) { n         r2 = max(r2, j.second); n       } n     } n     ans = min(ans, r1 + r2); n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x1, x2, a, b; n int main() { n   while (cin >> x1 >> x2 >> a >> b) { n     bool ch = false; n     if (x1 > x2) { n       swap(x1, x2); n       swap(a, b); n       a = -a; n       b = -b; n       ch = true; n     } n     int dif = abs(x1 - x2); n     if (a <= 0) { n       if (dif <= b) n         cout <<  FIRST  << endl << (ch ? x1 : x2) << endl; n       else n         cout <<  DRAW  << endl; n     } else { n       int rem = dif % (a + b); n       if (rem == 0) n         cout <<  SECOND  << endl; n       else if (rem >= a && rem <= b) n         cout <<  FIRST  << endl << (ch ? x2 - rem : x1 + rem) << endl; n       else n         cout <<  DRAW  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   map<int, int> pmax, maxp; n   vector<int> g[n + 2]; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     if (pmax.find(a) == pmax.end()) n       pmax[a] = b; n     else n       pmax[a] = max(pmax[a], b); n   } n   for (auto i = pmax.begin(); i != pmax.end(); i++) { n     maxp[i->second] = i->first; n   } n   int q; n   cin >> q; n   while (q--) { n     int k; n     cin >> k; n     vector<pair<int, int> > maxpaux; n     for (int i = 0; i < k; i++) { n       int l; n       cin >> l; n       if (pmax.find(l) == pmax.end()) continue; n       maxpaux.push_back(pair<int, int>(pmax[l], l)); n       maxp.erase(pmax[l]); n     } n     if (maxp.empty()) { n       cout << 0 <<     << 0 <<   n ; n     } else if (maxp.size() == 1) { n       cout << maxp.begin()->second <<     << g[maxp.begin()->second][0] <<   n ; n     } else { n       auto en = maxp.end(); n       en--; n       int personmax = en->second; n       cout << personmax <<    ; n       en--; n       int secondperson = en->second; n       int pos = upper_bound(g[personmax].begin(), g[personmax].end(), n                             pmax[secondperson]) - n                 g[personmax].begin(); n       int answer = g[personmax][pos]; n       cout << answer <<   n ; n     } n     for (int i = 0; i < maxpaux.size(); i++) { n       maxp[maxpaux[i].first] = maxpaux[i].second; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int gcd(int x, int y) { n   int r1 = x, q, r2 = y, r; n   while (r2 > 0) { n     q = r1 / r2; n     r = r1 - q * r2; n     r1 = r2; n     r2 = r; n   } n   return r1; n } n using namespace std; n int main(int argc, char const *argv[]) { n   int n, m, x, cnt; n   char c; n   cin >> n >> m; n   char arr[n]; n   int arr1[n]; n   map<int, pair<int, int> > arr2; n   cnt = 0; n   cin >> arr; n   arr1[0] = -1; n   for (int i = 1; i < n; i++) { n     arr1[i] = -1; n     if (arr[i] ==  .  && arr[i - 1] ==  . ) { n       arr2[cnt] = make_pair(i, i - 1); n       arr1[i] = cnt; n       cnt++; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> x >> c; n     x--; n     if (arr[x] ==  .  && c ==  .  || arr[x] !=  .  && c !=  . ) n       ; n     else if (arr[x] !=  .  && c ==  . ) { n       if (x != 0 && arr[x - 1] ==  . ) { n         arr2[cnt] = make_pair(x, x - 1); n         arr1[x] = cnt; n         cnt++; n       } n       if (x != n - 1 && arr[x + 1] ==  . ) { n         arr2[cnt] = make_pair(x + 1, x); n         arr1[x + 1] = cnt; n         cnt++; n       } n     } else { n       if (x == n - 1) { n         if (arr1[x] == -1) n           ; n         else { n           arr2.erase(arr2.find(arr1[x])); n           arr1[x] = -1; n         } n       } else { n         if (arr1[x] != -1 && arr1[x + 1] != -1) { n           arr2.erase(arr2.find(arr1[x + 1])); n           arr1[x + 1] = -1; n           arr2.erase(arr2.find(arr1[x])); n           arr1[x] = -1; n         } else if (arr1[x] != -1) { n           arr2.erase(arr2.find(arr1[x])); n           arr1[x] = -1; n         } else if (arr1[x + 1] != -1) { n           arr2.erase(arr2.find(arr1[x + 1])); n           arr1[x + 1] = -1; n         } else { n         } n       } n     } n     arr[x] = c; n     printf( %d , arr2.size()); n     printf(  n ); n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n int ans, cnt, tmp; n int main() { n   int n; n   cin >> n; n   int a[] = {0, 0, 0, 0, 0}; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     a[x]++; n     cnt += x; n   } n   if (cnt == 5 || cnt < 3) return cout << -1, 0; n   ans += min(a[1], a[2]); n   a[1] -= ans, a[2] -= ans, a[3] += ans; n   if (a[1] != 0) { n     ans += a[1] / 3 * 2; n     a[3] += a[1] / 3; n     a[1] = a[1] % 3; n     if (a[1] == 1) { n       if (a[3] != 0) n         ans += 1; n       else n         ans += 2; n     } else if (a[1] == 2) { n       ans += 2; n     } n     return cout << ans, 0; n   } else if (a[2] != 0) { n     ans += a[2] / 3 * 2; n     a[3] += a[2] / 3 * 2; n     a[2] = a[2] % 3; n     if (a[2] == 1) { n       if (a[4] != 0) n         ans++; n       else n         ans += 2; n     } else if (a[2] == 2) { n       ans += 2; n     } n     return cout << ans, 0; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   const int N = 26; n   int mas[26]; n   for (int i = 0; i < N; ++i) mas[i] = 0; n   int rez = 0; n   for (int i = 0; i < n * 2 - 2; ++i) { n     char a; n     cin >> a; n     if (isupper(a)) { n       if (mas[tolower(a) -  a ] > 0) n         mas[tolower(a) -  a ]--; n       else n         rez++; n     } else n       mas[a -  a ]++; n   } n   cout << rez << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[505]; n pair<int, int> query(set<int> s) { n   cout <<  ?  ; n   for (auto e : s) { n     cout << e <<    ; n   } n   cout << endl; n   pair<int, int> res; n   cin >> res.first >> res.second; n   a[res.first] = res.second; n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   set<int> s; n   for (int i = 1; i <= k; i++) { n     s.insert(i); n   } n   int mn = 1e9 + 1; n   for (int i = k + 1; i <= n; i++) { n     pair<int, int> u = query(s); n     mn = min(mn, u.second); n     s.erase(u.first); n     s.insert(i); n   } n   pair<int, int> u = query(s); n   mn = min(mn, u.second); n   s.erase(u.first); n   int ans = 1; n   set<int> t; n   int sm = 1e9 + 1, bg = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] != 0) { n       t.insert(i); n       sm = min(sm, a[i]); n       bg = max(bg, a[i]); n     } n     if (t.size() == 2) break; n   } n   set<int> ss; n   for (auto e : s) ss.insert(e); n   for (auto e : t) ss.insert(e); n   for (auto e : s) { n     ss.erase(e); n     auto u = query(ss); n     if (u.second == bg) ans++; n     ss.insert(e); n   } n   cout <<  !   << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int main() { n   string s; n   cin >> s; n   string a =   ; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  a  || s[i] ==  b ) a += s[i]; n   } n   int ans = 1, cnt = 0; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] ==  b ) { n       ans = (ans * 1ll * (cnt + 1)) % MOD; n       cnt = 0; n     } else { n       cnt++; n     } n   } n   ans = (ans * 1ll * (cnt + 1)) % MOD; n   printf( %d n , ans - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long ans = 0; n int r = -1; n int n, g[40][21], p[40][21]; n char ch; n void update(unsigned long long &a, int &b, int &c, unsigned long long d, n             int e) { n   if (b == 0 || a < d) { n     a = d; n     c = e; n     b = 1; n   } n } n unsigned long long f[40][21], bas[21], a[1010]; n int main() { n   bas[1] = 1; n   for (int i = 2; i <= 19; i++) bas[i] = bas[i - 1] * 10LL; n   scanf( %d , &n); n   for (int i = 1; i <= n + n; i++) { n     scanf(  %c , &ch); n     a[i] = ch -  0 ; n   } n   g[0][0] = 1; n   f[0][0] = 0; n   for (int i = 1; i <= n + n; i++) n     for (int j = 0; j <= n; j++) n       if (g[i - 1][j]) { n         int d1 = j, d2 = i - 1 - j; n         d1 = n - d1, d2 = n - d2; n         if (d2 > 0) n           update(f[i][j], g[i][j], p[i][j], f[i - 1][j] + bas[d2] * a[i], j); n         if (j != n) n           update(f[i][j + 1], g[i][j + 1], p[i][j + 1], n                  f[i - 1][j] + bas[d1] * a[i], j); n       } n   int sx = n + n, sy = n; n   string a =   ; n   while (sx != 0) { n     int ty = p[sx][sy]; n     sx--; n     if (ty == sy) n       a +=  M ; n     else n       a +=  H ; n     sy = ty; n   } n   reverse(a.begin(), a.end()); n   cout << a << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct pony { n   long long s, m, r, t; n } ponys[100005]; n bool cmp(pony &x, pony &y) { return x.t < y.t; } n struct node { n   long long l, r, latest; n   long long *sm, *sr, *t; n } nodes[100005 << 4]; n void build(long long ord, long long l, long long r) { n   nodes[ord].latest = 0; n   nodes[ord].l = l, nodes[ord].r = r; n   if (l == r) { n     nodes[ord].sm = &ponys[l].m; n     nodes[ord].sr = &ponys[l].r; n     nodes[ord].t = &ponys[l].t; n     return; n   } n   int m = (l + r) >> 1; n   build(ord << 1, l, m); n   build(ord << 1 | 1, m + 1, r); n   pony *tmp = new pony[r - l + 1]; n   for (int i = 0; i < r - l + 1; i++) tmp[i] = ponys[l + i]; n   sort(tmp, tmp + r - l + 1, cmp); n   nodes[ord].sm = new long long[r - l + 2]; n   nodes[ord].sr = new long long[r - l + 2]; n   nodes[ord].t = new long long[r - l + 2]; n   nodes[ord].sm[0] = nodes[ord].sr[0] = nodes[ord].t[0] = 0; n   for (int i = 1; i < r - l + 2; i++) { n     nodes[ord].sm[i] = tmp[i - 1].m + nodes[ord].sm[i - 1]; n     nodes[ord].sr[i] = tmp[i - 1].r + nodes[ord].sr[i - 1]; n     nodes[ord].t[i] = tmp[i - 1].t; n   } n   delete[] tmp; n } n long long mana_obserbed(long long ord, long long t, long long l, long long r) { n   long long res = 0; n   if (nodes[ord].latest > 0) n     nodes[ord << 1].latest = nodes[ord << 1 | 1].latest = nodes[ord].latest; n   if (nodes[ord].l == l && nodes[ord].r == r) { n     if (nodes[ord].latest > 0) { n       if (l == r) n         res = min(ponys[l].m, ponys[l].r * (t - nodes[ord].latest)); n       else { n         int len = nodes[ord].r - nodes[ord].l + 2; n         int tmp = upper_bound(nodes[ord].t + 1, nodes[ord].t + len, n                               t - nodes[ord].latest) - n                   nodes[ord].t; n         res = nodes[ord].sm[tmp - 1] + n               (nodes[ord].sr[len - 1] - nodes[ord].sr[tmp - 1]) * n                   (t - nodes[ord].latest); n       } n       nodes[ord].latest = t; n       return res; n     } else if (nodes[ord].latest == 0 && l == r) { n       res = min(ponys[l].m, ponys[l].s + ponys[l].r * t); n       nodes[ord].latest = t; n       return res; n     } n   } n   int m = (nodes[ord].l + nodes[ord].r) >> 1; n   if (r <= m) n     res += mana_obserbed(ord << 1, t, l, r); n   else if (m < l) n     res += mana_obserbed(ord << 1 | 1, t, l, r); n   else n     res += mana_obserbed(ord << 1, t, l, m) + n            mana_obserbed(ord << 1 | 1, t, m + 1, r); n   nodes[ord].latest = nodes[ord << 1].latest == nodes[ord << 1 | 1].latest n                           ? nodes[ord << 1].latest n                           : -1; n   return res; n } n int main() { n   long long n, m, t, l, r; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> ponys[i].s >> ponys[i].m >> ponys[i].r; n     if (ponys[i].r) n       ponys[i].t = (ponys[i].m + ponys[i].r - 1) / ponys[i].r; n     else n       ponys[i].t = 0x7fffffffffffffff; n   } n   build(1, 1, n); n   cin >> m; n   for (long long i = 1; i <= m; i++) { n     cin >> t >> l >> r; n     cout << mana_obserbed(1, t, l, r) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef int* Pint; n typedef long long* PLL; n const int MAXN = 1E4; n int a[110], f[110][3]{}; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) { n     f[i][0] = min(min(f[i - 1][0], f[i - 1][1]), f[i - 1][2]) + 1; n     f[i][1] = f[i][2] = MAXN; n     if (a[i] & 1) f[i][1] = min(f[i - 1][0], f[i - 1][2]); n     if (a[i] & 2) f[i][2] = min(f[i - 1][0], f[i - 1][1]); n   } n   printf( %d n , min(min(f[n][0], f[n][1]), f[n][2])); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using std::bitset; n using namespace std; n struct point { n   long long x, y; n   bool operator<(const point &I) const { n     return x < I.x || (x == I.x && y < I.y); n   } n } a[101000]; n long long n; n long long hmi[101000], tmi[101000], hma[101000], tma[101000]; n long long judge(double r) { n   long long i, j, tem; n   long long mmi, mma; n   j = 1; n   for (i = 1; i <= n; i++) { n     while (j <= n && a[j].x - a[i].x <= 2 * r) j++; n     mmi = min(hmi[i - 1], tmi[j]); n     mma = max(hma[i - 1], tma[j]); n     if (mma - mmi <= 2 * r) return 1; n   } n   return 0; n } n int main() { n   long long i, j, tem; n   long long tx, ty; n   while (scanf( %I64d , &n) != EOF) { n     for (i = 1; i <= n; i++) { n       scanf( %I64d%I64d , &tx, &ty); n       a[i].x = tx - ty; n       a[i].y = tx + ty; n     } n     sort(a + 1, a + 1 + n); n     hmi[0] = 0x3f3f3f3f3f3f3f3fll; n     hma[0] = -0x3f3f3f3f3f3f3f3fll; n     for (i = 1; i <= n; i++) { n       hmi[i] = min(hmi[i - 1], a[i].y); n       hma[i] = max(hma[i - 1], a[i].y); n     } n     tmi[n + 1] = 0x3f3f3f3f3f3f3f3fll; n     tma[n + 1] = -0x3f3f3f3f3f3f3f3fll; n     for (i = n; i > 0; i--) { n       tmi[i] = min(tmi[i + 1], a[i].y); n       tma[i] = max(tma[i + 1], a[i].y); n     } n     double low, high, mid, ans = 0; n     low = 0; n     high = 1e20; n     while (high - low > 1e-6) { n       mid = (low + high) / 2; n       if (judge(mid)) n         high = mid; n       else n         low = mid; n     } n     printf( %.6lf n , (low + high) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.1415926535897932384626433832795; n const int inf = 1e9; n const long long inf2 = 1e18; n const int mod = 1e9 + 7; n void boost() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n inline long long exp(long long x, long long n, long long m = mod) { n   long long r = 1; n   while (n) { n     if (n & 1) r = r * x % m; n     x = x * x % m, n >>= 1; n   } n   return r; n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> ncr(n + 1, 0); n   vector<int> arr(n + 1); n   for (int i = 1; i < n + 1; ++i) cin >> arr[i]; n   ncr[0] = 1; n   for (int i = 1; i < n + 1; ++i) { n     ncr[i] = (ncr[i - 1] * 1LL * (k + i - 1)) % mod; n     ncr[i] = (ncr[i] * exp(i, mod - 2)) % mod; n   } n   vector<int> ans(n + 1, 0); n   for (int i = 1; i < n + 1; ++i) { n     for (int j = i; j < n + 1; ++j) { n       ans[j] = (ans[j] * 1LL + 1LL * arr[i] * ncr[j - i]) % mod; n     } n   } n   for (int i = 1; i < n + 1; ++i) cout << ans[i] <<    ; n   return; n } n int main() { n   boost(); n   int tc = 1; n   while (tc--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a1[3], a2[3], i; n   cin >> a1[0] >> a1[1] >> a1[2]; n   cin >> a2[0] >> a2[1] >> a2[2]; n   for (i = 0; i < 3; i++) { n     if (a1[i] == a2[i]) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, k, cal; n string solve() { n   for (int i = 0; i < s.size(); i++) { n     int m4x = min(s[i] + k, (int) z ); n     int m1n = max(s[i] - k, (int) a ); n     if (s[i] - m1n >= m4x - s[i]) { n       k -= s[i] - m1n; n       s[i] = m1n; n     } else { n       k -= m4x - s[i]; n       s[i] = m4x; n     } n   } n   if (k == 0) return s; n   return  -1 ; n } n int main() { n   cin >> n >> k >> s; n   cout << solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <class Flow = long long, class Cost = long long> n struct MinCostFlow { n   const Flow INF_FLOW = 1000111000; n   const Cost INF_COST = 1000111000111000LL; n   long long n, t, S, T; n   Flow totalFlow; n   Cost totalCost; n   vector<long long> last, visited; n   vector<Cost> dis; n   struct Edge { n     long long to; n     Flow cap; n     Cost cost; n     long long ackjalscjaowjico; n     Edge(long long to, Flow cap, Cost cost, long long ackjalscjaowjico) n         : to(to), cap(cap), cost(cost), ackjalscjaowjico(ackjalscjaowjico) {} n   }; n   vector<Edge> edges; n   MinCostFlow(long long n) n       : n(n), n         t(0), n         totalFlow(0), n         totalCost(0), n         last(n, -1), n         visited(n, 0), n         dis(n, 0) { n     edges.clear(); n   } n   long long addEdge(long long from, long long to, Flow cap, Cost cost) { n     edges.push_back(Edge(to, cap, cost, last[from])); n     last[from] = t++; n     edges.push_back(Edge(from, 0, -cost, last[to])); n     last[to] = t++; n     return t - 2; n   } n   pair<Flow, Cost> minCostFlow(long long _S, long long _T) { n     S = _S; n     T = _T; n     SPFA(); n     while (1) { n       while (1) { n         for (long long i = 0, _i = (n); i < _i; ++i) visited[i] = 0; n         if (!findFlow(S, INF_FLOW)) break; n       } n       if (!modifyLabel()) break; n     } n     return make_pair(totalFlow, totalCost); n   } n  n  private: n   void SPFA() { n     for (long long i = 0, _i = (n); i < _i; ++i) dis[i] = INF_COST; n     priority_queue<pair<Cost, long long> > Q; n     Q.push(make_pair(dis[S] = 0, S)); n     while (!Q.empty()) { n       long long x = Q.top().second; n       Cost d = -Q.top().first; n       Q.pop(); n       if (dis[x] != d) continue; n       for (long long it = last[x]; it >= 0; it = edges[it].ackjalscjaowjico) n         if (edges[it].cap > 0 && dis[edges[it].to] > d + edges[it].cost) n           Q.push(make_pair(-(dis[edges[it].to] = d + edges[it].cost), n                            edges[it].to)); n     } n     Cost disT = dis[T]; n     for (long long i = 0, _i = (n); i < _i; ++i) dis[i] = disT - dis[i]; n   } n   Flow findFlow(long long x, Flow flow) { n     if (x == T) { n       totalCost += dis[S] * flow; n       totalFlow += flow; n       return flow; n     } n     visited[x] = 1; n     Flow now = flow; n     for (long long it = last[x]; it >= 0; it = edges[it].ackjalscjaowjico) n       if (edges[it].cap && !visited[edges[it].to] && n           dis[edges[it].to] + edges[it].cost == dis[x]) { n         Flow tmp = findFlow(edges[it].to, min(now, edges[it].cap)); n         edges[it].cap -= tmp; n         edges[it ^ 1].cap += tmp; n         now -= tmp; n         if (!now) break; n       } n     return flow - now; n   } n   bool modifyLabel() { n     Cost d = INF_COST; n     for (long long i = 0, _i = (n); i < _i; ++i) n       if (visited[i]) n         for (long long it = last[i]; it >= 0; it = edges[it].ackjalscjaowjico) n           if (edges[it].cap && !visited[edges[it].to]) n             d = min(d, dis[edges[it].to] + edges[it].cost - dis[i]); n     if (d == INF_COST) return false; n     for (long long i = 0, _i = (n); i < _i; ++i) n       if (visited[i]) dis[i] += d; n     return true; n   } n }; n signed main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   s =     + s; n   MinCostFlow<long long, long long> mcmf(n + 2); n   long long m; n   cin >> m; n   while (m--) { n     string t; n     long long cost; n     cin >> t >> cost; n     for (long long i = 1; i + t.size() <= s.size(); i++) { n       if (s.substr(i, t.size()) == t) mcmf.addEdge(i, i + t.size(), 1, -cost); n     } n   } n   long long x; n   cin >> x; n   for (long long i = 0; i <= n; i++) { n     mcmf.addEdge(i, i + 1, x, 0); n   } n   auto ans = mcmf.minCostFlow(0, n + 1); n   cout << -ans.second; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, x, y, curr, T, t, K, N, coun[51], fact[21], rev[51]; n vector<int> ans, v; n set<int> cand; n set<int>::iterator it; n void precomp() { n   fact[0] = 1; n   for (i = 1; i < 21; i++) { n     fact[i] = fact[i - 1] * i; n   } n   coun[0] = coun[1] = 1; n   for (i = 2; i < 23; i++) { n     coun[i] = fact[i - 2] + coun[i - 1]; n     for (j = 2; j < i; j++) { n       coun[i] += (fact[j - 2] * coun[i - j]); n     } n   } n   for (i = 23; i < 51; i++) coun[i] = coun[22]; n } n void fillv(long long n, long long k) { n   v.clear(); n   if (n < 3) { n     for (i = n; i > 0; i--) v.push_back(i); n     return; n   } n   cand.clear(); n   for (i = 1; i < n; i++) { n     cand.insert((int)i); n     rev[i] = 0; n   } n   v.push_back(n); n   rev[n] = 1; n   for (i = 1; i < (n - 1); i++) { n     for (j = i + 1; rev[j] > 0; j = rev[j]) n       ; n     x = cand.size(); n     for (it = cand.begin(), y = 0; y < (k / fact[x - 2]); y++, ++it) { n       if (j == (*it)) ++it; n     } n     if (j == (*it)) ++it; n     v.push_back(*it); n     rev[*it] = v.size(); n     cand.erase(it); n     k = k % fact[x - 2]; n   } n   v.push_back(*cand.begin()); n } n void fill(long long n, long long k) { n   int i; n   if (n == 0) return; n   if (coun[n - 1] > k) { n     ans.push_back(ans.size() + 1); n     fill(n - 1, k); n     return; n   } n   k -= coun[n - 1]; n   for (i = 2; i < n; i++) { n     j = fact[i - 2] * coun[n - i]; n     if (j <= k) { n       k -= j; n       continue; n     } n     break; n   } n   fillv(i, k / coun[n - i]); n   k = k % (coun[n - i]); n   n -= i; n   int x = ans.size(); n   for (j = 0; j < i; j++) { n     ans.push_back(x + v[j]); n   } n   fill(n, k); n } n int main() { n   precomp(); n   cin >> T; n   for (t = 0; t < T; t++) { n     cin >> N >> K; n     if (K > coun[N]) { n       cout <<  -1 n ; n       continue; n     } n     ans.clear(); n     fill(N, K - 1); n     for (i = 0; i < N; i++) { n       cout << ans[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   deque<int> dq; n   int n; n   cin >> n; n   int f = 0, a = 0, b = 0; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     dq.push_back(x); n   } n   while (!dq.empty()) { n     int ans = max(dq.front(), dq.back()); n     if (f == 0) { n       a += ans; n       f = 1; n       if (ans == dq.front()) n         dq.pop_front(); n       else n         dq.pop_back(); n     } else { n       b += ans; n       f = 0; n       if (ans == dq.front()) n         dq.pop_front(); n       else n         dq.pop_back(); n     } n   } n   cout << a <<     << b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100001; n int q, n; n int d[MAXN], p[MAXN]; n int main() { n   cin >> q; n   int small = 1 << 30; n   for (int i = 0; i < q; ++i) { n     cin >> d[i]; n     small = ((small) > (d[i]) ? (d[i]) : (small)); n   } n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n   } n   sort(p, p + n, greater<int>()); n   int cost = 0; n   for (int i = 0; i < n; i++) { n     if (i % (small + 2) < small) { n       cost += p[i]; n     } n   } n   cout << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n void file() {} n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   int x; n   cin >> x; n   int a[1003][3]; n   int ans = 0; n   for (int i = 0; i < (int)x; i++) n     for (int t = 0; t < (int)3; t++) { n       cin >> a[i][t]; n       if (t) a[i][t] += a[i][t - 1]; n       if (t == 2) ans += a[i][t] >= 2; n     } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x[1500][1500], y[1500][1500], vis[1510][1500]; n char ans[1500][1500]; n struct node { n   int x, y; n }; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n vector<node> a; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       scanf( %d%d , &x[i][j], &y[i][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (x[i][j] != -1 && !vis[x[i][j]][y[i][j]]) { n         if (x[x[i][j]][y[i][j]] == -1 || x[x[i][j]][y[i][j]] != x[i][j] || n             y[x[i][j]][y[i][j]] != y[i][j]) { n           printf( INVALID n ); n           return 0; n         } n         a.push_back(node{x[i][j], y[i][j]}); n         ans[x[i][j]][y[i][j]] =  X ; n         vis[x[i][j]][y[i][j]] = 1; n       } n     } n   } n   queue<node> q; n   for (int j = 0; j < a.size(); j++) { n     q.push(a[j]); n     while (!q.empty()) { n       node now = q.front(); n       q.pop(); n       for (int i = 0; i < 4; i++) { n         int xx = now.x + dx[i]; n         int yy = now.y + dy[i]; n         if (xx <= 0 || yy <= 0 || xx > n || yy > n || x[xx][yy] != a[j].x || n             y[xx][yy] != a[j].y) n           continue; n         if (!vis[xx][yy]) { n           vis[xx][yy] = 1; n           q.push(node{xx, yy}); n           if (i == 0) ans[xx][yy] =  L ; n           if (i == 1) ans[xx][yy] =  R ; n           if (i == 2) ans[xx][yy] =  U ; n           if (i == 3) ans[xx][yy] =  D ; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (x[i][j] != -1 && !vis[i][j]) { n         printf( INVALID n ); n         return 0; n       } n       if (x[i][j] == -1 && !vis[i][j]) { n         q.push(node{i, j}); n         vis[i][j] = 1; n         int flag = 0; n         while (!q.empty()) { n           node now = q.front(); n           q.pop(); n           for (int k = 0; k < 4; k++) { n             int xx = now.x + dx[k]; n             int yy = now.y + dy[k]; n             if (xx <= 0 || yy <= 0 || xx > n || yy > n || x[xx][yy] != -1) n               continue; n             if (x[xx][yy] == -1 && vis[xx][yy]) { n               if (!flag) { n                 if (k == 0) ans[i][j] =  R ; n                 if (k == 1) ans[i][j] =  L ; n                 if (k == 2) ans[i][j] =  D ; n                 if (k == 3) ans[i][j] =  U ; n                 flag = 1; n               } n             } n             if (!vis[xx][yy]) { n               vis[xx][yy] = 1; n               q.push(node{xx, yy}); n               if (k == 0) ans[xx][yy] =  L ; n               if (k == 1) ans[xx][yy] =  R ; n               if (k == 2) ans[xx][yy] =  U ; n               if (k == 3) ans[xx][yy] =  D ; n               if (!flag) { n                 if (k == 0) ans[i][j] =  R ; n                 if (k == 1) ans[i][j] =  L ; n                 if (k == 2) ans[i][j] =  D ; n                 if (k == 3) ans[i][j] =  U ; n                 flag = 1; n               } n             } n           } n           if (!flag) { n             printf( INVALID n ); n             return 0; n           } n         } n       } n     } n   } n   printf( VALID n ); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       printf( %c , ans[i][j]); n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int a[300000]; n int n, k; n int b[300000]; n int cnt = 0; n int cmp(int a, int b) { return mp[a] > mp[b]; } n bool check(int mid) { n   int sum = 0; n   for (int i = 1; i <= cnt; i++) { n     sum += mp[b[i]] / mid; n   } n   return sum >= k; n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     if (!mp[a[i]]) b[++cnt] = a[i]; n     mp[a[i]]++; n   } n   int l = 1; n   int r = 250000; n   int ans; n   while (l <= r) { n     int mid = l + r >> 1; n     if (check(mid)) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   int nd = k; n   int now = 1; n   sort(b + 1, b + 1 + cnt); n   while (nd) { n     while (nd && mp[b[now]] >= ans) n       cout << b[now] <<    , mp[b[now]] -= ans, nd--; n     now++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int x, y, a, b; n     cin >> x >> y >> a >> b; n     if ((y - x) % (a + b) == 0) { n       cout << (y - x) / (a + b) <<   n ; n     } else { n       cout << -1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n long long a[100000], b[100000]; n using namespace std; n bool isnp(long long n, long long k, long long t) { n   long long magic = 0; n   for (long long i = 0; i < t; i++) { n     if (a[i] * n > b[i]) magic += a[i] * n - b[i]; n     if (magic > k) return false; n   } n   return true; n } n int32_t main() { n   long long n, k; n   cin >> n >> k; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n   } n   long long l = 0, r = 2000000001; n   while (r - l != 1) { n     if (isnp((r + l) / 2, k, n)) n       l = (r + l) / 2; n     else n       r = (r + l) / 2; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, K = 505, INF = K * (1e4 + 5); n int n, m, k; n int c[N], a[N], visit[N]; n int w[K][K]; n vector<pair<int, int>> edges[N]; n vector<int> colors; n void dfs(int s) { n   visit[s] = 1; n   colors.push_back(a[s]); n   for (auto x : edges[s]) { n     int nxt = x.first; n     int w = x.second; n     if (visit[nxt] || w) continue; n     dfs(nxt); n   } n } n vector<vector<int>> ffloyd(int M) { n   vector<vector<int>> dist(M, vector<int>(M, INF)); n   for (int i = 0; i < M; i++) n     for (int j = 0; j < M; j++) dist[i][j] = w[i][j]; n   for (int k = 0; k < M; k++) { n     for (int i = 0; i < M; i++) { n       for (int j = 0; j < M; j++) { n         if (dist[i][k] < INF && dist[k][j] < INF) n           dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n       } n     } n   } n   return dist; n } n void solve() { n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) cin >> c[i]; n   int col = 0; n   for (int i = 0, id = 0; i < k; i++) { n     for (int j = 0; j < c[i]; j++) a[id++] = col; n     col++; n   } n   for (int i = 0; i < k; i++) n     for (int j = 0; j < k; j++) n       if (i != j) w[i][j] = INF; n   while (m--) { n     int u, v, x; n     cin >> u >> v >> x; n     u--; n     v--; n     edges[u].push_back({v, x}); n     edges[v].push_back({u, x}); n     u = a[u], v = a[v]; n     if (u == v) continue; n     w[u][v] = min(w[u][v], x); n     w[v][u] = min(w[v][u], x); n   } n   int fail = 0; n   for (int i = 0; i < n; i++) { n     if (!visit[i]) { n       dfs(i); n       sort(colors.begin(), colors.end()); n       int count = 1; n       for (int i = 1; i < colors.size(); i++) { n         if (colors[i] == colors[i - 1]) { n           count++; n           continue; n         } n         if (c[colors[i - 1]] != count) { n           fail = 1; n           break; n         } n         count = 1; n       } n       if (c[colors.back()] != count) { n         fail = 1; n         break; n       } n       colors.clear(); n     } n   } n   if (fail) { n     cout <<  No n ; n     return; n   } n   cout <<  Yes n ; n   vector<vector<int>> dist = ffloyd(k); n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < k; j++) { n       if (dist[i][j] == INF) dist[i][j] = -1; n       cout << dist[i][j] <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char a[222][222]; n struct Dinic { n   struct edge { n     int to, flow, cap; n   }; n   const static int N = 202 * 202; n   vector<edge> e; n   vector<int> g[N + 7]; n   int dp[N + 7]; n   int ptr[N + 7]; n   void clear() { n     for (int i = 0; i < N + 7; i++) g[i].clear(); n     e.clear(); n   } n   void addEdge(int a, int b, int cap = 1) { n     g[a].push_back(e.size()); n     e.push_back({b, 0, cap}); n     g[b].push_back(e.size()); n     e.push_back({a, 0, 0}); n   } n   int minFlow, start, finish; n   bool bfs() { n     for (int i = 0; i < N; i++) dp[i] = -1; n     dp[start] = 0; n     vector<int> st; n     int uk = 0; n     st.push_back(start); n     while (uk < st.size()) { n       int v = st[uk++]; n       for (int to : g[v]) { n         auto ed = e[to]; n         if (ed.cap - ed.flow >= minFlow && dp[ed.to] == -1) { n           dp[ed.to] = dp[v] + 1; n           st.push_back(ed.to); n         } n       } n     } n     return dp[finish] != -1; n   } n   int dfs(int v, int flow) { n     if (v == finish) return flow; n     for (; ptr[v] < g[v].size(); ptr[v]++) { n       int to = g[v][ptr[v]]; n       edge ed = e[to]; n       if (ed.cap - ed.flow >= minFlow && dp[ed.to] == dp[v] + 1) { n         int add = dfs(ed.to, min(flow, ed.cap - ed.flow)); n         if (add) { n           e[to].flow += add; n           e[to ^ 1].flow -= add; n           return add; n         } n       } n     } n     return 0; n   } n   int dinic(int start, int finish) { n     Dinic::start = start; n     Dinic::finish = finish; n     int flow = 0; n     for (minFlow = 1; minFlow; minFlow >>= 1) { n       while (bfs()) { n         for (int i = 0; i < N; i++) ptr[i] = 0; n         while (int now = dfs(start, (int)2e9 + 7)) flow += now; n       } n     } n     return flow; n   } n } dinic; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> a[i][j]; n   int s = n * m; n   int t = n * m + 1; n   auto get_node = [](int first, int second) { return first * m + second; }; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) n       if (a[i][j] ==  # ) { n         if (i > 0 && a[i - 1][j] ==  # ) n           dinic.addEdge(get_node(i, j), get_node(i - 1, j)); n         if (j + 1 < m && a[i][j + 1] ==  # ) n           dinic.addEdge(get_node(i, j), get_node(i, j + 1)); n         if (j == 0 || a[i][j - 1] ==  . ) dinic.addEdge(s, get_node(i, j)); n         if (i == 0 || a[i - 1][j] ==  . ) dinic.addEdge(get_node(i, j), t); n       } n   } n   cout << dinic.dinic(s, t); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   std::ios::sync_with_stdio(false); n   cin >> n >> m; n   int x, y; n   for (int i = 0; i < m; i++) cin >> x >> y; n   for (int i = 0; i < n; i++) n     if (i % 2 == 0) n       cout << 0; n     else n       cout << 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int ans = 0; n   for (int i = 0; i < k; i++) { n     int m = 0; n     for (int j = i; j < n; j += k) { n       if (a[j] == 1) { n         m++; n       } n     } n     ans += min(m, (n / k - m)); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct abc { n   long long int y, x; n }; n bool compare(abc a1, abc a2) { n   if (a1.x == a2.x) return a1.y < a2.y; n   return a1.x < a2.x; n } n long long int countDivisors(long long int n) { n   long long int cnt = 0; n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         cnt++; n       else n         cnt = cnt + 2; n     } n   } n   return cnt; n } n const long long int mod = 1e9 + 7; n long long int n, m, x, y, l, r, ans, k, q, w, h; n string second, p; n map<long long int, long long int> mp; n void solve() { n   cin >> n >> k; n   long long int a[n], sum = 0; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     ans += a[i] / 10; n     long long int x = a[i] % 10; n     if (x % 10 != 0) n       x = 10 - x; n     else n       x = 0; n     if (x) v.push_back(x); n     a[i] += x; n     sum += (100 - a[i]); n   } n   sort(v.begin(), v.end()); n   for (long long int i = 0; i < v.size(); i++) { n     if (k >= v[i]) n       ans++, k -= v[i]; n     else { n       k = 0; n       break; n     } n   } n   ans += min(sum, k) / 10; n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   int flag = 0; n   int m; n   cin >> m; n   long long a[m]; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   sort(a, a + m); n   for (int i = 1; i < m - 1; i++) { n     if ((a[i] - a[i - 1] == 1) && (a[i + 1] - a[i] == 1)) { n       flag = 1; n       break; n     } n   } n   if (flag == 1 || a[m - 1] == n || a[0] == 1) n     cout <<  NO  n          <<   n ; n   else n     cout <<  YES  n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e18; n const int N = 3e5 + 7; n const int MOD = 1e9 + 7; n constexpr int mul(int a, int b) { return 1LL * a * b % MOD; } n template <typename... T> n constexpr int mul(int a, int b, T... t) { n   return mul(mul(a, b), t...); n } n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline long long int readl() { n   char ch = getchar(); n   long long int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long int a[N], b[N]; n long long int dp[N][3]; n int main() { n   int T = read(); n   while (T--) { n     int n = read(); n     for (int i = 1; i <= n; i++) { n       a[i] = readl(); n       b[i] = readl(); n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < 3; j++) { n         dp[i][j] = INF; n       } n     } n     dp[1][0] = 0; n     dp[1][1] = b[1]; n     dp[1][2] = b[1] * 2; n     for (int i = 2; i <= n; i++) { n       for (int j = 0; j < 3; j++) { n         for (int k = 0; k < 3; k++) { n           if (a[i] + j == a[i - 1] + k) { n             continue; n           } n           dp[i][j] = min(dp[i][j], dp[i - 1][k] + b[i] * j); n         } n       } n     } n     cout << min(dp[n][0], min(dp[n][1], dp[n][2])) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, *a, s, t, d1 = 0, d2 = 0; n   cin >> n; n   a = new int[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> s >> t; n   if (s < t) { n     int tmp = t; n     t = s; n     s = tmp; n   } n   for (int i = t - 1; i < s - 1; i++) d1 += a[i]; n   for (int i = s - 1; i < n; i++) d2 += a[i]; n   for (int i = 0; i < t - 1; i++) d2 += a[i]; n   if (d1 > d2) n     cout << d2; n   else n     cout << d1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prefix_function(const string &s) { n   int n = (int)s.size(); n   vector<int> p(n); n   p[0] = 0; n   for (int i = 1; i < n; i++) { n     p[i] = p[i - 1]; n     while (p[i] > 0 && s[p[i]] != s[i]) p[i] = p[p[i] - 1]; n     if (s[p[i]] == s[i]) p[i]++; n   } n   return p; n } n vector<int> Z_function(string s) { n   int n = (int)s.size(); n   vector<int> Z(n); n   int l = 0, r = 0; n   for (int i = 1; i < n; i++) { n     if (i <= r) Z[i] = min(Z[i - l], r - i + 1); n     while (i + Z[i] < n && s[i + Z[i]] == s[Z[i]]) Z[i]++; n     if (i + Z[i] >= r) { n       l = i; n       r = i + Z[i] - 1; n     } n   } n   return Z; n } n int main() { n   string a; n   getline(cin, a); n   string b; n   getline(cin, b); n   int n = (int)a.size(); n   if (n != (int)b.size()) { n     cout <<  -1 -1  << endl; n     return 0; n   } n   vector<int> Z = Z_function(b + char(10) + a); n   reverse(a.begin(), a.end()); n   vector<int> pi = prefix_function(a + char(10) + b); n   reverse(a.begin(), a.end()); n   int i = 0; n   int x = -1; n   int y = -1; n   while (i < n - 1) { n     if (a[i] != b[n - i - 1]) break; n     if (i + Z[n + 1 + i + 1] + pi[2 * n - i - 1] + 1 >= n) { n       x = i; n       y = n - pi[2 * n - i - 1]; n     }; n     i++; n   } n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int A = 1000001; n int x, p, k; n bool b[A]; n vector<int> v[A]; n int calc(int x) { n   int ret = 0; n   int n = v[p].size(); n   for (int i = 1; i < (1 << n); i++) { n     int pro = 1; n     for (int j = 0; j < n; j++) { n       if ((1 << j) & i) pro *= v[p][j]; n     } n     ret += x / pro * (__builtin_popcount(i) % 2 ? 1 : -1); n   } n   return x - ret; n } n int main() { n   for (int i = 2; i < A; i++) b[i] = 1; n   for (int i = 2; i < A; i++) { n     if (b[i] == 0) continue; n     v[i].push_back(i); n     for (int j = i + i; j < A; j += i) { n       v[j].push_back(i); n       b[j] = 0; n     } n   } n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d %d , &x, &p, &k); n     int c = calc(x); n     int l = 1, r = 1e9; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (calc(mid) - c >= k) n         r = mid - 1; n       else n         l = mid + 1; n     } n     printf( %d n , r + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MXN = 1000100; n const long long inf = 1e15; n const int mod = 1000000007; n char s[MXN]; n int was[2 * MXN]; n int main() { n   scanf( %s , s); n   int n = strlen(s); n   if (s[n - 1] ==  L ) { n     for (int i = 0; i < (n); i++) { n       if (s[i] ==  R ) n         s[i] =  L ; n       else n         s[i] =  R ; n     } n   } n   for (int i = 0; i < (n); i++) { n     if (s[i] ==  R ) n       s[i] = 1; n     else n       s[i] = -1; n   } n   int cur = 0; n   int mn = 0, mx = 0, cmx = 0, tmx = 0; n   for (int i = 0; i < (n); i++) { n     was[cur + n] = 1; n     cur += s[i]; n     tmx = max(tmx, cur); n     if (cur < mn) { n       mn = cur; n       mx = -1e9; n       cmx = 0; n     } n     if (cur > mx) { n       mx = cur; n       cmx = 1; n     } else if (cur == mx) { n       cmx++; n     } n   } n   if (!was[cur + n]) { n     printf( 1 n ); n     return 0; n   } n   if (cur == mx && cmx == 1) { n     if (cur - mn > tmx) { n       printf( %d n , cur - mn - tmx); n       return 0; n     } n   } n   printf( 0 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000 * 1000 * 1000 + 7; n const int INF = 1000 * 1000 * 1000; n const long long LINF = (long long)INF * INF; n struct Query { n   int l, r, ind, k; n }; n int n, m; n int c[100100]; n vector<int> adj[100100]; n int cnt[100100], bnd[100100]; n int ans[100100]; n vector<int> st; n int sttime[100100]; n int fntime[100100]; n Query qr[100100]; n void dfs(int v, int par) { n   st.push_back(v); n   sttime[v] = st.size() - 1; n   for (int u : adj[v]) { n     if (u == par) { n       continue; n     } n     dfs(u, v); n   } n   fntime[v] = st.size() - 1; n } n bool mos_cmp(Query &a, Query &b) { n   if (a.l / 400 == b.l / 400) { n     return (a.l / 400 % 2 ? a.r > b.r : a.r < b.r); n   } n   return a.l / 400 < b.l / 400; n } n void mos_add(int i) { n   cnt[c[i]]++; n   bnd[cnt[c[i]]]++; n } n void mos_rem(int i) { n   bnd[cnt[c[i]]]--; n   cnt[c[i]]--; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = (1); i < (n + 1); i++) { n     cin >> c[i]; n   } n   for (int i = (0); i < (n - 1); i++) { n     int ai, bi; n     cin >> ai >> bi; n     adj[ai].push_back(bi); n     adj[bi].push_back(ai); n   } n   dfs(1, -1); n   for (int i = (0); i < (m); i++) { n     int vi, ki; n     cin >> vi >> ki; n     qr[i].l = sttime[vi]; n     qr[i].r = fntime[vi]; n     qr[i].k = ki; n     qr[i].ind = i; n   } n   sort(qr, qr + m, mos_cmp); n   int l = 0, r = -1; n   for (int i = (0); i < (m); i++) { n     while (l > qr[i].l) { n       l--; n       mos_add(st[l]); n     } n     while (r < qr[i].r) { n       r++; n       mos_add(st[r]); n     } n     while (l < qr[i].l) { n       mos_rem(st[l]); n       l++; n     } n     while (r > qr[i].r) { n       mos_rem(st[r]); n       r--; n     } n     ans[qr[i].ind] = bnd[qr[i].k]; n   } n   for (int i = (0); i < (m); i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long st = 1, mod = 998244353; n   int n; n   cin >> n; n   vector<long long> ans(n + 1); n   ans[n] = 10; n   ans[n - 1] = 20; n   for (int i = n - 2; i >= 1; i--) { n     ans[i] = ans[i + 1] + 9; n   } n   for (int i = n - 1; i >= 1; i--) { n     ans[i] = ((ans[i] * 9) % mod * st) % mod; n     st = (10 * st) % mod; n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n const long long p = 998244353; n long long qp(long long x, long long y) { n   long long res = 1; n   for (long long t = x; y; y >>= 1, t = t * t % p) n     if (y & 1) res = res * t % p; n   return res; n } n struct node { n   long long u, v, w; n } a[1000003]; n vector<node> v[1000003]; n long long f[1000003], g[1000003]; n long long c[1000003]; n signed main() { n   long long n = read(), m = read(), k = read() + 1; n   for (long long i = 1; i <= m; ++i) { n     long long X = read(); n     a[i].u = read(); n     a[i].v = read(); n     a[i].w = read(); n     v[X].push_back(a[i]); n   } n   long long ans = 1e18; n   memset(f, -1, sizeof(f)), memset(g, -1, sizeof(g)); n   memset(c, -1, sizeof(c)); n   long long S = 0, C = 0; n   for (long long i = 1; i <= 1000000; ++i) { n     for (node j : v[i]) n       if (j.v == 0) { n         if (c[j.u] == -1) { n           S += j.w, ++C, c[j.u] = j.w; n         } else if (c[j.u] > j.w) { n           S -= c[j.u] - j.w, c[j.u] = j.w; n         } n       } n     if (C == n) f[i] = S; n   } n   S = 0, C = 0; n   memset(c, -1, sizeof(c)); n   for (long long i = 1000000; i >= 1; --i) { n     for (node j : v[i]) n       if (j.u == 0) { n         if (c[j.v] == -1) { n           S += j.w, ++C, c[j.v] = j.w; n         } else if (c[j.v] > j.w) { n           S -= c[j.v] - j.w, c[j.v] = j.w; n         } n       } n     if (C == n) g[i] = S; n   } n   for (long long i = 1; i + k <= 1000000; ++i) n     if (f[i] > -1 && g[i + k] > -1) { n       ans = min(ans, f[i] + g[i + k]); n     } n   if (ans > 1e17) n     puts( -1 ); n   else n     printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sc() {} n template <typename T, typename... Args> n void sc(T& a, Args&... args) { n   cin >> a, sc(args...); n } n template <typename T> n void pr(T a) { n   cout << a; n } n template <typename T, typename... Args> n void pr(T a, Args... args) { n   cout << a <<    , pr(args...); n } n template <typename T> n void prl(T a) { n   cout << a <<   n ; n } n template <typename T, typename... Args> n void prl(T a, Args... args) { n   cout << a <<    , prl(args...); n } n template <typename T> n int sz(const T& a) { n   return (int)a.size(); n } n int dp[81][200003]; n struct comp { n   bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) const { n     return lhs.first + lhs.second <= rhs.first + rhs.second; n   } n }; n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n   int n, m; n   sc(n, m); n   pair<int, int> aten[n + 1]; n   aten[0] = {0, 0}; n   for (__typeof(2 * (m + 1)) i = (1) - ((1) > (2 * (m + 1))); n        i != (2 * (m + 1)) - ((1) > (2 * (m + 1))); n        i += 1 - 2 * ((1) > (2 * (m + 1)))) n     dp[0][i] = m + 1; n   for (__typeof(n + 1) i = (1) - ((1) > (n + 1)); n        i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) { n     sc(aten[i].first, aten[i].second); n   } n   sort(aten + 1, aten + n + 1, comp()); n   for (__typeof(n + 1) i = (1) - ((1) > (n + 1)); n        i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) { n     dp[i][2 * (m + 1)] = m + 1; n     for (__typeof(1) j = (2 * (m + 1)) - ((2 * (m + 1)) > (1)); n          j != (1) - ((2 * (m + 1)) > (1)); j += 1 - 2 * ((2 * (m + 1)) > (1))) { n       int addneeded = 0; n       if (j >= aten[i].first) { n         addneeded = max(0, j - aten[i].first - aten[i].second); n       } else n         addneeded = max(0, aten[i].first - aten[i].second - j); n       dp[i][j] = min( n           dp[i][j + 1], n           min(dp[i - 1][j], n               addneeded + (aten[i].first - aten[i].second - addneeded - 1 >= 0 n                                ? dp[i - 1][aten[i].first - aten[i].second - n                                            addneeded - 1] n                                : 0))); n     } n   } n   prl(dp[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z = 0, k, t = 0, e, g; n   vector<int> zero; n   vector<int> po; n   vector<int> ne; n   cin >> x; n   g = x; n   while (x--) { n     cin >> y; n     if (y < 0) { n       ne.push_back(y); n       k *= y; n     } n     if (y > 0) { n       po.push_back(y); n     } n     if (y == 0) { n       zero.push_back(y); n     } n     if (y < 0 || y == 0) t++; n   } n   if (t == g) { n     for (int i = 0; i < 2; i++) { n       e = ne.back(); n       po.push_back(e); n       ne.pop_back(); n     } n   } n   k = ne.size(); n   if (k % 2 == 0) { n     e = ne.back(); n     zero.push_back(e); n     ne.pop_back(); n   } n   cout << ne.size() <<    ; n   for (int i = 0; i < ne.size(); i++) cout << ne[i] <<    ; n   cout << endl; n   cout << po.size() <<    ; n   for (int i = 0; i < po.size(); i++) cout << po[i] <<    ; n   cout << endl; n   cout << zero.size() <<    ; n   for (int i = 0; i < zero.size(); i++) cout << zero[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9 + 9; n const long long inf = 1e18 + 18; n const int max6 = 1e6 + 6; n const int modx = 1e9 + 123; n const int mody = 997; n const int base = 137; n int a[max6], dd[max6], cnt[max6]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     for (int i = 0; i <= 2 * k; ++i) dd[i] = cnt[i] = 0; n     for (int i = 1; i <= n; ++i) cin >> a[i]; n     int m = n / 2; n     for (int i = 1; i <= m; ++i) { n       int gmax = max(a[i], a[n - i + 1]) + k; n       int gmin = min(a[i], a[n - i + 1]) + 1; n       dd[gmin]++; n       dd[gmax + 1]--; n       cnt[a[i] + a[n - i + 1]]++; n     } n     int res = oo; n     dd[1] += dd[0]; n     for (int i = 2; i <= 2 * k; ++i) { n       dd[i] += dd[i - 1]; n       res = min(res, n - dd[i] - cnt[i]); n     } n     cout << res <<   n ; n   } n } n