#include <bits/stdc++.h> n using namespace std; n int const MAX = 1e5 + 41; n int n; n int q; n int s[MAX]; n int m[MAX]; n int r[MAX]; n int const BLOCK = 441; n bool sp[MAX]; n int lt[MAX]; n int ltb[MAX]; n vector<pair<int, int> > a[MAX]; n vector<long long> prefsum[MAX]; n vector<long long> prefspeed[MAX]; n int const INF = 2e9 + 41; n void init() { n   for (int i = 0, k = 0; i < n; i += BLOCK, k++) { n     for (int j = 0; j <= BLOCK - 1; j++) { n       if (i + j >= n) break; n       int m = ::m[i + j]; n       int r = ::r[i + j]; n       if (r != 0) { n         a[k].push_back(make_pair((m + r - 1) / r, i + j)); n       } else { n         a[k].push_back(make_pair(INF, i + j)); n       } n     } n     sort(a[k].begin(), a[k].end()); n     long long sm = 0; n     long long sr = 0; n     for (int j = 0; j <= (int)a[k].size() - 1; j++) { n       int m = ::m[a[k][j].second]; n       int r = ::r[a[k][j].second]; n       sm += m; n       sr += r; n       prefsum[k].push_back(sm); n       prefspeed[k].push_back(sr); n     } n   } n } n long long queryblock(int id, int t) { n   long long res = 0; n   int p = (int)(lower_bound(a[id].begin(), a[id].end(), n                             make_pair(t - ltb[id], INF)) - n                 a[id].begin()); n   p--; n   if (p >= 0) { n     res += prefsum[id][p]; n   } n   res += prefspeed[id].back() * (t - ltb[id]); n   if (p >= 0) { n     res -= prefspeed[id][p] * (t - ltb[id]); n   } n   ltb[id] = t; n   return res; n } n long long bruteblock(int id, int l, int r, int t) { n   if (l > r) return 0LL; n   l -= BLOCK * id; n   r -= BLOCK * id; n   sp[id] = false; n   int sh = id * BLOCK; n   for (int i = 0; i <= (int)a[id].size() - 1; i++) { n     lt[sh + i] = max(lt[sh + i], ltb[id]); n   } n   long long res = 0; n   for (int i = l; i <= r; i++) { n     long long add = min((long long)m[sh + i], n                         (long long)::r[sh + i] * (t - lt[sh + i]) + s[sh + i]); n     res += add; n     lt[sh + i] = t; n     s[sh + i] = 0; n   } n   return res; n } n int getblockright(int x) { n   int res = (x / BLOCK + 1) * BLOCK - 1; n   res = min(res, n - 1); n   return res; n } n int getblockleft(int x) { return x / BLOCK * BLOCK; } n void solve() { n   init(); n   scanf( %d , &q); n   for (int o = 1; o <= q; o++) { n     int t, l, r; n     scanf( %d %d %d , &t, &l, &r); n     l--; n     r--; n     long long res = 0; n     int rl = min(r, getblockright(l)); n     res += bruteblock(l / BLOCK, l, rl, t); n     l = rl + 1; n     int lr = max(l, getblockleft(r)); n     res += bruteblock(r / BLOCK, lr, r, t); n     r = lr; n     while (l < r) { n       if (sp[l / BLOCK]) { n         res += queryblock(l / BLOCK, t); n       } else { n         res += bruteblock(l / BLOCK, l, l + BLOCK - 1, t); n         sp[l / BLOCK] = 1; n         ltb[l / BLOCK] = t; n       } n       l += BLOCK; n     } n     printf( %lld n , res); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i <= n - 1; i++) { n     scanf( %d %d %d , &s[i], &m[i], &r[i]); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100005], pn = 11, sumid[500005], lazy2[500005]; n long long sum[500005], sum2[500005], pw[15], lazy[500005]; n set<int> st; n void getsum(int x) { n   sum2[x] = sum[x] - *lower_bound(pw + 1, pw + pn + 1, sum[x]); n } n void pushup(int x) { n   sum2[x] = max(sum2[x * 2], sum2[x * 2 + 1]); n   sum[x] = max(sum[x * 2], sum[x * 2 + 1]); n   if (sum2[x] == sum2[x * 2]) n     sumid[x] = sumid[x * 2]; n   else n     sumid[x] = sumid[x * 2 + 1]; n } n void col(int x, long long v) { n   sum2[x] += v; n   sum[x] += v; n   lazy[x] += v; n } n void col2(int x) { n   sum[x] = 1e17; n   getsum(x); n   lazy[x] = 0; n   lazy2[x] = 1; n } n void pushdown(int x) { n   if (lazy2[x]) { n     col2(x * 2); n     col2(x * 2 + 1); n     lazy2[x] = 0; n   } n   if (lazy[x]) { n     col(x * 2, lazy[x]); n     col(x * 2 + 1, lazy[x]); n     lazy[x] = 0; n   } n } n void build(int x, int l, int r) { n   if (l == r) { n     sum[x] = a[l]; n     getsum(x); n     sumid[x] = l; n     return; n   } n   int mid = (l + r) / 2; n   build(x * 2, l, mid); n   build(x * 2 + 1, mid + 1, r); n   pushup(x); n } n void modify(int x, int l, int r, int ql, int qr) { n   if (l > qr || r < ql) return; n   if (ql <= l && r <= qr) { n     col2(x); n     return; n   } n   pushdown(x); n   int mid = (l + r) / 2; n   modify(x * 2, l, mid, ql, qr); n   modify(x * 2 + 1, mid + 1, r, ql, qr); n   pushup(x); n } n void modifyp(int x, int l, int r, int qx, long long v) { n   if (l > qx || r < qx) return; n   if (l == r) { n     sum[x] = v; n     getsum(x); n     return; n   } n   pushdown(x); n   int mid = (l + r) / 2; n   modifyp(x * 2, l, mid, qx, v); n   modifyp(x * 2 + 1, mid + 1, r, qx, v); n   pushup(x); n } n void modifya(int x, int l, int r, int ql, int qr, long long v) { n   if (l > qr || r < ql) return; n   if (ql <= l && r <= qr) { n     col(x, v); n     return; n   } n   pushdown(x); n   int mid = (l + r) / 2; n   modifya(x * 2, l, mid, ql, qr, v); n   modifya(x * 2 + 1, mid + 1, r, ql, qr, v); n   pushup(x); n } n long long query(int x, int l, int r, int qx) { n   if (l > qx || r < qx) return 0; n   if (l == r) return sum[x]; n   pushdown(x); n   int mid = (l + r) / 2; n   return query(x * 2, l, mid, qx) + query(x * 2 + 1, mid + 1, r, qx); n } n void querysum2(int x, int l, int r, int ql, int qr, long long &ans, int &id) { n   if (l > qr || r < ql) return; n   if (ql <= l && r <= qr) { n     if (sum2[x] > ans) { n       ans = sum2[x]; n       id = sumid[x]; n     } n     return; n   } n   pushdown(x); n   int mid = (l + r) / 2; n   querysum2(x * 2, l, mid, ql, qr, ans, id); n   querysum2(x * 2 + 1, mid + 1, r, ql, qr, ans, id); n   pushup(x); n } n void check(int x) { n   int u = *st.lower_bound(x); n   if (u != x) { n     st.insert(x); n     modifyp(1, 1, n, x, query(1, 1, n, u)); n   } n } n void print(int x, int l, int r) { n   printf( x=%d,l=%d,r=%d,sum=%lld,sum2=%lld n , x, l, r, sum[x], sum2[x]); n   if (l == r) return; n   pushdown(x); n   int mid = (l + r) / 2; n   print(x * 2, l, mid); n   print(x * 2 + 1, mid + 1, r); n } n int main() { n   pw[0] = 1; n   for (int i = 1; i <= pn; i++) pw[i] = pw[i - 1] * 42; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   build(1, 1, n); n   for (int i = 1; i <= n; i++) st.insert(i); n   for (int i = 1; i <= m; i++) { n     int op, l, r, x; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d , &x); n       int u = *st.lower_bound(x); n       printf( %lld n , query(1, 1, n, u)); n       continue; n     } n     scanf( %d%d%d , &l, &r, &x); n     if (l - 1) check(l - 1); n     check(r); n     if (op == 2) { n       modify(1, 1, n, l, r - 1); n       modifyp(1, 1, n, r, x); n       while (1) { n         set<int>::iterator it; n         it = st.lower_bound(l); n         if (it == st.end() || *it >= r) break; n         st.erase(it); n       } n     } else { n       while (1) { n         modifya(1, 1, n, l, r, x); n         while (sum2[1] > 0) { n           int u = sumid[1]; n           modifyp(1, 1, n, u, query(1, 1, n, u)); n         } n         long long ans = -1e18; n         int id = 0; n         querysum2(1, 1, n, l, r, ans, id); n         if (ans < 0) break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   if (x < 0) return -x; n   return x; n } n template <class T> n T sqr(T a) { n   return a * a; n } n const double pi = acos(-1.0); n const double eps = 1e-8; n int i, n, cnt; n char s1[110], s2[110]; n void fun1() { n   if (s1[i + 1] ==  0 ) { n     s1[i + 1] =  X ; n     cnt++; n   } else if (s2[i + 1] ==  0 ) { n     s2[i + 1] =  X ; n     cnt++; n   } n } n void fun2() { n   if (s1[i + 1] ==  0  && s2[i + 1] ==  0 ) { n     s1[i + 1] =  X ; n     s2[i + 1] =  X ; n     cnt++; n   } n } n int main() { n   while (2 == scanf( %s%s , &s1, s2)) { n     cnt = 0; n     n = strlen(s1); n     for (i = 0; i < n - 1; i++) { n       if (s1[i] ==  0  && s2[i] ==  0 ) fun1(); n       if (s1[i] ==  0  || s2[i] ==  0 ) fun2(); n     } n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int check(string s) { n   int d = 0; n   for (int i = 0; i < s.length(); i++) n     if (s[i] ==  a  || s[i] ==  e  || s[i] ==  o  || s[i] ==  u  || s[i] ==  i ) n       d++; n   return d; n } n int main() { n   string s1, s2, s3; n   getline(cin, s1); n   getline(cin, s2); n   getline(cin, s3); n   if (check(s1) == 5 && check(s2) == 7 && check(s3) == 5) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n const int N = 55; n string s[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> s[i]; n   int sx, sy, ex, ey; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       if (s[i][j] ==  S ) sx = i, sy = j; n       if (s[i][j] ==  E ) ex = i, ey = j; n     } n   string w; n   cin >> w; n   string p =  ULDR ; n   sort((p).begin(), (p).end()); n   map<char, int> dx = {{ D , -1}, { U , +1}}; n   map<char, int> dy = {{ L , -1}, { R , +1}}; n   int ans = 0; n   do { n     bool ok = true; n     int x = sx, y = sy; n     for (int i = 0; i < w.size(); ++i) { n       if (x == ex && y == ey) break; n       if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] ==  # ) { n         ok = false; n         break; n       } n       x += dx[p[w[i] -  0 ]]; n       y += dy[p[w[i] -  0 ]]; n     } n     if (ok && x == ex && y == ey) ++ans; n   } while (next_permutation((p).begin(), (p).end())); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str1[200002], str2[200002]; n const int K = 1024; n int aw[200002]; n int bw[200002]; n char bit[1 << 16]; n short cnt[200002 / K + 1][200002]; n int main() { n   gets(str1); n   gets(str2); n   int l1 = strlen(str1), l2 = strlen(str2); n   for (int i = 0; i < l1; ++i) { n     unsigned int one = 1; n     for (int j = 0; j < 16 && i + j < l1; ++j) { n       if (str1[i + j] ==  1 ) aw[i] |= one; n       one <<= 1; n     } n   } n   for (int i = 0; i < l2; ++i) { n     unsigned int one = 1; n     for (int j = 0; j < 16 && i + j < l2; ++j) { n       if (str2[i + j] ==  1 ) bw[i] |= one; n       one <<= 1; n     } n   } n   bit[0] = 0; n   for (int i = 1; i < (1 << 16); ++i) { n     bit[i] = (i & 1) + bit[i >> 1]; n   } n   int IT = K >> 4; n   for (int i = 0; i + K <= l1; i += K) { n     int ind = i / K; n     for (int j = 0; j + K <= l2; ++j) { n       int p1 = i, p2 = j; n       for (int t = 0; t < IT; ++t) { n         cnt[ind][j] += bit[aw[p1] ^ bw[p2]]; n         p1 += 16; n         p2 += 16; n       } n     } n   } n   int q, p1, p2, len; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d%d , &p1, &p2, &len); n     int ans = 0; n     int pos = (p1 / K) * K, r = p1 + len - 1; n     while (pos < p1) pos += K; n     for (int i = p1; i < pos && i <= r; ++i) { n       ans += str1[i] != str2[p2]; n       ++p2; n     } n     if (pos <= r) { n       while (pos + K - 1 <= r) { n         ans += cnt[pos / K][p2]; n         pos += K; n         p2 += K; n       } n       for (int i = pos; i <= r; ++i) { n         ans += str1[i] != str2[p2]; n         ++p2; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10086; n struct Node { n   string name; n   vector<Node *> son; n }; n char str[MAXN]; n int ans; n void build(Node *&root, int &pos) { n   if (str[pos] == 0) { n     root = NULL; n     return; n   } n   string name =   ; n   for (; str[pos] !=  .  && str[pos] !=  : ;) { n     name += str[pos++]; n   } n   root = new Node(); n   root->name = name; n   if (str[pos] ==  : ) { n     do { n       pos++; n       Node *son; n       build(son, pos); n       if (son != NULL) root->son.push_back(son); n     } while (str[pos] !=  . ); n   } n   pos++; n } n void dfs(Node *root, map<string, int> &mp) { n   if (root == NULL) return; n   mp[root->name]++; n   for (vector<Node *>::iterator it = root->son.begin(); it != root->son.end(); n        it++) { n     map<string, int> _mp; n     dfs(*it, _mp); n     ans += _mp[root->name]; n     for (map<string, int>::iterator mit = _mp.begin(); mit != _mp.end(); mit++) n       mp[mit->first] += mit->second; n   } n } n int main() { n   while (~scanf( %s , str)) { n     Node *root; n     int pos = 0; n     build(root, pos); n     ans = 0; n     map<string, int> mp; n     dfs(root, mp); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100008; n int n, a[N]; n vector<pair<int, int> > ans; n int main(void) { n   int i, j, k; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , a + i); n   for (i = 1; i + 2 <= n; i++) n     if (a[i]) { n       k = 1; n       for (j = 2; i + j <= n; j++) n         if (a[i + j]) { n           if (i + j + j <= n) n             k = j; n           else n             k = 1; n           break; n         } n       a[i] ^= 1; n       a[i + k] ^= 1; n       a[i + k + k] ^= 1; n       ans.push_back(make_pair(i, k)); n     } n   if (a[n - 1] == 0 && a[n] == 0) n     ; n   else if (a[n - 1] == 1 && a[n] == 0) { n     if (n >= 8) { n       ans.push_back(make_pair(n - 7, 1)); n       ans.push_back(make_pair(n - 7, 3)); n       ans.push_back(make_pair(n - 6, 1)); n     } else { n       printf( NO ); n       return 0; n     } n   } else if (a[n - 1] == 0 && a[n] == 1) { n     if (n >= 7) { n       ans.push_back(make_pair(n - 6, 1)); n       ans.push_back(make_pair(n - 6, 3)); n       ans.push_back(make_pair(n - 5, 1)); n     } else { n       printf( NO ); n       return 0; n     } n   } else { n     if (n >= 8) { n       ans.push_back(make_pair(n - 7, 1)); n       ans.push_back(make_pair(n - 7, 3)); n       ans.push_back(make_pair(n - 6, 1)); n       ans.push_back(make_pair(n - 6, 1)); n       ans.push_back(make_pair(n - 6, 3)); n       ans.push_back(make_pair(n - 5, 1)); n     } else { n       printf( NO ); n       return 0; n     } n   } n   printf( YES n%d n , (int)ans.size()); n   for (auto p : ans) n     printf( %d %d %d n , p.first, p.first + p.second, n            p.first + p.second + p.second); n   return 0; n } n


#include<bits/stdc++.h> n typedef long long ll; n using namespace std; n const int MAXN = 5005; n int tag[MAXN],s[MAXN]; n ll dp[MAXN]; n int main(){ n     ios::sync_with_stdio(false); n     int t;cin>>t; n     while(t--){ n         int n;cin>>n; n         for(int i=1;i<=n;i++){ n             cin>>tag[i]; n         } n         for(int i=1;i<=n;i++){ n             cin>>s[i]; n             dp[i]=0; n         } n         for(int i=2;i<=n;i++){ n             for(int j=i-1;j>=1;j--){ n                 if(tag[i]==tag[j]) continue; n                 ll di=dp[i],dj=dp[j],add=abs(s[i]-s[j]); n                 dp[i]=max(dp[i],dj+add); n                 dp[j]=max(dp[j],di+add); n             } n         } n         ll ans=0; n         for(int i=1;i<=n;i++){ n             ans=max(ans,dp[i]); n         } n         cout<<ans<<  n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n template <typename T> n inline T Bigmod(T base, T power, T MOD) { n   T ret = 1; n   while (power) { n     if (power & 1) ret = (ret * base) % MOD; n     base = (base * base) % MOD; n     power >>= 1; n   } n   return ret; n } n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   while (cin >> n >> m) { n     int a[n]; n     int b[m]; n     int max_a = 0, min_a = 110, min_b = 110; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       max_a = max(max_a, a[i]); n       min_a = min(min_a, a[i]); n     } n     for (int i = 0; i < m; i++) { n       cin >> b[i]; n       min_b = min(min_b, b[i]); n     } n     if (max(2 * min_a, max_a) < min_b) n       cout << max(2 * min_a, max_a) << endl; n     else n       cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const long long int P = 1000000007; n const double eps = 1e-6; n int val[N]; n int deg[N]; n vector<int> gr[N]; n vector<long long int> mx(N); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> val[i]; n     ans += val[i]; n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     gr[u].push_back(v); n     gr[v].push_back(u); n     deg[u]++, deg[v]++; n   } n   int start; n   cin >> start; n   start--; n   stack<int> st; n   for (int i = 0; i < n; i++) n     if (deg[i] == 1 && i != start) st.push(i); n   long long int maxall = 0; n   while (!st.empty()) { n     int now = st.top(); n     st.pop(); n     deg[now]--; n     ans -= val[now]; n     for (auto i : gr[now]) { n       if (deg[i]) { n         deg[i]--; n         if (deg[i] == 1 && i != start) st.push(i); n         mx[i] = max((long long int)mx[i], n                     (long long int)mx[now] + (long long int)val[now]); n         maxall = max(maxall, mx[i]); n       } n     } n   } n   cout << ans + maxall; n   cerr <<   nTime elapsed :   << 1000 * clock() / CLOCKS_PER_SEC <<   ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000005; n struct node { n   int l, r, sum, id; n } tr[200010 * 4]; n void build(int l, int r, int x) { n   tr[x].l = l, tr[x].r = r; n   if (l == r) { n     tr[x].sum = 0; n     tr[x].id = l; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, x << 1); n   build(mid + 1, r, x << 1 | 1); n   if (tr[x << 1].sum > tr[x << 1 | 1].sum) n     tr[x].sum = tr[x << 1].sum, tr[x].id = tr[x << 1].id; n   else n     tr[x].sum = tr[x << 1 | 1].sum, tr[x].id = tr[x << 1 | 1].id; n } n pair<int, int> query(int L, int R, int x) { n   int l = tr[x].l, r = tr[x].r; n   if (L == tr[x].l && R == tr[x].r) { n     return make_pair(tr[x].sum, tr[x].id); n   } n   int mid = (l + r) >> 1; n   if (R <= mid) n     return query(L, R, x << 1); n   else if (L > mid) n     return query(L, R, x << 1 | 1); n   else { n     pair<int, int> p = query(L, mid, x << 1), q = query(mid + 1, R, x << 1 | 1); n     if (p.first > q.first) n       return p; n     else n       return q; n   } n } n void upd(int L, int R, int x, int val) { n   int l = tr[x].l, r = tr[x].r; n   if (L == l && R == r) { n     tr[x].sum = val; n     tr[x].id = l; n     return; n   } n   int mid = (l + r) >> 1; n   if (R <= mid) n     upd(L, R, x << 1, val); n   else n     upd(L, R, x << 1 | 1, val); n   if (tr[x << 1].sum > tr[x << 1 | 1].sum) n     tr[x].sum = tr[x << 1].sum, tr[x].id = tr[x << 1].id; n   else n     tr[x].sum = tr[x << 1 | 1].sum, tr[x].id = tr[x << 1 | 1].id; n } n struct EDGE { n   int v, next, val; n } edges[200010 * 2]; n int head[200010], cnt; n void addedge(int u, int v, int val) { n   edges[cnt].v = v; n   edges[cnt].next = head[u]; n   edges[cnt].val = val; n   head[u] = cnt++; n   edges[cnt].v = u; n   edges[cnt].next = head[v]; n   edges[cnt].val = val; n   head[v] = cnt++; n } n int n, m; n int size[200010], fa[200010], dep[200010], son[200010]; n void dfs1(int u, int pre, int depth) { n   size[u] = 1; n   son[u] = -1; n   fa[u] = pre; n   dep[u] = depth; n   for (int i = head[u]; i != -1; i = edges[i].next) { n     int x = edges[i].v; n     if (x == pre) continue; n     dfs1(x, u, depth + 1); n     if (son[u] == -1 || size[son[u]] < size[x]) { n       son[u] = x; n     } n     size[u] += size[x]; n   } n } n int top[200010], pos[200010], no = 1, re[200010]; n void dfs2(int u, int tops) { n   top[u] = tops; n   pos[u] = no++; n   if (son[u] == -1) { n     return; n   } n   dfs2(son[u], tops); n   for (int i = head[u]; i != -1; i = edges[i].next) { n     int x = edges[i].v; n     if (x == fa[u] || x == son[u]) continue; n     dfs2(x, x); n   } n } n void dfs3(int u, int fa) { n   for (int i = head[u]; i != -1; i = edges[i].next) { n     int x = edges[i].v; n     if (x == fa) continue; n     upd(pos[x], pos[x], 1, edges[i].val); n     dfs3(x, u); n   } n } n int sum[200010]; n pair<int, int> qry(int x, int y) { n   int f1 = top[x], f2 = top[y]; n   pair<int, int> res(0, -1), tmp; n   while (f1 != f2) { n     if (dep[f1] < dep[f2]) { n       swap(f1, f2); n       swap(x, y); n     } n     tmp = query(pos[f1], pos[x], 1); n     if (tmp.first > res.first) res = tmp; n     x = fa[f1]; n     f1 = top[x]; n   } n   if (x == y) return res; n   if (dep[x] < dep[y]) swap(x, y); n   tmp = query(pos[son[y]], pos[x], 1); n   if (tmp.first > res.first) res = tmp; n   return res; n } n struct bian { n   pair<int, int> z; n   long long w, c; n   int id; n } ed[200010]; n bool cmp(bian a, bian b) { n   if (a.w == b.w) n     return a.c < b.c; n   else n     return a.w < b.w; n } n int pa[200010]; n bool vis[200010]; n int root(int x) { n   if (pa[x] == -1) n     return x; n   else { n     pa[x] = root(pa[x]); n     return pa[x]; n   } n } n int main() { n   int n, m, i, j; n   long long s; n   memset(head, -1, sizeof(head)); n   memset(pa, -1, sizeof(pa)); n   memset(vis, 0, sizeof(vis)); n   cnt = 0; n   scanf( %d %d , &n, &m); n   for (i = 1; i <= m; i++) scanf( %I64d , &ed[i].w), ed[i].id = i; n   for (i = 1; i <= m; i++) scanf( %I64d , &ed[i].c); n   for (i = 1; i <= m; i++) scanf( %d %d , &ed[i].z.first, &ed[i].z.second); n   scanf( %I64d , &s); n   sort(ed + 1, ed + m + 1, cmp); n   int mn = INF, mnid = -1; n   long long ans = 0, sm = 0; n   for (i = 1; i <= m; i++) { n     int u = ed[i].z.first, v = ed[i].z.second; n     if (root(u) != root(v)) { n       pa[root(u)] = root(v); n       vis[i] = 1; n       addedge(u, v, ed[i].w); n       if (ed[i].c < mn) mn = ed[i].c, mnid = i; n       ans += ed[i].w; n     } n   } n   build(1, n, 1); n   dfs1(1, 0, 1); n   dfs2(1, 1); n   dfs3(1, 0); n   for (i = 1; i <= n; i++) re[pos[i]] = i; n   int nw = -1, nwf = -1; n   ans -= s / mn; n   sm = ans + s / mn; n   for (i = 1; i <= m; i++) { n     if (vis[i]) continue; n     pair<int, int> mx = qry(ed[i].z.first, ed[i].z.second); n     if (sm - (long long)mx.first + ed[i].w - s / ed[i].c < ans) { n       ans = sm - (long long)mx.first + ed[i].w - s / ed[i].c; n       nw = i; n       nwf = mx.second; n     } n   } n   printf( %I64d n , ans); n   if (nw == -1) { n     for (i = 1; i <= m; i++) { n       if (vis[i] && i != mnid) { n         printf( %d %I64d n , ed[i].id, ed[i].w); n       } else if (mnid == i && vis[i]) { n         printf( %d %I64d n , ed[i].id, ed[i].w - s / ed[i].c); n       } n     } n   } else { n     for (i = 1; i <= m; i++) { n       if (i == nw) { n         printf( %d %I64d n , ed[i].id, ed[i].w - s / ed[i].c); n       } else if (vis[i]) { n         if (ed[i].z.first == re[nwf] && ed[i].z.second == fa[re[nwf]] || n             ed[i].z.second == re[nwf] && ed[i].z.first == fa[re[nwf]]) n           continue; n         printf( %d %I64d n , ed[i].id, ed[i].w); n       } n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   char A[100]; n   int M, N = 0, i = 0; n   scanf( %d , &M); n   scanf( %s , A); n   for (i = 0; i < M; i++) { n     if (A[i] ==  x  && A[i + 1] ==  x  && A[i + 2] ==  x ) { n       N++; n     } n   } n   printf( %d n , N); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, k; n   long long int n, m, t; n   long long int a, b; n   scanf( %I64d%I64d%I64d , &n, &a, &b); n   t = n / a + 1; n   for (i = 0; i <= t; i++) { n     m = i * a; n     if ((n - m) % b == 0 && n - m >= 0) { n       printf( YES n%I64d %I64d , i, (n - m) / b); n       return 0; n     } n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 9; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   vector<vector<long long>> v(n); n   vector<vector<long long>> vr(n); n   long long a, b; n   for (int i = 0; i < m; ++i) { n     cin >> a >> b; n     a--; n     b--; n     v[a].push_back(b); n     vr[b].push_back(a); n   } n   vector<long long> mr(n, 0); n   long long ps; n   cin >> ps; n   vector<long long> p(ps); n   for (int i = 0; i < ps; ++i) { n     cin >> p[i]; n     p[i]--; n   } n   mr[p[ps - 1]] = 0; n   queue<int> q; n   q.push(p[ps - 1]); n   vector<bool> used(n); n   used[p[ps - 1]] = true; n   while (!q.empty()) { n     long long cur = q.front(); n     q.pop(); n     for (auto i : vr[cur]) { n       if (!used[i]) { n         used[i] = true; n         mr[i] = mr[cur] + 1; n         q.push(i); n       } n     } n   } n   long long mn = 0, mx = 0; n   for (int i = 0; i < ps - 1; ++i) { n     long long lg = 1000000000000; n     set<long long> s; n     for (auto j : v[p[i]]) { n       if (mr[j] < lg) { n         s.clear(); n         s.insert(j); n         lg = mr[j]; n       } else if (mr[j] == lg) { n         s.insert(j); n       } n     } n     if (s.find(p[i + 1]) == s.end()) { n       mn++; n       mx++; n     } else { n       if (s.size() > 1) { n         mx++; n       } n     } n   } n   cout << mn <<     << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 2e5 + 4; n int main() { n   int n; n   scanf( %d , &n); n   long long b[n], c[n], sum = 0; n   for (int i = 0; i < n; i++) scanf( %lld , &b[i]), sum += b[i]; n   for (int i = 0; i < n; i++) scanf( %lld , &c[i]), sum += c[i]; n   if (sum % 2 * n != 0) { n     printf( %d , -1); n     return 0; n   } n   sum /= 2 * n; n   long long a[n]; n   for (int i = 0; i < n; i++) { n     long long here = b[i] + c[i] - sum; n     if (here >= 0 and here % n != 0) { n       printf( %d , -1); n       return 0; n     } n     a[i] = here / n; n   } n   vector<int> fa(31, 0); n   for (int bit = 0; bit < 31; bit++) { n     for (int i = 0; i < n; i++) n       if (a[i] & (1LL << bit)) fa[bit]++; n   } n   for (int i = 0; i < n; i++) { n     for (int bit = 0; bit < 31; bit++) n       if (a[i] & (1LL << bit)) b[i] -= (1LL << bit) * fa[bit]; n     if (b[i] != 0) { n       printf( %d , -1); n       return 0; n     } n   } n   for (int i = 0; i < n; i++) printf( %lld  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(   n      Ofast,unroll-loops,no-stack-protector,unsafe-math-optimizations ) n #pragma GCC target( avx ) n inline int read_char() { n   static char buf[1 << 16], *ptr = buf, *end_ptr = buf; n   if (ptr == end_ptr) { n     int len = fread(buf, 1, sizeof(buf), stdin); n     if (len <= 0) return EOF; n     ptr = buf; n     end_ptr = buf + len; n   } n   return *ptr++; n } n inline int read_int() { n   int ch; n   bool sg = 0; n   do { n     ch = read_char(); n     if (ch ==  - ) sg = 1; n   } while (ch <  0  || ch >  9 ); n   int x = ch -  0 ; n   while (true) { n     ch = read_char(); n     if (ch <  0  || ch >  9 ) break; n     x = x * 10 + ch -  0 ; n   } n   return (sg ? -x : x); n } n float __attribute__((aligned(64))) a[100005]; n int n, m, ans, t, l, r; n float x; n int main() { n   n = read_int(); n   m = read_int(); n   for (int i = 1; i <= n; i++) a[i] = read_int(); n   for (int i = 1; i <= m; i++) { n     t = read_int(); n     l = read_int(); n     r = read_int(); n     x = read_int(); n     if (t == 1) { n       for (int j = l; j <= r; j++) n         if (a[j] > x) a[j] -= x; n     } else { n       ans = 0; n       for (int j = l; j <= r; j++) ans += (a[j] == x); n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n unsigned long long x[N], y[N], f[N]; n int a[N], cnt[N]; n queue<int> g[N]; n mt19937_64 rng(time(0)); n char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; n inline int rd() { n   int x = 0, f = 1; n   char ch = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   while (isdigit(ch)) n     x = x * 10 + (ch ^ 48), n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   return x * f; n } n namespace Hash { n const int hashmod = 999983; n int v[hashmod]; n bool vis[hashmod]; n unsigned long long h[hashmod]; n int &get(unsigned long long S) { n   int i = S % hashmod; n   for (; vis[i] && h[i] != S; i = (i + 1) % hashmod) n     ; n   if (!vis[i]) vis[i] = 1, h[i] = S; n   return v[i]; n } n }  // namespace Hash n int main() { n   int n = rd(); n   for (int i = 1; i <= n; ++i) { n     a[i] = rd(); n     x[i] = rng(); n     y[i] = rng(); n   } n   unsigned long long now = 0, ans = 0; n   ++Hash::get(now); n   int p = 0; n   for (int i = 1; i <= n; ++i) { n     if (g[a[i]].size() == 3) { n       int t = g[a[i]].front(); n       g[a[i]].pop(); n       while (p < t) --Hash::get(f[p++]); n     } n     g[a[i]].push(i); n     switch (++cnt[a[i]] % 3) { n       case 1: n         f[i] = now ^= x[a[i]]; n         break; n       case 2: n         f[i] = now ^= y[a[i]]; n         break; n       case 0: n         f[i] = now ^= x[a[i]] ^ y[a[i]]; n         break; n     } n     ans += Hash::get(now)++; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n void bubbleSort(long long int arr[], int n) { n   for (int i = 0; i < n - 1; i++) { n     for (int j = 0; j < n - i - 1; j++) { n       if (arr[j] > arr[j + 1]) { n         int swap = arr[j]; n         arr[j] = arr[j + 1]; n         arr[j + 1] = swap; n       } n     } n   } n } n int main() { n   int n, k, ans; n   scanf( %d %d , &n, &k); n   long long int arr[n]; n   for (int i = 0; i < n; i++) { n     scanf( %lld , &arr[i]); n     bubbleSort(arr, i + 1); n     int j = i; n     long long int sum = 0; n     while (j > -1) { n       sum += arr[j]; n       j -= 2; n     } n     if (sum <= k) { n       ans = i + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000 * 1000; n const int mod = 1000 * 1000 + 3; n string com =  ><+-.,[] ; n int main() { n   string s; n   cin >> s; n   int res = 0; n   for (int i = 0; i < s.size(); i++) n     res = (res << 4) % mod, res = (res + 8 + com.find(s[i])) % mod; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const signed long long INF = 1000000100; n const long double EPS = 1e-9; n const long double Pi = 2 * atanl(1.0); n template <typename T, typename U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &V) { n   os <<  [ ; n   for (int(i) = (0); (i) < (int(V.size())); (i)++) n     os << V[i] << ((i == int(V.size()) - 1) ?    :  , ); n   return os <<  ] ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &S) { n   os <<  ( ; n   for (T i : S) os << i << (i == *S.rbegin() ?    :  , ); n   return os <<  ) ; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const map<T, U> &M) { n   os <<  { ; n   for (pair<T, U> i : M) os << i << (i.X == M.rbegin()->X ?    :  , ); n   return os <<  } ; n } n inline bool is_zero(const long double &first) { n   return first >= -EPS and first <= EPS; n } n inline int sign(const long double &first) { n   if (first < -EPS) n     return -1; n   else if (first > EPS) n     return 1; n   else n     return 0; n } n inline pair<long double, long double> operator+( n     const pair<long double, long double> &a, n     const pair<long double, long double> &b) { n   return pair<long double, long double>(a.first + b.first, a.second + b.second); n } n inline pair<long double, long double> operator-( n     const pair<long double, long double> &a, n     const pair<long double, long double> &b) { n   return pair<long double, long double>(a.first - b.first, a.second - b.second); n } n inline pair<long double, long double> operator*( n     const pair<long double, long double> &a, long double t) { n   return pair<long double, long double>(a.first * t, a.second * t); n } n inline pair<long double, long double> operator/( n     const pair<long double, long double> &a, long double t) { n   return pair<long double, long double>(a.first / t, a.second / t); n } n inline bool operator==(const pair<long double, long double> &a, n                        const pair<long double, long double> &b) { n   return is_zero(a.first - b.first) and is_zero(a.second - b.second); n } n inline bool operator!=(const pair<long double, long double> &a, n                        const pair<long double, long double> &b) { n   return !is_zero(a.first - b.first) or !is_zero(a.second - b.second); n } n pair<long double, long double> FAIL = make_pair(1e30, 1e30); n inline long double cp(const pair<long double, long double> &a, n                       const pair<long double, long double> &b) { n   return a.first * b.second - a.second * b.first; n } n inline long double dp(const pair<long double, long double> &a, n                       const pair<long double, long double> &b) { n   return a.first * b.first + a.second * b.second; n } n inline long double pieprzu(const pair<long double, long double> &a, n                            const pair<long double, long double> &b) { n   return sqrtl((b.first - a.first) * (b.first - a.first) + n                (b.second - a.second) * (b.second - a.second)); n } n inline long double distance_point_line( n     const pair<long double, long double> &p, n     const pair<pair<long double, long double>, pair<long double, long double> > n         &u) { n   return cp(p - u.first, p - u.second) / pieprzu(u.first, u.second); n } n inline bool on_right(const pair<long double, long double> &a, n                      const pair<long double, long double> &b, n                      const pair<long double, long double> &c) { n   return sign(cp(c - a, b - a)) == 1; n } n inline bool on_left(const pair<long double, long double> &a, n                     const pair<long double, long double> &b, n                     const pair<long double, long double> &c) { n   return sign(cp(c - a, b - a)) == -1; n } n inline double polygon_area(const vector<pair<long double, long double> > &A) { n   long double result = 0.0; n   if (int(A.size()) <= 2) return 0; n   for (int(i) = (2); (i) < (int(A.size())); (i)++) n     result += cp(A[i] - A[0], A[i - 1] - A[0]); n   return abs(result / 2); n } n inline pair<long double, long double> projection( n     const pair<long double, long double> &p, n     const pair<pair<long double, long double>, pair<long double, long double> > n         &u) { n   return u.first + n          ((u.second - u.first) * (dp(p - u.first, u.second - u.first) / n                                   dp(u.second - u.first, u.second - u.first))); n } n inline bool between(const pair<long double, long double> &a, n                     const pair<long double, long double> &b, n                     const pair<long double, long double> &c) { n   return sign(min(b.first, c.first) - a.first) <= 0 and n          sign(a.first - max(b.first, c.first)) <= 0 and n          sign(min(b.second, c.second) - a.second) <= 0 and n          sign(a.second - max(b.second, c.second)) <= 0; n } n inline bool on_segment(const pair<long double, long double> &p, n                        const pair<pair<long double, long double>, n                                   pair<long double, long double> > &u) { n   return between(p, u.first, u.second) and pieprzu(p, projection(p, u)) < EPS; n } n inline long double distance_segment_point( n     const pair<pair<long double, long double>, pair<long double, long double> > n         &u, n     const pair<long double, long double> &p) { n   if (on_segment(projection(p, u), u)) n     return fabs(distance_point_line(p, u)); n   else n     return min(pieprzu(u.first, p), pieprzu(u.second, p)); n } n inline long double angle(const pair<long double, long double> &a, n                          const pair<long double, long double> &o, n                          const pair<long double, long double> &b) { n   long double result = atan2l(a.first - o.first, a.second - o.second) - n                        atan2l(b.first - o.first, b.second - o.second); n   if (sign(result) == -1) result += 2 * Pi; n   return result; n } n inline pair<long double, long double> point_reflection( n     const pair<long double, long double> &s, n     const pair<long double, long double> &p) { n   return p + (s - p) * 2; n } n inline pair<long double, long double> axial_reflection( n     const pair<long double, long double> &p, n     const pair<pair<long double, long double>, pair<long double, long double> > n         &u) { n   return point_reflection(p, projection(p, u)); n } n inline int triangle_inequality(long double &p, long double q, long double r) { n   int a = sign(p + q - r); n   int b = sign(p + r - q); n   int c = sign(q + r - p); n   if (a == -1 or b == -1 or c == -1) return -1; n   if (a == 0 or b == 0 or c == 0) return 0; n   return 1; n } n vector<pair<long double, long double> > convex_hull( n     vector<pair<long double, long double> > A) { n   if (int(A.size()) <= 1) return A; n   sort(A.begin(), A.end()); n   vector<pair<long double, long double> > R{A[0], A[1]}; n   for (int(i) = (2); (i) < (int(A.size())); (i)++) { n     while (int(R.size()) >= 2 and n            !on_right(R[R.size() - 2], R[R.size() - 1], A[i])) n       R.pop_back(); n     R.push_back(A[i]); n   } n   int u = int(A.size()); n   for (int(i) = (int(A.size()) - 2); (i) >= (0); (i)--) { n     while (int(R.size()) > u and n            !on_right(R[R.size() - 2], R[R.size() - 1], A[i])) n       R.pop_back(); n     R.push_back(A[i]); n   } n   R.pop_back(); n   return move(R); n } n inline pair<long double, long double> rotate90_origin( n     const pair<long double, long double> &p) { n   return pair<long double, long double>(p.second, -p.first); n } n inline pair<long double, long double> rotate90( n     const pair<long double, long double> &p, n     const pair<long double, long double> &o) { n   return o + rotate90_origin(p - o); n } n inline pair<long double, long double> rotate_origin( n     const pair<long double, long double> &p, long double theta) { n   theta = -theta; n   long double s = sinl(theta); n   long double c = cosl(theta); n   return pair<long double, long double>(p.first * c - p.second * s, n                                         p.first * s + p.second * c); n } n inline pair<long double, long double> rotate( n     const pair<long double, long double> &p, n     const pair<long double, long double> &o, long double theta) { n   return o + rotate_origin(p - o, theta); n } n inline bool parallel(const pair<pair<long double, long double>, n                                 pair<long double, long double> > &u, n                      const pair<pair<long double, long double>, n                                 pair<long double, long double> > &v) { n   return is_zero(cp(u.second - u.first, v.second - v.first)); n } n bool perpendicular(const pair<pair<long double, long double>, n                               pair<long double, long double> > &u, n                    const pair<pair<long double, long double>, n                               pair<long double, long double> > &v) { n   return is_zero(dp(u.second - u.first, v.second - v.first)); n } n inline pair<pair<long double, long double>, pair<long double, long double> > n segment_bisector(const pair<pair<long double, long double>, n                             pair<long double, long double> > &u) { n   pair<long double, long double> p = (u.first + u.second) / 2; n   return pair<pair<long double, long double>, pair<long double, long double> >( n       p, rotate90(u.second, p)); n } n inline pair<pair<long double, long double>, pair<long double, long double> > n angle_bisector(const pair<long double, long double> &a, n                const pair<long double, long double> &o, n                const pair<long double, long double> &b) { n   return pair<pair<long double, long double>, pair<long double, long double> >( n       o, rotate(b, o, angle(a, o, b) / 2)); n } n inline pair<long double, long double> intersection_line_line( n     const pair<pair<long double, long double>, pair<long double, long double> > n         &u, n     const pair<pair<long double, long double>, pair<long double, long double> > n         &v) { n   long double p = cp(u.second - v.first, u.second - u.first); n   long double q = cp(v.second - v.first, u.second - u.first); n   if (is_zero(q)) return FAIL; n   return v.first + (v.second - v.first) * (p / q); n } n const pair<pair<long double, long double>, long double> CFAIL = n     pair<pair<long double, long double>, long double>(FAIL, 0); n pair<pair<long double, long double>, long double> incircle( n     const pair<long double, long double> &a, n     const pair<long double, long double> &b, n     const pair<long double, long double> &c) { n   if (is_zero(cp(b - a, c - a))) return CFAIL; n   pair<pair<long double, long double>, pair<long double, long double> > p = n       segment_bisector( n           pair<pair<long double, long double>, pair<long double, long double> >( n               a, b)); n   pair<pair<long double, long double>, pair<long double, long double> > q = n       segment_bisector( n           pair<pair<long double, long double>, pair<long double, long double> >( n               a, c)); n   pair<long double, long double> t = intersection_line_line(p, q); n   return pair<pair<long double, long double>, long double>(t, pieprzu(t, a)); n } n pair<long double, long double> intersection_segment_segment( n     const pair<pair<long double, long double>, pair<long double, long double> > n         &u, n     const pair<pair<long double, long double>, pair<long double, long double> > n         &v) { n   pair<long double, long double> r = intersection_line_line(u, v); n   if (on_segment(r, u) and on_segment(r, v)) n     return r; n   else n     return FAIL; n } n int main() { n   int N, X, Y; n   scanf( %d %d %d , &N, &X, &Y); n   vector<pair<long double, long double> > P; n   int max_a = 0; n   int max_b = 0; n   for (int(i) = (1); (i) <= (N); (i)++) { n     int a, b; n     scanf( %d %d , &a, &b); n     max_a = max(max_a, a); n     max_b = max(max_b, b); n     P.emplace_back(a, b); n   } n   P.emplace_back(max_a, 0); n   P.emplace_back(0, max_b); n   P = convex_hull(P); n   pair<long double, long double> n(X, Y); n   pair<long double, long double> z; n   for (int(i) = (1); (i) <= (1500000); (i)++) { n     if (X * i > 1490490 or Y * i > 1490010) { n       z = pair<long double, long double>(X * i, Y * i); n       break; n     } n   } n   pair<pair<long double, long double>, pair<long double, long double> > s( n       pair<long double, long double>(0, 0), z); n   long double result = INF; n   for (int(i) = (0); (i) < (int(P.size())); (i)++) { n     pair<long double, long double> a = P[i]; n     pair<long double, long double> b = P[(i + 1) % int(P.size())]; n     pair<pair<long double, long double>, pair<long double, long double> > t(a, n                                                                             b); n     pair<long double, long double> d = intersection_segment_segment(s, t); n     if (d == FAIL) continue; n     result = min(result, pieprzu(pair<long double, long double>(X, Y), n                                  pair<long double, long double>(0, 0)) / n                              pieprzu(d, pair<long double, long double>(0, 0))); n   } n   cout << fixed << result << endl; n   return 0; n } n


#include <bits/stdc++.h> n inline int Get() { n   char ch; n   while ((ch = getchar()) <  0  || ch >  9 ) n     ; n   int Num = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) n     Num = (Num << 3) + (Num << 1) + ch -  0 ; n   return Num; n } n const int M = 3e3 + 5; n const int m = 3000; n int n, a[M][M], s[M][M], r[M][M], d[M][M]; n int main() { n   n = Get(); n   for (int i = 1; i <= n; ++i) { n     int x0 = Get(), y0 = Get(), x1 = Get(), y1 = Get(); n     for (int x = x0 + 1; x <= x1; ++x) n       for (int y = y0 + 1; y <= y1; ++y) a[x][y] = i; n   } n   for (int i = 1; i <= m; ++i) n     for (int j = 1; j <= m; ++j) n       s[i][j] = (!a[i][j]) + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1]; n   for (int i = m; ~i; --i) n     for (int j = m; ~j; --j) { n       r[i][j] = a[i][j + 1] == a[i + 1][j + 1] ? 0 : r[i][j + 1] + 1; n       d[i][j] = a[i + 1][j] == a[i + 1][j + 1] ? 0 : d[i + 1][j] + 1; n     } n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) n       for (int k = 1; k <= r[i][j] && k <= d[i][j] && n                       !(s[i + k][j + k] - s[i][j + k] - s[i + k][j] + s[i][j]); n            ++k) n         if (k <= r[i + k][j] && k <= d[i][j + k]) { n           std ::vector<int> ans; n           for (int x = i + 1; x <= i + k; ++x) n             for (int y = j + 1; y <= j + k; ++y) ans.push_back(a[x][y]); n           std ::sort(ans.begin(), ans.end()), n               ans.erase(std ::unique(ans.begin(), ans.end()), ans.end()); n           printf( YES %d n , ans.size()); n           for (int k = 0, sze = ans.size(); k < sze; ++k) printf( %d  , ans[k]); n           putchar(  n ); n           return 0; n         } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n int t1, t2, x1, x2, t0; n int main() { n   scanf( %d %d %d %d %d , &t1, &t2, &x1, &x2, &t0); n   long long k1 = t0 - t1, k2 = t2 - t0; n   if (k1 == 0) { n     printf( %d %d n , x1, t1 == t2 ? x2 : 0); n   } else if (k2 == 0) { n     printf( %d %d n , t1 == t2 ? x1 : 0, x2); n   } else { n     int y1 = 0, y2 = 0; n     long long t; n     double tt = 1e100; n     for (int i = 0; i <= x1; i++) { n       t = ((k1 * 1.0 / k2) * i); n       if ((k1 * 1.0 / k2) * i - t > EPS) t++; n       if (i == 0) t = x2; n       if (t > x2) continue; n       double tmp = n           (((long long)(t1)*i + (long long)(t2)*t) * 1.0) / (i + t + 0.0); n       if (tmp - tt < 0) { n         y1 = i; n         y2 = t; n         tt = tmp; n       } else if (abs(tmp - tt) <= EPS) { n         if (i + t > y1 + y2) { n           y1 = i; n           y2 = t; n         } n       } n     } n     printf( %d %d n , y1, y2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n long long int power(long long int x, long long int y) { n   long long int res = 1; n   x = x; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n void solve() { n   long long int n; n   cin >> n; n   vector<string> v; n   while (n--) { n     string s; n     cin >> s; n     if (s ==  pwd ) { n       cout <<  / ; n       for (int i = 0; i < v.size(); i++) { n         cout << v[i] <<  / ; n       } n       cout << endl; n     } else { n       string s; n       cin >> s; n       vector<string> temp; n       string te; n       if (s[0] ==  / ) v.clear(); n       for (int i = 0; i < s.length(); i++) { n         if (s[i] !=  / ) n           te.push_back(s[i]); n         else { n           if (i != 0) { n             temp.push_back(te); n             te.clear(); n           } n         } n       } n       temp.push_back(te); n       ; n       for (int i = 0; i < temp.size(); i++) { n         if (temp[i] ==  .. ) { n           v.pop_back(); n         } else n           v.push_back(temp[i]); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n1, n2, i, j, n, ma = 0, temp, c; n   cin >> n1 >> n2; n   if (n1 < n2) { n     cout <<  2 ; n   } else { n     cout << n1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   unsigned long long int n, ans, i, j, k; n   cin >> n; n   if (n == 0) { n     cout <<  1  << endl; n     return 0; n   } n   ans = 2 * n * n; n   ans = ans + ((n + 2) * (n + 1)) - 1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[200001]; n int dis[200001]; n int par[200001]; n void addEdge(int u, int v) { n   adj[u].push_back(v); n   adj[v].push_back(u); n } n void dfs(int src, int parent, int dist) { n   dis[src] = dist; n   par[src] = parent; n   for (int child : adj[src]) { n     if (child != parent) { n       dfs(child, src, dist + 1); n     } n   } n } n int solve() { n   int n; n   cin >> n; n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     addEdge(u, v); n   } n   dfs(1, 0, 0); n   set<pair<int, int> > st; n   for (int i = 1; i <= n; i++) { n     if (dis[i] > 2) { n       st.insert({-dis[i], i}); n     } n   } n   int ans = 0; n   while (!st.empty()) { n     int rmv = (*st.begin()).second; n     st.erase(st.begin()); n     rmv = par[rmv]; n     auto itr = st.find({-dis[rmv], rmv}); n     if (itr != st.end()) st.erase(itr); n     for (int c : adj[rmv]) { n       if (dis[c] > 2) { n         itr = st.find({-dis[c], c}); n         if (itr != st.end()) st.erase(itr); n       } n     } n     ans++; n   } n   return ans; n } n int main() { cout << solve() <<   n ; } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e3 + 10; n vector<int> occ[maxn]; n int dp[maxn][maxn], a[maxn]; n void solve() { n   int n; n   cin >> n; n   int ptr = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[++ptr]; n     if (ptr > 1 and a[ptr] == a[ptr - 1]) ptr--; n   } n   n = ptr; n   for (int i = 1; i <= n; i++) occ[a[i]].push_back(i); n   for (int len = 1; len <= n; len++) { n     for (int l = 1; l + len - 1 <= n; l++) { n       int r = l + len - 1; n       if (len == 1) { n         dp[l][r] = 0; n         continue; n       } n       dp[l][r] = max(dp[l][r - 1], dp[l + 1][r]); n       if (a[l] == a[r]) dp[l][r] = max(dp[l][r - 1], dp[l + 1][r]) + 1; n       for (auto m : occ[a[l]]) n         if (l < m and m < r) dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]); n       for (auto m : occ[a[r]]) n         if (l < m and m < r) dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]); n     } n   } n   cout << n - 1 - dp[1][n] <<   n ; n   for (int i = 1; i <= n; i++) occ[a[i]].clear(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) dp[i][j] = 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   int tc = 1; n   cin >> tc; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int solve(int A, int B) { n   int count = 0; n   for (int i = 0; i < 32; i++) { n     if (((A >> i) & 1) != ((B >> i) & 1)) { n       count++; n     } n   } n   return count; n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   std::vector<int> arr(m + 1); n   for (size_t i = 0; i < m + 1; i++) { n     cin >> arr[i]; n   } n   int count = 0; n   for (size_t i = 0; i < m; i++) { n     int ans; n     ans = solve(arr[i], arr[m]); n     if (ans <= k) { n       count++; n     } n   } n   std::cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2001]; n int main() { n   int n, b, s = 0, m, s1, i; n   scanf( %d%d , &n, &b); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n; i++) { n     if (i == n - 1) n       s1 = b; n     else { n       m = *max_element(a + i, a + n); n       s1 = (b / a[i]) * m + b % a[i]; n     } n     if (s1 > s) s = s1; n   } n   printf( %d , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, sum, min = 1000000000; n   cin >> n; n   int k[n]; n   for (i = 0; i < n; i++) cin >> k[i]; n   for (i = 0; i < n; i++) { n     sum = 0; n     int m[k[i]]; n     for (j = 0; j < k[i]; j++) { n       cin >> m[j]; n       sum = sum + 5 * m[j] + 15; n     } n     if (sum < min) min = sum; n   } n   cout << min; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void work(int n) { n   map<int, int> v; n   int ans = 0; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k <= n; k++) { n         for (int x = 0; x <= n; x++) { n           if (i + j + k + x == n) { n             int tot = i + j * 5 + k * 10 + x * 50; n             if (v[tot]) continue; n             v[tot] = 1; n             ans++; n           } n         } n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   scanf( %d , &n); n   if (n <= 20) n     work(n); n   else n     cout << 1LL * 733 + 1LL * 49 * (n - 20); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   if (k >= 2 * n) { n     cout << 0; n     return 0; n   } n   long long q = k / 2, z; n   if (k <= n) n     z = k - 1; n   else n     z = n; n   if (k & 1) { n     long long ans = min(q - 1, z - q - 1); n     ans++; n     cout << ans; n   } else { n     cout << min(q - 1, z - q); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int u, v; n const int maxn = 5e3 + 10; n vector<long long> G[maxn]; n bool visited[maxn]; n long long dp[maxn]; n vector<long long> vc[maxn]; n bool mark[maxn]; n long long n; n vector<long long> ans; n void dfs(int x) { n   visited[x] = 1; n   for (int i = 0; i != G[x].size(); i++) { n     if (visited[G[x][i]] == 0) { n       dfs(G[x][i]); n       dp[x] += 1 + dp[G[x][i]]; n     } n   } n } n void main_dfs(int x) { n   visited[x] = 1; n   memset(mark, 0, sizeof(mark)); n   for (int i = 0; i < G[x].size(); i++) { n     if (visited[G[x][i]] == 0) { n       int g = vc[x].size(); n       for (int j = 0; j < g; j++) { n         if (vc[x][j] + dp[G[x][i]] + 1 != n - 1 && n             mark[vc[x][j] + dp[G[x][i]] + 1] == 0) { n           vc[x].push_back(vc[x][j] + dp[G[x][i]] + 1); n           mark[vc[x][j] + dp[G[x][i]] + 1] = 1; n         } n       } n       if (dp[G[x][i]] + 1 != n - 1 && mark[dp[G[x][i]] + 1] == 0) { n         vc[x].push_back(dp[G[x][i]] + 1); n         mark[dp[G[x][i]] + 1] = 1; n       } n     } else { n       int g = vc[x].size(); n       for (int j = 0; j < g; j++) { n         if (mark[vc[x][j] + n - 1 - dp[x]] == 0) n           if (vc[x][j] + n - 1 - dp[x] != 0 && n               vc[x][j] + n - 1 - dp[x] != n - 1) { n             vc[x].push_back(vc[x][j] + n - dp[x] - 1); n             mark[vc[x][j] + n - dp[x] - 1] = 1; n           } n       } n       if (mark[n - dp[x] - 1] == 0) n         if (n - dp[x] - 1 != 0 && n - dp[x] - 1 != n - 1) { n           vc[x].push_back(n - dp[x] - 1); n           mark[n - dp[x] - 1] = 1; n         } n     } n   } n   for (int i = 0; i != G[x].size(); i++) { n     if (visited[G[x][i]] == 0) main_dfs(G[x][i]); n   } n } n bool mk[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 0; i != n - 1; i++) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1); n   fill(visited, visited + maxn, 0); n   main_dfs(1); n   for (int i = 1; i != n + 1; i++) { n     for (int j = 0; j != vc[i].size(); j++) { n       if (find(ans.begin(), ans.end(), vc[i][j]) == ans.end()) { n         ans.push_back(vc[i][j]); n         if (n - 1 - vc[i][j] != vc[i][j]) ans.push_back(n - 1 - vc[i][j]); n       } n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (int i = 0; i != ans.size(); i++) { n     cout << ans[i] <<     << n - 1 - ans[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, m, x, y, cost, i, j; n   string s; n   cin >> t; n   while (t--) { n     cost = 0; n     cin >> n >> m >> x >> y; n     y = min(y, 2 * x); n     for (i = 0; i < n; i++) { n       cin >> s; n       for (j = 0; j < m; j++) { n         if (s[j] ==  . ) { n           if (j + 1 < m && s[j + 1] ==  . ) n             cost += y, j++; n           else n             cost += x; n         } n       } n     } n     cout << cost << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long test; n   cin >> test; n   while (test--) { n     long long b, p, f, h, c; n     cin >> b >> p >> f >> h >> c; n     long long profit = 0; n     if (h > c) { n       if (b - 2 * p >= 0) { n         profit += p * h; n         b = b - 2 * p; n         if (b - 2 * f >= 0) { n           profit += f * c; n         } else { n           profit += c * (b / 2); n         } n       } else { n         profit += h * (b / 2); n       } n     } else { n       if (b - 2 * f >= 0) { n         profit += f * c; n         b = b - 2 * f; n         if (b - 2 * p >= 0) { n           profit += p * h; n         } else { n           profit += h * (b / 2); n         } n       } else { n         profit += c * (b / 2); n       } n     } n     cout << profit <<   n ; n   } n } n


#include <bits/stdc++.h> n int n, m; n long long k; n long long pow(int n) { n   if (n == 0) return 1; n   long long tmp = pow(n >> 1); n   tmp = tmp * tmp % 1000000009ll; n   if (n & 1) tmp = tmp * 2ll % 1000000009ll; n   return tmp; n } n int main() { n   scanf( %d%d%I64d , &n, &m, &k); n   int t = n - m; n   long long ans = 0; n   if (m == 0) { n     puts( 0 ); n     return 0; n   } n   if (n - t * k < k) { n     ans = m; n   } else { n     int num = (n - t * k) / k; n     ans = (pow(num + 1) - 2 + 1000000009ll) * k % 1000000009ll; n     ans = (ans + (n - t * k) % k) % 1000000009ll; n     ans = (ans + t * (k - 1)) % 1000000009ll; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7f7f7f7f; n char s[1005]; n int dp[1005][2]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   if (1 == n) { n     puts( 0 ); n     return 0; n   } n   memset(dp, 0x7f, sizeof(dp)); n   dp[0][s[0] -  0 ] = 0; n   dp[0][(s[0] -  0 ) ^ 1] = 1; n   for (int i = 1; i < n; ++i) { n     if (s[i] ==  1 ) { n       dp[i][1] = dp[i - 1][0]; n       dp[i][0] = dp[i - 1][1] + 1; n     } else { n       dp[i][0] = dp[i - 1][1]; n       dp[i][1] = dp[i - 1][0] + 1; n     } n   } n   int ans = min(dp[n - 1][0], dp[n - 1][1]); n   if (ans >= INF) ans = -1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, int> > > AdjList; n priority_queue<pair<int, pair<int, int> > > Edgelist; n int n, j, k, m, l, t, i; n string s, ss; n int a[1050][1050]; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   for (i = 0; i < n; i++) { n     t += a[i][i] * a[i][i]; n   } n   scanf( %d , &k); n   while (k--) { n     scanf( %d , &n); n     if (n == 3) { n       printf( %d , t % 2); n     } else { n       scanf( %d , &m); n       t++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000 + 5], nxt[100000 + 5], lst[100000 + 5], fi[100000 + 5]; n int cnt, root[100000 + 5]; n struct node { n   int sum, ls, rs; n } t[100000 * 43 + 5]; n int build(int l, int r) { n   int id = ++cnt; n   if (l == r) { n     t[id].sum = fi[l]; n     return id; n   } n   int mid = (l + r) >> 1; n   t[id].ls = build(l, mid); n   t[id].rs = build(mid + 1, r); n   t[id].sum = t[t[id].ls].sum + t[t[id].rs].sum; n   return id; n } n void upd(int l, int r, int &x, int y, int p, int v) { n   t[x = ++cnt] = t[y]; n   if (l == r) { n     t[x].sum = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) n     upd(l, mid, t[x].ls, t[y].ls, p, v); n   else n     upd(mid + 1, r, t[x].rs, t[y].rs, p, v); n   t[x].sum = t[t[x].ls].sum + t[t[x].rs].sum; n } n int bs(int l, int r, int x, int v) { n   if (v >= t[x].sum) return r + 1; n   if (l == r) return l; n   int mid = (l + r) >> 1; n   if (t[t[x].ls].sum <= v) n     return bs(mid + 1, r, t[x].rs, v - t[t[x].ls].sum); n   else n     return bs(l, mid, t[x].ls, v); n } n int ans[100000 + 5]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) fi[i] = 1; n   for (int i = n; i >= 1; i--) n     nxt[i] = lst[a[i]], lst[a[i]] = i, fi[nxt[i]] = 0; n   root[1] = build(1, n); n   for (int i = 1; i <= n - 1; i++) { n     int tmp = 0; n     upd(1, n, tmp, root[i], i, 0); n     if (nxt[i]) n       upd(1, n, root[i + 1], tmp, nxt[i], 1); n     else n       root[i + 1] = tmp; n   } n   for (int k = 1; k <= n; k++) { n     int now = 1; n     while (now <= n) { n       ans[k]++; n       now = bs(1, n, root[now], k); n     } n   } n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i],    n [i == n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count()); n struct kuhn { n   int n, m; n   vector<vector<int>> g; n   vector<int> vis, ma, mb; n   kuhn(int n_, int m_) : n(n_), m(m_), g(n), vis(n + m), ma(n, -1), mb(m, -1) {} n   void add(int a, int b) { g[a].push_back(b); } n   bool dfs(int i) { n     vis[i] = 1; n     for (int j : g[i]) n       if (!vis[n + j]) { n         vis[n + j] = 1; n         if (mb[j] == -1 or dfs(mb[j])) { n           ma[i] = j, mb[j] = i; n           return true; n         } n       } n     return false; n   } n   int matching() { n     int ret = 0, aum = 1; n     for (auto& i : g) shuffle(i.begin(), i.end(), rng); n     while (aum) { n       for (int j = 0; j < m; j++) vis[n + j] = 0; n       aum = 0; n       for (int i = 0; i < n; i++) n         if (ma[i] == -1 and dfs(i)) ret++, aum = 1; n     } n     return ret; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<string> v(n); n   for (auto& i : v) cin >> i; n   vector<string> v2; n   kuhn k((n - 1) * m, (m - 1) * n); n   int vert = 0, qt = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (v[i][j] ==  # ) { n         qt++; n         if (i + 1 < n and v[i + 1][j] ==  # ) { n           vert++; n           if (j + 1 < m and v[i][j + 1] ==  # ) k.add(m * i + j, n * j + i); n           if (j + 1 < m and v[i + 1][j + 1] ==  # ) n             k.add(m * i + j, n * j + i + 1); n           if (j and v[i][j - 1] ==  # ) k.add(m * i + j, n * (j - 1) + i); n           if (j and v[i + 1][j - 1] ==  # ) n             k.add(m * i + j, n * (j - 1) + i + 1); n         } n         if (j + 1 < m and v[i][j + 1] ==  # ) vert++; n       } n   cout << qt - (vert - k.matching()) <<   n ; n   exit(0); n } n


#include <bits/stdc++.h> n const int mo = 998244353, N = 2005; n int n, a[N][N], ia[N][N], i, j, f[N][N], bi[N], mi[N], fac[N], ans; n inline void add(int x, int v = 1) { n   for (; x <= n; x += x & -x) bi[x] += v; n } n inline int ask(int x) { n   int ans = 0; n   for (; x; x -= x & -x) ans += bi[x]; n   return ans; n } n int main() { n   for (i = *fac = 1; i < N; ++i) fac[i] = 1ll * fac[i - 1] * i % mo; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) scanf( %d , a[i] + j); n   f[0][0] = 1; n   for (i = 1; i <= n; ++i) n     for (f[i][0] = fac[i], j = 1; j <= i; ++j) n       f[i][j] = (1ll * f[i - 1][j - 1] * (i - j) + n                  (j > 1 ? 1ll * f[i - 1][j - 2] * (j - 1) : 0)) % n                 mo; n   for (i = *mi = 1; i <= n; ++i) mi[i] = 1ll * mi[i - 1] * f[n][n] % mo; n   for (i = n; i; --i) n     ans = (ans + 1ll * fac[n - i] * ask(a[1][i])) % mo, add(a[1][i]); n   ans = 1ll * ans * mi[n - 1] % mo; n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) ia[i][a[i][j]] = j; n   for (i = 2; i <= n; ++i) { n     int tot = 0; n     int cnt = 0; n     memset(bi + 1, 0, n << 2); n     for (j = n; j; --j) { n       if (ia[i - 1][a[i][j]] > j) add(a[i][j]), ++cnt; n       if (cnt) { n         int w = ask(a[i][j] - 1); n         tot = (tot + 1ll * w * f[n - j][cnt - 1]) % mo; n       } n       if (ia[i][a[i - 1][j]] > j) add(a[i - 1][j]), ++cnt; n     } n     cnt = 0; n     memset(bi + 1, 0, n << 2); n     for (j = n; j; --j) { n       if (ia[i][a[i - 1][j]] > j) add(a[i - 1][j], -1); n       int w = ask(a[i][j] - 1); n       if (ia[i - 1][a[i][j]] > j) ++cnt; n       tot = (tot + 1ll * w * f[n - j][cnt]) % mo; n       if (ia[i - 1][a[i][j]] < j) add(a[i][j]); n       if (ia[i][a[i - 1][j]] > j) ++cnt; n     } n     ans = (ans + 1ll * tot * mi[n - i]) % mo; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   ,  ; n   __f(comma + 1, args...); n } n const int N = 2e5 + 10; n const int INF = 1e8; n int n; n vector<int> g[N]; n vector<int> BFS(vector<int> v, int src) { n   vector<int> dist = vector<int>(n, INF); n   queue<int> q; n   q.push(src); n   dist[src] = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (auto v : g[u]) n       if (dist[v] == INF) { n         dist[v] = dist[u] + 1; n         q.push(v); n       } n   } n   return dist; n } n int main() { n   ios::sync_with_stdio(0); n   int i, j, k, m; n   cin >> n >> m >> k; n   vector<int> a(k); n   for (i = 0; i < k; ++i) { n     cin >> a[i]; n     a[i]--; n   } n   for (i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   vector<int> d1 = BFS(a, 0); n   vector<int> d2 = BFS(a, n - 1); n   vector<int> b = {0}; n   vector<int> D3 = BFS(b, n - 1); n   int d3 = D3[0]; n   sort(a.begin(), a.end(), n        [&](int x, int y) { return d1[x] - d2[x] < d1[y] - d2[y]; }); n   vector<int> suf(k); n   suf[k - 1] = d2[a[k - 1]]; n   for (i = k - 2; i >= 0; --i) suf[i] = max(suf[i + 1], d2[a[i]]); n   int mx = 0; n   for (i = 0; i < k - 1; ++i) mx = max(d1[a[i]] + suf[i + 1] + 1, mx); n   cout << min(d3, mx) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get(char *s) { n   if (s[0] ==  8  && s[1] ==  < ) return 1; n   if (s[0] ==  [  && s[1] ==  ] ) return 2; n   if (s[0] ==  (  && s[1] ==  ) ) return 3; n } n int check(int a, int b) { n   if (a == 1 && b == 2 || a == 2 && b == 3 || a == 3 && b == 1) return 1; n   if (a == 2 && b == 1 || a == 3 && b == 2 || a == 1 && b == 3) return 2; n   return 0; n } n int main() { n   char a[100] = {0}, b[100] = {0}; n   scanf( %s , a); n   scanf( %s , b); n   int x = 0, y = 0; n   for (int i = 0; i < strlen(a); i += 2) { n     int t = check(get(a + i), get(b + i)); n     if (t == 1) x++; n     if (t == 2) y++; n   } n   if (x == y) n     printf( TIE ); n   else if (x < y) n     printf( TEAM 2 WINS ); n   else n     printf( TEAM 1 WINS ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int nn, i, j, k; n   cin >> nn; n   long long int a[nn]; n   for (i = 0; i < nn; i++) cin >> a[i]; n   sort(a, a + nn); n   for (i = 0; i < nn; i++) { n     for (j = i + 1; j < nn; j++) { n       if (a[i] != -1 && a[j] != -1 && a[j] % a[i] == 0) a[j] = -1; n     } n   } n   long long int sum = 0; n   for (i = 0; i < nn; i++) { n     if (a[i] != -1) sum++; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n long long ara[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   long long mx = 0; n   for (long long i = 0; i < n; ++i) { n     cin >> ara[i]; n     while (ara[i] % 2 == 0) ara[i] /= 2; n     while (ara[i] % 3 == 0) ara[i] /= 3; n   } n   for (long long i = 1; i < n; ++i) { n     if (ara[i] != ara[0]) return cout <<  No , 0; n   } n   cout <<  Yes ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 100005; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   { n     long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0; n     cin >> n; n     vector<long long int> v; n     cout << n <<    ; n     i = n - 1; n     while (i > 0) { n       if (n % i == 0) { n         cout << i <<    ; n         n = i; n         i = n - 1; n       } else { n         i--; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int i; n     int a[n]; n     int even = 0, odd = 0; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] % 2 == 0) n         even++; n       else n         odd++; n     } n     if (even % 2 == 0 && odd % 2 == 0) { n       cout <<  YES  << endl; n       continue; n     } n     sort(a, a + n, less<int>()); n     for (i = 1; i < n; i++) { n       int k = 1; n       while (a[i - k] == 0 && (i - k) > 0) { n         k++; n       } n       if (a[i] - a[i - k] == 1) { n         a[i] = 0; n         a[i - k] = 0; n         even--; n         odd--; n         if (even % 2 == 0 && odd % 2 == 0) { n           cout <<  YES  << endl; n           break; n         } n       } n     } n     if (i == n) cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[100005]; n long long ans = 0; n long long temp[100005]; n bool is(int m, string s) { n   set<char> ss; n   map<char, int> mp; n   for (int i = 0; i < m; i++) { n     ss.insert(s[i]); n     mp[s[i]]++; n   } n   if (ss.size() == ans) return true; n   for (int i = 0; i <= s.length() - m - 1; i++) { n     mp[s[i]]--; n     if (mp[s[i]] == 0) ss.erase(s[i]); n     ss.insert(s[i + m]); n     mp[s[i + m]]++; n     if (ss.size() == ans) { n       return true; n     } n   } n   return false; n } n int main() { n   int n; n   string s; n   cin >> n >> s; n   set<char> se; n   for (char c : s) se.insert(c); n   ans = se.size(); n   int l = 0; n   int r = n + 1; n   while (r - l > 1) { n     int m = l + (r - l) / 2; n     if (!is(m, s)) n       l = m; n     else n       r = m; n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n const int MOD = 1000000007; n int d, n; n int a[N]; n int ord[N], rnk[N]; n vector<int> v[N]; n inline bool cmp(int x, int y) { return a[x] > a[y]; } n int ans = 0; n int sz[N]; n inline void dfs(int x, int top) { n   sz[x] = 1; n   for (int i = 0; i < v[x].size(); i++) { n     int next = v[x][i]; n     if (sz[next] || rnk[next] < rnk[top] || a[top] - a[next] > d) continue; n     dfs(next, top); n     sz[x] = (long long)sz[x] * (sz[next] + 1) % MOD; n   } n } n int main() { n   scanf( %d%d , &d, &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), ord[i] = i; n   sort(ord + 1, ord + n + 1, cmp); n   for (int i = 1; i <= n; i++) rnk[ord[i]] = i; n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     memset(sz, 0, sizeof(sz)); n     dfs(ord[i], ord[i]); n     ans = (ans + sz[ord[i]]) % MOD; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <typename T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <typename T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <typename T> n inline void println(T first) { n   print(first), putchar(  n ); n } n template <typename T> n inline void printsp(T first) { n   print(first), putchar(   ); n } n template <class T, class T1> n inline void print(T first, T1 second) { n   printsp(first), println(second); n } n template <class T, class T1, class T2> n inline void print(T first, T1 second, T2 z) { n   printsp(first), printsp(second), println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = (long long)a * a % m) n     if (b & 1) ans = (long long)ans * a % m; n   return ans; n } n pair<int, int> p[50010]; n int f[50010]; n void go(int first) { n   printf( ? %d n , first); n   fflush(stdout); n   gn(p[first].first, p[first].second); n } n void end(int first) { n   printf( ! %d n , first); n   exit(0); n } n int main() { n   int n, id, first; n   gn(n, id, first); n   for (int i = 1; i <= n; i++) f[i] = i; n   srand(time(0)); n   random_shuffle(f + 1, f + n + 1); n   go(id); n   if (p[id].first >= first) end(p[id].first); n   int N = min(n, 1000); n   for (int i = 1; i <= N; i++) { n     go(f[i]); n     int u = p[f[i]].first; n     if (u < first && u > p[id].first) id = f[i]; n   } n   while (1) { n     if (!p[id].second) go(id); n     if (p[id].first >= first) end(p[id].first); n     if (p[id].second == -1) end(-1); n     id = p[id].second; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n vector<pair<int, int>> g[N], ans[N]; n long long dist[N]; n priority_queue<pair<int, int>> pq; n bool vis[N]; n void dijkstra(int s) { n   fill(dist, dist + N, 1LL << 61); n   memset(vis, false, sizeof(vis)); n   dist[s] = 0; n   pq.push({0, s}); n   while (!pq.empty()) { n     int u = pq.top().second; n     pq.pop(); n     if (vis[u]) continue; n     vis[u] = true; n     for (int i = 0; i < g[u].size(); i++) { n       int v = g[u][i].first, w = g[u][i].second; n       if (dist[v] > dist[u] + w) { n         dist[v] = dist[u] + w; n         pq.push({-dist[v], v}); n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   int x, y; n   cin >> x >> y; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n   } n   for (int i = 1; i <= n; i++) { n     long long ti, ci; n     cin >> ti >> ci; n     dijkstra(i); n     for (int j = 1; j <= n; j++) { n       if (dist[j] <= ti) { n         ans[i].push_back({j, ci}); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     g[i] = ans[i]; n   } n   dijkstra(x); n   if (dist[y] == 1LL << 61) n     cout << -1; n   else n     cout << dist[y]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e18; n const long long mod = (long long)1e9 + 7; n const double eps = (double)1e-9; n const double pi = acos(-1.0); n const int dx[] = {0, 0, 1, 0, -1}; n const int dy[] = {0, 1, 0, -1, 0}; n const int N = 300500; n int n, l[N], r[N], a[N], ans, c; n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     for (int i = 1; i <= n; ++i) l[i] = 0, r[i] = n; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n       if (l[a[i]] == 0) l[a[i]] = i; n       r[a[i]] = i; n     } n     sort(a + 1, a + n + 1); n     ans = 1, c = 0; n     int m = 1; n     for (int i = 2; i <= n; ++i) { n       if (a[i] == a[i - 1]) continue; n       m++; n       if (r[a[i - 1]] < l[a[i]]) n         c++; n       else n         c = 0; n       ans = max(ans, c + 1); n     } n     cout << m - ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n class IntegerIterator { n  public: n   using value_type = T; n   using difference_type = std::ptrdiff_t; n   using pointer = T*; n   using reference = T; n   using iterator_category = std::input_iterator_tag; n   explicit IntegerIterator(T value) : value(value) {} n   IntegerIterator& operator++() { n     ++value; n     return *this; n   } n   IntegerIterator operator++(int) { n     IntegerIterator copy = *this; n     ++value; n     return copy; n   } n   IntegerIterator& operator--() { n     --value; n     return *this; n   } n   IntegerIterator operator--(int) { n     IntegerIterator copy = *this; n     --value; n     return copy; n   } n   T operator*() const { return value; } n   bool operator==(IntegerIterator rhs) const { return value == rhs.value; } n   bool operator!=(IntegerIterator rhs) const { return !(*this == rhs); } n  n  private: n   T value; n }; n template <typename T> n class IntegerRange { n  public: n   IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; } n   IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); } n   IntegerIterator<T> end() const { return IntegerIterator<T>(end_); } n  n  private: n   T begin_; n   T end_; n }; n template <typename T> n class ReversedIntegerRange { n   using IteratorType = std::reverse_iterator<IntegerIterator<T>>; n  n  public: n   ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; } n   IteratorType begin() const { n     return IteratorType(IntegerIterator<T>(begin_)); n   } n   IteratorType end() const { return IteratorType(IntegerIterator<T>(end_)); } n  n  private: n   T begin_; n   T end_; n }; n template <typename T> n IntegerRange<T> range(T to) { n   return IntegerRange<T>(0, to); n } n template <typename T> n IntegerRange<T> range(T from, T to) { n   return IntegerRange<T>(from, to); n } n template <typename T> n IntegerRange<T> inclusiveRange(T to) { n   return IntegerRange<T>(0, to + 1); n } n template <typename T> n IntegerRange<T> inclusiveRange(T from, T to) { n   return IntegerRange<T>(from, to + 1); n } n template <typename T> n ReversedIntegerRange<T> downrange(T from) { n   return ReversedIntegerRange<T>(from, 0); n } n template <typename T> n ReversedIntegerRange<T> downrange(T from, T to) { n   return ReversedIntegerRange<T>(from, to); n } n template <typename T> n ReversedIntegerRange<T> inclusiveDownrange(T from) { n   return ReversedIntegerRange<T>(from + 1, 0); n } n template <typename T> n ReversedIntegerRange<T> inclusiveDownrange(T from, T to) { n   return ReversedIntegerRange<T>(from + 1, to); n } n namespace impl { n using Random = std::mt19937; n } n inline impl::Random& randomEngine() { n   static std::random_device device; n   static impl::Random engine(device()); n   return engine; n } n inline int randomInteger() { n   std::uniform_int_distribution<int> d; n   return d(randomEngine()); n } n inline int randomInteger(int to) { n   ; n   std::uniform_int_distribution<int> d(0, to - 1); n   return d(randomEngine()); n } n inline int randomInteger(int from, int to) { n   return from + randomInteger(to - from); n } n using int64 = int64_t; n using std::endl; n using std::make_pair; n using std::map; n using std::max; n using std::min; n using std::pair; n using std::set; n using std::sort; n using std::string; n using std::swap; n using std::unique; n using std::vector; n class AShuffleHashing { n  public: n   static void solveSlow(std::stringstream& in, std::ostream& out) {} n   static constexpr int kStressIterations = 0; n   static void generateTest(std::ostringstream& in, std::ostringstream& ___) { n     auto _____ = std::stringstream(in.str()); n     solveSlow(_____, ___); n   } n   static void solve(std::istream& in, std::ostream& out) { n     auto solve = [](string p, string h) -> bool { n       if (h.size() < p.size()) return false; n       sort(p.begin(), p.end()); n       for (int start : range(h.size() - p.size() + 1)) { n         string h_s =   ; n         for (int i : range<int>(start, start + p.size())) { n           assert(i >= 0 && i < h.size()); n           h_s.push_back(h[i]); n         } n         sort(h_s.begin(), h_s.end()); n         if (p == h_s) return true; n       } n       return false; n     }; n     int tests; n     in >> tests; n     for (int test : range(tests)) { n       string p, h; n       in >> p >> h; n       out << (solve(p, h) ?  YES  :  NO ) << endl; n     } n   } n }; n int main() { n   std::ios_base::sync_with_stdio(false); n   AShuffleHashing solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   in.tie(nullptr); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int G = 3; n const int MOD = 998244353; n const int N = (1 << 15) + 5; n int rev[N], w[N], inv_n; n int bigMod(int a, int e, int mod) { n   if (e == -1) e = mod - 2; n   int ret = 1; n   while (e) { n     if (e & 1) ret = (long long)ret * a % mod; n     a = (long long)a * a % mod; n     e >>= 1; n   } n   return ret; n } n void prepare(int &n) { n   int sz = abs(31 - __builtin_clz(n)); n   int r = bigMod(G, (MOD - 1) / n, MOD); n   inv_n = bigMod(n, MOD - 2, MOD), w[0] = w[n] = 1; n   for (int i = 1; i < n; ++i) w[i] = (long long)w[i - 1] * r % MOD; n   for (int i = 1; i < n; ++i) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (sz - 1)); n } n void ntt(int *a, int n, int dir) { n   for (int i = 1; i < n - 1; ++i) { n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   } n   for (int m = 2; m <= n; m <<= 1) { n     for (int i = 0; i < n; i += m) { n       for (int j = 0; j < (m >> 1); ++j) { n         int &u = a[i + j], &v = a[i + j + (m >> 1)]; n         int t = (long long)v * w[dir ? n - n / m * j : n / m * j] % MOD; n         v = u - t < 0 ? u - t + MOD : u - t; n         u = u + t >= MOD ? u + t - MOD : u + t; n       } n     } n   } n   if (dir) n     for (int i = 0; i < n; ++i) a[i] = (long long)a[i] * inv_n % MOD; n } n int f_a[N], f_b[N]; n vector<long long> multiply(vector<long long> a, vector<long long> b) { n   int sz = 1, n = a.size(), m = b.size(); n   while (sz < n + m - 1) sz <<= 1; n   prepare(sz); n   for (int i = 0; i < sz; ++i) f_a[i] = i < n ? a[i] : 0; n   for (int i = 0; i < sz; ++i) f_b[i] = i < m ? b[i] : 0; n   ntt(f_a, sz, 0); n   ntt(f_b, sz, 0); n   for (int i = 0; i < sz; ++i) f_a[i] = (long long)f_a[i] * f_b[i] % MOD; n   ntt(f_a, sz, 1); n   return vector<long long>(f_a, f_a + n + m - 1); n } n int n, k, q; n long long a[N], b[N]; n vector<long long> get(int lo, int hi) { n   if (lo == hi) return {b[lo], 1}; n   int mid = lo + hi >> 1; n   vector<long long> one = get(lo, mid); n   vector<long long> two = get(mid + 1, hi); n   return multiply(one, two); n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) scanf( %lld , a + i); n   cin >> q; n   while (q--) { n     long long cmd, x, l, r, p; n     scanf( %lld %lld %lld %lld , &cmd, &x, &l, &r); n     if (cmd == 1) { n       for (int i = 1; i <= n; ++i) { n         b[i] = a[i]; n         if (i == l) b[i] = r; n       } n     } else { n       scanf( %lld , &p); n       for (int i = 1; i <= n; ++i) { n         b[i] = a[i]; n         if (l <= i and i <= r) b[i] += p; n       } n     } n     for (int i = 1; i <= n; ++i) { n       b[i] = x - b[i], b[i] %= MOD; n       if (b[i] < 0) b[i] += MOD; n     } n     vector<long long> yo = get(1, n); n     printf( %lld n , yo[n - k]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[100003]; n int n; n int main() { n   int i; n   scanf( %d , &n); n   if (n == 1) { n     printf( 1 n ); n     return 0; n   } n   if (n == 2) { n     printf( -1 n ); n     return 0; n   } n   if (n % 2 != 0) p[n / 2 + 1] = n / 2 + 1; n   p[1] = 2; n   p[2] = n; n   p[n] = n - 1; n   p[n - 1] = 1; n   for (i = 3; i < n / 2 + 1; i++) { n     if (i % 2 != 0) n       p[i] = p[i - 2] + 2; n     else n       p[i] = p[i - 2] - 2; n   } n   int coun = 0; n   if (n % 2 == 0) { n     for (i = n - 2; i > n / 2; i--) { n       if (coun % 2 == 0) n         p[i] = p[i + 2] - 2; n       else n         p[i] = p[i + 2] + 2; n       coun++; n     } n   } else { n     for (i = n - 2; i > n / 2 + 1; i--) { n       if (coun % 2 == 0) n         p[i] = p[i + 2] - 2; n       else n         p[i] = p[i + 2] + 2; n       coun++; n     } n   } n   bool fl = true; n   for (i = 1; i <= n; i++) n     if (p[p[i]] != n - i + 1) fl = false; n   if (fl) { n     printf( %d , p[1]); n     for (i = 2; i <= n; i++) printf(  %d , p[i]); n     printf(  n ); n   } else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void use_cio() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n template <typename... Args> n void dbg(Args... args) { n   ((cout << args <<    ), ...); n   cout << endl; n } n int n; n int ask(int r1, int c1, int r2, int c2) { n   string ans; n   dbg( ? , r1, c1, r2, c2); n   cout.flush(); n   cin >> ans; n   return (ans ==  YES ); n } n int main() { n   cin >> n; n   string l, r; n   int sx = 1, sy = 1; n   while (sx + sy < n + 1) { n     if (sy + 1 <= n && ask(sx, sy + 1, n, n)) { n       sy++; n       l +=  R ; n     } else { n       sx++; n       l +=  D ; n     } n   } n   int ex = n, ey = n; n   while (ex + ey > n + 1) { n     if (ex && ask(1, 1, ex - 1, ey)) { n       ex--; n       r +=  D ; n     } else { n       ey--; n       r +=  R ; n     } n   } n   reverse(r.begin(), r.end()); n   dbg( ! , l + r); n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[15][1 << 15]; n int n, m, q; n struct NodeLCA { n   int a, b, c; n }; n vector<pair<int, int> > path; n vector<NodeLCA> LCA; n void readData() { n   cin >> n >> m >> q; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     path.push_back(make_pair(u, v)); n   } n   for (int i = 0; i < q; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     u--; n     v--; n     w--; n     LCA.push_back({u, v, w}); n   } n } n int check(int root, int mask1, int child, int mask2) { n   for (int i = 0; i < path.size(); i++) { n     if (!(mask1 & (1 << path[i].first)) || !(mask1 & (1 << path[i].second))) n       continue; n     if (((mask2 & (1 << path[i].first)) > 0) == n         ((mask2 & (1 << path[i].second)) > 0)) n       continue; n     if (path[i].second == root) swap(path[i].first, path[i].second); n     if ((path[i].first != root) || (path[i].second != child)) return 0; n   } n   for (int i = 0; i < LCA.size(); i++) { n     if (!(mask1 & (1 << LCA[i].a)) || !(mask1 & (1 << LCA[i].b))) continue; n     if (((mask2 & (1 << LCA[i].a)) > 0) == ((mask2 & (1 << LCA[i].b)) > 0)) n       continue; n     if (LCA[i].c != root) return 0; n   } n   return 1; n } n void dynamicProgramming() { n   int mask, submask; n   for (int i = 0; i < n; i++) dp[i][1 << i] = 1; n   for (mask = 1; mask < (1 << n); mask++) n     for (int i = 0; i < n; i++) n       if (mask & (1 << i)) { n         int tmp = -1; n         for (int t = 0; t < n; t++) n           if (t != i && (mask & (1 << t))) tmp = t; n         if (tmp == -1) continue; n         for (submask = (mask - 1) & mask; submask > 0; n              submask = (submask - 1) & mask) n           if ((submask & (1 << tmp)) && !(submask & (1 << i))) n             for (int child = 0; child < n; child++) n               if (submask & (1 << child)) { n                 if (check(i, mask, child, submask)) n                   dp[i][mask] += dp[child][submask] * dp[i][mask - submask]; n               } n       } n } n void writeData() { cout << dp[0][(1 << n) - 1]; } n int main() { n   readData(); n   for (int i = 0; i < LCA.size(); i++) n     if (LCA[i].a == LCA[i].b && LCA[i].a != LCA[i].c) { n       cout << 0; n       return 0; n     } n   dynamicProgramming(); n   writeData(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m, k, count = 0; n   cin >> n >> m >> k; n   long long int s = n * m; n   long long int a[s], b[s]; n   for (long long int i = 0; i < s; i++) cin >> a[i]; n   sort(a, a + s); n   for (long long int i = 0; i < s; i++) { n     b[i] = abs(a[i] - a[s / 2]); n     if (b[i] % k != 0) { n       cout << -1; n       return 0; n     } else { n       count += b[i] / k; n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, k; n int arr[N]; n int a, b; n vector<int> v[N]; n int tmp[N]; n int subtree1[N]; n int subtree2[N]; n int sum[N]; n int bst1[N]; n int bst2[N]; n bool ret; n void dfs(int node, int parent) { n   subtree1[node] = 1; n   subtree2[node] = tmp[node]; n   for (int next : v[node]) { n     if (next != parent) { n       dfs(next, node); n       subtree1[node] += subtree1[next]; n       subtree2[node] += subtree2[next]; n     } n   } n } n void dfs1(int node, int parent) { n   sum[node] = 0; n   bst1[node] = 0; n   bst2[node] = 0; n   for (int next : v[node]) { n     if (next != parent) { n       dfs1(next, node); n       if (subtree2[next] == subtree1[next]) { n         sum[node] += subtree1[next]; n       } else { n         if (sum[next] > bst1[node]) { n           bst2[node] = bst1[node]; n           bst1[node] = sum[next]; n         } else if (sum[next] > bst2[node]) { n           bst2[node] = sum[next]; n         } n       } n     } n   } n   if (tmp[node]) { n     ++sum[node]; n     sum[node] += bst1[node]; n   } else { n     sum[node] = 0; n     bst1[node] = 0; n     bst2[node] = 0; n   } n   ret |= (sum[node] >= k); n } n void dfs2(int node, int parent, int val) { n   if (val == n - subtree1[node]) { n     sum[node] += val; n   } else { n     sum[node] -= bst1[node]; n     if (val > bst1[node]) { n       bst2[node] = bst1[node]; n       bst1[node] = val; n     } else if (val > bst2[node]) { n       bst2[node] = val; n     } n     sum[node] += bst1[node]; n   } n   if (!tmp[node]) { n     sum[node] = 0; n     bst1[node] = 0; n     bst2[node] = 0; n   } n   ret |= (sum[node] >= k); n   for (int next : v[node]) { n     if (next != parent) { n       if (subtree1[next] == subtree2[next]) { n         dfs2(next, node, sum[node] - subtree1[next]); n       } else if (bst1[node] == sum[next]) { n         dfs2(next, node, sum[node] - bst1[node] + bst2[node]); n       } else { n         dfs2(next, node, sum[node]); n       } n     } n   } n } n bool check(int val) { n   for (int i = 1; i <= n; ++i) { n     tmp[i] = (arr[i] >= val); n   } n   ret = 0; n   dfs(1, 0); n   dfs1(1, 0); n   if (ret) { n     return 1; n   } n   dfs2(1, 0, 0); n   return ret; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , arr + i); n   } n   for (int i = 1; i < n; ++i) { n     scanf( %d %d , &a, &b); n     v[a].emplace_back(b); n     v[b].emplace_back(a); n   } n   int l = 1; n   int r = 1e6 + 1; n   while (l < r) { n     int mid = l + r >> 1; n     if (check(mid)) { n       l = mid + 1; n     } else { n       r = mid; n     } n   } n   printf( %d n , l - 1); n } n


#include <bits/stdc++.h> n using namespace std; n struct Bus { n   long long s, t; n }; n bool cmp(Bus &a, Bus &b) { return a.t < b.t; } n long long n, m, it[4 * 111111], tmp = 1, l, r; n Bus bus[111111]; n vector<long long> positions; n void update(int idx, int l, int r, int pos, long long val) { n   if (pos < l || pos > r) return; n   if (l == r) { n     it[idx] = val; n     return; n   } n   int m = (l + r) / 2; n   update(2 * idx, l, m, pos, val); n   update(2 * idx + 1, m + 1, r, pos, val); n   it[idx] = (it[2 * idx] + it[2 * idx + 1]) % 1000000007; n } n long long get(int idx, int l, int r, int u, int v) { n   if (v < l || r < u) return 0; n   if (u <= l && r <= v) return it[idx]; n   int m = (l + r) / 2; n   long long ret = n       (get(2 * idx, l, m, u, v) + get(2 * idx + 1, m + 1, r, u, v)) % n       1000000007; n   return ret; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> bus[i].s >> bus[i].t; n   sort(bus, bus + m, cmp); n   positions.push_back(0); n   for (int i = 0; i < m; i++) n     if (bus[i].t != positions[positions.size() - 1]) n       positions.push_back(bus[i].t); n   if (n != positions[positions.size() - 1]) positions.push_back(n); n   update(1, 0, positions.size() - 1, 0, 1); n   for (int i = 0; i < m; i++) { n     l = lower_bound(positions.begin(), positions.end(), bus[i].s) - n         positions.begin(); n     r = upper_bound(positions.begin(), positions.end(), bus[i].t) - n         positions.begin() - 1; n     tmp = get(1, 0, positions.size() - 1, l, r); n     update(1, 0, positions.size() - 1, r, tmp); n   } n   cout << get(1, 0, positions.size() - 1, positions.size() - 1, n               positions.size() - 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, k; n string s; n bool can_del(char c) { n   int l = N, r = -1; n   for (int i = 0; i < n; ++i) { n     if (s[i] == c) { n       l = min(l, i); n       r = max(r, i); n     } n   } n   return r - l + 1 <= k; n } n string ans1 =  tokitsukaze , ans2 =  quailty , ans3 =  once again ; n bool win(string s) { return can_del( 0 ) || can_del( 1 ); } n vector<int> pos[2]; n int ml[2], mr[2]; n void f(int l, int r, int t) { n   if (pos[t].size() && pos[t][0] < l) ml[t] = min(ml[t], pos[t][0]); n   auto p = upper_bound(pos[t].begin(), pos[t].end(), r); n   if (p != pos[t].end()) { n     ml[t] = min(ml[t], *p); n   } n   if (pos[t].size() && pos[t].back() > r) mr[t] = max(mr[t], pos[t].back()); n   p = lower_bound(pos[t].begin(), pos[t].end(), l); n   if (p != pos[t].begin()) { n     --p; n     mr[t] = max(mr[t], *p); n   } n } n bool col(int l, int r, int t) { n   for (int i = 0; i < 2; ++i) { n     ml[i] = N; n     mr[i] = -1; n   } n   ml[t] = l; n   mr[t] = r; n   f(l, r, 0); n   f(l, r, 1); n   for (int i = 0; i < 2; ++i) { n     if (mr[i] - ml[i] + 1 <= k) { n       return 0; n     } n   } n   return 1; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k >> s; n   for (int i = 0; i < n; ++i) { n     pos[s[i] -  0 ].push_back(i); n   } n   if (win(s)) { n     cout << ans1 <<   n ; n     exit(0); n   } n   for (int i = 0; i + k - 1 < n; ++i) { n     if (col(i, i + k - 1, 0)) { n       cout << ans3 <<   n ; n       exit(0); n     } n     if (col(i, i + k - 1, 1)) { n       cout << ans3 <<   n ; n       exit(0); n     } n   } n   cout << ans2 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, size = 1e6 + 1; n   cin >> n; n   vector<int> prime(1e6 + 1, 1); n   prime[0] = 0; n   prime[1] = 0; n   for (int ri = 2; ri <= sqrt(size); ri++) { n     if (prime[ri]) { n       for (int pi = ri * ri; pi <= size; pi += ri) n         if (prime[pi]) prime[pi] = 0; n     } n   } n   vector<long long int> tprime; n   for (long long int ri = 0; ri < size; ri++) { n     if (prime[ri]) { n       tprime.push_back(ri * ri); n     } n   } n   long long int arr[n]; n   for (int ri = 0; ri < n; ri++) scanf( %lld , &arr[ri]); n   for (int ri = 0; ri < n; ri++) { n     bool val = binary_search(tprime.begin(), tprime.end(), arr[ri]); n     if (val) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n int a[maxn]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int m = s.length(); n     int fa = 0, fb = 0, fc = 0; n     for (int i = 0; i < m; i++) { n       if (s[i] ==  1  && fa == 0) fa = i + 1; n       if (s[i] ==  2  && fb == 0) fb = i + 1; n       if (s[i] ==  3  && fc == 0) fc = i + 1; n     } n     if (fa == 0 || fb == 0 || fc == 0) { n       cout <<  0  << endl; n       continue; n     } n     int x = max(fa, max(fb, fc)); n     int ans = x - min(fa, min(fb, fc)) + 1; n     int sum = ans; n     for (int i = 0; i < m; i++) { n       if (s[i] ==  1 ) { n         int u = min(fb, fc); n         int v = max(fb, fc); n         if (i + 1 >= v) n           sum = i + 1 - u + 1; n         else if (i + 1 <= u) n           sum = v - i; n         else n           sum = v - u + 1; n         ans = min(ans, sum); n         fa = i + 1; n       } n       if (s[i] ==  2 ) { n         int u = min(fa, fc); n         int v = max(fa, fc); n         if (i + 1 >= v) n           sum = i + 1 - u + 1; n         else if (i + 1 <= u) n           sum = v - i; n         else n           sum = v - u + 1; n         ans = min(ans, sum); n         fb = i + 1; n       } n       if (s[i] ==  3 ) { n         int u = min(fb, fa); n         int v = max(fb, fa); n         if (i + 1 >= v) n           sum = i + 1 - u + 1; n         else if (i + 1 <= u) n           sum = v - i; n         else n           sum = v - u + 1; n         ans = min(ans, sum); n         fc = i + 1; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n