#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const int N = 2e5 + 10; n const long long mod = 1e9 + 7; n const long long mod2 = 998244353; n const long long inf = 8e18; n const int LOG = 22; n long long pw(long long a, long long b, long long M) { n   return (!b ? 1 n              : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M n                       : pw(a * a % M, b / 2, M))); n } n int v[N], dp[N], cnt[N], cu[N], Fi[N], kol[N], L[N], A[N]; n void solve() { n   map<int, int> mp; n   int n; n   cin >> n; n   A[0] = -1; n   for (int i = 0; i <= n + 1; i++) { n     if (i && i <= n) { n       cin >> v[i]; n       A[i] = v[i]; n     } n     dp[i] = cnt[i] = cu[i] = Fi[i] = L[i] = kol[i] = 0; n   } n   sort(A + 1, A + n + 1); n   int ptr = 0; n   for (int i = 1; i <= n; i++) { n     if (A[i] != A[i - 1]) { n       ptr++; n     } n     mp[A[i]] = ptr; n   } n   for (int i = n; ~(i - 1); i--) { n     v[i] = mp[v[i]]; n     Fi[v[i]] = i; n     cnt[v[i]]++; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     dp[i] = max(dp[L[v[i]]], cu[v[i] - 1]) + 1; n     if (cnt[v[i] - 1] == cu[v[i] - 1]) { n       dp[i] = max(dp[i], kol[v[i] - 1] + 1); n     } n     ans = max(ans, dp[i]); n     L[v[i]] = i; n     ++cu[v[i]]; n     if (cu[v[i]] == cnt[v[i]]) { n       kol[v[i]] = dp[Fi[v[i]]] + cnt[v[i]] - 1; n     } n   } n   cout << n - ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int q; n   cin >> q; n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mark[200002]; n vector<int> pos[11]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int i, l, t, j, cnt, mx, last; n   bool flag; n   string s; n   cin >> t; n   while (t--) { n     cin >> l >> s; n     for (i = 0; i < 10; i++) { n       pos[i].clear(); n     } n     mx = -1; n     for (i = 0; i < l; i++) { n       pos[s[i] -  0 ].push_back(i); n       mx = max(mx, (s[i] -  0 )); n       mark[i] = 0; n     } n     cnt = 0; n     for (i = pos[mx].size() - 1; i >= 0; i--) { n       mark[pos[mx][i]] = 2; n       ++cnt; n       last = pos[mx][i]; n     } n     flag = true; n     for (i = mx - 1; i >= 0 && flag; i--) { n       if (pos[i].size() > 0) { n         for (j = pos[i].size() - 1; j >= 0; j--) { n           if (last > pos[i][j]) { n             mark[pos[i][j]] = 2; n             last = pos[i][j]; n             ++cnt; n           } else { n             flag = false; n           } n         } n       } n     } n     last = l; n     for (i = mx - 1; i >= 0; i--) { n       if (pos[i].size() > 0) { n         for (j = pos[i].size() - 1; j >= 0; j--) { n           if (mark[pos[i][j]] == 0 && last > pos[i][j]) { n             mark[pos[i][j]] = 1; n             last = pos[i][j]; n             ++cnt; n           } n         } n       } n     } n     if (cnt == l) { n       for (i = 0; i < l; i++) { n         cout << mark[i]; n       } n       cout <<   n ; n     } else { n       cout <<  - n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int testCases = 1; n   cin >> testCases; n   while (testCases--) { n     long long int n, m; n     cin >> n >> m; n     vector<string> array(n); n     vector<pair<long long int, long long int>> sol; n     for (int i = 0; i < n; ++i) cin >> array[i]; n     for (int i = 0; i < n - 1; ++i) { n       for (int j = 0; j < m - 1; ++j) { n         if (array[i][j] ==  0  && array[i][j + 1] ==  0 ) continue; n         if (array[i][j] ==  1  && array[i][j + 1] ==  1 ) { n           sol.push_back({i + 1, j + 1}); n           sol.push_back({i + 1, j + 2}); n           sol.push_back({i + 2, j + 1}); n           array[i][j] =  0 ; n           array[i][j + 1] =  0 ; n           if (array[i + 1][j] ==  0 ) n             array[i + 1][j] =  1 ; n           else n             array[i + 1][j] =  0 ; n         } else if (array[i][j] ==  0  && array[i][j + 1] ==  1 ) { n           sol.push_back({i + 1, j + 2}); n           sol.push_back({i + 2, j + 1}); n           sol.push_back({i + 2, j + 2}); n           array[i][j + 1] =  0 ; n           if (array[i + 1][j] ==  0 ) n             array[i + 1][j] =  1 ; n           else n             array[i + 1][j] =  0 ; n           if (array[i + 1][j + 1] ==  0 ) n             array[i + 1][j + 1] =  1 ; n           else n             array[i + 1][j + 1] =  0 ; n         } else { n           sol.push_back({i + 1, j + 1}); n           sol.push_back({i + 2, j + 1}); n           sol.push_back({i + 2, j + 2}); n           array[i][j] =  0 ; n           if (array[i + 1][j] ==  0 ) n             array[i + 1][j] =  1 ; n           else n             array[i + 1][j] =  0 ; n           if (array[i + 1][j + 1] ==  0 ) n             array[i + 1][j + 1] =  1 ; n           else n             array[i + 1][j + 1] =  0 ; n         } n       } n     } n     for (int j = 0; j < m - 1; ++j) { n       if (array[n - 1][j] ==  0  && array[n - 1][j + 1] ==  0 ) continue; n       if (array[n - 1][j] ==  0  && array[n - 1][j + 1] ==  1 ) { n         sol.push_back({n - 1, j + 1}); n         sol.push_back({n - 1, j + 2}); n         sol.push_back({n, j + 2}); n         sol.push_back({n - 1, j + 2}); n         sol.push_back({n, j + 1}); n         sol.push_back({n, j + 2}); n         sol.push_back({n - 1, j + 1}); n         sol.push_back({n, j + 1}); n         sol.push_back({n, j + 2}); n         array[n - 1][j + 1] =  0 ; n       } else if (array[n - 1][j] ==  1  && array[n - 1][j + 1] ==  0 ) { n         sol.push_back({n - 1, j + 1}); n         sol.push_back({n - 1, j + 2}); n         sol.push_back({n, j + 1}); n         sol.push_back({n - 1, j + 2}); n         sol.push_back({n, j + 1}); n         sol.push_back({n, j + 2}); n         sol.push_back({n - 1, j + 1}); n         sol.push_back({n, j + 1}); n         sol.push_back({n, j + 2}); n         array[n - 1][j] =  0 ; n       } else { n         sol.push_back({n - 1, j + 1}); n         sol.push_back({n - 1, j + 2}); n         sol.push_back({n, j + 1}); n         sol.push_back({n - 1, j + 1}); n         sol.push_back({n - 1, j + 2}); n         sol.push_back({n, j + 2}); n         array[n - 1][j] = array[n - 1][j + 1] =  0 ; n       } n     } n     cout << sol.size() / 3 << endl; n     for (int i = 0; i < sol.size(); i += 3) { n       cout << sol[i].first <<     << sol[i].second <<    ; n       cout << sol[i + 1].first <<     << sol[i + 1].second <<    ; n       cout << sol[i + 2].first <<     << sol[i + 2].second << endl; n       ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct M { n   int x, y, z; n }; n int dis(int x, int y) { return abs(x - y); } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   int n; n   cin >> n; n   M note[5000]; n   vector<bool> vs(5000, 0); n   for (int i = 1; i <= n; i++) { n     cin >> note[i].x >> note[i].y >> note[i].z; n   } n   for (int i = 1; i <= n; i++) { n     int X = INT_MAX, Y = INT_MAX, Z = INT_MAX, now; n     if (!vs[i]) { n       for (int j = 1; j <= n; j++) { n         if (i != j && !vs[j]) { n           int disX = dis(note[i].x, note[j].x); n           int disY = dis(note[i].y, note[j].y); n           int disZ = dis(note[i].z, note[j].z); n           if (disX < X) { n             now = j; n           } else if (disX == X) { n             if (disY < Y) n               now = j; n             else if (disY == Y) { n               if (disZ < Z) now = j; n             } n           } n           if (now == j) { n             X = disX; n             Y = disY; n             Z = disZ; n           } n         } n       } n       cout << i <<     << now << endl; n       vs[i] = 1; n       vs[now] = 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 50; n struct node { n   int v, c, l, r, id; n   void read() { scanf( %d%d%d%d , &v, &c, &l, &r); } n } car[N]; n map<int, int> mp, MP; n inline int clr(int x) { return car[x].c + car[x].l + car[x].r; } n bool cmp(node a, node b) { n   int ta = a.c + a.l + a.r; n   int tb = b.c + b.l + b.r; n   if (ta == tb) n     return a.id < b.id; n   else n     return ta < tb; n } n int n, f[N], pre[N], c[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) car[i].read(), car[i].id = i; n   sort(car + 1, car + n + 1, cmp); n   int ans = 0, num = 0; n   for (int i = 1; i <= n;) { n     int j = i; n     while (j <= n && clr(j) == clr(i)) ++j; n     mp.clear(); n     MP.clear(); n     mp[0] = 0; n     MP[0] = 0; n     for (int k = i; k <= j - 1; k++) { n       if (mp.count(car[k].l)) { n         f[k] = mp[car[k].l] + car[k].v; n         pre[k] = MP[car[k].l]; n       } n       if (car[k].r == 0 && f[k] > ans) { n         ans = f[k]; n         num = k; n       } n       int tmp = car[k].c + car[k].l; n       if (!mp.count(tmp) || mp[tmp] < f[k]) { n         mp[tmp] = f[k]; n         MP[tmp] = k; n       } n     } n     i = j; n   } n   int tmp = num; n   ans = 0; n   while (tmp) c[++ans] = car[tmp].id, tmp = pre[tmp]; n   printf( %d n , ans); n   for (int i = ans; i >= 1; i--) printf( %d%c , c[i], i == 1 ?   n  :    ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T bpow(T p, T e) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p); n     p = (p * p); n   } n   return (T)ret; n } n int toInt(string s) { n   int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int toLlint(string s) { n   long long int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n const double eps = 1e-9; n int ts, kk = 1; n int n, b[500005]; n pair<int, int> a[500005]; n int lt[500005], rt[500005]; n int main() { n   int t, i, j, k; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &k); n     b[i] = k; n     a[i] = pair<int, int>(k, i); n     lt[i] = i - 1; n     rt[i] = i + 1; n   } n   sort(a, a + n); n   int x, y; n   long long int rs = 0; n   int lo, hi; n   lo = 0; n   hi = n - 1; n   for (i = 0; i < n - 2; i++) { n     k = a[i].second; n     if (k == lo) { n       rs += a[i].first; n       lo = rt[k]; n       continue; n     } n     if (k == hi) { n       rs += a[i].first; n       hi = lt[k]; n       continue; n     } n     x = lt[k]; n     y = rt[k]; n     rs += min(b[x], b[y]); n     rt[x] = y; n     lt[y] = x; n   } n   cout << rs <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const int maxn = (1 << 11) + 100; n int dp[maxn][maxn]; n vector<int> g[15]; n int n, m, k, u, v, mx, cc[maxn]; n void init() { n   for (int i = 0; i < maxn; ++i) cc[i] = __builtin_popcount(i); n } n int main() { n   init(); n   cin >> n >> m >> k; n   for (int i = 0; i < m; ++i) { n     cin >> u >> v; n     u--; n     v--; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   mx = (1 << n) - 1; n   for (int i = 1; i <= mx; i <<= 1) dp[i][i] = 1; n   for (int i = 1; i <= mx; ++i) { n     for (int j = i; j; --j &= i) { n       if (dp[i][j]) { n         for (int k = 0; k < n; ++k) { n           if (i & (1 << k)) { n             for (int u : g[k]) { n               if (~i & (1 << u)) { n                 if (cc[i] == 1) n                   v = i | (1 << u); n                 else n                   v = j & ~(1 << k) | (1 << u); n                 if (!(v >> u + 1)) dp[i | (1 << u)][v] += dp[i][j]; n               } n             } n           } n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i < mx; ++i) n     if (cc[i] == k) ans += dp[mx][i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100001; n const int MOD = 1e9 + 7; n mt19937 rnd(100); n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, x; n   vector<vector<int> > a; n   cin >> n >> m; n   a.assign(n, vector<int>(m)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> x; n       a[i][j] = (int)(x != a[i][j]); n     } n   for (int i = 0; i < n; i++) { n     int sum = 0; n     for (int j = 0; j < m; j++) sum += a[i][j]; n     if (sum % 2) { n       cout <<  No ; n       return 0; n     } n   } n   for (int i = 0; i < m; i++) { n     int sum = 0; n     for (int j = 0; j < n; j++) sum += a[j][i]; n     if (sum % 2) { n       cout <<  No ; n       return 0; n     } n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std;  n  n typedef long long s64;  n  n const int MaxN = 100000 + 5;  n  n int a[MaxN];  n  n void work() { n  int n;  n  scanf( %d , &n);  n  for (int i = 1; i <= n; ++i) { n   scanf( %d , a + i);  n  } n  n  sort(a + 1, a + n + 1);  n  n  s64 ans = a[n];  n  for (int i = 2; i <= n; ++i) n   ans -= 1LL * a[i] * ((i - 1) - (n - i));  n   n  printf( %lld n , ans);  n } n  n int main() { n  int T;  n  scanf( %d , &T);  n  n  while (T--) work();  n  n  return 0;  n }


#include <bits/stdc++.h> n using namespace std; n int edge[300005][5]; n int num[300005], res[300005]; n void dfs(int x) { n   int tmp = 0; n   for (int i = 0; i < num[x]; i++) n     if (res[x] == res[edge[x][i]]) tmp++; n   if (tmp > 1) { n     res[x] ^= 1; n     for (int i = 0; i < num[x]; i++) n       if (res[x] == res[edge[x][i]]) dfs(edge[x][i]); n   } n } n int main(int argc, char* argv[]) { n   std::ios::sync_with_stdio(false); n   int n, m, a, b; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     edge[a][num[a]++] = b; n     edge[b][num[b]++] = a; n   } n   for (int i = 1; i < n + 1; i++) dfs(i); n   for (int i = 1; i < n + 1; i++) cout << res[i]; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n), b(n), pa(n), pb(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     --a[i]; n     pa[a[i]] = i; n   } n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n     --b[i]; n     pb[b[i]] = i; n   } n   vector<int> ans(n, 999999); n   vector<vector<int>> evi(n), evo(n); n   multiset<int> segs; n   for (int i = 0; i < n; ++i) { n     int wh = (pb[i] - pa[i] + n) % n; n     int tol = n - pa[i], tor = pa[i]; n     evi[wh].push_back(wh); n     if ((wh + tor) % n < wh) { n       segs.insert(wh - n); n       evo[(wh + tor) % n].push_back(wh - n); n     } else { n       evo[(wh + tor) % n].push_back(wh); n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int z : evi[i]) { n       segs.insert(z); n     } n     if (segs.size()) { n       auto it = segs.end(); n       --it; n       int x = *it; n       ans[i] = min(ans[i], (i - x) % n); n     } n     for (int z : evo[i]) { n       segs.erase(segs.find(z)); n     } n   } n   segs.clear(); n   evi.assign(n, vector<int>()); n   evo.assign(n, vector<int>()); n   for (int i = 0; i < n; ++i) { n     int wh = (pb[i] - pa[i] + n) % n; n     int tol = n - pa[i] - 1, tor = pa[i]; n     evi[wh].push_back(wh); n     if ((wh - tol + n) % n > wh) { n       segs.insert(wh + n); n       evo[(wh - tol + n) % n].push_back(wh + n); n     } else { n       evo[(wh - tol + n) % n].push_back(wh); n     } n   } n   for (int i = n - 1; i >= 0; --i) { n     for (int z : evi[i]) { n       segs.insert(z); n     } n     if (segs.size()) { n       int x = *segs.begin(); n       ans[i] = min(ans[i], (x - i) % n); n     } n     for (int z : evo[i]) { n       segs.erase(segs.find(z)); n     } n   } n   for (int i = 0; i < n; ++i) { n     cout << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char st[100005]; n int vis[100005]; n int main() { n   long l, cnt = 0; n   scanf( %ld , &l); n   scanf( %s , st); n   if (l > 26) { n     cout << -1; n     return 0; n   } n   for (long i = 0; i < l + 10; i++) vis[i] = 0; n   for (long i = 0; i < l; i++) { n     if (vis[i] == 0) { n       vis[i] = 1; n       for (long j = i + 1; j < l; j++) { n         if (st[i] == st[j] && vis[j] == 0) { n           cnt++; n           vis[j] = 1; n         } n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, int> cntmp; n int n; n struct Point { n   int x, y; n } pt[200005]; n bool cpx(const Point &a, const Point &b) { n   if (a.x != b.x) { n     return a.x < b.x; n   } n   return a.y < b.y; n } n bool cpy(const Point &a, const Point &b) { n   if (a.y != b.y) { n     return a.y < b.y; n   } n   return a.x < b.x; n } n long long dcs(long long sx, long long mx) { n   return (sx + mx) * (mx - sx + 1) / 2; n } n int main() { n   scanf(  %d , &n); n   cntmp.clear(); n   for (int i = 1; i <= n; i++) { n     scanf(  %d %d , &pt[i].x, &pt[i].y); n     cntmp[make_pair(pt[i].x, pt[i].y)]++; n   } n   sort(pt + 1, pt + 1 + n, cpx); n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     int j = i; n     long long cnt = 0; n     while (pt[j].x == pt[i].x && j <= n) { n       cnt++; n       j++; n     } n     j--; n     if (cnt > 1) { n       res += dcs(1, cnt - 1); n     } n     i = j; n   } n   sort(pt + 1, pt + 1 + n, cpy); n   for (int i = 1; i <= n; i++) { n     int j = i; n     long long cnt = 0; n     while (pt[j].y == pt[i].y && j <= n) { n       cnt++; n       j++; n     } n     j--; n     if (cnt > 1) { n       res += dcs(1, cnt - 1); n     } n     i = j; n   } n   for (map<pair<int, int>, int>::iterator it = cntmp.begin(); it != cntmp.end(); n        it++) { n     if (it->second > 1) { n       res -= dcs(1, it->second - 1); n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n const int N = 1e6 + 10; n const int M = 9699690; n const int mod = 1e9 + 7, Gi = 332748118; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n const int SIZE = 1 << 21; n char ibuf[SIZE], *iS, *iT; n using std::pair; n template <class T> n void read(T &x) { n   x = 0; n   int f = 0; n   char c = getchar(); n   while (!isdigit(c)) f |= c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   if (f) x = -x; n } n int random(int i) { return rand() % i + 1; } n template <class T> n void ckmax(T &x, T y) { n   x = x > y ? x : y; n } n template <class T> n void ckmin(T &x, T y) { n   x = x < y ? x : y; n } n template <class T> n T Abs(T x) { n   return x > 0 ? x : -x; n } n inline int plus(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } n int qp(int d, int k) { n   int f = 1; n   while (k) { n     if (k & 1) f = (1ll * (f) * (d) % mod); n     d = (1ll * (d) * (d) % mod), k >>= 1; n   } n   return f; n } n int n, m, a[N], b[N], tmp[N]; n pair<int, int> p[N]; n long long ans; n void msort(int l, int r) { n   if (l == r) return; n   int mid = l + r >> 1; n   msort(l, mid), msort(mid + 1, r); n   int lp = l, rp = mid + 1, ct = 0; n   while (lp <= mid && rp <= r) { n     if (a[lp] <= a[rp]) n       tmp[++ct] = a[lp++], ans += rp - 1 - mid; n     else n       tmp[++ct] = a[rp++]; n   } n   while (lp <= mid) tmp[++ct] = a[lp++], ans += rp - 1 - mid; n   while (rp <= r) tmp[++ct] = a[rp++]; n   for (int i = (l); i <= (r); i++) a[i] = tmp[i + 1 - l]; n } n int tag[N << 2], mi[N << 2]; n void build(int id, int l, int r) { n   tag[id] = 0; n   if (l == r) { n     mi[id] = l - 1; n     return; n   } n   int mid = l + r >> 1; n   build(id << 1, l, mid), build(id << 1 | 1, mid + 1, r); n   mi[id] = std::min(mi[id << 1], mi[id << 1 | 1]); n } n void pushdown(int id) { n   if (tag[id]) { n     tag[id << 1] += tag[id]; n     tag[id << 1 | 1] += tag[id]; n     mi[id << 1] += tag[id]; n     mi[id << 1 | 1] += tag[id]; n     tag[id] = 0; n   } n } n void upt(int id, int L, int R, int l, int r, int d) { n   if (l <= L && R <= r) { n     tag[id] += d; n     mi[id] += d; n     return; n   } n   if (l > R || r < L) return; n   int Mid = L + R >> 1; n   pushdown(id); n   upt(id << 1, L, Mid, l, r, d); n   upt(id << 1 | 1, Mid + 1, R, l, r, d); n   mi[id] = std::min(mi[id << 1], mi[id << 1 | 1]); n } n void work() { n   read(n), read(m); n   for (int i = (1); i <= (n); i++) read(a[i]), p[i] = std::make_pair(a[i], i); n   ans = 0; n   msort(1, n); n   for (int i = (1); i <= (m); i++) read(b[i]); n   std::sort(p + 1, p + 1 + n); n   std::sort(b + 1, b + 1 + m); n   build(1, 1, n + 1); n   int ld = 1, lu = 1; n   for (int i = (1); i <= (m); i++) { n     while (ld <= n && p[ld].first <= b[i]) n       upt(1, 1, n + 1, p[ld++].second + 1, n + 1, -1); n     while (lu <= n && p[lu].first < b[i]) n       upt(1, 1, n + 1, 1, p[lu++].second, 1); n     ans += mi[1]; n   } n   printf( %lld n , ans); n } n int main() { n   int T = 1; n   read(T); n   while (T--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, s, e, t, r; n   cin >> n >> m; n   vector<pair<int, int> > sections(n + 1); n   for (int i = 1; i <= n; i++) sections[i].first = 0x3f3f3f3f; n   while (m--) { n     cin >> s >> e >> t >> r; n     for (int i = s; i <= e; i++) { n       if (t < sections[i].first) { n         sections[i].first = t; n         sections[i].second = r; n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += sections[i].second; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct item { n   long long val; n   int ind; n   bool operator<(const item& o) const { return val < o.val; } n }; n long long n, m, k, s; n vector<long long> dollar, pound, dday, pday; n vector<item> ditems, pitems; n void readIt() { n   cin >> n >> m >> k >> s; n   long long minn = 1e7; n   long long v, mday = 0; n   dollar.push_back(1e16); n   pound.push_back(1e16); n   pday.push_back(-1); n   dday.push_back(-1); n   for (int i = 1; i <= n; ++i) { n     cin >> v; n     if (minn > v) { n       minn = v; n       mday = i; n     } n     dollar.push_back(minn); n     dday.push_back(mday); n   } n   minn = 1e7; n   mday = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> v; n     if (minn > v) { n       mday = i; n       minn = v; n     } n     pound.push_back(minn); n     pday.push_back(mday); n   } n   int type; n   for (int i = 0; i < m; ++i) { n     cin >> type >> v; n     if (type == 1) ditems.push_back({v, i + 1}); n     if (type == 2) pitems.push_back({v, i + 1}); n   } n   sort(ditems.begin(), ditems.end()); n   sort(pitems.begin(), pitems.end()); n } n bool isPossible(int days, bool verbose) { n   long long d = dollar[days]; n   long long p = pound[days]; n   int dit = 0, pit = 0; n   long long total = 0; n   for (int i = 0; i < k; ++i) { n     if (dit == ditems.size()) { n       if (verbose) cout << pitems[pit].ind <<     << pday[days] <<   n ; n       total += pitems[pit++].val * p; n       continue; n     } else if (pit == pitems.size()) { n       if (verbose) cout << ditems[dit].ind <<     << dday[days] <<   n ; n       total += ditems[dit++].val * d; n       continue; n     } n     if (ditems[dit].val * d < pitems[pit].val * p) { n       if (verbose) cout << ditems[dit].ind <<     << dday[days] <<   n ; n       total += ditems[dit++].val * d; n     } else { n       if (verbose) cout << pitems[pit].ind <<     << pday[days] <<   n ; n       total += pitems[pit++].val * p; n     } n   } n   if (total <= s) return true; n   return false; n } n int getDay() { n   int l = 0, r = n; n   while (l < r) { n     int m = (l + r + 1) / 2; n     if (isPossible(m, false)) { n       r = m - 1; n     } else { n       l = m; n     } n   } n   if (l == n) return -1; n   return l + 1; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   readIt(); n   int day = getDay(); n   if (day == -1) { n     cout << -1 << endl; n   } else { n     cout << day <<   n ; n     isPossible(day, true); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 255; n int parent[N]; n int ra[N]; n void make_set(int v) { n   parent[v] = v; n   ra[v] = 0; n } n int find_set(int v) { n   if (v == parent[v]) return v; n   return parent[v] = find_set(parent[v]); n } n void union_sets(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) { n     if (ra[a] < ra[b]) swap(a, b); n     parent[b] = a; n     if (ra[a] == ra[b]) ++ra[a]; n   } n } n string s[N]; n int main() { n   cout.sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < n; i++) make_set(i); n   for (int j = 0; j < m; j++) make_set(100 + j); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  # ) { n         union_sets(i, 100 + j); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  #  && find_set(i) != find_set(100 + j)) { n         cout <<  No ; n         return 0; n       } n       if (s[i][j] !=  #  && find_set(i) == find_set(100 + j)) { n         cout <<  No ; n         return 0; n       } n     } n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const int mod = inf + 7; n const double eps = 1e-9; n const double pi = acos(-1.0); n int n, a[300300]; n int dp[33][300300]; n int calc(int bon, int ind) { n   if (ind == n) return 0; n   int &res = dp[bon][ind]; n   if (res != -1) return res; n   res = a[ind] + calc(min(bon + a[ind] / 10, 31), ind + 1); n   for (int i = 1; i <= bon && i <= a[ind]; i++) { n     res = min(res, a[ind] - i + calc(bon - i, ind + 1)); n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     a[i] /= 100; n   } n   memset(dp, -1, sizeof dp); n   printf( %d n , calc(0, 0) * 100); n   return 0; n } n


#include <bits/stdc++.h> n int n, sum; n int a[16]; n int b[4][4]; n int cmp(const void *a, const void *b) { return *((int *)a) - *((int *)b); } n int dfs(int x, int y, int z) { n   int p, q, i, j; n   if (x == n && y == 0) return 1; n   p = x; n   q = y + 1; n   if (q == n) { n     p = x + 1; n     q = 0; n   } n   for (i = 0; i < n * n; i++) { n     if ((z >> i) & 1) continue; n     if (x == n - 1) { n       int s = 0; n       for (j = 0; j < n - 1; j++) s += b[j][y]; n       if (s + a[i] != sum) continue; n     } n     if (y == n - 1) { n       int s = 0; n       for (j = 0; j < n - 1; j++) s += b[x][j]; n       if (s + a[i] != sum) continue; n     } n     if (x == n - 1 && y == 0) { n       int s = 0; n       for (j = 0; j < n - 1; j++) s += b[j][n - j - 1]; n       if (s + a[i] != sum) continue; n     } n     if (x == n - 1 && y == n - 1) { n       int s = 0; n       for (j = 0; j < n - 1; j++) s += b[j][j]; n       if (s + a[i] != sum) continue; n     } n     b[x][y] = a[i]; n     if (dfs(p, q, z | (1 << i)) == 1) return 1; n     for (j = i + 1; j < n * n; j++) { n       if (a[i] != a[j]) break; n     } n     i = j - 1; n   } n   return -1; n } n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 0; i < n * n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   sum /= n; n   printf( %d n , sum); n   qsort(a, n * n, sizeof(int), cmp); n   dfs(0, 0, 0); n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (j > 0) putchar(   ); n       printf( %d , b[i][j]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 1e18; n const double pi = acos(-1.0); n const double inf = 1e18; n const double eps = 1e-9; n const long long mod = 1e9 + 7; n const int maxmat = 10; n const unsigned long long BASE = 133333331; n inline void RI(int &x) { n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 ; n } n const int dy[3] = {-1, 0, 1}; n char maze[3][105]; n bool vis[3][105][105]; n int sx, sy; n int N, k; n struct Node { n   int x, y, t; n }; n bool valid(int x, int y, int t) { n   return (x >= 0 && x < 3 && y >= 0 && y < N && n           (maze[x][y + 2 * t] ==  .  || maze[x][y + 2 * t] == 0)); n } n bool bfs(int x, int y) { n   queue<Node> q; n   q.push((Node){x, y, 0}); n   while (!q.empty()) { n     Node tmp = q.front(); n     q.pop(); n     if (tmp.y == N - 1) return true; n     if (maze[tmp.x][tmp.y + 1 + 2 * tmp.t] >=  A  && n         maze[tmp.x][tmp.y + 1 + 2 * tmp.t] <=  Z ) n       continue; n     for (int k = 0; k < 3; k++) { n       int tmpx = tmp.x + dy[k]; n       int tmpy = tmp.y + 1; n       if (valid(tmpx, tmpy, tmp.t) && valid(tmpx, tmpy, tmp.t + 1) && n           !vis[tmpx][tmpy][tmp.t + 1]) { n         vis[tmpx][tmpy][tmp.t + 1] = true; n         q.push((Node){tmpx, tmpy, tmp.t + 1}); n       } n     } n   } n   return false; n } n int main(int argc, char const *argv[]) { n   int T; n   cin >> T; n   while (T--) { n     scanf( %d%d , &N, &k); n     memset(maze, 0, sizeof(maze)); n     memset(vis, false, sizeof(vis)); n     for (int i = 0; i < 3; i++) { n       char s[105]; n       scanf( %s , s); n       for (int j = 0; j < N; j++) { n         maze[i][j] = s[j]; n         if (maze[i][j] ==  s ) sx = i, sy = j; n       } n     } n     cout << (bfs(sx, sy) ?  YES  :  NO ) << endl; n   } n   return 0; n } n


# include<bits/stdc++.h> n using namespace std; n  n int main() n { n   int a,b,c=0;set<int>s; n   cin>>a>>b; n  n   while(a) n   { n     c++; n     s.insert(a%b); n     a/=b; n   } n   if(c==s.size()) n     cout<< YES <<endl; n     else n     cout<< NO <<endl; n  n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double E = 2.71828182845904523536; n const long double pi = acos(-1); n const double eps = 1e-9; n const long long mod = 1e9 + 7; n const long long inf = 1LL << 30; n const int N = 500500; n vector<int> adj[N]; n vector<int> v; n int dep[N]; n void DFS(int u, int p) { n   dep[u] = 1 + dep[p]; n   for (auto v : adj[u]) n     if (v != p) DFS(v, u); n   if (adj[u].size() == 1) v.push_back(dep[u]); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dep[1] = 0; n   int ans = 0; n   for (auto u : adj[1]) { n     v.clear(); n     DFS(u, 1); n     sort(v.begin(), v.end()); n     for (int i = 1; i < v.size(); i++) n       if (v[i] <= v[i - 1]) v[i] = v[i - 1] + 1; n     ans = max(ans, v.back()); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[500005], n, nm[500005], g[321]; n vector<int> v[55]; n char s[500005], p[500005]; n inline int qr() { n   int x = 0, w = 1; n   char a = 0; n   while (a <  0  || a >  9 ) { n     if (a ==  - ) w = -1; n     a = getchar(); n   } n   while (a <=  9  && a >=  0 ) { n     x = (x << 3) + (x << 1) + (a ^ 48); n     a = getchar(); n   } n   return x * w; n } n inline int lowbit(int x) { return x & -x; } n void add(int x, int d) { n   while (x <= n) { n     c[x] += d; n     x += lowbit(x); n   } n } n long long qu(int x) { n   long long ans = 0; n   while (x) { n     ans += c[x]; n     x -= lowbit(x); n   } n   return ans; n } n int main() { n   n = qr(); n   cin >> s + 1; n   for (register int i = 1; i <= n; i++) p[i] = s[n - i + 1]; n   for (register int i = 1; i <= n; i++) v[p[i] -  a  + 1].push_back(i); n   for (register int i = 1; i <= n; i++) { n     int ch = s[i] -  a  + 1; n     nm[i] = v[ch][g[ch]]; n     ++g[ch]; n   } n   long long ans = 0; n   for (register int i = 1; i <= n; i++) { n     add(nm[i], 1); n     ans += qu(n) - qu(nm[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n const int maxn = 500; n int v[maxn + 5]; n long long vv[maxn + 5]; n int ans[maxn + 5]; n int r[maxn + 5]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &v[i]); n   scanf( %d , &k); n   for (int i = 0; i < k; i++) scanf( %I64d , &vv[i]); n   memset(ans, -1, sizeof ans); n   bool ok = true; n   long long sum = 0; n   int vvi = 0; n   for (int i = 0; i < n; i++) { n     sum += v[i]; n     if (sum == vv[vvi]) { n       sum = 0; n       r[vvi] = i; n       vvi++; n     } else if (sum > vv[vvi]) { n       ok = false; n       break; n     } n   } n   ok = ok && (vvi == k); n   if (!ok) { n     printf( NO n ); n     return 0; n   } n   vvi = 0; n   int prev = -1; n   int maxval = -1; n   for (int i = 0; i < n; i++) { n     if (i > r[vvi]) { n       if (r[vvi] - prev == 1) { n         ans[vvi] = r[vvi]; n       } n       if (ans[vvi] == -1) { n         ok = false; n         break; n       } n       prev = r[vvi]; n       maxval = -1; n       vvi++; n     } n     if (i - 1 > prev && v[i] > v[i - 1] && v[i] > maxval) { n       maxval = v[i]; n       ans[vvi] = i; n     } n     if (i + 1 <= r[vvi] && v[i] > v[i + 1] && v[i] > maxval) { n       maxval = v[i]; n       ans[vvi] = i; n     } n   } n   if (ans[vvi] == -1) { n     if (r[vvi] - prev == 1) { n       ans[vvi] = r[vvi]; n     } else { n       ok = false; n     } n   } n   ok = ok && (vvi == k - 1); n   if (!ok) { n     printf( NO n ); n     return 0; n   } n   printf( YES n ); n   prev = -1; n   for (int i = 0; i < k; i++) { n     int idx = ans[i]; n     int pidx = ans[i] - prev + i; n     if (idx - 1 > prev && v[idx] > v[idx - 1]) { n       for (int j = 0; j < ans[i] - prev - 1; j++) { n         printf( %d L n , pidx--); n       } n       for (int j = 0; j < r[i] - ans[i]; j++) { n         printf( %d R n , i + 1); n       } n     } else { n       for (int j = 0; j < r[i] - ans[i]; j++) { n         printf( %d R n , pidx); n       } n       for (int j = 0; j < ans[i] - prev - 1; j++) { n         printf( %d L n , pidx--); n       } n     } n     prev = r[i]; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n void solve(void); n int main(){ n int t; n cin>>t; n while(t--){ n  solve(); n } n  n  n return 0;} n void solve(){ n ll n,i; n cin>>n; n ll a[n+1]; n for(i=1;i<=n;i++) n {cin>>a[i]; n  n } n cout<<3*n<<endl; n for(i=1;i<n;i++) n { n  n cout<< 1 <<   <<i<<   <<i+1<<endl; n     cout<< 2 <<   <<i<<   <<i+1<<endl; n     cout<< 1 <<   <<i<<   <<i+1<<endl; n     cout<< 2 <<   <<i<<   <<i+1<<endl; n     cout<< 1 <<   <<i<<   <<i+1<<endl; n cout<< 2 <<   <<i<<   <<i+1<<endl; n i++; n  n } n  n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n enum { MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009 }; n int a[200000]; n long long ft[200000]; n int t[800000]; n int n; n void inc(int index, int val) { n   while (index < n) { n     ft[index] += val; n     index |= index + 1; n   } n } n long long sum(int r) { n   long long ret = 0; n   for (; r >= 0; r &= r + 1, --r) { n     ret += ft[r]; n   } n   return ret; n } n long long sum(int l, int r) { return sum(r) - (l ? sum(l - 1) : 0); } n void build(int x = 0, int L = 0, int R = n - 1) { n   if (L == R) { n     t[x] = a[L]; n     return; n   } n   int M = (L + R) / 2; n   build(2 * x + 1, L, M); n   build(2 * x + 2, M + 1, R); n   t[x] = max(t[2 * x + 1], t[2 * x + 2]); n } n void upd(int index, int val, int x = 0, int L = 0, int R = n - 1) { n   if (L == R) { n     t[x] = val; n     return; n   } n   int M = (L + R) / 2; n   if (index <= M) { n     upd(index, val, 2 * x + 1, L, M); n   } else { n     upd(index, val, 2 * x + 2, M + 1, R); n   } n   t[x] = max(t[2 * x + 1], t[2 * x + 2]); n } n int get(long long val, int from, int x = 0, int L = 0, int R = n - 1) { n   if (t[x] < val || R < from) { n     return n; n   } else if (L == R) { n     return L; n   } n   int M = (L + R) / 2; n   if (M < from) { n     return get(val, from, 2 * x + 2, M + 1, R); n   } else if (t[2 * x + 1] >= val) { n     int s = get(val, from, 2 * x + 1, L, M); n     if (s < n) { n       return s; n     } n   } n   return get(val, from, 2 * x + 2, M + 1, R); n } n int get_ans() { n   long long cur = 0; n   int index = 0; n   if (a[0] == 0) { n     return 1; n   } n   while (index < n) { n     cur += a[index]; n     index = get(cur, index + 1); n     cur = sum(index - 1); n     if (a[index] == cur) { n       break; n     } n   } n   return index >= n ? -1 : index + 1; n } n int main() { n   int q; n   cin >> n >> q; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     inc(i, a[i]); n   } n   build(); n   while (q--) { n     int p, x; n     cin >> p >> x; n     --p; n     inc(p, x - a[p]); n     a[p] = x; n     upd(p, x); n     cout << get_ans() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, min, max; n   std::cin >> n >> m >> min >> max; n   int a[m]; n   int maxHit = 0; n   int minHit = 0; n   for (int i = 0; i < m; ++i) { n     std::cin >> a[i]; n     if (a[i] == min) { n       minHit = 1; n     } else if (a[i] == max) { n       maxHit = 1; n     } n     if (a[i] > max || a[i] < min) { n       std::cout <<  Incorrect ; n       return 0; n     } n   } n   if (minHit != 1 && maxHit != 1) { n     if (n - m < 2) { n       std::cout <<  Incorrect ; n     } else { n       std::cout <<  Correct ; n     } n   } else if (minHit != 1 || maxHit != 1) { n     if (n - m < 1) { n       std::cout <<  Incorrect ; n     } else { n       std::cout <<  Correct ; n     } n   } else { n     std::cout <<  Correct ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n double dp[5010], f[5010]; n int main() { n   int T, i, j, k, ca = 0, n, m; n   while (~scanf( %d%d , &n, &T)) { n     for (i = 0; i <= T; i++) dp[i] = 0; n     dp[0] = 1; n     double ans = 0; n     for (i = 0; i < n; i++) { n       int p1, t; n       scanf( %d%d , &p1, &t); n       double p = 1. * p1 / 100, q = 1; n       for (j = 1; j < t; j++) q *= (1 - p); n       f[0] = dp[0]; n       for (j = 1; j <= T; j++) f[j] = f[j - 1] * (1 - p) + dp[j]; n       for (j = T; j >= 1; j--) { n         double c = 0; n         if (j >= t) c = f[j - t]; n         dp[j] = (f[j - 1] - c * q) * p; n         if (j >= t) dp[j] += q * dp[j - t]; n       } n       dp[0] = 0; n       for (j = 1; j <= T; j++) ans += dp[j]; n     } n     printf( %lf n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   int x = abs(a - b); n   if (x % 2 == 0) { n     x /= 2; n     cout << x * (x + 1) << endl; n   } else { n     x /= 2; n     cout << x * (x + 1) + x + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Mx = 1e5 + 5; n const long long inf = 1e9 + 1; n struct Point { n   long long x, y; n } point[Mx]; n int n; n long long ans, hmax[Mx], emax[Mx], hmin[Mx], emin[Mx]; n template <class T> n bool cmp(T a, T b) { n   return a.x < b.x; n } n bool judge(long long mid) { n   int l = 1, r = 1; n   long long Max, Min; n   while (r <= n) { n     while (point[r].x - point[l].x <= mid && r <= n) r++; n     Max = -inf; n     Min = inf; n     if (l != 1) { n       Max = max(Max, hmax[l - 1]); n       Min = min(Min, hmin[l - 1]); n     } n     if (r <= n) { n       Max = max(Max, emax[r]); n       Min = min(Min, emin[r]); n     } n     if (Max == -inf) return 1; n     if (Max - Min <= mid) return 1; n     l++; n   } n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     point[i].x = x + y; n     point[i].y = x - y; n   } n   sort(point + 1, point + 1 + n, cmp<Point>); n   hmin[0] = inf; n   hmax[0] = -inf; n   emin[n + 1] = inf; n   emax[n + 1] = -inf; n   for (int i = 1; i <= n; i++) { n     hmax[i] = max(hmax[i - 1], point[i].y); n     hmin[i] = min(hmin[i - 1], point[i].y); n   } n   for (int i = n; i >= 1; i--) { n     emax[i] = max(emax[i + 1], point[i].y); n     emin[i] = min(emin[i + 1], point[i].y); n   } n   long long l = 0, r = 2e9; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (judge(mid)) { n       r = mid - 1; n       ans = mid; n     } else { n       l = mid + 1; n     } n   } n   printf( %lf , 1.0 * ans / 2); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n == 1 or n > 26) { n     cout <<  Yes n ; n     return 0; n   } n   set<char> s; n   char c; n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (s.find(c) != s.end()) { n       cout <<  Yes n ; n       return 0; n     } n     s.insert(c); n   } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l, r, shag = 0; n   cin >> n; n   l = 1; n   r = n; n   while (l <= r) { n     if (shag % 2 == 0) n       cout << l++ <<    ; n     else n       cout << r-- <<    ; n     shag++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, x, y, k; n   cin >> n >> m; n   long long a[n], b[m]; n   cin >> x >> k >> y; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < m; i++) cin >> b[i]; n   vector<long long> adj[n]; n   long long z[n]; n   long long ind = 0; n   for (long long i = 0; i < n; i++) { n     if (ind >= m) { n       adj[ind].push_back(a[i]); n       continue; n     } n     if (b[ind] == a[i]) { n       if (ind - 1 >= 0) n         z[ind] = max(b[ind], b[ind - 1]); n       else n         z[ind] = b[ind]; n       ind++; n     } else n       adj[ind].push_back(a[i]); n   } n   if (ind < m - 1) { n     cout <<  -1 n ; n     return 0; n   } n   z[ind] = b[m - 1]; n   ind = 0; n   long long ans = 0; n   int f = 0; n   for (auto &it : adj) { n     long long itl = it.size(); n     if (itl == 0) { n       ind++; n       continue; n     } n     long long mxv = -1; n     for (auto &it2 : it) mxv = max(mxv, it2); n     if (itl < k) { n       if (mxv < z[ind]) n         ans += itl * y; n       else { n         f = 1; n         break; n       } n     } else { n       if (x >= y * k) { n         if (mxv < z[ind]) n           ans += itl * y; n         else { n           ans = ans + (itl - k) * y + x; n         } n       } else { n         ans += (itl / k) * x + (itl % k) * y; n       } n     } n     ind++; n   } n   if (f == 1) n     cout <<  -1 n ; n   else n     cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   long long l = v.size(); n   for (long long i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n template <typename T> n void trace(const char* name, T&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename T, typename... Args> n void trace(const char* names, T&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   trace(comma + 1, args...); n } n const int N = 202; n const int K = 1010; n const int MOD = 1e9 + 7; n int dp[N][N][K]; n int n, kk, arr[N]; n int pre[N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> kk; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   sort(arr + 1, arr + 1 + n); n   dp[0][0][0] = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k < K; k++) { n         int nk = k + (j) * (arr[i + 1] - arr[i]); n         if (nk <= kk) { n           dp[i + 1][j][nk] = n               (0LL + (1LL * dp[i][j][k] * (j + 1)) % MOD + dp[i + 1][j][nk]) % n               MOD; n           dp[i + 1][j + 1][nk] = n               (0LL + dp[i + 1][j + 1][nk] + dp[i][j][k]) % MOD; n           if (j) n             dp[i + 1][j - 1][nk] = n                 (0LL + dp[i + 1][j - 1][nk] + 1LL * dp[i][j][k] * j) % MOD; n         } n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= kk; i++) { n     ans = (ans + dp[n][0][i]) % MOD; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long c(int a, int b) { n   long long res = 1; n   for (int i = 1; i <= b; ++i) { n     res = res * (a + 1 - i) / i; n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &k); n   long long ans = 1; n   for (int i = 1; i <= k; ++i) { n     if (i == 2) { n       ans += c(n, 2); n     } n     if (i == 3) { n       ans += 2 * c(n, 3); n     } n     if (i == 4) { n       ans += 9 * c(n, 4); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n namespace test { n struct Poly { n  public: n   using value_type = unsigned int; n   using ptr_type = value_type *; n   using const_ptr_type = value_type const *; n   using cast_type = unsigned long long; n   using size_type = unsigned int; n   using arr_type = value_type[1 << 21]; n   using iterator = ptr_type; n   using const_iterator = const_ptr_type; n   static constexpr value_type P = 998244353, G = 3; n  n  private: n   iterator _Start, _Finish, _End; n   static inline arr_type ROOT, IROOT; n   Poly &dft(size_type len) { return resize(len), _dft(len, _Start), *this; } n   Poly &idft(size_type len) { return _idft(len, _Start), *this; } n   Poly &revbin() { return _revbin(size(), _Start), *this; } n   value_type horner(value_type x) const { n     value_type res = *(end() - 1); n     for (const_iterator i = end() - 1, e = begin(); i != e; --i) n       res = (static_cast<cast_type>(res) * x + *(i - 1)) % P; n     return res; n   } n   using v_it = std::vector<value_type>::const_iterator; n   static void segment_tree_build(std::vector<Poly> &, size_type, v_it, v_it); n   static void segment_tree_solve(const std::vector<Poly> &, size_type, v_it, n                                  v_it, const Poly &, std::vector<value_type> &); n   static size_type _get_len(size_type); n   static value_type _mod_pow(value_type, long long); n   static value_type _legendre_symbol(value_type); n   static value_type _tonelli_shanks(value_type); n   static void _revbin(size_type, ptr_type); n   static void _dif_ntt(size_type, ptr_type, ptr_type); n   static void _dit_ntt(size_type, ptr_type, ptr_type); n   static void _init_root_of_unity(size_type); n   static void _dft(size_type, ptr_type); n   static void _idft(size_type, ptr_type); n  n  public: n   iterator begin() { return _Start; } n   iterator end() { return _Finish; } n   const_iterator begin() const { return const_cast<const_iterator>(_Start); } n   const_iterator end() const { return const_cast<const_iterator>(_Finish); } n   size_type size() const { return _Finish - _Start; } n   size_type capacity() const { return _End - _Start; } n   value_type &operator[](size_type x) { return *(_Start + x); } n   value_type operator[](size_type x) const { return *(_Start + x); } n   Poly &remove_trailing_zeros() { n     while (size() > 1 && *(_Finish - 1) == 0) --_Finish; n     return *this; n   } n   Poly &resize(size_type new_size) { n     assert(new_size > 0); n     if (new_size > capacity()) { n       size_t old_capacity = capacity(), new_capacity = _get_len(new_size); n       _Start = static_cast<iterator>( n           std::realloc(_Start, sizeof(value_type) * new_capacity)); n       assert(_Start != nullptr); n       _End = _Start + new_capacity; n       std::memset(_Start + old_capacity, 0, n                   sizeof(value_type) * (new_capacity - old_capacity)); n     } else if (size() > new_size) n       std::memset(_Start + new_size, 0, n                   sizeof(value_type) * (size() - new_size)); n     return _Finish = _Start + new_size, *this; n   } n   explicit Poly(size_type _reserved) n       : _Start(static_cast<iterator>( n             std::calloc(_get_len(_reserved), sizeof(value_type)))), n         _Finish(_Start + _reserved), n         _End(_Start + _get_len(_reserved)) { n     assert((_reserved > 0 && _Start != nullptr)); n   } n   Poly(const_iterator _first, const_iterator _last) n       : _Start(static_cast<iterator>( n             std::malloc(sizeof(value_type) * _get_len(_last - _first)))), n         _Finish(_Start + (_last - _first)), n         _End(_Start + _get_len(_last - _first)) { n     assert((_last - _first > 0 && _Start != nullptr)); n     std::memcpy(_Start, _first, sizeof(value_type) * (_last - _first)); n     std::memset(_Finish, 0, sizeof(value_type) * (_End - _Finish)); n   } n   Poly(const Poly &rhs) : Poly(rhs.begin(), rhs.end()) {} n   Poly(Poly &&rhs) : _Start(rhs._Start), _Finish(rhs._Finish), _End(rhs._End) { n     rhs._Start = rhs._Finish = rhs._End = nullptr; n   } n   Poly(std::initializer_list<value_type> l) : Poly(l.begin(), l.end()) {} n   ~Poly() { n     if (_Start != nullptr) std::free(_Start); n   } n   Poly &operator=(const Poly &rhs) { n     if (this == &rhs) return *this; n     return resize(rhs.size()), n            std::memcpy(this->_Start, rhs.begin(), n                        sizeof(value_type) * rhs.size()), n            *this; n   } n   Poly operator-() const { n     Poly res(*this); n     for (iterator i = res.begin(), e = res.end(); i != e; ++i) n       if (*i != 0) *i = P - *i; n     return res; n   } n   Poly &operator+=(const Poly &rhs) { n     if (size() < rhs.size()) resize(rhs.size()); n     for (size_type i = 0, e = rhs.size(); i != e; ++i) n       *(this->_Start + i) = (*(this->_Start + i) + *(rhs._Start + i)) % P; n     return *this; n   } n   Poly &operator-=(const Poly &rhs) { n     if (size() < rhs.size()) resize(rhs.size()); n     for (size_type i = 0, e = rhs.size(); i != e; ++i) n       *(this->_Start + i) = (*(this->_Start + i) + P - *(rhs._Start + i)) % P; n     return *this; n   } n   Poly &operator*=(const Poly &); n   Poly &operator/=(const Poly &); n   Poly &operator%=(const Poly &); n   Poly quo(const Poly &) const; n   std::pair<Poly, Poly> quo_with_rem(const Poly &) const; n   Poly deriv() const; n   Poly integr() const; n   Poly inv() const; n   Poly log() const; n   Poly exp() const; n   Poly sqrt() const; n   Poly isqrt() const; n   std::vector<value_type> eval(const std::vector<value_type> &) const; n   static Poly inter(const std::vector<value_type> &, n                     const std::vector<value_type> &); n   friend Poly operator+(const Poly &lhs, const Poly &rhs) { n     return Poly(lhs) += rhs; n   } n   friend Poly operator-(const Poly &lhs, const Poly &rhs) { n     return Poly(lhs) -= rhs; n   } n   friend Poly operator*(const Poly &lhs, const Poly &rhs) { n     return Poly(lhs) *= rhs; n   } n   friend Poly operator/(const Poly &lhs, const Poly &rhs) { n     return Poly(lhs) /= rhs; n   } n   friend Poly operator%(const Poly &lhs, const Poly &rhs) { n     return Poly(lhs) %= rhs; n   } n   friend void swap(Poly &lhs, Poly &rhs) { n     iterator tmp; n     tmp = lhs._Start, lhs._Start = rhs._Start, rhs._Start = tmp; n     tmp = lhs._Finish, lhs._Finish = rhs._Finish, rhs._Finish = tmp; n     tmp = lhs._End, lhs._End = rhs._End, rhs._End = tmp; n   } n   friend std::istream &operator>>(std::istream &is, Poly &rhs) { n     for (iterator i = rhs.begin(), e = rhs.end(); i != e; ++i) is >> *i; n     return is; n   } n   friend std::ostream &operator<<(std::ostream &os, const Poly &rhs) { n     for (const_iterator i = rhs.begin(), e = rhs.end(); i != e; ++i) n       os << *i <<    ; n     return os; n   } n }; n Poly::size_type Poly::_get_len(size_type x) { n   size_type res = 1; n   while (res < x) res <<= 1; n   return res; n } n Poly::value_type Poly::_mod_pow(value_type x, long long y) { n   if ((y %= static_cast<long long>(P - 1)) < 0) y += P - 1; n   value_type res = 1; n   for (; y; y >>= 1, x = static_cast<cast_type>(x) * x % P) n     if (y & 1) res = static_cast<cast_type>(res) * x % P; n   return res; n } n Poly::value_type Poly::_legendre_symbol(value_type x) { n   return x == 0 ? 0 : _mod_pow(x, P - 1 >> 1); n } n Poly::value_type Poly::_tonelli_shanks(value_type a) { n   if (_legendre_symbol(a) != 1) return 0; n   value_type q = P - 1, t = 0, n, z; n   while ((q & 1) == 0) q >>= 1, ++t; n   if (t == 1) return _mod_pow(a, P + 1 >> 2); n   for (n = 1; n != P; ++n) n     if (_legendre_symbol(n) == P - 1) { n       z = _mod_pow(n, q); n       break; n     } n   value_type y = z, r = t, x = _mod_pow(a, q - 1 >> 1), b = x; n   x = static_cast<cast_type>(x) * a % P, b = static_cast<cast_type>(x) * b % P; n   while (true) { n     if (b == 1) return x < P - x ? x : P - x; n     value_type m = 1; n     for (; m < r; ++m) n       if (_mod_pow(b, 1 << m) == 1) break; n     value_type v = _mod_pow(y, 1 << r - m - 1); n     y = static_cast<cast_type>(v) * v % P, r = m, n     x = static_cast<cast_type>(x) * v % P, n     b = static_cast<cast_type>(b) * y % P; n   } n } n void Poly::_dif_ntt(size_type n, ptr_type x, ptr_type root) { n   for (size_type i = n; i != 1; i >>= 1) n     for (size_type j = 0, l = i >> 1; j != n; j += i) n       for (size_type k = 0; k != l; ++k) { n         value_type u = *(x + j + k), v = *(x + j + k + l); n         *(x + j + k) = (u + v >= P ? u + v - P : u + v), n                   *(x + j + k + l) = n                       (static_cast<cast_type>(u) + P - v) * *(root + l + k) % P; n       } n } n void Poly::_dit_ntt(size_type n, ptr_type x, ptr_type root) { n   for (size_type i = 2; i <= n; i <<= 1) n     for (size_type j = 0, l = i >> 1; j != n; j += i) n       for (size_type k = 0; k != l; ++k) { n         value_type u = *(x + j + k), n                    v = static_cast<cast_type>(*(root + l + k)) * n                        *(x + j + k + l) % P; n         *(x + j + k) = (u + v >= P ? u + v - P : u + v), n                   *(x + j + k + l) = (u >= v ? u - v : u + P - v); n       } n } n void Poly::_init_root_of_unity(size_type n) { n   static size_type lim = 1; n   if (lim < n) { n     const size_type l = n >> 1; n     value_type g = _mod_pow(G, (P - 1) / n), n                ig = _mod_pow(G, -static_cast<int>((P - 1) / n)); n     ROOT[l] = IROOT[l] = 1; n     for (size_type i = l + 1; i < n; ++i) n       ROOT[i] = static_cast<cast_type>(ROOT[i - 1]) * g % P, n       IROOT[i] = static_cast<cast_type>(IROOT[i - 1]) * ig % P; n     for (size_type i = l - 1; i >= lim; --i) n       ROOT[i] = ROOT[i << 1], IROOT[i] = IROOT[i << 1]; n     lim = n; n   } n } n void Poly::_revbin(size_type n, ptr_type x) { n   for (size_type i = 0, j = 0; i != n; ++i) { n     if (i < j) std::swap(*(x + i), *(x + j)); n     for (size_type k = n >> 1; (j ^= k) < k; k >>= 1) { n     } n   } n } n void Poly::_dft(size_type n, ptr_type x) { n   _init_root_of_unity(n), _dif_ntt(n, x, ROOT); n } n void Poly::_idft(size_type n, ptr_type x) { n   _dit_ntt(n, x, IROOT); n   value_type in = P - (P - 1) / n; n   for (ptr_type i = x, e = x + n; i != e; ++i) n     *i = static_cast<cast_type>(*i) * in % P; n } n Poly &Poly::operator*=(const Poly &rhs) { n   size_type n = size(), m = rhs.size(), len = _get_len(n + m - 1); n   if (n + m - 1 <= 128 || std::min(n, m) <= 8) { n     Poly res(n + m - 1); n     for (size_type i = 0; i != n; ++i) n       for (size_type j = 0; j != m; ++j) n         *(res._Start + i + j) = n             (*(res._Start + i + j) + n              static_cast<cast_type>(*(this->_Start + i)) * *(rhs._Start + j)) % n             P; n     return this->operator=(res); n   } else { n     Poly rhs_cpy(rhs); n     dft(len), rhs_cpy.dft(len); n     for (size_type i = 0; i != len; ++i) n       *(_Start + i) = static_cast<cast_type>(*(this->_Start + i)) * n                       *(rhs_cpy._Start + i) % P; n     return idft(len).resize(n + m - 1); n   } n } n Poly &Poly::operator/=(const Poly &rhs) { n   if (size() < rhs.size()) return this->operator=(Poly{0}); n   if (rhs.size() == 1) return this->operator*=(rhs.inv()); n   size_type n = size() - rhs.size() + 1; n   Poly rhs_cpy(rhs); n   std::reverse(begin(), end()), std::reverse(rhs_cpy.begin(), rhs_cpy.end()); n   Poly res = resize(n).quo(rhs_cpy); n   std::reverse(res.begin(), res.end()); n   return this->operator=(res); n } n Poly &Poly::operator%=(const Poly &rhs) { n   return this->operator=(quo_with_rem(rhs).second); n } n std::pair<Poly, Poly> Poly::quo_with_rem(const Poly &rhs) const { n   if (size() < rhs.size()) return {{0}, *this}; n   Poly q = *this / rhs; n   if (rhs.size() == 1) return {q, {0}}; n   size_type len = _get_len(std::max(q.size(), rhs.size())); n   int overlap = static_cast<int>(q.size() + rhs.size() - 1) - len; n   Poly q_cpy(q), rhs_cpy(rhs); n   q_cpy.dft(len), rhs_cpy.dft(len); n   for (size_type i = 0; i != len; ++i) n     *(q_cpy._Start + i) = n         static_cast<cast_type>(*(q_cpy._Start + i)) * *(rhs_cpy._Start + i) % P; n   q_cpy.idft(len).resize(rhs.size() - 1); n   if (overlap > 0) n     for (size_type i = 0; i != overlap && q_cpy.begin() + i != q_cpy.end(); ++i) n       *(q_cpy._Start + i) = n           (*(q_cpy._Start + i) + P - this->operator[](len + i)) % P; n   for (size_type i = 0, e = q_cpy.size(); i != e; ++i) n     *(q_cpy._Start + i) = (this->operator[](i) + P - *(q_cpy._Start + i)) % P; n   return {q, q_cpy}; n } n Poly Poly::deriv() const { n   if (size() == 1) return {0}; n   Poly res(size() - 1); n   for (size_type i = 1, len = size(); i != len; ++i) n     *(res._Start + i - 1) = static_cast<cast_type>(this->operator[](i)) * i % P; n   return res; n } n Poly Poly::integr() const { n   static size_type lim = 0; n   static arr_type INV{0, 1}; n   size_type len = size() + 1; n   Poly res(len); n   if (lim < len) n     for (size_type i = (lim < 2 ? 2 : lim); i < len; ++i) n       INV[i] = static_cast<cast_type>(P - P / i) * INV[P % i] % P; n   for (size_type i = 1; i != len; ++i) n     *(res._Start + i) = n         static_cast<cast_type>(this->operator[](i - 1)) * INV[i] % P; n   return res; n } n Poly Poly::inv() const { n   assert(*begin() != 0); n   size_type len = _get_len(size()); n   Poly res(len); n   res[0] = _mod_pow(*this->_Start, -1); n   res.resize(1); n   for (size_type i = 2; i <= len; i <<= 1) { n     Poly cpy(begin(), begin() + i), res_cpy(res); n     res_cpy.dft(i), cpy.dft(i); n     for (size_type j = 0; j != i; ++j) n       *(cpy._Start + j) = n           static_cast<cast_type>(*(cpy._Start + j)) * *(res_cpy._Start + j) % P; n     cpy.idft(i); n     std::memset(cpy.begin(), 0, sizeof(value_type) * (i >> 1)); n     cpy.dft(i); n     for (size_type j = 0; j != i; ++j) n       *(cpy._Start + j) = n           static_cast<cast_type>(*(cpy._Start + j)) * *(res_cpy._Start + j) % P; n     cpy.idft(i), res.resize(i); n     for (iterator j = cpy.begin() + (i >> 1), e = cpy.end(); j != e; ++j) n       if (*j != 0) *j = P - *j; n     std::memcpy(res.begin() + (i >> 1), cpy.begin() + (i >> 1), n                 sizeof(value_type) * (i >> 1)); n   } n   return res.resize(size()); n } n Poly Poly::quo(const Poly &rhs) const { n   if (size() == 1) n     return Poly{static_cast<value_type>(static_cast<cast_type>(*begin()) * n                                         _mod_pow(*rhs.begin(), -1) % P)}; n   Poly rhs_cpy(rhs); n   size_type len = _get_len(size()), len_2 = len >> 1; n   rhs_cpy.resize(len); n   Poly rhs_inv = Poly(rhs_cpy.begin(), rhs_cpy.begin() + len_2).inv(), n        q(begin(), begin() + len_2); n   rhs_inv.dft(len), q.dft(len); n   for (size_type i = 0; i != len; ++i) n     *(q._Start + i) = n         static_cast<cast_type>(*(q._Start + i)) * *(rhs_inv._Start + i) % P; n   q.idft(len).resize(len_2).resize(size()); n   Poly q_cpy(q); n   q_cpy.dft(len), rhs_cpy.dft(len); n   for (size_type i = 0; i != len; ++i) n     *(q_cpy._Start + i) = n         static_cast<cast_type>(*(q_cpy._Start + i)) * *(rhs_cpy._Start + i) % P; n   q_cpy.idft(len); n   std::memset(q_cpy.begin(), 0, sizeof(value_type) * len_2); n   for (size_type i = len_2; i < size() && i != len; ++i) n     *(q_cpy._Start + i) = (*(q_cpy._Start + i) + P - this->operator[](i)) % P; n   q_cpy.dft(len); n   for (size_type i = 0; i != len; ++i) n     *(q_cpy._Start + i) = n         static_cast<cast_type>(*(q_cpy._Start + i)) * *(rhs_inv._Start + i) % P; n   q_cpy.idft(len); n   for (iterator i = q_cpy.begin() + len_2, e = q_cpy.begin() + size(); i != e; n        ++i) n     if (*i != 0) *i = P - *i; n   std::memcpy(q.begin() + len_2, q_cpy.begin() + len_2, n               sizeof(value_type) * (size() - len_2)); n   return q; n } n Poly Poly::log() const { n   assert(*begin() == 1); n   return deriv().quo(*this).integr(); n } n Poly Poly::exp() const { n   assert(*begin() == 0); n   if (size() == 1) return {1}; n   size_type len = _get_len(size()); n   Poly res(len), res_inv(len), der(deriv()), g0(len); n   res.operator=(Poly{1, this->operator[](1)}), res_inv.operator=(res.inv()), n   g0.operator=(res); n   g0.dft(4); n   for (size_type i = 4; i <= len; i <<= 1) { n     Poly h0(res_inv), g0p(res.deriv()), n         f0p(der.begin(), der.begin() + (i >> 1) - 1), g0f0p(i); n     h0.dft(i), f0p.dft(i >> 1); n     for (size_type j = 0; j != i >> 1; ++j) n       *(g0f0p._Start + j) = n           static_cast<cast_type>(*(g0._Start + j)) * *(f0p._Start + j) % P; n     g0f0p.idft(i >> 1).resize(i); n     for (size_type j = 0; j != (i >> 1) - 1; ++j) n       *(g0f0p._Start + j) = (*(g0f0p._Start + j) + P - *(g0p._Start + j)) % P; n     std::memcpy(g0f0p.begin() + (i >> 1), g0f0p.begin(), n                 sizeof(value_type) * ((i >> 1) - 2)); n     std::memset(g0f0p.begin(), 0, sizeof(value_type) * ((i >> 1) - 2)); n     for (iterator j = g0f0p.begin() + ((i >> 1) - 1), e = g0f0p.end(); j != e; n          ++j) n       if (*j != 0) *j = P - *j; n     g0f0p.dft(i); n     for (size_type j = 0; j != i; ++j) n       *(g0f0p._Start + j) = n           static_cast<cast_type>(*(g0f0p._Start + j)) * *(h0._Start + j) % P; n     g0f0p.idft(i); n     Poly imf(g0f0p.resize(i - 1).integr()); n     std::memset(imf.begin(), 0, sizeof(value_type) * (i >> 1)); n     for (size_type j = i >> 1; j != i; ++j) n       *(imf._Start + j) = (*(imf._Start + j) + P - this->operator[](j)) % P; n     imf.dft(i); n     for (size_type j = 0; j != i; ++j) n       *(imf._Start + j) = n           static_cast<cast_type>(*(imf._Start + j)) * *(g0._Start + j) % P; n     imf.idft(i), res.resize(i); n     for (iterator j = imf.begin() + (i >> 1), e = imf.end(); j != e; ++j) n       if (*j != 0) *j = P - *j; n     std::memcpy(res.begin() + (i >> 1), imf.begin() + (i >> 1), n                 sizeof(value_type) * (i >> 1)); n     if (i != len) { n       g0.operator=(res).dft(i << 1); n       Poly g0_half(i); n       for (size_type j = 0; j != i; ++j) n         *(g0_half._Start + j) = n             static_cast<cast_type>(*(g0._Start + j)) * *(h0._Start + j) % P; n       g0_half.idft(i); n       std::memset(g0_half.begin(), 0, sizeof(value_type) * (i >> 1)); n       g0_half.dft(i); n       for (size_type j = 0; j != i; ++j) n         *(g0_half._Start + j) = static_cast<cast_type>(*(g0_half._Start + j)) * n                                 *(h0._Start + j) % P; n       g0_half.idft(i), res_inv.resize(i); n       for (iterator j = g0_half.begin() + (i >> 1), e = g0_half.end(); j != e; n            ++j) n         if (*j != 0) *j = P - *j; n       std::memcpy(res_inv.begin() + (i >> 1), g0_half.begin() + (i >> 1), n                   sizeof(value_type) * (i >> 1)); n     } n   } n   return res.resize(size()); n } n Poly Poly::sqrt() const { n   assert(_legendre_symbol(*begin()) != 0); n   size_type len = _get_len(size()); n   Poly res(len), res_inv(len), g0(len); n   res.operator=(Poly{_tonelli_shanks(*begin())}), n   res_inv.operator=(Poly{_mod_pow(res.operator[](0), -1)}), g0.operator=(res); n   g0.dft(2); n   const value_type inv2 = P - (P - 1) / 2; n   for (size_type i = 2; i <= len; i <<= 1) { n     Poly g0_2(i), res_inv_cpy(res_inv); n     for (size_type j = 0; j != i >> 1; ++j) n       *(g0_2._Start + j) = n           (static_cast<cast_type>(*(g0._Start + j)) * *(g0._Start + j)) % P; n     g0_2.idft(i >> 1).resize(i); n     for (size_type j = 0; j != i >> 1; ++j) n       *(g0_2._Start + j) = (*(g0_2._Start + j) + P - this->operator[](j)) % P; n     std::memcpy(g0_2.begin() + (i >> 1), g0_2.begin(), n                 sizeof(value_type) * ((i >> 1) - 1)); n     std::memset(g0_2.begin(), 0, sizeof(value_type) * ((i >> 1) - 1)); n     for (size_type j = i >> 1; j != i; ++j) n       *(g0_2._Start + j) = (*(g0_2._Start + j) + P - this->operator[](j)) % P; n     g0_2.dft(i), res_inv_cpy.dft(i); n     for (size_type j = 0; j != i; ++j) n       *(g0_2._Start + j) = static_cast<cast_type>(*(g0_2._Start + j)) * n                            *(res_inv_cpy._Start + j) % P; n     g0_2.idft(i), res.resize(i); n     for (iterator j = g0_2.begin() + (i >> 1), e = g0_2.end(); j != e; ++j) n       if (*j != 0) *j = static_cast<cast_type>(P - *j) * inv2 % P; n     std::memcpy(res.begin() + (i >> 1), g0_2.begin() + (i >> 1), n                 sizeof(value_type) * (i >> 1)); n     if (i != len) { n       g0.operator=(res).dft(i); n       for (size_type j = 0; j != i; ++j) n         *(g0_2._Start + j) = static_cast<cast_type>(*(g0._Start + j)) * n                              *(res_inv_cpy._Start + j) % P; n       g0_2.idft(i); n       std::memset(g0_2.begin(), 0, sizeof(value_type) * (i >> 1)); n       g0_2.dft(i); n       for (size_type j = 0; j != i; ++j) n         *(g0_2._Start + j) = static_cast<cast_type>(*(g0_2._Start + j)) * n                              *(res_inv_cpy._Start + j) % P; n       g0_2.idft(i), res_inv.resize(i); n       for (iterator j = g0_2.begin() + (i >> 1), e = g0_2.end(); j != e; ++j) n         if (*j != 0) *j = P - *j; n       std::memcpy(res_inv.begin() + (i >> 1), g0_2.begin() + (i >> 1), n                   sizeof(value_type) * (i >> 1)); n     } n   } n   return res.resize(size()); n } n void Poly::segment_tree_build(std::vector<Poly> &vp, size_type pos, v_it left, n                               v_it right) { n   if (right - left == 1) { n     vp[pos] = Poly{*left == 0 ? 0 : P - *left, 1}; n     return; n   } n   v_it mid = left + (right - left >> 1); n   segment_tree_build(vp, pos << 1, left, mid), n       segment_tree_build(vp, pos << 1 | 1, mid, right); n   vp[pos] = vp[pos << 1] * vp[pos << 1 | 1]; n } n void Poly::segment_tree_solve(const std::vector<Poly> &vp, size_type pos, n                               v_it left, v_it right, const Poly &r, n                               std::vector<value_type> &res) { n   if (right - left <= 128) { n     for (v_it it = left; it != right; ++it) res.push_back(r.horner(*it)); n     return; n   } n   v_it mid = left + (right - left >> 1); n   segment_tree_solve(vp, pos << 1, left, mid, r % vp[pos << 1], res), n       segment_tree_solve(vp, pos << 1 | 1, mid, right, r % vp[pos << 1 | 1], n                          res); n } n std::vector<Poly::value_type> Poly::eval( n     const std::vector<value_type> &points) const { n   std::vector<Poly> vp(points.size() << 2, Poly{0}); n   segment_tree_build(vp, 1, points.begin(), points.end()); n   std::vector<value_type> res; n   segment_tree_solve(vp, 1, points.begin(), points.end(), *this % vp[1], res); n   return res; n } n Poly Poly::inter(const std::vector<value_type> &x, n                  const std::vector<value_type> &y) { n   size_type n = x.size(); n   assert(y.size() == n); n   std::vector<Poly> vp(n << 2, Poly{0}); n   segment_tree_build(vp, 1, x.begin(), x.end()); n   std::vector<value_type> v; n   segment_tree_solve(vp, 1, x.begin(), x.end(), vp[1].deriv() % vp[1], v); n   for (size_type i = 0; i != n; ++i) n     v[i] = _mod_pow(v[i], -1) * static_cast<cast_type>(y[i]) % P; n   std::function<Poly(size_type, v_it, v_it)> inter_rec = n       [&inter_rec, &vp](size_type pos, v_it left, v_it right) { n         if (right - left == 1) return Poly{*left}; n         return inter_rec(pos << 1, left, left + (right - left >> 1)) * n                    vp[pos << 1 | 1] + n                inter_rec(pos << 1 | 1, left + (right - left >> 1), right) * n                    vp[pos << 1]; n       }; n   return inter_rec(1, v.begin(), v.end()); n } n }  // namespace test n using test::Poly; n using namespace std; n using ll = long long; n using ull = unsigned long long; n const int N = 2e5 + 5; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int t; n   Poly a(m + 1); n   for (int i = 0; i < n; ++i) { n     cin >> t; n     if (t <= m) a[t] = 1; n   } n   Poly ans = Poly{2} * (Poly{1} + (Poly{1} - Poly{4} * a).sqrt()).inv(); n   for (int i = 1; i != m + 1; ++i) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 505, MOD = 1e9 + 7; n struct node { n   node *ch[20], *fail; n   int cnt, id; n   node(node* son = NULL) { n     for (int i = 0; i <= 19; i++) ch[i] = son; n     fail = son; n     cnt = id = 0; n   } n } nil, *null = &nil, *root = null, *nd[maxn]; n typedef node* P_node; n queue<P_node> que; n void Build() { n   root->fail = root; n   root->id = 0; n   nd[0] = root; n   for (int i = 0; i <= 19; i++) n     if (root->ch[i] != null) { n       P_node v = root->ch[i]; n       que.push(v); n       v->fail = root; n     } else n       root->ch[i] = root; n   while (!que.empty()) { n     P_node p = que.front(); n     que.pop(); n     for (int i = 0; i <= 19; i++) n       if (p->ch[i] != null) { n         P_node v = p->ch[i]; n         que.push(v); n         v->fail = p->fail->ch[i]; n         v->cnt += v->fail->cnt; n       } else n         p->ch[i] = p->fail->ch[i]; n   } n } n int a[maxn], b[maxn], c[maxn], n, m, K, tot; n int f[2][205][705][2][2]; n int Calc(int a[]) { n   memset(f, 0, sizeof(f)); n   f[0][0][0][0][0] = 1; n   for (int i = 0, o = 0; i <= a[0] - 1; i++, o ^= 1) { n     memset(f[o ^ 1], 0, sizeof(f[o ^ 1])); n     for (int j = 0; j <= tot; j++) n       for (int k = 0; k <= K; k++) n         for (int t1 = 0; t1 <= 1; t1++) n           for (int t2 = 0; t2 <= 1; t2++) n             if (f[o][j][k][t1][t2]) { n               for (int t = 0; t <= (t2 ? m - 1 : a[i + 1]); t++) { n                 int v = (t1 | (t > 0)) ? nd[j]->ch[t]->id : 0; n                 (f[o ^ 1][v][k + nd[v]->cnt][t1 | (t > 0)] n                   [t2 | (t < a[i + 1])] += f[o][j][k][t1][t2]) %= MOD; n               } n             } n   } n   int res = 0; n   for (int i = 0; i <= tot; i++) n     for (int j = 0; j <= K; j++) n       for (int k = 0; k <= 1; k++) n         for (int t = 0; t <= 1; t++) (res += f[a[0] & 1][i][j][k][t]) %= MOD; n   return res; n } n int main() { n   scanf( %d%d%d , &n, &m, &K); n   scanf( %d , &a[0]); n   for (int i = 1; i <= a[0]; i++) scanf( %d , &a[i]); n   scanf( %d , &b[0]); n   for (int i = 1; i <= b[0]; i++) scanf( %d , &b[i]); n   a[a[0]]--; n   for (int i = a[0]; i >= 1; i--) n     if (a[i] < 0) a[i] += m, a[i - 1]--; n   nil = node(null); n   for (int i = 1; i <= n; i++) { n     int t, x; n     scanf( %d , &t); n     P_node now = root; n     while (t--) { n       scanf( %d , &x); n       if (now->ch[x] == null) { n         now = now->ch[x] = new node(null); n         now->id = ++tot; n         nd[tot] = now; n       } else n         now = now->ch[x]; n     } n     scanf( %d , &x); n     now->cnt += x; n   } n   Build(); n   printf( %d n , ((Calc(b) - Calc(a)) % MOD + MOD) % MOD); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n int sp[N]; n int dp[N]; n int main() { n   ios_base::sync_with_stdio(0); n   const int N = 1e6 + 1; n   int n; n   cin >> n; n   int a[n + 1]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   a[n] = N; n   for (int i = 2; i < N; i++) { n     sp[i] = i; n   } n   for (long long i = 2; i * i < N; i++) { n     if (sp[i] == i) { n       for (long long j = i * i; j < N; j += i) { n         if (sp[j] == j) { n           sp[j] = i; n         } n       } n     } n   } n   int ans = 0; n   for (int i = 1, j = 0; i < N; i++) { n     int num = i; n     int maxi = 0; n     while (num > 1) { n       maxi = max(maxi, dp[i / sp[num]]); n       num /= sp[num]; n     } n     dp[i] = maxi; n     if (i == a[j]) { n       dp[i]++; n       j++; n     } n     ans = max(ans, dp[i]); n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n struct seg { n   int v[N * 4], tp; n   void build(int k, int l, int r, int *a) { n     if (l == r) return v[k] = a[l], void(0); n     int mid = (l + r) / 2; n     build(k * 2, l, mid, a); n     build(k * 2 + 1, mid + 1, r, a); n     v[k] = (tp ? min(v[k * 2], v[k * 2 + 1]) : max(v[k * 2], v[k * 2 + 1])); n   } n   int ask(int k, int l, int r, int x, int y) { n     if (l == x && r == y) return v[k]; n     int mid = (l + r) / 2; n     if (y <= mid) return ask(k * 2, l, mid, x, y); n     if (x > mid) return ask(k * 2 + 1, mid + 1, r, x, y); n     return tp ? min(ask(k * 2, l, mid, x, mid), n                     ask(k * 2 + 1, mid + 1, r, mid + 1, y)) n               : max(ask(k * 2, l, mid, x, mid), n                     ask(k * 2 + 1, mid + 1, r, mid + 1, y)); n   } n } mn[20], mx[20]; n int r[N], n, id[N]; n int L[20][N]; n int R[20][N]; n int main() { n   scanf( %d , &n); n   for (int i = (int)(1); i <= (int)(n); ++i) n     scanf( %d , &r[i]), r[i + n] = r[i + n + n] = r[i]; n   for (int i = (int)(1); i <= (int)(3 * n); ++i) n     L[0][i] = max(i - r[i], 1), R[0][i] = min(i + r[i], 3 * n); n   for (int i = (int)(1); i <= (int)(18); ++i) { n     mn[i - 1].tp = 1; n     mx[i - 1].tp = 0; n     mn[i - 1].build(1, 1, 3 * n, L[i - 1]); n     mx[i - 1].build(1, 1, 3 * n, R[i - 1]); n     for (int j = (int)(1); j <= (int)(3 * n); ++j) { n       L[i][j] = mn[i - 1].ask(1, 1, 3 * n, L[i - 1][j], R[i - 1][j]); n       R[i][j] = mx[i - 1].ask(1, 1, 3 * n, L[i - 1][j], R[i - 1][j]); n     } n   } n   mn[18].tp = 1; n   mx[18].tp = 0; n   mn[18].build(1, 1, 3 * n, L[18]); n   mx[18].build(1, 1, 3 * n, R[18]); n   for (int i = (int)(1); i <= (int)(n); ++i) { n     int l = n + i, r = n + i, ans = 0; n     for (int j = (int)(18); j >= (int)(0); --j) { n       int L = mn[j].ask(1, 1, 3 * n, l, r); n       int R = mx[j].ask(1, 1, 3 * n, l, r); n       if (R - L + 1 < n) l = L, r = R, ans += (1 << j); n     } n     printf( %d  , ans + ((r - l + 1) < n)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, c = 0; n   cin >> n >> x; n   int j = 1; n   for (int i = 0; i < n; i++) { n     int r, l; n     cin >> r >> l; n     c += l - r + 1; n     while (j + x <= r) { n       j += x; n     } n     c += r - j; n     j = l + 1; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long NMAX = 1000; n pair<long long, long long> func[NMAX + 5]; n pair<long long, long long> tmp_func[NMAX + 5]; n long long initial[NMAX + 5]; n pair<long long, long long> ans[NMAX + 5]; n map<pair<long long, long long>, long long> memo; n int cnt_queries = 0; n long long singular_query(long long id, long long wh) { n   if (memo.count({id, wh}) == 0) { n     cout <<  ?   << id <<     << wh << endl; n     cin >> memo[{id, wh}]; n     cnt_queries++; n   } n   return memo[{id, wh}]; n } n void query_fun(long long fst, long long lst, long long wh) { n   for (long long i = fst; i <= lst; i++) { n     func[i].first = singular_query(func[i].second, wh); n   } n } n void solve(long long fst, long long lst, long long l, long long r, n            long long buck) { n   if (fst == lst) { n     ans[func[fst].second] = {l, r}; n     return; n   } n   long long target_split = (lst + fst) / 2; n   long long st = l + 1LL * (target_split - fst + 1) * buck - 1, n             dr = r - 1LL * (lst - target_split) * buck + 1; n   while (dr - st > 1) { n     long long mid = (st + dr) / 2; n     for (long long i = fst; i <= lst; i++) { n       map<pair<long long, long long>, long long>::iterator it = n           memo.lower_bound({func[i].second, mid}); n       if (it != memo.end() && it->first.first == func[i].second && n           it->second - initial[func[i].second] < n               buck * (target_split - fst + 1)) { n         func[i].first = it->second; n       } else if (it != memo.begin()) { n         it--; n         if (it->first.first == func[i].second && n             it->second - initial[func[i].second] >= n                 buck * (target_split - fst + 1)) { n           func[i].first = it->second; n         } else { n           func[i].first = singular_query(func[i].second, mid); n         } n       } else { n         func[i].first = singular_query(func[i].second, mid); n       } n     } n     sort(func + fst, func + 1 + lst, n          [&](pair<long long, long long> &a, pair<long long, long long> &b) { n            return a.first - initial[a.second] > b.first - initial[b.second]; n          }); n     if (func[target_split].first - initial[func[target_split].second] >= n         buck * (target_split - fst + 1)) { n       dr = mid; n       for (long long i = fst; i <= lst; i++) { n         tmp_func[i] = func[i]; n       } n     } else { n       st = mid; n     } n   } n   for (long long i = fst; i <= lst; i++) { n     func[i] = tmp_func[i]; n   } n   for (int i = target_split + 1; i <= lst; i++) { n     initial[func[i].second] += buck * (target_split - fst + 1); n   } n   solve(fst, target_split, l, dr, buck); n   solve(target_split + 1, lst, dr, r, buck); n } n int main() { n   long long n; n   long long l; n   scanf( %lld %lld , &n, &l); n   for (long long i = 1; i <= n; i++) { n     func[i] = {0, i}; n     memo[{i, 0}] = 0; n     memo[{i, 1e18}] = l; n   } n   solve(1, n, 0, 1e18, l / n); n   cout <<  !  << endl; n   for (long long i = 1; i <= n; i++) { n     cout << ans[i].first <<     << ans[i].second << endl; n   } n   cerr <<  total queries   << cnt_queries << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n const int maxn = 1e6+5; n const int mod = 1e9+7; n const int inf = 0x3f3f3f3f; n #define for1(i,n) for(int i=1;i<=n;i++) n #define forn(i,n) for(int i=0;i<n;i++) n  n int a[maxn], n,m; n ll add[maxn]; n vector<ll> ans; n void dfs(int L, int R){ n     int mid = (a[L]+a[R])>>1; n     int idx = upper_bound(a+L,a+R+1,mid) - a;     n     //printf( [%d %d], [%d %d] n ,L,idx-1,idx,R); n     //printf( [%d %d] n ,L,R); n     ans.push_back(add[R]-add[L-1]); n     //ans.push_back(add[R]-add[idx-1]); n     if(idx>R) return; n     if(L<=idx-1) dfs(L,idx-1);  n     if(idx<=R) dfs(idx,R); n } n int main(){ n     int t; scanf( %d ,&t);while(t--){ n         ans.clear();      n         scanf( %d%d ,&n,&m); n         for1(i,n){ n             scanf( %d ,&a[i]); n         } n         sort(a+1,a+n+1); n         for1(i,n) add[i] = add[i-1] + a[i]; n         //int L=1, R=n; n         dfs(1,n); n         //for(auto v : ans) cout << v << endl; n         sort(ans.begin(),ans.end()); n         for1(i,m){ n             int q; scanf( %d ,&q); n             if(binary_search(ans.begin(),ans.end(),q)) printf( Yes n ); n             else printf( No n ); n         } n     } n } n /*2 n 5 5 n 1 2 3 4 5 n 1 n 8 n 9 n 12 n 6 n 5 5 n 3 1 3 1 3 n 1 n 2 n 3 n 9 n 11 n */


#include <bits/stdc++.h> n using namespace std; n int n; n string s1, s2; n int main() { n   cin >> n; n   if (n == 1) { n     cout <<  a na nb nb ; n     return 0; n   } n   s1 = s2 =   ; n   int m = n; n   while (m > 1) { n     if (s1 ==    || s1[s1.size() - 1] ==  b ) n       s1 +=  aa ; n     else n       s1 +=  bb ; n     m -= 2; n   } n   if (m) s1 +=  e ; n   cout << s1 << endl; n   for (char i : s1) { n     if (i ==  a ) n       cout <<  c ; n     else if (i ==  b ) n       cout <<  d ; n     else n       cout <<  e ; n   } n   s2 =  z ; n   n -= 1; n   cout << endl; n   while (n > 1) { n     if (s2[s2.size() - 1] ==  z  || s2[s2.size() - 1] ==  x ) n       s2 +=  yy ; n     else n       s2 +=  xx ; n     n -= 2; n   } n   if (n) s2 +=  k ; n   cout << s2 << endl; n   for (char i : s2) { n     if (i ==  x ) n       cout <<  y ; n     else if (i ==  y ) n       cout <<  x ; n     else n       cout << i; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int s[1005]; n int us[1005]; n int t[1005]; n int ut[1005]; n int a[1005]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &s[i]); n     us[s[i]]++; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &t[i]); n     ut[t[i]]++; n   } n   int t1 = 0; n   for (int i = 1; i <= n; i++) { n     if (us[i] == 0) { n       t1 = i; n     } n   } n   vector<int> dif; n   for (int i = 0; i < n; i++) { n     a[i] = s[i]; n     if (us[s[i]] == 2) { n       dif.push_back(i); n     } n   } n   int tmp; n   for (auto i : dif) { n     tmp = a[i]; n     a[i] = t1; n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (a[j] != t[j]) cnt++; n     } n     if (cnt == 1) { n       for (int j = 0; j < n; j++) { n         printf( %d%c , a[j], (   n [j == n - 1])); n       } n       return 0; n     } n     a[i] = tmp; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const double PI = atan(1.0) * 4; n const int inf = 2147483647 / 2; n const int maxn = 207; n const int maxs = 20; n const int mod = 1000000007; n template <class T> n bool chmin(T &a, const T &b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool chmax(T &a, const T &b) { n   return b > a ? a = b, 1 : 0; n } n template <class T> n int sgn(T first) { n   return (first > eps) - (first < -eps); n } n int M, K; n int dig_l[205], dig_r[205]; n int d[205][205][505]; n inline void add(int &a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n struct nod { n   nod *ch[maxs], *f; n   nod *jump[maxs]; n   int v; n   nod *go(int c); n } * bat; n nod *nod::go(int c) { n   if (ch[c] == 0) n     ch[c] = bat++, memset(ch[c]->ch, 0, sizeof(ch[c]->ch)), ch[c]->v = 0; n   return ch[c]; n } n struct AC { n   nod an[maxn], *rt, *q[maxn]; n   void newnod(nod *&o) { n     o = bat++; n     memset(o->ch, 0, sizeof(o->ch)); n     o->v = 0; n   } n   void init() { n     bat = an; n     newnod(rt); n   } n   int idx(nod *first) { return first - an; } n   void insert(int *s, int n, int v) { n     nod *first = rt; n     for (int i = (0); i <= (int)(n - 1); i++) first = first->go(s[i]); n     first->v += v; n   } n   void build() { n     int ql = 0, qr = 0; n     q[qr++] = rt; n     while (ql != qr) { n       nod *first = q[ql++]; n       for (int c = (0); c <= (int)(maxs - 1); c++) { n         nod *v = first->ch[c]; n         if (!v) continue; n         nod *last = first->f; n         while (last && last->ch[c] == 0) last = last->f; n         if (last) n           v->f = last->ch[c]; n         else n           v->f = rt; n         q[qr++] = v; n       } n     } n     for (int i = (0); i <= (int)(maxs - 1); i++) n       rt->jump[i] = rt->ch[i] ? rt->ch[i] : rt; n     for (int i = (1); i <= (int)(qr - 1); i++) { n       nod *first = q[i]; n       for (int j = (0); j <= (int)(maxs - 1); j++) n         if (first->ch[j]) n           first->jump[j] = first->ch[j]; n         else n           first->jump[j] = first->f->jump[j]; n     } n     for (int i = (0); i <= (int)(qr - 1); i++) { n       nod *first = q[i]; n       if (first->f) first->v += first->f->v; n     } n   } n   int dfs(int p, nod *u, int k, bool first, bool sl, bool sr) { n     if (p == -1) return 1; n     int safe = !(sl || sr); n     int s = idx(u); n     if (safe && ~d[p][s][k]) return d[p][s][k]; n     int ret = 0; n     for (int i = (0); i <= (int)(M - 1); i++) { n       if (sl && i < dig_l[p]) continue; n       if (sr && i > dig_r[p]) continue; n       nod *v = u; n       if (!first || i) v = u->jump[i]; n       if (k + v->v <= K) n         add(ret, dfs(p - 1, v, k + v->v, first && (!i), sl && i == dig_l[p], n                      sr && i == dig_r[p])); n     } n     return safe ? d[p][s][k] = ret : ret; n   } n } gao; n int td[205]; n int main() { n   int n; n   while (~scanf( %d%d%d , &n, &M, &K)) { n     int l1, l2; n     scanf( %d , &l1); n     for (int i = (l1 - 1); i >= (int)(0); i--) scanf( %d , dig_l + i); n     scanf( %d , &l2); n     for (int i = (l2 - 1); i >= (int)(0); i--) scanf( %d , dig_r + i); n     while (l1 < l2) dig_l[l1++] = 0; n     gao.init(); n     for (int i = (0); i <= (int)(n - 1); i++) { n       int l, val; n       scanf( %d , &l); n       for (int j = (0); j <= (int)(l - 1); j++) scanf( %d , td + j); n       scanf( %d , &val); n       gao.insert(td, l, val); n     } n     gao.build(); n     memset(d, -1, sizeof(d)); n     printf( %d n , gao.dfs(l2 - 1, gao.rt, 0, 1, 1, 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int INF = 0x3f3f3f3f; n const int iinf = 1 << 30; n const long long linf = 2e18; n const int MOD = 1000000007; n void print(int x) { n   cout << x << endl; n   exit(0); n } n void PRINT(string x) { n   cout << x << endl; n   exit(0); n } n void douout(double x) { printf( %lf n , x + 0.0000000001); } n char s[N], t[N]; n int nxt[N], val[N], f[N], sum[N]; n int n, m; n signed main() { n   scanf( %s , s + 1); n   scanf( %s , t + 1); n   n = strlen(s + 1); n   m = strlen(t + 1); n   nxt[1] = 0; n   for (int i = 2, j = 0; i <= m; i++) { n     while (j && t[i] != t[j + 1]) j = nxt[j]; n     if (t[i] == t[j + 1]) j++; n     nxt[i] = j; n   } n   for (int i = 1, j = 0; i <= n; i++) { n     while (j && s[i] != t[j + 1]) j = nxt[j]; n     if (s[i] == t[j + 1]) j++; n     if (j == m) { n       val[i] = i - m + 1; n       j = nxt[j]; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!val[i]) val[i] = val[i - 1]; n   for (int i = 1; i <= n; i++) { n     f[i] = f[i - 1]; n     if (val[i]) f[i] = (f[i] + sum[val[i] - 1] + val[i]) % MOD; n     sum[i] = (sum[i] + sum[i - 1] + f[i]) % MOD; n   } n   printf( %d n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, a[15], ans, b[10010]; n char s[15][10010]; n struct P { n   int val, id; n } p[10010]; n inline bool cmp(P x, P y) { return x.val < y.val; } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &m); n     ans = 0; n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     for (int i = 0; i < n; i++) scanf( %s , s[i] + 1); n     for (int i = 0; i < 1 << n; i++) { n       int sum = 0; n       for (int j = 1; j <= m; j++) { n         p[j].val = 0; n         p[j].id = j; n       } n       for (int j = 0; j < n; j++) { n         if (i & (1 << j)) { n           sum += a[j]; n           for (int k = 1; k <= m; k++) n             if (s[j][k] ==  1 ) p[k].val--; n         } else { n           sum -= a[j]; n           for (int k = 1; k <= m; k++) n             if (s[j][k] ==  1 ) p[k].val++; n         } n       } n       sort(p + 1, p + m + 1, cmp); n       for (int j = 1; j <= m; j++) sum += j * p[j].val; n       if (sum >= ans) { n         ans = sum; n         for (int j = 1; j <= m; j++) b[p[j].id] = j; n       } n     } n     for (int i = 1; i <= m; i++) printf( %d  , b[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int W, H; n   while (cin >> W >> H) { n     vector<pair<int, int>> pset; n     for (int dy = 0; dy < (int)(2); ++dy) n       for (int dx = 0; dx < (int)(2); ++dx) { n         if (dx <= W && dy <= H) pset.push_back(pair<int, int>(dx, dy)); n       } n     for (int dy = 0; dy < (int)(2); ++dy) n       for (int dx = 0; dx < (int)(2); ++dx) { n         if (dx <= W && H - dy >= 0) pset.push_back(pair<int, int>(dx, H - dy)); n       } n     for (int dy = 0; dy < (int)(2); ++dy) n       for (int dx = 0; dx < (int)(2); ++dx) { n         if (W - dx >= 0 && dy <= H) pset.push_back(pair<int, int>(W - dx, dy)); n       } n     for (int dy = 0; dy < (int)(2); ++dy) n       for (int dx = 0; dx < (int)(2); ++dx) { n         if (W - dx >= 0 && H - dy >= 0) n           pset.push_back(pair<int, int>(W - dx, H - dy)); n       } n     sort(pset.begin(), pset.end()); n     pset.erase(unique(pset.begin(), pset.end()), pset.end()); n     vector<pair<double, vector<pair<int, int>>>> ans; n     for (int comb = (1 << 4) - 1; comb < (1 << pset.size());) { n       vector<pair<int, int>> ps; n       for (int i = 0; i < (int)(pset.size()); ++i) n         if (comb >> i & 1) { n           ps.push_back(pset[i]); n         } n       sort(ps.begin(), ps.end()); n       do { n         double len = 0; n         for (int i = 0; i < (int)(3); ++i) { n           len += hypot(ps[i].first - ps[i + 1].first, n                        ps[i].second - ps[i + 1].second); n         } n         ans.push_back(make_pair(len, ps)); n       } while (next_permutation(ps.begin(), ps.end())); n       int x = comb & -comb, y = comb + x; n       comb = ((comb & ~y) / x >> 1) | y; n     } n     sort(ans.begin(), ans.end()); n     vector<pair<int, int>> ps = ans.back().second; n     for (int i = 0; i < (int)(4); ++i) { n       cout << ps[i].first <<     << ps[i].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(vector<int> v) { n   size_t sz = v.size(); n   for (int i : v) { n     cout << i <<    ; n   } n } n vector<int> a, b, c; n int main(void) { n   int n, m; n   scanf( %d , &n); n   a.resize(n); n   int x; n   for (unsigned i = 0; i < n; i++) { n     scanf( %d , &x); n     a[i] = x; n     c.push_back(x); n   } n   scanf( %d , &m); n   b.resize(m); n   for (unsigned i = 0; i < m; i++) { n     scanf( %d , &x); n     b[i] = x; n     c.push_back(x); n   } n   int a1 = 0, a2 = 0; n   int ans = INT_MIN; n   int co = m + n; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   sort(c.begin(), c.end()); n   c.push_back(c[co - 1] + 1); n   co++; n   int i = 0; n   for (i = 0; i < co; i++) { n     int p1, p2 = 0; n     int val = c[i] - 1; n     int l = 0, h = n - 1, mid; n     while (l <= h) { n       mid = (l + h) / 2; n       if (a[mid] <= val) n         l = mid + 1; n       else n         h = mid - 1; n     } n     p1 = 3 * (n - l) + 2 * l; n     l = 0; n     h = m - 1; n     while (l <= h) { n       mid = (l + h) / 2; n       if (b[mid] <= val) n         l = mid + 1; n       else n         h = mid - 1; n     } n     p2 = 3 * (m - l) + 2 * l; n     if ((p1 - p2) > ans) { n       ans = p1 - p2; n       a1 = p1; n       a2 = p2; n     } else if ((p1 - p2) == ans) { n       if (p1 > a1) { n         a1 = p1; n         a2 = p2; n       } n     } n   } n   printf( %d:%d , a1, a2); n } n


#include <bits/stdc++.h> n int main() { n   int count; n   std::vector<int> visitors; n   std::cin >> count; n   for (int i = 0; i < 2 * count; ++i) { n     int value; n     std::cin >> value; n     visitors.push_back(value); n   } n   int counter = 0; n   for (int i = 0; i < 2 * count; i += 2) { n     if (visitors[i] != visitors[i + 1]) { n       for (int j = 2 * count - 1; j > i + 1; j--) { n         if (visitors[j] == visitors[i]) { n           for (int k = j; k > i + 1; k--) { n             std::swap(visitors[k], visitors[k - 1]); n             counter++; n           } n           break; n         } n       } n     } n   } n   std::cout << counter << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<vector<int>> v(n); n   int mat[4][4] = { n       {8, 9, 1, 13}, {3, 12, 7, 5}, {0, 2, 4, 11}, {6, 10, 15, 14}}; n   int ans[n][n]; n   for (int i = 0; i < n * n; i++) { n     int x = i / n; n     v[x].push_back(i); n   } n   int count = 0; n   for (int i = 0; i < n; i += 4) { n     for (int j = 0; j < n; j += 4) { n       for (int k = i; k < i + 4; k++) { n         for (int l = j; l < j + 4; l++) { n           ans[k][l] = count++; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (j == 0) n         cout << ans[i][j]; n       else n         cout <<     << ans[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int A[maxn], MAX = 0; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> A[i]; n   sort(A, A + n); n   int ans = 0; n   for (int i = 0; i < n; i++) n     if (ans * A[i] + ans <= i) ans++; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q, i, s1, s[100010], a[100010], k[100010], t, at, dp, sum, now; n bool cmp(long long a, long long b) { return a > b; } n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   cin >> q; n   for (i = 0; i < q; i++) { n     cin >> k[i]; n   } n   sort(a, a + n, cmp); n   s[0] = 0; n   for (i = 0; i < n; i++) { n     s[i + 1] = s[i] + a[i]; n   } n   s1 = 0; n   for (i = 0; i < n; i++) { n     s1 += a[i] * i; n   } n   for (i = 0; i < q; i++) { n     if (i != 0) cout <<    ; n     if (k[i] == 1) { n       cout << s1; n       continue; n     } n     t = 1; n     dp = 1; n     now = k[i]; n     sum = 0; n     while (t + now < n) { n       at = s[t + now] - s[t]; n       at = at * dp; n       sum += at; n       t = t + now; n       now = now * k[i]; n       dp++; n     } n     at = s[n] - s[t]; n     at = at * dp; n     sum += at; n     cout << sum; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 5; n int arr[maxn][maxn]; n int main() { n   int m, n, k, p, sum = 0; n   bool bol = false; n   cin >> n >> m >> k; n   if (n % 2 != m % 2) { n     cout << 0 << endl; n     return 0; n   } n   if (m > n) { n     bol = true; n     swap(n, m); n   } n   for (int i = 0; i < k; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     if (bol) swap(x, y); n     arr[x - 1][y - 1] = z; n   } n   cin >> p; n   for (int i = 0, x, tmp; i < n; i++) { n     x = 0; n     tmp = 1; n     for (int j = 0; j < m; j++) { n       tmp *= arr[i][j]; n       if (arr[i][j]) x++; n     } n     if (x != m) n       sum += m - x - 1; n     else if (tmp != -1) { n       cout << 0 << endl; n       return 0; n     } n   } n   sum -= m - 1; n   int ans = 1; n   for (int i = 0; i < sum; i++) { n     ans *= 2; n     ans %= p; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long INF = 1e17, N = 2e5 + 1; n using namespace std; n int a[N], n, m, k, sum[N], t[N]; n int getime(int idx) { return min(idx, n - idx + 1); } n int getlen(int l, int r) { n   if (l > r) swap(l, r); n   int tmp = (r - l) / m; n   if ((r - l) % m != 0) tmp++; n   return tmp; n } n int f(int tmp) { n   for (int i = 1; i <= n; i++) { n     sum[i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     a[i] = t[i]; n   } n   int idx = 1; n   for (int i = (n / 2) - tmp + 1; i <= ((n + 1) / 2) + tmp; i++) { n     while (sum[i] != k && idx <= n) { n       if (a[idx] == 0 || getlen(idx, i) > getime(i)) { n         idx++; n       } else { n         int tmp = min(k - sum[i], a[idx]); n         a[idx] -= tmp; n         sum[i] += tmp; n       } n     } n     if (sum[i] != k) { n       return 0; n     } n   } n   return 1; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     cin >> t[i]; n   } n   int l = 0, r = n / 2; n   while (l != r) { n     int mid = (l + r + 1) >> 1; n     if (f(mid)) { n       l = mid; n     } else { n       r = mid - 1; n     } n   } n   cout << (n / 2) - l; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> tree; n void build(vector<long long> &arr, long long ind, long long l, long long r) { n   if (l == r) { n     tree[ind] = arr[l]; n     return; n   } n   long long mid = (l + r) / 2; n   build(arr, ind * 2, l, mid); n   build(arr, ind * 2 + 1, mid + 1, r); n   tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]); n } n long long getMax(long long ind, long long tl, long long tr, long long l, n                  long long r) { n   if (l > r) return 0; n   if (l == tl && r == tr) return tree[ind]; n   long long tm = (tl + tr) / 2; n   long long a = getMax(ind * 2, tl, tm, l, min(r, tm)); n   long long b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n   return max(a, b); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long count = 1; n   for (int i = n - 2; i >= 0; i--) { n     if (a[i] < a[i + 1]) { n       count++; n     } else { n       break; n     } n   } n   cout << n - count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[26] = {0}; n   string s; n   cin >> s; n   transform(s.begin(), s.end(), s.begin(), ::tolower); n   for (int i = 0; i < n; i++) arr[s[i] -  a ]++; n   for (int i = 0; i < 26; i++) { n     if (arr[i] == 0) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const double pi = acos(-1); n int n, i, j, k, t; n long double a[100004], b[100004], val[100003]; n int x[100004], c[100003]; n vector<int> adj[100005]; n int par[100003]; n bool bisa = true; n void dfs(int v, int p) { n   val[v] = b[v] - a[v]; n   for (int x : adj[v]) { n     if (x != p) { n       dfs(x, v); n       if (val[x] > 0) n         val[v] += val[x]; n       else n         val[v] += val[x] * c[x]; n     } n   } n   if (val[v] < 0 && fabsl(val[v]) > 1e17L / c[v]) { n     puts( NO ); n     exit(0); n   } n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %Lf , b + i); n   for (i = 1; i <= n; ++i) scanf( %Lf , a + i); n   for (i = 2; i <= n; ++i) { n     scanf( %d %d , x + i, c + i); n     adj[x[i]].push_back(i); n     adj[i].push_back(x[i]); n   } n   dfs(1, 1); n   puts(val[1] >= 0 ?  YES  :  NO ); n   return 0; n } n