#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n struct point { n   int first, second; n   point(int first = 0, int second = 0) : first(first), second(second) {} n   point operator-(point a) { return point(first - a.first, second - a.second); } n   long long operator^(point a) { n     return (long long)first * a.second - (long long)second * a.first; n   } n   long long operator*(point a) { n     return (long long)first * a.first + (long long)second * a.second; n   } n   void input() { scanf( %d%d , &first, &second); } n }; n int sgn(long long a) { return (a > 0) - (a < 0); } n int is_on(point a, point b, point c) { n   if (sgn((a - b) ^ (a - c))) return 0; n   return sgn((a - c) * (c - b)) > 0; n } n point ston[10], mons[1111]; n int flag[1111], id[10], n, k, runs, used; n vector<int> adj[10][1111]; n bool judge(int u) { n   if (used >= k) return false; n   flag[u] = runs; n   int shoot = id[used++]; n   for (int i = 0; i < adj[shoot][u].size(); i++) { n     int v = adj[shoot][u][i]; n     if (flag[v] != runs) { n       if (!judge(v)) return false; n     } n   } n   return true; n } n int main() { n   scanf( %d%d , &k, &n); n   for (int i = 0; i < k; i++) ston[i].input(); n   for (int i = 0; i < n; i++) mons[i].input(); n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       for (int p = 0; p < n; p++) n         if (p != j) { n           if (is_on(ston[i], mons[j], mons[p])) adj[i][j].push_back(p); n           if (adj[i][j].size() >= k) break; n         } n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < k; j++) id[j] = j; n     do { n       ++runs; n       used = 0; n       if (judge(i)) { n         ans++; n         break; n       } n     } while (next_permutation(id, id + k)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005, mod = 1000000007; n int ar[N]; n inline int lowbit(int x) { return x & (-x); } n void add(int i, int v) { n   for (; i < N; i += lowbit(i)) { n     ar[i] += v; n     if (ar[i] >= mod) { n       ar[i] %= mod; n     } n   } n } n int sum(int i) { n   int res = 0; n   for (; i > 0; i -= lowbit(i)) { n     res += ar[i]; n     if (res >= mod) res %= mod; n   } n   return res; n } n int main() { n   int n; n   scanf( %d , &n); n   memset(ar, 0, sizeof(ar)); n   for (int tp, i = 1; i <= n; i++) { n     scanf( %d , &tp); n     int s = sum(tp), v = ((long long)s * tp + tp) % mod, t = sum(tp - 1); n     t = v - s + t; n     if (t < 0) t += mod; n     if (t >= mod) t %= mod; n     add(tp, t); n   } n   printf( %d n , sum(N - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1000010]; n int main() { n   int n; n   scanf( %d , &n); n   long long sum = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a[i]); n     sum += a[i]; n   } n   if (sum <= 1) { n     printf( -1 n ); n     return 0; n   } n   long long ans = 9999999999999999; n   for (int i = 2; i * i <= sum; ++i) { n     if (sum % i) continue; n     long long cnt = 0, pre = 0; n     while (sum % i == 0) sum /= i; n     for (int j = 1; j <= n; ++j) { n       pre = (pre + a[j]) % i; n       cnt += min(pre, i - pre); n     } n     ans = min(ans, cnt); n   } n   if (sum > 1) { n     long long pre = 0, cnt = 0; n     for (int j = 1; j <= n; ++j) { n       pre = (pre + a[j]) % sum; n       cnt += min(pre, sum - pre); n     } n     ans = min(ans, cnt); n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000009; n map<string, int> mapa; n int n, m, k, a, parent[maxN], used[maxN], rozmiar[maxN], centr[maxN], q; n vector<int> graf[maxN]; n string s, s1; n void DFS(int start) { n   used[start] = 1; n   for (int i = 0; i < graf[start].size(); i++) { n     if (!used[graf[start][i]]) { n       DFS(graf[start][i]); n       rozmiar[start] += rozmiar[graf[start][i]]; n     } n   } n   rozmiar[start]++; n   for (int i = 0; i < graf[start].size(); i++) { n     if (graf[start][i] != parent[start]) { n       if (rozmiar[graf[start][i]] > rozmiar[start] / 2) { n         if (rozmiar[start] - rozmiar[centr[graf[start][i]]] <= n             rozmiar[start] / 2) n           centr[start] = centr[graf[start][i]]; n         else { n           int x = parent[centr[graf[start][i]]]; n           while (1) { n             if (rozmiar[start] - rozmiar[x] <= rozmiar[start] / 2) { n               centr[start] = x; n               break; n             } n             x = parent[x]; n           } n         } n       } n     } n   } n   if (centr[start] == 0) centr[start] = start; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &a); n     parent[i] = a; n     graf[i].push_back(a); n     graf[a].push_back(i); n   } n   DFS(1); n   for (int i = 0; i < q; i++) { n     scanf( %d , &a); n     printf( %d n , centr[a]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int a; n   cin >> a; n   string s; n   cin >> s; n   map<long long, long long> mp; n   long long ans = 0; n   for (int i = 0; i < s.size(); i++) { n     long long c = 0; n     for (int j = i; j < s.size(); j++) { n       c += (s[j] -  0 ); n       mp[c]++; n     } n   } n   for (auto i : mp) { n     if (!i.first) { n       if (!a) ans += (i.second * i.second); n     } else if (a % i.first == 0) { n       if (a == 0) n         ans += (mp[0] * i.second * 2); n       else n         ans += (mp[a / i.first] * i.second); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   fastio(); n   long long int T = 1; n   cin >> T; n   for (long long int test = 1; test <= T; test++) { n     long long int n, m, k, ans, cnt, sum; n     ans = 0; n     cnt = 100; n     sum = 0; n     cin >> n >> k; n     if (k < n) n       ans = k; n     else { n       while (k && cnt--) { n         if (k < n) { n           ans += k; n           break; n         } n         long long int z = k / n; n         long long int a = z * n; n         z *= (n - 1); n         long long int q = k - z; n         ans += a; n         k = q; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a, b, c, d; n   cin >> n >> k >> a >> b >> c >> d; n   if (n == 4 || k < n + 1) n     cout << -1 << endl; n   else { n     int cur = 1; n     vector<int> ret; n     ret.push_back(a); n     ret.push_back(c); n     while (cur == a || cur == b || cur == c || cur == d) cur++; n     ret.push_back(cur); n     cur++; n     ret.push_back(d); n     for (int x = 4; x < n - 1; x++) { n       while (cur == a || cur == b || cur == c || cur == d) cur++; n       ret.push_back(cur); n       cur++; n     } n     ret.push_back(b); n     for (int i = 0; i < n; i++) cout << ret[i] <<    ; n     cout << endl; n     cout << ret[1] <<     << ret[0] <<     << ret[2] <<     << ret[n - 1] n          <<    ; n     for (int i = n - 2; i > 2; i--) cout << ret[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int k; n   cin >> k; n   int sz = s.size(); n   string land; n   bool equal; n   int begin, lenth, idx, res = 0, end; n   for (int i = 0; i < sz; i++) { n     begin = i; n     land =   ; n     for (int j = i; j < sz; j++) { n       land += s[j]; n       end = min(j + 1 + j - begin + 1 - 1, sz - 1); n       idx = 0; n       equal = 1; n       for (int l = j + 1; l <= end; l++, idx++) { n         if (land[idx] != s[l]) { n           equal = 0; n           break; n         } n       } n       lenth = end - (j + 1) + 1; n       if (equal && lenth != j - begin + 1 && (j - begin + 1) - lenth <= k) { n         if (j == sz - 1) n           res = max(res, (((j - begin + 1) + lenth + k) / 2) * 2); n         else n           res = max(res, 2 * (j - begin + 1)); n       } n       if (equal && lenth == j - begin + 1) res = max(res, 2 * (j - begin + 1)); n     } n   } n   cout << res; 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   int cnt = 0; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n - 1; i++) { n     if (a[i] == 0) { n       if (a[i + 1] == 1 && a[i - 1] == 1) { n         if (i + 2 < n) { n           if (a[i + 2] == 0) { n             cnt++; n             a[i + 1] = 0; n           } else n             cnt++; n         } else n           cnt++; n       } n     } n   } n   cout << cnt; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 5; n char C[MAXN][20][20]; n long long aans[MAXN]; n struct node { n   long long u, v, val, way; n   node() {} n   node(long long a, long long b, long long c) { n     u = a; n     v = b; n     val = c; n   } n }; n vector<node> v; n vector<int> vv[MAXN]; n long long fa[MAXN * 10]; n bool cmp(node a, node b) { return a.val < b.val; } n long long getf(long long x) { n   if (x == fa[x]) n     return x; n   else n     return fa[x] = getf(fa[x]); n } n void add(long long a, long long b) { fa[getf(a)] = getf(fa[b]); } n void dfs(long long x, long long fa) { n   long long len = vv[x].size(); n   for (long long i = 0; i < len; i++) { n     long long to = vv[x][i]; n     if (to != fa) { n       cout << to <<     << x << endl; n       dfs(to, x); n     } n   } n } n int main() { n   long long n, m, k, w; n   cin >> n >> m >> k >> w; n   for (long long k1 = 1; k1 <= k; k1++) { n     for (long long i = 1; i <= n; i++) { n       cin >> C[k1][i] + 1; n     } n   } n   for (long long k1 = 1; k1 <= k; k1++) { n     for (long long k2 = k1 + 1; k2 <= k; k2++) { n       long long num = 0; n       for (long long i = 1; i <= n; i++) { n         for (long long j = 1; j <= m; j++) { n           if (C[k1][i][j] != C[k2][i][j]) num++; n         } n       } n       v.push_back(node(k1, k2, num * w)); n     } n   } n   for (long long i = 1; i <= k; i++) { n     v.push_back(node(0, i, n * m)); n   } n   long long ans = 0; n   for (long long i = 1; i <= k; i++) { n     fa[i] = i; n   } n   sort(v.begin(), v.end(), cmp); n   for (long long i = 0; i < v.size(); i++) { n     long long aa = v[i].u, cc = v[i].val; n     long long bb = v[i].v; n     if (getf(aa) != getf(bb)) { n       add(aa, bb); n       ans += cc; n       vv[aa].push_back(bb); n       vv[bb].push_back(aa); n     } n   } n   cout << ans << endl; n   dfs(0, 0); n } n


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


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000 ) n using namespace std; n struct N { n   int x, y; n }; n queue<N> q; n char Map[2010][2010]; n int jx[] = {-1, 0, 1, 0}; n int jy[] = {0, 1, 0, -1}; n char CalChar(int dir) { n   if (dir == 0) return  v ; n   if (dir == 1) return  < ; n   if (dir == 2) return  ^ ; n   return  > ; n } n int Check(int x, int y, int n, int m) { n   bool hav = false; n   int dir; n   for (int i = 0; i < 4; ++i) { n     if (1 <= x + jx[i] && x + jx[i] <= n && 1 <= y + jy[i] && y + jy[i] <= m && n         Map[x + jx[i]][y + jy[i]] ==  . ) { n       if (hav) return -1; n       hav = true; n       dir = i; n     } n   } n   if (hav == false) return -2; n   return dir; n } n void Output(int n, int m) { n   for (int i = 1; i <= n; ++i) puts(Map[i] + 1); n   puts(  ); n } n int main() { n   int n, m, i, j, tmp; n   scanf( %d %d , &n, &m); n   for (i = 1; i <= n; ++i) scanf( %s , Map[i] + 1); n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= m; ++j) { n       if (Map[i][j] ==  . ) { n         tmp = Check(i, j, n, m); n         if (0 <= tmp) { n           q.push((N){i + jx[tmp], j + jy[tmp]}); n           Map[i][j] = CalChar(tmp); n           Map[i + jx[tmp]][j + jy[tmp]] = CalChar((tmp + 2) % 4); n         } n       } n     } n   } n   N f, t; n   while (q.empty() == false) { n     f = q.front(); n     q.pop(); n     for (i = 0; i < 4; ++i) { n       t.x = f.x + jx[i]; n       t.y = f.y + jy[i]; n       if (1 <= t.x && t.x <= n && 1 <= t.y && t.y <= m && n           Map[t.x][t.y] ==  . ) { n         tmp = Check(t.x, t.y, n, m); n         if (tmp == -2) return puts( Not unique ), 0; n         if (0 <= tmp) { n           Map[t.x][t.y] = CalChar(tmp); n           Map[t.x + jx[tmp]][t.y + jy[tmp]] = CalChar((tmp + 2) % 4); n           q.push((N){t.x + jx[tmp], t.y + jy[tmp]}); n         } n       } n     } n   } n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= m; ++j) n       if (Map[i][j] ==  . ) return puts( Not unique ), 0; n   } n   for (i = 1; i <= n; ++i) puts(Map[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int n = s.length(); n     if (n <= 10) { n       cout << s << endl; n     } else { n       cout << s.at(0); n       cout << n - 2; n       cout << s.at(n - 1) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1007]; n long long cnt[1007]; n long long ncr[1007][1007]; n long long MOD = 998244353; n int main() { n   long long n, res = 0, x; n   scanf( %I64d , &n); n   ; n   for (long long i = 1; i <= n; ++i) scanf( %I64d , &a[i]); n   ; n   for (int i = 0; i < 1000; ++i) { n     for (int j = 0; j <= i; ++j) { n       if (i == 0 || j == 0) n         ncr[i][j] = 1; n       else n         ncr[i][j] = (ncr[i - 1][j] + ncr[i - 1][j - 1]) % MOD; n     } n   } n   for (long long i = n; i > 0; --i) { n     if ((a[i] > 0) && ((i + a[i]) <= n)) { n       x = ncr[n - i][a[i]]; n       cnt[i] = x; n       for (long long j = i + a[i] + 1; j < n; ++j) { n         x = ncr[j - i - 1][a[i]]; n         x = (x * cnt[j]) % MOD; n         cnt[i] = (cnt[i] + x) % MOD; n       } n     } n     res = (res + cnt[i]) % MOD; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n template <typename T> n using V = vector<T>; n template <typename T, typename S> n using P = pair<T, S>; n template <class T> n using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>; n using LD = long double; n template <typename T, typename S> n void check(T& a, S& b) { n   if (a >= b) { n     a %= b; n   } n } n template <typename T> n T gcd(T u, T v) { n   if (u == v) return u; n   if (u == 0) return v; n   if (v == 0) return u; n   if (~u & 1) { n     if (v & 1) n       return gcd(u >> 1, v); n     else n       return gcd(u >> 1, v >> 1) << 1; n   } n   if (~v & 1) return gcd(u, v >> 1); n   if (u > v) return gcd((u - v) >> 1, v); n   return gcd((v - u) >> 1, u); n } n const int N = 35001; n int n, k; n int dp[N][51]; n class SegmentTree { n  public: n   int n; n   V<int> arr, lazy; n   SegmentTree() {} n   SegmentTree(int n) { n     this->n = n; n     this->arr = V<int>(n << 2, 0); n     this->lazy = V<int>(n << 2, 0); n   } n   void propogate(int node, int s, int e) { n     if (lazy[node]) { n       arr[node] += lazy[node]; n       if (s != e) { n         lazy[node << 1] += lazy[node]; n         lazy[node << 1 | 1] += lazy[node]; n       } n     } n     lazy[node] = 0; n   } n   void update(int node, int s, int e, int l, int r, int val) { n     propogate(node, s, e); n     if (l > r || s > r || e < l) return; n     if (s >= l && e <= r) { n       lazy[node] += val; n       propogate(node, s, e); n       return; n     } n     int mid = (s + e) >> 1; n     update(node << 1, s, mid, l, r, val); n     update(node << 1 | 1, mid + 1, e, l, r, val); n     arr[node] = max(arr[node << 1], arr[node << 1 | 1]); n   } n   void upd(int node, int s, int e, int l, int val) { n     propogate(node, s, e); n     if (s == e) { n       arr[node] = max(arr[node], val); n       return; n     } n     int mid = (s + e) >> 1; n     if (l <= mid) n       upd(node << 1, s, mid, l, val); n     else n       upd(node << 1 | 1, mid + 1, e, l, val); n     propogate(node << 1, s, mid); n     propogate(node << 1 | 1, mid + 1, e); n     arr[node] = max(arr[node << 1], arr[node << 1 | 1]); n     return; n   } n   int get(int node, int s, int e, int l, int r) { n     propogate(node, s, e); n     if (l > r || s > r || e < l) return 0; n     if (s >= l && e <= r) { n       return arr[node]; n     } n     int mid = (s + e) >> 1; n     return max(get(node << 1, s, mid, l, r), n                get(node << 1 | 1, mid + 1, e, l, r)); n   } n }; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cerr.tie(NULL); n   ; n   cin >> n >> k; n   V<int> a, f(n + 1, n); n   a.resize(n); n   for (int i = 0; i < (int)n; i++) cin >> a[i], a[i]--; n   SegmentTree seg[51]; n   for (int i = 0; i < k; i++) seg[i] = SegmentTree(n); n   for (int i = n - 1; i >= 0; i--) { n     dp[i][0] = dp[i + 1][0]; n     if (f[a[i]] == n) dp[i][0]++; n     seg[0].upd(1, 0, n - 1, i, dp[i][0]); n     for (int j = 1; j < min(n - i, k); j++) { n       seg[j - 1].update(1, 0, n - 1, i + 1, min(f[a[i]], n - 1), 1); n       dp[i][j] = seg[j - 1].get(1, 0, n - 1, i + 1, n - 1); n       seg[j].upd(1, 0, n - 1, i, dp[i][j]); n     } n     f[a[i]] = i; n   } n   cout << dp[0][k - 1] <<   n ; n   cerr <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     if (n == 1) { n       printf( a na nb nb n ); n       continue; n     } n     if (n % 2 == 0) { n       for (int i = 0; i < 4; i++) { n         for (int j = 0; j < n; j++) { n           if (i % 4 < 2) { n             printf( %c ,  a  + (i % 2 + (j % 4) / 2) % 2); n           } else { n             if (j == 0) { n               printf( %c ,  c ); n             } else if (j == n - 1) { n               printf( %c ,  f ); n             } else { n               printf( %c ,  d  + (i % 2 + (j - 1) % 4 / 2) % 2); n             } n           } n         } n         printf(  n ); n       } n     } else { n       for (int i = 0; i < 4; i++) { n         for (int j = 0; j < n; j++) { n           if (i < 2) { n             if (j == n - 1) { n               printf( %c ,  c ); n             } else { n               printf( %c ,  a  + (i % 2 + (j % 4) / 2) % 2); n             } n           } else { n             if (j < 1) { n               printf( %c ,  d ); n             } else { n               printf( %c ,  f  + (i % 2 + (j - 1) % 4 / 2) % 2); n             } n           } n         } n         printf(  n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9 + 7; n const int N = (int)3e5 + 4; n const int M = 1001; n int n, k; n int a[6]; n double ans; n void f(int s) { n   if (s == k) { n     for (int i = 0; i < n; ++i) { n       for (int j = i + 1; j < n; ++j) { n         if (a[i] > a[j]) { n           ++ans; n         } n       } n     } n     return; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = i; j < n; ++j) { n       reverse(a + i, a + j + 1); n       f(s + 1); n       reverse(a + i, a + j + 1); n     } n   } n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   f(0); n   for (int i = 0; i < k; ++i) { n     ans /= n; n     ans /= (n + 1); n     ans *= 2.0; n   } n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test, i, j, xy, flag = 0, n, u, count, d, o1 = 0, o2 = 0, s, e, l, n                             r, x, y, m, z, max1, x1, y1, k, x2, y2, z1, sum, f, n                             min1; n   cin >> test; n   while (test--) { n     x1 = y1 = 0; n     string a; n     cin >> n >> x >> y >> z >> a; n     for (i = 0; i < n; i++) { n       if (a[i] ==  0 ) { n         x1++; n       } else { n         y1++; n       } n     } n     x2 = (x1 * z) + (x1 * y) + (y1 * y); n     y2 = (y1 * z) + (y1 * x) + (x1 * x); n     z1 = x1 * x + y1 * y; n     cout << min(min(x2, y2), z1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long s, x; n   cin >> s >> x; n   s -= x; n   if (s % 2 || s < 0) { n     cout << 0 <<   n ; n     return 0; n   } n   s >>= 1; n   bool ok = true; n   for (int i = 50; i >= 0; --i) n     if (s & (1LL << i)) ok &= (~x & (1LL << i)) != 0; n   if (!ok) { n     cout << 0 <<   n ; n     return 0; n   } n   long long ans = 1LL << __builtin_popcountll(x); n   cout << (s == 0 ? ans - 2 : ans) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a[105], b[105], i, q, j, f, x, c = 0; n   double s = 0, s1 = 0, min = -1, max = -1; n   cin >> n >> k; n   for (i = 1; i <= n; i++) cin >> a[i]; n   cin >> q; n   for (i = 1; i <= q; i++) { n     f = 0; n     s = 0; n     for (j = 1; j <= n / k; j++) { n       cin >> x; n       if (a[x] == -1) { n         f = 1; n       } n       s = s + a[x]; n       a[x] = -1; n     } n     if (f == 0) { n       c++; n       s = s / (n / k); n       if (i == 1) { n         min = s; n         max = s; n       } else { n         if (s < min) n           min = s; n         else if (s > max) n           max = s; n       } n     } n   } n   j = 0; n   for (i = 1; i <= n; i++) { n     if (a[i] != -1) b[j++] = a[i]; n   } n   sort(b, b + j); n   if (j >= (n / k) && k - c >= 1) { n     s = 0; n     s1 = 0; n     for (i = 0; i < (n / k); i++) { n       s = s + b[i]; n       s1 = s1 + b[j - 1 - i]; n     } n     s = s / (n / k); n     s1 = s1 / (n / k); n     if (min == -1 && max == -1) { n       min = s; n       max = s1; n     } else { n       if (s < min) min = s; n       if (s1 > max) max = s1; n     } n   } n   printf( %0.10lf %0.10lf , min, max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using u64 = uint64_t; n using u32 = uint32_t; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using vi = vector<int>; n using vl = vector<ll>; n using vvi = vector<vi>; n using vpi = vector<pi>; n static constexpr int INF = (int)1e9; n static constexpr ll INFL = (ll)1e18; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m; n   cin >> n >> m; n   vector<string> s(n), t(m); n   for (auto& si : s) cin >> si; n   for (auto& ti : t) cin >> ti; n   int q, y; n   cin >> q; n   while (q--) cin >> y, --y, cout << s[y % n] << t[y % m] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   if (n > 2) { n     for (long long int i = 0; i < n - 1; i++) cout << 2 <<    ; n     cout << 1 << endl; n   } else n     cout << -1 << endl; n } n void testcase() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD(1000000007); n const int INF((1 << 30) - 1); n const int MAXN(); n int main() { n   long long k, b, t; n   int n; n   cin >> k >> b >> n >> t; n   long long z = 1; n   int num = 0; n   while (z <= t) { n     z = z * k + b; n     num++; n   } n   printf( %d , max(0, n - num + 1)); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using uint = unsigned int; n const int NMax = 1e2 + 5; n const ll inf_ll = 1e18 + 5; n const int inf_int = 1e9 + 5; n const int mod = 100003; n using zint = int; n void getABC(char* sir, int& a, int& b, int& c) { n   ++sir; n   a = 0; n   while (isdigit(*sir)) { n     a = a * 10 + *sir -  0 ; n     ++sir; n   } n   ++sir; n   b = 0; n   while (isdigit(*sir)) { n     b = b * 10 + *sir -  0 ; n     ++sir; n   } n   ++sir; n   ++sir; n   c = 0; n   while (*sir != 0 && isdigit(*sir)) { n     c = c * 10 + *sir -  0 ; n     ++sir; n   } n } n int euclid(int a, int b) { return b ? euclid(b, a % b) : a; } n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   int N; n   cin >> N; n   char sir[100]; n   map<pair<int, int>, int> make_pair; n   vector<pair<int, int>> v(N + 1); n   for (int i = 1; i <= N; ++i) { n     cin >> sir; n     int a, b, c; n     getABC(sir, a, b, c); n     int top = a + b; n     int gcd = euclid(top, c); n     top /= gcd; n     c /= gcd; n     make_pair[{top, c}]++; n     v[i] = {top, c}; n   } n   for (int i = 1; i <= N; ++i) { n     cout << make_pair[v[i]] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int judge(int n) { n   if (n == 2 || n == 3) return 1; n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return 0; n   return 1; n } n int main() { n   int i, j, m; n   scanf( %d , &n); n   if (judge(n)) n     printf( 1 n%d n , n); n   else if (judge(n - 2)) n     printf( 2 n2 %d n , n - 2); n   else if (judge(n - 4)) n     printf( 3 n2 2 %d n , n - 4); n   else { n     for (i = n - 2; i >= n - 300 && i >= 1; i--) n       if (judge(i)) { n         k = i; n         m = n - i; n         break; n       } n     for (j = 2; j <= m; j++) { n       if (judge(j) && judge(m - j)) { n         printf( 3 n%d %d %d n , k, j, n - i - j); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int MOD = 1e9 + 7; n int n, revfact[N << 1], fact[N << 1]; n int mul(int, int); n int sum(int, int); n int pow(int, int); n int choose(int, int); n int32_t main() { n   cin >> n; n   fact[0] = 1; n   for (int i = 1; i < 2 * n + 3; i++) fact[i] = mul(fact[i - 1], i); n   for (int i = 0; i < 2 * n + 3; i++) revfact[i] = pow(fact[i], MOD - 2); n   int res = choose((2 * n) + 2, n + 1) - 1; n   if (res < 0) res += MOD; n   cout << res << endl; n } n int mul(int a, int b) { return 1LL * a * b % MOD; } n int sum(int a, int b) { n   int c = a + b; n   if (c >= MOD) c -= MOD; n   return c; n } n int pow(int n, int md) { n   int res = 1; n   for (; md; md /= 2) { n     if (md % 2 == 1) res = mul(res, n); n     n = mul(n, n); n   } n   return res; n } n int choose(int m, int k) { n   return mul(fact[m], mul(revfact[k], revfact[m - k])); n } n


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


#include <bits/stdc++.h> n using namespace std; n using namespace std; n using ll = long long; n using ld = long double; n const int MOD = 1e9 + 7, inf = 1e9, N = 2e5 + 1; n const ll INF = 1e18; n ld tic, tac; n void solve() { n   int n, count = 0; n   cin >> n; n   int i, a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 1) count++; n   } n   cout << count <<   n ; n   for (i = 1; i < n; i++) { n     if (a[i] == 1) { n       cout << a[i - 1] <<     ; n     } n   } n   cout << a[n - 1] <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1000100; n const int off = 1 << 19; n const int inf = 1e9; n int a[MAX], s[MAX], c[MAX]; n pair<int, int> T[2 * off]; n int n, k; n pair<int, int> getmax(int i, int lo, int hi, int a, int b) { n   if (lo >= b || hi <= a) return {-inf, -1}; n   if (lo >= a && hi <= b) return T[i]; n   return max(getmax(i * 2, lo, (lo + hi) / 2, a, b), n              getmax(i * 2 + 1, (lo + hi) / 2, hi, a, b)); n } n long long solve(int lo, int hi) { n   if (lo >= hi) { n     for (int i = lo; i <= hi; ++i) c[s[i]]--; n     return 0; n   } n   int m = getmax(1, 0, off, lo, hi).second; n   assert(m != -1); n   long long ans = 0; n   if (hi - m < m - lo) { n     for (int i = m + 1; i <= hi; ++i) c[s[i]]--; n     for (int i = m + 1; i <= hi; ++i) ans += c[(s[i] - (a[m] % k) + k) % k]; n     ans--; n     ans += solve(lo, m); n     for (int i = m + 1; i <= hi; ++i) c[s[i]]++; n     ans += solve(m + 1, hi); n   } else { n     for (int i = lo; i <= m; ++i) c[s[i]]--; n     for (int i = lo; i <= m; ++i) ans += c[(s[i] + a[m]) % k]; n     ans--; n     ans += solve(m + 1, hi); n     for (int i = lo; i <= m; ++i) c[s[i]]++; n     ans += solve(lo, m); n   } n   return ans; n } n int main(void) { n   scanf( %d %d , &n, &k); n   for (int i = (0); i < (n); ++i) scanf( %d , a + i); n   s[0] = 0; n   for (int i = (0); i < (n); ++i) s[i + 1] = (s[i] + a[i]) % k; n   for (int i = (0); i < (n + 1); ++i) c[s[i]]++; n   for (int i = (0); i < (n); ++i) T[off + i] = {a[i], i}; n   for (int i = off - 1; i > 0; --i) T[i] = max(T[i * 2], T[i * 2 + 1]); n   long long ans = solve(0, n); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500005; n const int inf = 100000000; n struct elem { n   long long val; n   int l, r; n }; n struct V { n   long long val; n   int ind; n }; n bool compare(V x, V y) { n   if (x.val < y.val) return true; n   if (x.val == y.val && x.ind < y.ind) return true; n   return false; n } n V a[MAXN]; n elem L[MAXN]; n int main() { n   int N; n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> a[i].val; n     a[i].ind = i; n     L[i].val = a[i].val; n     L[i].l = i - 1; n     L[i].r = i + 1; n   } n   int GlobalLeft = 1; n   int GlobalRight = N; n   sort(a + 1, a + N + 1, compare); n   long long ans = 0; n   a[N + 1].val = inf; n   int pntL = 1, pntR = 1; n   int Left = N; n   while (pntL <= N && Left > 2) { n     pntR = pntL; n     while (a[pntR].val == a[pntL].val) pntR++; n     int NextL = pntR; n     pntR--; n     while (pntL <= pntR && a[pntL].ind == GlobalLeft && Left > 2) { n       ans += L[GlobalLeft].val; n       GlobalLeft = L[GlobalLeft].r; n       pntL++; n       Left--; n     } n     while (pntL <= pntR && a[pntR].ind == GlobalRight && Left > 2) { n       ans += L[GlobalRight].val; n       GlobalRight = L[GlobalRight].l; n       pntR--; n       Left--; n     } n     while (pntL <= pntR && Left > 2) { n       ans += min(L[L[a[pntL].ind].l].val, L[L[a[pntL].ind].r].val); n       L[L[a[pntL].ind].l].r = L[a[pntL].ind].r; n       L[L[a[pntL].ind].r].l = L[a[pntL].ind].l; n       pntL++; n       Left--; n     } n     pntL = NextL; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int u, v; n   int c; n   int w; n   int id; n   bool use; n }; n bool operator<(const Edge& l, const Edge& r) { return l.w < r.w; } n class Node { n   int depth; n   pair<int, int> w; n   Node* parent; n  n  public: n   Node() : depth(0), w(0, -1), parent(nullptr) {} n   static pair<int, int> merge(Node* a, Node* b, int W, int id) { n     pair<int, int> mx = {-1, -1}; n     while ((a->parent || b->parent) && a != b) { n       if (!b->parent || a->parent && a->depth <= b->depth) { n         mx = max(mx, a->w); n         a = a->parent; n       } else { n         mx = max(mx, b->w); n         b = b->parent; n       } n     } n     if (a == b) return {W - mx.first, mx.second}; n     if (a->depth < b->depth) swap(a, b); n     b->parent = a; n     b->w.first = W; n     b->w.second = id; n     a->depth = max(a->depth, b->depth + 1); n     return {0, -id}; n   } n }; n int main(int argc, const char* argv[]) { n   int N, M; n   std::vector<int> W, C; n   int S; n   scanf( %d%d , &N, &M); n   W.resize(M); n   C.resize(M); n   for (auto&& x : W) scanf( %d , &x); n   for (auto&& x : C) scanf( %d , &x); n   vector<Edge> edges(M); n   for (int i = 0; i < M; i++) { n     Edge& e = edges[i]; n     scanf( %d %d , &e.u, &e.v); n     e.u--; n     e.v--; n     e.c = C[i]; n     e.w = W[i]; n     e.id = i + 1; n   } n   scanf( %d , &S); n   sort((edges).begin(), (edges).end()); n   vector<Node> nodes(N); n   int best = 1 << 30; n   int exc = -1; n   const Edge* useE = nullptr; n   long long sumw = 0; n   for (auto&& e : edges) { n     pair<int, int> r = Node::merge(&nodes[e.u], &nodes[e.v], e.w, e.id); n     if (r.second < 0) { n       e.use = true; n       sumw += e.w; n       r.second = -r.second; n     } else { n       e.use = false; n     } n     int bonus = r.first - S / e.c; n     if (bonus < best) { n       best = bonus; n       exc = r.second; n       useE = &e; n     } n   } n   printf( n        %I64d  n         n , n       sumw + best); n   for (auto&& e : edges) n     if (e.use && e.id != exc) printf( %d %d n , e.id, e.w); n   printf( %d %d n , useE->id, useE->w - S / useE->c); n   return 0; n } n


#include <bits/stdc++.h> n int star[100005]; n int scnt; n int pack[100005]; n int pcnt; n int unit(int x) { n   if (x > 0) return x; n   return 0; n } n int check(int time) { n   int i; n   int sat = 0; n   for (i = 1; i <= pcnt; i++) { n     if (sat >= star[scnt]) return 1; n     int l = 1, r = scnt, mid, idx; n     while (l <= r) { n       mid = (l + r) / 2; n       if (star[mid] > sat) { n         idx = mid; n         r = mid - 1; n       } else n         l = mid + 1; n     } n     if (star[idx] < pack[i]) { n       if (pack[i] - star[idx] > time) return 0; n       int t = pack[i] - star[idx]; n       sat = pack[i] + unit(time - 2 * t); n       if (sat < pack[i] + (time - t) / 2) sat = pack[i] + (time - t) / 2; n     } else n       sat = pack[i] + time; n   } n   if (sat >= star[scnt]) return 1; n   return 0; n } n int main(void) { n   int n; n   char tmp[100005]; n   scanf( %d , &n); n   scanf( %s , &tmp[1]); n   int i; n   for (i = 1; i <= n; i++) { n     if (tmp[i] ==  * ) star[++scnt] = i; n     if (tmp[i] ==  P ) pack[++pcnt] = i; n   } n   int l = 1, r = 2 * n - 1, mid, ans; n   while (l <= r) { n     mid = (l + r) / 2; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 2e5; n int n, a[mxN], k; n void solve() { n   long long n; n   cin >> n; n   if (n == 0) { n     cout << 1 << endl; n     return; n   } n   if (n % 4 == 1) { n     cout << 8 << endl; n   } else if (n % 4 == 2) { n     cout << 4 << endl; n   } else if (n % 4 == 3) { n     cout << 2 << endl; n   } else if (n % 4 == 0) { n     cout << 6 << endl; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n long long x, a[N], b[N]; n long long n, k; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + 1 + n); n   b[1] = a[1]; n   int j = 2; n   for (int i = 2; i <= n; i++) { n     if (a[i] != a[i - 1]) { n       b[j] = a[i] - a[i - 1]; n       j++; n     } n   } n   for (int i = 1; i <= k; i++) { n     cout << b[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 100; n int a[maxn]; n long long dp[maxn][2]; n int get1(long long n) { n   int sum = 0; n   while (n != 0) { n     if ((n & 1) == 1) sum += 1; n     n >>= 1; n   } n   return sum; n } n int main() { n   int N; n   cin >> N; n   for (int i = 1; i <= N; i++) { n     long long x; n     cin >> x; n     a[i] = get1(x); n   } n   dp[0][0] = 0; n   dp[0][1] = 0; n   dp[1][0] = 0; n   dp[1][1] = 0; n   for (int i = 2; i <= N; i++) { n     if (a[i] & 1) { n       dp[i][0] = dp[i - 1][1] + (a[i - 1] & 1); n       dp[i][1] = dp[i - 1][0] + !(a[i - 1] & 1); n     } else { n       dp[i][0] = dp[i - 1][0] + !(a[i - 1] & 1); n       dp[i][1] = dp[i - 1][1] + (a[i - 1] & 1); n     } n   } n   long long ans = 0; n   for (int i = 2; i <= N; i++) { n     int L = max(1, i - 120); n     int sum = a[i]; n     int pmax = a[i]; n     int res = 0; n     for (int j = i - 1; j >= L; j--) { n       pmax = max(pmax, a[j]); n       sum += a[j]; n       if (sum & 1) continue; n       if (pmax > sum / 2) res += 1; n     } n     ans += dp[i][0] - res; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 810000; n inline long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n long long n; n struct segg { n   long long son[N][2], siz[N], cnt = 0, val[N], rd[N], root, x, y, z, sum[N]; n   inline long long New_Node(long long x) { n     cnt++; n     val[cnt] = sum[cnt] = x; n     siz[cnt] = 1; n     rd[cnt] = rand(); n     return cnt; n   } n   inline void UpDate(long long x) { n     siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1; n     sum[x] = sum[son[x][0]] + sum[son[x][1]] + val[x]; n   } n   void Split(long long root, long long k, long long &x, long long &y) { n     if (!root) n       x = y = 0; n     else { n       if (val[root] > k) { n         y = root; n         Split(son[root][0], k, x, son[root][0]); n       } else { n         x = root; n         Split(son[root][1], k, son[root][1], y); n       } n       UpDate(root); n     } n   } n   long long Merge(long long x, long long y) { n     if (!x || !y) return x + y; n     if (rd[x] < rd[y]) { n       son[x][1] = Merge(son[x][1], y); n       UpDate(x); n       return x; n     } else { n       son[y][0] = Merge(x, son[y][0]); n       UpDate(y); n       return y; n     } n   } n   void Insert(long long s) { n     Split(root, s, x, y); n     root = Merge(Merge(x, New_Node(s)), y); n   } n   void Delete(long long s) { n     Split(root, s, x, z); n     Split(x, s - 1, x, y); n     y = Merge(son[y][0], son[y][1]); n     root = Merge(Merge(x, y), z); n   } n   void Rankdx(long long s) { n     Split(root, s - 1, x, y); n     printf( %lld n , siz[x] + 1); n     root = Merge(x, y); n   } n   long long dk(long long root, long long k) { n     while (1) { n       if (siz[son[root][0]] >= k) n         root = son[root][0]; n       else if (siz[son[root][0]] == k - 1) n         return val[root]; n       else n         k -= siz[son[root][0]] + 1, root = son[root][1]; n     } n   } n   void Findkx(long long s) { printf( %lld n , val[dk(root, s)]); } n   void Findqq(long long s) { n     Split(root, s - 1, x, y); n     printf( %lld n , val[dk(x, siz[x])]); n     root = Merge(x, y); n   } n   void Findhz(long long s) { n     Split(root, s, x, y); n     printf( %lld n , val[dk(y, 1)]); n     root = Merge(x, y); n   } n   long long FindS(long long x, long long p) { n     if (!p) return 0; n     long long qwq = 0; n     while (1) { n       if (p == siz[son[x][0]] + 1) return qwq + sum[son[x][0]] + val[x]; n       if (p <= siz[son[x][0]]) n         x = son[x][0]; n       else n         qwq += sum[son[x][0]] + val[x], p = p - siz[son[x][0]] - 1, n                                         x = son[x][1]; n     } n   } n } A, B; n signed main() { n   srand((unsigned)time(NULL)); n   n = read(); n   for (register long long i = 1; i <= n; i++) { n     long long opt = read(), x = read(); n     if (!opt) { n       if (x > 0) n         A.Insert(-x); n       else n         A.Delete(x); n     } n     if (x > 0) n       B.Insert(-x); n     else n       B.Delete(x); n     long long qwq = B.sum[B.root]; n     long long res = B.siz[B.root] - A.siz[A.root]; n     if (!res) { n       printf( %lld n , -qwq); n       continue; n     } n     if (B.dk(B.root, res) < A.dk(A.root, 1)) n       printf( %lld n , -qwq - A.dk(A.root, 1) - B.FindS(B.root, res - 1)); n     else n       printf( %lld n , -qwq - B.FindS(B.root, res)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace chu_xuan { n void swap(int &a, int &b) { n   register int tmp = a; n   a = b, b = tmp; n } n void read(long long &a) { n   a = 0; n   int b = 1, c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) a = (a << 3) + (a << 1) + c - 48, c = getchar(); n   a *= b; n } n int In() { n   int a = 0, b = 1, c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) a = (a << 3) + (a << 1) + c - 48, c = getchar(); n   return a * b; n } n int max(int a, int b) { return a > b ? a : b; } n int min(int a, int b) { return a > b ? b : a; } n }  // namespace chu_xuan n using namespace chu_xuan; n const int N = 3e5; n long long n, a[N]; n long long ans; n int main() { n   read(n); n   for (int i = 0; i < n; i++) read(a[i]); n   sort(a, a + n); n   for (int i = 0; i < n / 2; i++) { n     ans += (a[i] + a[n - i - 1]) * (a[i] + a[n - i - 1]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long fastpow(int b, int e) { n   if (!e) return 1; n   if (e & 1) return b * fastpow(b, e - 1); n   long long temp = fastpow(b, e / 2); n   return temp * temp; n } n int notwork(char a, char b) { n   set<char> s; n   s.insert(a); n   s.insert(b); n   if (a == b && a ==  o ) return 0; n   if (a == b && a ==  A ) return 0; n   if (a == b && a ==  v ) return 0; n   if (a == b && a ==  V ) return 0; n   if (a == b && a ==  I ) return 0; n   if (a == b && a ==  H ) return 0; n   if (a == b && a ==  O ) return 0; n   if (a == b && a ==  M ) return 0; n   if (a == b && a ==  x ) return 0; n   if (a == b && a ==  X ) return 0; n   if (a == b && a ==  Y ) return 0; n   if (a == b && a ==  w ) return 0; n   if (a == b && a ==  W ) return 0; n   if (a == b && a ==  U ) return 0; n   if (a == b && a ==  T ) return 0; n   if (s.count( b ) && s.count( d )) return 0; n   if (s.count( p ) && s.count( q )) return 0; n   return 1; n } n int midwork(char a) { n   char arr[] = { U ,  T ,  W ,  w ,  Y ,  x ,  X ,  M , n                  o ,  O ,  H ,  I ,  v ,  V ,  A }; n   for (int i = 0; i < 15; i++) n     if (arr[i] == a) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   int len = s.length(); n   int mid = len / 2; n   if (s.length() & 1 == 0) mid++; n   if (len & 1) { n     if (!midwork(s[mid])) return cout <<  NIE , 0; n   } n   for (int i = 0, j = len - 1; i < mid; i++, j--) { n     if (notwork(s[i], s[j])) return cout <<  NIE , 0; n   } n   cout <<  TAK ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct chess { n   bool used; n   int x, y; n   char p; n } up, down, Left, Right, up_Left, up_Right, down_Left, down_Right; n int n, kx, ky; n void add(int x, int y, char c) { n   if (x == kx) { n     if (y < ky) { n       if (!Left.used || y > Left.y) { n         Left.x = x; n         Left.y = y; n         Left.p = c; n         Left.used = true; n       } n     } else { n       if (!Right.used || y < Right.y) { n         Right.x = x; n         Right.y = y; n         Right.p = c; n         Right.used = true; n       } n     } n   } n   if (y == ky) { n     if (x < kx) { n       if (!up.used || x > up.x) { n         up.x = x; n         up.y = y; n         up.p = c; n         up.used = true; n       } n     } else { n       if (!down.used || x < down.x) { n         down.x = x; n         down.y = y; n         down.p = c; n         down.used = true; n       } n     } n   } n   if (x - y == kx - ky) { n     if (x < kx) { n       if (!up_Left.used || x > up_Left.x) { n         up_Left.x = x; n         up_Left.y = y; n         up_Left.p = c; n         up_Left.used = true; n       } n     } else { n       if (!down_Right.used || x < down_Right.x) { n         down_Right.x = x; n         down_Right.y = y; n         down_Right.p = c; n         down_Right.used = true; n       } n     } n   } n   if (x + y == kx + ky) { n     if (x < kx) { n       if (!up_Right.used || x > up_Right.x) { n         up_Right.x = x; n         up_Right.y = y; n         up_Right.p = c; n         up_Right.used = true; n       } n     } else { n       if (!down_Left.used || x < down_Left.x) { n         down_Left.x = x; n         down_Left.y = y; n         down_Left.p = c; n         down_Left.used = true; n       } n     } n   } n } n bool check() { n   if (up.p ==  R  || up.p ==  Q  || down.p ==  R  || down.p ==  Q ) return true; n   if (Left.p ==  R  || Left.p ==  Q  || Right.p ==  R  || Right.p ==  Q ) n     return true; n   if (up_Right.p ==  B  || up_Right.p ==  Q  || up_Left.p ==  B  || n       up_Left.p ==  Q ) n     return true; n   if (down_Right.p ==  B  || down_Right.p ==  Q  || down_Left.p ==  B  || n       down_Left.p ==  Q ) n     return true; n   return false; n } n int main() { n   int x, y; n   ios::sync_with_stdio(false); n   cin.tie(0); n   char c; n   cin >> n >> kx >> ky; n   for (int i = 0; i < n; i++) { n     cin >> c >> x >> y; n     add(x, y, c); n   } n   if (check()) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long x; n   cin >> x; n   for (long long i = 0; i <= 1000; i++) { n     for (long long j = 0; j <= 10000; j++) { n       long long g = i * 1234567 + j * 123456; n       long long m = (x - g) / 1234; n       double l = (double)((x - g) / 1234); n       long long d = (long long)(l); n       if (m == d && m >= 0 && g + m * 1234 == x) { n         cout <<  YES  << endl; n         return 0; n       } n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   cout << (n - 2) * (n - 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long k, p; n long long getZCY(long long x) { n   long long res = x; n   while (x) { n     res *= 10LL; n     res += x % 10; n     x /= 10; n   } n   return res; n } n int main() { n   scanf( %I64d , &k); n   scanf( %I64d , &p); n   long long ans = 0; n   for (int i = (1); i <= (int)(k); ++i) { n     ans += getZCY(i); n     ans %= p; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> neigh[200009]; n int ans[200009]; n int a[200009]; n int b[200009]; n int c[200009]; n int f[200009]; n int Q[200009]; n int L, R; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d %d %d , &a[i], &b[i], &c[i]); n     f[a[i]] += c[i]; n     f[b[i]] += c[i]; n     neigh[a[i]].push_back(i); n     neigh[b[i]].push_back(i); n   } n   f[1] = f[n] = 0; n   for (int i = 2; i <= n - 1; i++) f[i] /= 2; n   Q[R++] = 1; n   while (L < R) { n     int cur = Q[L++]; n     for (int i = 0; i < neigh[cur].size(); i++) { n       int u; n       if (cur == a[neigh[cur][i]]) n         u = b[neigh[cur][i]]; n       else n         u = a[neigh[cur][i]]; n       if (u != n && f[u] == 0) continue; n       if (u == a[neigh[cur][i]]) ans[neigh[cur][i]] = 1; n       f[u] -= c[neigh[cur][i]]; n       if (u != n && f[u] == 0) Q[R++] = u; n     } n   } n   for (int i = 0; i < m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long base, long long exp, long long modulus) { n   base %= modulus; n   long long result = 1; n   while (exp > 0) { n     if (exp & 1) result = (result * base) % modulus; n     base = (base * base) % modulus; n     exp >>= 1; n   } n   return result; n } n long long n; n long long a[100005]; n long long dp[5004][5005]; n long long bin(long long lo, long long hi, long long val) { n   long long ans = -1; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     if (a[mid] >= val) { n       ans = mid; n       hi = mid - 1; n     } else { n       lo = mid + 1; n     } n   } n   return ans; n } n long long doit(long long prev, long long curr, int flag) { n   if (curr == n + 1) return 0; n   if (dp[prev][curr] != -1) return dp[prev][curr]; n   long long ans = 0; n   long long y = 100009; n   long long x = 1 + doit(prev, curr + 1, flag); n   long long val = a[curr - 1] - a[prev - 1] + a[curr - 1]; n   long long ind = bin(curr, n, val); n   if (ind != -1) { n     y = doit(curr, ind + 1, flag) + (ind - curr); n   } n   ans = min(x, y); n   dp[prev][curr] = ans; n   return ans; n } n int main() { n   scanf( %lld , &n); n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] = a[i] + a[i - 1]; n   } n   long long ans = doit(1, 2, 0); n   cout << ans; n   printf(  n ); n   ; n   return 0; n } n


#include <iostream> n #include <vector> n #include <algorithm> n #include <string>  n #include <set> n #include <cmath> n #include <bitset> n #include <iomanip> n #include <queue> n #include <stack> n #include <map> n #include <unordered_map> n #include <unordered_set> n #include <chrono> n #include <random> n #include <complex> n using namespace std; n  n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n inline void boostIO() { n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  cout.tie(0); n  cout << fixed << setprecision(10); n } n  n bool isPrime(int x) { n  if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; n  for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; n } n  n typedef long long int ll; n typedef long double ld; n typedef unsigned long long ull; n  n typedef vector<int> vi; n typedef vector<ll> vll; n typedef pair<int, int> pii; n  n #define fori(n) for(int i = 0; i < (n); ++i) n #define forj(n) for(int j = 0; j < (n); ++j) n #define fork(n) for(int k = 0; k < (n); ++k) n #define forx(n) for(int x = 0; x < (n); ++x) n  n #define all(x) (x).begin(), (x).end() n #define rall(x) (x).rbegin(), (x).rend() n #define ff first n #define ss second n #define debug(x) cerr << #x <<   =   << x << endl; n #define debug_p(x) cerr << #x <<     << x.ff <<     << x.ss << endl; n #define debug_v(x) cerr << #x <<    ; for (auto ii : x) cout << i <<    ; cout << endl; n #define debug_vp(x) cerr << #x <<    ; for (auto ii : x) cout <<  [  << ii.ff <<     << ii.ss <<  ] ; cout << endl; n #define mp make_pair n #define rand kjsdflsdfsdlsjdlkfsdjflsjdfl  n  n #define Return cerr<<endl<< RUNTIME:  <<1.0*clock()/CLOCKS_PER_SEC <<   s  <<endl; return 0; n  n #define PI 3.14159265 n  n ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } n ll lcm(ll a, ll b) { return a * b / gcd(a, b); } n  n ///////////////////// n #define int long long n int Mod = 1e9 + 7; n #define endl   n  n ///////////////////// n  n int inf = 1e18; n  n vector<int> A; n int dp[2000][2000] = {}; n int seen[2000][2000] = {}; n  n int rec(int l, int r) { n  if (l == r) { n   return 0; n  } n  if (seen[l][r]) { n   return dp[l][r]; n  } n  seen[l][r] = 1; n  //here n  int ans = inf; n  ans = min(rec(l+1, r), rec(l, r-1)) + A[r] - A[l]; n  dp[l][r] = ans; n  return dp[l][r]; n  n } n  n int32_t main() { n  boostIO(); n  int n; cin >> n; n  A.assign(n, 0); n  fori(n) { n   cin >> A[i]; n  }  n  sort(all(A)); n  cout << rec(0, n - 1); n }


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n int n, m; n vector<int> a; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n >> m; n   a.resize(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   int cnt = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (m <= 0) break; n     m -= a[i], cnt++; n   } n   cout << cnt <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, z; n int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); } n int main() { n   cin >> n >> m >> z; n   cout << (z / ((long long)n * m / gcd(n, m))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(string &s1, string &s2) { return s1.size() < s2.size(); } n bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first < b.first) return true; n   return false; n } n void solve() { n   long long int n, op; n   cin >> n >> op; n   string s; n   cin >> s; n   for (int i = 0; i < n - 2 && op > 0; i++) { n     if (s[i] !=  4  && s[i + 1] ==  4  && s[i + 2] ==  7 ) { n       if (i % 2) n         s[i + 2] =  4 , op--; n       else n         s[i + 1] =  7 , op--; n     } else if (s[i] ==  4  && s[i + 1] ==  7  && s[i + 2] !=  7 ) { n       if (i % 2 == 0) n         s[i + 1] =  4 , op--; n       else n         s[i] =  7 , op--; n     } else if (s[i] ==  4  && s[i + 1] ==  7  && s[i + 2] ==  7 ) { n       if (i % 2 == 0) { n         if (op % 2) { n           s[i + 1] =  4 ; n           break; n         } else { n           break; n         } n       } else n         s[i] =  7 , op--; n     } else if (s[i] ==  4  && s[i + 1] ==  4  && s[i + 2] ==  7 ) { n       if (i % 2 == 0) { n         if (op % 2) { n           s[i + 1] =  7 ; n           break; n         } else { n           break; n         } n       } else n         s[i + 2] =  4 , op--; n     } n   } n   cout << s <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using std::swap; n const int N = (1 << 18) + 5, p = 998244353, G = 3; n int rev[N], invG, f[N], g[N], inv2; n inline int Dec(int x, int y) { n   x -= y; n   return x < 0 ? x + p : x; n } n inline int Plus(int x, int y) { n   x += y; n   return x >= p ? x - p : x; n } n inline void read(int &x) { n   x = 0; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) ch = getchar(); n   while (ch <=  9  && ch >=  0 ) x = x * 10 + (ch ^ 48), ch = getchar(); n } n inline int quick_pow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = a * 1ll * ans % p; n     a = a * 1ll * a % p; n     b >>= 1; n   } n   return ans; n } n inline void butterfly(int n) { n   register int i; n   for (i = 0; i < n; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0); n } n void NTT(int a[], bool op, int n) { n   register int i, j, mid; n   static int w[N]; n   w[0] = 1; n   for (i = 0; i < n; ++i) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (mid = 1; mid < n; mid <<= 1) { n     int g1 = quick_pow(op ? G : invG, (p - 1) / (mid << 1)); n     for (j = 1; j < mid; ++j) w[j] = w[j - 1] * 1ll * g1 % p; n     for (i = 0; i < n; i += mid << 1) n       for (j = 0; j < mid; ++j) { n         int x = a[i + j], y = a[i + j + mid] * 1ll * w[j] % p; n         a[i + j] = Plus(x, y), a[i + j + mid] = Dec(x, y); n       } n   } n   if (!op) { n     int invn = quick_pow(n, p - 2); n     for (i = 0; i < n; ++i) a[i] = a[i] * 1ll * invn % p; n   } n } n void Poly_Inv(int a[], int b[], int m) { n   int n; n   register int i, len; n   for (n = 1; n < m; n <<= 1) n     ; n   static int w[N], tmp[N], r[N]; n   memset(w, 0, sizeof(int) * (n << 1)); n   memset(r, 0, sizeof(int) * (n << 1)); n   memset(tmp, 0, sizeof(int) * (n << 1)); n   w[0] = quick_pow(a[0], p - 2); n   for (len = 2; len <= n; len <<= 1) { n     int Len = len << 1; n     memcpy(tmp, a, sizeof(int) * len); n     memcpy(r, w, sizeof(int) * (len >> 1)); n     butterfly(Len); n     NTT(r, 1, Len), NTT(tmp, 1, Len); n     for (i = 0; i < Len; ++i) r[i] = r[i] * 1ll * r[i] % p * tmp[i] % p; n     NTT(r, 0, Len); n     for (i = 0; i < len; ++i) w[i] = Dec((w[i] << 1) % p, r[i]); n     memset(r + len, 0, sizeof(int) * len); n   } n   memcpy(b, w, sizeof(int) * n); n } n void Poly_Sqrt(int a[], int b[], int m) { n   int n; n   register int i, len; n   for (n = 1; n < m; n <<= 1) n     ; n   static int w[N], tmp[N], r[N]; n   memset(w, 0, sizeof(int) * (n << 1)); n   memset(r, 0, sizeof(int) * (n << 1)); n   memset(tmp, 0, sizeof(int) * (n << 1)); n   w[0] = a[0]; n   for (len = 2; len <= n; len <<= 1) { n     int Len = len << 1; n     Poly_Inv(w, r, len); n     butterfly(Len); n     memcpy(tmp, a, sizeof(int) * len); n     NTT(r, 1, Len), NTT(tmp, 1, Len); n     for (i = 0; i < Len; ++i) r[i] = r[i] * 1ll * tmp[i] % p; n     NTT(r, 0, Len); n     for (i = 0; i < len; ++i) n       w[i] = Plus(inv2 * 1ll * w[i] % p, inv2 * 1ll * r[i] % p); n   } n   memcpy(b, w, sizeof(int) * n); n } n int main() { n   int n, m, x; n   register int i; n   read(n), read(m); n   f[0] = 1; n   for (i = 1; i <= n; ++i) read(x), f[x] = 1; n   for (i = 1; i <= m; ++i) f[i] = Dec(0, 4 * f[i]); n   invG = quick_pow(G, p - 2), inv2 = quick_pow(2, p - 2); n   Poly_Sqrt(f, g, m + 1); n   ++g[0]; n   Poly_Inv(g, f, m + 1); n   for (i = 1; i <= m; ++i) printf( %d n , f[i] * 2ll % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(long long n) { n   if (n == 1) return false; n   if (n == 2) return true; n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n long long powm(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * 1ll * a % 1000000007; n     a = a * 1ll * a % 1000000007; n     b >>= 1; n   } n   return res; n } n long long mul(long long x, long long y) { return (x * 1ll * y) % 1000000007; } n void solve() { n   long long i = 0, j = 0, k = 0, n = 0, m = 0, rb, cb, rd, cd; n   cin >> n >> m >> rb >> cb >> rd >> cd; n   if (rd >= rb && cd >= cb) { n     { n       cout << min((rd - rb), (cd - cb)) <<   n ; n       return; n     }; n   } n   if (cd >= cb) { n     cout << min((n - rb) * 2 + abs(rb - rd), (cd - cb)) <<   n ; n     return; n   }; n   if (rd >= rb) { n     cout << min((m - cb) * 2 + abs(cb - cd), (rd - rb)) <<   n ; n     return; n   }; n   { n     cout << min((((n - rb) * 2) + abs(rb - rd)), ((m - cb) * 2 + abs(cb - cd))) n          <<   n ; n     return; n   } n } n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cout << setprecision(15); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10001; n int st, en; n const int limit = -5000000; n const int maxlongint = 1 << 20; n struct edges { n   int v, flow, rev; n   int cost; n } make; n vector<edges> g[maxn]; n int dist[maxn]; n bool vis[maxn]; n int tot; n bool ans[maxn]; n int i, j, m, n, l, o, p, x, y, z; n int k; n struct pairs { n   int x, y; n } pre[maxn]; n struct tris { n   int s, t, fee; n } sp[maxn]; n inline bool spfa() { n   for (int i = 0; i <= st; i++) dist[i] = maxlongint; n   dist[st] = 0; n   queue<int> q; n   q.push(st); n   while (!q.empty()) { n     int x = q.front(); n     vis[x] = false; n     q.pop(); n     for (int i = 0; i < g[x].size(); i++) n       if (g[x][i].flow && dist[g[x][i].v] > dist[x] + g[x][i].cost) { n         dist[g[x][i].v] = dist[x] + g[x][i].cost; n         pre[g[x][i].v].x = x; n         pre[g[x][i].v].y = i; n         if (!vis[g[x][i].v]) { n           q.push(g[x][i].v); n           vis[g[x][i].v] = true; n         } n       } n   } n   return dist[en] < maxlongint; n } n inline void addedge(int x, int y, int flow, int cost) { n   int v1 = g[x].size(); n   int v2 = g[y].size(); n   make.v = y; n   make.flow = flow; n   make.cost = cost; n   make.rev = v2; n   g[x].push_back(make); n   make.v = x; n   make.flow = 0; n   make.rev = v1; n   make.cost = -cost; n   g[y].push_back(make); n } n int mincut(int x, int fw) { n   if (x == st) return fw; n   int f = mincut(pre[x].x, min(fw, g[pre[x].x][pre[x].y].flow)); n   g[pre[x].x][pre[x].y].flow -= f; n   g[x][g[pre[x].x][pre[x].y].rev].flow += f; n   return f; n } n int main() { n   scanf( %d%d , &n, &k); n   en = 2 * n + 1; n   st = en + 1; n   addedge(st, 0, k, 0); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &sp[i].s, &sp[i].t, &sp[i].fee); n     addedge(0, i * 2 - 1, 1, 0); n     addedge(i * 2 - 1, i * 2, 1, -sp[i].fee); n     addedge(i * 2, en, 1, 0); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (sp[i].s + sp[i].t <= sp[j].s) addedge(i * 2, j * 2 - 1, 1, 0); n   while (spfa()) mincut(en, maxlongint); n   for (int i = 1; i <= n; i++) n     for (j = 0; j < g[i * 2 - 1].size(); j++) n       if (g[i * 2 - 1][j].v == i * 2 && g[i * 2 - 1][j].flow == 0) n         ans[i] = true; n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   cout << endl; n   fclose(stdin); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace FFT { n const int N = 1 << 21; n struct Complex { n   double r, i; n }; n inline Complex operator+(const Complex &a, const Complex &b) { n   return {a.r + b.r, a.i + b.i}; n } n inline Complex operator-(const Complex &a, const Complex &b) { n   return {a.r - b.r, a.i - b.i}; n } n inline Complex operator*(const Complex &a, const Complex &b) { n   return {a.r * b.r - a.i * b.i, a.i * b.r + a.r * b.i}; n } n int rev[N]; n const double PI = acos(-1); n void FFT(Complex a[], int n, int fl) { n   for (int i = 0; i < n; i++) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int s = 1; s < n; s <<= 1) { n     Complex wn = {cos(PI / s), sin(PI / s) * fl}; n     for (int i = 0; i < n; i += s << 1) { n       Complex w = {1, 0}; n       for (int j = 0; j < s; j++, w = w * wn) { n         Complex x = a[i + j], y = w * a[i + s + j]; n         a[i + j] = x + y; n         a[i + s + j] = x - y; n       } n     } n   } n   if (fl == -1) n     for (int i = 0; i < n; i++) a[i].r /= n, a[i].i /= n; n } n Complex f[N], g[N]; n int n, m; n void solve() { n   int lim = 1, d = 0; n   while (lim <= n + m) ++d, lim <<= 1; n   for (int i = 0; i < lim; i++) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (d - 1)); n   for (int i = n + 1; i < lim; i++) f[i] = {0, 0}; n   for (int i = m + 1; i < lim; i++) g[i] = {0, 0}; n   FFT(f, lim, 1); n   FFT(g, lim, 1); n   for (int i = 0; i < lim; i++) f[i] = f[i] * g[i]; n   FFT(f, lim, -1); n } n }  // namespace FFT n const int N = 55, M = 105, K = 20005; n const double INF = 1e18; n int n, m, t, x, a[M], b[M]; n double dis[N][N], c[M], p[M][K * 2], f[N][K * 2], g[M][K]; n void solve(int l, int r) { n   if (l == t) return; n   if (l == r) { n     for (int i = 1; i < n; i++) f[i][l] = INF; n     for (int i = 1; i <= m; i++) n       if (a[i] != n) f[a[i]][l] = min(f[a[i]][l], g[i][l] + c[i]); n     return; n   } n   int mid = (l + r) >> 1; n   solve(mid + 1, r); n   FFT::n = r - l - 1; n   FFT::m = r - mid - 1; n   for (int i = 1; i <= m; i++) n     if (a[i] != n) { n       for (int j = 0; j <= FFT::n; j++) FFT::f[j] = {p[i][j + 1], 0}; n       for (int j = 0; j <= FFT::m; j++) FFT::g[j] = {f[b[i]][r - j], 0}; n       FFT::solve(); n       for (int j = l; j <= mid; j++) g[i][j] += FFT::f[r - j - 1].r; n     } n   solve(l, mid); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> t >> x; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i != j) dis[i][j] = INF; n   int t1; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i] >> t1; n     c[i] = dis[a[i]][b[i]] = t1; n     for (int j = 1; j <= t; j++) cin >> t1, p[i][j] = t1 / 1e5; n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]); n   for (int i = 0; i < t * 2; i++) f[n][i] = i <= t ? 0 : x; n   for (int i = 1; i < n; i++) n     for (int j = t; j < t * 2; j++) f[i][j] = dis[i][n] + x; n   solve(0, t * 2 - 1); n   printf( %.10lf n , f[1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void write(long long a) { n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(int a) { n   write(a); n   puts(  ); n } n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n const int N = 200005, oo = 2000000005; n struct data { n   int l, r, cost; n } a[N], b[N]; n int n, m, ans, tong[N], j; n inline bool cmp1(data a, data b) { return a.r < b.r; } n inline bool cmp2(data a, data b) { return a.l < b.l; } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) { n     a[i].l = read(); n     a[i].r = read(); n     a[i].cost = read(); n     b[i] = a[i]; n   } n   sort(&a[1], &a[n + 1], cmp1); n   ans = oo; n   sort(&b[1], &b[n + 1], cmp2); n   j = 1; n   for (int i = 1; i <= n; i++) { n     for (; a[j].r < b[i].l; j++) n       if (!tong[a[j].r - a[j].l + 1]) n         tong[a[j].r - a[j].l + 1] = a[j].cost; n       else n         tong[a[j].r - a[j].l + 1] = min(tong[a[j].r - a[j].l + 1], a[j].cost); n     if (m >= b[i].r - b[i].l + 1 && tong[m - (b[i].r - b[i].l + 1)]) n       ans = min(ans, b[i].cost + tong[m - (b[i].r - b[i].l + 1)]); n   } n   if (ans < oo) n     writeln(ans); n   else n     puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n int const INF = numeric_limits<int>::max(); n long long const LLINF = numeric_limits<long long>::max(); n long double const EPS = 1e-9; n int n, m, k, q; n int const N = 505; n int a[N][N]; n struct qu { n   int x, y, t; n }; n bool operator<(const qu& a, const qu& b) { return a.t < b.t; } n qu qq[N * N]; n bool check() { n   int ans = 0; n   vector<int> d(m, -1), d1(m), d2(m); n   stack<int> st; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) n       if (a[i][j] == 0) d[j] = i; n     while (!st.empty()) st.pop(); n     for (int j = 0; j < m; ++j) { n       while (!st.empty() && d[st.top()] <= d[j]) st.pop(); n       d1[j] = st.empty() ? -1 : st.top(); n       st.push(j); n     } n     while (!st.empty()) st.pop(); n     for (int j = m - 1; j >= 0; --j) { n       while (!st.empty() && d[st.top()] <= d[j]) st.pop(); n       d2[j] = st.empty() ? m : st.top(); n       st.push(j); n     } n     for (int j = 0; j < m; ++j) n       ans = max(ans, min((i - d[j]), (d2[j] - d1[j] - 1))); n   } n   return ans >= k; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> k >> q; n   for (int i = 0; i < q; i++) { n     cin >> qq[i].x >> qq[i].y >> qq[i].t; n     qq[i].x--; n     qq[i].y--; n     a[qq[i].x][qq[i].y] = 1; n   } n   if (!check()) { n     cout << -1 << endl; n     return 0; n   } n   sort(qq, qq + q); n   int l = 0, r = q - 1; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         a[i][j] = 0; n       } n     } n     for (int i = 0; i <= mid; i++) { n       a[qq[i].x][qq[i].y] = 1; n     } n     if (check()) { n       r = mid; n     } else { n       l = mid; n     } n   } n   while (r >= l) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         a[i][j] = 0; n       } n     } n     for (int i = 0; i <= l; i++) { n       a[qq[i].x][qq[i].y] = 1; n     } n     if (check()) { n       break; n     } else { n       l++; n     } n   } n   cout << qq[l].t << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y; n }; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     int i, j, cnt = 0; n     node a[110]; n     for (i = 0; i < n; i++) { n       scanf( %d%d , &a[i].x, &a[i].y); n     } n     bool u[110] = {0}; n     queue<int> q; n     for (i = 0; i < n; i++) { n       if (u[i]) continue; n       u[i] = 1; n       q.push(i); n       while (!q.empty()) { n         node t = a[q.front()]; n         q.pop(); n         for (j = 0; j < n; j++) { n           if (u[j] == 0 && (t.x == a[j].x || t.y == a[j].y)) { n             u[j] = 1; n             q.push(j); n           } n         } n       } n       cnt += 1; n     } n     printf( %d n , cnt - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, b; n     cin >> a >> b; n     long long xx = fabs(a - b); n     long long d = xx / 2; n     if ((a + b) % 2) { n       long long yy = a + b - 2 * d + 1; n       cout << yy <<   n ; n       for (long long i = d; i <= a + b - d; i++) { n         cout << i <<    ; n       } n       cout <<   n ; n     } else { n       long long yy = (a + b - 2 * d) / 2 + 1; n       cout << yy <<   n ; n       for (long long i = d; i <= a + b - d; i += 2) { n         cout << i <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long N = 2e5 + 5, M = N; n long long mx, n; n vector<long long> a; n long long dp[N]; n void solve() { n   long long i, j, m; n   cin >> n; n   a.resize(n); n   map<long long, long long> mapp; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     mapp[a[i] - i] += a[i]; n   } n   for (auto it : mapp) { n     mx = max(it.second, mx); n   } n   cout << mx; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int power(int a, int b, int c) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % c; n     } n     y = (y * y) % c; n     b /= 2; n   } n   return x % c; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int dp[26][1 << 21]; n int main() { n   long long int n, m; n   string str1, str2; n   memset(dp, 0, sizeof(dp)); n   long long int ans = 0, i; n   cin >> n >> m; n   cin >> str1 >> str2; n   long long int g = gcd(int((str1).size()), int((str2).size())); n   for (i = 0; i < int((str1).size()); i++) { n     dp[str1[i] -  a ][i % g]++; n   } n   for (i = 0; i < int((str2).size()); i++) { n     ans += dp[str2[i] -  a ][i % g]; n   } n   ans = int((str2).size()) * m - ans * gcd(n, m); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e+5 + 10; n const int INF = 0x7fffffff; n struct edge { n   int v, n; n } e[maxn * 2]; n struct node { n   int mx, mi, i, c; n   bool operator<(const node &sec) const { return mx > sec.mx; } n } a[maxn]; n int n, m, k; n int head[maxn], rd[maxn]; n void read() { n   int i, u, v; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i].c); n     a[i].i = i; n     if (a[i].c) n       a[i].mx = a[i].mi = a[i].c; n     else n       a[i].mx = k, a[i].mi = 1; n   } n   memset(head, -1, sizeof(head)); n   for (i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     e[i << 1].v = v; n     e[i << 1].n = head[u]; n     head[u] = i << 1; n     e[i << 1 | 1].v = u; n     e[i << 1 | 1].n = head[v]; n     head[v] = i << 1 | 1; n   } n } n void tuopu(int d) { n   int i, u, v; n   queue<int> q; n   memset(rd, 0, sizeof(rd)); n   for (i = 0; i < m; i++) rd[e[i << 1 | d].v]++; n   for (i = 1; i <= n; i++) n     if (!rd[i]) q.push(i); n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     for (i = head[u]; i != -1; i = e[i].n) n       if ((i & 1) == d) { n         v = e[i].v; n         rd[v]--; n         if (!rd[v]) q.push(v); n         if (d) n           a[v].mi = max(a[v].mi, a[u].mi + 1); n         else n           a[v].mx = min(a[v].mx, a[u].mx - 1); n       } n   } n   for (i = 1; i <= n; i++) n     if (rd[i]) { n       printf( -1 n ); n       exit(0); n     } n } n bool cmpa(node aa, node bb) { return aa.mi < bb.mi; } n bool cmpi(node aa, node bb) { return aa.i < bb.i; } n void check() { n   for (int i = 1; i <= n; i++) n     if (a[i].mx < a[i].mi) { n       printf( -1 n ); n       exit(0); n     } n } n void solve() { n   int i, j = 1; n   priority_queue<node> q; n   bool del; n   for (i = 1; i <= k; i++) { n     del = false; n     for (; j <= n && a[j].mi == i; j++) { n       node tmp = a[j]; n       tmp.i = j; n       q.push(tmp); n     } n     while (!q.empty() && q.top().mx == i) { n       node tmp = q.top(); n       q.pop(); n       a[tmp.i].c = i; n       del = true; n     } n     if (!del) { n       if (q.empty()) { n         printf( -1 n ); n         exit(0); n       } n       node tmp = q.top(); n       q.pop(); n       a[tmp.i].c = i; n     } n   } n } n void print() { n   for (int i = 1; i <= n; i++) printf( %d  , a[i].c); n } n int main() { n   read(); n   tuopu(0); n   tuopu(1); n   sort(a + 1, a + n + 1, cmpa); n   check(); n   solve(); n   sort(a + 1, a + n + 1, cmpi); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n int pre[1005], c[1005]; n vector<int> a[1005]; n int find(int x) { return pre[x] = (x == pre[x] ? x : find(pre[x])); } n void Union(int a, int b) { n   if (a != b) pre[a] = b; n } n int main() { n   int n, m, cnt = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) pre[i] = i; n   for (int i = 1; i <= n; i++) { n     int k, b; n     cin >> c[i]; n     k = c[i]; n     if (k == 0) cnt++; n     while (k--) { n       cin >> b; n       a[b].push_back(i); n     } n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j < a[i].size(); j++) { n       Union(find(a[i][0]), find(a[i][j])); n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) n     if (pre[i] == i && c[i]) ans++; n   cout << max(0, ans - 1) + cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double calcC(int N, int M) { n   if (M > N) return 0; n   M = min(N - M, M); n   double res = 1; n   for (int i = 1; i <= M; ++i) res *= N - M + i, res /= i; n   return res; n } n int main() { n   int N, M; n   while (cin >> N >> M) { n     double result = 0; n     for (int k = 1; k <= N; ++k) { n       double tmp = 1; n       tmp = calcC(M, k) * calcC(N * M - M, N - k) * k * k; n       tmp /= calcC(N * M, N) * N * N; n       result += tmp; n     } n     result *= N; n     cout << setprecision(10) << result << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, l, r, l1 = 0, r1 = 1e9, l2 = 0, r2 = 1e9; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> l >> r; n     l1 = max(l1, l); n     r1 = min(r1, r); n   } n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> l >> r; n     l2 = max(l2, l); n     r2 = min(r2, r); n   } n   cout << max(l1 - r2, max(l2 - r1, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char A[200]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; 4 * j <= n; j++) { n       if (A[i] ==  * ) n         if (A[i] == A[i + j]) n           if (A[i + j] == A[i + 2 * j]) n             if (A[i + 2 * j] == A[i + 3 * j]) n               if (A[i + 3 * j] == A[i + 4 * j]) { n                 cout <<  yes ; n                 return 0; n               } n     } n   } n   cout <<  no ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n = 4; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (max(a[2], a[3]) - 1 > min(a[2], a[3])) { n     cout << -1; n     return 0; n   } n   string ans =   ; n   vector<int> cnt(2, 0); n   if (a[2] > a[3]) { n     for (int i = 0; i < a[2]; i++) { n       ans +=  47 ; n     } n   } else { n     if (a[2] == a[3]) { n       if (a[0] > a[2]) { n         for (int i = 0; i < a[2]; i++) { n           ans +=  47 ; n         } n         ans +=  4 ; n       } else { n         if (a[1] > a[3]) { n           ans +=  7 ; n           for (int i = 0; i < a[2]; i++) { n             ans +=  47 ; n           } n         } else { n           cout << -1 <<   n ; n           return 0; n         } n       } n     } else { n       for (int i = 0; i < a[3]; i++) { n         ans +=  74 ; n       } n     } n   } n   cnt[0] = cnt[1] = 0; n   for (char c : ans) { n     if (c ==  4 ) cnt[0]++; n     if (c ==  7 ) cnt[1]++; n   } n   if (cnt[0] > a[0] || cnt[1] > a[1]) { n     cout << -1 <<   n ; n     return 0; n   } n   a[0] -= cnt[0]; n   a[1] -= cnt[1]; n   for (int i = 0; i < (int)ans.size(); i++) { n     if (ans[i] ==  4  && a[0] > 0) { n       string nans =   ; n       for (int j = 0; j <= i; j++) { n         nans += ans[j]; n       } n       for (int j = 0; j < a[0]; j++) { n         nans +=  4 ; n       } n       for (int j = i + 1; j < (int)ans.size(); j++) { n         nans += ans[j]; n       } n       swap(ans, nans); n       break; n     } n   } n   for (int i = (int)ans.size() - 1; i >= 0; i--) { n     if (ans[i] ==  7  && a[1] > 0) { n       string nans =   ; n       for (int j = 0; j <= i; j++) { n         nans += ans[j]; n       } n       for (int j = 0; j < a[1]; j++) { n         nans +=  7 ; n       } n       for (int j = i + 1; j < (int)ans.size(); j++) { n         nans += ans[j]; n       } n       swap(ans, nans); n       break; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   long long n, q; n   cin >> n >> q; n   long long* m = (long long*)malloc((n + 2) * sizeof(long long)); n   for (int i = 0; i < n + 2; i++) { n     m[i] = 0; n   } n   long long* num = (long long*)malloc(n * sizeof(long long)); n   for (int i = 0; i < n; i++) cin >> num[i]; n   while (q--) { n     long long a, b; n     cin >> a >> b; n     b++; n     m[a]++; n     m[b]--; n   } n   for (int i = 1; i < n + 2; i++) m[i] += m[i - 1]; n   sort(num, num + n); n   sort(m + 1, m + n + 1); n   long long answer = 0; n   for (int i = 0; i < n; i++) { n     answer += num[i] * m[i + 1]; n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > days; n vector<int> visited; n int main() { n   int k, n; n   pair<int, int> temp; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     temp.first = i + 1; n     cin >> temp.second; n     days.push_back(temp); n   } n   for (int i = 0; i < (n - 1); i++) { n     for (int j = 0; j < (n - i - 1); j++) { n       if (days[j].second > days[j + 1].second) swap(days[j], days[j + 1]); n     } n   } n   int total = 0; n   for (int i = 0; i < n; i++) { n     if ((total + days[i].second) <= k) { n       total += days[i].second; n       visited.push_back(days[i].first); n     } n   } n   int sz = visited.size(); n   for (int i = 0; i < (sz - 1); i++) { n     for (int j = 0; j < (sz - i - 1); j++) { n       if (visited[j] > visited[j + 1]) swap(visited[j], visited[j + 1]); n     } n   } n   cout << sz << endl; n   for (int i = 0; i < sz; i++) cout << visited[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int PASSED_POINT = 0; n void Duxar() {} n void read_int(int &value) { n   int sign = 1; n   char ch; n   value = 0; n   while (!isdigit(ch = getchar())) { n     (ch ==  - ) && (sign = -1); n   } n   do { n     value = value * 10 + (ch -  0 ); n   } while (isdigit(ch = getchar())); n   value *= sign; n } n int N, M; n vector<int> hero, state; n vector<pair<int, int> > actionTeam; n int nr_bits(int x) { n   int ans = 0; n   while (x) { n     ans += (x & 1); n     x >>= 1; n   } n   return ans; n } n int main() { n   int i, mask, act; n   string s; n   read_int(N); n   hero.resize(N); n   for (i = 0; i < N; ++i) { n     read_int(hero[i]); n   } n   sort((hero).rbegin(), (hero).rend()); n   read_int(M); n   state.resize((1 << M) - 1); n   actionTeam.resize(M); n   for (i = 0; i < M; ++i) { n     cin >> s >> actionTeam[i].second; n     actionTeam[i].first = 0; n     if (s[0] ==  p ) actionTeam[i].first = 1; n   } n   state[(1 << M) - 1] = 0; n   for (mask = (1 << M) - 2; mask >= 0; --mask) { n     act = nr_bits(mask); n     if (actionTeam[act].second == 1) { n       state[mask] = -1000000007; n       for (i = 0; i < M; ++i) { n         if ((mask & (1 << i)) == 0) { n           state[mask] = max(state[mask], state[mask | (1 << i)] + n                                              actionTeam[act].first * hero[i]); n         } n       } n     } else { n       state[mask] = 1000000007; n       for (i = 0; i < M; ++i) { n         if ((mask & (1 << i)) == 0) { n           state[mask] = min(state[mask], state[mask | (1 << i)] - n                                              actionTeam[act].first * hero[i]); n         } n       } n     } n   } n   printf( %d n , state[0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8e5 + 5, B = 20; n char s[N]; n int n, fa[N], len[N], sz[N], a[N], cc[N], np = 1, cnt = 1; n map<int, int> c[N]; n int poi[N]; n vector<int> e[N]; n int st[B][N << 1], dfn[N], pos[N], tim; n int qq[N << 1], pre[N], za[N], zb[N], siz; n bool cmp(int a, int b) { return dfn[a] < dfn[b]; } n void dfs(int u) { n   dfn[u] = ++tim; n   pos[tim] = u; n   st[0][tim] = dfn[u]; n   for (auto v : e[u]) dfs(v), st[0][++tim] = dfn[u]; n } n void bui_st() { n   for (int i = 1; i <= B - 1; ++i) n     for (int j = 1; j + (1 << i) - 1 <= tim; ++j) n       st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << i - 1)]); n } n int lca(int a, int b) { n   if ((a = dfn[a]) > (b = dfn[b])) swap(a, b); n   int d = floor(log2(b - a + 1)); n   return pos[min(st[d][a], st[d][b - (1 << d) + 1])]; n } n void append(int x) { n   int p = np; n   np = ++cnt; n   len[np] = len[p] + 1; n   sz[np] = 1; n   for (; p && !c[p][x]; p = fa[p]) c[p][x] = np; n   if (!p) n     fa[np] = 1; n   else { n     int q = c[p][x]; n     if (len[q] == len[p] + 1) n       fa[np] = q; n     else { n       int nq = ++cnt; n       len[nq] = len[p] + 1; n       c[nq] = c[q]; n       fa[nq] = fa[q]; n       fa[q] = fa[np] = nq; n       for (; c[p][x] == q; p = fa[p]) c[p][x] = nq; n     } n   } n } n void get_edge() { n   for (int i = 1; i <= cnt; ++i) e[fa[i]].push_back(i); n } n void ini2() { n   while (siz) za[qq[siz]] = zb[qq[siz]] = 0, --siz; n   int la, lb, x; n   scanf( %d%d , &la, &lb); n   for (int i = 1; i <= la; ++i) n     scanf( %d , &x), ++za[qq[++siz] = poi[n + 1 - x]]; n   for (int i = 1; i <= lb; ++i) n     scanf( %d , &x), ++zb[qq[++siz] = poi[n + 1 - x]]; n   sort(qq + 1, qq + siz + 1, cmp); n   for (int i = siz; i >= 2; --i) qq[++siz] = lca(qq[i], qq[i - 1]); n   sort(qq + 1, qq + siz + 1, cmp); n   siz = unique(qq + 1, qq + siz + 1) - qq - 1; n   stack<int> Q; n   Q.push(qq[1]); n   pre[qq[1]] = 0; n   for (int i = 2; i <= siz; ++i) { n     for (int t; t = Q.top(), lca(t, qq[i]) != t;) Q.pop(); n     pre[qq[i]] = Q.top(); n     Q.push(qq[i]); n   } n } n long long solve() { n   long long re = 0; n   for (int i = siz; i >= 1; --i) { n     int x = qq[i]; n     re += 1ll * za[x] * zb[x] * (len[x] - len[pre[x]]); n     za[pre[x]] += za[x]; n     zb[pre[x]] += zb[x]; n   } n   return re; n } n int main() { n   int q; n   scanf( %d%d%s , &n, &q, s + 1); n   reverse(s + 1, s + n + 1); n   for (int i = 1; i <= n; ++i) append(s[i] -  a ), poi[i] = np; n   get_edge(); n   dfs(1); n   bui_st(); n   while (q--) { n     ini2(); n     printf( %lld n , solve()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int Max(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ b; } n inline int Min(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ a; } n string s; n long long num[2][1000005], len[2][1000005]; n long long rnum[2][1000005]; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   while (cin >> n) { n     cin >> s; n     memset(num, 0, sizeof(num)); n     memset(rnum, 0, sizeof(rnum)); n     memset(len, 0, sizeof(len)); n     for (int i = (1); i <= (n); i++) { n       for (int j = (0); j <= (1); j++) { n         num[j][i] = num[j][i - 1]; n         len[j][i] = len[j][i - 1]; n       } n       if (s[i - 1] ==  U ) { n         num[0][i]++; n         len[0][i] += i; n         rnum[0][num[0][i]] = i; n       } else { n         num[1][i]++; n         len[1][i] += i; n         rnum[1][num[1][i]] = i; n       } n     } n     for (int i = (1); i <= (n); i++) { n       long long ret = 0; n       int a = num[1][n] - num[1][i]; n       int b = num[0][i - 1]; n       if (s[i - 1] ==  U ) { n         if (a <= b) { n           int ps = rnum[0][b - a]; n           ret = 2 * ((len[1][n] - len[1][i]) - (len[0][i - 1] - len[0][ps])) + n                 n - i + 1; n         } else { n           int ps = rnum[1][num[1][i] + b + 1]; n           ret = 2 * ((len[1][ps] - len[1][i]) - len[0][i - 1]) - i; n         } n       } else { n         if (a < b) { n           int ps = rnum[0][b - a - 1]; n           ret = 2 * ((len[1][n] - len[1][i]) - (len[0][i - 1] - len[0][ps])) + n                 n + i + 1; n         } else { n           int ps = rnum[1][num[1][i] + b]; n           ret = 2 * ((len[1][ps] - len[1][i]) - len[0][i - 1]) + i; n         } n       } n       cout << ret <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 1; n const long long int mod2 = 1e9 + 9; n const long long int maxn = 1e6 + 1; n const int root = 1e7 + 5; n const long long inf = 1e18 + 18; n double pie4 = 3.1415926535; n long long int visit[maxn], a[maxn]; n void die() { n   cout <<  INFINITE ; n   exit(0); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, i, pos; n   string s; n   cin >> n; n   cin >> s; n   for (i = 0; i < n; i++) cin >> a[i]; n   pos = 0; n   visit[0] = 0; n   while (1) { n     if (s[pos] ==  > ) { n       pos += a[pos]; n       if (pos >= n || pos < 0) { n         cout <<  FINITE ; n         return 0; n       } n       if (visit[pos]) die(); n       visit[pos] = 1; n     } else { n       pos -= a[pos]; n       if (pos >= n || pos < 0) { n         cout <<  FINITE ; n         return 0; n       } n       if (visit[pos]) die(); n       visit[pos] = 1; n     } n     if (pos >= n || pos < 0) { n       cout <<  FINITE ; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long flag = 0, temp; n   temp = b - a % b; n   for (long long i = temp; (i + a) <= c; i += b) { n     cout << i <<    ; n     flag = 1; n   } n   if (!flag) cout <<  -1 ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n ll ara[300005], ans_ara[300005]; n set<ll> st; n vector<pair<ll, ll> > vv; n int main() { n   ll n, k, i, j, ans = 0; n   scanf( %I64d %I64d , &n, &k); n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &ara[i]); n     ans -= (ara[i] * i); n     st.insert(i + k); n     vv.push_back({ara[i], i}); n   } n   sort(vv.rbegin(), vv.rend()); n   for (pair<ll, ll> u : vv) { n     ll num = *st.lower_bound(u.second); n     ans += (num * u.first); n     st.erase(st.lower_bound(u.second)); n     ans_ara[u.second] = num; n   } n   printf( %I64d n , ans); n   for (i = 1; i <= n; i++) { n     printf( %I64d  , ans_ara[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int Abs(int x) { return x < 0 ? -x : x; } n int main() { n   int i, N, ans = 0, sum = 0, c = 0, d = 0; n   double p; n   scanf( %d , &N); n   for (i = 0; i < 2 * N; i++) { n     scanf( %lf , &p); n     int t = ((int)(p * 1000 + 0.1)) % 1000; n     if (!t) c++; n     sum += t; n   } n   ans = 1000 * N - sum; n   int q = Abs(ans); n   for (i = 1; i <= c; i++) { n     if (q > Abs(ans - i * 1000)) q = Abs(ans - i * 1000); n   } n   printf( %d.%03d n , q / 1000, q % 1000); n   return 0; n } n


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


#include <bits/stdc++.h> n struct Edge { n   int u, v, next; n } edge[100000]; n int head[105], num[105]; n bool visited[105]; n int en, cnt, tt; n void add_edge(int u, int v) { n   edge[en].u = u; n   edge[en].v = v; n   edge[en].next = head[u]; n   head[u] = en++; n } n void DFS1(int u) { n   int v; n   visited[u] = true; n   for (int id = head[u]; id != -1; id = edge[id].next) { n     v = edge[id].v; n     if (!visited[v]) DFS1(v); n   } n } n void DFS2(int u, int p) { n   int v; n   num[u] = tt++; n   visited[u] = true; n   for (int id = head[u]; id != -1; id = edge[id].next) { n     v = edge[id].v; n     if (v == p) continue; n     if (!visited[v]) n       DFS2(v, u); n     else if (num[v] < num[u]) n       cnt++; n   } n } n int main() { n   int n, m, u, v; n   en = 0; n   memset(head, 0xff, sizeof head); n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     add_edge(u, v); n     add_edge(v, u); n   } n   memset(visited, false, sizeof visited); n   DFS1(1); n   cnt = 0; n   for (int i = 1; i <= n; i++) n     if (!visited[i]) cnt++; n   if (cnt) n     puts( NO ); n   else { n     memset(visited, false, sizeof visited); n     cnt = tt = 0; n     DFS2(1, -1); n     if (cnt == 1) n       puts( FHTAGN! ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   vector<long long> q(n); n   for (long long i = 0; i < k; i++) q[k - 1] += a[i]; n   for (long long i = k; i < n; i++) q[i] = q[i - 1] + a[i] - a[i - k]; n   vector<pair<long long, long long>> left(n); n   left[0] = {q[0], 0}; n   for (long long i = 1; i < n; i++) { n     if (q[i] > left[i - 1].first) n       left[i] = {q[i], i}; n     else n       left[i] = left[i - 1]; n   } n   long long ans = 0, l = 0, r = 0; n   for (long long i = 2 * k - 1; i < n; i++) { n     long long first_ans = q[i]; n     pair<long long, long long> second_ans = left[i - k]; n     if (first_ans + second_ans.first > ans) { n       ans = first_ans + second_ans.first; n       l = second_ans.second; n       r = i; n     } n   } n   l++, l++, r++, r++; n   l -= k, r -= k; n   cout << l <<     << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, k, n, m, r, s; n   cin >> n; n   if (n % 2 == 0) { n     m = n / 2; n   } else { n     m = n - 1; n     m = m / 2 - n; n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void fun() { n   int n, k; n   cin >> n >> k; n   map<string, int> m; n   string s; n   string a[n]; n   for (int i = 0; i < n; i++) { n     cin >> s; n     a[i] = s; n     m[s]++; n   } n   int c = 0; n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) { n       s =   ; n       for (int p = 0; p < k; p++) { n         if (a[i][p] == a[j][p]) n           s += a[i][p]; n         else n           s += (char)( S  +  E  +  T  - a[i][p] - a[j][p]); n       } n       c += m[s]; n     } n   cout << c / 3 << endl; n } n int main() { n   { fun(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 600010; n const double pi = acos(-1.0); n const double eps = 1e-15; n stack<int> h; n vector<int> g[maxx]; n int t, n, m; n int ans = 0, total = 0, sum = 0, cnt = 0; n int a[maxx], bj[maxx], vis[maxx], dfn[maxx], low[maxx]; n char ch[3111]; n char c[3111][3111], s[3111][3111]; n void tarjan(int x, int father) { n   dfn[x] = low[x] = ++cnt, vis[x] = 1, h.push(x); n   for (int i = 0; i < g[x].size(); i++) { n     int to = g[x][i]; n     if (to == father) continue; n     if (!dfn[to]) { n       tarjan(to, x); n       low[x] = min(low[x], low[to]); n     } else if (vis[to]) n       low[x] = min(low[x], dfn[to]); n   } n   if (low[x] == dfn[x]) { n     bj[x] = ++total, vis[x] = 0; n     while (h.top() != x) { n       bj[h.top()] = total, vis[h.top()] = 0, h.pop(); n     } n     h.pop(); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf(  %c , &c[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf(  %c , &s[i][j]); n   for (int i = 1; i <= n; i++) scanf(  %c , &ch[i]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (c[i][j] ==  0 ) { n         if (s[i][j] ==  1 ) { n           if (ch[j] ==  0  && ch[i] ==  0 ) { n             ans = -1; n             i = n + 1; n             break; n           } n           if (ch[j] ==  0  && ch[i] ==  1 ) g[j + 3 * n].push_back(j + n); n           if (ch[j] ==  1  && ch[i] ==  0 ) g[i + 2 * n].push_back(i); n           if (ch[j] ==  1  && ch[i] ==  1 ) n             g[i].push_back(j + 3 * n), g[j + 3 * n].push_back(i), n                 g[i + 2 * n].push_back(j + n), g[j + n].push_back(i + 2 * n); n         } else { n           if (ch[j] ==  0  && ch[i] ==  1 ) g[j + n].push_back(j + 3 * n); n           if (ch[j] ==  1  && ch[i] ==  0 ) g[i].push_back(i + 2 * n); n           if (ch[j] ==  1  && ch[i] ==  1 ) n             g[i].push_back(j + n), g[j + n].push_back(i), n                 g[i + 2 * n].push_back(j + 3 * n), n                 g[j + 3 * n].push_back(i + 2 * n); n         } n       } else { n         if (s[i][j] ==  1 ) { n           if (ch[j] ==  0  && ch[i] ==  1 ) g[j + n].push_back(j + 3 * n); n           if (ch[j] ==  1  && ch[i] ==  0 ) g[i].push_back(i + 2 * n); n           if (ch[j] ==  1  && ch[i] ==  1 ) n             g[i].push_back(j + n), g[i + 2 * n].push_back(j + 3 * n), n                 g[j + n].push_back(i), g[j + 3 * n].push_back(i + 2 * n); n         } else { n           if (ch[j] ==  0  && ch[i] ==  0 ) { n             i = n + 1; n             ans = -1; n             break; n           } n           if (ch[j] ==  0  && ch[i] ==  1 ) g[j + 3 * n].push_back(j + n); n           if (ch[j] ==  1  && ch[i] ==  0 ) g[i + 2 * n].push_back(i); n           if (ch[j] ==  1  && ch[i] ==  1 ) n             g[j + n].push_back(i + 2 * n), g[i].push_back(j + 3 * n), n                 g[i + 2 * n].push_back(j + n), g[j + 3 * n].push_back(i); n         } n       } n     } n   if (ans) n     printf( %d , ans); n   else { n     for (int i = 1; i <= 4 * n; i++) n       if (!dfn[i]) tarjan(i, i); n     for (int i = 1; i <= 2 * n; i++) n       if (bj[i] == bj[i + 2 * n]) { n         ans = 1; n         break; n       } n     if (ans) n       printf( -1 ); n     else { n       for (int i = 1; i <= n; i++) n         if (bj[i] < bj[i + 2 * n]) ans++; n       for (int i = n + 1; i <= 2 * n; i++) n         if (bj[i] < bj[i + 2 * n]) ans++; n       printf( %d n , ans); n       for (int i = 1; i <= n; i++) n         if (bj[i] < bj[i + 2 * n]) printf( row %d n , i - 1); n       for (int i = n + 1; i <= 2 * n; i++) n         if (bj[i] < bj[i + 2 * n]) printf( col %d n , i - n - 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   vector<long long> v; n   long long h = 1, m; n   while (h <= 25820) { n     m = ((3 * h + 1) * h) / 2; n     v.push_back(m); n     h++; n   } n   for (long long j = 0; j < t; j++) { n     long long n, pos = 0; n     cin >> n; n     for (long long i = 0; i < v.size(); i++) { n       if (n <= v[i]) { n         pos = i; n         break; n       } n     } n     long long ans = 0; n     while (n > 0 && pos >= 0) { n       ans += (n / v[pos]); n       n %= v[pos]; n       pos--; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 111; n int op[123456], val[123456], b[123456]; n vector<int> x; n vector<int>::iterator it; n long long sum[333][5]; n int k[333], sumk; n map<int, int> ma; n char s[5]; n int main(void) { n   int i, j, n; n   int no, ni; n   long long res; n   scanf( %d , &n); n   x.clear(); n   for (i = 0; i < n; i++) { n     scanf(  %s , s); n     if (s[0] ==  a ) n       op[i] = 1; n     else if (s[0] ==  d ) n       op[i] = 2; n     else n       op[i] = 3; n     if (op[i] < 3) { n       scanf( %d , &val[i]); n       x.push_back(val[i]); n     } n   } n   sort(x.begin(), x.end()); n   it = unique(x.begin(), x.end()); n   x.resize(it - x.begin()); n   for (i = 0; i < x.size(); i++) ma[x[i]] = i; n   memset(sum, 0, sizeof(sum)); n   memset(k, 0, sizeof(k)); n   memset(b, 0, sizeof(b)); n   for (i = 0; i < n; i++) { n     if (op[i] == 1) { n       b[ma[val[i]]]++; n       no = ma[val[i]] / 320; n       k[no]++; n       for (j = 0; j < 5; j++) sum[no][j] = 0; n       j = 0; n       for (ni = 0; ni < 320; ni++) { n         if (b[no * 320 + ni]) { n           sum[no][j] = sum[no][j] + x[no * 320 + ni]; n           j++; n           if (j == 5) j = 0; n         } n       } n     } else if (op[i] == 2) { n       b[ma[val[i]]]--; n       no = ma[val[i]] / 320; n       k[no]--; n       for (j = 0; j < 5; j++) sum[no][j] = 0; n       j = 0; n       for (ni = 0; ni < 320; ni++) { n         if (b[no * 320 + ni]) { n           sum[no][j] = sum[no][j] + x[no * 320 + ni]; n           j++; n           if (j == 5) j = 0; n         } n       } n     } else if (op[i] == 3) { n       res = 0; n       sumk = 2; n       for (no = 0; no < 320; no++) { n         res += sum[no][4 - sumk % 5]; n         sumk += k[no]; n       } n       printf( %I64d n , res); n     } n   } n   return 0; n } n