#include <bits/stdc++.h> n using namespace std; n int n, k, s, a[5][205]; n struct node { n   int id, x, y; n }; n vector<node> ans; n inline void park() { n   for (int i = 0; i <= 2 * n - 1; ++i) n     if (a[1][i] && a[1][i] == a[0][i]) { n       ans.push_back(node{a[1][i], i / n ? 4 : 1, i % n + 1}); n       a[1][i] = 0; n       k--; n     } n } n inline bool judge() { n   park(); n   for (int i = 0; i <= 2 * n - 1; ++i) n     if (!a[1][i]) return 1; n   return 0; n } n inline void turn() { n   vector<node> t1, t2; n   int t = a[1][2 * n - 1]; n   for (int i = 2 * n - 1; i >= 1; --i) { n     a[1][i] = a[1][i - 1]; n     if (a[1][i]) { n       if (i > s) n         t1.push_back(node{a[1][i], i / n ? 3 : 2, i % n + 1}); n       else n         t2.push_back(node{a[1][i], i / n ? 3 : 2, i % n + 1}); n     } n   } n   a[1][0] = t; n   if (t) t2.push_back(node{t, 2, 1}); n   int sz1 = t1.size(), sz2 = t2.size(); n   for (int i = 0; i <= sz2 - 1; ++i) ans.push_back(t2[i]); n   for (int i = 0; i <= sz1 - 1; ++i) ans.push_back(t1[i]); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i <= n - 1; ++i) scanf( %d , &a[0][i]); n   for (int i = 0; i <= n - 1; ++i) scanf( %d , &a[1][i]); n   for (int i = 2 * n - 1; i >= n; --i) scanf( %d , &a[1][i]); n   for (int i = 2 * n - 1; i >= n; --i) scanf( %d , &a[0][i]); n   if (!judge()) return puts( -1 ), 0; n   for (int i = 0; i <= 2 * n - 1; ++i) n     if (!a[1][i]) { n       s = i; n       break; n     } n   while (k) { n     turn(); n     park(); n     s = (s + 1) % (2 * n); n   } n   int sz = ans.size(); n   printf( %d n , sz); n   for (int i = 0; i < sz; ++i) { n     if (ans[i].x == 3 || ans[i].x == 4) ans[i].y = n - ans[i].y + 1; n     printf( %d %d %d n , ans[i].id, ans[i].x, ans[i].y); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define int long long n using namespace std; n signed main(){ n     int t; n     cin >> t; n     while(t--){ n         int k, n, m; n         cin >> k >> n >> m; n         int a[n + 1], b[m + 1]; n         for(int i = 0;i < n;i++){ n             cin >> a[i]; n         } n         for(int i = 0;i < m;i++){ n             cin >> b[i]; n         } n         a[n] = 0x3f3f3f3f, b[m] = 0x3f3f3f3f; n         vector<int> ans; //答案序列 n         int l = k, ai = 0, bi = 0; //ai，bi分别表示两个数组分别删到了哪里，l为行数 n         bool flag = false; n         while(ans.size() < n + m){ n             if(a[ai] == 0){ n                 ai++, ans.push_back(0); n                 l++; n                 continue; n             } n             else if(b[bi] == 0){ n                 bi++, ans.push_back(0); n                 l++; n                 continue; n             } n             int p = min(a[ai], b[bi]); n             if(p == a[ai]){ n                 ai++; n             } n             else{ n                 bi++; n             } n             if(p > l){ n                 flag = true; n                 break; n             } n             ans.push_back(p); n         } n         if(flag){ n             cout << -1 << endl; n         } n         else{ n             for(int i = 0;i < ans.size();i++){ n                 cout << ans[i] <<    ; //输出答案序列 n             } n             cout << endl; n         } n     } n     return 0; n }


#include <bits/stdc++.h> n const int M = 2000; n int T, n, x, ans, f0[M + 10], f1[M + 10]; n inline int min(int x, int y) { return x < y ? x : y; } n inline int max(int x, int y) { return x > y ? x : y; } n void work() { n   for (int i = 0; i <= M; ++i) f0[i] = i; n   scanf( %d , &n), ans = 114514; n   while (n--) { n     scanf( %d , &x), memset(f1, 0x3f, sizeof f1); n     for (int i = 0; i <= M; ++i) n       i - x >= 0 && (f1[i] = min(f1[i], max(f0[i - x], i))), n           i + x <= M && (f1[i] = min(f1[i], f0[i + x])); n     memcpy(f0, f1, sizeof f0); n   } n   for (int i = 0; i <= M; ++i) ans = min(ans, f0[i]); n   printf( %d n , ans); n } n int main() { n   scanf( %d , &T); n   while (T--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 332748118, M = 998244353, N = 1e6 + 5; n int l[N]; n int ksm(int a, long long b) { n   int an = 1; n   while (b) { n     if (b & 1) an = 1ll * an * a % M; n     a = 1ll * a * a % M; n     b >>= 1; n   } n   return an; n } n int main() { n   int n, c = 1, a = 0, b = 0, f = 1, s, d = 1, e, g, h; n   scanf( %d , &n); n   e = s = ksm(L, n); n   h = g = ksm(3, 1ll * n * n); n   l[1] = 1; n   for (int i = 1; i <= n; i++) { n     if (i != 1) l[i] = (M - 1ll * M / i * l[M % i]) % M; n     c = 1ll * (n - i + 1) * l[i] % M * c % M; n     d = 1ll * d * s % M; n     e = 3ll * e % M; n     h = 3ll * h * s % M; n     a = (a + 1ll * f * c % M * h % M) % M; n     b = (b + 1ll * f * c * d % M * (ksm(1 - e, n) - 1) % M) % M; n     f = -f; n   } n   printf( %lld , (((a << 1) + 3ll * g * b) % M + M) % M); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, d; n   cin >> n >> k >> d; n   vector<int> U(k, 0); n   for (int i = 0; i < k; i++) U[i] = i + 1; n   vector<int> V(d - 1, 0); n   for (int i = 0; i < d - 1; i++) V[i] = i + 1; n   vector<long long> dp2(n + 1, 0); n   vector<long long> dp1(n + 1, 0); n   dp2[0] = 1; n   dp1[0] = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < U.size(); j++) { n       if (i + U[j] <= n) n         dp2[i + U[j]] = (dp2[i + U[j]] % (long long)1000000007 + n                          dp2[i] % (long long)1000000007) % n                         (long long)1000000007; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < V.size(); j++) { n       if (i + V[j] <= n) n         dp1[i + V[j]] = (dp1[i + V[j]] % (long long)1000000007 + n                          dp1[i] % (long long)1000000007) % n                         (long long)1000000007; n     } n   } n   cout << (dp2[n] - dp1[n] + (long long)1000000007) % (long long)1000000007 n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct triple { n   int x, y, z; n }; n int n, x1, y11, x2, y22; n string s[9]; n int main() { n   cin >> n; n   while (n--) { n     for (__typeof(8) i = 1; i <= 8; i++) cin >> s[i], s[i] =  #  + s[i]; n     bool tmp = false; n     for (__typeof(8) i = 1; i <= 8; i++) { n       for (__typeof(8) j = 1; j <= 8; j++) n         if (s[i][j] ==  K ) { n           if (tmp == false) { n             tmp = true; n             x1 = i; n             y11 = j; n           } else { n             x2 = i; n             y22 = j; n           } n         } n     } n     tmp = false; n     for (__typeof(8) i = 1; i <= 8; i++) n       for (__typeof(8) j = 1; j <= 8; j++) n         if (tmp == false && s[i][j] !=  # ) { n           int tx1 = abs(i - x1), ty1 = abs(j - y11), tx2 = abs(i - x2), n               ty2 = abs(j - y22); n           if (tx1 % 2 == 0 && ty1 % 2 == 0 && tx2 % 2 == 0 && ty2 % 2 == 0 && n               (tx1 / 2 + ty1 / 2) % 2 == 0 && (tx2 / 2 + ty2 / 2) % 2 == 0 && n               (max(tx1 / 2, ty1 / 2) + max(tx2 / 2, ty2 / 2)) % 2 == 0) { n             cout <<  YES n ; n             tmp = true; n           } n         } n     if (tmp == false) cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, b; n int t[200005], d[200005]; n queue<pair<int, pair<int, int> > > Q; n long long ans[200005]; n int main() { n   int i, j; n   scanf( %d , &n); n   scanf( %d , &b); n   for (i = 1; i <= n; i++) { n     scanf( %d , &t[i]); n     scanf( %d , &d[i]); n   } n   memset(ans, -1, sizeof(ans)); n   long long curr = 0; n   i = 1; n   while (1) { n     if (i <= n && Q.size() == 0) { n       Q.push({i, {t[i], d[i]}}); n       i++; n     } n     if (Q.size() == 0) break; n     pair<int, pair<int, int> > now = Q.front(); n     Q.pop(); n     curr = max(curr, (long long)now.second.first); n     long long en = curr + now.second.second; n     ans[now.first] = en; n     while (i <= n && Q.size() < b) { n       Q.push({i, {t[i], d[i]}}); n       i++; n     } n     while (i <= n && t[i] < en) { n       i++; n     } n     curr = en; n   } n   for (i = 1; i <= n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int SIZE = (int)1e6; n int n, k, from, to; n vector<int> g[SIZE], h[SIZE]; n vector<int> ord; n vector<bool> check; n string ans; n int component[SIZE]; n int comp; n void dfs1(int v) { n   check[v] = true; n   for (int i : g[v]) { n     if (!check[i]) { n       dfs1(i); n     } n   } n   ord.push_back(v); n } n void dfs2(int v) { n   check[v] = true; n   component[v] = comp; n   for (int i : h[v]) { n     if (!check[i]) { n       dfs2(i); n     } n   } n } n int main() { n   cin >> n >> k; n   for (int j = 0; j < 2; j++) { n     cin >> from; n     from--; n     for (int i = 1; i < n; i++) { n       cin >> to; n       to--; n       g[from].push_back(to); n       h[to].push_back(from); n       from = to; n     } n   } n   check.resize(n, false); n   for (int i = 0; i < n; i++) { n     if (!check[i]) { n       dfs1(i); n     } n   } n   for (int i = 0; i < n; i++) { n     check[i] = false; n   } n   for (int i = ord.size() - 1; i >= 0; i--) { n     if (!check[ord[i]]) { n       dfs2(ord[i]); n       comp++; n     } n   } n   if (comp < k) { n     cout <<  NO ; n     return 0; n   } n   ans.resize(n); n   cout <<  YES  << endl; n   for (int i = 0; i < ans.size(); i++) { n     if (k - 1 < component[i]) { n       ans[i] = char( a  + k - 1); n     } else { n       ans[i] = char( a  + component[i]); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p, k, ans, cnt, num[1000005], check[1000005], a, now, fac[100005]; n int main() { n   fac[0] = fac[1] = 1; n   ans = 1; n   scanf( %lld%lld , &p, &k); n   if (k == 0) { n     for (int i = 1; i < p; i++) { n       ans *= p; n       ans %= 1000000007; n     } n     printf( %lld , ans); n     return 0; n   } n   if (k == 1) { n     for (int i = 0; i < p; i++) { n       ans *= p; n       ans %= 1000000007; n     } n     printf( %lld , ans); n     return 0; n   } n   check[0] = 1; n   for (int i = 1; i < p; i++) { n     a = (k * i) % p; n     num[i] = a; n   } n   for (int i = 1; i < p; i++) { n     if (!check[i]) { n       now = i; n       while (!check[now]) { n         check[now] = 1; n         now = num[now]; n         cnt++; n       } n     } n     if (cnt > 1) ans *= p; n     cnt = 0; n     ans %= 1000000007; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long tmp = sqrt(n); n     vector<long long> ans; n     ans.push_back(0); n     for (long long i = 1; i <= tmp; i++) { n       ans.push_back(i); n       if (n / i != i) { n         ans.push_back(n / i); n       } n     } n     sort(ans.begin(), ans.end()); n     cout << ans.size() << endl; n     for (long long v : ans) cout << v <<    ; n     cout << endl; n   } n } n


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


#include<iostream> n #include<cmath> n using namespace std; n  n long long l,r,d; n  n int main() n { n  int t; n  cin >> t; n  while(t--) n  { n   scanf( %lld %lld %lld ,&l,&r,&d); n   if(min(l,r) * d >= max(l,r) - min(l,r)) cout <<  YES  << endl; n   else cout <<  NO  << endl; n  } n  return 0;  n }  n


#include <bits/stdc++.h> n using namespace std; n char st[100000 + 123]; n bool inm[256]; n int main() { n   scanf( %s , st); n   memset(inm, 0, sizeof(inm)); n   inm[ A ] = true; n   inm[ H ] = true; n   inm[ I ] = true; n   inm[ M ] = true; n   inm[ O ] = true; n   inm[ T ] = true; n   inm[ U ] = true; n   inm[ V ] = true; n   inm[ W ] = true; n   inm[ X ] = true; n   inm[ Y ] = true; n   int r = strlen(st) - 1; n   int l = 0; n   bool flag = true; n   while (l <= r) { n     if (st[l] != st[r] || !inm[st[l]]) { n       flag = false; n       break; n     } n     l++; n     r--; n   } n   if (flag) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n long long niz[300005]; n long long ima[605][605]; n long long res[300005]; n long long dp[300005]; n struct strukt { n   int a, b, ind; n } query[300005]; n bool cmp(strukt a, strukt b) { return a.b < b.b; } n const int k = 600; n int main() { n   ios_base::sync_with_stdio(false); n   cout.precision(10); n   cout << fixed; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> niz[i]; n   int q; n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> query[i].a >> query[i].b; n     query[i].ind = i; n   } n   sort(query + 1, query + 1 + q, cmp); n   for (int i = 1; i <= q; i++) { n     if (query[i].b > k) { n       long long sum = 0; n       int a = query[i].a; n       int b = query[i].b; n       for (int j = a; j <= n; j += b) { n         sum += niz[j]; n       } n       res[query[i].ind] = sum; n     } else if (query[i].b == query[i - 1].b) { n       res[query[i].ind] = dp[query[i].a]; n     } else { n       int b = query[i].b; n       for (int j = n; j >= 1; j--) { n         if (j + b > n) n           dp[j] = niz[j]; n         else n           dp[j] = niz[j] + dp[j + b]; n       } n       res[query[i].ind] = dp[query[i].a]; n     } n   } n   for (int i = 1; i <= q; i++) cout << res[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   int m; n   cin >> n >> m; n   int l; n   int r; n   int arr[n]; n   int pos = 0; n   int neg = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] == -1) { n       neg++; n     } else { n       pos++; n     } n   } n   int mn = min(neg, pos); n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     int c = 0; n     if (l == r) { n       cout <<  0  << endl; n       c++; n     } n     if (c == 0) { n       if ((abs(l - r) + 1) / 2 <= mn && (abs(l - r) + 1) % 2 == 0) { n         cout <<  1  << endl; n       } else { n         cout <<  0  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1147483646; n const long long MX2 = 9223372036854775800; n const int MOD = 1000000007; n int p[30]; n int find(int n) { n   if (p[n] < 0) return n; n   return p[n] = find(p[n]); n } n void uni(int e1, int e2) { n   e1 = find(e1); n   e2 = find(e2); n   if (e1 == e2) return; n   p[e1] = e2; n   return; n } n int main() { n   int n; n   scanf( %d , &n); n   char s1[100011], s2[100011]; n   vector<pair<int, int> > ans; n   fill(p, p + 30, -1); n   scanf( %s %s , s1, s2); n   int a1, a2; n   for (int i = 0; i < n; i++) { n     a1 = find(int(s1[i] -  a )); n     a2 = find(int(s2[i] -  a )); n     if (a1 != a2) { n       ans.push_back({a1, a2}); n       uni(a1, a2); n     } n   } n   printf( %d n , ans.size()); n   for (pair<int, int> x : ans) { n     printf( %c %c n , x.first +  a , x.second +  a ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int i, j, k, n, m, ans = 0, cnt = 0, sum = 0; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<int> res; n   res.push_back(a[0]); n   int last = a[0], flag = 1; n   for (int i = 1; i < n - 1;) { n     if (abs(a[i] - last) + abs(a[i] - a[i + 1]) > abs(last - a[i + 1])) { n       res.push_back(a[i]); n       last = a[i]; n       i++; n     } else n       i++; n   } n   if (flag) res.push_back(a[n - 1]); n   cout << res.size() <<   n ; n   for (auto x : res) cout << x <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool valid(int i, int j, int n, int m) { n   return i >= 0 && i < n && j >= 0 && j < m; n } n void solve() { n   int n, m; n   int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n   cin >> n >> m; n   char a[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  B ) { n         for (int p = 0; p < 4; p++) { n           if (valid(i + dir[p][0], j + dir[p][1], n, m) && n               a[i + dir[p][0]][j + dir[p][1]] ==  . ) n             a[i + dir[p][0]][j + dir[p][1]] =  # ; n         } n       } n     } n   } n   bool visited[n][m]; n   memset(visited, false, sizeof(visited)); n   queue<pair<int, int> > q; n   if (a[n - 1][m - 1] ==  . ) { n     q.push(make_pair(n - 1, m - 1)); n     visited[n - 1][m - 1] = true; n   } n   while (!q.empty()) { n     pair<int, int> temp = q.front(); n     q.pop(); n     for (int p = 0; p < 4; p++) { n       int x = temp.first + dir[p][0]; n       int y = temp.second + dir[p][1]; n       if (valid(x, y, n, m)) { n         if (!visited[x][y] && a[x][y] !=  # ) { n           q.push(make_pair(x, y)); n           visited[x][y] = true; n         } n       } n     } n   } n   bool ans = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  G  && visited[i][j] != true) { n         ans = false; n         break; n       } else if (a[i][j] ==  B  && visited[i][j] != false) { n         ans = false; n         break; n       } n     } n   } n   cout << (ans ?  Yes  :  No ); n } n int main() { n   int t, c = 0; n   cin >> t; n   while (t--) { n     solve(); n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 5; n const int md = 1e9 + 7; n string mat[2], pat; n long long n, m, dp[2][maxn][2], pr[2][maxn][2], re, is[2][maxn][maxn], n     ris[2][maxn][maxn]; n long long sum(long long a, long long b) { return (a + b) % md; } n long long mul(long long a, long long b) { return (a * b) % md; } n void solve() { n   memset(dp, 0, sizeof dp); n   memset(is, 0, sizeof is); n   memset(ris, 0, sizeof ris); n   for (int r = 0; r < 2; ++r) { n     for (int j = 0; j < m; ++j) { n       is[r][0][j] = mat[r][0] == pat[j]; n     } n     for (int i = 1; i < n; ++i) { n       is[r][i][0] = mat[r][i] == pat[0]; n       for (int j = 1; j < m; ++j) { n         if (mat[r][i] == pat[j]) is[r][i][j] = is[r][i - 1][j - 1] + 1; n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       ris[r][i][0] = mat[r][i] == pat[0]; n       for (int j = 1; j < m; ++j) { n         if (mat[r][i] == pat[j]) ris[r][i][j] = ris[r][i + 1][j - 1] + 1; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     memcpy(pr, dp, sizeof dp); n     memset(dp, 0, sizeof dp); n     dp[0][0][0] = (mat[0][i] == pat[0]); n     dp[1][0][0] = (mat[1][i] == pat[0]); n     for (int j = 1; j < m; ++j) { n       for (int r = 0; r < 2; ++r) n         if (mat[r][i] == pat[j]) { n           dp[r][j][0] = pr[r][j - 1][0]; n           dp[r][j][0] = sum(dp[r][j][0], pr[r][j - 1][1]); n           int x = j + 1; n           if (x % 2 == 0 && x / 2 > 1 && i >= x / 2 - 1) { n             x /= 2; n             dp[r][j][1] = n                 sum(dp[r][j][1], n                     (is[r][i][j] >= x) * (ris[r ^ 1][i - x + 1][j - x] >= x)); n           } n           dp[r][j][1] += dp[r ^ 1][j - 1][0]; n         } n     } n     for (int j = 0; j < m; ++j) { n       if (j == m - 1) { n         re = sum(re, dp[0][j][0] + dp[0][j][1] + dp[1][j][1] + dp[1][j][0]); n         continue; n       } n       for (int r = 0; r < 2; ++r) { n         int x = m - 1 - j; n         if (x % 2) continue; n         x /= 2; n         if (x == 1) continue; n         int y = i + x; n         if (y >= n) continue; n         re = sum(re, dp[r][j][0] * (is[r][y][j + x] >= x) * n                          (ris[r ^ 1][i + 1][m - 1] >= x)); n         re = sum(re, dp[r][j][1] * (is[r][y][j + x] >= x) * n                          (ris[r ^ 1][i + 1][m - 1] >= x)); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   for (int i = 0; i < 2; ++i) { n     cin >> mat[i]; n   } n   n = mat[0].size(); n   cin >> pat; n   m = pat.size(); n   if (m == 1) { n     for (auto u : mat[0]) { n       if (u == pat[0]) { n         re = re + 1; n       } n     } n     for (auto u : mat[1]) { n       if (u == pat[0]) { n         re = re + 1; n       } n     } n     cout << re <<   n ; n     return 0; n   } n   if (m == 2) { n     for (int r = 0; r < 2; ++r) { n       for (int i = 0; i < n; ++i) { n         if (i > 0 && mat[r][i] == pat[1] && mat[r][i - 1] == pat[0]) { n           re = re + 1; n         } n         if (i < n - 1 && mat[r][i] == pat[1] && mat[r][i + 1] == pat[0]) { n           re = re + 1; n         } n         if (mat[r][i] == pat[1] && mat[r ^ 1][i] == pat[0]) { n           re = re + 1; n         } n       } n     } n     cout << re <<   n ; n     return 0; n   } n   solve(); n   for (int r = 0; r < 2; ++r) { n     reverse(mat[r].begin(), mat[r].end()); n   } n   solve(); n   cout << re <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n, k; n     cin >> n >> k; n     int mn = INF, mx = -1; n     for (int i = 1; i <= n; ++i) { n       int x; n       scanf( %d , &x); n       mn = min(mn, x); n       mx = max(mx, x); n     } n     int m = (mx + mn) >> 1; n     printf( %d n , max(m - mn, mx - m) <= k ? mn + k : -1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n #ifdef DEBUG n   freopen( a.in ,  r , stdin); n #endif n   ios_base::sync_with_stdio(false), cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int a[2]; n     cin >> a[0] >> a[1]; n     string s; n     cin >> s; n     int n = a[0] + a[1]; n     vector<pair<int, int>> x; n     for (int i = 0; i < n / 2; ++i) { n       if (s[i] ==  ?  && s[n - 1 - i] ==  ? ) { n         x.emplace_back(i, n - 1 - i); n       } else if (s[i] ==  ? ) { n         a[s[n - 1 - i] -  0 ] -= 2; n         s[i] = s[n - 1 - i]; n       } else if (s[n - 1 - i] ==  ? ) { n         a[s[i] -  0 ] -= 2; n         s[n - 1 - i] = s[i]; n       } else { n         if (s[i] != s[n - 1 - i]) { n           a[0] = -1; n         } else { n           a[s[i] -  0 ] -= 2; n         } n       } n     } n     if (a[0] < 0 || a[1] < 0) { n       cout << -1 <<   n ; n       continue; n     } n     for (int i = 0; i < x.size(); ++i) { n       if (a[0] >= 2) { n         s[x[i].first] = s[x[i].second] =  0 ; n         a[0] -= 2; n       } else if (a[1] >= 2) { n         s[x[i].first] = s[x[i].second] =  1 ; n         a[1] -= 2; n       } else { n         a[0] = -1; n       } n     } n     if (a[0] < 0 || a[1] < 0) { n       cout << -1 <<   n ; n       continue; n     } n     if ((n & 1) && s[n / 2] !=  ? ) a[s[n / 2] -  0 ]--; n     if ((n & 1) && s[n / 2] ==  ? ) s[n / 2] = a[0] ?  0  :  1 ; n     if (a[0] < 0 || a[1] < 0) n       cout << -1 <<   n ; n     else n       cout << s <<   n ; n   } n   return 0; n }


#include <bits/stdc++.h> n int main() { n   int n, k, i, j, swap; n   scanf( %d %d , &n, &k); n   int ara[n + 1][3]; n   for (i = 1; i <= n; i++) { n     scanf( %d , &ara[i][1]); n     ara[i][2] = i; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= (n - i); j++) { n       if (ara[j][1] > ara[j + 1][1]) { n         swap = ara[j][1]; n         ara[j][1] = ara[j + 1][1]; n         ara[j + 1][1] = swap; n         swap = ara[j][2]; n         ara[j][2] = ara[j + 1][2]; n         ara[j + 1][2] = swap; n       } n     } n   } n   int sum = 0, p = 0; n   for (i = 1; i <= n; i++) { n     sum = sum + ara[i][1]; n     if (sum <= k) n       p++; n     else n       break; n   } n   if (p == 0) n     printf( 0 n ); n   else { n     printf( %d n , p); n     for (i = 1; i <= p; i++) printf( %d  , ara[i][2]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int p[n + 1]; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   bool done[n + 1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) done[j] = false; n     int start = i; n     while (1) { n       if (done[start] == true) { n         cout << start <<    ; n         break; n       } n       done[start] = true; n       start = p[start]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}; n const double EPS = 1e-8; n const double PI = acos(-1.0); n int popcount(int n) { return __builtin_popcount(n); } n int popcount(long long n) { return __builtin_popcountll(n); } n template <class T> n int SIZE(T a) { n   return a.size(); n } n template <class T> n string IntToString(T num) { n   string res; n   stringstream ss; n   ss << num; n   return ss.str(); n } n template <class T> n T StringToInt(string str) { n   T res = 0; n   for (int i = 0; i < SIZE(str); i++) res = (res * 10 + str[i] -  0 ); n   return res; n } n template <class T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n void PrintSeq(T &a, int sz) { n   for (int i = 0; i < sz; i++) { n     cout << a[i]; n     if (sz == i + 1) n       cout << endl; n     else n       cout <<    ; n   } n } n bool EQ(double a, double b) { return abs(a - b) < EPS; } n void fastStream() { n   cin.tie(0); n   std::ios_base::sync_with_stdio(0); n } n vector<string> split(string str, char del) { n   vector<string> res; n   for (int i = 0, s = 0; i < SIZE(str); i++) { n     if (str[i] == del) { n       if (i - s != 0) res.push_back(str.substr(s, i - s)); n       s = i + 1; n     } else if (i == SIZE(str) - 1) { n       res.push_back(str.substr(s)); n     } n   } n   return res; n } n int froms[1000001]; n int tos[1000001]; n vector<pair<int, int> > G[1000001]; n bool used[1000001]; n void dfs(int pprv, int prv, int now) { n   used[now] = true; n   for (int i = 0; i < (int)G[now].size(); i++) { n     int to = G[now][i].first; n     int en = G[now][i].second; n     if (!used[to]) { n       dfs(prv, en, to); n     } n   } n   if (prv != -1 && pprv != -1) cout << pprv <<     << prv << endl; n } n int main() { n   fastStream(); n   int N; n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     cin >> froms[i] >> tos[i]; n     G[froms[i]].push_back(make_pair(tos[i], i + 1)); n     G[tos[i]].push_back(make_pair(froms[i], i + 1)); n   } n   cout << N - 1 << endl; n   for (int i = 0; i < N - 1; i++) n     cout << 2 <<     << froms[i] <<     << tos[i] << endl; n   for (int i = 0; i < N; i++) { n     if (G[i + 1].size() == 1) { n       dfs(-1, -1, i + 1); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, val, i, j, k, n, m, val2; n   vector<pair<long long, long long> > ans; n   cin >> x; n   for (n = 1; n <= (3000000); n++) { n     val = x; n     val -= (n - 1) * n * (2 * n - 1) / 6; n     val += n * n * (n - 1) / 2; n     val2 = n * n - ((n - 1) * n) / 2; n     if (val % val2 == 0) { n       val2 = val / val2; n       if (val2 >= n) { n         ans.push_back(make_pair(n, val2)); n         if (n != val2) ans.push_back(make_pair(val2, n)); n       } n     } n   } n   printf( %d n , ans.size()); n   sort(ans.begin(), ans.end()); n   for (i = 0; i < ans.size(); i++) n     printf( %lld %lld n , ans[i].first, ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int n, m, f[N], vis[N], cnt; n int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } n struct node { n   int x, y, w; n } a[N]; n bool cmp(node a, node b) { return a.w < b.w; } n int main() { n   while (cin >> n >> m) { n     int ans = 0; n     for (int i = 1; i <= n; i++) f[i] = i; n     for (int i = 1; i <= m; i++) { n       int x, y, k; n       cin >> x >> y >> k; n       a[i].x = x; n       a[i].y = y; n       a[i].w = k; n     } n     sort(a + 1, a + 1 + m, cmp); n     int mx = 0; n     for (int i = 1; i <= m;) { n       int j = i; n       while (a[i].w == a[j].w) j++; n       cnt = 0; n       for (int k = i; k < j; k++) n         if (find(a[k].x) != find(a[k].y)) cnt++; n       for (int k = i; k < j; k++) { n         if (find(a[k].x) != find(a[k].y)) { n           f[find(a[k].x)] = find(a[k].y); n           cnt--; n         } n       } n       ans += cnt; n       i = j; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long x[105]; n double f[105][105525]; n double sum[105525]; n inline void read() { n   cin >> n >> m; n   for (long long i = 0; i <= n - 1; i++) { n     cin >> x[i]; n   } n } n inline void solve() { n   long long ms = n * m; n   if (m == 1) { n     puts( 1 ); n     return; n   } n   f[0][0] = 1; n   for (long long i = 0; i <= n - 1; i++) { n     for (long long s = 0; s <= ms; s++) { n       if (s) { n         sum[s] = sum[s - 1] + f[i][s]; n       } else { n         sum[s] = f[i][s]; n       } n       if (s > m) { n         f[i + 1][s] = sum[s - 1] - sum[s - m - 1]; n       } else { n         f[i + 1][s] = sum[s - 1]; n       } n       if (s >= x[i]) { n         f[i + 1][s] -= f[i][s - x[i]]; n       } n       f[i + 1][s] /= (m - 1); n     } n   } n   long long score = 0; n   for (long long i = 0; i <= n - 1; i++) { n     score += x[i]; n   } n   double prob = 0; n   for (long long s = 0; s <= score - 1; s++) { n     prob += f[n][s]; n   } n   prob *= (m - 1); n   prob += 1.0; n   cout.precision(20); n   cout << fixed << prob; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::fixed; n using std::make_pair; n using std::max; n using std::min; n using std::pair; n using std::set; n using std::sort; n const long long base = (long long)(5 * 1e8 + 5); n const long long coefficient = (long long)(4 * 1e9); n long long convertCoordinate(long long x, long long y) { n   return (x + base) * coefficient + (y + base); n } n pair<long long, long long> convertCoordinateBack(long long pos) { n   long long x = pos / coefficient - base - base; n   long long y = pos % coefficient - base - base; n   return make_pair(x, y); n } n const int MaxN = 200000 + 9; n int n, k, l, r, used; n long long points[MaxN]; n long long x, y, now; n set<long long> waitList; n set<pair<long long, long long> > ans; n set<long long>::iterator it; n set<pair<long long, long long> >::iterator pit; n int main() { n   waitList.clear(); n   ans.clear(); n   scanf( %d%d , &n, &k); n   if (k >= n) return cout <<  -1 n , 0; n   for (int i = 1; i <= n; i++) { n     scanf( %I64d%I64d , &x, &y); n     points[i] = convertCoordinate(x, y); n   } n   sort(points + 1, points + n + 1); n   for (int i = 1; i <= min(k + 2, n); i++) { n     for (int j = max(n - k - 2, 1); j <= n; j++) { n       waitList.insert(points[i] + points[j]); n     } n   } n   for (it = waitList.begin(); it != waitList.end(); it++) { n     l = 1; n     r = n; n     used = 0; n     while (l <= r) { n       now = points[l] + points[r]; n       if (now < (*it)) n         l++, used++; n       else if (now > (*it)) n         r--, used++; n       else n         l++, r--; n     } n     if (used <= k) ans.insert(convertCoordinateBack(*it)); n   } n   if (ans.size() == 0) n     printf( 0 n ); n   else { n     printf( %d n , ans.size()); n     for (pit = ans.begin(); pit != ans.end(); pit++) { n       printf( %.2lf %.2lf n , (double)(*pit).first * 0.5, n              (double)(*pit).second * 0.5); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   string s; n   cin >> s; n   long long int n = s.length(); n   long long int dp[s.length() + 5][27]; n   for (int i = 0; i <= s.length(); i++) { n     for (int j = 0; j < 27; j++) dp[i][j] = 0; n   } n   for (int i = 0; i < s.length(); i++) { n     if (i != 0) { n       for (int j = 0; j < 27; j++) { n         dp[i + 1][j] = dp[i][j]; n       } n     } n     dp[i + 1][s[i] -  a ]++; n   } n   long long int ans = 1000000000000000000; n   for (char c =  a ; c <=  z ; c++) { n     long long int low = 1, high = n; n     long long int mid; n     while (low <= high) { n       mid = (low + high) / 2; n       bool f = 1; n       for (int i = mid; i <= n; i++) { n         if (dp[i][c -  a ] - dp[i - mid][c -  a ] <= 0) { n           f = 0; n         } n       } n       if (f) { n         high = mid - 1; n       } else { n         low = mid + 1; n       } n     } n     ans = min(low, ans); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n template <class T> n void clr(T& a, int n) { n   a.clear(); n   a.resize(n + 1); n } n void solve(); n const int N = 2e5 + 10; n const int INF = 1e9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n int n; n void solve() { n   cin >> n; n   vector<long long> a(n + 1); n   for (int i = int(1); i <= int(n); i++) { n     cin >> a[i]; n   } n   const int B = 20; n   vector<long long> count(B + 1); n   for (int i = int(1); i <= int(n); i++) { n     for (int j = int(0); j <= int(B); j++) { n       long long mask = (1ll << j); n       count[j] += (mask & a[i] ? 1 : 0); n     } n   } n   debug() <<   [  n           <<  count  n               :   n           << (count) <<  ]  ; n   long long ans = 0; n   for (int i = int(1); i <= int(n); i++) { n     long long curr = 0; n     for (int j = int(0); j <= int(B); j++) { n       if (count[j] == 0) continue; n       curr += (1ll << j); n       count[j]--; n     } n     ans += curr * curr; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n class state { n  public: n   int d[4]; n   int e; n   state() : d{0, 0, 0, 0}, e(1) {} n   bool operator<(const state &x) const { n     for (int i = 0; i < 4; ++i) { n       if (d[i] == x.d[i]) continue; n       return d[i] > x.d[i]; n     } n     return e > x.e; n   } n   bool operator!=(const state &x) const { n     for (int i = 0; i < 4; ++i) n       if (d[i] != x.d[i]) return true; n     return e != x.e; n   } n }; n std::map<state, int> mp; n int main() { n   int n; n   scanf( %d , &n); n   mp[state()] = 0; n   while (n--) { n     int a, b; n     scanf( %d%d , &a, &b); n     std::map<state, int> nmp; n     for (auto it = mp.begin(); it != mp.end(); ++it) { n       if (it->first.d[0] != 0) continue; n       state s = it->first; n       int t = it->second; n       t += abs(a - s.e) + 1; n       s.e = a; n       s.d[0] = b; n       std::sort(s.d, s.d + 4); n       auto it2 = nmp.lower_bound(s); n       if (it2 == nmp.end() || it2->first != s) n         it2 = nmp.insert(it2, std::pair<state, int>(s, t)); n       it2->second = std::min(it2->second, t); n     } n     mp.swap(nmp); n     0; n     for (auto it = mp.begin(); it != mp.end(); ++it) { n       0; n       for (int i = 0; i < 4; ++i) { n         if (!it->first.d[i]) continue; n         state s = it->first; n         int t = it->second; n         t += abs(s.d[i] - s.e) + 1; n         s.e = s.d[i]; n         s.d[i] = 0; n         std::sort(s.d, s.d + 4); n         auto it2 = mp.lower_bound(s); n         if (it2 == mp.end() || it2->first != s) n           it2 = mp.insert(it2, std::pair<state, int>(s, t)); n         it2->second = std::min(it2->second, t); n       } n     } n   } n   int ans = ((int)1e9); n   for (auto it = mp.begin(); it != mp.end(); ++it) { n     if (it->first.d[0] || it->first.d[1] || it->first.d[2] || it->first.d[3]) n       continue; n     ans = std::min(ans, it->second); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[15], cnt[7] = {0}, ans = 0; n int j, hr = 0, mi = 0; n int se[100] = {0}; n vector<int> na, ma; n void rec(int pos, int a) { n   int i; n   if (pos == -1) { n     ans++; n     return; n   } n   if (pos == mi - 1) { n     for (i = 0; i <= na[pos]; i++) { n       if (cnt[i] != 1) { n         cnt[i] = 1; n         if (i == na[pos]) se[pos] = 1; n         rec(pos - 1, i); n         cnt[i] = 0; n       } n       se[pos] = 0; n     } n   } else if (se[pos + 1] == 0) { n     for (i = 0; i < 7; i++) { n       if (cnt[i] != 1) { n         cnt[i] = 1; n         rec(pos - 1, i); n         cnt[i] = 0; n       } n     } n   } else { n     for (i = 0; i <= na[pos]; i++) { n       if (cnt[i] != 1) { n         cnt[i] = 1; n         if (i == na[pos]) { n           se[pos] = 1; n         } n         rec(pos - 1, i); n         cnt[i] = 0; n       } n       se[pos] = 0; n     } n   } n } n bool chk(int *a, int n) { n   int i, flg = 0; n   for (i = 0; i < n - 1; i++) { n     if (a[i] <= a[i + 1]) n       flg = 1; n     else { n       flg = 0; n       break; n     } n   } n   return flg; n } n int main() { n   arr[0] = 1; n   for (int i = 1; i <= 10; i++) { n     arr[i] = arr[i - 1] * 7; n   } n   int n, m, i; n   cin >> n >> m; n   n = n - 1; n   m = m - 1; n   int x = n, y = m; n   if (y == 0) { n     na.push_back(0); n     mi++; n   } n   while (y != 0) { n     na.push_back(y % 7); n     mi++; n     y = y / 7; n   } n   if (x == 0) { n     na.push_back(0); n     hr++; n   } n   while (x != 0) { n     na.push_back(x % 7); n     hr++; n     x = x / 7; n   } n   if (hr + mi > 7) n     ans = 0; n   else { n     int pos = na.size() - 1; n     for (i = 0; i <= na[pos]; i++) { n       cnt[i] = 1; n       if (i == na[pos]) se[pos] = 1; n       rec(pos - 1, i); n       cnt[i] = 0; n       se[pos] = 0; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[55][55]; n int main() { n   int n; n   scanf( %d , &n); n   memset(a, 0, sizeof(a)); n   int p = 1, q = 2, nn = n / 2 + 1; n   for (int i = 1; i <= n; ++i) { n     a[nn][i] = p; n     p += 2; n   } n   for (int i = 1; i <= n; ++i) { n     if (i != nn) { n       a[i][nn] = p; n       p += 2; n     } n   } n   int maxn = n * n; n   for (int i = 1; i < nn; ++i) { n     for (int j = 1; j < nn; ++j) { n       if (p <= maxn) { n         a[i][j] = p; n         a[n + 1 - i][j] = p + 2; n         a[n + 1 - i][n + 1 - j] = p + 4; n         a[i][n + 1 - j] = p + 6; n         p += 8; n       } else { n         a[i][j] = q; n         a[n + 1 - i][j] = q + 2; n         a[n + 1 - i][n + 1 - j] = q + 4; n         a[i][n + 1 - j] = q + 6; n         q += 8; n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) printf( %d  , a[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int max(int a, int b) { return (a > b) ? a : b; } n struct player { n   int i; n   int j; n   int pos; n }; n int main() { n   bool firstwon1 = false; n   bool secondwon1 = false; n   bool firstwon2 = false, firstwon3 = false, firstwon4 = false; n   bool secondwon2 = false, secondwon3 = false, secondwon4 = false; n   int won = 0; n   player a[2], b[2]; n   cin >> a[0].i >> a[0].j; n   cin >> a[1].i >> a[1].j; n   cin >> b[0].i >> b[0].j; n   cin >> b[1].i >> b[1].j; n   if (a[0].i > b[1].j && a[1].j > b[0].i) firstwon1 = true; n   if (a[0].i < b[1].j && a[1].j < b[0].i) secondwon1 = true; n   if (a[0].i > b[0].j && a[1].j > b[1].i) firstwon2 = true; n   if (a[0].i < b[0].j && a[1].j < b[1].i) secondwon2 = true; n   if (a[1].i > b[1].j && a[0].j > b[0].i) firstwon3 = true; n   if (a[1].i < b[1].j && a[0].j < b[0].i) secondwon3 = true; n   if (a[1].i > b[0].j && a[0].j > b[1].i) firstwon4 = true; n   if (a[1].i < b[0].j && a[0].j < b[1].i) secondwon4 = true; n   if ((firstwon1 == true && firstwon2 == true) || n       (firstwon3 == true && firstwon4 == true)) n     won = 1; n   if ((secondwon1 == true || secondwon2 == true) && n       (secondwon3 == true || secondwon4 == true)) n     won = 2; n   switch (won) { n     case 1: { n       cout <<  Team 1 n ; n       break; n     } n     case 2: { n       cout <<  Team 2 n ; n       break; n     } n     case 0: { n       cout <<  Draw n ; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const double pi = acos(-1.0); n void cp(); n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n long long fact(long long n) { n   if (n <= 1) return n; n   return n * fact(n - 1); n } n bool prime[100001]; n void sieve() { n   int n = 10000; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n void solve() { n   int n, i; n   cin >> n; n   string s; n   cin >> s; n   for (i = 0; i <= (2 * n) - 1; i++) { n     if (i % 2 == 0) cout << s[i]; n   } n   cout <<   n ; n } n int main() { n   cp(); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n void cp() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n void enumerateSubmasks(int m) { n   for (int s = m;; s = (s - 1) & m) { n     if (s == 0) { n       break; n     } n   } n } n int mpow(int a, int b, int m) { n   if (b == 0) return 1; n   int x = mpow(a, b / 2, m); n   x = (x * x) % m; n   if (b % 2) { n     x = (x * a) % m; n   } n   return x; n } n void update(int s, int e, int qs, int qe, vector<int> &seg, vector<int> &lazy, n             int index, int value) { n   if (lazy[index] != -1) { n     seg[index] = max(seg[index], lazy[index]); n     if (s != e) { n       if (lazy[2 * index] == -1) n         lazy[2 * index] = lazy[index]; n       else n         lazy[2 * index] = max(lazy[2 * index], lazy[index]); n       if (lazy[2 * index + 1] == -1) n         lazy[2 * index + 1] = lazy[index]; n       else n         lazy[2 * index + 1] = max(lazy[2 * index + 1], lazy[index]); n     } n     lazy[index] = -1; n   } n   if (qs > e || qe < s) return; n   if (s >= qs && e <= qe) { n     seg[index] = max(seg[index], value); n     if (s != e) { n       if (lazy[2 * index] == -1) n         lazy[2 * index] = value; n       else n         lazy[2 * index] = max(lazy[2 * index], value); n       if (lazy[2 * index + 1] == -1) n         lazy[2 * index + 1] = value; n       else n         lazy[2 * index + 1] = max(lazy[2 * index + 1], value); n     } n     return; n   } n   int mid = (s + e) / 2; n   update(s, mid, qs, qe, seg, lazy, 2 * index, value); n   update(mid + 1, e, qs, qe, seg, lazy, 2 * index + 1, value); n } n int query(int s, int e, int qs, int qe, vector<int> &seg, vector<int> &lazy, n           int index) { n   if (lazy[index] != -1) { n     seg[index] = max(seg[index], lazy[index]); n     if (s != e) { n       if (lazy[2 * index] == -1) n         lazy[2 * index] = lazy[index]; n       else n         lazy[2 * index] = max(lazy[2 * index], lazy[index]); n       if (lazy[2 * index + 1] == -1) n         lazy[2 * index + 1] = lazy[index]; n       else n         lazy[2 * index + 1] = max(lazy[2 * index + 1], lazy[index]); n     } n     lazy[index] = -1; n   } n   if (qs > e || qe < s) return LLONG_MIN; n   if (s >= qs && e <= qe) { n     return seg[index]; n   } n   int mid = (s + e) / 2; n   int a = query(s, mid, qs, qe, seg, lazy, 2 * index); n   int b = query(mid + 1, e, qs, qe, seg, lazy, 2 * index + 1); n   return max(a, b); n } n void printBinaryString(int n) { n   vector<int> temp; n   while (n) { n     if (n & 1) n       temp.push_back(1); n     else n       temp.push_back(0); n     n = n >> 1; n   } n   reverse(temp.begin(), temp.end()); n   for (auto node : temp) cout << node <<    ; n   cout << endl; n } n void readVector(vector<int> &a) { n   int n = a.size(); n   for (int i = 0; i < n; ++i) cin >> a[i]; n } n vector<vector<int>> adj(100002); n vector<int> par(100002); n void dfs(int node, int parent) { n   par[node] = parent; n   for (auto child : adj[node]) { n     if (child == parent) continue; n     dfs(child, node); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<pair<int, int>> graph; n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n     graph.push_back({u, v}); n   } n   dfs(1, 1); n   cout << n - 1 << endl; n   for (int i = 2; i <= n; i++) { n     cout << 2 <<     << i <<     << par[i] << endl; n   } n   int oneCousin = *adj[1].begin(); n   for (auto node : adj[1]) { n     if (node == oneCousin) continue; n     graph.push_back({oneCousin, node}); n   } n   for (auto node : graph) n     if (node.first != 1 && node.second != 1) n       cout << node.first - 1 <<     << node.second - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n typedef pair<int, int> P; n const int OO = 1e18; n const int md = 1e9 + 7; n int X[] = {0, 0, 1, -1}, Y[] = {1, -1, 0, 0}; n void fast() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int dp[int(1e4 + 4)], t = 0, x, y, z, n, q, g[int(1e4 + 4)], res; n pair<P, int> p[int(1e4 + 4)]; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < q; i++) n     scanf( %d%d%d , &p[i].first.first, &p[i].first.second, &p[i].second); n   sort(p, p + q); n   memset(dp, -1, sizeof dp); n   memset(g, -1, sizeof g); n   dp[0] = g[0] = n; n   for (int i = 0; i < q; i++) { n     for (int j = 0; j <= n; j++) { n       g[j] = max(g[j], dp[j]); n       if (dp[j] >= p[i].first.first && j + p[i].second <= n) n         g[j + p[i].second] = n             max(dp[j + p[i].second], min(dp[j], p[i].first.second)); n     } n     g[p[i].second] = max(g[p[i].second], p[i].first.second); n     swap(dp, g); n   } n   for (int i = 1; i <= n; i++) n     if (dp[i] != -1) res++; n   printf( %d n , res); n   for (int i = 1; i <= n; i++) n     if (dp[i] != -1) printf( %d  , i); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int Maxn = 10010; n int a[Maxn]; n int tot = 0; n int main() { n   int n; n   cin >> n; n   long long sum = 0; n   for (int i = 2; i <= n; i++) { n     for (int j = 2 * i; j <= n; j += i) { n       sum += 4ll * (j / i); n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct PR { n   int to; n   int ne; n } Prise[200010]; n bool compare(PR a, PR b) { return (a.to - a.ne) < (b.to - b.ne); } n int main(int argc, char const *argv[]) { n   int n, k; n   cin >> n >> k; n   int i; n   for (i = 0; i < n; i++) { n     cin >> Prise[i].to; n   } n   for (i = 0; i < n; i++) { n     cin >> Prise[i].ne; n   } n   sort(Prise, Prise + n, compare); n   int count = 0; n   for (i = 0; i < k; i++) { n     count += Prise[i].to; n   } n   for (i = k; i < n; i++) { n     if (Prise[i].to < Prise[i].ne) { n       count += Prise[i].to; n     } else { n       count += Prise[i].ne; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long far(long long x, long long y) { return x * x + y * y; } n vector<long long> val; n vector<long long> dsu; n vector<long long> rang; n long long pred(long long a) { n   if (a == dsu[a]) { n     return a; n   } else { n     long long to = pred(dsu[a]); n     if (to != dsu[a]) { n       val[a] *= val[dsu[a]]; n     } n     dsu[a] = to; n     return to; n   } n } n void unite(long long a, long long b) { n   a = pred(a); n   b = pred(b); n   if (a != b) { n     if (rang[a] < rang[b]) { n       swap(a, b); n     } n     if (val[a] == -1) { n       val[b] *= -1; n     } n     dsu[b] = a; n     if (rang[a] == rang[b]) { n       rang[a]++; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   val.resize(n); n   dsu.resize(n); n   rang.resize(n); n   vector<pair<long long, long long> > x(n); n   vector<long long> type(n, 1); n   long long stx = 0, sty = 0; n   for (long long i = 0; i < n; i++) { n     cin >> x[i].first >> x[i].second; n   } n   long long p = 1e6; n   p = p * p; n   vector<long long> ind(n); n   for (long long i = 0; i < n; i++) { n     ind[i] = i; n     dsu[i] = i; n     rang[i] = 1; n     val[i] = 1; n   } n   for (long long i = 0; i < n - 2; i++) { n     for (long long j = i + 0; j < min(n, 3 + i); j++) { n       for (long long d = j + 1; d < min(n, 3 + i); d++) { n         if (far(x[j].first + x[d].first, x[j].second + x[d].second) <= p) { n           unite(ind[j], ind[d]); n           x[d].first += x[j].first; n           x[d].second += x[j].second; n           if (j != i) { n             x[i + 1] = x[i]; n             ind[i + 1] = ind[i]; n           } n           j = 3 + i; n           break; n         } else if (far(x[j].first - x[d].first, x[j].second - x[d].second) <= n                    p) { n           val[pred(ind[d])] *= -1; n           unite(ind[j], ind[d]); n           x[d].first = x[j].first - x[d].first; n           x[d].second = x[j].second - x[d].second; n           if (j != i) { n             x[i + 1] = x[i]; n             ind[i + 1] = ind[i]; n           } n           j = 3 + i; n           break; n         } n       } n     } n   } n   long long p1 = 1.5e6; n   p1 = p1 * p1; n   if (n > 1) { n     if (far(x[n - 2].first + x[n - 1].first, n             x[n - 2].second + x[n - 1].second) <= p1) { n       unite(ind[n - 2], ind[n - 1]); n     } else { n       val[pred(ind[n - 1])] *= -1; n       unite(ind[n - 2], ind[n - 1]); n     } n   } n   for (long long i = 0; i < n; i++) { n     long long t = pred(i); n     if (t != i) { n       cout << val[t] * val[i] <<    ; n     } else { n       cout << val[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265358979323846; n const double EPS = 1e-15; n const int INF = 1e9 + 7; n const long long LINF = 8e18 + 7; n const int N = 2e5 + 7; n vector<long long> t[4 * N]; n int a[N]; n long long s[N]; n void build(int v, int L, int R) { n   if (L + 1 == R) { n     t[v].push_back(s[L]); n     return; n   } n   int M = L + R >> 1; n   build(v << 1, L, M); n   build(v << 1 | 1, M, R); n   t[v].resize(t[v << 1].size() + t[v << 1 | 1].size()); n   merge(t[v << 1].begin(), t[v << 1].end(), t[v << 1 | 1].begin(), n         t[v << 1 | 1].end(), t[v].begin()); n } n int get(int v, int L, int R, int l, int r, long long val) { n   if (L == l && R == r) { n     return t[v].end() - upper_bound(t[v].begin(), t[v].end(), val); n   } n   int M = L + R >> 1; n   int ans = 0; n   if (l < M) { n     ans += get(v << 1, L, M, l, min(M, r), val); n   } n   if (r > M) { n     ans += get(v << 1 | 1, M, R, max(l, M), r, val); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   long long t; n   cin >> n >> t; n   for (int i = 0; i < int(n); ++i) { n     cin >> a[i]; n     s[i + 1] = a[i] + s[i]; n   } n   build(1, 0, n + 1); n   long long ans = 0; n   for (int i = 0; i < int(n); ++i) { n     ans += get(1, 0, n + 1, 0, i + 1, s[i + 1] - t); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1e5 + 3; n int t; n long long m, n; n pair<long long, long long> v[MX]; n long long p[MX]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %lld %lld , &n, &m); n     long long ans = 0; n     for (int i = 0; i < m; i++) { n       scanf( %lld %lld , &v[i].first, &v[i].second); n     } n     sort(v, v + m, greater<pair<long long, long long>>()); n     for (int i = 0; i < m; i++) { n       p[i] = v[i].first + (i ? p[i - 1] : 0); n       if (i < n) ans += v[i].first; n     } n     for (int i = 0; i < m; i++) { n       int l = 0; n       int r = m - 1; n       int md; n       while (l != r) { n         md = (l + r) / 2; n         if (v[md].first < v[i].second) n           r = md; n         else n           l = md + 1; n       } n       int cnt = l; n       if (v[m - 1].first >= v[i].second) cnt++, l++; n       long long sum = 0; n       if (l) sum = p[l - 1]; n       if (l > i && cnt) { n         cnt--; n         sum -= v[i].first; n       } n       if (cnt > n - 2) { n         continue; n       } n       long long ret = v[i].first + v[i].second * (n - cnt - 1) + sum; n       ans = max(ans, ret); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string equalString(string a) { n   int len = a.size(); n   if (len % 2 == 1) return a; n   string a1 = a.substr(0, len / 2); n   string a2 = a.substr(len / 2, len / 2); n   a1 = equalString(a1); n   a2 = equalString(a2); n   return a1.compare(a2) < 0 ? a1 + a2 : a2 + a1; n } n int main() { n   string a, b; n   cin >> a >> b; n   string nor_a = equalString(a); n   string nor_b = equalString(b); n   if (nor_a.compare(nor_b) == 0) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > seq; n const int mod = 1e6; n const long long A = 42344; n const long long B = 333; n int seed = 239; n int mrand() { n   static int now = -1; n   if (now == -1) n     now = seed; n   else n     now = (A * now + B) % mod; n   return now; n } n struct line { n   long long a, b, c; n   int id; n   line() {} n   line(long long A, long long B, long long C, int i) n       : a(A), b(B), c(C), id(i) {} n }; n void run(vector<line> a, int k) { n   int n = (int)a.size(); n   if (k >= n) { n     for (int i = 0; i < n; i++) seq.push_back(make_pair(a[i].id, -1)); n     sort(seq.begin(), seq.end()); n     seq.resize(unique(seq.begin(), seq.end()) - seq.begin()); n     puts( YES ); n     printf( %d n , (int)seq.size()); n     for (int i = 0; i < (int)seq.size(); i++) n       printf( %d %d n , seq[i].first, seq[i].second); n     exit(0); n   } n   if (k == 0) return; n   vector<line> go; n   for (int iter = 0; iter < k * k; iter++) { n     int i = mrand() % n, j = mrand() % (n - 1); n     if (j >= i) j++; n     if (a[i].a * a[j].b == a[j].a * a[i].b) continue; n     go.resize(0); n     int kol = 0; n     double lim = max(1.0 * kol, n * (1.0 - 1.0 / (2.0 * k))); n     for (int q = 0; q < n; q++) n       if (a[i].a * a[j].b * a[q].c + a[i].b * a[j].c * a[q].a + n               a[i].c * a[j].a * a[q].b != n           a[i].c * a[j].b * a[q].a + a[i].b * a[j].a * a[q].c + n               a[i].a * a[j].c * a[q].b) { n         go.push_back(a[q]); n         kol++; n         if (kol > lim) break; n       } n     if (kol > lim) continue; n     seq.push_back(make_pair(a[i].id, a[j].id)); n     run(go, k - 1); n     seq.pop_back(); n   } n } n int main() { n   srand(123654); n   int n, k; n   scanf( %d%d , &n, &k); n   vector<line> l(n); n   for (int i = 0; i < n; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     l[i] = line(a, b, c, i + 1); n   } n   run(l, k); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string express(int layers) { n   string ans =   ; n   for (int i = 1; i <= layers; i++) { n     if (i % 2 == 1) { n       if (i == layers) { n         ans +=  I hate it ; n       } else { n         ans +=  I hate that  ; n       } n     } else { n       if (i == layers) { n         ans +=  I love it ; n       } else { n         ans +=  I love that  ; n       } n     } n   } n   return ans; n } n int main() { n   int layers; n   cin >> layers; n   cout << express(layers); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long> vec; n   vec.push_back(0LL); n   for (int i = 0; i < n; i++) { n     long long k; n     cin >> k; n     vec.push_back(k); n   } n   int m; n   cin >> m; n   vector<long long> v; n   long long x = 0, y = 0; n   for (int i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     if (vec[a] >= y) { n       v.push_back(vec[a]); n       y = vec[a] + b; n     } else { n       v.push_back(y); n       y += b; n     } n   } n   for (int i = 0; i < m; i++) cout << v[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx1 = 20; n const int mx2 = 405; n const int mx3 = 2005; n const int mx4 = 30005; n const int mx5 = 100005; n const int mx6 = 1000005; n int _toggle(int N, int pos) { return N = N ^ (1 << pos); } n int _set(int N, int pos) { return N = N | (1 << pos); } n int _reset(int N, int pos) { return N = N & ~(1 << pos); } n bool _check(int N, int pos) { return (bool)(N & (1 << pos)); } n bool _upper(char a) { return a >=  A  && a <=  Z ; } n bool _lower(char a) { return a >=  a  && a <=  z ; } n bool _digit(char a) { return a >=  0  && a <=  9 ; } n int dx[] = {1, -1, 0, 0, -1, -1, 1, 1}; n int dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; n const int inf = 1e9 + 7; n int n, a[mx5]; n int fnd(int x, int y) { n   int cnt = (x != 0) + (y != 0); n   int A = a[1] + x, B = a[2] + y, d = B - A, pre = B; n   for (int i = 3; i <= n; i++) { n     bool f = 0; n     for (int j = -1; j < 2; j++) { n       int now = a[i] + j; n       if (now - pre == d) { n         f = 1; n         pre = now; n         cnt += (j != 0); n         break; n       } n     } n     if (!f) { n       return inf; n     } n   } n   return cnt; n } n void solve() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   if (n < 3) { n     cout << 0 <<   n ; n     return; n   } n   int ans = inf; n   for (int i = -1; i < 2; i++) { n     for (int j = -1; j < 2; j++) { n       ans = min(ans, fnd(i, j)); n     } n   } n   if (ans == inf) { n     cout << -1 <<   n ; n   } else { n     cout << ans <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int tc = 1; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long l, r, lim[60]; n long long f[60][60][2][2]; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     (x *= 10) += ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long dfs(long long at, long long sum, bool is_lim, bool is_pre) { n   if (at == 0) return sum <= 3 && !is_pre; n   if (f[at][sum][is_lim][is_pre] != -1) return f[at][sum][is_lim][is_pre]; n   long long mx = is_lim ? lim[at] : 9, res = 0; n   for (long long i = 0; i <= mx; i++) { n     if (i == 0 && is_pre) n       res += dfs(at - 1, sum, is_lim && i == mx, is_pre); n     else n       res += dfs(at - 1, sum + (i != 0), is_lim && i == mx, 0); n   } n   return f[at][sum][is_lim][is_pre] = res; n } n long long solve(long long x) { n   memset(f, -1, sizeof(f)); n   memset(lim, 0, sizeof(lim)); n   long long mx_pos = 0; n   while (x) lim[++mx_pos] = x % 10, x /= 10; n   return dfs(mx_pos, 0, 1, 1); n } n signed main() { n   long long T = read(); n   while (T--) { n     l = read(), r = read(); n     printf( %lld n , solve(r) - solve(l - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000100, mod = 998244353; n inline int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= ch ==  - , ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n int n, el, head[maxn], to[maxn * 2], nxt[maxn * 2], len[maxn], fa[maxn], n     son[maxn], ans[maxn], *f[maxn]; n inline void add(int u, int v) { n   to[++el] = v; n   nxt[el] = head[u]; n   head[u] = el; n } n void dfs1(int u, int f) { n   fa[u] = f; n   for (int i = head[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (v == f) continue; n     dfs1(v, u); n     if (len[v] + 1 > len[u]) son[u] = v, len[u] = len[v] + 1; n   } n } n void dfs2(int u) { n   if (son[fa[u]] != u) f[u] = new int[len[u] + 5](); n   if (son[u]) { n     f[son[u]] = f[u] + 1; n     dfs2(son[u]); n     if (f[u][ans[son[u]] + 1] > 1) ans[u] = ans[son[u]] + 1; n   } n   f[u][0]++; n   for (int i = head[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (v == fa[u] || v == son[u]) continue; n     dfs2(v); n     for (int j = (0); j <= (len[v]); j++) { n       f[u][j + 1] += f[v][j]; n       if (f[u][j + 1] == f[u][ans[u]]) ans[u] = min(ans[u], j + 1); n       if (f[u][j + 1] > f[u][ans[u]]) ans[u] = j + 1; n     } n   } n } n int main() { n   n = read(); n   for (int i = (1); i <= (n - 1); i++) { n     int u = read(), v = read(); n     add(u, v); n     add(v, u); n   } n   dfs1(1, 0); n   dfs2(1); n   for (int i = (1); i <= (n); i++) n     if (son[fa[i]] != i) delete[] f[1]; n   for (int i = (1); i <= (n); i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long N[100006]; n long long sum[1000006], S[1000006]; n long long t[1000006]; n long long t1[1000006]; n long long lowbit(long long u) { return u & (-u); } n void add(long long u, long long w) { n   while (u <= n) { n     t[u] += w; n     u += lowbit(u); n   } n } n long long Find(long long u) { n   long long ans = 0; n   while (u) { n     ans += t[u]; n     u -= lowbit(u); n   } n   return ans; n } n void add1(long long u, long long w) { n   while (u <= n) { n     t1[u] += w; n     u += lowbit(u); n   } n } n long long Find1(long long u) { n   long long ans = 0; n   while (u) { n     ans += t1[u]; n     u -= lowbit(u); n   } n   return ans; n } n double ans; n signed main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) scanf( %lld , &N[i]); n   for (long long i = 1; i <= n; i++) { n     sum[i] += Find(n) - Find(N[i]); n     S[i] += Find1(n) - Find1(N[i]); n     if (sum[i] != 0) n       ans += ((double)(n * (n + 1ll) / 2ll) * sum[i] - n               (double)(n - i + 1ll) * S[i]) / n              (double)(n * (n + 1ll) / 2ll); n     add(N[i], 1); n     add1(N[i], i); n   } n   for (long long i = 2; i <= n; i++) n     ans += (double)((i - 1) * i) / 2.00 / 2.00 * (double)(n - i + 1) / n            (double)(n * (n + 1ll) / 2ll); n   printf( %.12lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, nq, tr, tg, z, tot, root; n int ls[7000010], rs[7000010], s[7000010]; n long long mod, ans, x, y; n long long b[100010], a[100010]; n void push_down(int p) { n   if (!ls[p]) ls[p] = ++tot; n   if (!rs[p]) rs[p] = ++tot; n   if (s[ls[p]] == 0 || s[p] < s[ls[p]]) s[ls[p]] = s[p]; n   if (s[rs[p]] == 0 || s[p] < s[rs[p]]) s[rs[p]] = s[p]; n   s[p] = 0; n } n void change(int &p, long long l, long long r) { n   if (l > y || r < x) return; n   if (!p) p = ++tot; n   if (l >= x && r <= y) { n     if (s[p] == 0 || z < s[p]) s[p] = z; n     return; n   } n   if (s[p]) push_down(p); n   change(ls[p], l, (l + r) / 2); n   change(rs[p], (l + r) / 2 + 1, r); n } n int query(int &p, long long l, long long r) { n   if (!p) p = ++tot; n   if (l == r) return s[p]; n   if (s[p]) push_down(p); n   if (x <= (l + r) / 2) n     return query(ls[p], l, (l + r) / 2); n   else n     return query(rs[p], (l + r) / 2 + 1, r); n } n int main() { n   int i, j, q; n   long long tx, ty; n   scanf( %d%d%d , &n, &tg, &tr); n   mod = tg + tr; n   for (i = 1; i <= n + 1; i++) scanf( %I64d , &a[i]); n   for (i = 1; i <= n + 1; i++) a[i] += a[i - 1]; n   for (i = n; i >= 1; i--) { n     x = mod - a[i] % mod; n     y = query(root, 1, mod); n     if (!y) n       b[i] = 0; n     else n       b[i] = b[y] + (mod - (a[y] - a[i]) % mod); n     tx = ((tg - a[i]) % mod + mod - 1) % mod + 1; n     ty = ((mod - 1 - a[i]) % mod + mod - 1) % mod + 1; n     z = i; n     if (tx <= ty) { n       x = tx; n       y = ty; n       change(root, 1, mod); n     } else { n       x = 1; n       y = ty; n       change(root, 1, mod); n       x = tx; n       y = mod; n       change(root, 1, mod); n     } n   } n   scanf( %d , &nq); n   for (q = 1; q <= nq; q++) { n     scanf( %d , &x); n     ans = x + a[n + 1]; n     x = (x - 1) % mod + 1; n     y = query(root, 1, mod); n     if (y) ans += mod - (a[y] + x) % mod + b[y]; n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n istream &operator>>(istream &in, vector<T> &arr) { n   for (auto &x : arr) in >> x; n   return in; n } n long long n, m; n const long long N = 1e6 + 100; n vector<pair<long long, long long>> g[N]; n long long col[N]; n bool cyc; n long long res[N]; n void dfs(long long r) { n   col[r] = 1; n   for (auto x : g[r]) { n     long long to = x.first, ind = x.second; n     if (col[to] == 0) { n       res[ind] = 1; n       dfs(to); n     } else if (col[to] == 1) { n       res[ind] = 2; n       cyc = true; n     } else { n       res[ind] = 1; n     } n   } n   col[r] = 2; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (long long i = 0; i < m; ++i) { n     long long u, v; n     cin >> u >> v; n     --u, --v; n     g[u].push_back({v, i}); n   } n   for (long long i = 0; i < n; ++i) { n     if (col[i] == 0) { n       dfs(i); n     } n   } n   cout << (cyc ? 2 : 1) <<   n ; n   for (long long i = 0; i < m; ++i) { n     cout << res[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s1[1010000]; n char s2[1010000]; n int main() { n   scanf( %s , s1); n   scanf( %s , s2); n   int len1, len2, i; n   len1 = strlen(s1); n   len2 = strlen(s2); n   int ans; n   if (len1 - len2) n     ans = 0; n   else if (len1 == 1) { n     if (s1[0] == s2[0]) n       ans = 1; n     else n       ans = 0; n   } else { n     if (strcmp(s1, s2) == 0) n       ans = 1; n     else { n       int j1, j2; n       j1 = j2 = 0; n       for (i = 0; s1[i]; ++i) n         if (s1[i] ==  1 ) j1 = 1; n       for (i = 0; s2[i]; ++i) n         if (s2[i] ==  1 ) j2 = 1; n       if (j1 && j2) n         ans = 1; n       else n         ans = 0; n     } n   } n   if (ans) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, lb[300010], ind[300010]; n vector<int> g[300010], g2[300010]; n int val[300010]; n bool viz[300010]; n int dfs(int nod, int v) { n   val[nod] = v; n   viz[nod] = 1; n   for (int nxt : g2[nod]) { n     if (viz[nxt]) continue; n     return dfs(nxt, v + 1); n   } n   return nod; n } n int main() { n   cin >> N >> M; n   int i, x, y; n   for (i = 1; i <= M; i++) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (i = 1; i <= N; i++) { n     g[i].push_back(i); n     sort(g[i].begin(), g[i].end()); n     ind[i] = i; n   } n   sort(ind + 1, ind + N + 1, [](int x, int y) { return g[x] < g[y]; }); n   lb[ind[1]] = 1; n   for (i = 2; i <= N; i++) n     if (g[ind[i]] == g[ind[i - 1]]) n       lb[ind[i]] = lb[ind[i - 1]]; n     else n       lb[ind[i]] = 1 + lb[ind[i - 1]]; n   for (i = 1; i <= N; i++) n     for (int nxt : g[i]) { n       if (lb[nxt] == lb[i]) continue; n       g2[lb[i]].push_back(lb[nxt]); n     } n   int cr = dfs(1, 1); n   memset(val, 0, sizeof(val)); n   memset(viz, 0, sizeof(viz)); n   dfs(cr, 1); n   for (i = 1; i <= lb[ind[N]]; i++) n     if (val[i] == 0) { n       cout <<  NO n ; n       return 0; n     } n   cout <<  YES n ; n   for (i = 1; i <= N; i++) cout << val[lb[i]] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 9223372036854775807LL; n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroint-loops ) n long long int power(long long int b, long long int e, long long int m) { n   if (b == 0) return 0; n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b % m, e / 2, m) % m; n   return power(b * b % m, e / 2, m); n } n long long int power(long long int b, long long int e) { n   if (b == 0) return 0; n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b, e / 2); n   return power(b * b, e / 2); n } n bool isPowerOfTwo(long long int x) { return (x && !(x & (x - 1))); } n void solve() { n   long long int n, m; n   cin >> n >> m; n   vector<long long int> v(n + 1); n   cin >> v[1]; n   for (long long int i = 2; i <= n; i++) { n     cin >> v[i]; n     v[i] += v[i - 1]; n   } n   long long int ans = 0; n   while (m--) { n     long long int a, b; n     cin >> a >> b; n     if (v[b] - v[a - 1] >= 0) ans += v[b] - v[a - 1]; n   } n   cout << ans; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int tt = 1; n   for (long long int i = 1; i <= tt; i++) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 55; n int x[MAX]; n long long poww(long long x, long long y) { n   if (y == 0) return 1; n   long long s = poww(x, y / 2); n   s = (s * s); n   if (y % 2 == 1) s = (s * x); n   return s; n } n int main() { n   string s =  OYGR ; n   string c =  BIV ; n   int t = 0; n   int n; n   cin >> n; n   string b =   ; n   for (int i = 0; i < n - 3; i++) { n     if (t == 4) t = 0; n     b += s[t]; n     t++; n   } n   cout << b + c; n   return 0; n } n


#include <bits/stdc++.h> n int a[1511]; n int main() { n   int n, m, i, j, s(0); n   scanf( %d , &n); n   for (i = 0; i < n; ++i) scanf( %d , &a[i]); n   for (i = 0; i < n; ++i) n     for (j = i + 1; j < n; ++j) n       if (a[i] > a[j]) s ^= 1; n   scanf( %d , &m); n   while (m--) { n     scanf( %d%d , &i, &j); n     if ((j - i + 1) * (j - i) / 2 & 1) s ^= 1; n     printf( %s n , s ?  odd  :  even ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1000000000 ) n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) n using namespace std; n const int maxn = (int)1e5 + 10; n struct vt { n   long double x, y; n }; n vt operator-(vt a, vt b) { n   a.x -= b.x; n   a.y -= b.y; n   return a; n } n vt operator+(vt a, vt b) { n   a.x += b.x; n   a.y += b.y; n   return a; n } n vt operator*(vt a, long double x) { n   a.x *= x; n   a.y *= x; n   return a; n } n long double dst(vt a) { return hypotl(a.x, a.y); } n vt operator/(vt a, long double x) { n   a.x /= x; n   a.y /= x; n   return a; n } n const long double eps = 1e-18; n vector<vt> inter(vt a, long double r) { n   long double d = dst(a); n   if (d * d <= r * r) { n     return {}; n   } n   long double x = r * r / d; n   long double y = r * sqrt(1. - (r / d) * (r / d)); n   vt b = a * (x / dst(a)); n   vt n = {-a.y, a.x}; n   n = n / dst(n); n   return {b - n * y, b + n * y}; n } n long double operator*(vt a, vt b) { return a.x * b.y - a.y * b.x; } n int tp(vt a) { return a.y > eps || (abs(a.y) < eps && a.x > eps); } n vt v[maxn]; n bool operator<(vt a, vt b) { n   int tpa = tp(a); n   int tpb = tp(b); n   if (tpa != tpb) { n     return tpa < tpb; n   } n   return a * b > eps; n } n int tr[4 * maxn]; n void change(int pos) { n   for (; pos < 4 * maxn; pos |= pos + 1) { n     tr[pos]++; n   } n } n int go(int pos) { n   int ans = 0; n   for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { n     ans += tr[pos]; n   } n   return ans; n } n int go(int l, int r) { return go(r) - go(l - 1); } n ostream& operator<<(ostream& out, vt a) { return out << a.x <<     << a.y; } n mt19937 rr; n int main() { n   int n; n   long long k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> v[i].x >> v[i].y; n   } n   k = (long long)n * (n - 1) / 2 - k; n   long double l = 0; n   long double r = sqrt(2.) * 1e4; n   for (int itr = 0; itr < 36; itr++) { n     long double m = (l + r) / 2; n     vector<vt> g; n     vector<pair<vt, vt> > f; n     for (int i = 0; i < n; i++) { n       auto a = inter(v[i], m); n       if (a.empty()) { n         continue; n       } n       f.push_back(make_pair(a[0], a[1])); n       g.push_back(a[0]); n       g.push_back(a[1]); n     } n     sort((g).begin(), (g).end()); n     vector<pair<int, int> > a; n     memset(tr, 0, sizeof tr); n     for (int i = 0; i < (int)f.size(); i++) { n       a.push_back(make_pair( n           lower_bound((g).begin(), (g).end(), f[i].first) - g.begin(), n           lower_bound((g).begin(), (g).end(), f[i].second) - g.begin())); n       if (a.back().first > a.back().second) { n         change(a.back().second); n         a.back().second += (int)g.size(); n       } n     } n     sort((a).begin(), (a).end()); n     long long tot = 0; n     for (int i = 0; i < (int)a.size(); i++) { n       tot += go(a[i].first, a[i].second); n       change(a[i].second); n     } n     if (tot <= k) { n       r = m; n     } else { n       l = m; n     } n   } n   printf( %.10lf n , (double)r); n } n


#include<bits/stdc++.h> n  n using namespace std; n  n #define mp make_pair n #define fi first n #define se second n #define pb push_back n #define FOR(i,a,b) for(int i=a;i<=b;i++) n #define FORD(i,a,b) for(int i=a;i>=b;i--) n #define faster() ios_base::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL); n  n typedef long long ll; n typedef vector<int> vi; n typedef vector<ll> vll; n  n int main() n { n     faster(); n     int t; n     cin>>t; n     while(t--) n     { n         int a[4]; n         FOR(i,0,3) cin>>a[i]; n         int max1=max(a[0],a[1]),max2=max(a[2],a[3]); n         sort(a,a+4); n         if(max1>max2 && max1==a[3] && max2==a[2]) cout<< YES <<endl; n         else if(max1<max2 && max2==a[3] && max1==a[2]) cout<< YES <<endl; n         else cout<< NO <<endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long sum = 0, ff = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) { n       ff = -1; n     } n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     sum = sum * 10 + ch - 48; n     ch = getchar(); n   } n   return sum * ff; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 1000007; n int n, m; n struct edge { n   int from, to, dis, next; n } e[N], a[N]; n int he[N], cnt; n void add(int u, int v, int w) { n   e[++cnt].from = u; n   e[cnt].to = v; n   e[cnt].dis = w; n   e[cnt].next = he[u]; n   he[u] = cnt; n } n bool cmp(edge x, edge y) { n   if (x.from == y.from) return x.to < y.to; n   return x.from < y.from; n } n int col[N]; n queue<int> q; n struct node { n   int pos, dis; n }; n queue<node> que; n bool vis[N]; n int bfs() { n   que.push((node){n, 0}); n   vis[n] = true; n   while (!que.empty()) { n     node now = que.front(); n     que.pop(); n     if (now.pos == 1) return now.dis; n     for (int i = he[now.pos]; i; i = e[i].next) { n       int v = e[i].to; n       if (vis[v]) continue; n       if (col[v] == -1 || col[v] == 1 - e[i].dis) n         col[v] = 1 - e[i].dis; n       else { n         vis[v] = true; n         que.push((node){v, now.dis + 1}); n       } n     } n   } n   return -1; n } n map<pair<int, int>, bool> mp; n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     a[i].from = read(), a[i].to = read(), a[i].dis = read(); n     add(a[i].to, a[i].from, a[i].dis); n   } n   memset(col, -1, sizeof(col)); n   write(bfs()), puts(  ); n   for (int i = 1; i <= n; i++) n     if (col[i] == -1) n       printf( 0 ); n     else n       write(col[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2010; n struct point { n   point(int xx, int yy) : x(xx), y(yy) {} n   int x, y; n }; n int n, m; n char a[N][N]; n queue<point> q; n int dx[4] = {-1, 1, 0, 0}; n int dy[4] = {0, 0, -1, 1}; n long long pp; n bool uniq(int xx, int yy) { n   if (a[xx][yy] !=  . ) { n     return false; n   } n   int counter = 0; n   for (int i = 0; i < 4; ++i) { n     int x = xx + dx[i]; n     int y = yy + dy[i]; n     if (a[x][y] ==  . ) { n       ++counter; n     } n   } n   return (counter == 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i <= n + 1; ++i) { n     for (int j = 0; j <= m + 1; ++j) { n       if (i == 0 || j == 0 || i == n + 1 || j == m + 1) { n         a[i][j] =  * ; n         continue; n       } n       cin >> a[i][j]; n       if (a[i][j] ==  . ) { n         ++pp; n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (a[i][j] ==  * ) { n         continue; n       } n       int counter = 0; n       if (a[i][j + 1] ==  . ) { n         ++counter; n       } n       if (a[i][j - 1] ==  . ) { n         ++counter; n       } n       if (a[i - 1][j] ==  . ) { n         ++counter; n       } n       if (a[i + 1][j] ==  . ) { n         ++counter; n       } n       if (counter == 0) { n         cout <<  Not unique ; n         return 0; n       } n       if (counter == 1) { n         q.push(point(i, j)); n       } n     } n   } n   while (true) { n     if (q.empty()) { n       break; n     } n     point cur = q.front(); n     q.pop(); n     if (a[cur.x][cur.y] !=  . ) { n       continue; n     } n     if (!uniq(cur.x, cur.y)) { n       cout <<  Not unique ; n       return 0; n     } n     int xx = cur.x; n     int yy = cur.y; n     for (int i = 0; i < 4; ++i) { n       xx += dx[i]; n       yy += dy[i]; n       if (a[xx][yy] ==  . ) { n         break; n       } n       xx -= dx[i]; n       yy -= dy[i]; n     } n     if (yy == cur.y) { n       if (xx < cur.x) { n         a[cur.x][cur.y] =  v ; n         a[xx][yy] =  ^ ; n       } else { n         a[xx][yy] =  v ; n         a[cur.x][cur.y] =  ^ ; n       } n     } n     if (xx == cur.x) { n       if (yy < cur.y) { n         a[cur.x][cur.y] =  > ; n         a[xx][yy] =  < ; n       } else { n         a[cur.x][cur.y] =  < ; n         a[xx][yy] =  > ; n       } n     } n     pp -= 2; n     for (int i = 0; i < 4; ++i) { n       if (uniq(xx + dx[i], yy + dy[i])) { n         q.push(point(xx + dx[i], yy + dy[i])); n       } n     } n   } n   if (pp > 0) { n     cout <<  Not unique ; n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cout << a[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, ans; n map<long long, long long> m, ind; n long long b[150001], a; n int main() { n   scanf( %d , &n); n   ans = n; n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &a); n     m[a]++; n     b[i] = a; n     while (m[a] > 1) { n       ans--; n       m[a] = 0; n       b[ind[a]] = 0; n       a *= 2; n       b[i] = a; n       m[a]++; n     } n     ind[a] = i; n   } n   printf( %d n , ans); n   for (i = 1; i <= n; i++) n     if (b[i] > 0) printf( %I64d  , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, string> > > regions; n int main() { n   int n, m; n   cin >> n >> m; n   regions.resize(m); n   string name; n   int r, s; n   for (int i = 0; i < n; ++i) { n     cin >> name >> r >> s; n     regions[r - 1].push_back(make_pair(s, name)); n   } n   for (int i = 0; i < m; ++i) { n     sort(regions[i].rbegin(), regions[i].rend()); n     if (regions[i].size() == 2 || regions[i][1].first > regions[i][2].first) n       cout << regions[i][0].second <<     << regions[i][1].second << endl; n     else n       cout <<  ?  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 2e5 + 7; n const long long INF = 3e18 + 7; n long long n, t; n long long a[MAXN], p[MAXN], l[MAXN], r[MAXN]; n long long ans[MAXN]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> t; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   for (long long i = 0; i < n; ++i) { n     cin >> p[i]; n     --p[i]; n   } n   for (long long i = 0; i < n - 1; ++i) { n     if (p[i + 1] < p[i]) { n       cout <<  No n ; n       exit(0); n     } n   } n   for (long long i = 0; i < n; ++i) { n     l[i] = 1; n     r[i] = INF; n   } n   for (long long i = 0; i < n; ++i) { n     l[p[i]] = max(l[p[i]], a[i] + t); n   } n   for (long long i = 0; i < n; ++i) { n     l[i] = max(l[i], a[i] + t); n   } n   long long mx = -1; n   for (long long i = 0; i < n - 1; ++i) { n     mx = max(mx, p[i]); n     if (i < mx) { n       l[i] = max(l[i], a[i + 1] + t); n     } n   } n   for (long long i = 0; i < n - 1; ++i) { n     if (p[i] < i) { n       r[p[i]] = min(r[p[i]], a[p[i]] + t - 1); n     } else { n       r[p[i]] = min(r[p[i]], a[p[i] + 1] + t - 1); n     } n   } n   for (long long i = 0; i < n; ++i) { n     if (!i) n       ans[i] = l[i]; n     else n       ans[i] = max(ans[i - 1] + 1, l[i]); n   } n   for (long long i = 0; i < n - 1; ++i) { n     if (ans[i + 1] <= a[i]) { n       cout <<  No n ; n       exit(0); n     } n     if (!(l[i] <= ans[i] && ans[i] <= r[i])) { n       cout <<  No n ; n       exit(0); n     } n   } n   cout <<  Yes n ; n   for (long long i = 0; i < n; ++i) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void JIZZ(string output =   ) { n   cout << output; n   exit(0); n } n const long double PI = 3.14159265358979323846264338327950288; n const long double eps = 1e-13; n const long long mod = 1e9 + 7; n struct node { n   node *l, *r; n   int mx; n   node() : l(0), r(0), mx(-(1 << 30)) {} n } * root, pool[100000]; n int pptr = -1; n int a[20005], pre[20005], dp[53][20005]; n node *nnode() { n   ++pptr; n   pool[pptr].l = pool[pptr].r = 0; n   pool[pptr].mx = -(1 << 30); n   return &pool[pptr]; n } n void build(node *now, int l, int r) { n   if (l == r) return; n   build(now->l = nnode(), l, ((l + r) >> 1)); n   build(now->r = nnode(), ((l + r) >> 1) + 1, r); n } n void modify(node *now, int l, int r, int x, int v) { n   if (l == r) { n     now->mx = max(now->mx, v); n     return; n   } n   if (x <= ((l + r) >> 1)) n     modify(now->l, l, ((l + r) >> 1), x, v); n   else n     modify(now->r, ((l + r) >> 1) + 1, r, x, v); n   now->mx = max(now->l->mx, now->r->mx); n } n int query(node *now, int l, int r, int ql, int qr) { n   ; n   ; n   if (qr < l || r < ql) return -(1 << 30); n   if (ql <= l && r <= qr) return now->mx; n   return max(query(now->l, l, ((l + r) >> 1), ql, qr), n              query(now->r, ((l + r) >> 1) + 1, r, ql, qr)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, k, p; n   cin >> n >> k >> p; n   for (int i = 1; i <= n; ++i) cin >> a[i], a[i] %= p; n   for (int i = 1; i <= n; ++i) pre[i] = (pre[i - 1] + a[i]) % p; n   for (int i = 1; i <= n; ++i) dp[0][i] = -(1 << 30); n   for (int i = 1; i <= k; ++i) { n     pptr = -1; n     build(root = nnode(), 0, p - 1); n     modify(root, 0, p - 1, 0, 0); n     for (int j = 1; j <= n; ++j) { n       dp[i][j] = pre[j] + query(root, 0, p - 1, 0, pre[j]); n       if (pre[j] != p - 1) n         dp[i][j] = max(dp[i][j], n                        pre[j] + query(root, 0, p - 1, pre[j] + 1, p - 1) + p); n       modify(root, 0, p - 1, pre[j], -pre[j] + dp[i - 1][j]); n       ; n       ; n     } n   } n   cout << dp[k][n] << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n int n, cnt; n int ans[1005]; n bool prime(int x) { n   for (int i = 2; i < x; i++) n     if (x % i == 0) return 0; n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     if (!prime(i)) continue; n     int p = i; n     while (p <= n) { n       ans[++cnt] = p; n       p *= i; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i < cnt; i++) printf( %d  , ans[i]); n   if (cnt) printf( %d n , ans[cnt]); n } n


#include <bits/stdc++.h> n using namespace std; n int NewMessages(int right, int left, int k, int size) { n   int right_border = min(right + k, size - 1); n   int left_border = max(min(left + k, size - 1), max(0, right - k - 1)); n   return right_border - left_border; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> messages(n); n   for (auto& x : messages) { n     cin >> x; n     --x; n   } n   vector<int> counts(n, 0); n   counts[0] = min(static_cast<int>(counts.size()) - 1, k) + 1; n   for (int i = 1; i < counts.size(); ++i) { n     if (messages[i] == -1) { n       counts[i] = n           min(static_cast<int>(counts.size()) - 1, i + k) - max(0, i - k) + 1; n     } else { n       counts[i] = n           counts[messages[i]] + n           NewMessages(i, messages[i], k, static_cast<int>(counts.size())); n     } n   } n   for (auto x : counts) { n     cout << x <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const long long INFLL = 0x3f3f3f3f3f3f3f3fll; n const int maxn = 2e5 + 5; n const int mod = 1e9 + 7; n int n, k; n char ans[55][3]; n int main() { n   cin >> n >> k; n   char ch1 =  A , ch2 =  a ; n   for (int i = 0; i < n - k + 1; i++) { n     string s; n     cin >> s; n     if (i == 0) { n       if (s ==  YES ) { n         for (int j = i; j < i + k; j++) { n           ans[j][0] = ch1; n           ans[j][1] = ch2++; n           if (ch2 >  z ) ch1 =  B , ch2 =  a ; n         } n       } else { n         ans[i][0] = ans[i + k - 1][0] = ch1; n         ans[i][1] = ans[i + k - 1][1] = ch2++; n         for (int j = i + 1; j < i + k - 1; j++) { n           ans[j][0] = ch1; n           ans[j][1] = ch2++; n           if (ch2 >  z ) ch1 =  B , ch2 =  a ; n         } n       } n     } else { n       if (s ==  YES ) { n         ans[i + k - 1][0] = ch1; n         ans[i + k - 1][1] = ch2++; n       } else { n         ans[i + k - 1][0] = ans[i][0]; n         ans[i + k - 1][1] = ans[i][1]; n       } n       if (ch2 >  z ) ch1 =  B , ch2 =  a ; n     } n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i][0] << ans[i][1] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a[100][100], ans, ma[100]; n bool mark[10]; n int seen[100000]; n void BT(int n) { n   if (n > ::n) { n     int ans = 0; n     for (int i = 1; i <= ::n; i++) { n       if (a[i][ma[i]] == 0 && ma[i] != 0) return; n       ans += a[i][ma[i]]; n     } n     seen[ans]++; n     return; n   } n   for (int i = 0; i <= ::n; i++) { n     if (i == 0 || !mark[i]) { n       ma[n] = i; n       mark[i] = true; n       BT(n + 1); n       ma[n] = 0; n       mark[i] = false; n     } n   } n   return; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     int A, b, c; n     cin >> A >> b >> c; n     a[A][b] = c; n   } n   int t = 0, i = 0; n   BT(1); n   seen[0] = 1; n   while (t < k) { n     if (seen[i]) t += seen[i]; n     i++; n   } n   cout << i - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n int f[maxn], n; n struct struc { n   long long a[maxn]; n   int go[19][maxn], nxt[maxn]; n   void build(int ty = 0) { n     if (!ty) { n       for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n       for (int i = n; i; i--) a[i] += a[i + 1]; n     } n     static map<long long, int> pos; n     pos.clear(); n     pos[0] = n + 1; n     for (int i = n; i; i--) nxt[i] = pos[a[i]], pos[a[i]] = i; n   } n } sav[3]; n vector<pair<int, int> > up[maxn], dow[maxn]; n int c[maxn]; n void push(int i, int j, int v) { n   c[max(i, j)] = max(c[max(i, j)], v); n   if (i <= j) up[i].push_back(pair<int, int>(j, v)); n   if (j <= i) dow[j].push_back(pair<int, int>(i, v)); n } n void trans(int i, int j, int v) { n   push(i + 1, j, v), push(i, j + 1, v); n   if (sav[0].nxt[i]) push(sav[0].nxt[i], j, v + 1); n   if (sav[1].nxt[j]) push(i, sav[1].nxt[j], v + 1); n   if (i == j && sav[2].nxt[i]) push(sav[2].nxt[i], sav[2].nxt[i], v + 1); n } n int main() { n   scanf( %d , &n); n   sav[0].build(), sav[1].build(); n   for (int i = 1; i <= n; i++) sav[2].a[i] = sav[0].a[i] + sav[1].a[i]; n   sav[2].build(1); n   for (int i = 1; i <= n; i++) { n     int go_up = (1 << 30), go_dow = (1 << 30); n     trans(i, i, c[i]); n     for (auto p : up[i]) n       if (p.second == c[i] + 1) go_dow = min(go_dow, p.first); n     for (auto p : dow[i]) n       if (p.second == c[i] + 1) go_up = min(go_up, p.first); n     if (go_up <= n + 1) trans(go_up, i, c[i] + 1); n     if (go_dow <= n + 1) trans(i, go_dow, c[i] + 1); n   } n   printf( %d n , c[n + 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l; n   string s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> l >> s; n     int first = s[0] -  0 ; n     int second = s[1] -  0 ; n     if (s.length() == 2 && first >= second) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     cout << 2 << endl; n     cout << s[0] <<     << s.substr(1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename arg> n void debugger(const char* varname, arg&& var) { n   cout << varname <<   =   << var << endl; n } n template <typename arg, typename... args> n void debugger(const char* varnames, arg&& var, args&&... vars) { n   const char* comma = strchr(varnames + 1,  , ); n   cout.write(varnames, comma - varnames) <<   =   << var <<   |  ; n   int sz = 1; n   if (isspace(comma[sz])) sz++; n   debugger(comma + sz, vars...); n } n using ll = long long; n using ii = pair<ll, ll>; n using vi = vector<ll>; n extern const int INF = INT_MAX - 5; n extern const ll LINF = LLONG_MAX - 5; n extern const int MOD = 1e9 + 7; n extern const int N = 2e5 + 200; n int t, n; n string s; n vi find_min() { n   vi ret(n), pos; n   if (s[0] ==  < ) pos.emplace_back(0); n   for (int i = 1; i < (int)s.size(); ++i) { n     if (s[i - 1] ==  >  and s[i] ==  < ) { n       pos.emplace_back(i); n     } n   } n   if (s[(int)s.size() - 1] ==  > ) pos.emplace_back(n - 1); n   int c = 1; n   for (int i = (int)pos.size() - 1; i >= 0; --i) { n     ret[pos[i]] = c; n     c++; n   } n   for (int i = pos[pos.size() - 1] + 1; i < n; ++i) { n     ret[i] = c; n     c++; n   } n   for (int i = (int)pos.size() - 1; i >= 1; --i) { n     int r = pos[i] - 1, l = pos[i - 1]; n     while (s[r] ==  >  and s[r - 1] ==  > ) { n       ret[r] = c; n       r--, c++; n     } n     while (s[l] ==  <  and s[l + 1] ==  < ) { n       ret[l + 1] = c; n       l++, c++; n     } n     ret[l + 1] = c; n     c++; n   } n   for (int i = pos[0] - 1; i >= 0; --i) { n     ret[i] = c; n     c++; n   } n   return ret; n } n vi find_max() { n   vi ret(n); n   vector<ii> rng; n   int ant = 0; n   for (int i = 0; i < (int)s.size(); ++i) { n     if (s[i] ==  < ) { n       rng.emplace_back(ant, i); n       ant = i + 1; n     } n   } n   rng.emplace_back(ant, n - 1); n   int c = 1; n   for (auto i : rng) { n     ret[i.second] = c; n     c++; n   } n   for (auto i : rng) { n     for (int k = i.second - 1; k >= i.first; --k) { n       ret[k] = c; n       c++; n     } n   } n   return ret; n } n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> s; n     vi mn = find_min(); n     vi mx = find_max(); n     for (int i = 0; i < n; ++i) { n       if (i != 0) cout <<    ; n       cout << mn[i]; n     } n     cout <<   n ; n     for (int i = 0; i < n; ++i) { n       if (i != 0) cout <<    ; n       cout << mx[i]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int inf = 2e9 + 9; n int n, m, p; n pair<int, int> a[maxn], b[maxn]; n pair<pair<int, int>, int> mon[maxn]; n struct segment_tree { n   int val[maxn * 4], lazy[maxn * 4]; n   void build(int x, int l, int r) { n     if (l == r) { n       val[x] = -b[l].second; n       return; n     } n     int mid = (l + r) / 2; n     build(x * 2, l, mid); n     build(x * 2 + 1, mid + 1, r); n     val[x] = max(val[x * 2], val[x * 2 + 1]); n   } n   void push(int x) { n     if (lazy[x] == 0) return; n     int t = lazy[x]; n     val[x * 2] += t; n     lazy[x * 2] += t; n     val[x * 2 + 1] += t; n     lazy[x * 2 + 1] += t; n     lazy[x] = 0; n   } n   void upd(int x, int l, int r, int L, int R, int k) { n     if (L > r || l > R || L > R) return; n     if (l >= L && r <= R) { n       val[x] += k; n       lazy[x] += k; n       return; n     } n     push(x); n     int mid = (l + r) / 2; n     upd(x * 2, l, mid, L, R, k); n     upd(x * 2 + 1, mid + 1, r, L, R, k); n     val[x] = max(val[x * 2], val[x * 2 + 1]); n   } n   int get() { return val[1]; } n } tree; n void read_input() { n   cin >> n >> m >> p; n   for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second; n   for (int i = 1; i <= m; ++i) cin >> b[i].first >> b[i].second; n   for (int i = 1; i <= p; ++i) { n     int x, y, z; n     cin >> x >> y >> z; n     mon[i] = make_pair(pair<int, int>(x, y), z); n   } n } n void init() { n   sort(a + 1, a + n + 1); n   sort(b + 1, b + m + 1); n   sort(mon + 1, mon + p + 1); n   tree.build(1, 1, m); n } n void solve() { n   int cur = 1; n   int ans = -inf; n   for (int i = 1; i <= n; ++i) { n     int ap = a[i].first; n     while (cur <= p && mon[cur].first.first < ap) { n       int p = upper_bound(b + 1, b + m + 1, n                           pair<int, int>(mon[cur].first.second, inf)) - n               b; n       tree.upd(1, 1, m, p, m, mon[cur].second); n       ++cur; n     } n     ans = max(ans, -a[i].second + tree.get()); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   read_input(); n   init(); n   solve(); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n, w; n   cin >> w >> n; n   if (w <= 3) { n     cout <<  YES  << endl; n     return 0; n   } n   while (n > 0) { n     if (n % w > 1 && n % w < w - 1) { n       cout <<  NO  << endl; n       return 0; n     } n     n = (n + 1) / w; n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Read() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int t = c -  0 ; n   c = getchar(); n   while (c >=  0  && c <=  9 ) t = (t << 3) + (t << 1) + c -  0 , c = getchar(); n   return t; n } n int n, Q, s; n int Head[900001], To[3200001], Next[3200001], t1; n int W[3200001]; n void add(int a, int b, int c) { n   t1++; n   Next[t1] = Head[a]; n   Head[a] = t1; n   To[t1] = b; n   W[t1] = c; n } n void Build(int bh, int x, int y) { n   if (x == y) { n     add(bh, n * 8 + x, 0), add(n * 8 + x, bh, 0); n     add(n * 4 + bh, n * 8 + x, 0), add(n * 8 + x, n * 4 + bh, 0); n     return; n   } n   add(bh, bh * 2, 0), add(bh, bh * 2 + 1, 0); n   add(n * 4 + bh * 2, n * 4 + bh, 0), add(n * 4 + bh * 2 + 1, n * 4 + bh, 0); n   int mid = (x + y) / 2; n   Build(bh * 2, x, mid); n   Build(bh * 2 + 1, mid + 1, y); n } n void add(int bh, int x, int y, int a, int b, int c, bool t, int u) { n   if (x == a && y == b) { n     if (!t) n       add(n * 8 + u, bh, c); n     else n       add(n * 4 + bh, n * 8 + u, c); n     return; n   } n   int mid = (x + y) / 2; n   if (b <= mid) n     add(bh * 2, x, mid, a, b, c, t, u); n   else if (a > mid) n     add(bh * 2 + 1, mid + 1, y, a, b, c, t, u); n   else n     add(bh * 2, x, mid, a, mid, c, t, u), n         add(bh * 2 + 1, mid + 1, y, mid + 1, b, c, t, u); n } n struct Node { n   int id; n   long long d; n   bool operator<(const Node& a) const { return a.d < d; } n } t; n priority_queue<Node> q; n bool vis[900001]; n long long dis[900001]; n void Dij() { n   for (int i = 1; i <= n * 9; i++) dis[i] = 1e18; n   dis[n * 8 + s] = 0; n   t.id = n * 8 + s; n   t.d = 0; n   q.push(t); n   while (!q.empty()) { n     while (!q.empty() && vis[q.top().id]) q.pop(); n     if (q.empty()) break; n     int curr = q.top().id; n     q.pop(); n     vis[curr] = 1; n     for (int i = Head[curr]; i; i = Next[i]) { n       if (!vis[To[i]]) { n         if (dis[To[i]] > dis[curr] + W[i]) { n           dis[To[i]] = dis[curr] + W[i]; n           t.id = To[i]; n           t.d = dis[To[i]]; n           q.push(t); n         } n       } n     } n   } n } n int main() { n   n = Read(); n   Q = Read(); n   s = Read(); n   Build(1, 1, n); n   for (int i = 1, t, u, v, w, l, r; i <= Q; i++) { n     t = Read(); n     if (t == 1) { n       u = Read(); n       v = Read(); n       w = Read(); n       add(n * 8 + u, n * 8 + v, w); n     } else if (t == 2) { n       u = Read(); n       l = Read(); n       r = Read(); n       w = Read(); n       add(1, 1, n, l, r, w, 0, u); n     } else { n       u = Read(); n       l = Read(); n       r = Read(); n       w = Read(); n       add(1, 1, n, l, r, w, 1, u); n     } n   } n   Dij(); n   for (int i = n * 8 + 1; i <= n * 8 + n; i++) { n     if (!vis[i]) n       printf( -1  ); n     else n       printf( %lld  , dis[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = (int)1e9 + 7; n int f(const std::vector<int> &arr, int k, int d) { n   std::list<std::pair<int, int>> events, se; n   for (auto &it : arr) { n     events.push_back({it - d, 0}); n     se.push_back({it + d, 1}); n   } n   events.merge(se); n   int bal = 0; n   for (auto it : events) { n     bal += (it.second ? -1 : +1); n     if (bal > k) { n       return it.first; n     } n   } n   return -INF; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   int t; n   std::cin >> t; n   while (t--) { n     int n, k; n     std::cin >> n >> k; n     std::vector<int> arr(n); n     for (auto &it : arr) { n       std::cin >> it; n     } n     std::sort((arr).begin(), (arr).end()); n     int low = -1, high = (int)1e9; n     while (high - low > 1) { n       int mid = (low + high) / 2; n       if (f(arr, k, mid) > -INF) { n         high = mid; n       } else { n         low = mid; n       } n     } n     std::cout << f(arr, k, high) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   char a[n][m]; n   int num = 0; n   int mn[m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < m; i++) { n     num = 0; n     for (int j = 0; j < n; j++) { n       num = max(num, a[j][i] -  0 ); n     } n     mn[i] = num; n   } n   int c = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     c = 0; n     for (int j = 0; j < m; j++) { n       if (mn[j] == a[i][j] -  0 ) { n         c = 3; n       } n     } n     if (c == 3) { n       ans++; n     } n   } n   cout << ans; n   return 0; n } n