n  n //Shrey Dubey n  n  n #include<iostream> n #include<string> n #include<algorithm> n #include<map> n #include<unordered_map> n #include<vector> n #include<set> n #include<list> n #include<iomanip> n #include<queue> n #include<stack> n #include <math.h> n #include<climits> n #include<bitset> n #include<cstring> n #include<numeric> n #include<array> n  n  n using namespace std; n typedef long long ll; n typedef long double ld; n  n #define YES cout<< YES n  n #define Yes cout<< Yes n  n #define NO cout<< NO n  n #define No cout<< No n  n #define prDouble(x) cout<<fixed<<setprecision(10)<<x //to print decimal numbers n #define pb push_back n #define ff first n #define sec second n #define umap unordered_map n #define mp make_pair n #define KOBE ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n #define fo(n) for(ll i = 0; i<n; i++) n #define fnd(stl, data) find(stl.begin(), stl.end(), data) n #define forn(x,n) for(ll x = 0; x<n; x++) n #define imax INT_MAX n #define lmax LLONG_MAX n #define imin INT_MIN n #define lmin LLONG_MIN n #define vi vector<int> n #define vl vector<ll> n #define vp vector<pair<ll,ll> > n #define vs vector<string> n #define vb vector<bool> n #define pr(t) cout<<t<<  n  n #define int long long n #define ql queue<ll> n #define qp queue<pair<ll,ll> > n #define endl   n  n #define nl cout<<  n  n #define re cin >> n #define pll pair<ll,ll> n #define FOR(a,b) for(ll i = a; i<=b; i++) n #define all(x) x.begin(),x.end() n #define LG 22 n  n // ll dx[] = {1,0,-1,0}; n // ll dy[] = {0,1,0,-1}; n  n ll mod = 1e9 + 7; n  n ll cl(ld a){ n     if(a>(ll) a){ n         return (ll)a+1; n     } n     else{ n         return (ll)a; n     } n } n  n ll flr(ld a){ n     if(a < 0.0){ n         return (ll) a - 1; n     } n     return (ll) a; n } n  n ll gcd(ll a, ll b){ n     if(b == 0) return a; n     else return gcd(b,a%b); n } n  n //code starts here n  n ll n,q; n const ll M = 1e5+100; n ll nxt[M] = {0}, a[M]; n ll dp[LG][M] = {0}; n vl fact[M]; n void solve(){ n     re n; re q; n     fo(n) re a[i+1]; n     for(ll i = 2; i<M; i++){ n         if(!fact[i].empty()) continue; n         for(ll j = i; j<M; j+=i){ n             nxt[i] = n+1; n             fact[j].pb(i); n         } n     } n     dp[0][n+1] = n+1; n     for(ll i = n; i>=1; i--){ n         dp[0][i] = dp[0][i+1]; n         for(auto f: fact[a[i]]){ n             // if(i == 6 && f == 7) pr( here ); n             dp[0][i] = min(dp[0][i],nxt[f]); n             nxt[f] = i; n         } n         // cout<<i<< () <<dp[0][i]<<  n ; n     } n  n     for(ll j = 1; j<20; j++){ n         for(ll i = 1; i<=n+1; i++){ n             ll k = dp[j-1][i]; n             dp[j][i] = dp[j-1][k]; n         } n     } n     while(q--){ n         ll l,r; re l; re r; n         ll ans = 0; n         for(ll i = 19; i>=0; i--){ n             if(dp[i][l] <= r){ n                 // cout<<l<< () n ; n                 ans += (1<<i); n                 l = dp[i][l]; n             } n         } n         pr(ans+1); n     } n } n  n int32_t main(){ n     KOBE; n     ll t; n     t = 1; n     // re t; n     while(t--) solve(); n } n  n  n //common errors n // row - n, col - m always and loop var n // see the freq of numbers carefully n // see if there s array overflow n // use map for large inputs n // always check for n = 1 condition n // check loop starting and end points :( n  n //problem ideas n //check piegonhole wherever possible n //there might be many instances of limited answers like 0,1,2 only n // see suffix and prefix n //don t be obsessed with binary search n // try to find repeating pattern in matrices n


#include <bits/stdc++.h> n using namespace std; n const pair<int, int> DD[] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1}, n                              {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; n template <class T> n T smin(T &a, T b) { n   if (a > b) a = b; n   return a; n } n template <class T> n T smax(T &a, T b) { n   if (a < b) a = b; n   return a; n } n int main(void) { n   srand(time(0)); n   cout << fixed << setprecision(7); n   cerr << fixed << setprecision(7); n   int n; n   cin >> n; n   if ((n % 4) == 2 || (n % 4) == 3) return puts( NO ) * 0; n   puts( YES ); n   auto go = [&](int a, int b) { n     cout << min(a, b) <<     << max(a, b) <<   n ; n   }; n   for (int i = 1; i + 3 <= n; i += 4) { n     if (n % 4) n       go(i, n), go(i, i + 1), go(i + 1, n), go(n, i + 2), go(i + 2, i + 3), n           go(i + 3, n); n     else n       go(i, i + 1), go(i + 2, i + 3); n     go(i + 1, i + 2); n     go(i, i + 3); n     go(i, i + 2); n     go(i + 1, i + 3); n   } n   vector<pair<int, int> > v; n   v.push_back(make_pair(3, 2)); n   v.push_back(make_pair(2, 2)); n   v.push_back(make_pair(3, 1)); n   v.push_back(make_pair(0, 1)); n   v.push_back(make_pair(1, 3)); n   v.push_back(make_pair(2, 3)); n   v.push_back(make_pair(1, 2)); n   v.push_back(make_pair(1, 1)); n   v.push_back(make_pair(3, 0)); n   v.push_back(make_pair(3, 3)); n   v.push_back(make_pair(0, 2)); n   v.push_back(make_pair(0, 3)); n   v.push_back(make_pair(2, 0)); n   v.push_back(make_pair(2, 1)); n   v.push_back(make_pair(1, 0)); n   v.push_back(make_pair(0, 0)); n   for (int i = 1; i + 3 <= n; i += 4) n     for (int j = i + 4; j + 3 <= n; j += 4) n       for (auto it : v) go(i + it.first, j + it.second); n   cerr <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> v; n int bil; n void gen(long long int now) { n   if (now > 1000000000LL) return; n   if (now != 0) v.push_back(now); n   gen(now * 10 + 4); n   gen(now * 10 + 7); n } n int main(void) { n   gen(0); n   sort(v.begin(), v.end()); n   cin >> bil; n   for (int i = 0; i < v.size(); i++) { n     if (v[i] == bil) { n       cout << i + 1 << endl; n       return 0; n     } n   } n } n


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


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


#include <bits/stdc++.h> n using namespace std; n const int p[] = {1, 2, 2, 1}; n const int q[] = {1, 1, 2, 2}; n const int N = 500005; n int n, m; n int a[N], b[N]; n long long ans; n void check(int& a, int b) { n   if (!a) n     a = b; n   else if (a != b) n     ans = 0; n } n int F(int a, int b) { return (b & 1) ? a : 3 - a; } n int main() { n   scanf( %d%d n , &n, &m); n   ans = 1; n   char ch; n   int k; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       scanf( %c , &ch); n       if (ch ==  . ) continue; n       k = ch -  1 ; n       check(a[j], F(p[k], i)); n       check(b[i], F(q[k], j)); n     } n     scanf(  n ); n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       (ans *= ((i == 1 && !a[j]) + 1) * ((j == 1 && !b[i]) + 1)) %= 1000003; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n, m, en, q[maxn], depth[maxn], f[maxn][20], dist[maxn]; n struct edge { n   int e; n   edge *next; n } * v[maxn], ed[maxn << 1]; n void add_edge(int s, int e) { n   en++; n   ed[en].next = v[s]; n   v[s] = ed + en; n   v[s]->e = e; n } n void bfs(int root) { n   int front = 1, tail = 1; n   q[1] = root; n   depth[1] = 1; n   for (; front <= tail;) { n     int now = q[front++]; n     for (edge *e = v[now]; e; e = e->next) n       if (!depth[e->e]) { n         depth[e->e] = depth[now] + 1; n         f[e->e][0] = now; n         int p = now, x = 0; n         while (f[p][x]) { n           f[e->e][x + 1] = f[p][x]; n           p = f[p][x]; n           x++; n         } n         q[++tail] = e->e; n       } n   } n } n int get_lca(int p1, int p2) { n   if (depth[p1] < depth[p2]) swap(p1, p2); n   int delta = depth[p1] - depth[p2], x = 0; n   while (delta) { n     if (delta & 1) p1 = f[p1][x]; n     delta >>= 1; n     x++; n   } n   x = 0; n   while (p1 != p2) { n     if (!x || f[p1][x] != f[p2][x]) { n       p1 = f[p1][x]; n       p2 = f[p2][x]; n       x++; n     } else n       x--; n   } n   return p1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int a = 1; a < n; a++) { n     int s, e; n     scanf( %d%d , &s, &e); n     add_edge(s, e); n     add_edge(e, s); n   } n   int size = (int)sqrt(n); n   bfs(1); n   int cnt = 1; n   q[1] = 1; n   memset(dist, 0x3f, sizeof(dist)); n   dist[1] = 0; n   int opt, p; n   for (int a = 1; a <= m; a++) { n     scanf( %d%d , &opt, &p); n     if (opt == 1) { n       q[++cnt] = p; n       dist[p] = 0; n     } else { n       int ans = dist[p]; n       for (int b = 1; b <= cnt; b++) n         ans = min(ans, depth[p] + depth[q[b]] - 2 * depth[get_lca(p, q[b])]); n       printf( %d n , ans); n     } n     if (cnt >= size) { n       for (int front = 1, tail = cnt + 1; front != tail;) { n         int now = q[front++]; n         if (front == maxn) front = 0; n         for (edge *e = v[now]; e; e = e->next) n           if (dist[e->e] > dist[now] + 1) { n             dist[e->e] = dist[now] + 1; n             q[tail++] = e->e; n             if (tail == maxn) tail = 0; n           } n       } n       cnt = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ty { n   int d[4][11], a; n } t[400005], ans; n int n, m, q, x, y, a[11][100005], co[42], col, fa1, fa2, fa[42]; n int find(int x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n ty update(ty x, ty y) { n   ty z; n   for (int i = 1; i <= n; i++) { n     fa[x.d[3][i]] = x.d[3][i]; n     fa[x.d[2][i]] = x.d[2][i]; n     fa[y.d[2][i] + 20] = y.d[2][i] + 20; n     fa[y.d[3][i] + 20] = y.d[3][i] + 20; n   } n   z.a = x.a + y.a; n   int col = 0; n   memset(co, 0, sizeof(co)); n   for (int i = 1; i <= n; i++) n     if (x.d[1][i] == y.d[0][i]) { n       fa1 = find(x.d[3][i]); n       fa2 = find(y.d[2][i] + 20); n       if (fa1 != fa2) { n         z.a--; n         fa[fa2] = fa1; n       } n     } n   for (int i = 1; i <= n; i++) { n     z.d[0][i] = x.d[0][i]; n     z.d[1][i] = y.d[1][i]; n     fa1 = find(x.d[2][i]); n     if (!co[fa1]) co[fa1] = ++col; n     z.d[2][i] = co[fa1]; n     fa1 = find(y.d[3][i] + 20); n     if (!co[fa1]) co[fa1] = ++col; n     z.d[3][i] = co[fa1]; n   } n   return z; n } n void build(int o, int l, int r) { n   if (l == r) { n     t[o].a = 0; n     int col = 0; n     for (int i = 1; i <= n; i++) { n       t[o].d[0][i] = t[o].d[1][i] = a[i][l]; n       if (a[i][l] != a[i - 1][l]) { n         t[o].a++; n         col++; n       } n       t[o].d[2][i] = t[o].d[3][i] = col; n     } n     return; n   } n   int mid = (l + r) >> 1; n   build(o * 2, l, mid); n   build(o * 2 + 1, mid + 1, r); n   t[o] = update(t[o * 2], t[o * 2 + 1]); n } n ty find(int o, int l, int r, int L, int R) { n   if (l == L && r == R) return t[o]; n   int mid = (l + r) >> 1; n   if (R <= mid) n     return find(o * 2, l, mid, L, R); n   else if (L > mid) n     return find(o * 2 + 1, mid + 1, r, L, R); n   else n     return update(find(o * 2, l, mid, L, mid), n                   find(o * 2 + 1, mid + 1, r, mid + 1, R)); n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) n     for (int r = 1; r <= m; r++) scanf( %d , &a[i][r]); n   build(1, 1, m); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &x, &y); n     ans = find(1, 1, m, x, y); n     printf( %d n , ans.a); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n string best; n void LongestPalindromicPrefix(string str) { n   string temp = str +  ? ; n   reverse(str.begin(), str.end()); n   temp += str; n   int n = temp.length(); n   int lps[n]; n   fill(lps, lps + n, 0); n   for (int i = 1; i < n; i++) { n     int len = lps[i - 1]; n     while (len > 0 && temp[len] != temp[i]) { n       len = lps[len - 1]; n     } n     if (temp[i] == temp[len]) { n       len++; n     } n     lps[i] = len; n   } n   if (best.length() < lps[n - 1]) { n     best = temp.substr(0, lps[n - 1]); n   } n } n int solve() { n   int n, i, j; n   best.erase(); n   string s; n   cin >> s; n   n = s.length(); n   i = 0, j = n - 1; n   string temp; n   long long ans = 0; n   while (s[i] == s[j] && i <= j) { n     temp.push_back(s[i]); n     i++; n     j--; n     ans++; n     if (i != j) { n       ans++; n     } n     if (i >= j) { n       break; n     } n   } n   int k; n   string res; n   for (k = i; k <= j; k++) { n     res.push_back(s[k]); n   } n   if (res.length() == 0) { n     cout << s <<   n ; n     return 0; n   } n   cout << temp; n   LongestPalindromicPrefix(res); n   reverse(res.begin(), res.end()); n   LongestPalindromicPrefix(res); n   cout << best; n   reverse(temp.begin(), temp.end()); n   cout << temp <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double l[200006]; n int main() { n   l[1] = 0; n   l[0] = 0; n   for (int i = 2; i < 200005; i++) { n     l[i] = log(i) + l[i - 1]; n   } n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   if (k >= m) { n     printf( 1.00000 n ); n   } else if (k + n < m) { n     printf( 0.00000 n ); n   } else { n     long double ans = l[n] + l[m] - l[n + k + 1] - l[m - k - 1]; n     ans = exp(ans); n     double tmp = 1.0 - ans; n     printf( %.10f n , tmp); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   bool flag = true; n   int test_case = 1, tc; n   if (!flag) cin >> test_case; n   tc = test_case; n   while (tc--) { n     int i, j, n, m, sum = 0, ans = 0, cnt = 0, flag = 0; n     int maxi = -1e9, mini = 1e9; n     string s; n     cin >> s; n     cin >> n; n     m = s.size(); n     int pre[26][m + 2]; n     memset(pre, 0, sizeof(pre)); n     for (i = 0; i < m; i++) pre[s[i] -  a ][i] = 1; n     for (i = 0; i < 26; i++) { n       for (j = 1; j < m; j++) { n         pre[i][j] += pre[i][j - 1]; n       } n     } n     while (n--) { n       int l, r; n       cin >> l >> r; n       cnt = 0; n       if (r == l) { n         cout <<  Yes n ; n         continue; n       } else if (l == 1) { n         for (i = 0; i < 26; i++) { n           if (pre[i][r - 1] > 0) cnt++; n         } n       } else { n         for (i = 0; i < 26; i++) { n           if (pre[i][r - 1] - pre[i][l - 2] > 0) cnt++; n         } n       } n       if (cnt > 2 or s[l - 1] != s[r - 1]) n         cout <<  Yes n ; n       else n         cout <<  No n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001]; n int main() { n   ios::sync_with_stdio(0); n   int n, mn = 1e9; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     mn = min(mn, a[i]); n   } n   int last = -1; n   int ans = n + 1e1; n   for (int i = 1; i <= n; ++i) { n     if (a[i] == mn) { n       if (last != -1) { n         ans = min(ans, i - last); n       } n       last = i; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int hr, mt; n   int h, d, c, n; n   scanf( %d%d , &hr, &mt); n   scanf( %d%d%d%d , &h, &d, &c, &n); n   int p = h + max((1200 - (hr * 60 + mt)) * d, 0), t; n   if (p % n != 0) n     t = p / n + 1; n   else n     t = p / n; n   double ans = t * c * 0.8; n   if (h % n != 0) n     t = h / n + 1; n   else n     t = h / n; n   ans = min(ans, t * c * 1.0); n   printf( %.3f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, clr[300001][3] = {{0}}; n vector<long long> v[300001]; n void dfs(long long node, long long parent) { n   for (auto x : v[node]) { n     if (x == parent) continue; n     dfs(x, node); n     clr[node][1] += clr[x][1]; n     clr[node][2] += clr[x][2]; n   } n } n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   cin >> n; n   long long r = 0, b = 0; n   for (long long i = 1; i < n + 1; i++) { n     long long c; n     cin >> c; n     if (c == 1) r++; n     if (c == 2) b++; n     clr[i][c]++; n   } n   for (long long i = 1; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 1); n   long long ans = 0; n   if (b == 0 || r == 0) return cout << n - 1 <<   n , 0; n   for (long long i = 1; i < n + 1; i++) { n     if ((clr[i][2] == 0 && clr[i][1] == r) || n         (clr[i][1] == 0 && clr[i][2] == b)) n       ans++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > ans; n int main() { n   int n; n   scanf( %d , &n); n   int a[n + 1], b[n + 1]; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &b[i]); n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] != b[i]) { n       for (int j = i + 1; j <= n; ++j) { n         if (a[i] == b[j]) { n           for (int k = j; k > i; k--) { n             int sw = b[k]; n             b[k] = b[k - 1]; n             b[k - 1] = sw; n             ans.push_back(make_pair(k - 1, k)); n           } n           break; n         } n       } n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T x, T y) { n   while (y > 0) { n     x %= y; n     swap(x, y); n   } n   return x; n } n template <class _T> n inline _T sqr(const _T& x) { n   return x * x; n } n template <class _T> n inline string tostr(const _T& a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795L; n template <typename T> n inline void input(T& a) { n   static int c; n   a = 0; n   while (!isdigit(c = getchar()) && c !=  - ) { n   } n   char neg = 0; n   if (c ==  - ) { n     neg = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     a = 10 * a + c -  0 ; n     c = getchar(); n   } n   if (neg) a = -a; n } n template <typename T = int> n inline T nxt() { n   T res; n   input(res); n   return res; n } n void solve() {} n void imp() { n   puts( IMPOSSIBLE ); n   exit(0); n } n const int N = 1000010; n vector<int> q[N][2]; n int g[N][2]; n int r[N]; n int maleft[N]; n int miright[N]; n int n; n int dfs(int v, int le, int ri) { n   if (r[v] >= ri) { n     imp(); n   } n   if (v >= ri) { n     imp(); n   } n   if (le >= ri) { n     imp(); n   } n   if (maleft[v] > v) { n     g[v][0] = v + 1; n     int ret = dfs(v + 1, maleft[v], min(ri, miright[v])); n     le = max(le, r[v]); n     if (ret < le) { n       g[v][1] = ret + 1; n       return dfs(ret + 1, le, ri); n     } else { n       return ret; n     } n   } else { n     le = max(le, r[v]); n     if (v < le) { n       g[v][1] = v + 1; n       return dfs(v + 1, le, ri); n     } n   } n   return v; n } n void print(int v) { n   if (v >= n) { n     return; n   } n   if (g[v][0]) { n     print(g[v][0]); n   } n   printf( %d  , v + 1); n   if (g[v][1]) { n     print(g[v][1]); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int c; n   n = nxt(); n   c = nxt(); n   char s[10]; n   for (int i = 0; i < c; ++i) { n     int a = nxt() - 1; n     int b = nxt() - 1; n     if (b <= a) { n       imp(); n     } n     scanf( %s , s); n     if (s[0] ==  L ) { n       q[a][0].push_back(b); n     } else { n       q[a][1].push_back(b); n     } n   } n   for (int i = 0; i < n; ++i) { n     r[i] = i; n     maleft[i] = i; n     miright[i] = INT_MAX; n   } n   for (int i = n - 2; i >= 0; --i) { n     for (int x : q[i][0]) { n       r[i] = max(r[i], r[x]); n     } n     for (int x : q[i][1]) { n       r[i] = max(r[i], r[x]); n     } n     for (int x : q[i][0]) { n       maleft[i] = max(maleft[i], r[x]); n     } n     for (int x : q[i][1]) { n       miright[i] = min(miright[i], x); n     } n     if (miright[i] <= maleft[i]) { n       imp(); n     } n   } n   int val = dfs(0, n - 1, n); n   assert(val == n - 1); n   print(0); n   puts(  ); n   return 0; n }; n


#include <bits/stdc++.h> n template <typename T> n std::istream& operator>>(std::istream& input, std::vector<T>& v) { n   for (T& a : v) input >> a; n   return input; n } n void answer(int v) { std::cout << v <<   n ; } n void solve(const std::vector<size_t>& id) { n   int z = 0; n   std::unordered_map<size_t, size_t> x; n   for (const size_t a : id) { n     if (a == 0) continue; n     const size_t c = ++x[a]; n     if (c > 2) return answer(-1); n     z += (c == 2); n   } n   answer(z); n } n int main() { n   size_t n; n   std::cin >> n; n   std::vector<size_t> id(n); n   std::cin >> id; n   solve(id); n   return 0; n } n


#include <bits/stdc++.h> n std::pair<int, int> a[111111]; n int main() { n   int i, n, m, max = 0; n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) scanf( %d%d , &a[i].first, &a[i].second); n   max = std::max(a[0].second + a[0].first - 1, n                  a[m - 1].second - a[m - 1].first + n); n   for (i = 1; i < m; i++) { n     if (std::abs(a[i].second - a[i - 1].second) > a[i].first - a[i - 1].first) { n       puts( IMPOSSIBLE ); n       return 0; n     } n     max = std::max(max, std::max(a[i].second, a[i - 1].second) + n                             (a[i].first - a[i - 1].first - n                              std::abs(a[i].second - a[i - 1].second)) / n                                 2); n   } n   printf( %d , max); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, m, pos[N], ans[N]; n vector<int> g[N]; n map<int, int> tot; n struct data { n   int a, i; n } a[N]; n bool cmp(data u, data v) { return u.a > v.a; } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i].a), a[i].i = i; n     ++tot[a[i].a]; n   } n   sort(a + 1, a + 1 + n, cmp); n   for (int i = 1; i <= n; ++i) pos[a[i].i] = i; n   for (int i = 1, x, y; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     x = pos[x], y = pos[y]; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int cnt = n; n   a[0].a = -1; n   for (int i = 1; i <= n; ++i) { n     int s = g[i].size(); n     if (a[i].a != a[i - 1].a) cnt -= tot[a[i].a]; n     ans[a[i].i] = cnt; n     for (int j = 0; j < s; ++j) n       if (g[i][j] > i && a[g[i][j]].a != a[i].a) --ans[a[i].i]; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, aid = 1, did = 1, atk[105], def[105], c[105]; n char s[10]; n int main() { n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     scanf( %s , s); n     if (s[0] ==  A ) n       scanf( %lld , &atk[aid++]); n     else n       scanf( %lld , &def[did++]); n   } n   for (long long i = 1; i <= m; i++) { n     scanf( %lld , &c[i]); n   } n   sort(atk + 1, atk + aid); n   sort(c + 1, c + m + 1, greater<long long>()); n   long long indx = 1, sum = 0, off = 0; n   for (long long i = 1; i <= m; i++) { n     if (i >= aid) break; n     if (c[i] < atk[i]) { n       off = 1; n       break; n     } n     sum += (c[i] - atk[i]); n     indx = i + 1; n   } n   multiset<long long> cel; n   bool flag = 1; n   if (off == 1 || indx > m) { n     goto p; n     return 0; n   } n   for (long long i = indx; i <= m; i++) cel.insert(c[i]); n   for (long long i = 1; i < did; i++) { n     auto it = cel.upper_bound(def[i]); n     if (it == cel.end()) { n       goto p; n     } n     cel.erase(it); n   } n   while (!cel.empty()) { n     auto it = cel.begin(); n     sum += *it; n     cel.erase(it); n   } n p: n   long long sum2 = 0, cnt = 0; n   multiset<long long> cel2; n   for (long long i = 1; i <= m; i++) cel2.insert(c[i]); n   for (long long i = 1; i < did; i++) { n     auto it = cel2.upper_bound(def[i]); n     if (it == cel2.end()) continue; n     cnt++; n     cel2.erase(it); n   } n   for (long long i = 1; i < aid; i++) { n     auto it = cel2.upper_bound(atk[i]); n     if (it == cel2.end()) continue; n     cnt++; n     sum2 += (*it - atk[i]); n     cel2.erase(it); n   } n   while (!cel2.empty() && cnt == n) { n     auto it = cel2.begin(); n     sum2 += *it; n     cel2.erase(it); n   } n   cout << max(sum, sum2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 2020; n const int MOD = 1e9 + 7; n const long long INF = 1061109567; n const long double EPS = 1e-9; n const long double PI = acos(-1); n const int MXX = 2e5 + 5; n long long F[MXX + 5], iF[MXX + 5]; n long long qpow(long long b, long long e) { n   if (!e) return 1LL; n   if (e & 1) return (qpow(b, e - 1) * b) % MOD; n   long long aux = qpow(b, e / 2); n   return (aux * aux) % MOD; n } n long long invmod(long long n) { return qpow(n, MOD - 2); } n long long ways(int p, int q) { n   if (p == 0 || q == 0) return 1; n   long long ans = ((F[p + q] % MOD) * (iF[p] % MOD)) % MOD; n   ans = ((ans % MOD) * (iF[q] % MOD)) % MOD; n   return ans; n } n long long ways(int p, int q, int u, int v) { return ways(u - p, v - q) % MOD; } n pair<int, int> pt[MX]; n long long dp[MX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   iF[0] = F[0] = iF[1] = F[1] = 1LL; n   for (int i = (2); i < (MXX); i++) { n     F[i] = ((F[i - 1] % MOD) * (i % MOD)) % MOD; n     iF[i] = ((iF[i - 1] % MOD) * (invmod(i) % MOD)) % MOD; n   } n   int h, w; n   cin >> h >> w; n   int n; n   cin >> n; n   n++; n   pt[0].first = pt[0].second = 1; n   for (int i = (1); i < (n); i++) { n     cin >> pt[i].first >> pt[i].second; n   } n   sort(pt, pt + n); n   for (int i = (n - 1); i >= (0); i--) { n     dp[i] = ways(pt[i].first, pt[i].second, h, w); n     for (int j = (i + 1); j < (n); j++) { n       if (pt[j].second < pt[i].second) continue; n       long long v = n           ways(pt[i].first, pt[i].second, pt[j].first, pt[j].second) % MOD; n       v = (v * dp[j]) % MOD; n       dp[i] = (dp[i] + MOD - v) % MOD; n     } n   } n   cout << dp[0]; n } n


#include<bits/stdc++.h> n using namespace std; n #define M 500000 n typedef long long ll; n typedef unsigned long long ull; n inline ll read() { n  char ch; n  ll f = 1, res = 0; n  while ((ch = getchar()) <  0  || ch >  9 ) n   if (ch ==  - ) f = -1; n  while (ch >=  0  && ch <=  9 ) { n   res = (res << 1) + (res << 3) + (ch ^ 48); n   ch = getchar(); n  } n  return f * res; n } n inline void pt(ll u,ll mod) { n  printf( %lld , u); n  if (mod == 1) n   putchar(   ); n  else if (mod == 2) n   putchar(  n ); n } n ll mod=1e9+7; n ll arr[M]; n ll p[M]; n int main() { n  //freopen( ../in ,  r , stdin); n  //freopen( ../out ,  w , stdout); n  p[1]=1; n  p[0]=1; n  for(ll i=2;i<M;i++) n   p[i]=p[i-1]*i%mod; n  ll q = read(); n  while (q--) { n   ll n = read(); n   ll k = pow(2, 40) + 0.1 - 1; n   for (ll i = 0; i < n; i++) { n    ll t = read(); n    arr[i] = t; n    k &= t; n   } n   ll cnt = 0; n   for (ll i = 0; i < n; i++) { n    if (arr[i] == k) n     cnt++; n   } n   ll ans = cnt * (cnt - 1) % mod; n   pt(ans * p[n - 2] % mod, 2); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[1 << 16], *S, *T; n   if (S == T) { n     T = (S = buf) + fread(buf, 1, 1 << 16, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline long long read() { n   long long x = 0, f = 1; n   char ch = gc(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = gc(); n   return x * f; n } n int m, k, n, a[1000010], dp[1000010], ans = 0; n long long l; n pair<int, int> c[1000010]; n int main() { n   m = read(); n   l = read(); n   k = read(); n   n = m * k; n   for (int i = 0; i < m; ++i) a[i] = read(); n   for (int i = 0; i < n; ++i) c[i].first = a[i % m], c[i].second = i; n   sort(c, c + n); n   long long tot = l / m; n   int yu = l % m; n   ans = l % 1000000007; n   for (int i = 0; i < n; ++i) { n     int id = c[i].second; n     if (id / m + 1 == 1) { n       dp[id / m + 1]++; n       continue; n     } n     dp[id / m + 1] += dp[id / m + 1 - 1], dp[id / m + 1] %= 1000000007; n     if (id >= l) continue; n     long long cnt = tot - (id / m + 1) + 1; n     if (id % m <= yu - 1) cnt++; n     ans += cnt % 1000000007 * dp[id / m + 1 - 1] % 1000000007; n     ans %= 1000000007; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sq(long long x) { return x * x; } n long double ptos(pair<long long, long long> A, pair<long long, long long> B, n                  pair<long long, long long> C) { n   long long area = abs((B.first - A.first) * (C.second - A.second) - n                        (B.second - A.second) * (C.first - A.first)); n   long long llen = sq(B.first - C.first) + sq(B.second - C.second); n   return 1.0l * area / sqrtl(llen); n } n int main() { n   int n; n   scanf( %d , &n); n   vector<pair<long long, long long> > p(n); n   for (int i = 0; i < n; ++i) { n     int first, second; n     scanf( %d %d , &first, &second); n     p[i].first = first, p[i].second = second; n   } n   p.push_back(p[0]); n   long double dans = 1e18l; n   for (int i = 0; i < n; ++i) { n     dans = min(dans, ptos(p[i], p[(i - 1 + n) % n], p[i + 1])); n     for (int j = 0; j < n; ++j) { n       if (i != j) { n         dans = min(dans, sqrtl(sq(p[i].first - p[j].first) + n                                sq(p[i].second - p[j].second))); n       } n       if (i != j && i != (j + 1) % n) { n         dans = min(dans, ptos(p[i], p[j], p[j + 1])); n       } n     } n   } n   printf( %.10lf n , (double)(dans / 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n vector<vector<long long> > adj(N), w(N); n long long out[N], col[N], res[N], cnt; n queue<long long> q; n void dfs(long long v, long long p) { n   for (auto &u : adj[v]) { n     if (u == p) continue; n     dfs(u, v); n   } n   for (auto &u : adj[v]) { n     if (u == p) continue; n     if (col[u] != col[v] && !res[u] && !res[v]) { n       res[u] = res[v] = ++cnt; n       q.push(v); n       q.push(u); n     } n   } n   return; n } n void bfs() { n   q.push(1); n   col[1] = 1; n   while (!q.empty()) { n     long long v = q.front(); n     q.pop(); n     for (auto &u : adj[v]) { n       if (!col[u]) { n         col[u] = col[v] % 2 + 1; n         q.push(u); n       } n     } n   } n   cnt = 0; n   dfs(1, 0); n   while (!q.empty()) { n     long long v = q.front(); n     q.pop(); n     for (auto &u : adj[v]) { n       if (!res[u]) { n         res[u] = res[v]; n         q.push(u); n       } n     } n   } n   return; n } n int main() { n   long long n, i, j, v, u, ans; n   cin >> n; n   for (i = 0; i < n - 1; i++) { n     cin >> v >> u; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   bfs(); n   for (i = 1; i <= n; i++) w[res[i]].push_back(i); n   ans = 0; n   for (i = 1; i <= cnt; i++) { n     ans += (w[i].size() - 1) * 2; n     for (j = 0; j < w[i].size(); j++) n       out[w[i][j]] = w[i][(j + 1) % w[i].size()]; n   } n   cout << ans <<   n ; n   for (i = 1; i <= n; i++) cout << out[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x; n   int y; n }; n struct vec { n   int x; n   int y; n }; n point r_array[12], b_array[12]; n int root[12]{}, ocupied[12]{}; n int r, b; n double cross(point a, point b, point c) { n   vec v1 = {c.x - a.x, c.y - a.y}; n   vec v2 = {b.x - a.x, b.y - a.y}; n   return (v1.x * v2.y - v1.y * v2.x); n } n bool r_check(int r1_index, int b1_index, int r2_index, int b2_index) { n   point r1_cor = r_array[r1_index]; n   point r2_cor = r_array[r2_index]; n   point b1_cor = b_array[b1_index]; n   point b2_cor = b_array[b2_index]; n   double kq_r1 = cross(r1_cor, r2_cor, b2_cor); n   double kq_b1 = cross(b1_cor, r2_cor, b2_cor); n   double kq_r2 = cross(r2_cor, r1_cor, b1_cor); n   double kq_b2 = cross(b2_cor, r1_cor, b1_cor); n   if (kq_r1 * kq_b1 > 0) return 0; n   if (kq_r2 * kq_b2 > 0) return 0; n   return 1; n } n bool check() { n   bool kq = 1; n   for (int i = 1; i < r; i++) n     for (int j = i + 1; j <= r; j++) { n       kq = min(kq, r_check(i, root[i], j, root[j])); n       if (kq == 0) return 0; n     } n   return kq; n } n bool sinh(int r_pos) { n   if (r_pos > r) { n     return check(); n   } n   for (int i = 1; i <= b; i++) { n     if (ocupied[i]) continue; n     ocupied[i] = 1; n     root[r_pos] = i; n     bool kq = sinh(r_pos + 1); n     ocupied[i] = 0; n     if (kq) return kq; n   } n } n int main() { n   cin >> r >> b; n   for (int i = 1; i <= r; i++) cin >> r_array[i].x >> r_array[i].y; n   for (int i = 1; i <= b; i++) cin >> b_array[i].x >> b_array[i].y; n   if (r != b) { n     cout <<  No ; n     return 0; n   } n   bool kq = sinh(1); n   if (kq) n     puts( Yes ); n   else n     puts( No ); n } n


#include <bits/stdc++.h> n using namespace std; n using DO = double; n using LL = long long; n using VI = vector<LL>; n const int N = 5e5 + 10; n int head[N], nxt[N << 1], to[N << 1]; n LL wei[N << 1]; n int E; n void init(int n) { n   for (int i = 1; i <= n; i++) { n     head[i] = -1; n   } n   E = 0; n } n void add(int u, int v, int w) { n   nxt[E] = head[u]; n   to[E] = v; n   wei[E] = w; n   head[u] = E++; n   nxt[E] = head[v]; n   to[E] = u; n   wei[E] = w; n   head[v] = E++; n } n LL dp[N][2]; n int n, k; n void dfs(int u, int fa = 0) { n   dp[u][0] = dp[u][1] = 0; n   VI vec; n   for (int e = head[u], v; ~e; e = nxt[e]) { n     v = to[e]; n     if (v == fa) continue; n     dfs(v, u); n     dp[u][0] += dp[v][0]; n     dp[u][1] += dp[v][0]; n     if (dp[v][0] < dp[v][1] + wei[e]) { n       vec.push_back(dp[v][1] + wei[e] - dp[v][0]); n     } n   } n   sort(vec.begin(), vec.end()); n   reverse(vec.begin(), vec.end()); n   for (int i = 0; i < min((int)vec.size(), k); i++) { n     dp[u][0] += vec[i]; n   } n   for (int i = 0; i < min((int)vec.size(), k - 1); i++) { n     dp[u][1] += vec[i]; n   } n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d , &n, &k); n     init(n); n     for (int i = 1, u, v, w; i < n; i++) { n       scanf( %d %d %d , &u, &v, &w); n       add(u, v, w); n     } n     dfs(1); n     printf( %I64d n , max(dp[1][0], dp[1][1])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n const int M = 205; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n struct node { n   int l, r, id; n } p[N << 1]; n bool cmp(node a, node b) { return a.r < b.r; } n int sum[N << 4]; n void update(int p, int L, int R, int pos) { n   if (L == R) { n     sum[pos]++; n     return; n   } n   int mid = (L + R) >> 1; n   if (p <= mid) n     update(p, L, mid, pos << 1); n   else n     update(p, mid + 1, R, pos << 1 | 1); n   sum[pos] = sum[pos << 1] + sum[pos << 1 | 1]; n } n int query(int k, int L, int R, int pos) { n   if (L == R) return L; n   int mid = (L + R) >> 1; n   if (k <= sum[pos << 1]) n     return query(k, L, mid, pos << 1); n   else n     return query(k - sum[pos << 1], mid + 1, R, pos << 1 | 1); n } n int n, k, x[N << 1]; n vector<int> Ans; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) n     scanf( %d%d , &p[i].l, &p[i].r), p[i].id = i + 1, x[i * 2] = p[i].l, n                                      x[i * 2 + 1] = p[i].r; n   sort(p, p + n, cmp); n   sort(x, x + 2 * n); n   int m = unique(x, x + 2 * n) - x; n   for (int i = 0; i < n; i++) { n     p[i].l = lower_bound(x, x + m, p[i].l) - x; n     p[i].r = lower_bound(x, x + m, p[i].r) - x; n   } n   int ans = 0, st, ed; n   for (int i = n - 1; i >= 0; i--) { n     update(p[i].l, 0, m - 1, 1); n     if (sum[1] < k) continue; n     int pos = query(k, 0, m - 1, 1); n     int len = x[p[i].r] - x[pos] + 1; n     if (len > ans) ans = len, st = pos, ed = p[i].r; n   } n   if (!ans) { n     puts( 0 ); n     for (int i = 1; i <= k; i++) printf( %d  , i); n   } else { n     printf( %d n , ans); n     for (int i = 0; i < n; i++) n       if (p[i].l <= st && ed <= p[i].r) Ans.push_back(p[i].id); n     sort(Ans.begin(), Ans.end()); n     for (int i = 0; i < k; i++) printf( %d  , Ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, har[27]; n string s; n int sum[200005][27]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> m; n     cin >> s; n     for (int i = 0; i <= 25; i++) har[i] = 0; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 26; j++) sum[i][j] = 0; n     } n     for (int i = 0; i < n; i++) { n       if (i != 0) { n         for (int j = 0; j <= 25; j++) { n           sum[i][j] = sum[i - 1][j]; n         } n       } n       sum[i][s[i] -  a ]++; n     } n     int l = 0; n     for (int i = 0; i < m; i++) { n       cin >> l; n       for (int j = 0; j < 26; j++) { n         har[j] += sum[l - 1][j]; n       } n     } n     for (int j = 0; j < 26; j++) { n       har[j] += sum[n - 1][j]; n     } n     for (int i = 0; i < 26; i++) { n       cout << har[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n struct node { n   int x, y; n   bool operator<(const node &_) const { n     return x < _.x || (x == _.x && y < _.y); n   } n } A[2005]; n int h, w, n; n long long dp[2005], jie[200005 << 1], inv[200005 << 1]; n long long Pow(long long x) { n   int n = mod - 2; n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return res; n } n void Init() { n   jie[0] = inv[0] = 1; n   for (int i = 1; i < 200005; i++) { n     jie[i] = 1LL * jie[i - 1] * i % mod; n     inv[i] = Pow(jie[i]); n   } n } n long long C(int a, int b) { n   return 1LL * jie[a] * inv[b] % mod * inv[a - b] % mod; n } n int main() { n   Init(); n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &A[i].x, &A[i].y); n   sort(A + 1, A + n + 1); n   A[n + 1].x = h, A[n + 1].y = w; n   for (int i = 1; i <= n + 1; i++) { n     dp[i] = C(A[i].x + A[i].y - 2, A[i].x - 1); n     for (int j = 1; j < i; j++) { n       if (A[i].y < A[j].y) continue; n       dp[i] = ((dp[i] - n                 C(A[i].x - A[j].x + A[i].y - A[j].y, A[i].x - A[j].x) * dp[j]) % n                    mod + n                mod) % n               mod; n     } n   } n   cout << dp[n + 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<int> > adj; n vector<bool> top; n vector<int> branched; n vector<bool> visited; n vector<bool> fertile; n int root = -1; n void fail() { n   cout <<  No  << endl; n   exit(0); n } n void dfsFindRoot(int u) { n   if (root != -1) return; n   visited[u] = true; n   bool leaf = true; n   for (int child : adj[u]) { n     if (visited[child]) continue; n     leaf = false; n   } n   if (!leaf) { n     branched[u] = 0; n     for (int child : adj[u]) { n       if (visited[child]) continue; n       dfsFindRoot(child); n       branched[u] += branched[child]; n     } n     if (root != -1) return; n     if (branched[u] >= 3) { n       root = u; n     } n   } n } n void dfs(int u) { n   visited[u] = true; n   bool leaf = true; n   for (int child : adj[u]) { n     if (visited[child]) continue; n     leaf = false; n   } n   if (!leaf) { n     branched[u] = 0; n     int numTops = 0; n     for (int child : adj[u]) { n       if (visited[child]) continue; n       dfs(child); n       if (fertile[child]) top[u] = true; n       branched[u] += branched[child]; n       if (top[child]) { n         numTops++; n         top[u] = true; n       } n     } n     if (u != root && numTops > 1) n       fail(); n     else if (numTops > 2) n       fail(); n     if (branched[u] >= 3) { n       top[u] = true; n     } else if (branched[u] == 2) n       fertile[u] = true; n   } n } n int main() { n   int m; n   cin >> n; n   adj = vector<vector<int> >(n, vector<int>(0)); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   if (n <= 4) { n     cout <<  YES  << endl; n     return 0; n   } n   top = vector<bool>(n, false); n   branched = vector<int>(n, 1); n   visited = vector<bool>(n, false); n   dfsFindRoot(0); n   if (root == -1) { n     cout <<  Yes  << endl; n     return 0; n   } n   top = vector<bool>(n, false); n   branched = vector<int>(n, 1); n   visited = vector<bool>(n, false); n   fertile = vector<bool>(n, false); n   dfs(root); n   cout <<  Yes  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, ans = 0, a, b, t = 1; n   cin >> n >> x; n   while (n--) { n     cin >> a >> b; n     while (t <= a) t = t + x; n     t = t - x; n     ans = ans + b - t + 1; n     t = b + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 10; n long long bit[maxn]; n void update(long long idx, long long v) { n   while (idx <= maxn) { n     bit[idx] += v; n     idx += idx & -idx; n   } n } n long long query(long long idx) { n   long long ans = 0; n   while (idx > 0) { n     ans += bit[idx]; n     idx -= idx & -idx; n   } n   return ans; n } n long long inter(long long l, long long r) { return query(r) - query(l - 1); } n void solve() { n   long long n, m; n   cin >> n >> m; n   memset(bit, 0, sizeof bit); n   vector<pair<long long, pair<long long, long long> > > hor(n), vert(m); n   vector<pair<long long, pair<long long, long long> > > ord; n   long long ans = 1; n   for (long long i = 0; i < n; i++) { n     cin >> hor[i].first >> hor[i].second.first >> hor[i].second.second; n     if ((hor[i].second.first + hor[i].second.second) == 1e6) ans++; n   } n   for (long long i = 0; i < m; i++) { n     cin >> vert[i].first >> vert[i].second.first >> vert[i].second.second; n     if ((vert[i].second.first + vert[i].second.second) == 1e6) ans++; n   } n   for (long long i = 0; i < m; i++) { n     vert[i].first++; n     vert[i].second.first++; n     vert[i].second.second++; n   } n   for (long long i = 0; i < n; i++) { n     hor[i].first++; n     hor[i].second.first++; n     hor[i].second.second++; n   } n   for (long long i = 0; i < (long long)vert.size(); i++) { n     ord.push_back({vert[i].first, {1, i}}); n   } n   for (long long i = 0; i < (long long)hor.size(); i++) { n     ord.push_back({hor[i].second.first, {0, i}}); n     ord.push_back({hor[i].second.second, {2, i}}); n   } n   sort(ord.begin(), ord.end()); n   for (long long i = 0; i < (long long)ord.size(); i++) { n     long long x = ord[i].first; n     long long tipo = ord[i].second.first; n     long long ind = ord[i].second.second; n     if (tipo == 1) { n       long long y1 = vert[ind].second.first; n       long long y2 = vert[ind].second.second; n       ans += inter(y1, y2); n     } else { n       long long y = hor[ind].first; n       if (tipo == 2) n         update(y, -1); n       else n         update(y, 1); n     } n   } n   cout << ans << endl; n } n int32_t main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   long long tc = 1; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v, a; n vector<pair<double, int> > ans; n int n, sum, del, sx, sy, x, y, Nx, Ny; n set<int> u; n int cmp(pair<int, int> a, pair<int, int> b) { n   return ((a.first) * (a.first)) + ((a.second) * (a.second)) < n          ((b.first) * (b.first)) + ((b.second) * (b.second)); n } n int main() { n   v.clear(); n   u.clear(); n   for (int i = 0; i <= 200; i++) n     for (int j = i; j <= 200; j++) n       if (i + j) n         if (!u.count(((i) * (i)) + ((j) * (j)))) n           v.push_back(make_pair(i, j)), u.insert(((i) * (i)) + ((j) * (j))); n   sort(v.begin(), v.end(), cmp); n   cin >> n; n   sum = 0; n   for (int i = 0; i < n; i++) sum += (v[i].first + v[i].second) & 1; n   a.clear(); n   del = n; n   if (sum & 1) { n     sum += (v[n].first + v[n].second) & 1; n     for (int i = n - 1; i >= 0; i--) n       if (!((sum - ((v[i].first + v[i].second) & 1)) & 1)) { n         del = i; n         break; n       } n   } n   for (int i = 0; i <= n; i++) n     if (del != i) a.push_back(v[i]); n   sx = sy = 0; n   for (int i = n - 1; i >= 0; i--) { n     x = a[i].first, y = a[i].second; n     for (int s = 0; s < 8; s++) { n       Nx = x, Ny = y; n       if (s & 1) swap(Nx, Ny); n       if (s & 2) Nx *= -1; n       if (s & 4) Ny *= -1; n       if (((sx + a[i].first) * (sx + a[i].first)) + n               ((sy + a[i].second) * (sy + a[i].second)) > n           ((sx + Nx) * (sx + Nx)) + ((sy + Ny) * (sy + Ny))) n         a[i] = make_pair(Nx, Ny); n     } n     sx += a[i].first, sy += a[i].second; n   } n   for (int i = 0; i < n; i++) n     ans.push_back(make_pair(atan2(a[i].second, a[i].first), i)); n   sort(ans.begin(), ans.end()); n   cout <<  YES  << endl; n   sx = 0, sy = 0; n   for (int i = 0; i < n; i++) { n     cout << sx <<     << sy << endl; n     int id = ans[i].second; n     sx += a[id].first, sy += a[id].second; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ax, ay, bx, by, cx, cy; n void check(long long axx, long long ayy) { n   if (cx == 0 && cy == 0) { n     if (axx == bx && ayy == by) { n       cout <<  YES ; n       exit(0); n     } else n       return; n   } n   if ((cx * (by - ayy) - cy * (bx - axx)) % (cx * cx + cy * cy) != 0 || n       (cx * (bx - axx) + cy * (by - ayy)) % (cx * cx + cy * cy) != 0) n     return; n   cout <<  YES ; n   exit(0); n } n int main() { n   cin >> ax >> ay >> bx >> by >> cx >> cy; n   check(ax, ay); n   check(ay, -ax); n   check(-ax, -ay); n   check(-ay, ax); n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int A[2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   long long ans = 0; n   cin >> n; n   while (cin >> n) { n     ++A[n]; n     if (!n) ans += A[1]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1}; n int dy[9] = {0, 1, 0, -1, 1, -1, 1, -1}; n struct edge { n   int st, ed; n   char col; n   edge() { n     this->st = 0; n     this->ed = 0; n     this->col = 0; n   } n   edge(int st, int ed, char col) { n     this->st = st; n     this->ed = ed; n     this->col = col; n   } n }; n long long n, m; n long long a[2000001], b[2000001]; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   for (int test_case = 0; test_case < t; ++test_case) { n     cin >> n >> m; n     string input; n     cin >> input; n     string s2 =   ; n     for (int i = 0; i < n; ++i) { n       a[i] = 1000000000; n     } n     for (int i = 0; i < n; ++i) { n       if (a[i % m] == 1000000000) a[i % m] = (long long)input[i] -  0 ; n       s2 += input[i % m]; n     } n     bool flg = 0; n     if (s2 < input) flg = 1; n     if (flg) { n       int i; n       for (i = 0; i < n; ++i) { n         if (a[i] == 1000000000) break; n       } n       i--; n       while (a[i] == 9) { n         a[i] = 0; n         i--; n       } n       a[i]++; n     } n     cout << n <<   n ; n     for (int i = 0; i < n; ++i) { n       cout << a[i % m]; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n } n int fr[100005], to[100005 << 1], ne[100005 << 1], x[100005]; n int a[100005], b[100005], l, r; n int n, m; n int flag; n int c, d, e; n inline void dfs(int w) { n   for (int u = fr[w]; u; u = ne[u]) n     if (!x[to[u]]) { n       x[to[u]] = 3 - x[w]; n       dfs(to[u]); n     } else if (x[w] == x[to[u]]) n       flag = 1; n } n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= m; ++i) { n     read(c); n     read(d); n     to[++e] = d; n     ne[e] = fr[c]; n     fr[c] = e; n     to[++e] = c; n     ne[e] = fr[d]; n     fr[d] = e; n   } n   flag = 0; n   for (int i = 1; i <= n; ++i) n     if (!x[i]) { n       x[i] = 1; n       dfs(i); n     } n   if (flag) n     printf( -1 n ); n   else { n     for (int i = 1; i <= n; ++i) n       if (x[i] == 1) n         a[++l] = i; n       else n         b[++r] = i; n     printf( %d n , l); n     for (int i = 1; i <= l; ++i) printf( %d%c , a[i], i < l ?     :   n ); n     printf( %d n , r); n     for (int i = 1; i <= r; ++i) printf( %d%c , b[i], i < r ?     :   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mod(long long n, long long m) { n   long long ret = n % m; n   if (ret < 0) ret += m; n   return ret; n } n long long gcd(long long a, long long b) { n   return (b == 0LL ? a : gcd(b, a % b)); n } n long long exp(long long a, long long b, long long m) { n   if (b == 0LL) return 1LL; n   if (b == 1LL) return mod(a, m); n   long long k = mod(exp(a, b / 2, m), m); n   if (b & 1LL) { n     return mod(a * mod(k * k, m), m); n   } else n     return mod(k * k, m); n } n void print(int i) { n   for (int j = 0; j < 3; j++) { n     cout << !!(i & (1 << j)); n   } n   cout <<  - ; n } n int v[5][5]; n int OK[10][10]; n int n, m; n bool ok(int a, int b) { n   if (OK[a][b] != -1) return OK[a][b]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2; j++) { n       int c = (j == 0 ? a : b); n       if ((c & (1 << i))) { n         v[i][j] = 1; n       } else n         v[i][j] = 0; n     } n   } n   for (int i = 0; i < n - 1; i++) { n     int tot = v[i][0] + v[i][1] + v[i + 1][0] + v[i + 1][1]; n     if (tot % 2 == 0) return OK[a][b] = 0; n   } n   return OK[a][b] = 1; n } n const int N = 1e6 + 10; n vector<vector<char> > mat; n int C[N][(1 << 3)]; n int cost(int id, int msk) { n   if (C[id][msk] != -1) return C[id][msk]; n   int c = 0; n   for (int j = 0; j < n; j++) { n     int sla = (msk & (1 << j)) > 0; n     if ((sla) != (mat[j][id] -  0 )) c++; n   } n   return C[id][msk] = c; n } n const int inf = 1e8; n int dp[N][(1 << 3)]; n int solve(int j, int msk) { n   if (j == m) return 0; n   int& x = dp[j][msk]; n   if (x != -1) return x; n   x = inf; n   for (int m = 0; m < (1 << n); m++) { n     if (ok(m, msk)) x = min(x, solve(j + 1, m) + cost(j, m)); n   } n   return x; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   memset(C, -1, sizeof(C)); n   memset(OK, -1, sizeof(OK)); n   memset(dp, -1, sizeof(dp)); n   cin >> n >> m; n   mat.resize(n, vector<char>(m)); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (m); ++j) cin >> mat[i][j]; n   if (n == 1) { n     cout << 0 <<   n ; n   } else if (n <= 3) { n     int res = 1e9; n     for (int i = 0; i < (1 << n); i++) { n       res = min(res, solve(1, i) + cost(0, i)); n     } n     cout << res <<   n ; n   } else { n     cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, ans, ini, fin, med, idx; n bool t[40]; n int 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     cout <<  1 n ; n     cout.flush(); n     cin >> ans; n     if (!ans) return 0; n     if (ans == 1) t[i] = true; n   } n   ini = 1; n   fin = n; n   while (ini != fin) { n     med = (ini + fin) / 2; n     cout << med <<   n ; n     cout.flush(); n     cin >> ans; n     if (!ans) return 0; n     if (t[idx]) { n       if (ans == 1) { n         ini = med + 1; n       } else { n         fin = med - 1; n       } n     } else { n       if (ans == 1) { n         fin = med - 1; n       } else { n         ini = med + 1; n       } n     } n     idx++; n     idx %= m; n   } n   cout << ini <<   n ; n   cout.flush(); n   cin >> ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5001; n long long n, m, a[N + 1], b[N + 1], type[N], l[N], r[N], x[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) a[i] = 1e9; n   for (int j = 1; j <= m; j++) { n     cin >> type[j] >> l[j] >> r[j] >> x[j]; n     if (type[j] == 1) n       for (int i = l[j]; i <= r[j]; i++) b[i] += x[j]; n     else { n       bool ok = false; n       for (int i = l[j]; i <= r[j]; i++) { n         if (a[i] >= x[j] - b[i]) ok = true; n         a[i] = min(a[i], x[j] - b[i]); n       } n       if (!ok) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   for (int i = 1; i <= n; i++) b[i] = a[i]; n   bool ok = true; n   for (int i = 1; i <= m; i++) { n     if (type[i] == 1) n       for (int j = l[i]; j <= r[i]; j++) a[j] += x[i]; n     else { n       long long mx = a[l[i]]; n       for (int j = l[i] + 1; j <= r[i]; j++) mx = max(mx, a[j]); n       if (mx != x[i]) { n         ok = false; n         break; n       } n     } n   } n   if (!ok) n     cout <<  NO ; n   else { n     cout <<  YES  << endl; n     for (int i = 1; i <= n; i++) cout << b[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[105][105]; n int n, m, res; n void solve(int p) { n   if (p % 2) return; n   for (int i = 0, j = p - 1; i < p / 2; i++, j--) n     for (int k = 0; k < m; k++) n       if (mat[i][k] != mat[j][k]) return; n   res = res / 2; n   solve(p / 2); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &mat[i][j]); n   res = n; n   solve(n); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 400005; n int read() { n   int x = 0, flag = 1; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag = -1; n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return x * flag; n } n int n, m, a[M], b[M], s[M], f[M]; n vector<int> t; n void manacher() { n   for (int i = 1; i <= n; i++) s[i * 2] = b[i]; n   for (int i = 1; i <= n; i++) s[i * 2 - 1] = -1; n   s[2 * n + 1] = -1; n   int r = 0, c = 0; n   for (int i = 1; i <= 2 * n + 1; i++) { n     f[i] = r > i ? min(r - i + 1, f[2 * c - i]) : 1; n     while (i + f[i] <= 2 * n + 1 && i - f[i] >= 1 && s[i - f[i]] == s[i + f[i]]) n       f[i]++; n     if (i + f[i] - 1 > r) { n       r = i + f[i] - 1; n       c = i; n     } n   } n } n int chk(int l, int r) { n   if (l >= r) return 1; n   int p = l + r; n   int ll = p - f[p] + 1, rr = p + f[p] - 1; n   return ll <= 2 * l && rr >= 2 * r; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) b[i] = a[i] - a[i - 1]; n   if (n == 1) { n     printf( 1 n%d n , (a[1] + a[1]) % m); n     return 0; n   } n   manacher(); n   for (int i = 1; i < n; i++) { n     if ((a[1] + a[i]) % m == (a[i + 1] + a[n]) % m) n       if (chk(2, i) && chk(i + 2, n)) t.push_back((a[1] + a[i]) % m); n   } n   if (chk(2, n)) t.push_back((a[1] + a[n]) % m); n   sort(t.begin(), t.end()); n   printf( %d n , t.size()); n   for (int i = 0; i < t.size(); i++) printf( %d  , t[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans, m; n queue<int> q; n int main() { n   cin >> n; n   int *a = new int[n + 1]; n   int *j = new int[n + 1]; n   memset(a, 0, sizeof(int) * (n + 1)); n   for (int i = 1; i <= n; i++) { n     cin >> j[i]; n     a[j[i]]++; n     if (a[j[i]] > 1) ans++; n   } n   cout << ans << endl; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0) q.push(i); n   } n   for (int i = 1; i <= n; i++) { n     if (q.empty()) { n       cout << j[i] <<    ; n     } else { n       if (a[j[i]] > 1) { n         if (j[i] < q.front()) { n           cout << j[i] <<    ; n           a[j[i]] = 0; n         } else { n           a[j[i]]--; n           m = q.front(); n           cout << m <<    ; n           q.pop(); n         } n       } else if (a[j[i]] == 1) { n         cout << j[i] <<    ; n       } else { n         m = q.front(); n         cout << m <<    ; n         q.pop(); n       } n     } n   } n   delete[] a; n   delete[] j; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 5; n int a[maxn]; n int main() { n   int n; n   cin >> n; n   int mn = 0x3f3f3f3f; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     mn = min(a[i], mn); n   } n   int ans = 0x3f3f3f3f; n   for (int i = 1; i <= n; i++) { n     if (i != 1) ans = min(min(a[i], a[1]) / abs(1 - i), ans); n     if (i != n) ans = min(min(a[i], a[n]) / abs(n - i), ans); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void xx(int *a, int n) { n   for (int j = 1; j < n; ++j) n     for (int i = j; i <= n; ++i) n       if (a[j] < a[i]) { n         int t = a[i]; n         a[i] = a[j]; n         a[j] = t; n       } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n; n   cin >> t; n   for (int i = 1; i <= t; ++i) { n     cin >> n; n     int a[n]; n     for (int j = 1; j <= n; ++j) cin >> a[j]; n     xx(a, n); n     for (int j = 1; j <= n; ++j) cout << a[j] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(long long x, long long y, long long p, long long q, long long t) { n   return p * t >= x && q * t - p * t >= y - x; n } n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long x, y, p, q, m; n     cin >> x >> y >> p >> q; n     long long l = -1; n     long long r = (long long)1e9; n     if (!check(x, y, p, q, r)) { n       printf( -1 n ); n       continue; n     } n     while (r - l > 1) { n       m = l + (r - l) / 2; n       if (check(x, y, p, q, m)) n         r = m; n       else n         l = m; n     } n     printf( %lld n , r * q - y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int INF = 0x3f3f3f3f; n const int iinf = 1 << 30; n const long long linf = 2e18; n const int mod = 1000000007; n const double eps = 1e-7; n void douout(double x) { printf( %lf n , x + 0.0000000001); } n template <class T> n void print(T a) { n   cout << a << endl; n   exit(0); n } n template <class T> n void chmin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T &a, T b) { n   if (a < b) a = b; n } n void add(int &a, int b) { a = a + b < mod ? a + b : a + b - mod; } n void sub(int &a, int b) { a = (a - b + mod) % mod; } n void mul(int &a, int b) { a = (long long)a * b % mod; } n int addv(int a, int b) { return (a += b) >= mod ? a -= mod : a; } n int subv(int a, int b) { return (a -= b) < 0 ? a += mod : a; } n int mulv(int a, int b) { return (long long)a * b % mod; } n int read() { n   int f = 1, x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int pw(int a, int b) { n   int s = 1; n   for (; b; b >>= 1, a = (long long)a * a % mod) n     if (b & 1) s = (long long)s * a % mod; n   return s; n } n int n, ans; n int val[N] = {1}, ans1[N], ans2[N], ans3[N], p[N]; n void dfs(int x) { n   if (val[x] == n && x == ans) { n     printf( %d n , ans); n     for (int i = (0); i <= (ans - 1); i++) n       if (~ans2[i]) n         printf( lea e%cx, [e%cx + %d*e%cx] n , ans1[i] +  a , ans2[i] +  a , n                p[i], ans3[i] +  a ); n       else n         printf( lea e%cx, [%d*e%cx] n , ans1[i] +  a , p[i], ans3[i] +  a ); n     exit(0); n   } n   if (x == ans) return; n   for (int i = (0); i <= (x); i++) n     for (int j = (-1); j <= (x); j++) n       for (int k = 1; k < 9; k <<= 1) { n         if (j + 1) n           val[x + 1] = val[i] * k + val[j]; n         else n           val[x + 1] = val[i] * k; n         ans1[x] = x + 1; n         ans2[x] = j; n         ans3[x] = i; n         p[x] = k; n         dfs(x + 1); n       } n } n signed main() { n   scanf( %d , &n); n   for (ans = 0; ans <= n; ans++) dfs(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000; n const double eps = 1e-9; n long long INF = 1e9; n const int OO = 0x3f3f3f3f; n int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; n int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; n bool comp(pair<int, int> a, pair<int, int> b) { n   if (a.first < b.first) n     return true; n   else if (a.first > b.first) n     return false; n   else n     return a.first < b.first; n } n void read(string str, int node) { n   stringstream ss; n   int number; n   ss << str; n   while (ss >> number) cout << number << endl; n } n long long fp(long long b, long long p) { n   if (p == 0) return 1; n   if (p % 2 == 0) return fp((b * b) % INF, p / 2); n   return (fp(b, p - 1) * b) % INF; n } n unsigned long long read_binary(string s) { n   unsigned long long x = 0, tmp = 1; n   for (int i = s.size() - 1; i >= 0; i--) { n     x = (x + tmp * (s[i] -  0 )) % INF; n     tmp = (tmp * 2) % INF; n   } n   return x; n } n long long com(long long x, long long y) { n   long long sum = 1; n   for (int i = 1; i <= y; i++) { n     sum *= x; n     x--; n     sum /= i; n   } n   return sum; n } n long long gcd(long long n1, long long n2) { n   if (n2 != 0) n     return gcd(n2, n1 % n2); n   else n     return n1; n } n int setBit(int num, int idx, int val = 1) { n   return (val) ? (num | (1 << idx)) : (num & ~(1 << idx)); n } n int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; } n int cntBit(int num) { n   int ret = 0; n   while (num) { n     if (num % 2) ret++; n     num /= 2; n   } n   return ret; n } n bool sum[1500]; n void sieve() { n   sum[0] = sum[1] = 1; n   for (int i = 2; i * i < 1002; i++) { n     if (!sum[i]) { n       for (int j = 2 * i; j < 1002; j += i) { n         sum[j] = 1; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long n; n   cin >> n; n   string s = to_string(n); n   long long mi = INF; n   for (int i = 0; i < (1 << s.size()); ++i) { n     int cnt = 0; n     string k; n     for (int j = 0; j < s.size(); j++) { n       if (i & (1 << j)) { n         k += s[j]; n         cnt++; n       } n     } n     if (k[0] ==  0 ) continue; n     long long val; n     istringstream ss(k); n     ss >> val; n     double d = sqrt(val); n     int ii = sqrt(val); n     if (d == ii) { n       mi = min(mi, (long long)s.size() - cnt); n     } n   } n   if (mi == INF) n     cout << -1 << endl; n   else n     cout << mi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long xorsum(long long n, long long k) { n   if (k == 1) return n; n   int msb = (int)log2((double)n); n   if ((1ll << msb) > n) msb--; n   return (2ll << msb) - 1; n } n int main() { n   long long n, k; n   cin >> n >> k; n   cout << xorsum(n, k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n void manupulate(string &str) { n   for (long long int i = 0; i < str.size(); ++i) str[i] -= 48; n } n bool isprime(long long int n) { n   if (n <= 1) return false; n   for (long long int i = 2; i * i <= n; ++i) n     if (n % i == 0) return false; n   return true; n } n long long int expo(long long int a, long long int n) { n   if (n == 0) return 1; n   if (n == 1) return a; n   long long int tmp = expo(a, n / 2); n   tmp = (tmp * tmp) % (long)(1e9 + 7); n   if (n & 1) tmp = (tmp * a) % (long)(1e9 + 7); n   return tmp; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     string str; n     cin >> str; n     set<array<long long int, 4> > s; n     long long int ans = 0; n     long long int x = 0, y = 0; n     for (long long int i = 0; i < str.size(); ++i) { n       long long int x1 = x, y1 = y; n       if (str[i] ==  N ) n         ++y1; n       else if (str[i] ==  E ) n         ++x1; n       else if (str[i] ==  S ) n         --y1; n       else n         --x1; n       array<long long int, 4> tmp = {x, y, x1, y1}; n       array<long long int, 4> tmp2 = {x1, y1, x, y}; n       if (s.find(tmp) != s.end() || s.find(tmp2) != s.end()) n         ++ans; n       else { n         s.insert(tmp); n         s.insert(tmp2); n         ans += 5; n       } n       x = x1, y = y1; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int x = 0, f = 1; n   register char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + (ch ^  0 ); n     ch = getchar(); n   } n   return f ? x : -x; n } n const int mod = 998244353; n int n, l[55], r[55], inv[105], b[105], t, f[55][105], s[55][105]; n inline void inc(int& x, int y) { x = x + y < mod ? x + y : x + y - mod; } n inline int power(int b, int n) { n   int ans = 1; n   for (; n; n >>= 1, b = 1ll * b * b % mod) n     if (n & 1) ans = 1ll * ans * b % mod; n   return ans; n } n int main() { n   n = read(); n   for (int i = (1), _ed = (n); i <= _ed; ++i) n     l[i] = read(), r[i] = read(), b[++t] = l[i], b[++t] = ++r[i]; n   inv[0] = inv[1] = 1; n   for (int i = (2), _ed = (n + n); i <= _ed; ++i) n     inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; n   sort(b + 1, b + t + 1), t = unique(b + 1, b + t + 1) - b - 1; n   for (int i = (1), _ed = (n); i <= _ed; ++i) n     l[i] = lower_bound(b + 1, b + t + 1, l[i]) - b, n     r[i] = lower_bound(b + 1, b + t + 1, r[i]) - b; n   f[0][t] = s[0][t] = 1; n   for (int i = (1), _ed = (n + 1); i <= _ed; ++i) n     for (int j = (t - 1), _ed = (1); j >= _ed; --j) { n       s[i - 1][j] = (s[i - 1][j + 1] + f[i - 1][j]) % mod; n       if (l[i] > j || r[i] <= j) continue; n       int L = b[j + 1] - b[j], C = 1, cnt = 0; n       for (int k = (i - 1), _ed = (0); k >= _ed; --k) { n         ++cnt; n         C = 1ll * C * (L + cnt - 1) % mod * inv[cnt] % mod; n         inc(f[i][j], 1ll * s[k][j + 1] * C % mod); n         if (l[k] > j || r[k] <= j) break; n       } n     } n   int ans = s[n][1]; n   for (int i = (1), _ed = (n); i <= _ed; ++i) n     ans = 1ll * ans * power(b[r[i]] - b[l[i]], mod - 2) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n enum { N = 201 }; n int n; n int where[N][N]; n int cnt[N] = {}; n int what[N]; n int set[N][N]; n int sn[N] = {}; n int eq(int i, int j) { n   int k; n   for (k = 0; k < n - 1; ++k) { n     if (where[i][k] != where[j][k]) return 0; n   } n   return 1; n } n int main(void) { n   scanf( %d , &n); n   int i, j, k, x; n   if (n == 2) { n     scanf( %d %d , &k, &x); n     printf( 1 %d n%d  , x, k - 1); n     for (i = 0; i < k - 1; ++i) { n       scanf( %d , &x); n       printf( %d  , x); n     } n     printf(  n ); n     return 0; n   } n   int nn = n * (n - 1) / 2; n   for (i = 0; i < nn; ++i) { n     scanf( %d , &k); n     for (j = 0; j < k; ++j) { n       scanf( %d , &x); n       where[x][cnt[x]] = i; n       ++cnt[x]; n     } n   } n   for (i = 1; i < N; ++i) { n     if (cnt[i] == 0) continue; n     what[i] = i; n     for (j = 1; j < i; ++j) { n       if (cnt[j] == 0) continue; n       if (eq(j, i)) { n         what[i] = j; n         break; n       } n     } n     set[what[i]][sn[what[i]]] = i; n     ++sn[what[i]]; n   } n   for (i = 1; i < N; ++i) { n     if (sn[i] == 0) continue; n     printf( %d  , sn[i]); n     for (j = 0; j < sn[i]; ++j) { n       printf( %d  , set[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 42; n int n, m; n char s[N]; n long long dp[N][N][2], nex[N]; n void kmp_init() { n   int j = 0; n   nex[1] = 0; n   nex[0] = -1; n   for (int i = 1; i < n; i++) { n     while (j >= 0 && s[i + 1] != s[j + 1]) j = nex[j]; n     if (j == -1 || s[i + 1] == s[j + 1]) j++; n     nex[i + 1] = j; n   } n } n int getNex(int pos, int val) { n   if (pos == 0) return val == s[1] -  0  ? 1 : 0; n   if (s[pos + 1] ==  0  + val) return pos + 1; n   return getNex(nex[pos], val); n } n long long dfs(int pre, int now, int matched_len, int isok) { n   if (now == m + 1) { n     return matched_len == pre && isok; n   } n   if (dp[now][matched_len][isok] != -1) return dp[now][matched_len][isok]; n   long long ans = 0; n   ans += dfs(pre, now + 1, getNex(matched_len, 0), n              isok | (getNex(matched_len, 0) == n)); n   ans += dfs(pre, now + 1, getNex(matched_len, 1), n              isok | (getNex(matched_len, 1) == n)); n   return dp[now][matched_len][isok] = ans; n } n int main() { n   scanf( %d %s , &m, s + 1); n   n = strlen(s + 1); n   kmp_init(); n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     memset(dp, -1, sizeof(dp)); n     ans += dfs(i, 1, i, 0); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 100; n int n, p, k; n int dp[2][maxn][55][55]; n int maks[2][2][maxn][55]; n int tmaks[2][maxn]; n int question[2][maxn]; n int main() { n   scanf( %d %d %d , &n, &p, &k); n   int cnt, x; n   int res = 0; n   for (int i = 0; i < 2; ++i) { n     scanf( %d , &cnt); n     while (cnt--) { n       scanf( %d , &x); n       question[i][x] = 1; n     } n   } n   if (p > 2 * ceil(n / k)) { n     for (int i = 1; i <= n; ++i) res += (question[0][i] || question[1][i]); n     cout << res << endl; n     return 0; n   } n   int now = 0; n   memset(dp, 0xc0, sizeof dp); n   memset(maks, 0xc0, sizeof maks); n   memset(tmaks, 0xc0, sizeof tmaks); n   maks[1][0][0][0] = maks[1][1][0][0] = tmaks[1][0] = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= p; ++j) { n       tmaks[now][j] = -0x3f3f3f3f; n       for (int r = 0; r <= k; ++r) { n         maks[now][0][j][r] = maks[now][1][j][r] = -0x3f3f3f3f; n         for (int r1 = 0; r1 <= k; ++r1) dp[now][j][r][r1] = -0x3f3f3f3f; n       } n     } n     for (int j = 0; j <= p; ++j) { n       for (int r1 = 0; r1 <= k; ++r1) { n         for (int r2 = 0; r2 <= k; ++r2) { n           int &cur = dp[now][j][r1][r2]; n           if (r1 > 1 && r2 > 1 && j >= 2) n             cur = max(cur, dp[!now][j][r1 - 1][r2 - 1] + n                                (question[0][i] || question[1][i])); n           if ((r1 == 0 || r1 == 1) && j >= r1 && !(r2 == 0 || r2 == 1)) n             cur = max(cur, maks[!now][0][j - r1][r2 - 1] + n                                (question[1][i] || (question[0][i] && r1))); n           if ((r2 == 0 || r2 == 1) && j >= r2 && !(r1 == 0 || r1 == 1)) n             cur = max(cur, maks[!now][1][j - r2][r1 - 1] + n                                ((question[1][i] && r2) || question[0][i])); n           if ((r1 == 0 || r1 == 1) && (r2 == 0 || r2 == 1) && j >= r1 + r2) n             cur = max(cur, tmaks[!now][j - r1 - r2] + ((question[0][i] && r1) || n                                                        (question[1][i] && r2))); n           tmaks[now][j] = max(tmaks[now][j], cur); n           maks[now][0][j][r2] = max(maks[now][0][j][r2], cur); n           maks[now][1][j][r1] = max(maks[now][1][j][r1], cur); n           res = max(res, cur); n         } n       } n     } n     now = !now; n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev; n const int INF = INT_MAX; n const long long LINF = LLONG_MAX; n const int MOD = 1000000000 + 7; n const double EPS = 1e-9; n const double PI = acos(-1); n const int di[]{0, -1, 0, 1, -1, -1, 1, 1}; n const int dj[]{1, 0, -1, 0, 1, -1, -1, 1}; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   long long n, m; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   cin >> m; n   vector<long long> q(m); n   for (int i = 0; i < m; ++i) { n     cin >> q[i]; n   } n   vector<long long> sum(n + 1, 0); n   for (int i = 0; i < n; ++i) { n     sum[i + 1] = sum[i] + a[i]; n   } n   sort((a).begin(), (a).end(), greater<long long>()); n   for (int i = 0; i < m; ++i) { n     long long ans = sum[n] - a[q[i] - 1]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e18 + 7; n const long long N = 2e6 + 5; n int main() { n   long long i, j, k, l = 0; n   long long n, m, t; n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> k, l += k; n   if (l % n) n     cout << n - 1 << endl; n   else n     cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[100005]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &num[i]); n   } n   sort(num, num + n); n   if (n < k) { n     printf( %d n , num[n - 1]); n     return 0; n   } else { n     vector<int> ans; n     for (int i = 0; i < n - k; i++) { n       ans.push_back(num[i] + num[2 * n - 2 * k - 1 - i]); n     } n     for (int i = 2 * n - 2 * k; i < n; i++) ans.push_back(num[i]); n     sort(ans.begin(), ans.end()); n     printf( %d n , ans[ans.size() - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int m5 = 100042; n int main(void) { n   string s, ans =   ; n   getline(cin, s); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  . ) { n       if (i == 0) { n         ans +=  ... ; n       } else { n         ans +=   ... ; n       } n       i += 2; n     } else if (s[i] ==  , ) { n       ans +=  ,  ; n     } else { n       ans += s[i]; n     } n   } n   while (true) { n     int pos = ans.find(    ); n     if (pos == string::npos) { n       break; n     } n     ans.erase(ans.begin() + pos, ans.begin() + pos + 1); n   } n   if (ans[ans.size() - 1] ==    ) { n     ans.erase(ans.begin() + ans.size() - 1); n   } n   string ans2 =   ; n   for (int i = 0; i < ans.size(); i++) { n     if (ans[i] ==     && isdigit(ans[i - 1]) && ans[i + 1] ==  , ) { n       continue; n     } else if (ans[i] ==     && isdigit(ans[i + 1]) && ans[i - 1] ==  . ) { n       continue; n     } else if (ans[i] ==     && ans[i + 1] ==  ,  && ans[i - 1] ==  . ) { n       continue; n     } else { n       ans2 += ans[i]; n     } n   } n   cout << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int minn(int, int, int); n int main() { n   int x1, x2, x3; n   cin >> x1 >> x2 >> x3; n   int d1 = abs(x2 - x1) + abs(x3 - x1); n   int d2 = abs(x1 - x2) + abs(x3 - x2); n   int d3 = abs(x3 - x1) + abs(x3 - x2); n   cout << minn(d1, d2, d3) << endl; n   return 0; n } n int minn(int a, int b, int c) { n   int m = a; n   if (b < m) m = b; n   if (c < m) m = c; n   return m; n } n


#include <bits/stdc++.h> n using namespace std; n long long sqr(long long x) { return x * x; } n int mysqrt(long long x) { n   int l = 0, r = 1e9 + 1; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (m * (long long)m <= x) n       l = m; n     else n       r = m; n   } n   return l; n } n mt19937 rnd(1227); n mt19937_64 rndll(12365); n long long AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993; n long long myrand() { n   long long ZR = (XR * AR + YR * BR + CR) % MODR; n   XR = YR; n   YR = ZR; n   return ZR; n } n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n int gcdex(int a, int b, int &x, int &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   int x1, y1; n   int ret = gcdex(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return ret; n } n const int Mod = 1e9 + 7; n int Bpow(int x, int y) { n   int ret = 1; n   int w = x; n   while (y) { n     if (y & 1) ret = (ret * (long long)w) % Mod; n     w = (w * (long long)w) % Mod; n     y >>= 1; n   } n   return ret; n } n int Bdiv(int x) { n   int a, b; n   gcdex(x, Mod, a, b); n   if (a < 0) a += Mod; n   return a; n } n int Bdiv(int x, int y) { return (x * (long long)Bpow(y, Mod - 2)) % Mod; } n void setmin(int &x, int y) { x = min(x, y); } n void setmax(int &x, int y) { x = max(x, y); } n void setmin(long long &x, long long y) { x = min(x, y); } n void setmax(long long &x, long long y) { x = max(x, y); } n const long long llinf = 2e18 + 100; n const double eps = 1e-9; n const int maxn = 1e5 + 100, maxw = 1e6 + 100, inf = 2e9 + 100, sq = 300, n           mod = 1e9 + 7, LG = 17; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int w = 1; n   for (int i = 2; i <= n; i++) n     if (i + (n + i - 1) / i < w + (n + w - 1) / w) w = i; n   for (int i = n; i > 0; i -= w) { n     for (int j = max(1, i - w + 1); j <= i; j++) cout << j <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   scanf( %d%d , &m, &n); n   int t[m][n]; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) scanf( %d , &t[i][j]); n   } n   int latest[m]; n   fill(latest, latest + m, 0); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       latest[j] += t[j][i]; n       if (j != m - 1) latest[j + 1] = max(latest[j], latest[j + 1]); n     } n   } n   for (int i = 0; i < m; i++) { n     printf( %d , latest[i]); n     if (i != m - 1) putchar(   ); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<int, vector<int>> mp; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     mp[a].push_back(i); n   } n   vector<pair<int, int>> ans; n   int cnt = 0; n   for (auto ele : mp) { n     int a = ele.first; n     vector<int> pos = ele.second; n     if (pos.size() == 1) { n       ans.push_back({a, 0}); n     } else { n       int diff = pos[1] - pos[0]; n       bool can = true; n       for (int i = 2; i <= pos.size() - 1; i++) { n         if (pos[i] - pos[i - 1] != diff) { n           can = false; n           break; n         } n       } n       if (can) { n         ans.push_back({a, diff}); n       } n     } n   } n   cout << ans.size() << endl; n   for (auto& ele : ans) { n     cout << ele.first <<     << ele.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, w, nm; n char ls[1000][10][10]; n int _d[1000][1000]; n int used[1000], res[1000], sz = 0, c = 0; n int main() { n   cin >> n >> m >> k >> w; n   nm = n * m; n   c = nm; n   for (int i = 0; i < k; i++) n     for (int y = 0; y < n; y++) n       for (int x = 0; x < m; x++) cin >> ls[i][y][x]; n   for (int a = 0; a < k; a++) n     for (int b = 0; b < k; b++) { n       int res = 0; n       for (int i = 0; i < n; i++) n         for (int j = 0; j < m; j++) n           if (ls[a][i][j] != ls[b][i][j]) res++; n       _d[a][b] = res; n     } n   res[0] = 0; n   used[0] = 1; n   sz = 1; n   stringstream ss; n   ss <<  1 0  << endl; n   while (sz < k) { n     int mini = -1, minval = -1, minj = -1, type = 0; n     for (int j = 0; j < sz; j++) { n       for (int i = 0; i < k; i++) { n         if (used[i]) continue; n         int a = _d[res[j]][i] * w, t = min(a, nm); n         if (t < minval || mini < 0) { n           mini = i; n           minval = t; n           minj = -1; n           if (a < nm) { n             minj = res[j]; n             type = 1; n           } n         } n       } n     } n     res[sz++] = mini; n     used[mini] = 1; n     ss << mini + 1 <<     << minj + 1 << endl; n     c += minval; n   } n   cout << c << endl << ss.str(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 15e4 + 5; n int n, b[N]; n long long a[N]; n int prev(int x) { n   int y = x - 1; n   if (y == 0) y = n; n   return y; n } n int nxt(int x) { return x % n + 1; } n int main() { n   scanf( %d , &n); n   bool f = true; n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (int i = 2; i <= n; i++) n     if (b[i] != b[i - 1]) f = false; n   if (f) { n     if (b[1] != 0) return !printf( NO ); n     puts( YES ); n     for (int i = 1; i <= n; i++) printf( %d  , 3122002); n     return 0; n   } n   int pos; n   for (int i = 1; i <= n; i++) { n     if (b[i] > b[prev(i)]) { n       pos = i; n       break; n     } n   } n   a[pos] = b[pos]; n   for (int i = pos - 1; i >= 1; i--) { n     int k = max(1LL, (b[prev(i)] - b[i] + a[nxt(i)]) / a[nxt(i)]); n     a[i] = 1LL * a[nxt(i)] * k + b[i]; n   } n   for (int i = n; i > pos; i--) { n     int k = max(1LL, (b[prev(i)] - b[i] + a[nxt(i)]) / a[nxt(i)]); n     a[i] = 1LL * a[nxt(i)] * k + b[i]; n   } n   puts( YES ); n   for (int i = 1; i <= n; i++) printf( %lld  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str; n int main() { n   long long int i, j, t, n; n   cin >> t; n   while (t--) { n     cin >> str; n     int ar[4]; n     for (i = 0; i < 4; i++) { n       ar[i] = 0; n     } n     int l = str.length(); n     int count = 0; n     int mn = l + 1; n     for (i = 0, j = 0; i <= j && j < l; j++) { n       ar[str[j] -  1 ]++; n       if (ar[str[j] -  1 ] == 1) { n         count++; n       } n       if (count == 3) { n         if (j - i + 1 < mn) { n           mn = j - i + 1; n         } n         while (i <= j && count == 3) { n           ar[str[i] -  1 ]--; n           if (ar[str[i] -  1 ] == 0) { n             count--; n           } n           i++; n           if (count == 3 && j - i + 1 < mn) { n             mn = j - i + 1; n           } n         } n       } n     } n     if (mn > l) { n       cout << 0 <<   n ; n     } else { n       cout << mn <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, c[6][223456], child[223456]; n double ans[223456], res = 0.0; n vector<pair<int, double> > ke[223456]; n pair<pair<int, int>, double> way[223456]; n int dad[223456]; n void dfs1(int u, int par) { n   child[u] = 1; n   dad[u] = par; n   for (int i = 0; i < ke[u].size(); i++) { n     int v = ke[u][i].first; n     if (v == par) continue; n     dfs1(v, u); n     child[u] += child[v]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   int u, v; n   double w; n   for (int i = 1; i < n; i++) { n     cin >> u >> v >> w; n     ke[u].push_back(make_pair(v, w)); n     ke[v].push_back(make_pair(u, w)); n     way[i] = make_pair(make_pair(u, v), w); n   } n   dfs1(1, 0); n   res = 0; n   for (int i = 1; i < n; i++) { n     int u1 = way[i].first.first; n     int u2 = way[i].first.second; n     if (dad[u2] == u1) swap(u1, u2); n     double we = way[i].second; n     res += we * (child[u1] * (n - child[u1])); n   } n   int q; n   cin >> q; n   while (q--) { n     cin >> u >> w; n     int u1 = way[u].first.first; n     int u2 = way[u].first.second; n     if (dad[u2] == u1) swap(u1, u2); n     double we = way[u].second; n     res -= (we - w) * double(child[u1] * (n - child[u1])); n     way[u].second = w; n     cout << fixed << setprecision(8) << res / double(n * (n - 1) / 6.0) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n void query(int x) { n   cout <<  ? ; n   for (int i = 1; i <= k + 1; ++i) n     if (i != x) cout <<     << i; n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   int x, y, px, py; n   query(k + 1); n   cin >> px >> x; n   query(px); n   cin >> py >> y; n   int m = 1; n   if (y > x) { n     for (int i = 1; i <= k; ++i) { n       if (i != px) { n         query(i); n         int pa, a; n         cin >> pa >> a; n         if (a > x) ++m; n       } n     } n   } else { n     for (int i = 1; i <= k; ++i) { n       if (i != px) { n         query(i); n         int pa, a; n         cin >> pa >> a; n         if (a > y) ++m; n       } n     } n   } n   cout <<  !   << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265358979323846; n void solve() { n   long long n, m, a, b, k, x, y, queries; n   string s; n   cin >> n >> s; n   s =  G  + s; n   long long i = 1; n   while (i <= n) { n     if (s[i] ==  8 ) break; n     i++; n   } n   if (i + 10 <= n) n     cout <<  YES ; n   else n     cout <<  NO ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     solve(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n inline long long read() { n   char c; n   long long x; n   for (c = getchar(); !isdigit(c); c = getchar()) n     ; n   for (x = 0; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   return x; n } n const int N = 2e5 + 5; n int n, q, d[N], f[N]; n long long a[N], b[N], c[N], s[N]; n std::multiset<long long> set[N], all; n void modify(int u, int sgn, int flag) { n   int v = f[u]; n   if (!set[v].empty()) { n     if (flag || sgn < 0) { n       all.erase(all.find(*set[v].begin() + c[v])); n       all.erase(all.find(*set[v].rbegin() + c[v])); n     } n   } n   if (sgn == 1) { n     set[v].insert(b[u] + s[u]); n   } n   if (sgn == -1) { n     set[v].erase(set[v].find(b[u] + s[u])); n   } n   if (!set[v].empty()) { n     if (flag || sgn > 0) { n       assert(!set[v].empty()); n       all.insert(*set[v].begin() + c[v]); n       all.insert(*set[v].rbegin() + c[v]); n     } n   } n } n int main() { n   n = read(); n   q = read(); n   for (int u = 1; u <= n; u++) { n     a[u] = read(); n     d[u] = 1; n   } n   for (int u = 1; u <= n; u++) { n     f[u] = read(); n     d[f[u]]++; n   } n   for (int u = 1; u <= n; u++) { n     c[u] = a[u] / (d[u] + 1); n     b[u] = a[u] - c[u] * d[u]; n     s[f[u]] += c[u]; n   } n   for (int u = 1; u <= n; u++) { n     modify(u, 1, 1); n   } n   for (int i = 0; i < q; i++) { n     int opt = read(); n     if (opt == 1) { n       int u = read(), v = read(); n       modify(u, -1, 0); n       modify(f[u], -1, 0); n       modify(f[f[u]], -1, 0); n       d[f[u]]--; n       s[f[u]] -= c[u]; n       s[f[f[u]]] -= c[f[u]]; n       c[f[u]] = a[f[u]] / (d[f[u]] + 1); n       b[f[u]] = a[f[u]] - c[f[u]] * d[f[u]]; n       s[f[f[u]]] += c[f[u]]; n       modify(u, 2, 0); n       modify(f[u], 1, 0); n       modify(f[f[u]], 1, 0); n       f[u] = v; n       modify(u, -2, 0); n       modify(f[u], -1, 0); n       modify(f[f[u]], -1, 0); n       d[f[u]]++; n       s[f[u]] += c[u]; n       s[f[f[u]]] -= c[f[u]]; n       c[f[u]] = a[f[u]] / (d[f[u]] + 1); n       b[f[u]] = a[f[u]] - c[f[u]] * d[f[u]]; n       s[f[f[u]]] += c[f[u]]; n       modify(u, 1, 0); n       modify(f[u], 1, 0); n       modify(f[f[u]], 1, 0); n     } else if (opt == 2) { n       int u = read(); n       printf( %lld n , b[u] + c[f[u]] + s[u]); n     } else { n       printf( %lld %lld n , *all.begin(), *all.rbegin()); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000000 ) n using namespace std; n int n, l[30], m[30], w[30]; n int tw[30], P1, P2; n map<pair<long long, long long>, pair<long long, long long> > mp; n long long best = -(1ll << 58), msk1, msk2; n void rec(int p, int msk, long long L, long long M, long long W) { n   if (p == P1) { n     pair<long long, long long> tmp = make_pair(L - M, L - W); n     if (mp.find(tmp) == mp.end()) n       mp[tmp] = make_pair(L, msk); n     else if (mp[tmp].first < L) { n       mp[tmp] = make_pair(L, msk); n     } n     return; n   } n   rec(p + 1, msk * 3, L, M + m[p], W + w[p]); n   rec(p + 1, msk * 3 + 1, L + l[p], M, W + w[p]); n   rec(p + 1, msk * 3 + 2, L + l[p], M + m[p], W); n } n void rec2(int p, int msk, long long L, long long M, long long W) { n   if (p == n) { n     pair<long long, long long> tmp = make_pair(-(L - M), -(L - W)); n     if (mp.find(tmp) == mp.end()) return; n     pair<long long, long long> tmp2 = mp[tmp]; n     long long sum = L + tmp2.first; n     if (sum < best) return; n     best = sum; n     msk1 = tmp2.second; n     msk2 = msk; n     return; n   } n   rec2(p + 1, msk * 3, L, M + m[p], W + w[p]); n   rec2(p + 1, msk * 3 + 1, L + l[p], M, W + w[p]); n   rec2(p + 1, msk * 3 + 2, L + l[p], M + m[p], W); n } n void print(int x, int msk) { n   if (x == 0) return; n   print(x - 1, msk / 3); n   if (msk % 3 == 0) n     printf( MW n ); n   else if (msk % 3 == 1) n     printf( LW n ); n   else n     printf( LM n ); n } n int main() { n   for (int i = 0; i < 30; i++) tw[i] = 1 << i; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d %d , &l[i], &m[i], &w[i]); n   } n   P1 = n / 2, P2 = n - P1; n   rec(0, 0, 0, 0, 0); n   rec2(P1, 0, 0, 0, 0); n   if (best == -(1ll << 58)) n     printf( Impossible n ); n   else { n     print(P1, msk1); n     print(P2, msk2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   long long n, r1, r2, r3, d; n   cin >> n >> r1 >> r2 >> r3 >> d; n   long long a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long adv[n + 1], ret[n + 1]; n   adv[0] = ret[n] = 0; n   ret[n - 1] = min( n       {a[n - 1] * r1 + r3, a[n - 1] * r1 + 2 * r1 + 2 * d, r2 + r1 + 2 * d}); n   for (int i = 1; i <= n; i++) { n     adv[i] = adv[i - 1] + a[i - 1] * r1 + r3 + d; n     if (i - 2 >= 0) n       adv[i] = min(adv[i], adv[i - 2] + min(a[i - 2] * r1 + r1, r2) + n                                min(a[i - 1] * r1 + r1, r2) + 2 * r1 + 4 * d); n   } n   adv[n] -= d; n   for (int i = n - 2; i >= 0; i--) { n     ret[i] = ret[i + 1] + min(a[i] * r1 + 2 * r1, r2 + r1) + 2 * d; n   } n   long long sol = LLONG_MAX; n   for (int i = 0; i <= n; i++) sol = min(sol, adv[i] + ret[i]); n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int t; n   cin >> t; n   for (int _ = 0; _ < (t); ++_) { n     int n; n     cin >> n; n     if (n < 4) { n       cout << -1 <<   n ; n       continue; n     } n     deque<int> p{3, 1, 4}; n     vector<int> nums{2}; n     for (int i = 5; i <= n; ++i) { n       nums.push_back(i); n     } n     for (int i = 0; i < (((int)(nums).size())); ++i) { n       if (abs(p.front() - nums[i]) >= 2 && abs(p.front() - nums[i]) <= 4) { n         p.push_front(nums[i]); n       } else { n         p.push_back(nums[i]); n       } n     } n     for (int i = 0; i < (((int)(p).size())); ++i) { n       if (i) { n         cout <<    ; n       } n       cout << p[i]; n     } n     cout <<   n ; n   } n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int c = 102; n int kezd[c][c], veg[c][c], dp[c][c], n, m, sum; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int db; n     cin >> db; n     for (int j = 1; j <= db; j++) { n       int a, b; n       cin >> a >> b; n       for (int k = a; k <= b; k++) kezd[i][k] = a, veg[i][k] = b; n     } n   } n   for (int j = 0; j < m; j++) n     for (int i = 1; i + j <= m; i++) { n       int maxi = 0; n       for (int k = i; k <= i + j; k++) { n         int cnt = 0; n         for (int l = 1; l <= n; l++) n           cnt += (i <= kezd[l][k] && veg[l][k] <= i + j); n         maxi = max(maxi, cnt * cnt + dp[i][k - 1] + dp[k + 1][i + j]); n       } n       dp[i][i + j] = maxi; n     } n   cout << dp[1][m] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long t; n   cin >> t; n   while (t--) { n     long long n, l, r; n     cin >> n >> l >> r; n     vector<long long> lol = {0}; n     for (long long i = n - 1; i >= 1; i--) { n       lol.push_back(lol.back() + 2 * i); n     } n     for (long long x = l; x <= r; x++) { n       if (x == n * (n - 1) + 1) { n         cout << 1 <<    ; n       } else { n         long long block = n             (long long)(lower_bound(lol.begin(), lol.end(), x) - lol.begin()); n         if (x % 2 == 1) { n           cout << block <<    ; n         } else { n           long long h = x - lol[block - 1]; n           cout << h / 2 + block <<    ; n         } n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t != 0) { n     int n; n     cin >> n; n     long long m = (n - 1) / 2; n     long long sum = m * (m + 1) * (2 * m + 1) / 6; n     long long raspuns = 8 * sum; n     cout << raspuns <<   n ; n     t--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void ensure(bool b) { n   if (!b) { n     cout << 0 << endl; n     exit(0); n   } n } n void solve() { n   int n; n   cin >> n; n   vector<vector<int>> g(n); n   vector<int> e(2 * n - 2); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].emplace_back(i << 1); n     e[i << 1] = y; n     g[y].emplace_back(i << 1 | 1); n     e[i << 1 | 1] = x; n   } n   vector<int> dep(n), pa(n, -1); n   function<void(int, int)> dfs = [&](int u, int p) { n     for (int i : g[u]) n       if ((i ^ p) != 1) { n         int v = e[i]; n         dep[v] = dep[u] + 1; n         pa[v] = i; n         dfs(v, i); n       } n   }; n   dfs(0, -1); n   auto get_path = [&](int u, int v) { n     vector<int> left, right; n     while (u != v) { n       if (dep[u] > dep[v]) { n         left.push_back(pa[u] ^ 1); n         u = e[pa[u] ^ 1]; n       } else { n         right.push_back(pa[v]); n         v = e[pa[v] ^ 1]; n       } n     } n     reverse(right.begin(), right.end()); n     for (auto& x : right) { n       left.push_back(x); n     } n     return left; n   }; n   int total_diff = 0; n   const int HEAD = -2, TAIL = -3; n   vector<int> S(n, -1), T(n, -1), pre(2 * n - 2, -1), nxt(2 * n - 2, -1); n   auto limit_path = [&](int u, int v) { n     auto p = get_path(u, v); n     int len = p.size(); n     total_diff += len; n     ensure(total_diff <= 2 * n - 2); n     int i; n     i = p[0]; n     ensure(S[u] == -1 && pre[i] == -1); n     pre[i] = HEAD; n     S[u] = i; n     i = p.back() ^ 1; n     ensure(T[v] == -1 && nxt[i] == -1); n     nxt[i] = TAIL; n     T[v] = i; n     for (int _ = 0, j; _ + 1 < len; _++) { n       i = p[_], j = p[_ + 1]; n       u = e[i]; n       i ^= 1; n       ensure(nxt[i] == -1 && pre[j] == -1); n       nxt[i] = j; n       pre[j] = i; n     } n   }; n   for (int v = 0; v < n; v++) { n     int u; n     cin >> u; n     u--; n     if (u == -1) continue; n     ensure(u != v); n     limit_path(u, v); n   } n   long long res = 1; n   const long long MOD = 1e9 + 7; n   vector<bool> trk(2 * n - 2, false); n   for (int u = 0; u < n; u++) { n     int cnt = 0; n     int vis = 0; n     if (S[u] != -1) { n       int i = S[u]; n       trk[i] = true; n       vis++; n       while (nxt[i] >= 0) { n         i = nxt[i]; n         ensure(!trk[i]); n         trk[i] = true; n         vis++; n       } n       if (T[u] == i) { n         ensure(vis == g[u].size()); n       } n       cnt++; n     } n     for (int i : g[u]) n       if (pre[i] == -1) { n         trk[i] = true; n         vis++; n         while (nxt[i] >= 0) { n           i = nxt[i]; n           ensure(!trk[i]); n           trk[i] = true; n           vis++; n         } n         cnt++; n       } n     ensure(vis == g[u].size()); n     if (S[u] != -1) cnt--; n     if (T[u] != -1) cnt--; n     for (int i = 1; i <= cnt; i++) { n       (res *= i) %= MOD; n     } n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   std::map<long long, long long> m; n   for (int i = 0; i < n; ++i) { n     m[a[i]]++; n   } n   long long l = 1, r = n / k, mid; n   multiset<long long> ans, s; n   multiset<long long>::iterator it; n   map<long long, long long> m1; n   while (l <= r) { n     mid = l + (r - l) / 2; n     long long c = 0; n     m1 = m; n     s.clear(); n     for (int i = 0; i < n; ++i) { n       if (m1[a[i]] >= mid) { n         s.insert(a[i]); n         c++; n         m1[a[i]] -= mid; n       } n       if (c == k) { n         break; n       } n     } n     if (c >= k) { n       ans = s; n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   for (it = ans.begin(); it != ans.end(); it++) { n     cout << *it <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ,  omit-frame-pointer ,  inline ) n #pragma GCC option( arch=native ,  tune=native ,  no-zero-upper ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n int n, q, fa[66666], aa[66666], kh[66666], a[66666], p[66666], fp[66666], n     siz[66666], ck, pa[66666]; n vector<int> edge[66666]; n int dd[66666]; n void dfs(int u) { n   siz[u] = 1; n   p[++ck] = u; n   fp[u] = ck; n   pa[ck] = fp[fa[u]]; n   dd[ck] = dd[pa[ck]] + 1; n   for (auto v : edge[u]) { n     dfs(v); n     siz[u] += siz[v]; n   } n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , aa + i); n   } n   for (int i = 2; i <= n; ++i) { n     scanf( %d , fa + i); n     edge[fa[i]].push_back(i); n   } n   dfs(1); n   for (int i = 1; i <= n; ++i) { n     a[fp[i]] = aa[i]; n     kh[i] = -50001; n   } n   for (int i = 1; i <= q; ++i) { n     int x; n     scanf( %d , &x); n     int y = siz[x]; n     x = fp[x]; n     int pp = i + 1, *jj = kh + x, *diao = dd + x, *aa = a + x; n     int ans = 0, cnt = 0; n     for (int j = 0; j < y; ++j) { n       if (jj[j] + aa[j] < pp) { n         jj[j] = i; n         ans += diao[j]; n         cnt++; n       } n     } n     ans = ans - cnt * dd[x]; n     printf( %d %d n , ans, cnt); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:256000000 ) n using namespace std; n int main() { n   int n; n   scanf( %d , &(n)); n   vector<int> v1(n), v2(n), v3(n); n   if (!(n % 2)) { n     printf( -1 n ); n     return 0; n   } n   for (int i = (0), endi = (n); i < endi; ++i) v1[i] = i; n   for (int i = (0), endi = (n); i < endi; ++i) v2[i] = (i + 1) % n; n   for (int i = (0), endi = (n); i < endi; ++i) v3[i] = (v1[i] + v2[i]) % n; n   for (int i = (0), endi = ((v1).size()); i < endi; ++i) { n     printf( %d  , (v1[i])); n   }; n   printf(  n ); n   for (int i = (0), endi = ((v2).size()); i < endi; ++i) { n     printf( %d  , (v2[i])); n   }; n   printf(  n ); n   for (int i = (0), endi = ((v3).size()); i < endi; ++i) { n     printf( %d  , (v3[i])); n   }; n   printf(  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   cout.tie(NULL); n   ; n   long long int n, t, x, y, a = 0, b = 0, suma = 0, sumb = 0; n   cin >> n; n   while (n--) { n     cin >> t >> x >> y; n     if (t == 1) { n       a++; n       suma += x; n     } else { n       b++; n       sumb += x; n     } n   } n   if (suma >= 5 * a) n     cout <<  LIVE ; n   else n     cout <<  DEAD ; n   cout << endl; n   if (sumb >= 5 * b) n     cout <<  LIVE ; n   else n     cout <<  DEAD ; n } n