#include <bits/stdc++.h> n using namespace std; n complex<double> a[524288], b[524288], r[524288], o[524289]; n void fft(complex<double> *x, int n, int base) { n   int size = 0, mask = (n >> 1) - 1; n   for (int i = base - 1; i >= 0; --i, size = (size << 1) | 1, mask >>= 1) { n     for (int j = 0; j < n; ++j) { n       int front = j >> i, back = j & mask; n       int a = (front & size) << 1, b = a | 1; n       r[j] = x[(a << i) | back] + n              o[(front << i) * (524288 / n)] * x[(b << i) | back]; n     } n     for (int j = 0; j < n; ++j) x[j] = r[j]; n   } n } n void ifft(complex<double> *x, int n, int base) { n   fft(x, n, base); n   reverse(x + 1, x + n); n   for (int i = 0; i < n; ++i) x[i] /= n; n } n void pin(vector<int> *v) { n   cout << v->size() <<   : ; n   for (int i : *v) cout << i <<    ; n   cout << endl; n } n vector<int> *mg(vector<int> *pa, vector<int> *pb) { n   int nn = pa->size() + pb->size() + 2, base = 0; n   while ((1 << base) < nn) ++base; n   nn = (1 << base); n   for (int i = 0; i < pa->size(); ++i) a[i] = complex<double>((*pa)[i], 0); n   for (int i = pa->size(); i <= nn; ++i) a[i] = complex<double>(0, 0); n   for (int i = 0; i < pb->size(); ++i) b[i] = complex<double>((*pb)[i], 0); n   for (int i = pb->size(); i <= nn; ++i) b[i] = complex<double>(0, 0); n   fft(a, nn, base); n   fft(b, nn, base); n   for (int i = 0; i < nn; ++i) a[i] *= b[i]; n   ifft(a, nn, base); n   vector<int> *rt = new vector<int>(); n   for (int i = 0; i < nn; ++i) n     rt->push_back(((long long)(real(a[i]) + 0.5)) % 1009); n   while (rt->size() && rt->back() == 0) rt->pop_back(); n   delete pa; n   delete pb; n   return rt; n } n int cnt[524288]; n const double PI = acos(-1); n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (int i = 0; i <= 524288; ++i) n     o[i] = exp(2. * i * PI / 524288 * complex<double>(0, 1)); n   int n, m, k; n   cin >> n >> m >> k; n   while (n--) { n     int t; n     cin >> t; n     ++cnt[t]; n   } n   auto cmp = [&](const vector<int> *a, const vector<int> *b) { n     return a->size() > b->size(); n   }; n   priority_queue<vector<int> *, vector<vector<int> *>, decltype(cmp)> pq(cmp); n   for (int i = 1; i <= m; ++i) { n     if (cnt[i] == 0) continue; n     vector<int> *v = new vector<int>(); n     for (int j = 0; j <= cnt[i]; ++j) v->push_back(1); n     pq.push(v); n   } n   while (pq.size() > 1u) { n     vector<int> *p1, *p2; n     p1 = pq.top(); n     pq.pop(); n     p2 = pq.top(); n     pq.pop(); n     pq.push(mg(p1, p2)); n   } n   vector<int> *ans = pq.top(); n   cout << (ans->size() > k ? (*ans)[k] : 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, v; n map<int, int> mp; n vector<int> a; n bool can(int mid) { n   int rem = 0; n   for (int i = 0; i < a.size(); i++) { n     if (mp[a[i]] >= mid) { n       rem += mp[a[i]] / mid; n     } n   } n   return rem >= k; n } n bool cmp(int s, int f) { return mp[s] > mp[f]; } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> v; n     mp[v]++; n   } n   for (auto i : mp) { n     a.push_back(i.first); n   } n   sort(a.begin(), a.end(), cmp); n   int l, r, ans = 1; n   l = 1, r = n; n   while (l < r) { n     int mid = (l + r) / 2; n     if (can(mid)) { n       l = mid + 1; n       ans = mid; n     } else { n       r = mid; n     } n   } n   n = a.size(); n   for (int i = 0; i < n; i++) { n     if (k - mp[a[i]] / ans >= 0) { n       for (int j = 0; j < mp[a[i]] / ans; j++) cout << a[i] <<    ; n       k -= mp[a[i]] / ans; n     } else { n       for (int j = 0; j < k; j++) cout << a[i] <<    ; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void update(int *arr, int pos, int diff) { n   while (pos <= 100001) { n     arr[pos] = max(arr[pos], diff); n     pos += pos & -pos; n   } n } n int sum(int *arr, int pos) { n   int ret = 0; n   while (pos > 0) { n     ret = max(ret, arr[pos]); n     pos -= pos & -pos; n   } n   return ret; n } n int n, c, d; n int coin[100001], dia[100001]; n int main() { n   scanf( %d%d%d , &n, &c, &d); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int b, p; n     char k; n     int coin_max, dia_max, m; n     coin_max = 0; n     dia_max = 0; n     scanf( %d%d %c , &b, &p, &k); n     if (k ==  C ) { n       coin_max = sum(dia, d); n       if (p > c) continue; n       coin_max = max(coin_max, sum(coin, c - p)); n       update(coin, p, b); n     } else { n       dia_max = sum(coin, c); n       if (p > d) continue; n       dia_max = max(dia_max, sum(dia, d - p)); n       update(dia, p, b); n     } n     if (coin_max == 0 && dia_max == 0) continue; n     m = max(b + coin_max, b + dia_max); n     ans = max(ans, m); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n signed main() { n   std::string s; n   std::cin >> s; n   long long n = s.size(); n   long long p = 0; n   long long ttl = 0; n   while (1) { n     if (p == n) break; n     while (p < n && std::isalpha(s[p])) ++p; n     long long a = 0; n     std::string tmp; n     while (p < n && (s[p] ==  .  || std::isdigit(s[p]))) { n       tmp.push_back(s[p]); n       if (std::isdigit(s[p])) a = a * 10 + (s[p] -  0 ); n       ++p; n     } n     if (tmp.size() < 3 || tmp[tmp.size() - 3] !=  . ) a *= 100; n     ttl += a; n   } n   long long integral = ttl / 100; n   std::deque<char> output; n   if (integral == 0) { n     output.push_front( 0 ); n   } else { n     long long dgt = 0; n     while (integral) { n       if (dgt == 3) { n         dgt = 0; n         output.push_front( . ); n       } n       output.push_front(integral % 10 +  0 ); n       integral /= 10; n       ++dgt; n     } n   } n   long long fractional = ttl % 100; n   if (fractional) { n     output.push_back( . ); n     output.push_back( 0  + fractional / 10); n     output.push_back( 0  + fractional % 10); n   } n   for (auto ch : output) std::cout << ch; n   std::cout << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n vector<char> cc; n void solve() { n   int n, m, k, d = 0; n   cin >> n >> m >> k; n   vector<string> arr(n), ans(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     ans[i].resize(m); n     for (int j = 0; j < m; j++) d += (arr[i][j] ==  R ); n   } n   vector<int> cnt; n   for (int i = 0; i < k - d % k; i++) cnt.push_back(d / k); n   for (int i = 0; i < d % k; i++) cnt.push_back(d / k + 1); n   vector<int> have(k); n   int ptr = 0; n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       for (int j = 0; j < m; j++) { n         char cur = cc[ptr]; n         ans[i][j] = cur; n         have[ptr] += (arr[i][j] ==  R ); n         if (have[ptr] == cnt[ptr] && ptr < k - 1) ptr++; n       } n     } else { n       for (int j = m - 1; j >= 0; j--) { n         char cur = cc[ptr]; n         ans[i][j] = cur; n         have[ptr] += (arr[i][j] ==  R ); n         if (have[ptr] == cnt[ptr] && ptr < k - 1) ptr++; n       } n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<   n ; n } n int main() { n   srand(1373737); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   for (char c =  0 ; c <=  9 ; c++) cc.push_back(c); n   for (char c =  a ; c <=  z ; c++) cc.push_back(c); n   for (char c =  A ; c <=  Z ; c++) cc.push_back(c); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const double eps = (double)1e-8; n const int mod = (int)1000000007; n const int maxn = (int)1e6 + 5; n int n, l, r, z[2 * maxn]; n string a, b, c, d; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   cin >> a; n   cin >> b; n   c =   ; n   for (int i = (n - 1); i >= (0); i--) { n     if (a[i] ==  W ) c +=  E ; n     if (a[i] ==  E ) c +=  W ; n     if (a[i] ==  N ) c +=  S ; n     if (a[i] ==  S ) c +=  N ; n   } n   d = c +  #  + b; n   l = 0; n   r = 0; n   z[0] = 0; n   for (int i = (1); i < (d.size()); i++) { n     z[i] = 0; n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < d.size() && d[i + z[i]] == d[z[i]]) { n       z[i]++; n     } n     if (i + z[i] - 1 > r) { n       l = i; n       r = i + z[i] - 1; n     } n     if (i + z[i] - 1 == (int)d.size() - 1) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const int K = 3; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int ans = -1; n     for (int i = 1; i <= n; ++i) { n       long long int x; n       cin >> x; n       ans = max(ans, x - i); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   cin >> n; n   long long ans = n; n   for (int i = 2; i * i <= n; ++i) n     if (n % i == 0) { n       while (n % i == 0) n /= i, ans += n; n     } n   if (n > 1) ++ans; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200200]; n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int pref = 0; n   for (int i = 0; i < n; i++) { n     if (a[i]) n       pref++; n     else n       break; n   } n   if (pref == n) { n     printf( NO n ); n     return; n   } n   vector<int> rj; n   if (pref % 2 == 1) { n     for (int i = pref - 1; i < n - 2; i += 1) { n       int xr = a[i] ^ a[i + 1] ^ a[i + 2]; n       int sm = a[i] + a[i + 1] + a[i + 2]; n       if (xr == 0) { n         rj.push_back(i + 1); n         a[i] = 0; n         a[i + 1] = 0; n         a[i + 2] = 0; n         pref -= 1; n         break; n       } else { n         if (sm == 1) { n           rj.push_back(i + 1); n           a[i + 1] = 1; n           a[i + 2] = 1; n           i += 1; n           pref += 2; n         } else if (sm == 3) { n           i += 1; n           pref += 2; n         } n       } n     } n   } n   if (pref >= n) { n     printf( NO n ); n     return; n   } n   if (pref % 2 == 1) { n     printf( NO n ); n     return; n   } n   for (int i = pref - 1; i >= 1; i -= 2) { n     rj.push_back(i); n   } n   for (int i = 0; i < pref; i++) a[i] = 0; n   for (int i = pref; i < n - 2; i++) { n     int xr = a[i] ^ a[i + 1] ^ a[i + 2]; n     int sm = a[i] + a[i + 1] + a[i + 2]; n     if (sm == 0) continue; n     if (a[i]) { n       if (xr == 1) { n         rj.push_back(i + 1); n         rj.push_back(i); n         a[i] = 0; n         i++; n         a[i + 1] = 1; n         a[i] = 0; n         i--; n       } else { n         rj.push_back(i + 1); n         a[i] = 0; n         a[i + 1] = 0; n         a[i + 2] = 0; n       } n     } else { n       if (xr == 0) { n         rj.push_back(i + 1); n         a[i] = 0; n         a[i + 1] = 0; n         a[i + 2] = 0; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[i]) { n       printf( NO n ); n       return; n     } n   } n   printf( YES n%d n , rj.size()); n   for (int i : rj) { n     printf( %d  , i); n   } n   printf(  n ); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 5; n struct Edge { n   int to, nxt; n } e[N * 2]; n int n, head[N], cntE, siz[N], f[N][2]; n int ans[N], g[N], mxpt[N], pos[N]; n inline void add(int u, int v) { n   e[++cntE] = (Edge){v, head[u]}; n   head[u] = cntE; n } n void dfs1(int u, int fa) { n   siz[u] = 1; n   for (int i = head[u], v; i; i = e[i].nxt) n     if ((v = e[i].to) != fa) { n       dfs1(v, u); n       siz[u] += siz[v]; n       if (siz[v] > siz[mxpt[u]]) mxpt[u] = v; n       int w = siz[v] <= n / 2 ? siz[v] : f[v][0]; n       if (f[u][0] < w) n         f[u][1] = f[u][0], f[u][0] = w, pos[u] = v; n       else n         f[u][1] = max(f[u][1], w); n     } n } n void dfs2(int u, int fa) { n   if (siz[mxpt[u]] > n / 2) n     ans[u] = siz[mxpt[u]] - f[mxpt[u]][0] <= n / 2; n   else n     ans[u] = n - siz[u] - g[u] <= n / 2; n   for (int i = head[u], v; i; i = e[i].nxt) n     if ((v = e[i].to) != fa) { n       int w = (n - siz[u] <= n / 2) ? n - siz[u] : g[u]; n       g[v] = max(w, f[u][pos[u] == v]); n       dfs2(v, u); n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1, a, b; i < n; ++i) { n     scanf( %d%d , &a, &b); n     add(a, b); n     add(b, a); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n pair<int, int> a[200]; n pair<int, int> b[200]; n int pp[200]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int aa, bb; n     scanf( %d%d , &aa, &bb); n     a[i].first = min(aa, bb), a[i].second = max(aa, bb); n   } n   for (int i = 1; i <= m; i++) { n     int aa, bb; n     scanf( %d%d , &aa, &bb); n     b[i].first = min(aa, bb), b[i].second = max(aa, bb); n   } n   for (int i = 1; i <= n; i++) { n     bool flag1 = false, flag2 = false; n     for (int j = 1; j <= m; j++) { n       if (a[i].first != b[j].first && a[i].second == b[j].second) n         pp[b[j].second] = 1, flag1 = true; n       if (a[i].second != b[j].second && a[i].first == b[j].first) n         pp[b[j].first] = 1, flag2 = true; n       if (a[i].second != b[j].first && a[i].first == b[j].second) n         pp[b[j].second] = 1, flag2 = true; n       if (a[i].first != b[j].second && a[i].second == b[j].first) n         pp[b[j].first] = 1, flag1 = true; n       if (flag1 && flag2 && a[i].first != a[i].second) { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     bool flag1 = false, flag2 = false; n     for (int j = 1; j <= n; j++) { n       if (a[j].first != b[i].first && a[j].second == b[i].second) flag2 = true; n       if (a[j].second != b[i].second && a[j].first == b[i].first) flag1 = true; n       if (a[j].second != b[i].first && a[j].first == b[i].second) flag2 = true; n       if (a[j].first != b[i].second && a[j].second == b[i].first) flag1 = true; n       if (flag1 && flag2 && b[i].first != b[i].second) { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   int sum = 0; n   int ans; n   for (int i = 1; i <= 9; i++) { n     if (pp[i]) ans = i; n     sum += pp[i]; n   } n   if (sum == 1) { n     cout << ans << endl; n     return 0; n   } else { n     cout << 0 << endl; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)2e5 + 10; n vector<int> res[maxn]; n int main() { n   string s; n   cin >> s; n   stack<int> st, st2; n   int ma = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) { n       if (st.empty()) { n         cout << -1 << endl; n         return 0; n       } n       res[st.top()].push_back(i + 1); n       st2.push(st.top()); n       st.pop(); n     } else { n       if (!st2.empty()) { n         res[st2.top()].push_back(i + 1); n         st.push(st2.top()); n         st2.pop(); n       } else { n         res[ma].push_back(i + 1); n         st.push(ma); n         ma++; n       } n     } n   } n   if (!st2.empty()) { n     cout << -1 << endl; n     return 0; n   } n   int cnt = 0; n   for (int i = 0; i < maxn; i++) n     if (!res[i].empty()) cnt++; n   cout << cnt << endl; n   for (int i = 0; i < maxn; i++) n     if (!res[i].empty()) { n       printf( %d , res[i].size()); n       for (int j = 0; j < res[i].size(); j++) { n         printf(  %d , res[i][j]); n       } n       printf(  n ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 255; n int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; n char s[MX][MX]; n long long tot[MX][MX]; n int was[MX][MX]; n int cur[MX][MX]; n int main() { n   int n, m, p, w; n   scanf( %d %d %d %d , &n, &m, &p, &w); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i]); n     for (int j = m; j >= 1; j--) { n       s[i][j] = s[i][j - 1]; n     } n     s[i][0] =   0 ; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (s[i][j] >=  A  && s[i][j] <=  Z ) { n         cnt++; n         queue<pair<int, int> > q; n         q.push(make_pair(i, j)); n         cur[i][j] = p * (s[i][j] -  A  + 1); n         was[i][j] = cnt; n         while (!q.empty()) { n           int xx = q.front().first, yy = q.front().second; n           q.pop(); n           tot[xx][yy] += cur[xx][yy]; n           for (int dir = 0; dir < 4; dir++) { n             int nx = xx + dx[dir], ny = yy + dy[dir]; n             if (was[nx][ny] != cnt && cur[xx][yy] / 2 > 0 && s[nx][ny] !=  *  && n                 nx >= 1 && nx <= n && ny >= 1 && ny <= m) { n               was[nx][ny] = cnt; n               cur[nx][ny] = cur[xx][yy] / 2; n               q.push(make_pair(nx, ny)); n             } n           } n         } n       } n     } n   } n   int res = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (tot[i][j] > w) res++; n     } n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > ans; n int p[2000], damage[2000], u[2000]; n int main() { n   int n, Max, r, used = 0, TT; n   cin >> n >> Max >> r; n   for (int i = 1; i <= n; i++) cin >> p[i] >> damage[i]; n   int cap = Max; n   memset(u, 0, sizeof(u)); n   for (int s = 0;; s++) { n     int cur = -1; n     for (int i = 1; i <= n; i++) n       if (!u[i] && p[i] / 100.00 >= Max / double(cap)) n         if (cur == -1 || damage[i] > damage[cur]) cur = i; n     if (cur == -1 && r >= 0) { n       cout <<  NO  << endl; n       return 0; n     } n     if (cur != -1) { n       r -= damage[cur]; n       u[cur] = 1; n       used++; n       ans.push_back(make_pair(s, cur)); n     } n     Max += r; n     if (Max > cap) Max = cap; n     if (Max <= 0) { n       TT = s + 1; n       break; n     } n   } n   cout <<  YES  << endl; n   cout << TT <<     << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 100001; n bool ok; n long long int n, q; n long long int trie[6 * N][5], done[6 * N]; n long long int nxt = 1; n string s; n void insert(string s) { n   long long int node = 0; n   for (char c : s) { n     if (trie[node][c -  a ] == 0) { n       node = trie[node][c -  a ] = nxt++; n     } else { n       node = trie[node][c -  a ]; n     } n   } n   done[nxt - 1] = 1; n } n void dfs(long long int u, long long int idx, long long int f) { n   if (f >= 2) return; n   if (idx == s.length() - 1 && f == 1 && done[u]) { n     ok = 1; n     return; n   } n   if (idx == s.length() - 1 || done[u]) return; n   for (long long int i = 0; i < 3; i++) { n     if (trie[u][i] == 0) continue; n     if (s[idx + 1] == (char)(i +  a )) n       dfs(trie[u][i], idx + 1, f); n     else n       dfs(trie[u][i], idx + 1, f + 1); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (long long int i = 0; i < n; i++) { n     cin >> s; n     insert(s); n   } n   for (long long int i = 0; i < q; i++) { n     cin >> s; n     ok = 0; n     dfs(0, -1, 0); n     cout << (ok ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[310]; n bool mp[310][310]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> ar[i]; n   string str; n   for (int i = 1; i <= n; i++) { n     cin >> str; n     for (int j = 0; j < n; j++) n       if (str[j] ==  1 ) mp[i][j + 1] = true; n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (mp[i][k] && mp[k][j]) mp[i][j] = true; n   for (int i = 1; i < n; i++) n     for (int j = i + 1; j <= n; j++) n       if (ar[i] > ar[j] && mp[i][j]) swap(ar[i], ar[j]); n   cout << ar[1]; n   for (int i = 2; i <= n; i++) cout <<     << ar[i]; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long int> v(n + 1, 0); n   for (long long int i = 1; i <= n; i++) { n     if (s[i - 1] ==  ( ) n       v[i] = v[i - 1] + 1; n     else n       v[i] = v[i - 1] - 1; n   } n   if (v[n] != 0) { n     cout <<  -1  <<   n ; n     return 0; n   } n   long long int ans = 0; n   long long int temp = 0; n   for (long long int i = 1; i <= n; i++) { n     if (v[i] < 0) { n       temp++; n     } else if (v[i] == 0 && v[i - 1] < 0) { n       temp++; n       ans += temp; n       temp = 0; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using vi = vector<int>; n using vll = vector<ll, ll>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n ll modpow(ll a, ll b, ll mod = (ll)(1e9 + 7)) { n   if (!b) return 1; n   a %= mod; n   return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int DEBUG = 1; n const int mxk = 13, mxm = 4, dsz = (1 << mxm) * mxk, mod = 1e9 + 7; n int n, m, k; n void add(ll &a, ll b) { n   a += b; n   if (a >= mod) a -= mod; n } n struct Matrix { n   ll a[dsz][dsz]; n   Matrix() { n     for (int i = 0; i < dsz; i++) n       for (int j = 0; j < dsz; j++) a[i][j] = 0; n   } n } id; n void init() { n   for (int i = 0; i < dsz; i++) id.a[i][i] = 1; n } n Matrix operator*(Matrix a, Matrix b) { n   Matrix ret; n   for (int i = 0; i < dsz; i++) n     for (int j = 0; j < dsz; j++) n       for (int kk = 0; kk < dsz; kk++) n         add(ret.a[i][kk], a.a[i][j] * b.a[j][kk] % mod); n   return ret; n } n int cnv(int mask, int kk) { return (kk << mxm) + mask; } n int rot(int mask) { return (mask << 1) % (1 << m); } n Matrix modpow(Matrix a, ll b) { n   Matrix ans = id; n   while (b) { n     if (b & 1) ans = ans * a; n     a = a * a; n     b /= 2; n   } n   return ans; n } n void solve() { n   init(); n   Matrix op; n   for (int mask = 0; mask < 1 << m; mask++) n     for (int kk = 0; kk <= k; kk++) { n       add(op.a[cnv(rot(mask), kk)][cnv(mask, kk)], 1); n       if (kk < k) n         add(op.a[cnv(rot(mask) + 1, kk + 1)][cnv(mask, kk)], n             1 + __builtin_popcount(mask)); n     } n   Matrix b; n   b.a[0][0] = 1; n   Matrix res = modpow(op, n) * b; n   ll ans = 0; n   for (int mask = 0; mask < 1 << m; mask++) add(ans, res.a[cnv(mask, k)][0]); n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   while (cin >> n >> k >> m) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chmax(T& a, const T& b) { n   return a < b ? (a = b, true) : false; n } n template <typename T> n bool chmin(T& a, const T& b) { n   return b < a ? (a = b, true) : false; n } n int N; n string S; n int main() { n   cin >> S; n   N = S.size(); n   for (int i = (1); i < (N); i++) { n     if (S[i] == S[i - 1]) { n       cout <<  Impossible  << endl; n       return 0; n     } n   } n   char twice; n   for (char c =  A ; c <=  Z ; c++) { n     if (count((S).begin(), (S).end(), c) == 2) { n       twice = c; n       break; n     } n   } n   int x = S.find(twice), y = S.find(twice, x + 1); n   string a = S.substr(0, x), b = S.substr(x + 1, y - x - 1), n          c = S.substr(y + 1); n   string t = b + c + a; n   vector<string> ans(2, string(13,  . )); n   ans[0][12 - b.size() / 2] = twice; n   int idx = 0; n   bool flag = false; n   while (true) { n     for (int i = 0; i < 13; i++) { n       if (ans[0][i] == twice && flag == false) { n         flag = true; n       } else if (ans[0][i] == twice && flag == true) { n         cout << ans[0] << endl; n         cout << ans[1] << endl; n         return 0; n       } else if (flag && idx < t.size()) { n         ans[0][i] = t[idx++]; n       } n     } n     for (int i = 12; i >= 0; i--) { n       if (flag && idx < t.size()) { n         ans[1][i] = t[idx++]; n       } n     } n   } n   return 0; n } n


n #include<bits/stdc++.h> n using namespace std; n #define ll long long n #define pb push_back n #define f first n #define s second n #define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) n /* n  n ll dx[8]={-1,-1,-1,0,0,1,1,1}; n ll dy[8]={-1,0,1,-1,1,-1,0,1}; n bool sortbysec(const pair<int,int> &a, n               const pair<int,int> &b) n { n     return (a.second < b.second); n } n */ n int main() n { n      ll t; n      cin>>t; n      while(t--) n      { n          ll n; n          cin>>n; n          for(int i=2;i<=n;i++) n             cout<<i<<   ; n          cout<< 1 <<endl; n      } n  n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n map<int, int> mp; n int n; n struct pnt { n   int x, w; n   bool operator<(const pnt &p) const { return x < p.x; } n } p[maxn]; n int ask(int x) { n   int res = 0; n   for (int i = x; i > 0; i -= (i & -i)) n     if (mp.count(i)) res = max(res, mp[i]); n   return res; n } n void update(int x, int v) { n   for (int i = x; i < 0x3f3f3f3f; i += (i & -i)) mp[i] = max(mp[i], v); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &p[i].x, &p[i].w); n   sort(p, p + n); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int tmp = ask(p[i].x - p[i].w); n     ans = max(ans, tmp + 1); n     update(p[i].x + p[i].w, tmp + 1); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   for (int i = n + 1; i < 10000; i++) { n     int d = i % 10; n     int c = (i % 100 - d) / 10; n     int a = i / 1000; n     int b = (i - 1000 * a) / 100; n     if (a != b && a != c && a != d && b != c && b != d && c != d) { n       printf( %d%d%d%d , a, b, c, d); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 2; n long long n, m; n long long a[N], b[N]; n long long f(long long x) { n   long long res = 0; n   for (long long(i) = (0); (i) < (n); (i)++) { n     if (a[i] < x) res += llabs(a[i] - x); n   } n   for (long long(i) = (0); (i) < (m); (i)++) { n     if (b[i] > x) res += llabs(b[i] - x); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n >> m; n   for (long long(i) = (0); (i) < (n); (i)++) cin >> a[i]; n   for (long long(i) = (0); (i) < (m); (i)++) cin >> b[i]; n   long long le = 1, ri = 1e9; n   while (llabs(ri - le) > 3) { n     long long m1 = le + (ri - le) / 3; n     long long m2 = ri - (ri - le) / 3; n     if (f(m1) < f(m2)) n       ri = m2; n     else n       le = m1; n   } n   long long ans = 1e18; n   for (long long(x) = (le); (x) < (ri + 1); (x)++) { n     ans = min(ans, f(x)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long ma = -1e15, need = 0; n     for (int i = 0; i < n; i++) { n       long long a; n       cin >> a; n       ma = max(a, ma); n       need = max(need, ma - a); n     } n     long long ans = 0; n     while ((1ll << ans) - 1 < need) ans++; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double eps = 1e-12; n   double y1, y2, yw, xb, yb, r; n   cin >> y1 >> y2 >> yw >> xb >> yb >> r; n   double yk = yw - r; n   double yt = y1 + r; n   double d1 = yk - yt; n   double d2 = yk - yb; n   double xk = xb * d1 / (d2 + d1); n   double A = yt - yk; n   double B = xk; n   double C = -B * yt; n   double h = fabs(B * y2 + C) / sqrt(A * A + B * B); n   if (r > h - eps) { n     cout << -1 << endl; n   } else { n     printf( %.12lf n , xk - eps); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int n, a[500010], b[500010], c[500010], bit[500010]; n void init() { n   for (int i = 1; i <= n; i++) { n     bit[i] = 0; n   } n } n int lowbit(int x) { return x & (-x); } n void add(int u, int x) { n   for (; u <= n; u += lowbit(u)) { n     bit[u] = (bit[u] + x) % mod; n   } n } n int sum(int u) { n   int res = 0; n   for (; u >= 1; u -= lowbit(u)) { n     res = (res + bit[u]) % mod; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) { n     a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; n   } n   int ans = 0; n   init(); n   for (int i = 1; i <= n; i++) { n     ans = (ans + 1LL * b[a[i]] * sum(a[i]) % mod * (n - i + 1) % mod) % mod; n     c[a[i]] += sum(a[i]) * (n - i + 1); n     add(a[i], i); n   } n   init(); n   for (int i = n; i >= 1; i--) { n     ans = (ans + 1LL * b[a[i]] * sum(a[i]) % mod * i % mod) % mod; n     c[a[i]] += sum(a[i]) * i; n     add(a[i], n - i + 1); n   } n   for (int i = 1; i <= n; i++) { n     ans = (ans + 1LL * b[a[i]] * i % mod * (n - i + 1) % mod) % mod; n     c[a[i]] += i * (n - i + 1); n   } n   printf( %d n , ans); n   return 0; n } n


#include<bits/stdc++.h> n // #define int long long n #define ld long double n #define fi first n #define se second n #define vll vector<int> n #define pii pair<int,int> n #define pb push_back n #define sz(v) (int)(v).size() n #define inf (int)(1e8) n #define md (int)(1e9+7) n #define all(v) (v).begin(),(v).end() n #define rep(i,a,b) for(int i=a;i<b;++i) n using namespace std; n  n const int M = 50 + 11; n const int K = 251; n  n int n; n string s; n int dp[2][M][K][K]; n /* n int f(int i,int car,int pos,int neg) { n  if(pos<0 or neg<0 or car+30>=M or car+30<0) return inf; n  if(i>=n) return car?inf:0; n  if(dp[i][car+30][pos][neg]!=-1) return dp[i][car+30][pos][neg]; n  int ans=min(f(i,car,pos-1,neg),f(i,car,pos,neg-1)); n  int now=s[i]- 0 ,tot=pos-neg+car; n  if((tot%10+10)%10==now) { n   ans=min(ans,f(i+1,tot/10+(tot>0?0:-1)*(bool)(tot%10),pos,neg)+pos+neg); n  } n  return dp[i][car+30][pos][neg]=ans; n } n */ n int32_t main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(0); n      n     cin>>s; n     reverse(all(s)); n     s+= 0 ; n     n=sz(s); n     rep(i,0,2)rep(car,0,M)rep(pos,0,K)rep(neg,0,K) dp[i][car][pos][neg]=inf; n    rep(pos,0,K)rep(neg,0,K) dp[0][0+M/2][pos][neg]=0; n    for(int i=n-1;i>=0;--i) { n     rep(car,-M/2,M/2+1)rep(pos,0,K)rep(neg,0,K) { n      int &ans=dp[1][car+M/2][pos][neg]; n      if(pos) ans=min(ans,dp[1][car+M/2][pos-1][neg]); n      if(neg) ans=min(ans,dp[1][car+M/2][pos][neg-1]); n      int now=s[i]- 0 ,tot=pos-neg+car; n      if((tot%10+10)%10==now) { n       int ncar=tot/10+(tot>0?0:-1)*(bool)(tot%10)+M/2; n       if(ncar<0 or ncar>=M) continue; n       ans=min(ans,dp[0][ncar][pos][neg]+pos+neg); n      } n     } n     rep(car,-M/2,M/2+1)rep(pos,0,K)rep(neg,0,K) { n      dp[0][car+M/2][pos][neg]=dp[1][car+M/2][pos][neg]; n      dp[1][car+M/2][pos][neg]=inf; n     } n    } n    cout<<dp[0][0+M/2][K-1][K-1]; n    /* n     memset(dp,-1,sizeof(dp)); n     cout<<f(0,0,K-1,K-1); n     */ n      n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long mx, mx2; n void solve(long long m, long long blocks, long long sum) { n   if (m == 0) { n     if (blocks > mx) { n       mx = blocks; n       mx2 = sum; n     } else if (blocks == mx && sum > mx2) { n       mx2 = sum; n     } n     return; n   } n   long long x = 1; n   while ((x + 1) * (x + 1) * (x + 1) <= m) x++; n   solve(m - x * x * x, blocks + 1, sum + x * x * x); n   solve(x * x * x - 1 - (x - 1) * (x - 1) * (x - 1), blocks + 1, n         sum + (x - 1) * (x - 1) * (x - 1)); n } n int main() { n   long long m; n   cin >> m; n   solve(m, 0, 0); n   cout << mx <<     << mx2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[300]; n int main() { n   int n, a, minn = 99999, sum = 0, sum2 = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &num[i]); n   for (int i = 1; i <= n; i++) sum += abs(num[i]); n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long t, n, m; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     if (n == 1 || m == 1 || (n == 2 && m == 2)) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const double eps = 1e-12; n const long long MOD = 1e9 + 7; n const double PI = acos(-1.0); n const long long INF = 1e18; n void solve() { n   long long n, m, k; n   cin >> n >> m >> k; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<long long> pref(n + 1, 0); n   pref[1] = a[0]; n   for (long long i = 2; i <= n; i++) { n     pref[i] = pref[i - 1] + a[i - 1]; n   } n   vector<long long> sums(n + 1, 0); n   for (long long i = 0; i + m <= n; i++) { n     sums[i] = pref[i + m] - pref[i]; n   } n   long long dp[n + 1][k + 1]; n   memset(dp, 0, sizeof(dp)); n   for (long long i = 0; i <= n; i++) { n     for (long long j = 1; j <= k; j++) { n       if (i < j - 1) continue; n       dp[i][j] = max(((i - m >= 0) ? dp[i - m][j - 1] : 0) + sums[i], n                      ((i - 1 >= 0) ? dp[i - 1][j] : 0)); n     } n   } n   long long ans = 0; n   for (long long i = 0; i <= n; i++) { n     ans = max(dp[i][k], ans); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int k = a + b + c + d; n   if (k % 2 == 0) { n     if (a + b == k / 2 || a + c == k / 2 || a + d == k / 2 || b + c == k / 2 || n         b + d == k / 2 || c + d == k / 2 || a + b + c == k / 2 || n         a + b + d == k / 2 || a + c + d == k / 2 || b + c + d == k / 2) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 510; n const int MAXQ = 100010; n const double EPS = 1e-10; n const double DINF = 1e30; n const int IINF = ~0U >> 3; n const long long LINF = 1LL << 61; n void debug() { cout <<  error_here  << endl; } n char str[MAXN][MAXN]; n int a[MAXN][MAXN], b[MAXN][MAXN]; n int n, m, k; n void init() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < n; ++i) scanf( %s , str[i]); n   memset(a, 0, sizeof(a)); n   memset(b, 0, sizeof(b)); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) a[i][j] = (str[i - 1][j - 1] ==  1 ); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (a[i][j] && a[i - 1][j] && a[i + 1][j] && a[i][j - 1] && a[i][j + 1]) n         b[i][j] = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]; n } n int get_sum(int x, int y, int xx, int yy) { n   return b[xx][yy] - b[xx][y - 1] - b[x - 1][yy] + b[x - 1][y - 1]; n } n void solve() { n   long long ans = 0; n   for (int u = 1; u <= n; ++u) n     for (int v = u + 2; v <= n; ++v) { n       for (int i = 1, j = 3; i <= m && j <= m;) { n         if (get_sum(u + 1, i + 1, v - 1, j - 1) >= k) n           ans += m - j + 1, i++; n         else n           j++; n       } n     } n   cout << ans << endl; n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int comp(pair<long long int, long long int> a, n                    pair<long long int, long long int> b) { n   if (a.first != b.first) return a < b; n   return a.second < b.second; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n; n   cin >> n; n   vector<pair<long long int, long long int>> v; n   for (long long int i = 0; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     x += 1000; n     y += 1000; n     v.push_back({x, y}); n   } n   sort(v.begin(), v.end(), comp); n   long long int cnt = 0; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = i + 1; j < n; j++) { n       if (((((v[i].first + v[j].first)) % 2 == 0) && n            (((v[i].second + v[j].second)) % 2 == 0))) { n         long long int x1, y1; n         x1 = (v[i].first + v[j].first) / 2; n         y1 = (v[i].second + v[j].second) / 2; n         long long int lo = i + 1, hi = j - 1, id = -1; n         while (lo <= hi) { n           long long int mid = (lo + hi) / 2; n           if (v[mid].first >= x1) { n             id = mid; n             hi = mid - 1; n           } else if (v[mid].first < x1) n             lo = mid + 1; n         } n         if (id != -1) { n           while (v[id].first == x1 && id < n) { n             if (v[id].second == y1) { n               cnt++; n               break; n             } n             id++; n           } n         } n       } n     } n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e5 + 10; n int p[mx]; n int main() { n   int n; n   cin >> n; n   if (n % 4 > 1) { n     puts( -1 ); n     return 0; n   } n   int l = 1, r = n; n   while (l < r) { n     p[l] = l + 1; n     p[l + 1] = r; n     p[r] = r - 1; n     p[r - 1] = l; n     l += 2; n     r -= 2; n   } n   if (l == r) p[l] = l; n   for (int i = 1; i <= n; i++) printf( %d  , p[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   long long int res = 1; n   a %= 1000000007; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n long long int modInverse(long long int a) { return power(a, 1000000007 - 2); } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   a %= b; n   return gcd(b, a); n } n const int N = 500023; n bool vis[N]; n vector<int> adj[N]; n void solve() { n   int n; n   cin >> n; n   vector<long long int> a(n + 1); n   for (long long int i = 0; i < n; i++) cin >> a[i + 1]; n   int moves = 1; n   long long int by_ana = 0; n   long long int by_bob = 0; n   int prev_a = 1; n   int prev_b = n + 1; n   by_ana += a[1]; n   int to_comp_by_bob = a[1]; n   int to_comp_by_ana = 0; n   int turn = 2; n   int f = 0; n   int eaten[n + 1]; n   for (int i = 1; i <= n; i++) eaten[i] = 0; n   eaten[1] = 1; n   while (1) { n     moves++; n     int temp = 0; n     if (prev_a == prev_b - 1 || prev_a > n || prev_b < 1) { n       moves--; n       break; n     } n     if (turn == 2) { n       while (temp <= to_comp_by_bob) { n         if (prev_b == prev_a || prev_b < 1) { n           f = 1; n           break; n         } n         prev_b--; n         if (eaten[prev_b] == 1) { n           f = 1; n           break; n         } n         temp += a[prev_b]; n         by_bob += a[prev_b]; n         eaten[prev_b] = 1; n       } n       to_comp_by_ana = temp; n       turn = 1; n       if (f == 1) break; n     } else if (turn == 1) { n       while (temp <= to_comp_by_ana) { n         if (prev_b == prev_a || prev_a > n) { n           f = 1; n           break; n         } n         prev_a++; n         if (eaten[prev_a] == 1) { n           f = 1; n           break; n         } n         temp += a[prev_a]; n         by_ana += a[prev_a]; n         eaten[prev_a] = 1; n       } n       if (f == 1) break; n       to_comp_by_bob = temp; n       turn = 2; n     } n     if (f == 1) { n       if (temp == 0) moves--; n       break; n     } n   } n   if (n == 1) moves = 1; n   cout << moves <<     << by_ana <<     << by_bob << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   cin >> T; n   int t = 0; n   while (t++ < T) { n     solve(); n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pp(long long int a, long long int b) { n   if (b == 0) return 1; n   long long int t = pp(a, b / 2); n   if (b % 2 == 0) n     return t * t; n   else n     return t * t * a; n } n string ss(string s, long long int j) { n   for (long long int i = 0; i < j; i++) { n     if (s[i] ==  0 ) n       s[i] =  1 ; n     else n       s[i] =  0 ; n   } n   long long int l = 0, r = j - 1; n   while (l < r) { n     swap(s[l], s[r]); n     l++; n     r--; n   } n   return s; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int q; n   cin >> q; n   while (q--) { n     long long int i, j, k, n, m, x, y, l, r, c = 0; n     string s, s1, s2 =   ; n     cin >> n; n     cin >> s >> s1; n     vector<long long int> v; n     for (i = n - 1; i > 0; i--) { n       if (s[i] == s1[i]) n         continue; n       else { n         if ((s1[i] ==  0  && s[i] ==  1  && s[0] ==  1 ) || n             (s1[i] ==  1  && s[i] ==  0  && s[0] ==  0 )) { n           v.push_back(i + 1); n           s = ss(s, i + 1); n         } else if (s1[i] ==  0  && s[i] ==  1  && s[0] ==  0 ) { n           v.push_back(1); n           s[0] =  1 ; n           v.push_back(i + 1); n           s = ss(s, i + 1); n         } else { n           v.push_back(1); n           s[0] =  0 ; n           v.push_back(i + 1); n           s = ss(s, i + 1); n         } n       } n     } n     if (s[0] != s1[0]) v.push_back(1); n     cout << v.size() <<    ; n     for (i = 0; i < v.size(); i++) cout << v[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n static unsigned rnd() { n   static int second = 124612741; n   second ^= second << 13; n   second ^= second >> 17; n   return second ^= second << 5; n } n inline int fpow(int a, int st, int mod) { n   int ans = 1; n   while (st) { n     if (st % 2) ans = (long long)ans * a % mod; n     a = (long long)a * a % mod; n     st /= 2; n   } n   return ans; n } n int invmod(int first, int second) { n   int a = 1, b = 0, k; n   for (; second; swap(first, second), swap(a, b)) { n     k = first / second; n     first -= second * k; n     a -= b * k; n   } n   return a + max(b, 0); n } n inline int calcBits(int a) { return __builtin_popcount(a); } n struct Hash { n   Hash() : x1(0), x2(0) {} n   Hash(const int& x1, const int& x2) : x1(x1), x2(x2) {} n   int x1, x2; n   static int mod1, mod2; n   static int p1, p2; n   static int invp1, invp2; n   static bool initCalled; n   static const Hash ONE, ZERO, INVALID; n   static Hash UNIT, INV_UNIT; n   static bool prime(int a) { n     if (a == 1) return 0; n     for (int i = 2; i * i <= a; ++i) { n       if (a % i == 0) return false; n     } n     return true; n   } n   static int pickModHelper(int start, int bad) { n     int mn = 1000000000, best = -1; n     for (int _n(start + 10000), i(start); i <= _n; i++) { n       if (i == bad || !prime(i)) continue; n       int cur = calcBits(i); n       if (cur < mn) { n         mn = cur; n         best = i; n       } n     } n     return best; n   } n   static void init() { n     if (initCalled) return; n     initCalled = true; n     mod1 = pickModHelper(1000000000 + rnd() % 10000000, -1); n     while (!prime(mod1)) ++mod1; n     mod2 = pickModHelper(1000000000 + rnd() % 10000000, mod1); n     invp1 = fpow(p1, mod1 - 2, mod1); n     invp2 = fpow(p2, mod2 - 2, mod2); n     INV_UNIT = Hash(invp1, invp2); n   } n   inline Hash& operator+=(const Hash& other) { n     x1 += other.x1; n     if (x1 >= Hash::mod1) x1 -= Hash::mod1; n     x2 += other.x2; n     if (x2 >= Hash::mod2) x2 -= Hash::mod2; n     return *this; n   } n   inline Hash& operator-=(const Hash& other) { n     x1 -= other.x1; n     if (x1 < 0) x1 += Hash::mod1; n     x2 -= other.x2; n     if (x2 < 0) x2 += Hash::mod2; n     return *this; n   } n   inline Hash& operator*=(const Hash& other) { n     x1 = (long long)x1 * other.x1 % Hash::mod1; n     x2 = (long long)x2 * other.x2 % Hash::mod2; n     return *this; n   } n   inline Hash& operator*=(const int& other) { n     x1 = (long long)x1 * other % Hash::mod1; n     x2 = (long long)x2 * other % Hash::mod2; n     return *this; n   } n   inline Hash& operator^=(const int& power) { n     int st = power; n     Hash ans(1, 1); n     while (st) { n       if (st % 2) ans *= *this; n       x1 = (long long)x1 * x1 % Hash::mod1; n       x2 = (long long)x2 * x2 % Hash::mod2; n       st /= 2; n     } n     return *this; n   } n   inline bool operator==(const Hash& other) const { n     return x1 == other.x1 && x2 == other.x2; n   } n }; n bool Hash::initCalled = false; n int Hash::mod1 = 0; n int Hash::mod2 = 0; n int Hash::p1 = 23561; n int Hash::p2 = 10000019; n int Hash::invp1 = -1; n int Hash::invp2 = -1; n const Hash Hash::ONE(1, 1); n const Hash Hash::ZERO(0, 0); n const Hash Hash::INVALID(-1, -1); n Hash Hash::UNIT(Hash::p1, Hash::p2); n Hash Hash::INV_UNIT(-1, -1); n inline Hash operator+(const Hash& a, const Hash& b) { n   Hash ans(a.x1 + b.x1, a.x2 + b.x2); n   if (ans.x1 >= Hash::mod1) ans.x1 -= Hash::mod1; n   if (ans.x2 >= Hash::mod2) ans.x2 -= Hash::mod2; n   return ans; n } n inline Hash operator-(const Hash& a, const Hash& b) { n   Hash ans(a.x1 - b.x1, a.x2 - b.x2); n   if (ans.x1 < 0) ans.x1 += Hash::mod1; n   if (ans.x2 < 0) ans.x2 += Hash::mod2; n   return ans; n } n inline Hash operator*(const Hash& a, const Hash& b) { n   return Hash((long long)a.x1 * b.x1 % Hash::mod1, n               (long long)a.x2 * b.x2 % Hash::mod2); n } n inline Hash operator*(const Hash& a, const int& b) { n   return Hash((long long)a.x1 * b % Hash::mod1, n               (long long)a.x2 * b % Hash::mod2); n } n inline Hash operator^(const Hash& a, const int& power) { n   int st = power; n   Hash ans(1, 1), cur(a); n   while (st) { n     if (st % 2) ans *= cur; n     cur *= cur; n     st /= 2; n   } n   return ans; n } n inline Hash fpow(const Hash& a, const int& power) { return a ^ power; } n inline Hash invmod(const Hash& a) { n   return Hash(invmod(a.x1, Hash::mod1), invmod(a.x2, Hash::mod2)); n } n template <class H> n struct HashString : public string { n   HashString() : string() { precalc(0); } n   HashString(const HashString& other) : string(other) { precalc(size()); } n   HashString(HashString&& other) n       : hh(std::move(other.hh)), string(std::move(other)) { n     precalc(size()); n   } n   HashString(const string& other) : string(other) { n     precalc(size()); n     build(); n   } n   HashString(string&& other) : string(std::move(other)) { n     precalc(size()); n     build(); n   } n   HashString(const char* other) : string(other) { n     precalc(size()); n     build(); n   } n   inline void clear() { n     hh.clear(); n     string::clear(); n   } n   inline static H getPow(int p) { n     if (p < haveN) { n       return pp[p]; n     } else { n       return fpow(H::UNIT, p); n     } n   } n   inline static H getInvPow(size_t p) { n     if (p < invpp.size()) { n       return invpp[p]; n     } else { n       return fpow(H::INV_UNIT, p); n     } n   } n   inline H getInvRegister(const H& p) const { n     if (p == register_) { n       return registerValue_; n     } else { n       register_ = p; n       return registerValue_ = invmod(p); n     } n   } n   static void precalc(int N = 0) { n     Hash::init(); n     if (haveN >= N) return; n     pp.resize(N); n     invpp.resize(N); n     if (!haveN) { n       pp[0] = H::ONE; n       invpp[0] = H::ONE; n       haveN = 1; n     } n     for (int i = haveN; i < N; ++i) { n       pp[i] = pp[i - 1] * H::UNIT; n       invpp[i] = invpp[i - 1] * H::INV_UNIT; n     } n     haveN = N; n   } n   inline void build() { n     int curSize = size(); n     if (haveN <= curSize) { n       precalc(curSize); n     } n     for (int i = hh.size(); i < curSize; ++i) { n       H pr = i ? hh.back() : H::ZERO; n       hh.push_back(pr + getPow(i) * (*this)[i]); n     } n   } n   inline H getHash(int pos, int len) const { n     if (len <= 0) return H::ZERO; n     const int S = size(); n     if (pos + len - 1 < S) { n       H ans = hh[pos + len - 1]; n       if (pos) { n         ans -= hh[pos - 1]; n         ans *= getInvPow(pos); n       } n       return ans; n     } else { n       H head = hh.back(); n       if (pos) { n         head -= hh[pos - 1]; n         head *= getInvPow(pos); n       } n       int full = -1, en = -1; n       H mid = H::ZERO; n       int midlen = 0; n       if (len <= S) { n         en = pos + len - 1 - S; n         H tail = hh[en]; n         full = 0; n         return head + tail * getPow(S - pos); n       } else { n         en = (pos + len - 1) % S; n         H tail = hh[en]; n         full = (len - (S - pos) - (en + 1)) / S; n         mid = hh.back() * (H::ONE - getInvPow(full * S)) * n               getInvRegister(H::ONE - getInvPow(S)); n         return head + mid * getPow(S - pos) + tail * getPow(len - (en + 1)); n       } n     } n   } n   inline HashString& operator+=(const char& ch) { n     string::operator+=(ch); n     build(); n     return *this; n   } n   inline HashString& operator+=(const HashString& other) { n     string::operator+=(other); n     build(); n     return *this; n   } n   inline bool operator==(const HashString& other) const { n     return hh.back() == other.hh.back(); n   } n   inline bool operator<(const HashString& other) const { n     int s1 = size(); n     int s2 = other.size(); n     const int S = min(s1, s2); n     int l = 0, r = S + 1; n     while (r - l > 1) { n       int xx = (r + l) / 2; n       if (getHash(0, xx) == other.getHash(0, xx)) n         l = xx; n       else n         r = xx; n     } n     char c1 = 0, c2 = 0; n     if (l < s1) c1 = (*this)[l]; n     if (l < s2) c2 = other[l]; n     return c1 < c2; n   } n   inline bool operator<=(const HashString& other) const { n     return (*this == other) || (*this < other); n   } n   inline bool operator>(const HashString& other) const { n     return !(*this == other) && !(*this < other); n   } n   inline bool operator>=(const HashString& other) const { n     return !(*this < other); n   } n   inline bool operator!=(const HashString& other) const { n     return !(*this == other); n   } n   inline void reserve(int N) { n     hh.reserve(N); n     string::reserve(N); n   } n   vector<H> hh; n   static vector<H> pp, invpp; n  n  private: n   mutable H register_, registerValue_; n   static int haveN; n }; n template <> n int HashString<Hash>::haveN = 0; n template <> n vector<Hash> HashString<Hash>::pp = vector<Hash>(); n template <> n vector<Hash> HashString<Hash>::invpp = vector<Hash>(); n template <class H, bool cyclic = true> n struct HashStringPiece { n   explicit HashStringPiece(const HashString<H>& s, int pos, int len) n       : s(s), pos(pos), len(len), hash(H::INVALID) {} n   inline H getHash() const { n     if (hash == H::INVALID) hash = s.getHash(pos, len); n     return hash; n   } n   inline H getPartialHash(int needLen) const { return s.getHash(pos, needLen); } n   inline char getChar(int i) const { n     i += pos; n     const int S = s.size(); n     if (i < S) return s[i]; n     if (!cyclic) return 0; n     i -= S; n     if (i < S) return s[i]; n     return s[i % S]; n   } n   const HashString<H>& s; n   const int pos, len; n   mutable H hash; n }; n template <class H, class T> n char getHSPRangeChar(T beg, T en, int pos) { n   for (T it = beg; it != en; ++it) { n     if (pos < it->len) { n       return it->getChar(pos); n     } n     pos -= it->len; n   } n   return 0; n } n template <class H, class T> n H getHSPRangeHash(T beg, T en, int len) { n   H ans = H::ZERO; n   int cur = 0; n   for (T it = beg; it != en; ++it) { n     if (len >= it->len) { n       ans += it->getHash() * it->s.getPow(cur); n       cur += it->len; n       len -= it->len; n     } else { n       ans += it->getPartialHash(len) * it->s.getPow(cur); n       break; n     } n   } n   return ans; n } n int TB, IT; n template <class H, class T> n inline int compareHSPRanges(T beg1, T en1, int len1, T beg2, T en2, int len2) { n   if (beg1 == en1 && beg2 == en2) return 0; n   if (beg1 == en1) return -1; n   if (beg2 == en2) return 1; n   int l = 0, r = min(len1, len2) + 1; n   while (r - l > 1) { n     int xx = (r + l) / 2; n     auto h1 = getHSPRangeHash<H, T>(beg1, en1, xx); n     auto h2 = getHSPRangeHash<H, T>(beg2, en2, xx); n     if (h1 == h2) n       l = xx; n     else n       r = xx; n   } n   char c1 = getHSPRangeChar<H, T>(beg1, en1, l); n   char c2 = getHSPRangeChar<H, T>(beg2, en2, l); n   if (c1 < c2) n     return -1; n   else if (c1 == c2) n     return 0; n   else n     return 1; n } n template <class H, class T> n inline int compareHSPRanges(T beg1, T en1, T beg2, T en2) { n   int len1 = 0; n   for (T it = beg1; it != en1; ++it) { n     len1 += it->len; n   } n   int len2 = 0; n   for (T it = beg2; it != en2; ++it) { n     len2 += it->len; n   } n   return compareHSPRanges<H, T>(beg1, en1, len1, beg2, en2, len2); n } n int a, b, c, d, n, m, k; n char str[1000002]; n int bal[2000002]; n HashString<Hash> s; n inline string build(pair<pair<int, int>, int> a) { n   string ans =   ; n   for (int _n((a.first.first) - 1), i(0); i <= _n; i++) ans +=  ( ; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int cur = a.second + i; n     if (cur >= n) cur -= n; n     ans += str[cur]; n   } n   for (int _n((a.first.second) - 1), i(0); i <= _n; i++) ans +=  ) ; n   return ans; n } n HashString<Hash> op, cl; n inline bool better(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b, n                    const HashString<Hash>& s) { n   HashStringPiece<Hash> A[3] = {HashStringPiece<Hash>(op, 0, a.first.first), n                                 HashStringPiece<Hash>(s, a.second, n), n                                 HashStringPiece<Hash>(cl, 0, a.first.second)}; n   HashStringPiece<Hash> B[3] = {HashStringPiece<Hash>(op, 0, b.first.first), n                                 HashStringPiece<Hash>(s, b.second, n), n                                 HashStringPiece<Hash>(cl, 0, b.first.second)}; n   int t = compareHSPRanges<Hash>(A, A + 3, B, B + 3); n   if (t == -1) n     return 1; n   else n     return 0; n } n int main() { n   gets(str); n   n = (int)strlen(str); n   HashString<Hash> s(str); n   HashString<Hash>::precalc(2 * n + 1); n   op.reserve(n + 1); n   cl.reserve(n + 1); n   for (int _n((n)-1), i(0); i <= _n; i++) { n     op +=  ( ; n     cl +=  ) ; n   } n   c = 0; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     if (str[i] ==  ( ) n       ++c; n     else n       --c; n   } n   bal[0] = 0; n   for (int _n((2 * n) - 1), i(0); i <= _n; i++) { n     if (i) bal[i] = bal[i - 1]; n     int cur = i; n     if (i >= n) cur = i - n; n     if (str[cur] ==  ( ) n       ++bal[i]; n     else n       --bal[i]; n   } n   int blen = 1000000000; n   pair<pair<int, int>, int> best = make_pair(make_pair(0, 0), 0); n   stack<pair<int, int> > s1, s2; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int new_element = bal[i]; n     int minima = s1.empty() ? new_element : min(new_element, s1.top().second); n     s1.push(make_pair(new_element, minima)); n   } n   for (int _n((n)-1), cyc(0); cyc <= _n; cyc++) { n     int current_minimum = -1000000000; n     if (s1.empty() || s2.empty()) n       current_minimum = s1.empty() ? s2.top().second : s1.top().second; n     else n       current_minimum = min(s1.top().second, s2.top().second); n     int p = 0; n     if (cyc) p = bal[cyc - 1]; n     int mn = current_minimum - p; n     int beg = max(0, -mn); n     int en = c + beg; n     int len = beg + en + n; n     if (len < blen || n         (len == blen && better(make_pair(make_pair(beg, en), cyc), best, s))) { n       blen = len; n       best = make_pair(make_pair(beg, en), cyc); n     } n     if (s2.empty()) { n       while (!s1.empty()) { n         int element = s1.top().first; n         s1.pop(); n         int minima = s2.empty() ? element : min(element, s2.top().second); n         s2.push(make_pair(element, minima)); n       } n     } n     s2.pop(); n     int new_element = bal[cyc + n]; n     int minima = s1.empty() ? new_element : min(new_element, s1.top().second); n     s1.push(make_pair(new_element, minima)); n   } n   string ans = build(best); n   printf( %s n , ans.c_str()); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v); n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { n   return cout <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) { n   cout <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<  ,  ; n     cout << v[i]; n   } n   return cout <<  ] ; n } n template <typename A, typename B> n istream &operator>>(istream &cin, pair<A, B> &p) { n   cin >> p.first; n   return cin >> p.second; n } n mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); n void usaco(string filename) { n   freopen((filename +  .in ).c_str(),  r , stdin); n   freopen((filename +  .out ).c_str(),  w , stdout); n } n const long double pi = 3.14159265358979323846; n const long long mod = 1000000007; n struct dsu { n   int n; n   vector<int> sizes, marks; n   vector<vector<int>> graph; n   void init(int rn) { n     n = rn; n     sizes = vector<int>(n); n     fill(sizes.begin(), sizes.end(), 1); n     marks = vector<int>(n); n     for (int i = 0; i < n; i++) marks[i] = i; n     graph = vector<vector<int>>(n); n   } n   void mark(int m, int node) { n     if (marks[node] == m) return; n     marks[node] = m; n     for (int i : graph[node]) mark(m, i); n   } n   bool merge(int a, int b) { n     if (marks[a] == marks[b]) return 0; n     graph[a].push_back(b); n     graph[b].push_back(a); n     if (sizes[marks[a]] > sizes[marks[b]]) swap(a, b); n     sizes[marks[b]] += sizes[marks[a]]; n     sizes[marks[a]] = 0; n     mark(marks[b], a); n     return 1; n   } n   int find(int n) { return marks[n]; } n }; n long long n, m, k, q, l, r, x, y, z; n const long long template_array_size = 1e6 + 6884; n long long a[template_array_size]; n long long b[template_array_size]; n long long c[template_array_size]; n string second, t; n long long ans = 0; n pair<long long, pair<long long, long long>> vals[200005]; n long long root[100005]; n bool cnt[100005]; n dsu d; n void solve(int tc = 0) { n   cin >> m >> n; n   for (long long i = 0; i < m; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) cin >> b[i]; n   long long ans = 0, second = 0; n   for (long long i = 0; i < m; i++) { n     cin >> x; n     for (long long j = 0; j < x; j++) { n       cin >> y; n       --y; n       ans += a[i] + b[y]; n       vals[second++] = make_pair(-a[i] - b[y], make_pair(i, y)); n     } n   } n   sort(vals, vals + second); n   memset(cnt, 0, sizeof(cnt)); n   d.init(n); n   for (long long i = 0; i < second; i++) { n     long long a = vals[i].second.first, b = vals[i].second.second, n               val = vals[i].first; n     if (!cnt[a]) { n       cnt[a] = 1; n       root[a] = b; n       ans += val; n     } else { n       if (d.merge(root[a], b)) ans += val; n     } n   } n   cout << ans <<   n ; n } n int main() { n   { ios_base::sync_with_stdio(false); } n   { cin.tie(NULL); } n   cout << setprecision(12) << fixed; n   int tc = 1; n   for (int t = 0; t < tc; t++) solve(t); n } n


#include <bits/stdc++.h> n using namespace std; n FILE *in; n FILE *out; n const int MAX = 1048576; n const int TREE = 2097152; n const int MOD = 1000000007; n int n; n int a[MAX]; n int tree[TREE]; n bool seen[MAX]; n int query(int idx1, int idx2) { n   idx1 += (TREE >> 1); n   idx2 += (TREE >> 1); n   if (idx1 == idx2) return tree[idx1]; n   int ret = 0; n   ret += tree[idx1]; n   if (ret >= MOD) ret -= MOD; n   int flag1 = !(idx1 & 1); n   idx1 >>= 1; n   ret += tree[idx2]; n   if (ret >= MOD) ret -= MOD; n   int flag2 = (idx2 & 1); n   idx2 >>= 1; n   while (idx1 != idx2) { n     if (flag1) { n       ret += tree[(idx1 << 1) + 1]; n       if (ret >= MOD) ret -= MOD; n     } n     if (flag2) { n       ret += tree[(idx2 << 1) + 0]; n       if (ret >= MOD) ret -= MOD; n     } n     flag1 = !(idx1 & 1); n     idx1 >>= 1; n     flag2 = (idx2 & 1); n     idx2 >>= 1; n   } n   return ret; n } n void update(int idx, int val) { n   idx += (TREE >> 1); n   while (idx) { n     tree[idx] += val; n     if (tree[idx] >= MOD) tree[idx] -= MOD; n     idx >>= 1; n   } n } n int main(void) { n   in = stdin; n   out = stdout; n   fscanf(in,  %d , &n); n   for (int i = 0; i < n; i++) fscanf(in,  %d , &a[i]); n   int ans = 0; n   memset(tree, 0, sizeof(tree)); n   for (int i = n - 1; i >= 0; i--) { n     int cur = ((long long)a[i] * (query(a[i], MAX - 1) + 1)) % MOD; n     ans += cur; n     if (ans >= MOD) ans -= MOD; n     int sub = query(a[i], a[i]); n     ans -= sub; n     if (ans < 0) ans += MOD; n     update(a[i], (cur - sub + MOD) % MOD); n   } n   fprintf(out,  %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n ostream& operator<<(ostream& o, pair<A, B>& p) { n   return o <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& v) { n   o <<  { ; n   for (typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) n     o << *it <<  ,  ; n   return o <<  } ; n } n struct debugger { n   ostream& out; n   bool first; n   void start() { first = true; } n   debugger(ostream& out) : out(out) { start(); } n   void nl() { n     out << endl; n     start(); n   } n   template <class T> n   debugger& operator,(T& v) { n     if (first) n       first = false; n     else n       out <<  ,  ; n     out << v; n     return *this; n   } n   template <class T> n   debugger& operator,(pair<T*, int> arr) { n     out <<  { ; n     for (typeof((arr.second) - 1) i = (0); i <= ((arr.second) - 1); i++) { n       out << arr.first[i] <<  ,  ; n     } n     out <<  } ; n     return *this; n   } n } dbg(cerr); n long long pyra(long long n) { return (n + 1) * (n + 1); } n long long incr(long long n) { return (n * (n + 1)) / 2; } n long long f(long long x, long long y, long long step, long long n) { n   long long ret = 2 * pyra(step) - (2 * step + 1); n   if (x + step > n) { n     ret -= pyra(x + step - n - 1); n   } n   if (y + step > n) { n     ret -= pyra(y + step - n - 1); n   } n   if (x - step < 1) { n     ret -= pyra(1 - (x - step) - 1); n   } n   if (y - step < 1) { n     ret -= pyra(1 - (y - step) - 1); n   } n   if (step - 1 - (n - x) - (n - y) > 0) { n     ret += incr(step - 1 - (n - x) - (n - y)); n   } n   if (step - 1 - (n - x) - (y - 1) > 0) { n     ret += incr(step - 1 - (n - x) - (y - 1)); n   } n   if (step - 1 - (x - 1) - (y - 1) > 0) { n     ret += incr(step - 1 - (x - 1) - (y - 1)); n   } n   if (step - 1 - (x - 1) - (n - y) > 0) { n     ret += incr(step - 1 - (x - 1) - (n - y)); n   } n   return ret; n } n int main() { n   long long n, x, y, c, lo, hi; n   cin >> n >> x >> y >> c; n   lo = 0; n   hi = 1e9 + 10; n   {} n   while (lo < hi) { n     long long mid = (lo + hi) / 2; n     if (f(x, y, mid, n) < c) n       lo = mid + 1; n     else n       hi = mid; n   } n   cout << lo << endl; n   return 0; n } n


#include <bits/stdc++.h> n int minDig(uint64_t n) { n   int k = n % 10, min = k; n   n /= 10; n   while (n != 0) { n     k = n % 10; n     if (k <= min) min = k; n     n /= 10; n   } n   return min; n } n int maxDig(uint64_t n) { n   int k = n % 10, max = k; n   n /= 10; n   while (n != 0) { n     k = n % 10; n     if (k >= max) max = k; n     n /= 10; n   } n   return max; n } n int searchzero(uint64_t n) { n   while (n != 0) { n     if (n % 10 == 0) return 1; n     n /= 10; n   } n   return 0; n } n int main(void) { n   int t; n   uint64_t a, k; n   scanf( %d , &t); n   while (t--) { n     scanf( %llu %llu , &a, &k); n     for (uint64_t i = 1; i < k; i++) { n       a += (minDig(a) * maxDig(a)); n       if (searchzero(a) == 1) break; n     } n     printf( %llu n , a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   std::cerr << name <<   :   << arg1 << 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   std::cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n bool findfxn(string a, string b) { n   size_t found = a.find(b); n   if (found != string::npos) n     return 1; n   else n     return 0; n } n long long int power(long long int a, long long int b) { n   if (b == 0) { n     return 1; n   } else if (b % 2 == 0) { n     return power(a * a, b / 2); n   } else { n     return a * power(a * a, (b - 1) / 2); n   } n } n void solve() { n   long long int n, p, k, z = 0; n   cin >> n >> p >> k; n   int a[n]; n   int sum[n + 10]; n   for (int i = 0; i < (n); i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int i = 0; n   for (i = 0; i < n; i++) { n     if (i > 1) { n       sum[i] = sum[i - 2] + a[i]; n     } else n       sum[i] = a[i]; n     if (sum[i] > p) { n       break; n     } n   } n   cout << i << endl; n   return; n } n signed main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n   return 0; n } n


#include<bits/stdc++.h> n #define L(i, j, k) for(int i = (j), i##E = k; i <= i##E; i++)  n #define R(i, j, k) for(int i = (j), i##E = k; i >= i##E; i--) n #define mkp make_pair n #define pii pair<int, int> n #define ll long long n #define db double n #define sz(a) a.size() n #define x first n #define y second n #define vi vector<int> n using namespace std; n const int N = 1e4, M = 100, mod = 998244353; n int m; n struct Matrix { n  int f[M][M]; n  Matrix (int x = 1) { n   L(i, 1, m) L(j, 1, m) f[i][j] = (i == j ? x : 0); n  } n } ; n Matrix operator * (Matrix aa, Matrix bb) { n  Matrix res(0); n  L(i, 1, m) L(j, 1, m) L(k, 1, m) (res.f[i][j] += (ll) aa.f[i][k] * bb.f[k][j] % mod) %= mod; n  return res; n } n Matrix operator ^ (Matrix x, ll y) { n  Matrix res(1); n  for(; y; x = x * x, y >>= 1) if(y & 1) res = res * x; n  return res; n } n  n int n, nousex, otp, s[N], tot, v[N], sv[N], ns; n ll a[N], p[N], mx, val, arr[N], sl[N], sr[N]; n void calc(int l, int r) { n  ll mx = 0; n  p[l] = 0, tot = 0; n  L(i, l + 1, r) { n   sl[i] = (p[i - 1] + (a[i] < 0 ? -1 : 1)), p[i] = p[i - 1] + a[i], sr[i] = p[i]; n   if(sl[i] > sr[i]) swap(sl[i], sr[i]); n   mx = max(mx, p[i]);  n  } n  if(val != mx) return; n  sl[l + 1] = 0; n  L(i, l + 1, r)  n   arr[++tot] = sl[i] - 1, arr[++tot] = sl[i], arr[++tot] = sl[i] + 1,  n   arr[++tot] = sr[i] - 1, arr[++tot] = sr[i], arr[++tot] = sr[i] + 1; n  sort(arr + 1, arr + tot + 1), tot = unique(arr + 1, arr + tot + 1) - arr - 1; n  m = r - l; n  memset(v, 0, sizeof(v)); n  v[1] = 1, arr[0] = -1; n  L(k, 1, tot) if(arr[k] <= mx) { n   Matrix G(0); n   L(i, l + 1, r) if(sl[i] <= arr[k - 1] + 1 && arr[k] <= sr[i]) { n    L(j, l + 1, i - 1) G.f[j - l][i - l] = 1; n    if(a[i] > 0) G.f[i - l][i - l] = 1; n   } n   G = G ^ (arr[k] - arr[k - 1]); n   L(i, 1, m) L(j, 1, m) (sv[j] += (ll) v[i] * G.f[i][j] % mod) %= mod; n   L(i, 1, m) v[i] = sv[i], sv[i] = 0; n  } n  L(i, 1, m) (ns += v[i]) %= mod; n } n void Main() { n  cin >> n >> nousex; n  L(i, 1, n) { n   cin >> a[i]; n   if(a[i]) a[++otp] = a[i]; n  } n  n = otp, otp = 0; n  L(i, 1, n)  n   if((a[i] > 0 && a[i - 1] > 0) || (a[i] < 0 && a[i - 1] < 0)) a[otp] += a[i]; n   else a[++otp] = a[i]; n  n = otp; n  L(i, 1, n) mx += a[i], val = max(val, mx), mx = max(mx, 0ll); n  cout << val + 1 <<    ; n  if(val == 0) { n   int sum = 1; n   L(i, 1, n) (sum += abs(a[i]) % mod) %= mod; n   return cout << sum <<   n , void(); n  } n  int top = 0; n  mx = 0; n  L(i, 1, n) { n   mx += a[i]; n   if(mx < 0) s[++top] = i, mx = 0; n  } n  s[++top] = n + 1; n  L(i, 1, top) calc(s[i - 1], s[i] - 1); n  cout << ns <<   n ; n } n int main() { n  ios::sync_with_stdio(false); n  cin.tie(0); cout.tie(0); n  Main(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double x, y; n   Point() {} n   Point(double a, double b) { n     x = a; n     y = b; n   } n   Point operator+(Point who) { return {x + who.x, y + who.y}; } n   Point operator-(Point who) { return {x - who.x, y - who.y}; } n   Point operator/(double val) { return {x / val, y / val}; } n   Point operator*(double val) { return {x * val, y * val}; } n   bool operator==(const Point who) const { n     return max(abs(x - who.x), abs(y - who.y)) <= 1e-7; n   } n   bool operator<(const Point who) const { n     if (*this == who) return false; n     return make_pair(x, y) < make_pair(who.x, who.y); n   } n }; n struct Line { n   double a, b, c; n   Line(){}; n   Line(double _a, double _b, double _c) { n     a = _a; n     b = _b; n     c = _c; n   } n   Line(Point A, Point B) { n     a = B.y - A.y; n     b = A.x - B.x; n     c = -(A.x * a + B.x * b); n   } n   Line operator||(Point A) { return {a, b, -(a * A.x + b * A.y)}; } n   Line perp() { n     Line ans = {a, b, c}; n     swap(ans.a, ans.b); n     ans.a *= -1; n     return ans; n   } n   Point intersect(Line who) { n     double up_x = who.c * b - c * who.b; n     double up_y = c * who.a - a * who.c; n     double down = a * who.b - b * who.a; n     return {up_x / down, up_y / down}; n   } n   bool is_parallel(Line who) { return abs(a * who.b - b * who.a) <= 1e-7; } n }; n int n, i, j, ans; n Point P[2017], mid, Origin(0.00, 0.00); n bool used[2017]; n Line aux; n Point _P[2017], _mid; n map<Point, int> M; n vector<Line> ant; n void remove_sym() { n   int i, j; n   for (i = 1; i <= n; i++) { n     for (j = i; j <= n; j++) { n       if (used[j]) continue; n       if (P[i] + P[j] == mid * 2) { n         used[i] = used[j] = true; n         break; n       } n     } n   } n } n Point get_proj(Point A, Line L) { n   Line aux = (L.perp() || A); n   return aux.intersect(L); n } n bool check(Line act) { n   int i; n   int rm = 0; n   Point sym; n   M.clear(); n   _mid = get_proj(mid, act); n   for (i = 1; i <= n; i++) { n     _P[i] = get_proj(P[i], act); n     if (_P[i] == _mid) { n       rm++; n       continue; n     } n     sym = _mid * 2 - _P[i]; n     if (M[sym] > 0) { n       rm += 2; n       M[sym]--; n       continue; n     } n     M[_P[i]]++; n   } n   if (rm != n) return false; n   return true; n } n int main() { n   scanf( %d , &n); n   mid = {0, 0}; n   for (i = 1; i <= n; i++) { n     scanf( %lf%lf , &P[i].x, &P[i].y); n     mid = mid + P[i]; n   } n   mid = mid / n; n   remove_sym(); n   for (i = 1; used[i] && i <= n; i++) n     ; n   if (i == n + 1) { n     printf( -1 ); n     return 0; n   } n   for (j = i; j <= n; j++) { n     if (used[j]) continue; n     aux = Line(mid, (P[i] + P[j]) / 2); n     aux = (aux.perp() || Origin); n     bool ok = true; n     for (auto e : ant) { n       if (e.is_parallel(aux)) { n         ok = false; n         break; n       } n     } n     if (!ok) continue; n     ant.push_back(aux); n     if (check(aux)) ans++; n   } n   printf( %d , ans); 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 + 1) % 4) > 1) n     cout << 1 << endl; n   else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long nInf = -1000000000; n const long long pInf = 1000000000; n const long long mod = 1000000007; n const int MAXN = 3010; n void getdata(); n void preproc(); n void solve(); n vector<int> g[MAXN]; n bool gr[MAXN][MAXN]; n int n, m; n int main() { n   int t = 1; n   for (int i = 0; i < t; i++) { n     getdata(); n     preproc(); n     solve(); n   } n   return 0; n } n void getdata() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x--; n     y--; n     g[x].push_back(y); n     gr[x][y] = true; n   } n } n void preproc() {} n void solve() { n   long long res = 0; n   for (int a = 0; a < n; a++) { n     for (int c = 0; c < n; c++) { n       if (a == c) continue; n       int count = 0; n       for (int i = 0; i < (int)g[a].size(); i++) { n         int b = g[a][i]; n         if (gr[b][c]) { n           count++; n         } n       } n       res += count * (count - 1) / 2; n     } n   } n   printf( %I64d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T mod_v(T num) { n   if (num >= 0) n     return num % 1000000007; n   else n     return (num % 1000000007 + 1000000007) % 1000000007; n } n template <class T> n inline T gcd(T a, T b) { n   a = abs(a); n   b = abs(b); n   while (b) b ^= a ^= b ^= a %= b; n   return a; n } n template <class T> n T fast_pow(T n, T p) { n   if (p == 0) return 1; n   if (p % 2) { n     T g = mod_v(mod_v(n) * mod_v(fast_pow(n, p - 1))); n     return g; n   } else { n     T g = fast_pow(n, p / 2); n     g = mod_v(mod_v(g) * mod_v(g)); n     return g; n   } n } n template <class T> n inline T modInverse(T n) { n   return fast_pow(n, 1000000007 - 2); n } n bool equalTo(double a, double b) { n   if (fabs(a - b) <= 1e-9) n     return true; n   else n     return false; n } n bool notEqual(double a, double b) { n   if (fabs(a - b) > 1e-9) n     return true; n   else n     return false; n } n bool lessThan(double a, double b) { n   if (a + 1e-9 < b) n     return true; n   else n     return false; n } n bool lessThanEqual(double a, double b) { n   if (a < b + 1e-9) n     return true; n   else n     return false; n } n bool greaterThan(double a, double b) { n   if (a > b + 1e-9) n     return true; n   else n     return false; n } n bool greaterThanEqual(double a, double b) { n   if (a + 1e-9 > b) n     return true; n   else n     return false; n } n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n int nextInt() { n   int n; n   scanf( %d , &n); n   return n; n } n long long nextLong() { n   long long n; n   scanf( %lld , &n); n   return n; n } n void print(int n) { printf( %d , n); } n void println(int n) { printf( %d n , n); } n void println(long long n) { printf( %lld n , n); } n template <class T> n inline int in(register T& num) { n   register char c = 0; n   num = 0; n   bool n = false; n   while (c < 33) c = getchar(); n   while (c > 33) { n     if (c ==  - ) n       n = true; n     else n       num = num * 10 + c -  0 ; n     c = getchar(); n   } n   num = n ? -num : num; n   return 1; n } n string tshirt[] = { S ,  M ,  L ,  XL ,  XXL ,  XXXL }; n int cnt[10]; n string res[1000004]; n int main() { n   int var; n   int n; n   map<string, int> val; n   for (int i = 0; i < 6; i++) { n     in(var); n     val[tshirt[i]] = i; n     cnt[i] = var; n   } n   in(n); n   bool ok = true; n   vector<pair<int, pair<int, int> > > pos; n   for (int i = 1; i <= n; i++) { n     char s[10]; n     char* tmp; n     char v[] =  , ; n     scanf( %s , s); n     if (!ok) continue; n     tmp = strtok(s, v); n     int l = 100, r = -1; n     while (tmp != NULL) { n       l = min(l, val[tmp]); n       r = max(r, val[tmp]); n       tmp = strtok(NULL, v); n     } n     if (l == r) { n       if (cnt[l] == 0) n         ok = false; n       else { n         cnt[l]--; n         res[i] = tshirt[l]; n       } n     } else n       pos.push_back({l, {r, i}}); n   } n   sort((pos).begin(), (pos).end()); n   for (auto i : pos) { n     if (!ok) break; n     if (cnt[i.first]) { n       cnt[i.first]--; n       res[i.second.second] = tshirt[i.first]; n     } else if (cnt[i.second.first]) { n       cnt[i.second.first]--; n       res[i.second.second] = tshirt[i.second.first]; n     } else n       ok = false; n   } n   if (!ok) n     printf( NO n ); n   else { n     printf( YES n ); n     for (int i = 1; i <= n; i++) cout << res[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char matr[1010][1010]; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, -1, 0, 1}; n bool Field(int x, int y) { n   if (x < 1 || x > n) return false; n   if (y < 1 || y > m) return false; n   return true; n } n int color[1010][1010]; n int ans[1010][1010]; n int f[10]; n int H; n void Solve() { n   int i, j, o; n   int x2, y2; n   for (j = 1; j <= m; j++) { n     for (i = 1; i <= n; i++) { n       if (color[j][i]) continue; n       if (matr[j][i] ==  . ) { n         for (o = 0; o <= 3; o++) { n           x2 = i + dx[o]; n           y2 = j + dy[o]; n           if (!Field(x2, y2)) continue; n           if (color[y2][x2]) continue; n           if (matr[y2][x2] !=  . ) continue; n           color[j][i] = 1; n           color[y2][x2] = 1; n           memset(f, 0, sizeof(f)); n           ; n           ans[j][i] = H; n           ans[y2][x2] = H; n           H++; n           color[j][i] = 1; n           color[y2][x2] = 1; n           break; n         } n       } n     } n   } n } n int C; n int dfs(int x, int y) { n   int o; n   int x2, y2; n   int res; n   res = 1; n   color[y][x] = C; n   for (o = 0; o <= 3; o++) { n     x2 = x + dx[o]; n     y2 = y + dy[o]; n     if (!Field(x2, y2)) continue; n     if (color[y2][x2] == C) continue; n     if (ans[y2][x2] != ans[y][x]) continue; n     if (matr[y2][x2] !=  . ) continue; n     res += dfs(x2, y2); n   } n   return res; n } n void Solve2() { n   int i, j, o; n   int x2, y2; n   memset(color, 0, sizeof(color)); n   ; n   int flag; n   for (j = 1; j <= m; j++) { n     for (i = 1; i <= n; i++) { n       if (matr[j][i] ==  .  && ans[j][i] == -1) { n         flag = 0; n         for (o = 0; o <= 3; o++) { n           x2 = i + dx[o]; n           y2 = j + dy[o]; n           if (!Field(x2, y2)) continue; n           if (ans[y2][x2] == -1) continue; n           C++; n           if (dfs(x2, y2) < 5) { n             ans[j][i] = ans[y2][x2]; n             flag = 1; n             break; n           } n         } n         if (!flag) { n           printf( -1 n ); n           exit(0); n         } n       } n     } n   } n } n vector<pair<int, int> > t; n void dfs2(int x, int y) { n   int o; n   int x2, y2; n   color[y][x] = C; n   t.push_back(make_pair(x, y)); n   for (o = 0; o <= 3; o++) { n     x2 = x + dx[o]; n     y2 = y + dy[o]; n     if (!Field(x2, y2)) continue; n     if (color[y2][x2] == C) continue; n     if (matr[y2][x2] !=  . ) continue; n     if (ans[y2][x2] >= 0 && ans[y2][x2] <= 9) { n       f[ans[y2][x2]] = 1; n     } n     if (ans[y2][x2] != ans[y][x]) continue; n     dfs2(x2, y2); n   } n } n void Solve3() { n   int i, j, o; n   int k; n   for (j = 1; j <= m; j++) { n     for (i = 1; i <= n; i++) { n       if (ans[j][i] >= 10) { n         memset(f, 0, sizeof(f)); n         ; n         C++; n         t.clear(); n         dfs2(i, j); n         k = 0; n         for (o = 0; o <= 9; o++) { n           if (!f[o]) { n             k = o; n             break; n           } n         } n         for (o = 0; o <= (int)t.size() - 1; o++) { n           ans[t[o].second][t[o].first] = k; n         } n       } n     } n   } n } n int main() { n   int i, j; n   scanf( %d %d , &m, &n); n   for (j = 1; j <= m; j++) { n     scanf( %s , matr[j] + 1); n   } n   memset(ans, -1, sizeof(ans)); n   Solve(); n   Solve2(); n   Solve3(); n   for (j = 1; j <= m; j++) { n     for (i = 1; i <= n; i++) { n       if (matr[j][i] ==  . ) { n         printf( %d , ans[j][i]); n       } else { n         printf( %c , matr[j][i]); n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100, mod = 1e9 + 7; n long long a[N], k, n; n struct mat { n   int d[N][N]; n   mat() { memset(d, 0, sizeof d); } n   mat operator*(const mat& o) { n     mat p; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         for (int k = 0; k < n; k++) { n           p.d[i][j] = (p.d[i][j] + 1ll * d[i][k] * o.d[k][j]) % mod; n         } n       } n     } n     return p; n   } n   mat pw(long long k) { n     mat result; n     for (int i = 0; i < n; i++) result.d[i][i] = 1; n     mat s = *this; n     while (k) { n       if (k & 1) { n         result = result * s; n       } n       s = s * s; n       k >>= 1; n     } n     return result; n   } n }; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   mat adj; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       long long x = a[i] ^ a[j]; n       int cnt = 0; n       while (x) { n         if (x & 1) cnt++; n         x >>= 1; n       } n       if (cnt % 3 == 0) adj.d[i][j] = 1; n     } n   } n   mat r = adj.pw(k - 1); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       ans = (ans + r.d[i][j]) % mod; n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int mn[1111]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < m; i++) { n     mn[i] = 1e9; n   } n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     --x; n     mn[x] = min(mn[x], y); n   } n   int s = 0; n   for (int i = 0; i < m; i++) { n     s += mn[i]; n   } n   cout << min(s, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long int MOD = 1e9 + 7; n const int N = 1e6 + 6; n int sum[N][3], prv[N][3]; n int main() { n   int n; n   string s; n   cin >> n >> s; n   s =  *  + s; n   prv[0][0] = prv[0][1] = -1; n   for (int i = 1; i <= n; i++) { n     sum[i][0] = sum[i - 1][0]; n     sum[i][1] = sum[i - 1][1]; n     prv[i][0] = prv[i - 1][0]; n     prv[i][1] = prv[i - 1][1]; n     if (s[i] ==  1 ) { n       sum[i][1]++; n       prv[i][1] = i; n     } n     if (s[i] ==  0 ) { n       sum[i][0]++; n       prv[i][0] = i; n     } n   } n   for (int len = 1; len <= n; len++) { n     int now = 1; n     int cnt = 0; n     while (now + len - 1 <= n) { n       if (sum[now + len - 1][1] - sum[now - 1][1] == 0 || n           sum[now + len - 1][0] - sum[now - 1][0] == 0) { n         cnt++; n         now += len; n         continue; n       } n       now = min(prv[now + len - 1][0], prv[now + len - 1][1]) + 1; n     } n     cout << cnt <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > anom; n long long int fact[200005]; n long long int invfact[200005]; n long long int MOD = 1000000007; n long long int dp[2005][25]; n long long int invmod(long long int i) { n   long long int ans = 1; n   long long int mult = i; n   long long int exp = MOD - 2; n   while (exp > 0) { n     if (exp % 2 == 1) { n       ans = (ans * mult) % MOD; n     } n     mult = (mult * mult) % MOD; n     exp /= 2; n   } n   return ans; n } n void precomp() { n   fact[0] = 1; n   invfact[0] = 1; n   for (long long int i = 1; i < 200005; i++) { n     fact[i] = (fact[i - 1] * i) % MOD; n     invfact[i] = (invfact[i - 1] * invmod(i)) % MOD; n   } n } n long long int comb(int a, int b) { n   long long int ans = (fact[a] * invfact[b]) % MOD; n   ans = (ans * invfact[a - b]) % MOD; n   return ans; n } n long long int paths(int a, int b, int c, int d) { n   if (a <= c && b <= d) { n     return comb(c - a + d - b, c - a); n   } n   return 0; n } n int main() { n   precomp(); n   int n, m, k, s; n   cin >> n >> m >> k >> s; n   if (k == 0) { n     cout << s << endl; n     return 0; n   } n   int limred = ceil(log2(s)) + 1; n   int a, b; n   for (int i = 0; i < k; i++) { n     cin >> a >> b; n     anom.push_back({a, b}); n   } n   sort(anom.begin(), anom.end()); n   bool beginanom = true; n   if (k > 0 && anom[k] != make_pair(1, 1)) { n     anom.push_back({1, 1}); n     beginanom = false; n     k++; n   } n   sort(anom.begin(), anom.end()); n   for (int i = k - 1; i >= 0; i--) { n     dp[i][0] = paths(anom[i].first, anom[i].second, n, m); n     for (int j = i + 1; j < k; j++) { n       dp[i][0] = (dp[i][0] - n                   (dp[j][0] * paths(anom[i].first, anom[i].second, n                                     anom[j].first, anom[j].second)) % n                       MOD + n                   MOD) % n                  MOD; n     } n   } n   for (int i = 1; i <= limred; i++) { n     for (int j = k - 1; j >= 0; j--) { n       dp[j][i] = paths(anom[j].first, anom[j].second, n, m); n       for (int p = j + 1; p < k; p++) { n         dp[j][i] = (dp[j][i] - n                     (dp[p][i] * paths(anom[j].first, anom[j].second, n                                       anom[p].first, anom[p].second)) % n                         MOD + n                     MOD) % n                    MOD; n       } n       for (int p = 0; p < i; p++) { n         dp[j][i] = (dp[j][i] - dp[j][p] + MOD) % MOD; n       } n     } n   } n   int prevs; n   long long int expsum = 0; n   long long int numways = 0; n   for (int i = 0; i <= limred; i++) { n     prevs = s; n     s = ceil(s / 2.0); n     if (beginanom) { n       expsum = (expsum + (s * dp[0][i]) % MOD) % MOD; n     } else { n       expsum = (expsum + (prevs * dp[0][i]) % MOD) % MOD; n     } n     numways = (numways + dp[0][i]) % MOD; n   } n   long long int leftways = paths(1, 1, n, m); n   leftways = (leftways - numways + MOD) % MOD; n   expsum = (expsum + leftways) % MOD; n   long long int ans = (expsum * invmod(comb(n + m - 2, n - 1))) % MOD; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 150; n const int M = 6e4 + 5; n int n, m; n struct Node { n   int a, b; n } s[N]; n bool cmp(Node a, Node b) { n   if (a.b > 0 && b.b < 0) return 1; n   if (a.b < 0 && b.b > 0) return 0; n   if (a.b > 0 && b.b > 0) return a.a < b.a; n   return a.a + a.b > b.a + b.b; n } n int dp[N][M]; n int main() { n   cin >> n >> m; n   memset(dp, 0, sizeof dp); n   for (int i = 1; i <= n; i++) cin >> s[i].a >> s[i].b; n   sort(s + 1, s + 1 + n, cmp); n   int ans = 0; n   dp[0][m] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < M; j++) { n       dp[i][j] = max(dp[i][j], dp[i - 1][j]); n       if (j - s[i].b >= 0 && j - s[i].b < M && dp[i - 1][j - s[i].b] && n           j - s[i].b >= s[i].a) n         dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - s[i].b] + 1); n       ans = max(ans, dp[i][j]); n     } n   } n   cout << ans - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans, k, cnt1[200010], cnt2[200010][10]; n vector<long long> adj[200010]; n long long sub(long long a, long long b) { return (a - b + k) % k; } n void dfs(long long st, long long par, long long depth) { n   cnt2[st][depth % k] = 1; n   cnt1[st] = 1; n   for (int l = 0; l < adj[st].size(); l++) { n     long long to = adj[st][l]; n     if (to != par) { n       dfs(to, st, depth + 1); n       for (int i = 0; i < k; i++) { n         for (int j = 0; j < k; j++) { n           long long rem = sub(i + j, 2 * depth); n           long long need = sub(k, rem); n           ans += need * cnt2[st][i] * cnt2[to][j]; n         } n       } n       for (int i = 0; i < k; i++) { n         cnt2[st][i] += cnt2[to][i]; n       } n       cnt1[st] += cnt1[to]; n     } n   } n   long long sv = cnt1[st]; n   ans += sv * (n - sv); n } n int main() { n   ans = 0; n   memset(cnt1, 0, sizeof(cnt1)); n   ; n   memset(cnt2, 0, sizeof(cnt2)); n   ; n   cin >> n >> k; n   for (int i = 1; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs(1, -1, 0); n   cout << ans / k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 233; n struct item { n   int ini, num; n } t[MAXN]; n int vis1[MAXN] = {0}, vis2[MAXN] = {0}; n int cg[MAXN] = {0}; n int n; n bool cmp1(const item &a, const item &b) { return a.num < b.num; } n bool cmp2(const item &a, const item &b) { return a.ini < b.ini; } n int main() { n   int cnt = 1; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &t[i].num); n     t[i].ini = i; n     if (vis1[t[i].num] == 0 && t[i].num <= n) { n       vis2[i] = 1; n     } n     vis1[t[i].num] = 1; n   } n   sort(t + 1, t + 1 + n, cmp1); n   for (int i = 1; i <= n; i++) { n     while (vis1[cnt] != 0) cnt++; n     if (vis2[t[i].ini] == 0) { n       t[i].num = cnt; n       cnt++; n     } n   } n   sort(t + 1, t + 1 + n, cmp2); n   for (int i = 1; i <= n; i++) { n     printf( %d  , t[i].num); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[3], ans, a, b, c, ma, t; n multiset<int> s; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < 3; i++) scanf( %d , arr + i); n   for (int i = 0; i < n; i++) { n     scanf( %d , &t); n     s.insert(t); n     ma = max(t, ma); n   } n   sort(arr, arr + 3); n   a = arr[0]; n   b = arr[1]; n   c = arr[2]; n   if (a + b + c < ma) return 0 * printf( -1 n ); n   multiset<int>::iterator now, it = s.upper_bound(b + c); n   while (it != s.end()) s.erase(it++), ans++; n   it = s.upper_bound(a + c); n   while (it != s.end()) { n     ans++; n     s.erase(it++); n     now = s.upper_bound(a); n     now--; n     if (now != s.end()) s.erase(now); n   } n   it = s.upper_bound(max(a + b, c)); n   while (it != s.end()) { n     ans++; n     s.erase(it++); n     now = s.upper_bound(b); n     now--; n     if (now != s.end()) s.erase(now); n   } n   if (a + b > c) { n     now = it = s.upper_bound(c); n     now--; n     while (it != s.end()) { n       ans++; n       s.erase(it++); n       if (now != s.end()) s.erase(now--); n     } n   } n   while (!s.empty()) { n     ans++; n     it = s.end(); n     it--; n     s.erase(it); n     if (s.empty()) break; n     now = s.upper_bound(b); n     now--; n     if (now != s.end() && b >= *now) { n       s.erase(now); n       if (s.empty()) break; n       now = s.upper_bound(a); n       now--; n       if (now != s.end() && a >= *now) s.erase(now); n     } else { n       now = s.upper_bound(a + b); n       now--; n       if (now != s.end() && a + b >= *now) s.erase(now); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     set<char> res; n     vector<char> ans; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] == s[i + 1]) { n         i++; n       } else { n         char c = s[i]; n         res.insert(c); n       } n     } n     for (auto i : res) cout << i; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x[1010], y[1010], cnt, _cnt; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]); n   while (true) { n     int cnt = 0; n     for (int i = 1; i <= n; i++) n       if ((x[i] + y[i]) & 1 == 1) cnt++; n     if (cnt >= 1 && cnt <= n - 1) { n       cout << cnt << endl; n       for (int i = 1; i <= n; i++) n         if ((x[i] + y[i]) & 1 == 1) printf( %d  , i); n       return 0; n     } n     if (cnt == n) { n       for (int i = 1; i <= n; i++) x[i]--; n     } n     for (int i = 1; i <= n; i++) { n       cnt = (x[i] - y[i]) / 2; n       _cnt = (x[i] + y[i]) / 2; n       x[i] = cnt; n       y[i] = _cnt; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 123; n const long double EPS = 1e-9; n const int MX = 1e9 + 1; n const int inf = 1e9 + 123; n const int MOD = 1e9 + 7; n const int N = 1e5 + 123; n const int dx[] = {0, 0, 1, -1}; n const int dy[] = {1, -1, 0, 0}; n int n, m; n long long dp[1001], a[13], b[13], c[13], d[13]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> c[0] >> d[0]; n   for (int i = 1; i <= m; ++i) { n     cin >> a[i] >> b[i] >> c[i] >> d[i]; n   } n   for (int i = 1; i <= m; ++i) { n     for (int l = n; l >= 0; --l) { n       for (int j = 1; j <= a[i] / b[i]; ++j) { n         if (l + j * c[i] > n) { n           continue; n         } n         dp[l + j * c[i]] = max(dp[l + j * c[i]], dp[l] + j * d[i]); n       } n     } n   } n   for (int l = n; l >= 0; --l) { n     for (int j = 1; j <= n / c[0]; ++j) { n       if (l + j * c[0] > n) { n         continue; n       } n       dp[l + j * c[0]] = max(dp[l + j * c[0]], dp[l] + d[0] * j); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans = max(ans, dp[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n const double PI = acos(-1.0); n const int EPS = (1e-10); n const int N = 1e5 * 2 + 10; n const int inf = 1e9 * 2 + 10; n const ll INF = 1e18 * 2 + 10; n const int MOD = 1e9 + 7; n int nxt() { n   int x; n   cin >> x; n   return x; n } n int fcmp(double a, double b) { n   return (fabs(a - b) < EPS ? 0 : a > b ? 1 : -1); n } n int n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   int mx = 0, p = 0; n   for (int i = (1); i <= (n); ++i) { n     int k, a; n     cin >> k >> a; n     mx = max(mx, k); n     ll s = 1; n     int m = 0; n     while (s < a) { n       s *= 4; n       m++; n     } n     p = max(p, k + m); n   } n   if (mx == p) p++; n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1001; n int i, j, n, ans; n int a[N], f[N][300], d[20], sum[N][10]; n void init() { n   d[1] = 1; n   for (i = 2; i <= 9; i++) d[i] = d[i - 1] * 2; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= 8; j++) sum[i][j] = sum[i - 1][j]; n     sum[i][a[i]]++; n   } n } n void mark(int st, int k, int len, int v, int s) { n   for (i = st; i <= n; i++) { n     int t = sum[i][k] - sum[st][k]; n     if (t > len) return; n     if (t == len) n       if (f[i][s] == -1 || f[i][s] < v) f[i][s] = v; n   } n } n int cal(int len) { n   int ans = -1; n   memset(f, 255, sizeof(f)); n   f[0][0] = 0; n   int i, j, k; n   for (i = 0; i <= n; i++) n     for (j = 0; j <= d[9] - 1; j++) n       if (f[i][j] != -1) { n         if (j == d[9] - 1 && f[i][j] > ans) ans = f[i][j]; n         for (k = 1; k <= 8; k++) n           if ((d[k] & j) == 0) { n             mark(i, k, len, f[i][j] + len, d[k] + j); n             mark(i, k, len + 1, f[i][j] + len + 1, d[k] + j); n           } n       } n   return ans; n } n void work() { n   int l = 0, r = n, mid, ans = 0, t; n   while (l <= r) { n     mid = (l + r) / 2; n     t = cal(mid); n     if (t != -1) n       l = mid + 1, ans = t; n     else n       r = mid - 1; n   } n   printf( %d n , ans); n } n int main() { n   init(); n   work(); n   return 0; n } n