#include <bits/stdc++.h> n using namespace std; n const int N = 5000005; n using ll = long long; n ll f[N], pre[N]; n int n, a[N]; n vector<int> ans; n void move(int x) { n   if (x == n || a[x] == 0 || a[x + 1] == 0) return; n   ans.push_back(x); n   int t = min(a[x], a[x + 1]); n   a[x] -= t; n   a[x + 1] -= t; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   f[2] = a[1]; n   for (int i = 3; i <= n + 1; i++) n     if (f[i - 3] + max(a[i - 1], a[i - 2]) < f[i - 2] + a[i - 1]) n       f[i] = f[pre[i] = i - 3] + max(a[i - 1], a[i - 2]); n     else n       f[i] = f[pre[i] = i - 2] + a[i - 1]; n   for (int i = f[n] < f[n + 1] ? n : n + 1; i > 1; i = pre[i]) n     if (pre[i] == i - 3) { n       move(i - 2), move(i - 1); n       if (i > 3) move(i - 3); n     } else { n       move(i - 1); n       if (i > 2) move(i - 2); n     } n   cout << ans.size() << endl; n   for (auto &i : ans) cout << i << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int ReadInt() { n   int i = 0; n   char c = 0; n   char flag = 1; n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     flag = 0; n     c = getchar(); n   } n   for (; c >=  0  && c <=  9 ; c = getchar()) i = i * 10 + c -  0 ; n   if (!flag) i = -i; n   return i; n } n inline void WriteInt(int n) { n   if (n < 0) { n     putchar( - ); n     WriteInt(-n); n     return; n   } n   if (n < 10) n     putchar(n +  0 ); n   else { n     WriteInt(n / 10); n     putchar(n % 10 +  0 ); n   } n } n int n, m; n int top; n long long ans, sum, last; n int A[100100]; n int tmp; n int main() { n   scanf( %i%i , &n, &m); n   for (int i = 0; i < n; i++) scanf( %i , &A[i]), sum += A[i]; n   sort(A, A + n); n   int top = 0; n   for (int i = 0; i < n; i++) n     if (A[i] > top) n       ans++, top++; n     else n       ans++; n   ans += A[n - 1] - top; n   printf( %I64d , sum - ans); n   putc(  n , stdout); n } n


#include <bits/stdc++.h> n using namespace std; n int Matrix[1002][2002]; n int max(int a, int b) { n   if (a > b) n     return a; n   else n     return b; n } n int min(int a, int b) { n   if (a < b) n     return a; n   else n     return b; n } n int distance(int a, int b, int c) { n   int first, second; n   first = a < b ? b - a : a - b; n   second = b < c ? c - b : b - c; n   return first + second; n } n int main() { n   int n, k, p; n   int person[1005]; n   int key[2002]; n   cin >> n >> k >> p; n   for (int i = 1; i <= n; i++) cin >> person[i]; n   for (int i = 1; i <= k; i++) cin >> key[i]; n   sort(person + 1, person + n + 1); n   sort(key + 1, key + k + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= k; j++) { n       if (i == j) { n         Matrix[i][j] = n             max(Matrix[i - 1][j - 1], distance(person[i], key[i], p)); n       } else { n         Matrix[i][j] = n             min(Matrix[i][j - 1], n                 max(Matrix[i - 1][j - 1], distance(person[i], key[j], p))); n       } n     } n   } n   int ans = 2e9; n   for (int i = n; i <= k; i++) ans = min(ans, Matrix[n][i]); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { return a > b ? a : b; } n long long min(long long a, long long b) { return a < b ? a : b; } n void fun(int arr[], int n) { n   stack<int> s; n   int left[n + 1]; n   int right[n + 1]; n   for (int i = 0; i < n; i++) { n     left[i] = -1; n     right[i] = n; n   } n   for (int i = 0; i < n; i++) { n     while (!s.empty() && arr[s.top()] >= arr[i]) s.pop(); n     if (!s.empty()) left[i] = s.top(); n     s.push(i); n   } n   while (!s.empty()) s.pop(); n   for (int i = n - 1; i >= 0; i--) { n     while (!s.empty() && arr[s.top()] >= arr[i]) s.pop(); n     if (!s.empty()) right[i] = s.top(); n     s.push(i); n   } n   int ans[n + 1]; n   for (int i = 0; i <= n; i++) ans[i] = 0; n   for (int i = 0; i < n; i++) { n     int len = right[i] - left[i] - 1; n     ans[len] = max(ans[len], arr[i]); n   } n   for (int i = n - 1; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]); n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t, n, k; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   fun(arr, n); n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int K; n int main() { n   scanf( %d , &K); n   if (K % 2) { n     puts( YES ); n     if (K == 1) { n       puts( 2 1 n1 2 ); n       return 0; n     } n     int i, j, N = (K << 1) + 4, n               M = (3 * K - 2 + (K - 1) * (K - 2) / 2 - K / 2) * 2 + 1, n               tmp1 = K + 1, tmp2 = K + 2; n     printf( %d %d n , N, M); n     for (i = 1; i < K; i++) n       printf( %d %d n%d %d n%d %d n%d %d n%d %d n%d %d n , K, i, K + tmp2, n              i + tmp2, tmp1, i, tmp1 + tmp2, i + tmp2, tmp2, i, tmp2 << 1, n              i + tmp2); n     printf( %d %d n%d %d n , tmp1, tmp2, tmp1 + tmp2, tmp2 << 1); n     int lim = K - 1; n     for (i = 1, tmp1 = K >> 1; i < lim; i++) n       for (j = i + 1; j < K; j++) n         if (j != i + tmp1) printf( %d %d n%d %d n , i, j, i + tmp2, j + tmp2); n     printf( %d %d n , K, K + tmp2); n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3050; n int F[N][N]; n char buf[N]; n vector<int> row[N]; n short S[N][N]; n inline short get(int y1, int x1, int y2, int x2) { n   return S[y2][x2] + S[y1][x1] - S[y2][x1] - S[y1][x2]; n } n int A[N]; n int n, m, k; n short to_L[N], to_R[N]; n short path[N]; n int pt = 0; n inline short getL(short x) { n   if (x == -1) return -1; n   if (to_L[x] == -1 || A[to_L[x]]) return to_L[x]; n   pt = 0; n   path[pt++] = x; n   while (true) { n     x = to_L[x]; n     if (x == -1 || A[x] != 0) n       break; n     else n       path[pt++] = x; n   } n   for (int i = 0; i < pt - 1; i++) to_L[path[i]] = x; n   return x; n } n short getR(short x) { n   if (x == m) return m; n   if (to_R[x] == m || A[to_R[x]]) return to_R[x]; n   pt = 0; n   path[pt++] = x; n   while (true) { n     x = to_R[x]; n     if (x == m || A[x] != 0) n       break; n     else n       path[pt++] = x; n   } n   for (int i = 0; i < pt - 1; i++) to_R[path[i]] = x; n   return x; n } n vector<short> visited; n short Rpos[N]; n int used[N]; n int curver = 0; n vector<short> interesting; n void calcLpos(short x) { n   visited.clear(); n   short cur = 0; n   while (true) { n     if (x == -1) return; n     if (used[x] == curver) return; n     used[x] = curver; n     visited.push_back(x); n     cur += A[x]; n     if (cur >= k) { n       x = getL(x); n       if (x != -1 && used[x] != curver) visited.push_back(x), used[x] = curver; n       return; n     } n     x = getL(x); n   } n   assert(false); n } n long long start(short r) { n   for (short j = 0; j < m; j++) { n     A[j] = get(r, j, n, j + 1); n   } n   short lst = m; n   for (short j = m - 1; j >= 0; j--) { n     to_R[j] = lst; n     if (A[j]) lst = j; n   } n   lst = -1; n   for (short j = 0; j < m; j++) { n     to_L[j] = lst; n     if (A[j]) lst = j; n   } n   lst = -1; n   for (short j = 0; j < m; j++) { n     if (A[j]) { n       Rpos[j] = max(j, lst); n       while (true) { n         short nrpos = getR(Rpos[j]); n         if (Rpos[j] == m || get(r, j, n, Rpos[j] + 1) >= k) { n           break; n         } else { n           Rpos[j] = nrpos; n         } n       } n       lst = Rpos[j]; n     } n   } n   int curval = 0; n   for (short j = 0; j < m; j++) { n     if (A[j]) { n       short pj = getL(j); n       short from = Rpos[j]; n       curval += (int)(j - pj) * (m - from); n     } n   } n   long long ans = 0; n   ans += curval; n   for (int i = n - 1; i > r; i--) { n     interesting.clear(); n     ++curver; n     for (short x : row[i]) { n       calcLpos(x); n       interesting.insert(interesting.end(), visited.rbegin(), visited.rend()); n       short r = getR(x); n       if (r != m && used[r] != curver) { n         used[r] = curver; n         interesting.push_back(r); n       } n     } n     for (short j : interesting) { n       short pj = getL(j); n       short from = Rpos[j]; n       curval -= (int)(j - pj) * (m - from); n     } n     for (short x : row[i]) { n       A[x]--; n     } n     short lst = -1; n     for (short j : interesting) { n       if (!A[j]) continue; n       short pj = getL(j); n       Rpos[j] = max(Rpos[j], lst); n       while (true) { n         short nrpos = getR(Rpos[j]); n         if (Rpos[j] == m || get(r, j, i, Rpos[j] + 1) >= k) { n           break; n         } else { n           Rpos[j] = nrpos; n         } n       } n       lst = Rpos[j]; n       short from = Rpos[j]; n       curval += (int)(j - pj) * (m - from); n     } n     ans += curval; n   } n   return ans; n } n int main() { n   int tmp; n   scanf( %d %d %d %d  , &n, &m, &tmp, &k); n   for (int i = 0; i < tmp; i++) { n     int y, x; n     scanf( %d %d , &y, &x); n     --y, --x; n     F[y][x] = 1; n     row[y].push_back(x); n   } n   for (int i = 0; i < n; i++) { n     sort(row[i].begin(), row[i].end()); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       S[i + 1][j + 1] = S[i + 1][j] + S[i][j + 1] - S[i][j] + F[i][j]; n     } n   } n   long long ans = 0; n   long long last = start(n - 1); n   ans = last; n   for (int i = n - 2; i >= 0; i--) { n     if (i % 10 == 0) { n     } n     if (row[i].empty()) n       ans += last; n     else { n       ans += (last = start(i)); n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long n, tim; n string st; n long lpos, calc1, calc2; n long need, ans, calc; n long l, r, has, bad; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> tim; n   cin >> st; n   lpos = -1; n   for (int i = 0; i < st.size(); i++) { n     if (st[i] ==  H ) ++calc1; n     if (st[i] ==  H ) lpos = i; n     if (st[i] ==  S ) ++calc2; n   } n   if (tim <= lpos) { n     cout << -1 << endl; n     return 0; n   } n   l = 0; n   r = 1e6; n   if (calc1 > calc2) l = calc1 - calc2; n   while (l < r) { n     long mid = l + r; n     mid /= 2; n     calc = 0; n     has = mid; n     bad = 1e9; n     need = 0; n     long oscore = 1e9; n     long tt = mid; n     long epos = 0; n     for (int i = 0; i < st.size(); i++) { n       if (tt < calc1) epos = i; n       if (st[i] ==  S ) ++tt; n     } n     if (epos < lpos) epos = lpos; n     for (long i = 0; i < st.size(); i++) { n       ++calc; n       if (bad < 1e7) n         oscore = min(oscore, epos - i + calc + (epos - bad) * (bad < 1e7)); n       if (st[i] ==  H ) { n         if (has == 0) { n           bad = min(bad, i); n           need++; n         } else { n           --has; n         } n       } n       if (st[i] ==  S ) { n         ++has; n         if (has >= need && i >= lpos) { n           if (need) calc += i - bad; n           break; n         } n         if (has >= need && need > 0) { n           calc += 2 * (i - bad); n           bad = 1e9; n           has -= need; n           need = 0; n         } n       } n       if (i >= lpos && has >= need) { n         if (need) calc += i - bad; n         break; n       } n     } n     calc = min(calc, oscore); n     if (calc > tim) n       l = mid + 1; n     else n       r = mid; n   } n   cout << l << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     cout << 1 <<    ; n     for (int i = 4; i < 2 * n; i += 4) cout << i <<     << i + 1 <<    ; n     for (int i = 2; i < 2 * n; i += 4) cout << i <<     << i + 1 <<    ; n     cout << 2 * n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, SF, FS; n   SF = FS = 0; n   string flights; n   cin >> n >> flights; n   for (int i = 0; i < flights.size() - 1; i++) { n     if (flights[i] ==  S  && flights[i + 1] ==  F ) { n       SF++; n     } else if (flights[i] ==  F  && flights[i + 1] ==  S ) { n       FS++; n     } n   } n   if (SF > FS) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n struct Node { n   int id, w; n }; n vector<int> g[maxn]; n vector<Node> gg; n bool vis[maxn]; n int dfs(int u) { n   vis[u] = 1; n   for (int i : g[u]) { n     if (!vis[i]) { n       return dfs(i); n     } n   } n   return u; n } n int main(int argc, const char* argv[]) { n   ios::sync_with_stdio(false); n   int n, a, b; n   Node tmp; n   cin >> n; n   for (int i = 0; i < n - 1; ++i) { n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   int t = 0; n   for (int i = 1; i <= n; ++i) { n     int tt = g[i].size(); n     tmp.id = i; n     tmp.w = tt; n     gg.push_back(tmp); n     if (tt > 2) { n       ++t; n     } n     if (t >= 2) { n       break; n     } n   } n   if (t >= 2) { n     cout <<  No  << endl; n   } else { n     cout <<  Yes  << endl; n     sort(gg.begin(), gg.end(), [&](Node a, Node b) { return a.w > b.w; }); n     if (t) n       tmp = gg[0]; n     else n       tmp = gg[n - 1]; n     vis[tmp.id] = 1; n     cout << tmp.w << endl; n     for (int i : g[tmp.id]) { n       cout << tmp.id <<     << dfs(i) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n int a[200007]; n long long fac[200007]; n long long inv[200007]; n long long pw2[200007]; n long long free_pw[200007]; n long long fastpow(long long x, long long st) { n   long long ret = 1; n   while (st > 0) { n     if ((st & 1) != 0) { n       ret = (ret * x) % 998244353; n       --st; n     } else { n       x = (x * x) % 998244353; n       st >>= 1; n     } n   } n   return ret; n } n long long get_comb(long long x, long long y) { n   if (x < y) { n     return 0; n   } n   long long ret = fac[x]; n   ret = (ret * inv[y]) % 998244353; n   ret = (ret * inv[x - y]) % 998244353; n   return ret; n } n void input() { n   scanf( %I64d%I64d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n   } n   a[n + 1] = a[1]; n   fac[0] = pw2[0] = free_pw[0] = 1; n   for (int i = 1; i <= n; ++i) { n     fac[i] = (fac[i - 1] * i) % 998244353; n     pw2[i] = (pw2[i - 1] * 2) % 998244353; n     free_pw[i] = (free_pw[i - 1] * (k - 2)) % 998244353; n   } n   inv[n] = fastpow(fac[n], 998244353 - 2); n   for (int i = n - 1; i >= 0; --i) { n     inv[i] = (inv[i + 1] * (i + 1)) % 998244353; n   } n } n void solve() { n   int free = 0; n   long long aux = 1; n   for (int i = 1; i <= n; ++i) { n     if (a[i] == a[i + 1]) { n       ++free, aux = (aux * k) % 998244353; n     } n   } n   long long ans = 0; n   for (int i = 0; i < n - free; ++i) { n     long long w = (get_comb(n - free, i) * free_pw[i]) % 998244353; n     int lft = n - free - i; n     long long coef = pw2[lft]; n     if ((lft & 1) == 0) { n       coef = (coef - get_comb(lft, lft / 2) + 998244353) % 998244353; n     } n     coef = (coef * inv[2]) % 998244353; n     ans = (ans + (w * coef) % 998244353) % 998244353; n   } n   printf( %I64d n , (aux * ans) % 998244353); n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 0; n   cin >> n; n   n -= 10; n   if (n == 11) { n     ans += 4; n     n -= 11; n   } n   if (n == 10) { n     ans = (4 * 4) - 1; n     n -= 10; n   } n   for (int i = 9; i >= 1; --i) { n     if (n == i) { n       ans += 4; n       n -= i; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dist[5001][5001]; n double x[5001], y[5001], z[5001]; n int n; n double sqr(double x) { return x * x; } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> z[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       dist[i][j] = sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]) + sqr(z[i] - z[j])); n       dist[j][i] = dist[i][j]; n     } n   } n   double distance = 100000; n   for (int i = 2; i <= n; i++) { n     double minn = 100000; n     for (int j = 2; j <= n; j++) { n       if (i != j) { n         if (dist[1][j] + dist[j][i] < minn) minn = dist[1][j] + dist[j][i]; n       } n     } n     if (dist[1][i] + (minn - dist[1][i]) / 2 < distance) n       distance = dist[1][i] + (minn - dist[1][i]) / 2; n   } n   cout.precision(20); n   cout << distance; 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   ; n   int n, m; n   cin >> n >> m; n   string s, t; n   cin >> s >> t; n   int mincnt = n + 1; n   int nin = -1; n   for (int i = 0; i <= m - n; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (s[j] != t[i + j]) cnt++; n     } n     if (mincnt > cnt) { n       mincnt = cnt; n       nin = i; n     } n   } n   vector<long long> a; n   int i = nin; n   for (int k = i; k <= n + i - 1; k++) n     if (s[k - i] != t[k]) a.push_back(k - i); n   cout << a.size() <<   n ; n   for (int i = 0; i < mincnt; i++) cout << a[i] + 1 <<    ; n } n


#include <bits/stdc++.h> n int days(int y) { n   if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) { n     return 366; n   } else { n     return 365; n   } n } n int main() { n   int year, i, d, di, dii; n   unsigned long long int count = 0; n   scanf( %d , &year); n   d = days(year); n   di = d; n   for (i = year;; i++) { n     dii = days(i + 1); n     count += di; n     if (count % 7 == 0 && d == dii) { n       printf( %d n , i + 1); n       return 0; n     } n     di = dii; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 28; n const double INF = 1e12, EPS = 1e-9; n int n, m, c[101]; n void run() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     for (; a <= b; a++) c[a]++; n   } n   for (int i = 1; i <= n; i++) n     if (c[i] != 1) { n       cout << i <<     << c[i] << endl; n       return; n     } n   cout <<  OK  << endl; n } n int main() { run(); } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[1501]; n int calcinv() { n   int j; n   int rez = 0; n   for (int i = 1; i <= n; i++) n     for (j = i + 1; j <= n; j++) n       if (a[i] > a[j]) rez++; n   return rez; n } n int main() { n   int i, nri, j, m, l, r, lg; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   nri = calcinv() % 2; n   cin >> m; n   for (i = 1; i <= m; i++) { n     cin >> l >> r; n     lg = r - l; n     lg = lg * (lg + 1) / 2; n     nri = (nri + lg) % 2; n     if (nri == 0) n       cout <<  even ; n     else n       cout <<  odd ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int &a, int &b) { n   int temp = a; n   a = b; n   b = temp; n } n int main() { n   int n; n   cin >> n; n   int x[n], y[n], min_x = INT_MAX, max_y = INT_MIN; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     if (x[i] < min_x) min_x = x[i]; n     if (y[i] > max_y) max_y = y[i]; n   } n   int k = 0, res = -1; n   while (k < n) { n     if (x[k] == min_x) { n       if (y[k] == max_y) { n         res = k + 1; n         break; n       } n     } n     k++; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n void no() { n   cout <<  NO n ; n   exit(0); n } n int main() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   sort(arr.begin(), arr.end()); n   for (int i = n - 1; i >= 0; --i) { n     arr[i] -= arr[0]; n     if (arr[i] >= n) no(); n   } n   vector<int> cnt(n); n   for (int i = 0; i < n; ++i) cnt[arr[i]]++; n   int mx = arr[n - 1]; n   for (int i = 1; i < mx; ++i) { n     if (cnt[i] < 2) no(); n     cnt[i] -= 2; n   } n   cnt[0]--; n   cnt[mx]--; n   for (int i = 0; i <= mx; ++i) { n     while (cnt[i] > 0) { n       if (cnt[i + 1] == 0) no(); n       cnt[i]--; n       cnt[i + 1]--; n     } n   } n   cout <<  YES n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 100 + 10; n const long long INF = 1000 * 1000 * 1000LL * 1000 * 10000; n int n, q, pt = 1, cnt; n vector<long long> ans; n set<pair<long long, long long> > s; n set<pair<long long, long long> >::iterator it1, it2; n long long po[100], a[N], sum[N * 4][2]; n pair<long long, long long> chng[N * 4][2], minn[N * 4][2]; n void shift(int ind, bool o, int b, int mid) { n   if (chng[ind][o].first) { n     chng[ind * 2][o] = chng[ind * 2 + 1][o] = chng[ind][o]; n     minn[ind * 2][o] = make_pair(chng[ind][o].second, b); n     minn[ind * 2 + 1][o] = make_pair(chng[ind][o].second, mid); n     sum[ind * 2][o] = sum[ind * 2 + 1][o] = 0; n     chng[ind][o] = make_pair(0, 0); n   } n   long long t = sum[ind][o]; n   sum[ind * 2][o] += t; n   sum[ind * 2 + 1][o] += t; n   minn[ind * 2][o].first += t; n   minn[ind * 2 + 1][o].first += t; n   sum[ind][o] = 0; n   return; n } n void add(int l, int r, pair<long long, long long> val, bool o, int b = 0, n          int e = n, int ind = 1) { n   if (l <= b && e <= r) { n     minn[ind][o].first += val.second; n     sum[ind][o] += val.second; n     return; n   } n   int mid = (b + e) / 2; n   shift(ind, o, b, mid); n   if (l < mid) add(l, r, val, o, b, mid, ind * 2); n   if (r > mid) add(l, r, val, o, mid, e, ind * 2 + 1); n   minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); n   return; n } n void sett(int l, int r, pair<long long, long long> val, bool o, int b = 0, n           int e = n, int ind = 1) { n   if (l <= b && e <= r) { n     chng[ind][o] = val; n     sum[ind][o] = 0; n     minn[ind][o] = make_pair(val.second, b); n     return; n   } n   int mid = (b + e) / 2; n   shift(ind, o, b, mid); n   if (l < mid) sett(l, r, val, o, b, mid, ind * 2); n   if (r > mid) sett(l, r, val, o, mid, e, ind * 2 + 1); n   minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); n } n pair<long long, long long> getmin(int l, int r, int b = 0, int e = n, n                                   int ind = 1) { n   pair<long long, long long> ret = make_pair(INF, INF); n   if (l <= b && e <= r) return minn[ind][0]; n   int mid = (b + e) / 2; n   shift(ind, 0, b, mid); n   if (l < mid) ret = min(ret, getmin(l, r, b, mid, ind * 2)); n   if (r > mid) ret = min(ret, getmin(l, r, mid, e, ind * 2 + 1)); n   return ret; n } n long long get(int p, bool o, int b = 0, int e = n, int ind = 1) { n   if (b + 1 == e) return sum[ind][o] + chng[ind][o].second; n   int mid = (b + e) / 2; n   shift(ind, o, b, mid); n   if (p < mid) return get(p, o, b, mid, ind * 2); n   return get(p, o, mid, e, ind * 2 + 1); n } n long long type1(int x) { n   x--; n   return po[get(x, 1)] - get(x, 0); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> q; n   po[0] = 1; n   for (int i = 1; i <= 12; i++) po[i] = po[i - 1] * 42; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] = 1 - a[i]; n     sett(i, i + 1, make_pair(pt++, a[i]), 0); n     s.insert(make_pair(i, i + 1)); n   } n   long long tp, x, l, r; n   for (int i = 0; i < q; i++) { n     cin >> tp; n     if (tp == 1) { n       cin >> x; n       ans.push_back(type1(x)); n     } else { n       cin >> l >> r >> x; n       l--; n       if (tp == 2) { n         sett(l, r, make_pair(pt++, 1 - x), 0); n         sett(l, r, make_pair(pt++, 0), 1); n         s.insert(make_pair(l, r)); n         it1 = it2 = s.find(make_pair(l, r)); n         if (it1 != s.begin() && (*--it1).second > l) { n           pair<long long, long long> temp = *it1; n           s.erase(it1); n           it1 = s.find(make_pair(l, r)); n           if (temp.first < l) s.insert(make_pair(temp.first, l)); n           if (temp.second > r) s.insert(make_pair(r, temp.second)); n         } n         it2++; n         while (it2 != s.end() && (*it2).first < r) { n           pair<long long, long long> temp = *it2; n           s.erase(it2); n           it2 = ++s.find(make_pair(l, r)); n           if (temp.second > r) s.insert(make_pair(r, temp.second)); n         } n       } else { n         bool flag = 1; n         pair<long long, long long> t1 = *--s.upper_bound(make_pair(l, n + 1)); n         if (t1.first != l) { n           s.erase(t1); n           s.insert(make_pair(t1.first, l)); n           s.insert(make_pair(l, t1.second)); n         } n         pair<long long, long long> t2 = *--s.upper_bound(make_pair(r, n + 1)); n         if (t2.first != r) { n           s.erase(t2); n           s.insert(make_pair(t2.first, r)); n           if (t2.second != r) s.insert(make_pair(r, t2.second)); n         } n         while (flag) { n           add(l, r, make_pair(pt++, -x), 0); n           pair<long long, long long> temp = getmin(l, r); n           while (temp.first < 0) { n             pair<long long, long long> seg = n                 *--s.upper_bound(make_pair(temp.second, n + 1)); n             int pnt = get(temp.second, 1); n             add(seg.first, seg.second, make_pair(pt++, po[pnt + 1] - po[pnt]), n                 0); n             sett(seg.first, seg.second, make_pair(pt++, pnt + 1), 1); n             temp = getmin(l, r); n           } n           if (temp.first != 0) flag = 0; n         } n       } n     } n   } n   for (long long u : ans) cout << u << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int B = 12; n const int D = 64; n const int MASK = 1030; n int q, b; n long long f[B][D][MASK]; n long long sum[B][D]; n long long call(int base, int rem, int mask) { n   if (rem <= 0) return mask == 0; n   long long &ret = f[base][rem][mask]; n   if (ret != -1) return ret; n   ret = 0; n   for (int i = 0; i < base; ++i) { n     ret += call(base, rem - 1, mask ^ 1 << i); n   } n   return ret; n } n long long solve(long long x) { n   if (x == 0) return 0; n   vector<int> v; n   while (x) { n     v.push_back(x % b); n     x /= b; n   } n   reverse(v.begin(), v.end()); n   int mask = 0, sz = v.size(); n   long long ans = 0; n   for (int i = 0; i < sz; ++i) { n     mask ^= 1 << v[i]; n     if (i + 1 < sz) { n       for (int j = 0; j < v[i + 1]; ++j) { n         ans += call(b, sz - 2 - i, mask ^ 1 << j); n       } n     } else { n       ans += (mask == 0); n     } n   } n   for (int i = 1; i < v[0]; ++i) { n     ans += call(b, sz - 1, 1 << i); n   } n   for (int i = 1; i < sz; ++i) { n     for (int j = 1; j < b; ++j) { n       ans += call(b, sz - 1 - i, 1 << j); n     } n   } n   return ans; n } n int main() { n   memset(f, -1, sizeof f); n   memset(sum, -1, sizeof sum); n   scanf( %d , &q); n   while (q--) { n     long long l, r; n     scanf( %d %lld %lld , &b, &l, &r); n     printf( %lld n , solve(r) - solve(l - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a, b, c, result = 0, i, x, y, j = 0; n   cin >> n >> a >> b >> c; n   for (i = 0; i <= min(n, a / 2); i++) { n     x = min(c, (n - i) / 2); n     y = min(i + b, n); n     if (y - (n - x * 2) / 2 >= 0) { n       if (y >= n - x * 2) result += (y - (n - x * 2)) / 2 + 1; n     } n   } n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T BM(T p, T e, T M) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T mdINV(T a, T M) { n   return BM(a, M - 2, M); n } n template <class T> n inline T PW(T p, T e) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p); n     p = (p * p); n   } n   return (T)ret; n } n template <class T> n string NTS(T Number) { n   stringstream ss; n   ss << Number; n   return ss.str(); n } n template <class T> n T stringtonumber(const string &Text) { n   istringstream ss(Text); n   T result; n   return ss >> result ? result : 0; n } n template <class T> n bool ISLEFT(T a, T b, T c) { n   if (((a.first.first - b.first.first) * (b.second.second - c.second.second) - n        (b.first.first - c.first.first) * (a.second.second - b.second.second)) < n       0.0) n     return 1; n   else n     return 0; n } n int n, k; n vector<int> v[300ll + 2]; n long long int dp[102][44]; n void go(int u, int p) { n   dp[u][0] = 1; n   dp[u][k + 1] = 1; n   for (int i = 0; i < v[u].size(); i++) { n     int first = v[u][i]; n     if (first == p) continue; n     go(first, u); n     long long int kop[44]; n     memset(kop, 0, sizeof kop); n     for (int j = 0; j <= k * 2; j++) { n       for (int l = 0; l <= k * 2; l++) { n         if (j + l <= k * 2) { n           kop[min(j, l + 1)] = n               (kop[min(j, l + 1)] + dp[u][j] * dp[first][l]) % 1000000007; n         } else { n           kop[max(j, l + 1)] = n               (kop[max(j, l + 1)] + dp[u][j] * dp[first][l]) % 1000000007; n         } n       } n     } n     for (int j = 0; j <= k * 2; j++) dp[u][j] = kop[j]; n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n - 1; i++) { n     int first, second; n     scanf( %d%d , &first, &second); n     v[first].push_back(second); n     v[second].push_back(first); n   } n   go(1, -1); n   int ans = 0; n   for (int i = 0; i <= k; i++) { n     ans += dp[1][i]; n     if (ans >= 1000000007) ans -= 1000000007; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int b, g, n; n   scanf( %d%d%d , &b, &g, &n); n   printf( %d n , min(b, n) - (n - min(g, n)) + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cf[45], nc[45]; n vector<int> ppf[45]; n bool canadd(int i, int m) { n   for (int p : ppf[m]) { n     if (nc[p] && cf[p] != i % p) return false; n   } n   return true; n } n void add(int i, int m) { n   for (int p : ppf[m]) { n     if (nc[p] == 0) cf[p] = i % p; n     nc[p]++; n   } n } n void rem(int m) { n   for (int p : ppf[m]) { n     nc[p]--; n   } n } n vector<pair<int, int> > loc[100005]; n int k[100005]; n int n, m; n int main() { n   for (int i = (2); i < (45); i++) { n     int x = i; n     for (int j = (2); j < (45); j++) { n       int fct = 1; n       while (x % j == 0) { n         fct *= j; n         ppf[i].push_back(fct); n         x /= j; n       } n     } n   } n   scanf( %d %d , &n, &m); n   for (int i = (0); i < (n); i++) { n     scanf( %d , k + i); n     for (int j = (0); j < (k[i]); j++) { n       int x; n       scanf( %d , &x); n       loc[x].push_back(make_pair(i, j)); n     } n   } n   for (int x = (1); x < (m + 1); x++) { n     queue<pair<int, int> > cq; n     int prv = -1, ans = 0; n     for (auto l : loc[x]) { n       int i, j; n       tie(i, j) = l; n       if (i != prv + 1) { n         while (int(cq.size())) { n           int fi, fj; n           tie(fi, fj) = cq.front(); n           cq.pop(); n           rem(k[fi]); n         } n       } n       while (!canadd(j, k[i])) { n         int fi, fj; n         tie(fi, fj) = cq.front(); n         cq.pop(); n         rem(k[fi]); n       } n       add(j, k[i]); n       cq.emplace(i, j); n       prv = i; n       ans = max(ans, int(cq.size())); n     } n     while (int(cq.size())) { n       int fi, fj; n       tie(fi, fj) = cq.front(); n       cq.pop(); n       rem(k[fi]); n     } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n, num; n   cin >> t; n   while (t--) { n     cin >> n; n     bool flag = false; n     unordered_map<int, vector<int>> m; n     for (int i = 0; i < n; ++i) { n       cin >> num; n       m[num].push_back(i); n     } n     for (auto& k : m) { n       if (k.second.back() - k.second.front() >= 2) { n         flag = true; n         break; n       } n     } n     if (flag) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 61 * 30 + 5; n pair<int, int> C[62]; n int M, A[32], cc; n bool dp[32][62][maxn], D[62][62]; n short int fr[32][62][maxn]; n int main() { n   scanf( %d , &M); n   for (int i = 1; i <= M; ++i) scanf( %d , A + i); n   sort(A + 1, A + M + 1); n   dp[0][0][0] = true; n   for (int i = 1; i <= M; ++i) n     for (int j = i; j <= 61; ++j) n       for (int k = (j * (j - 1)) >> 1; k < maxn; ++k) { n         for (int m = 1; m <= j; ++m) n           if (k >= m * A[i] && dp[i - 1][j - m][k - m * A[i]]) { n             dp[i][j][k] = true; n             fr[i][j][k] = m; n             break; n           } n       } n   int N = M; n   while (!dp[M][N][(N * (N - 1)) >> 1]) ++N; n   printf( %d n , N); n   for (int i = M, j = N, k = (N * (N - 1)) >> 1; i; --i) { n     int c = fr[i][j][k]; n     j -= c; n     k -= c * A[i]; n     for (int j = 0; j < c; ++j) { n       ++cc; n       C[cc] = make_pair(A[i], cc); n     } n   } n   for (int i = 1; i <= N; ++i) { n     sort(C + 1, C + cc + 1); n     for (int j = i + 1; j <= i + C[i].first; ++j) n       D[C[i].second][C[j].second] = true; n     for (int j = i + C[i].first + 1; j <= N; ++j) n       D[C[j].second][C[i].second] = true, --C[j].first; n     C[i].first = -1; n   } n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= N; ++j) printf( %d , D[i][j] ? 1 : 0); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const char *par =  () ; n const int pm[] = {1, -1}; n int n; n int mn, res, fir; n string s; n vector<int> v, w; n map<int, int> mp; n int init(string s) { n   v = vector<int>(n); n   for (int i = 0, pre = 0; i < n; ++i) { n     v[i] = (pre += pm[strchr(par, s[i]) - par]); n     mp[v[i]]++; n   } n   if (v.back() != 0) return 0; n   res = mp.begin()->second; n   mn = mp.begin()->first; n   for (int i = 0; i < n; ++i) n     if (v[i] == mn) { n       fir = (i + 1) % n; n       return res; n     } n } n int count(int l, int r) { n   int cur = v[(l - 1 + n) % n]; n   s[l] =  ) ; n   for (int i = l; i != r; i = (i + 1) % n) { n     cur += pm[strchr(par, s[i]) - par]; n     if (!--mp[v[i]]) mp.erase(v[i]); n     mp[cur]++; n   } n   int ret = mp.begin()->second; n   cur = v[(l - 1 + n) % n]; n   for (int i = l; i != r; i = (i + 1) % n) { n     cur += pm[strchr(par, s[i]) - par]; n     if (!--mp[cur]) mp.erase(cur); n     mp[v[i]]++; n   } n   s[l] =  ( ; n   return ret; n } n int main() { n   cin >> n >> s; n   if (!init(s)) { n     cout <<  0 n1 1 n ; n     return 0; n   } n   stack<int> st; n   int i = fir; n   w = vector<int>(n); n   do { n     if (s[i] ==  ( ) st.push(i); n     if (s[i] ==  ) ) { n       w[i] = st.top(); n       w[w[i]] = i; n       st.pop(); n     } n   } while ((i = (i + 1) % n) != fir); n   int ans[3] = {res}; n   for (int i = 0; i < n; ++i) n     if (s[i] ==  (  && v[i] <= mn + 2) { n       int ret = count(i, w[i]); n       if (ret > ans[0]) ans[0] = ret, ans[1] = i, ans[2] = w[i]; n     } n   cout << ans[0] << endl << ans[1] + 1 <<     << ans[2] + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first != b.first) return a.first > b.first; n   return a.second < b.second; n } n int a[1000005], b[1000005]; n char s[1000005], r[1000005]; n int test; n int main() { n   int t, n, m, k, l, x, y, z, flag = 0, count, d, mx, mn; n   long long int sum, prod; n   scanf( %s%s , s, r); n   count = sum = 0; n   n = strlen(s), m = strlen(r); n   int j = 0; n   for (int i = 0; i < m; i++) { n     if (r[i] == s[j]) j++; n     if (j == n) break; n   } n   printf( %d , j + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200005]; n void slove() { n   int temp, u, v, w, m; n   for (int i = 1; i <= n; i++) cin >> temp, a[i] = 0x3f3f3f3f; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v >> w; n     if (a[v] > w) a[v] = w; n   } n   int flag = 1, k = 1; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0x3f3f3f3f && k) { n       ans -= 0x3f3f3f3f; n       k = 0; n     } else if (a[i] == 0x3f3f3f3f) { n       flag = 0; n       break; n     } n     ans += a[i]; n   } n   if (flag) n     cout << ans <<   n ; n   else n     cout << -1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   slove(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (1ll << 60); n const int N = 205; n int n, m, _len; n int nxt[N]; n long long k; n long long ADD(long long x, long long y) { return min(INF, x + y); } n struct node { n   char pref[N]; n   char suff[N]; n   long long len; n   long long sum; n } t[N * 4], eddd; n char s[N * 4]; n char ans[N]; n node operator+(node a, node b) { n   node ans; n   ans.len = ADD(a.len, b.len); n   ans.sum = ADD(a.sum, b.sum); n   int l1 = min(a.len, 2ll * _len); n   int l2 = min(b.len, 2ll * _len); n   for (int i = (int)(1); i <= (int)(min(l1, _len)); i++) s[i] = a.pref[i]; n   for (int i = (int)(1); i <= (int)(min(l1, _len)); i++) n     s[l1 - i + 1] = a.suff[i]; n   for (int i = (int)(1); i <= (int)(min(l2, _len)); i++) s[l1 + i] = b.pref[i]; n   for (int i = (int)(1); i <= (int)(min(l2, _len)); i++) n     s[l1 + l2 - i + 1] = b.suff[i]; n   int p = 0; n   s[l1 + l2 + 1] = 0; n   for (int i = (int)(l1 - min(l1, _len - 1) + 1); n        i <= (int)(l1 + 1 + min(l2, _len - 1) - 1); i++) { n     for (; p && s[i] != ::ans[p + 1]; p = nxt[p]) n       ; n     if (s[i] == ::ans[p + 1]) ++p; n     if (p == _len) ++ans.sum, p = nxt[p]; n   } n   for (int i = (int)(1); i <= (int)(min(_len, l1 + l2)); i++) n     ans.pref[i] = s[i]; n   for (int i = (int)(1); i <= (int)(min(_len, l1 + l2)); i++) n     ans.suff[i] = s[l1 + l2 - i + 1]; n   return ans; n } n long long count(int l) { n   _len = l; n   nxt[1] = 0; n   for (int i = (int)(2); i <= (int)(l); i++) { n     int j = nxt[i - 1]; n     for (; j && ans[j + 1] != ans[i]; j = nxt[j]) n       ; n     nxt[i] = j + (ans[j + 1] == ans[i]); n   } n   t[0].pref[1] = t[0].suff[1] =  0 ; n   t[1].pref[1] = t[1].suff[1] =  1 ; n   eddd.pref[1] = eddd.suff[1] =  2 ; n   t[0].len = t[1].len = eddd.len = 1; n   t[0].sum = (l == 1 && ans[1] ==  0 ); n   t[1].sum = (l == 1 && ans[1] ==  1 ); n   eddd.sum = (l == 1 && ans[1] ==  2 ); n   for (int i = (int)(2); i <= (int)(n); i++) { n     t[i] = t[i - 2] + t[i - 1]; n   } n   return (t[n] + eddd).sum; n } n int main() { n   scanf( %d%lld%d , &n, &k, &m); n   k++; n   for (int i = (int)(1); i <= (int)(m); i++) { n     ans[i] =  2 ; n     long long val = count(i); n     if (val >= k) break; n     k -= val; n     ans[i] =  0 ; n     val = count(i); n     if (val >= k) { n       putchar( 0 ); n       continue; n     } n     k -= val; n     ans[i] =  1 ; n     val = count(i); n     if (val >= k) { n       putchar( 1 ); n       continue; n     } n     assert(0); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int B = 27397, MOD = 1e9 + 7; n const int B1 = 33941, MOD1 = 1e9 + 9; n int t; n inline void solve() { n   int r, n; n   scanf( %d%d , &n, &r); n   --r; n   int sum = 0; n   for (int i = 0; i < n - 1; ++i) { n     int x; n     scanf( %d , &x); n     --x; n     sum += x; n   } n   for (int i = 0; i < n; ++i) n     if ((sum + i) % n == r) printf( %d n , i + 1); n } n int main(void) { n   scanf( %d , &t); n   while (t-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, long long> > > g; n int n, a[100005]; n bool erased[100005]; n void dfs(int cur, long long cost, long long minCost) { n   if (cost - minCost > 1ll * a[cur]) { n     erased[cur] = 1; n     return; n   } n   for (int i = 0; i < g[cur].size(); i++) { n     dfs(g[cur][i].first, cost + g[cur][i].second, min(minCost, cost)); n   } n } n int count(int u) { n   if (erased[u]) return 0; n   int ans = 1; n   for (int i = 0; i < g[u].size(); i++) ans += count(g[u][i].first); n   return ans; n } n int main() { n   scanf( %d , &n); n   g.assign(n, vector<pair<int, long long> >()); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int v, c; n     scanf( %d %d , &v, &c); n     v--; n     g[v].push_back(pair<int, long long>(i, c)); n   } n   memset(erased, 0, sizeof erased); n   dfs(0, 0ll, 0ll); n   printf( %d n , n - count(0)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long N = 1e5 + 10; n long long power(long long a, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret *= a; n     a *= a; n     if (ret >= MOD) ret %= MOD; n     if (a >= MOD) a %= MOD; n     b >>= 1; n   } n   return ret; n } n long long invmod(long long x) { return power(x, MOD - 2); } n long long dp[55][2]; n long long a[55]; n long long n; n long long go(long long pos, long long check) { n   if (pos == n + 1) return 0; n   if (dp[pos][check] != -1) return dp[pos][check]; n   long long ans = 0; n   if (check == 0) { n     ans += max(a[pos] + go(pos + 1, check ^ 1), go(pos + 1, check)); n   } else { n     ans += min(go(pos + 1, check ^ 1), a[pos] + go(pos + 1, check)); n   } n   dp[pos][check] = ans; n   return ans; n } n int32_t main() { n   cin >> n; n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   memset(dp, -1, sizeof(dp)); n   long long k = go(1, 0); n   cout << sum - k <<     << k << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long m, n, b[12345], w[12345], u, v, d = 0, c = 0; n   int mx = INT_MIN, mn = INT_MAX; n   string p[123]; n   char ch; n   bool sorted; n   cin >> n >> m; n   vector<int> a(n); n   for (int row = 0; row < n; row++) { n     cin >> p[row]; n     for (int col = 0; col < m; col++) { n       if (p[row][col] ==  * ) { n         c = c ^ row; n         d = d ^ col; n       } n     } n   } n   cout << c + 1 <<     << d + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int top = 0; n int N, Q, NN; n int K, M, R; n vector<int> Adj[100005]; n int dep[100005]; n int anc[100005][21]; n int pre[100005], pst[100005], tt; n void dfs(int u) { n   pre[u] = ++tt; n   for (int j = 1; j <= 20; j++) { n     if (dep[u] <= (1 << j)) break; n     anc[u][j] = anc[anc[u][j - 1]][j - 1]; n   } n   int v; n   for (int k = 0; k < Adj[u].size(); k++) { n     v = Adj[u][k]; n     if (v == anc[u][0]) continue; n     anc[v][0] = u; n     dep[v] = dep[u] + 1; n     dfs(v); n   } n   pst[u] = ++tt; n } n int lca(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   for (int j = 20; j >= 0; j--) { n     if (dep[anc[u][j]] >= dep[v]) { n       u = anc[u][j]; n     } n   } n   if (u == v) return u; n   for (int j = 20; j >= 0; j--) { n     if (anc[u][j] != anc[v][j]) { n       u = anc[u][j]; n       v = anc[v][j]; n     } n   } n   return anc[u][0]; n } n int h[100005], c[2 * 100005]; n int lbt(int x) { return x & (-x); } n void update(int x, int dv) { n   for (; x <= NN; x += lbt(x)) { n     c[x] += dv; n   } n } n int getsum(int x) { n   int ans = 0; n   for (; x > 0; x -= lbt(x)) { n     ans += c[x]; n   } n   return ans; n } n bool cmp(const int& a, const int& b) { return dep[a] < dep[b]; } n int query(int u, int lca) { return getsum(pre[u] - 1) - getsum(pre[lca] - 1); } n int a[100005]; n bool vis[100005]; n long long dp[100005][305]; n int work() { n   int u, z; n   for (int i = 1; i <= K; i++) { n     u = a[i]; n     update(pre[u], 1); n     update(pst[u], -1); n     vis[a[i]] = 1; n   } n   int x; n   for (int i = 1; i <= K; i++) { n     u = a[i]; n     z = lca(u, R); n     if (u == R) n       h[i] = 0; n     else if (u == z) n       h[i] = query(R, u) - (vis[u] == true) + (vis[R] == true); n     else if (z == R) n       h[i] = query(u, R); n     else n       h[i] = query(u, z) + query(R, z) - (vis[z] == true) + (vis[R] == true); n   } n   sort(h + 1, h + 1 + K); n   dp[1][1] = 1; n   for (int i = 2; i <= K; i++) { n     for (int j = 1; j <= M; j++) { n       if (j > h[i]) n         dp[i][j] = ((long long)dp[i - 1][j] * (j - h[i]) % int(1e9 + 7) + n                     dp[i - 1][j - 1]) % n                    int(1e9 + 7); n       else n         dp[i][j] = 0; n     } n   } n   long long ans = 0; n   for (int j = 1; j <= M; j++) { n     ans = (ans + dp[K][j]) % int(1e9 + 7); n   } n   for (int i = 1; i <= K; i++) { n     u = a[i]; n     update(pre[u], -1); n     update(pst[u], +1); n     vis[a[i]] = 0; n   } n   return ans; n } n int main() { n   scanf( %d%d , &N, &Q); n   int u, v; n   for (int i = 1; i < N; i++) { n     scanf( %d%d , &u, &v); n     Adj[u].push_back(v); n     Adj[v].push_back(u); n   } n   dep[1] = 1; n   dfs(1); n   NN = 2 * N; n   while (Q--) { n     scanf( %d%d%d , &K, &M, &R); n     for (int i = 1; i <= K; i++) { n       scanf( %d , &a[i]); n     } n     printf( %d n , work()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 255; n const int M = 16; n const int mod = (int)1e9 + 7; n char grid[N][N], ngrid[N][N]; n long long dp[2][M][1 << M][2][2]; n void add(long long& a, long long b) { n   a += b; n   while (a >= mod) { n     a -= mod; n   } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , grid[i]); n   } n   if (n < m) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         ngrid[j][i] = grid[i][j]; n       } n     } n     swap(n, m); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         grid[i][j] = ngrid[i][j]; n       } n     } n   } n   if (grid[0][0] ==  x ) { n     dp[0][0][0][0][0] = 1; n   } else { n     dp[0][0][0][0][1] = 1; n     dp[0][0][1][1][0] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (i > 0) { n       for (int j = 0; j < m; j++) { n         for (int mask = 0; mask < 1 << m; mask++) { n           for (int a = 0; a < 2; a++) { n             for (int b = 0; b < 2; b++) { n               dp[i & 1][j][mask][a][b] = 0; n             } n           } n         } n       } n     } n     for (int j = 0; j < m; j++) { n       if (i == 0 && j == 0) continue; n       for (int mask = 0; mask < 1 << m; mask++) { n         for (int a = 0; a < 2; a++) { n           for (int b = 0; b < 2; b++) { n             if (grid[i][j] !=  x ) { n               if (a == 1) { n                 if (mask & (1 << j)) { n                   if (j == 0) { n                     add(dp[i & 1][j][mask][0][b], n                         dp[1 - (i & 1)][m - 1][mask][a][b]); n                     add(dp[i & 1][j][mask][1][b], n                         dp[1 - (i & 1)][m - 1][mask][a][b]); n                   } else { n                     add(dp[i & 1][j][mask][a][b], n                         2 * dp[i & 1][j - 1][mask][a][b]); n                   } n                 } else { n                   if (j == 0) { n                     if (b == 0) { n                       add(dp[i & 1][j][mask][0][b + 1], n                           dp[1 - (i & 1)][m - 1][mask][a][b]); n                     } n                     add(dp[i & 1][j][mask | (1 << j)][a][b], n                         dp[1 - (i & 1)][m - 1][mask][a][b]); n                   } else { n                     add(dp[i & 1][j][mask | (1 << j)][a][b], n                         dp[i & 1][j - 1][mask][a][b]); n                     add(dp[i & 1][j][mask][a][b], dp[i & 1][j - 1][mask][a][b]); n                   } n                 } n               } else { n                 if (mask & (1 << j)) { n                   if (j == 0) { n                     add(dp[i & 1][j][mask][1][b], n                         dp[1 - (i & 1)][m - 1][mask][a][b]); n                     add(dp[i & 1][j][mask][a][b], n                         dp[1 - (i & 1)][m - 1][mask][a][b]); n                   } else { n                     add(dp[i & 1][j][mask][1][b], dp[i & 1][j - 1][mask][a][b]); n                     add(dp[i & 1][j][mask][a][b], dp[i & 1][j - 1][mask][a][b]); n                   } n                 } else { n                   if (j == 0) { n                     if (b == 0) { n                       add(dp[i & 1][j][mask][a][b + 1], n                           dp[1 - (i & 1)][m - 1][mask][a][b]); n                     } n                     add(dp[i & 1][j][mask | (1 << j)][1][b], n                         dp[1 - (i & 1)][m - 1][mask][a][b]); n                   } else { n                     if (b == 0) { n                       add(dp[i & 1][j][mask][a][b + 1], n                           dp[i & 1][j - 1][mask][a][b]); n                     } n                     add(dp[i & 1][j][mask | (1 << j)][1][b], n                         dp[i & 1][j - 1][mask][a][b]); n                   } n                 } n               } n             } else { n               if (j == 0) { n                 add(dp[i & 1][j][mask & (~(1 << j))][0][b], n                     dp[1 - (i & 1)][m - 1][mask][a][b]); n               } else { n                 add(dp[i & 1][j][mask & (~(1 << j))][0][b], n                     dp[i & 1][j - 1][mask][a][b]); n               } n             } n           } n         } n       } n     } n   } n   int mask = 0; n   for (int j = 0; j < m; j++) { n     if (grid[n - 1][j] !=  x ) { n       mask |= 1 << j; n     } n   } n   long long ans = 0; n   for (int nmask = 0; nmask < 1 << m; nmask++) { n     if (nmask | mask == mask) { n       for (int a = 0; a < 2; a++) { n         for (int b = 0; b < 2; b++) { n           add(ans, dp[(n - 1) & 1][m - 1][nmask][a][b]); n         } n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k, a[100007]; n pair<long long int, long long int> cur[1007]; n bool res[200001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (long long int i = 1; i <= n; ++i) cin >> a[i]; n   long long int last = 0, cnt = 0; n   for (long long int t = 0; t <= 150000; ++t) { n     for (long long int i = 1; i <= k; ++i) { n       if (a[cur[i].second] == cur[i].first) { n         if (cur[i].second <= n && cur[i].second >= 1) cnt++; n         cur[i].second = ++last; n         cur[i].first = 0; n       } n     } n     long long int d = round((100.0 * cnt) / (1.0 * n)); n     for (long long int i = 1; i <= k; ++i) { n       if (cur[i].second <= n && cur[i].second >= 1 && cur[i].first + 1 == d) n         res[cur[i].second] = 1; n       cur[i].first++; n     } n   } n   long long int sum = 0; n   for (long long int i = 1; i <= n; ++i) sum += res[i]; n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, x1, x2; n   cin >> s >> x1 >> x2; n   int t1, t2; n   cin >> t1 >> t2; n   int time_for_igor = abs(x1 - x2) * t2; n   int p, d; n   cin >> p >> d; n   int time_for_tram = -1; n   if (x2 > x1) { n     if (d == 1) { n       if (p > x1) { n         time_for_tram = s - p; n         time_for_tram += s; n         time_for_tram += x1; n         time_for_tram += (x2 - x1); n         time_for_tram *= t1; n       } else { n         time_for_tram = abs(x1 - p) * t1; n         time_for_tram += abs(x1 - x2) * t1; n       } n     } else { n       time_for_tram = p; n       time_for_tram += x1; n       time_for_tram += (x2 - x1); n       time_for_tram *= t1; n     } n   } else { n     if (d == -1) { n       if (p < x1) { n         time_for_tram = p; n         time_for_tram += s; n         time_for_tram += (s - x1); n         time_for_tram += (x1 - x2); n         time_for_tram *= t1; n       } else { n         time_for_tram = p - x1; n         time_for_tram += x1 - x2; n         time_for_tram *= t1; n       } n     } else { n       time_for_tram = s - p; n       time_for_tram += s - x1; n       time_for_tram += x1 - x2; n       time_for_tram *= t1; n     } n   } n   cout << min(time_for_tram, time_for_igor) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n bool f[N][N]; n int x[3005], y[3005]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n     f[x[i] + 1000][y[i] + 1000] = true; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if ((x[i] + x[j]) % 2 != 0 || (y[i] + y[j]) % 2 != 0) continue; n       int x_md = (x[i] + x[j]) / 2; n       int y_md = (y[i] + y[j]) / 2; n       if (f[x_md + 1000][y_md + 1000] == 1) ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   float num; n   int t, n, d; n   cin >> num >> t >> n >> d; n   int a = (num / n); n   if ((num / n) > a) a += 1; n   int y = d, x = 0; n   for (int i = 0; i < a; i++) { n     if (x <= y) n       x += t; n     else n       y += t; n   } n   if (max(x, y) < (t * a)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _print(long long t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(T a[], V sz); n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<   ->  ; n   _print(p.second); n   cerr <<  }  << endl; n } n template <class T, class V> n void _print(T a[], V sz) { n   for (V i = 0; i < sz; i++) { n     _print(a[i]); n     cerr <<    ; n   } n   cerr << endl; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ]  << endl; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ]  << endl; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ]  << endl; n } n void _printall(long long t) { cout << t; } n void _printall(int t) { cout << t; } n void _printall(string t) { cout << t; } n void _printall(char t) { cout << t; } n void _printall(long double t) { cout << t; } n void _printall(double t) { cout << t; } n void _printall(unsigned long long t) { cout << t; } n template <class T, class V> n void _printall(T a[], V sz); n template <class T, class V> n void _printall(pair<T, V> p); n template <class T> n void _printall(vector<T> v); n template <class T> n void _printall(set<T> v); n template <class T, class V> n void _printall(map<T, V> v); n template <class T> n void _printall(multiset<T> v); n template <class T, class V> n void _printall(T a[], V sz) { n   for (V i = 0; i < sz; i++) { n     _printall(a[i]); n     cout <<    ; n   } n   cout << endl; n } n template <class T, class V> n void _printall(pair<T, V> p) { n   _printall(p.ff); n   cout <<  , ; n   _printall(p.ss); n } n template <class T> n void _printall(vector<T> v) { n   for (T i : v) { n     _printall(i); n     cout <<    ; n   } n   cout << endl; n } n template <class T> n void _printall(set<T> v) { n   for (T i : v) { n     _printall(i); n     cout <<    ; n   } n   cout << endl; n } n template <class T> n void _printall(multiset<T> v) { n   for (T i : v) { n     _printall(i); n     cout <<    ; n   } n   cout << endl; n } n template <class T, class V> n void _printall(map<T, V> v) { n   for (auto i : v) { n     _printall(i); n     cout <<    ; n   } n   cout << endl; n } n long long MAXN = 200001; n long long spf[200001]; n void sieve() { n   spf[1] = 1; n   for (int i = 2; i < MAXN; i++) { n     spf[i] = i; n   } n   for (long long i = 4; i < MAXN; i += 2) { n     spf[i] = 2; n   } n   for (long long i = 3; i * i < MAXN; i++) { n     if (spf[i] == i) { n       for (long long j = i * i; j < MAXN; j += i) { n         if (spf[j] == j) spf[j] = i; n       } n     } n   } n } n vector<long long> getFactorization(long long x) { n   vector<long long> ret; n   while (x != 1) { n     ret.push_back(spf[x]); n     x = x / spf[x]; n   } n   return ret; n } n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> pre; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     pre.push_back(1); n   } n   for (int i = n - 2; i >= 0; i--) { n     if (s[i] == s[i + 1]) { n       pre[i] = pre[i + 1] + 1; n     } n   } n   reverse(pre.begin(), pre.end()); n   vector<int> fin; n   for (int i = 0; i < pre.size() - 1; i++) { n     if (pre[i] == 1 && pre[i + 1] == 1) { n       fin.push_back(pre[i]); n     } else if (pre[i] >= 1 && pre[i + 1] == 1) { n       fin.push_back(pre[i]); n     } n   } n   if (pre[n - 1] >= 1) { n     fin.push_back(pre[n - 1]); n   } n   int ind1 = fin.size() - 1; n   int ind2 = fin.size() - 1; n   while (pre.size() != 0 && ind1 >= 0 && ind2 >= 0) { n     if (fin[ind1] > 1) { n       cnt++; n       fin.pop_back(); n       if (ind2 == ind1) { n         ind2--; n       } n       ind1--; n     } else { n       while (fin[ind2] == 1) { n         ind2--; n       } n       if (ind2 < 0) { n         break; n       } else { n         cnt++; n         fin[ind2]--; n         fin.pop_back(); n         ind1--; n       } n     } n   } n   if (pre.size() != 0) { n     cnt += (fin.size() + 1) / 2; n   } n   cout << cnt << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   cerr <<  time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 14; n int a[MAX_N]; n void add(int st, int x) { n   for (int i = 0; i < MAX_N; i++) a[i] += x / MAX_N; n   for (int i = 1; i <= abs(x % MAX_N); i++) n     a[(st + i) % MAX_N] += (x < 0 ? -1 : 1); n } n long long point() { n   long long ans = 0; n   for (int i = 0; i < MAX_N; i++) n     if (a[i] % 2 == 0) ans += a[i]; n   return ans; n } n long long move(int st) { n   int x = a[st]; n   a[st] = 0, add(st, x); n   long long ans = point(); n   add(st, -x), a[st] = x; n   return ans; n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   for (int i = 0; i < MAX_N; i++) cin >> a[i]; n   long long ans = 0; n   for (int i = 0; i < MAX_N; i++) ans = max(ans, move(i)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isNonDegenerateTriangle(int a, int b, int c) { n   return (a + b > c && b + c > a && c + a > b); n } n bool isDegenerateTriangle(int a, int b, int c) { n   return (a + b == c || b + c == a || c + a == b); n } n int main() { n   int n; n   cin >> n; n   long long int a[n], s = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s += a[i]; n   } n   sort(a, a + n); n   cout << 2 * a[n - 1] - s + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tmp; n long long A[1005]; n long long B[1005]; n long long Acost(int q) { n   q *= 4; n   long long ret = 0; n   for (int i = 1; i <= n; i++) { n     long long tmp2 = i * 4 - 2 - q; n     ret += A[i] * tmp2 * tmp2; n   } n   return ret; n } n long long Bcost(int q) { n   q *= 4; n   long long ret = 0; n   for (int i = 1; i <= m; i++) { n     long long tmp2 = i * 4 - 2 - q; n     ret += B[i] * tmp2 * tmp2; n   } n   return ret; n } n int ansx, ansy; n long long sumx = LLONG_MAX, sumy = LLONG_MAX; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> tmp; n       A[i] += tmp; n       B[j] += tmp; n     } n   } n   for (int i = 0; i <= n; i++) { n     long long tmp3 = Acost(i); n     if (tmp3 < sumx) { n       ansx = i; n       sumx = tmp3; n     } n   } n   for (int i = 0; i <= m; i++) { n     long long tmp3 = Bcost(i); n     if (tmp3 < sumy) { n       ansy = i; n       sumy = tmp3; n     } n   } n   cout << sumx + sumy << endl; n   cout << ansx <<     << ansy << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1005; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long rand(long long a, long long b) { n   return uniform_int_distribution<long long>(a, b)(rng); n } n const int K = 3; n struct Hash { n   long long a[K]; n   Hash() : a() {} n   Hash operator^(const Hash &A) const { n     Hash ans = Hash(); n     for (int i = 0; i < K; i++) ans.a[i] = a[i] ^ A.a[i]; n     return ans; n   } n   bool operator==(const Hash &A) const { n     for (int i = 0; i < K; i++) n       if (a[i] != A.a[i]) return false; n     return true; n   } n   bool operator<(const Hash &A) const { n     for (int i = 0; i < K; i++) n       if (a[i] != A.a[i]) return a[i] < A.a[i]; n     return false; n   } n }; n Hash genHash() { n   Hash ret = Hash(); n   for (int i = 0; i < K; i++) ret.a[i] = rand(1, 1LL << 60); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, s, d; n   cin >> n >> s >> d; n   Hash hash[n], sw[s]; n   for (int i = 0; i < n; i++) hash[i] = genHash(); n   for (int i = 0; i < s; i++) { n     int k; n     cin >> k; n     Hash cur = Hash(); n     while (k--) { n       int x; n       cin >> x; n       cur = cur ^ hash[x - 1]; n     } n     sw[i] = cur; n   } n   map<Hash, int> left_sz; n   int left = min(s / 2, 13); n   for (int mask = 0; mask < (1 << left); mask++) { n     Hash cur = Hash(); n     for (int bit = 0; bit < left; bit++) { n       if (!((mask >> bit) & 1)) continue; n       cur = cur ^ sw[bit]; n     } n     if (!left_sz.count(cur)) n       left_sz[cur] = __builtin_popcountll(mask); n     else n       left_sz[cur] = min(left_sz[cur], __builtin_popcountll(mask)); n   } n   map<Hash, int> right_sz; n   int right = s - left; n   for (int mask = 0; mask < (1 << right); mask++) { n     Hash cur = Hash(); n     for (int bit = 0; bit < right; bit++) { n       if (!((mask >> bit) & 1)) continue; n       cur = cur ^ sw[bit + left]; n     } n     if (!right_sz.count(cur)) n       right_sz[cur] = __builtin_popcountll(mask); n     else n       right_sz[cur] = min(right_sz[cur], __builtin_popcountll(mask)); n   } n   while (d--) { n     Hash cur = Hash(); n     int sz; n     cin >> sz; n     for (int i = 0; i < sz; i++) { n       int x; n       cin >> x; n       cur = cur ^ hash[x - 1]; n     } n     int ans = 100; n     for (auto p : left_sz) { n       Hash tmp = cur ^ p.first; n       if (right_sz.count(tmp)) ans = min(ans, p.second + right_sz[tmp]); n       if (left_sz.count(tmp)) { n         if (tmp == p.first) n           ans = 0; n         else n           ans = min(ans, p.second + left_sz[tmp]); n       } n     } n     cout << (ans == 100 ? -1 : ans) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 210; n int n; n int a[N]; n bool vis[N]; n int lp[N]; n int dis[N]; n int inloop(int v) { n   vis[v] = 1; n   int u = a[v]; n   int cnt = 1; n   while (u != v && !vis[u]) { n     vis[u] = 1; n     ++cnt; n     u = a[u]; n   } n   if (u == v) return cnt; n   return 0; n } n int dfsandsetlp(int v) { n   if (lp[v] > 0) return dis[v]; n   int t = dfsandsetlp(a[v]); n   lp[v] = lp[a[v]]; n   return dis[v] = t + 1; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { n   if (a == 0) return b; n   if (b == 0) return a; n   return a / gcd(a, b) * b; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n   } n   for (int i = 1; i <= n; ++i) { n     memset(vis, 0, sizeof(vis)); n     int t = inloop(i); n     lp[i] = t; n   } n   for (int i = 1; i <= n; ++i) { n     if (!lp[i]) { n       dis[i] = dfsandsetlp(i); n     } n   } n   long long res = 1; n   for (int i = 1; i <= n; ++i) { n     res = lcm(res, lp[i]); n   } n   int mx = 0; n   for (int i = 1; i <= n; ++i) { n     mx = max(mx, dis[i]); n   } n   if (res < mx) { n     int t = mx / res; n     if (res * t == mx) { n       res = mx; n     } else { n       res = res * (t + 1); n     } n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = LLONG_MAX / 2; n const long long MX = 1e5 + 10; n int a[MX]; n set<int> s; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   for (long long(i) = (0); (i) <= (n - 1); ++(i)) { n     cin >> a[i]; n     s.insert(a[i]); n   } n   int num = 1; n   while (m >= num) { n     if (!s.count(num)) { n       m -= num; n       v.push_back(num); n     } n     num++; n   } n   cout << v.size() << endl; n   for (auto it : v) { n     cout << it <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long tr(long long d) { return d * (d + 1) / 2; } n inline long long tr2(long long d) { return 2 * tr(d) - d; } n int n; n long long solve(int y, int x, long long c) { n   long long res = 4 * (tr(c + 1) - c - 1) + 1; n   if (c - (y + 1) >= 0) res -= tr2(c - (y + 1) + 1); n   if (c - (n - y) >= 0) res -= tr2(c - (n - y) + 1); n   if (c - (x + 1) >= 0) res -= tr2(c - (x + 1) + 1); n   if (c - (n - x) >= 0) res -= tr2(c - (n - x) + 1); n   if (c - (y + x + 2) >= 0) res += tr(c - (y + x + 2) + 1); n   if (c - (y + 1 + n - x) >= 0) res += tr(c - (y + 1 + n - x) + 1); n   if (c - (x + 1 + n - y) >= 0) res += tr(c - (x + 1 + n - y) + 1); n   if (c - (n - x + n - y) >= 0) res += tr(c - (n - x + n - y) + 1); n   return res; n } n int main() { n   int y, x, c; n   while (scanf( %d%d%d%d , &n, &y, &x, &c) >= 1) { n     --y, --x; n     int l = -1, r = (int)2e9 + 1; n     while (l + 1 < r) { n       int m = l + (r - l) / 2; n       if (solve(y, x, m) >= (long long)c) n         r = m; n       else n         l = m; n     } n     printf( %d n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void setIO(string name =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   if (int((name).size())) { n     freopen((name +  .in ).c_str(),  r , stdin); n     freopen((name +  .out ).c_str(),  w , stdout); n   } n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long mcm(long long a, long long b) { return (a * b) / gcd(a, b); } n bool prime(long long n, long long i) { n   if (i > sqrt(n)) return true; n   if (n % i == 0) n     return false; n   else n     return prime(n, i + 1); n } n struct compii { n   bool operator()(const pair<int, int> &a, const pair<int, int> &b) { n     if (a.first == a.second) return a.second < b.second; n     return a.first > b.first; n   } n }; n bool comp(int a, int b) { return a > b; } n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n void solve() { n   int n; n   cin >> n; n   vector<int> num(n); n   int c0 = 0; n   int c1 = 0; n   for (auto &x : num) { n     cin >> x; n     if (x == 1) n       c1++; n     else n       c0++; n   } n   int i = 0; n   for (i = 0; i < n; i++) { n     if (num[i] == 1) n       c1--; n     else n       c0--; n     if (c1 == 0) break; n     if (c0 == 0) break; n   } n   cout << i + 1 << endl; n } n int main() { n   setIO(  ); n   int t = 1; n   while (t-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #define int long long n using namespace std; n const int maxm=5e5+5; n int stk[maxm],head; n int d[maxm]; n int a[maxm]; n int n; n // n int rt[maxm],tot; n int lc[maxm*40],rc[maxm*40],ma[maxm*40]; n inline void pp(int k){ n     ma[k]=max(ma[lc[k]],ma[rc[k]]); n } n void update(int x,int val,int l,int r,int &k,int last){ n     k=++tot; n     lc[k]=lc[last]; n     rc[k]=rc[last]; n     if(l==r){ n         ma[k]=val; n         return ; n     } n     int mid=(l+r)/2; n     if(x<=mid)update(x,val,l,mid,lc[k],lc[last]); n     else update(x,val,mid+1,r,rc[k],rc[last]); n     pp(k); n } n int ask(int st,int ed,int l,int r,int k){ n     if(!k)return 0; n     if(st<=l&&ed>=r)return ma[k]; n     int mid=(l+r)/2; n     int ans=0; n     if(st<=mid)ans=max(ans,ask(st,ed,l,mid,lc[k])); n     if(ed>mid)ans=max(ans,ask(st,ed,mid+1,r,rc[k])); n     return ans; n } n // n void solve(){ n     cin>>n; n     //init n     tot=0; n     ma[0]=0; n     // n     for(int i=1;i<=n;i++){ n         cin>>a[i]; n     } n     int ans=0; n     head=0; n     for(int i=1;i<=n;i++){ n         while(head&&a[stk[head]]<a[i]){ n             head--; n         } n         d[i]=1; n         d[i]=max(d[i],ask(1,a[i],1,n,rt[i-1])+1); n         if(head){ n             int k=stk[head]; n             d[i]=max(d[i],ask(1,a[i],1,n,rt[k-1])+2); n         } n         update(a[i],d[i],1,n,rt[i],rt[i-1]); n         stk[++head]=i; n         ans=max(ans,d[i]); n     } n     cout<<ans<<endl; n } n signed main(){ n     ios::sync_with_stdio(0); n     int T;cin>>T; n     while(T--){ n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> fact(2000006); n vector<long long int> prime_check(1e7 + 2); n vector<long long int> primes; n long long powmod(long long x, long long y, long long m) { n   long long res = 1LL; n   while (y) { n     if (y & 1) res = (res * x) % m; n     x = (x * x) % m; n     y /= 2; n   } n   return res; n } n void init_fact() { n   fact[0] = 1; n   for (long long int i = 1; i < 2000006; i++) { n     fact[i] = (fact[i - 1] * i) % 1000000007; n   } n } n long long int C(long long int n, long long int r) { n   if (n < r) return 0; n   long long int temp = n       fact[n] * powmod(fact[r], 1000000007 - 2, 1000000007) % 1000000007; n   temp *= powmod(fact[n - r], 1000000007 - 2, 1000000007); n   temp %= 1000000007; n   return temp; n } n void Sieve() { n   for (long long int i = 2; i <= 1e7 + 1; i++) { n     if (prime_check[i]) continue; n     primes.push_back(i); n     for (long long int j = 2 * i; j <= 1e7 + 1; j += i) prime_check[j] = i; n   } n } n long long int phi(long long int n) { n   long long int i, res = n; n   for (i = 2; i * i <= n; i++) n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       res -= res / i; n     } n   if (n > 1) res -= res / n; n   return res; n } n long long int n, x, y; n vector<long long int> a(2000006), b(2000006), cnt(2000006), sum(2000006); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> x >> y; n   for (long long int i = 1; i <= n; i++) cin >> a[i], b[a[i]]++; n   for (long long int i = 1; i < 2000006; i++) n     cnt[i] = cnt[i - 1] + b[i], sum[i] = sum[i - 1] + b[i] * i; n   long long int c = x / y; n   long long int ans = INT64_MAX; n   for (long long int i = 2; i <= 1e6; i++) { n     long long int cost = 0, f = i - min(i, c); n     for (long long int j = 0; j <= 1e6; j += i) { n       long long int st = j + 1, en = j + i, st2 = max(st, st + f - 1); n       long long int c1 = cnt[st2 - 1] - cnt[st - 1]; n       long long int c2 = cnt[en] - cnt[st2 - 1]; n       long long int c3 = sum[en] - sum[st2 - 1]; n       cost += c1 * x + y * (c2 * en - c3); n     } n     ans = min(ans, cost); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   if ((x1 <= x2 && y1 <= y2) || x1 + y1 <= max(x2, y2)) { n     cout <<  Polycarp ; n   } else { n     cout <<  Vasiliy ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[6], t; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &t), a[t]++; n   } n   if (a[0] == 0) { n     printf( -1 n ); n     return 0; n   } n   int k = a[5] / 9; n   for (int i = 0; i < k; i++) { n     printf( 555555555 ); n   } n   if (k == 0) { n     printf( 0 n ); n   } else { n     while (a[0]--) printf( 0 ); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 210; n const int mod = 1e9 + 7; n void reduce(int& x) { x += x >> 31 & mod; } n int mul(int x, int y) { return (long long)x * y % mod; } n void fma(int& x, int y, int z) { x = ((long long)y * z + x) % mod; } n struct vec { n   int x, y; n   vec() { x = y = 0; } n   vec(int a, int b) { x = a, y = b; } n   vec operator+(vec b) const { return vec(x + b.x, y + b.y); } n   vec operator-(vec b) const { return vec(x - b.x, y - b.y); } n   long long operator^(vec b) const { n     return (long long)x * b.y - (long long)y * b.x; n   } n   long long operator*(vec b) const { n     return (long long)x * b.x + (long long)y * b.y; n   } n   bool operator==(vec b) const { return x == b.x && y == b.y; } n } ps[MAXN]; n int n; n long long cross(vec x, vec y, vec z) { return (y - x) ^ (z - x); } n inline int sgn(long long x) { return x < 0 ? -1 : x > 0; } n int dp[MAXN][MAXN]; n int main() { n   std::ios_base::sync_with_stdio(false), std::cin.tie(0); n   std::cin >> n; n   for (int i = 1; i <= n; ++i) std::cin >> ps[i].x >> ps[i].y; n   long long area = 0; n   for (int i = 1; i <= n; ++i) area += ps[i] ^ ps[i == n ? 1 : i + 1]; n   for (int i = 1; i < n; ++i) dp[i][i + 1] = 1; n   for (int L = 3; L <= n; ++L) n     for (int l = 1; l + L - 1 <= n; ++l) { n       int r = l + L - 1; n       for (int p = l + 1; p < r; ++p) n         if (sgn(cross(ps[p], ps[r], ps[l])) == sgn(area)) n           fma(dp[l][r], dp[l][p], dp[p][r]); n     } n   std::cout << dp[1][n] << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n template <int mod = mod> n struct Modular { n   int val; n   Modular() { val = 0; } n   Modular(long long _val) { val = Normalize(_val); } n   Modular(int _val) { val = Normalize(_val); } n   inline int Normalize(long long& _val) { n     if (_val < 0) _val = _val % mod + mod; n     if (_val >= mod) _val %= mod; n     return _val; n   } n   inline int Normalize(int& _val) { n     if (_val < 0) _val = _val % mod + mod; n     if (_val >= mod) _val %= mod; n     return _val; n   } n   inline Modular modinv(int a, int m = mod) { n     int u = 0, v = 1; n     while (a != 0) { n       int t = m / a; n       m -= t * a; n       swap(a, m); n       u -= t * v; n       swap(u, v); n     } n     assert(m == 1); n     return u; n   } n   inline Modular modinv() { return modinv(val); } n   inline Modular modpow(long long p) const { n     assert(p >= 0); n     Modular a = *this, result = 1; n     while (p > 0) { n       if (p & 1) result *= a; n       a *= a; n       p >>= 1; n     } n     return result; n   } n   static unsigned Fastmod(uint64_t x, unsigned m = mod) { n     return x % m; n     unsigned xhi = x >> 32, xlo = (unsigned)x; n     unsigned q, r; n     asm( divl %4 n  :  =a (q),  =d (r) :  d (xhi),  a (xlo),  r (m)); n     return r; n   } n   inline Modular& operator+=(const Modular& other) { n     val += other.val; n     if (val >= mod) val -= mod; n     return *this; n   } n   inline Modular& operator-=(const Modular& other) { n     val -= other.val; n     if (val < 0) val += mod; n     return *this; n   } n   inline Modular& operator%=(const Modular& other) { n     val %= other.val; n     return *this; n   } n   inline Modular& operator*=(const Modular& other) { n     val = Fastmod((uint64_t)val * other.val); n     return *this; n   } n   inline Modular& operator/=(const Modular& other) { n     return *this *= modinv(other.val); n   } n   inline friend Modular operator+(const Modular& a, const Modular& b) { n     return Modular(a) += b; n   } n   inline friend Modular operator-(const Modular& a, const Modular& b) { n     return Modular(a) -= b; n   } n   inline friend Modular operator%(const Modular& a, const Modular& b) { n     return Modular(a) %= b; n   } n   inline friend Modular operator*(const Modular& a, const Modular& b) { n     return Modular(a) *= b; n   } n   inline friend Modular operator/(const Modular& a, const Modular& b) { n     return Modular(a) /= b; n   } n   inline friend istream& operator>>(istream& stream, Modular& m) { n     long long x; n     stream >> x; n     m.val = m.Normalize(x); n     return stream; n   } n   inline friend ostream& operator<<(ostream& stream, const Modular& m) { n     return stream << m.val; n   } n   inline Modular operator++() { return *this += 1; } n   inline Modular operator--() { return *this -= 1; } n   inline Modular operator++(int) { n     Modular x = *this; n     ++*this; n     return x; n   } n   inline Modular operator--(int) { n     Modular x = *this; n     --*this; n     return x; n   } n   inline Modular operator-() const { return Modular(-val); } n   inline friend bool operator==(const Modular& a, const Modular& b) { n     return a.val == b.val; n   } n   inline friend bool operator!=(const Modular& a, const Modular& b) { n     return a.val != b.val; n   } n   inline friend bool operator>(const Modular& a, const Modular& b) { n     return a.val > b.val; n   } n   inline friend bool operator<(const Modular& a, const Modular& b) { n     return a.val < b.val; n   } n   inline friend bool operator>=(const Modular& a, const Modular& b) { n     return a.val >= b.val; n   } n   inline friend bool operator<=(const Modular& a, const Modular& b) { n     return a.val <= b.val; n   } n }; n const int G = 3; n const int N = 1 << 20; n Modular<mod> root[1 << 20]; n Modular<mod> _pow2[(1 << 20) + 1]; n struct ntt { n   ntt() { n     root[N / 2] = 1; n     root[N / 2 + 1] = ((Modular<mod>)G).modpow((mod - 1) / N); n     for (int i = N / 2 + 2; i < N; ++i) root[i] = root[i - 1] * root[N / 2 + 1]; n     for (int i = N / 2; --i;) root[i] = root[2 * i]; n     for (int i = 0; i <= 20; i++) n       _pow2[1 << i] = ((Modular<mod>)(1 << i)).modinv(); n   } n   vector<Modular<mod> > Convolute(vector<Modular<mod> >& a, n                                   vector<Modular<mod> >& b) { n     int s = a.size() + b.size() - 1, n = 1; n     while (n < s) n *= 2; n     vector<Modular<mod> > c(n); n     a.resize(n); n     NTT(a); n     b.resize(n); n     NTT(b); n     for (int i = 0; i < n; i++) c[i] = a[i] * b[i] * _pow2[n]; n     reverse(c.begin() + 1, c.end()); n     NTT(c); n     c.resize(s); n     return c; n   } n   void NTT(vector<Modular<mod> >& a) { n     int n = a.size(); n     for (int i = 0, j = 0; i < n; ++i) { n       if (i > j) swap(a[i], a[j]); n       for (int k = n / 2; (j ^= k) < k; k /= 2) n         ; n     } n     for (int k = 1; k < n; k *= 2) { n       for (int i = 0; i < n; i += 2 * k) { n         for (int j = 0; j < k; j++) { n           Modular<mod> z = root[j + k] * a[i + j + k], &ai = a[i + j]; n           a[i + j + k] = ai - z; n           ai += z; n         } n       } n     } n   } n } NTT; n void solve() { n   int n, x, y; n   cin >> n >> x >> y; n   vector<Modular<mod> > a(x + 1); n   for (int i = 0; i <= n; ++i) { n     int var; n     cin >> var; n     a[var] = 1; n   } n   vector<Modular<mod> > b = a; n   reverse(begin(b), end(b)); n   b = NTT.Convolute(a, b); n   vector<int> c(1e6 + 1); n   for (int i = 0; i < int(b.size()); ++i) { n     if (b[i] == 0 || !(abs(x - i))) continue; n     int idx = 2 * (y + abs(x - i)); n     if (idx <= 1e6) c[idx] = 1; n   } n   vector<int> dp(1e6 + 1); n   for (int i = 2; i <= 1e6; ++i) { n     if (!c[i]) continue; n     for (int j = i; j <= 1e6; j += i) { n       dp[j] = max(dp[j], i); n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int l; n     cin >> l; n     if (dp[l]) { n       cout << dp[l] <<    ; n       continue; n     } n     cout << -1 <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n long long dp_hor[2][N][N], dp_vert[2][N][N]; n vector<pair<int, int>> cost_hor[N][N], cost_vert[N][N]; n long long inf = 1e18; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= k; i++) { n     int x, y, d, t, e; n     cin >> x >> y >> d >> t >> e; n     if (x + y >= t && ((x + y) % 4) == (t % 4)) n       cost_hor[x][y].push_back({1e9, e}), cost_vert[x][y].push_back({1e9, e}); n     if (x + y >= t + 1 && ((x + y) % 4) == ((t + 1) % 4)) n       cost_hor[x + d][y - d].push_back({1e9, e}), n           cost_vert[x + d][y - d].push_back({1e9, e}); n     if (x + y + d >= t + 2 && ((x + y + d) % 4) == ((t + 2) % 4)) { n       if (x + y >= t + 1 && ((x + y) % 4) == ((t + 1) % 4)) n         cost_hor[x + d][y].push_back({d, e}); n       else n         cost_hor[x + d][y].push_back({1e9, e}); n       if (x + y >= t && ((x + y) % 4) == (t % 4)) n         cost_vert[x + d][y].push_back({d, e}); n       else n         cost_vert[x + d][y].push_back({1e9, e}); n     } n     if (x + y + d >= t + 3 && ((x + y + d) % 4) == ((t + 3) % 4)) { n       if (x + y >= t && ((x + y) % 4) == (t % 4)) n         cost_hor[x][y + d].push_back({d, e}); n       else n         cost_hor[x][y + d].push_back({1e9, e}); n       cost_vert[x][y + d].push_back({1e9, e}); n     } n   } n   for (int x = 0; x < 2; x++) n     for (int y = 0; y < N; y++) n       for (int z = 0; z < N; z++) dp_hor[x][y][z] = dp_vert[x][y][z] = inf; n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < m; y++) { n       int f = x & 1; n       for (int z = 0; z < N; z++) dp_hor[f][y][z] = dp_vert[f][y][z] = inf; n       if (x == 0 && y == 0) { n         dp_hor[f][y][0] = dp_vert[f][y][0] = 0; n       } n       sort(cost_hor[x][y].begin(), cost_hor[x][y].end()); n       sort(cost_vert[x][y].begin(), cost_vert[x][y].end()); n       long long cost = 0; n       for (int z = y; z >= 1; z--) { n         while (cost_hor[x][y].size() > 0 && cost_hor[x][y].back().first > z) { n           pair<int, int> t = cost_hor[x][y].back(); n           cost_hor[x][y].pop_back(); n           cost += t.second; n         } n         dp_hor[f][y][z] = dp_hor[f][y - 1][z - 1] + cost; n       } n       cost = 0; n       for (int z = x; z >= 1; z--) { n         while (cost_vert[x][y].size() > 0 && cost_vert[x][y].back().first > z) { n           pair<int, int> t = cost_vert[x][y].back(); n           cost_vert[x][y].pop_back(); n           cost += t.second; n         } n         dp_vert[f][y][z] = dp_vert[f ^ 1][y][z - 1] + cost; n       } n       for (int z = 1; z <= x; z++) { n         dp_hor[f][y][0] = min(dp_hor[f][y][0], dp_vert[f][y][z]); n       } n       for (int z = 1; z <= y; z++) { n         dp_vert[f][y][0] = min(dp_vert[f][y][0], dp_hor[f][y][z]); n       } n     } n   } n   long long ans = inf; n   for (int z = 1; z < m; z++) { n     ans = min(ans, dp_hor[(n - 1) & 1][m - 1][z]); n   } n   for (int z = 1; z < n; z++) { n     ans = min(ans, dp_vert[(n - 1) & 1][m - 1][z]); n   } n   if (ans == inf) ans = 0; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   return os <<  ] ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n const long long int mod = 1e9 + 7; n const long long int maxn = 5 * 100001; n long long int power(long long int x, long long int n) { n   x %= mod; n   long long int res = 1; n   for (; n > 0; n >>= 1) { n     if (n & 1) res = (res * x) % mod; n     x = (x * x) % mod; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n; n   cin >> n; n   pair<long long int, long long int> a[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i + 1; n   } n   sort(a, a + n); n   long long int dp[n + 1]; n   dp[n] = 0; n   dp[n - 1] = mod; n   dp[n - 2] = mod; n   dp[n - 3] = a[n - 1].first - a[n - 3].first; n   long long int got[n + 5]; n   got[n - 3] = n; n   got[n - 2] = n; n   got[n - 1] = n; n   for (long long int i = n - 4; i >= 0; i--) { n     long long int mini = mod; n     mini = min(mini, a[i + 2].first - a[i].first + dp[i + 3]); n     if (i + 4 <= n) mini = min(mini, a[i + 3].first - a[i].first + dp[i + 4]); n     if (i + 5 <= n) mini = min(mini, a[i + 4].first - a[i].first + dp[i + 5]); n     for (long long int j = 2; j <= 4; j++) { n       if (j + i + 1 <= n) { n         if (mini == a[i + j].first - a[i].first + dp[i + j + 1]) { n           got[i] = i + j + 1; n           break; n         } n       } n     } n     dp[i] = mini; n   } n   long long int ans[n + 1]; n   long long int m = 0; n   for (long long int i = 0, j = 0; i < n; i = j) { n     m++; n     while (j != got[i]) { n       ans[a[j].second] = m; n       j++; n     } n   } n   cout << dp[0] <<     << m << endl; n   for (long long int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = INT_MAX; n int n; n struct node { n   long double x, y; n } a[100010]; n bool check(long double xx) { n   long double l, r; n   l = -1e19, r = 1e19; n   for (int i = 0; i < n; ++i) { n     if (a[i].y >= xx * 2 || l > r) return false; n     l = max(l, a[i].x - sqrt(2 * a[i].y * xx - a[i].y * a[i].y)); n     r = min(r, a[i].x + sqrt(2 * a[i].y * xx - a[i].y * a[i].y)); n   } n   return l <= r; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %lf%lf , &a[i].x, &a[i].y); n   for (int i = 0; i < n; ++i) n     if (a[i].y * a[n - 1].y < 0) { n       printf( -1 ); n       return 0; n     } else n       a[i].y = (a[i].y > 0) ? a[i].y : -a[i].y; n   long double l = 0, r = 1e19, mid; n   for (int i = 0; i <= 100; i++) { n     mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %.10Lf , mid); n } n


#include <bits/stdc++.h> n #include <algorithm> n #include <unordered_map> n using namespace std; n  n typedef long long ll; n typedef vector<long long int> vll; n typedef vector<int> vi; n typedef unordered_map<int, int> umap; n typedef vector<bool> vb; n typedef pair<int, int> pii; n typedef vector<pii> vpii; n typedef vector<pair<int, vi>> vpiv; n typedef unordered_map<ll, vll> ull; n typedef set<ll> sll; n  n #define min(a, b) ((a > b) ? b : a) n #define max(a, b) ((a > b) ? a : b) n #define run(i, n) for (int i = 0; i < n; i++) n #define rund(i, n) for (int i = n - 1; i >= 0; i--) n #define in(v) insert(v) n #define pb(v) push_back(v) n #define mp(v, u) make_pair(v, u) n #define MOD 1000000007 n #define lim 10000000 n #define INF 1000000009 n #define f first n #define s second n #define yes cout <<  YES  << endl n #define no cout <<  NO  << endl n //-------------------Number theory------------ n // generate prime numbers till n n vb seive(int n) n { n     vb prime(n + 1, true); n     prime[0] = false; n     prime[1] = false; n     for (ll i = 2; i * i <= n; i++) n     { n         if (prime[i]) n             for (ll j = i * i; j <= n; j += i) n             { n                 prime[j] = false; n             } n     } n     return prime; n } n  n //euler totient function n vll totient(ll n) n { n     vll tot(n + 1); n     run(i, n + 1) n     { n         tot[i] = i; n     } n     for (int i = 2; i * i <= n; i++) n     { n         if (tot[i] == i) n         { n             for (int j = i; j <= n; j += i) n             { n                 tot[j] /= i; n                 tot[j] *= i - 1; n             } n         } n     } n     return tot; n } n //modular exponentiation n ll modExp(ll a, ll n, ll p) n { n     if (n == 1) n     { n         return a % p; n     } n     if (n % 2) n     { n         ll t = modExp(a, n / 2, p); n         return (a * t % p * t) % p; n     } n     else n     { n         ll t = modExp(a, n / 2, p); n         return t % p * t % p; n     } n } n //a inverse mod p, where a and p are coprime n ll modInv(ll a, ll p, vb prime) n { n     // we know if a and p are coprime then a^totient(p) = 1 mod p => a^{totient(p)-1}=a^-1 mod p n     vll tmp = totient(p); n     ll t = tmp[t]; n     return modExp(a, t, p); n } n  n //----------------Segment Tree -SUM ----------------------------------------------- n void buildSeg(int *a, int *t, int v, int l, int r) n { n     //initially v should be passed as 1 n     //declare t in main method n     if (l == r) n     { n         t[v] = a[l]; n         return; n     } n     int m = (l + r) / 2; n     buildSeg(a, t, 2 * v, l, m);         //build Left sub-tree n     buildSeg(a, t, 2 * v + 1, m + 1, r); //build Right sub-tree n     // store the sum of left and right sub-segments in the present node n     t[v] = t[2 * v] + t[2 * v + 1]; n } n  n ll sumQuery(int *t, int v, int tl, int tr, int l, int r) n { n     // start = 0 and end = n-1 initially . n     // v initially should be 1 . n     if (l > r) n     { n         return 0; n     } n     if (tl == l && tr == r) n     { n         return t[v]; n     } n     else n     { n         int tm = (tl + tr) / 2; n         if (tl <= l && l <= tm && tl <= r && r <= tm) n         { n             // the query range is completely in left sub-segment n             return sumQuery(t, 2 * v, tl, tm, l, r); n         } n         else if (tm < l && l <= tr && tm < r && r <= tr) n         { n             return sumQuery(t, 2 * v + 1, tm + 1, tr, l, r); n         } n         else n         { n             ll s1 = sumQuery(t, 2 * v, tl, tm, l, tm); n             ll s2 = sumQuery(t, 2 * v + 1, tm + 1, tr, tm + 1, r); n             return s1 + s2; n         } n     } n } n  n // few helpers n void print(vll a) n { n     for (int i = 0; i < a.size(); i++) n     { n         cout << a[i] <<    ; n     } n     cout << endl; n } n  n void inp(vll &a) n { n     int i = 0; n     for (auto &it : a) n     { n         int t; n         cin >> t; n         it = t; n     } n } n class myComparator n { n public: n     bool operator()(pair<ll, ll> &p1, pair<ll, ll> &p2) n     { n         if (p1.first < p2.first) n             return true; n         else if (p1.first == p2.first) n             return p1.second > p2.second; n         else n             return false; n     } n }; n class four n { n public: n     ll x1, y1, x2, y2; n }; n int p1 = 1000000007; n int p2 = 1824236251; n ll p = 998244353; n  n //----------------MAIN METHOD (Code)------------------------------------------------ n ll merge(vll& arr, vll& left, vll& right) { n     ll i = 0, j = 0, count = 0; n     while (i < left.size() || j < right.size()) { n         if (i == left.size()) { n             arr[i+j] = right[j]; n             j++; n         } else if (j == right.size()) { n             arr[i+j] = left[i]; n             i++; n         } else if (left[i] <= right[j]) { n             arr[i+j] = left[i]; n             i++;                 n         } else { n             arr[i+j] = right[j]; n             count += left.size()-i; n             j++; n         } n     } n     return count; n } n  n ll getInvCount(vll& arr) { n     if (arr.size() < 2) n         return 0; n  n     int m = (arr.size() + 1) / 2; n     vll left(arr.begin() , arr.begin()+m); n     vll right(arr.begin()+ m, arr.end()); n  n     return getInvCount(left) + getInvCount(right) + merge(arr, left, right); n } n ll findInv(string &tmp , string&s , unordered_map<char,vector<ll>>& t){ n     int inv = 0; n     int c1 =0,c2 =0 , c3 = 0 , c4 =0; n     vector<ll> id; n     for(auto i:tmp ){ n         if(i== A ){ n             id.pb(t[ A ][c1++]); n         } n         if(i== N ){ n             id.pb(t[ N ][c2++]); n         } n         if(i== O ){ n             id.pb(t[ O ][c3++]); n         } n         if(i== T ){ n             id.pb(t[ T ][c4++]); n         } n     } n     // print(id); n     return getInvCount(id); n } n  n int main() n { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     int t ; cin>>t; n     while(t--){ n         string s;cin>>s; n         unordered_map<char,int> freq; n         unordered_map<char,vll> rec; n         string ans =  ; n         ll inversion_number = 0; n         vector<char> letter = { A , N , O , T }; n         for(int i=0;i<s.length();i++){ n             freq[s[i]] ++; n             rec[s[i]].pb(i); n         } n         int i = 1; n         do{ n             string tmp =  ; n             for(auto c : letter){ n                 run(i,freq[c]){ n                     tmp+=c; n                 } n             } n             // cout<<tmp<<endl; n             ll temp = findInv(tmp,s,rec); n             if(temp >= inversion_number){ n                 inversion_number = temp; n                 ans = tmp; n             } n              n         }while( next_permutation(letter.begin(),letter.end())); n  n         cout<<ans<<endl; n     } n      n }


#include<bits/stdc++.h> n using namespace std; n int read() { n  char c=getchar(); int x=0,f=1; n  while (c< 0  || c> 9 ) { n   if (c== - ) f=-1; n   c=getchar(); n  } n  while (c>= 0  && c<= 9 ) { n   x=x*10+c- 0 ; n   c=getchar(); n  } n  return x*f; n } n const int p=998244353; n int a[1200005],b[1200005],c[1200005],fac[1200005],inv[1200005];  n int pow_3[1200005],inv_3[1200005],R[1200005]; n void add(int &x,int y) {x+=y; if (x>=p) x-=p;} n int pow_mod(int x,int k) { n  int ans=1; n  while (k) { n   if (k&1) ans=1LL*ans*x%p; n   x=1LL*x*x%p; n   k>>=1;  n  } n  return ans; n } n void ntt(int *x,int n,int f) { n     pow_3[1]=pow_mod(3,(p-1)/n); n     if (f==1) { n         pow_3[0]=1;  n         for (int i=1; i<n; i++) pow_3[i]=1LL*pow_3[i-1]*pow_3[1]%p; n     } n     else { n         inv_3[0]=1; inv_3[1]=pow_mod(pow_3[1],p-2); n         for (int i=2; i<n; i++) inv_3[i]=1LL*inv_3[i-1]*inv_3[1]%p; n     } n     int t=0,tp=1; n     while (tp<n) tp<<=1,++t; n     for (int i=0; i<tp; i++) R[i]=(R[i>>1]>>1)|((i&1)?(tp>>1):0); n     for (int i=0; i<n; i++) if (i<R[i]) swap(x[i],x[R[i]]); n     for (int l=2; l<=n; l<<=1) { n         int m=l/2; n         for (int *y=x; y!=x+n; y+=l) { n             for (int j=0; j<m; j++) { n                 int tmp; n                 if (f==1) tmp=1LL*y[j+m]*pow_3[n/l*j]%p; n                 else tmp=1LL*y[j+m]*inv_3[n/l*j]%p; n                 y[j+m]=y[j]-tmp; if (y[j+m]<0) y[j+m]+=p;  n                 y[j]+=tmp; if (y[j]>=p) y[j]-=p; n             } n         } n     } n     if (f==-1) { n         int tmp=pow_mod(n,p-2); n         for (int i=0; i<n; i++) x[i]=1LL*x[i]*tmp%p; n     } n } n int f[300005],tot; n vector<int> vec[300005]; n char s[300005]; n int calc(int x) { n  return 1LL*f[x+1]*pow_mod(2,tot-x)%p; n } n int main() { n  for (int i=0; i<=300001; i++) a[i]=i/2; n  for (int i=0; i<=300001; i++) b[i]=pow_mod(2,i); n  int N=1; n     while (N<=600000) N<<=1; n  ntt(a,N,1); ntt(b,N,1); n  for (int i=0; i<N; i++) c[i]=1LL*a[i]*b[i]%p; n  ntt(c,N,-1); n  f[0]=0; n  int sum=0; n  for (int i=1; i<=300001; i++) { n   f[i]=(i-1)/2; n   add(f[i],sum); n   if (i>=2) add(f[i],c[i-2]); n   add(sum,f[i]); n  } n  int n=read(),m=read(); tot=0; n  for (int i=0; i<n; i++) { n   scanf( %s , s); n   for (int j=0; j<m; j++) { n    vec[i].push_back((s[j]== o )?1:-1); n    if (s[j]== o ) ++tot; n   } n  } n  int ans=0; n  for (int i=0; i<n; i++) { n   int cnt=0; n   for (int j=0; j<m; j++) { n    if (vec[i][j]==1) ++cnt; n    else { n     if (cnt>0) add(ans,calc(cnt)); n     cnt=0; n    } n   } n   if (cnt>0) add(ans,calc(cnt)); n  } n  for (int j=0; j<m; j++) { n   int cnt=0; n   for (int i=0; i<n; i++) { n    if (vec[i][j]==1) ++cnt; n    else { n     if (cnt>0) add(ans,calc(cnt)); n     cnt=0; n    } n   } n   if (cnt>0) add(ans,calc(cnt)); n  } n  printf( %d n , ans); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long N = 1e10; n int getLen(unsigned long long a) { n   int len = 0; n   while (a) { n     len++; n     a /= 10; n   } n   return len; n } n unsigned long long gcd(unsigned long long a, unsigned long long b) { n   return b == 0 ? a : gcd(b, (a % b)); n } n unsigned long long lcm(unsigned long long a, unsigned long long b) { n   unsigned long long t = gcd(a, b); n   if (getLen(a / t) + getLen(b) > 22) return 0; n   return a / gcd(a, b) * b; n } n int main() { n   unsigned long long t, w, b; n   unsigned long long p; n   cin >> t >> w >> b; n   if (w == b) { n     printf( 1/1 n ); n     return 0; n   } n   unsigned long long mi = min(w, b); n   if (t < mi) { n     printf( 1/1 n ); n     return 0; n   } n   unsigned long long l = lcm(w, b); n   if (l <= 0) n     p = mi - 1; n   else { n     unsigned long long x = t / l; n     if (x == 0) { n       p = mi - 1; n     } else { n       p = min(mi, t - x * l + 1) + x * mi - 1; n     } n   } n   unsigned long long d = gcd(p, t); n   printf( %lld/%lld n , p / d, t / d); n   return 0; n } n