#include <bits/stdc++.h> n #pragma GCC optimize(                                                                                n      -fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2 ,   n     3) n #pragma GCC target( avx ,  sse2 ) n const double esp = 1e-6; n const double pi = acos(-1.0); n const int INF = 0x3f3f3f3f; n const int inf = 1e9; n using namespace std; n long long read() { n   char ch = getchar(); n   long long x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int N = 6e5 + 10; n const int diff = 3e5; n int Q; n struct node { n   int pos, to, h; n   long long cntx, cnty; n   node(int pos, int to, int h, long long cntx, long long cnty) { n     this->pos = pos, this->to = to, this->h = h, this->cntx = cntx, n     this->cnty = cnty; n   } n }; n struct DSU { n   int par[N], height[N]; n   long long cntx[N], cnty[N]; n   long long sum = 0; n   DSU() { n     for (int i = 1; i <= 6e5 + 1; i++) { n       par[i] = i, height[i] = 1; n       if (i <= 3e5) n         cntx[i] = 1; n       else n         cnty[i] = 1; n     } n   } n   int find(int x) { return x == par[x] ? x : find(par[x]); } n   int unit(int x, int y, stack<node> &st) { n     x = find(x), y = find(y); n     if (x == y) return 0; n     if (height[x] < height[y]) swap(x, y); n     node sttemp(y, x, height[y], cntx[y], cnty[y]); n     st.push(sttemp); n     sum -= cntx[x] * cnty[x]; n     sum -= cntx[y] * cnty[y]; n     cntx[x] += cntx[y]; n     cnty[x] += cnty[y]; n     sum += cntx[x] * cnty[x]; n     par[y] = x; n     height[x] += height[y]; n     return 1; n   } n   void del(stack<node> &st) { n     while (st.size()) { n       node temp = st.top(); n       st.pop(); n       par[temp.pos] = temp.pos; n       height[temp.to] -= temp.h; n       sum -= cntx[temp.to] * cnty[temp.to]; n       cntx[temp.to] -= temp.cntx; n       cnty[temp.to] -= temp.cnty; n       sum += cntx[temp.to] * cnty[temp.to]; n       sum += temp.cntx * temp.cnty; n     } n   } n } dsu; n struct SEG { n   vector<pair<int, int> > tr[N << 2]; n   void update(int l, int r, int root, int lf, int rt, pair<int, int> val) { n     if (lf <= l && r <= rt) { n       tr[root].push_back(val); n       return; n     } n     int mid = (l + r) >> 1; n     if (lf <= mid) update(l, mid, root << 1, lf, rt, val); n     if (rt > mid) update(mid + 1, r, root << 1 | 1, lf, rt, val); n   } n   void query(int l, int r, int root) { n     stack<node> st; n     for (auto k : tr[root]) { n       dsu.unit(k.first, k.second + diff, st); n     } n     if (l == r) { n       printf( %lld  , dsu.sum); n       dsu.del(st); n       return; n     } n     int mid = (l + r) >> 1; n     query(l, mid, root << 1); n     query(mid + 1, r, root << 1 | 1); n     dsu.del(st); n   } n } sgt; n map<pair<int, int>, pair<int, int> > mp; n struct nd { n   int x, y, st, ed; n }; n vector<nd> query; n int main() { n   Q = read(); n   int x, y; n   for (int i = 1; i <= Q; i++) { n     x = read(), y = read(); n     if (mp[{x, y}].first) { n       query.push_back(nd{x, y, mp[{x, y}].first, i - 1}); n       mp[{x, y}].first = 0; n     } else { n       mp[{x, y}].first = i; n     } n   } n   for (auto k : mp) { n     if (k.second.first) n       query.push_back(nd{k.first.first, k.first.second, k.second.first, Q}); n   } n   for (auto k : query) { n     sgt.update(1, Q, 1, k.st, k.ed, make_pair(k.x, k.y)); n   } n   sgt.query(1, Q, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string mat[n + 5]; n   int dp[n + 5][n + 5]; n   for (int i = 0; i < (n); i++) cin >> mat[i]; n   memset(dp, 0, sizeof(dp)); n   if (mat[0][0] ==  a ) dp[0][0] = 1; n   for (int i = 1; i < (n); i++) { n     dp[i][0] = dp[i - 1][0]; n     if (mat[i][0] ==  a ) dp[i][0]++; n     dp[0][i] = dp[0][i - 1]; n     if (mat[0][i] ==  a ) dp[0][i]++; n   } n   for (int i = 1; i < (n); i++) { n     for (int j = 1; j < (n); j++) { n       dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); n       if (mat[i][j] ==  a ) dp[i][j]++; n     } n   } n   int maxi = -1; n   for (int i = 0; i < (n); i++) { n     for (int j = 0; j < (n); j++) { n       if (i + j + 1 - dp[i][j] - k <= 0) maxi = max(maxi, i + j); n     } n   } n   set<pair<int, int>> pts; n   if (maxi == -1) n     pts.insert(make_pair(0, 0)); n   else { n     for (int i = 0; i < (n); i++) { n       for (int j = 0; j < (n); j++) { n         if (i + j == maxi && (i + j + 1 - dp[i][j] - k) == 0) { n           pts.insert(make_pair(i, j)); n         } n       } n     } n   } n   string ans, cont; n   for (int i = 0; i < (maxi + 1); i++) ans.push_back( a ); n   if (maxi == -1) ans.push_back(mat[0][0]), maxi++; n   for (int i = maxi; i < (2 * n - 2); i++) { n     char mini =  z  + 1; n     for (auto x : pts) { n       int r = x.first, c = x.second; n       if (r < n - 1) mini = min(mini, mat[r + 1][c]); n       if (c < n - 1) mini = min(mini, mat[r][c + 1]); n     } n     ans.push_back(mini); n     set<pair<int, int>> temp; n     for (auto x : pts) { n       int r = x.first, c = x.second; n       if (r < n - 1) { n         if (mat[r + 1][c] == mini) temp.insert(make_pair(r + 1, c)); n       } n       if (c < n - 1) { n         if (mat[r][c + 1] == mini) temp.insert(make_pair(r, c + 1)); n       } n     } n     pts = temp; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<int, vector<int> > ps; n   set<int> xs; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     ps[y].push_back(x); n     xs.insert(x); n   } n   map<int, int> rk; n   const int INF = 1 << 30; n   for (int x : xs) { n     int i = rk.size(); n     rk[x] = i + 1; n   } n   rk[INF] = xs.size() + 1; n   int bit[200100] = {}; n   long long ans = 0; n   xs.clear(); n   for (auto it = ps.rbegin(); it != ps.rend(); it++) { n     int y = it->first; n     sort(it->second.begin(), it->second.end()); n     for (int i = 0; i < it->second.size(); i++) { n       int l = it->second[i], n           r = (i + 1 == it->second.size() ? INF : it->second[i + 1]); n       long long sum1 = 0, sum2 = 0; n       if (xs.find(l) == xs.end()) { n         xs.insert(l); n         for (int j = rk[l]; j <= rk.size() + 1; j += j & -j) bit[j]++; n       } n       for (int j = rk[l]; j > 0; j -= j & -j) sum1 += bit[j]; n       for (int j = rk[r] - 1; j > 0; j -= j & -j) sum2 += bit[j]; n       ans += sum1 * (sum2 - sum1 + 1); n     } n   } n   cout << ans << endl; 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     string s; n     bool x = false, y = false; n     cin >> s; n     if (n == 1) { n       s[0] % 2 ? cout <<  1 n  : cout <<  2 n ; n       continue; n     } n     for (int i = 0; i < n; i++) { n       if ((i + 1) % 2 == 1) n         x |= ((s[i] -   0 ) % 2 == 1); n       else if ((i + 1) % 2 == 0) n         y |= ((s[i] -   0 ) % 2 == 0); n     } n     if (n % 2 == 1) { n       x ? cout << 1 : cout << 2; n     } else { n       y ? cout << 2 : cout << 1; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long INF = 4e18; n const int inf = 2e9; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a * b / gcd(a, b); } n int a[3005], pos[3005]; n int main(void) { n   int n, i, j, naik = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     pos[a[i]] = i; n   } n   int ans = 0; n   for (i = 1; i <= n; ++i) { n     while (pos[i] > i) { n       swap(a[pos[i]], a[pos[i] - 1]); n       swap(pos[a[pos[i]]], pos[a[pos[i] - 1]]); n       ans++; n     } n   } n   ans = ((ans >> 1) << 2) + (ans & 1); n   printf( %.9f n , (double)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n; n   char s[105][105]; n   while (~scanf( %d , &n)) { n     for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1); n     vector<pair<int, int> > r; n     bool rc = true; n     for (int i = 1; i <= n; ++i) { n       int j; n       for (j = 1; j <= n; ++j) n         if (s[i][j] ==  . ) { n           r.push_back(pair<int, int>(i, j)); n           break; n         } n       if (j > n) { n         rc = false; n         break; n       } n     } n     vector<pair<int, int> > c; n     bool cc = true; n     for (int j = 1; j <= n; ++j) { n       int i; n       for (i = 1; i <= n; ++i) n         if (s[i][j] ==  . ) { n           c.push_back(pair<int, int>(i, j)); n           break; n         } n       if (i > n) { n         cc = false; n         break; n       } n     } n     if (rc) { n       for (int i = 0; i < n; ++i) printf( %d %d n , r[i].first, r[i].second); n     } else if (cc) { n       for (int i = 0; i < n; ++i) printf( %d %d n , c[i].first, c[i].second); n     } else { n       puts( -1 ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5 + 10; n vector<string> tot[MaxN]; n int cnt; n char s[MaxN]; n bool check[MaxN], flag; n set<string> ans; n int main() { n   scanf( %s , s); n   int len = strlen(s); n   check[len] = 1; n   for (int i = len - 2; i >= 5; i--) { n     if (check[i + 2]) { n       string t; n       t += s[i]; n       t += s[i + 1]; n       if (tot[i + 2].size() == 0) { n         check[i] = 1; n         tot[i].push_back(t); n         ans.insert(t); n       } else { n         flag = 1; n         for (int j = 0; j < tot[i + 2].size(); j++) { n           if (tot[i + 2][j] != t) { n             flag = 0; n             break; n           } n         } n         if (!flag) { n           tot[i].push_back(t); n           ans.insert(t), check[i] = 1; n         } n       } n     } n     if (check[i + 3]) { n       string t; n       t += s[i]; n       t += s[i + 1]; n       t += s[i + 2]; n       if (tot[i + 3].size() == 0) { n         check[i] = 1; n         tot[i].push_back(t); n         ans.insert(t); n       } else { n         flag = 1; n         for (int j = 0; j < tot[i + 3].size(); j++) { n           if (tot[i + 3][j] != t) { n             flag = 0; n             break; n           } n         } n         if (!flag) { n           tot[i].push_back(t); n           ans.insert(t), check[i] = 1; n         } n       } n     } n   } n   printf( %d n , ans.size()); n   for (set<string>::iterator ite = ans.begin(); ite != ans.end(); ite++) { n     cout << *ite << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int r, g, b, w; n     cin >> r >> g >> b >> w; n     int evens = (r % 2 == 0) + (g % 2 == 0) + (b % 2 == 0) + (w % 2 == 0); n     int odds = (r % 2 == 1) + (g % 2 == 1) + (b % 2 == 1) + (w % 2 == 1); n     if (evens == 4 || evens == 3 || odds == 4 || (odds == 3 && r && g && b)) 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 int main() { n   string name; n   cin >> name; n   string right =   , left =   ; n   int i = 0; n   for (; true; i++) { n     if (name[i] ==  | ) { n       break; n     } n     left += name[i]; n   } n   i++; n   for (; i < name.length(); i++) { n     right += name[i]; n   } n   int ll = left.length(); n   int rl = right.length(); n   string toAdd; n   cin >> toAdd; n   if (abs(rl - ll) == 0) { n     if (toAdd.length() % 2 == 0) { n       for (int i = 0; i < toAdd.length(); i += 2) { n         right += toAdd[i]; n         left += toAdd[i + 1]; n       } n       cout << left <<  |  << right; n     } else { n       cout <<  Impossible ; n     } n   } else if (abs(rl - ll) == toAdd.length()) { n     if (rl > ll) { n       for (int i = 0; i < (rl - ll); i++) { n         left += toAdd[i]; n       } n       cout << left <<  |  << right; n     } else { n       for (int i = 0; i < (ll - rl); i++) { n         right += toAdd[i]; n       } n       cout << left <<  |  << right; n     } n   } else if (abs(ll - rl) < toAdd.length()) { n     if ((rl + ll + toAdd.length()) % 2 == 0) { n       if (rl > ll) { n         for (int i = 0; i < (rl - ll); i++) { n           left += toAdd[i]; n         } n         for (int i = abs(ll - rl); i < toAdd.length(); i += 2) { n           right += toAdd[i]; n           left += toAdd[i + 1]; n         } n         cout << left <<  |  << right; n       } else { n         for (int i = 0; i < (ll - rl); i++) { n           right += toAdd[i]; n         } n         for (int i = abs(ll - rl); i < toAdd.length(); i += 2) { n           right += toAdd[i]; n           left += toAdd[i + 1]; n         } n         cout << left <<  |  << right; n       } n     } else { n       cout <<  Impossible ; n     } n   } else { n     cout <<  Impossible ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 7; n pair<double, double> a[MAXN]; n double F[MAXN], S[MAXN]; n int n, p; n bool cmp(pair<double, double> x, pair<double, double> y) { n   return x.first * y.second > x.second * y.first; n } n int vt = -1; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> p; n   for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; n   if (n == 1) { n     if (p >= a[1].first) { n       cout << -1; n       return 0; n     } n     double res = a[1].second / (a[1].first - p); n     cout << res; n     return 0; n   } n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; i++) S[i] = S[i - 1] + a[i].second; n   for (int i = 1; i <= n; i++) F[i] = F[i - 1] + a[i].first; n   for (int i = 2; i <= n; i++) { n     double value = a[i].second / a[i].first; n     double res = value * F[i - 1] - S[i - 1]; n     if (res > p * value) { n       vt = i; n       break; n     } n   } n   if (vt == -1) { n     double res = S[n] / (F[n] - p); n     if (F[n] <= p) n       cout << -1; n     else n       cout << res; n     return 0; n   } n   double kq = S[vt - 1] / (F[vt - 1] - p); n   cout << fixed; n   cout << setprecision(10) << kq; n   return 0; n } n


#include <bits/stdc++.h> n #include <string> n #include <cstdint> n #include <iterator> n #include <algorithm> n #include <fstream> n #include <vector> n #include <set> n  n using namespace std; n  n using ll = long long; n using ld = long double; n  n #define FAST_IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); n #define Int int_fast64_t n #define pb push_back n #define mp make_pair n #define all(x) (x).begin(),(x).end() n #define fi first n #define se second n  n #define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll)) n #define mine(a)    (*min_element((a).begin(), (a).end())) n #define maxe(a)    (*max_element((a).begin(), (a).end())) n  n typedef vector<Int> vi; n typedef pair<Int, Int> pii; n typedef pair<Int, bool> pib; n typedef pair<string, Int> psi; n typedef pair<string, string> pss; n typedef vector<pii> vpii; n typedef vector<psi> vpsi; n typedef vector<pss> vpss; n  n  n using namespace std; n // Vector n template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;} n template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i <<    ; return os;} n template <typename T> n pair<T, bool> getNthElement(set<T>& searchSet,int index) n { n     pair<T, bool> result; n  n     // Check if index is valid or not n     if (searchSet.size() > index) { n         result.first n                 = *(std::next( n                 searchSet.begin(), n                 index)); n         result.second = true; n     } n  n     else n         result.second = false; n  n     // Return the pair n     return result; n } n  n  n const Int N=7+1e10; n  n Int gcd(Int a, Int b) n { n     // Everything divides 0 n     if (a == 0) n         return b; n     if (b == 0) n         return a; n  n     // base case n     if (a == b) n         return a; n  n     // a is greater n     if (a > b) n         return gcd(a-b, b); n     return gcd(a, b-a); n } n  n int main() { n     FAST_IO; n  n     Int t; cin >> t; n     while(t--){ n         Int n; cin >> n; n         vi arr(n); n         cin >> arr; n         Int ans=0; n         Int Min=mine(arr); n         auto it=find(all(arr), Min); n         Int a=it-arr.begin(); n         if(arr[0]==Min){ n             cout << n-1 << endl; n         } n         else{ n             cout << n << endl; n             cout <<  1   << a+1 <<     << Min <<     << Min+1 <<   n ; n         } n         for(Int i=1;i<n;++i){ n             cout <<  1   << i+1 <<     << Min <<     << Min+i <<   n ; n         } n     } n  n     return 0; n } n  n


#include <bits/stdc++.h> n const long long P = 1000000000039LL; n inline long long mul(long long a, long long b) { n   long long ans = 0; n   for (; b; b >>= 1) { n     if (b & 1) { n       ans = (ans + a) % P; n     } n     a = (a << 1) % P; n   } n   return ans; n } n int k = 0, t = 0; n long long fac[10], ifac[10]; n void init() { n   fac[0] = 1; n   ifac[0] = 1; n   fac[1] = 1; n   ifac[1] = 1; n   for (int i = 2; i < 10; i++) { n     ifac[i] = (P - mul(P / i, ifac[P % i])) % P; n   } n   for (int i = 2; i < 10; i++) { n     ifac[i] = mul(ifac[i - 1], ifac[i]); n     fac[i] = mul(fac[i - 1], i); n   } n } n int cnt[16]; n long long f[10], g[10]; n long long c[10]; n void calc(int len) { n   std::fill(f, f + len + 1, 0); n   for (int i = 0; i < 16; i++) { n     if (cnt[i] < 0) { n       return; n     } n   } n   f[0] = 1; n   for (int i = 0; i < 16; i++) { n     std::fill(g, g + len + 1, 0); n     for (int j = 0; j <= cnt[i]; j++) { n       for (int u = 0; u <= len - j; u++) { n         g[u + j] = (g[u + j] + mul(ifac[j], f[u])) % P; n       } n     } n     std::copy(g, g + len + 1, f); n   } n } n void preDP() { n   std::fill(cnt, cnt + 16, t); n   c[0] = 0; n   cnt[1]--; n   calc(9); n   for (int i = 1; i < 10; i++) { n     c[i] = c[i - 1] + 15 * mul(f[i - 1], fac[i - 1]); n   } n } n long long solve(long long x) { n   long long l = 0, ans = 0; n   int d[10]; n   std::fill(cnt, cnt + 16, t); n   for (; x; d[l++] = x % 16, x /= 16) n     ; n   if (l > 0) { n     ans += c[l - 1]; n   } n   for (int i = l - 1; i >= 0; i--) { n     for (int u = ((i == l - 1) ? 1 : 0); u < d[i]; u++) { n       cnt[u]--; n       calc(i); n       ans += mul(f[i], fac[i]); n       cnt[u]++; n     } n     cnt[d[i]]--; n   } n   for (int i = 0; i < 16; i++) { n     if (cnt[i] < 0) { n       return ans; n     } n   } n   return ans + 1; n } n int main() { n   scanf( %d%d , &k, &t); n   init(); n   preDP(); n   long long l = 0, r = 0xFFFFFFFFFLL; n   for (long long mid = 0; l + 1 < r; n        solve(mid = (l + r) >> 1) >= k ? r = mid : l = mid) n     ; n   printf( %I64x n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   double r; n   cin >> n >> r; n   double X[n]; n   for (int i = 0; i < n; i++) { n     cin >> X[i]; n   } n   double Y[n]; n   Y[0] = r; n   for (int i = 1; i < n; i++) { n     Y[i] = r; n     for (int j = 0; j < i; j++) { n       if (abs(X[i] - X[j]) <= 2 * r) n         Y[i] = max(Y[i], Y[j] + sqrt(4 * r * r + 2 * X[i] * X[j] - X[i] * X[i] - n                                      X[j] * X[j])); n     } n   } n   for (int i = 0; i < n; i++) { n     cout << fixed << setprecision(9) << Y[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> grundies; n   grundies.push_back(0); n   for (int i = 1; i <= 60; i++) { n     long long accum = 0; n     long long add = 0; n     while (accum < i) { n       accum += add; n       add++; n     } n     if (accum > i) { n       add--; n       accum -= add; n     } n     add--; n     grundies.push_back(add); n   } n   long long numPiles; n   cin >> numPiles; n   long long finalState = 0; n   for (long long i = 0; i < numPiles; i++) { n     long long pile; n     cin >> pile; n     finalState = finalState ^ grundies[pile]; n   } n   if (!finalState) cout <<  YES  << endl; n   if (finalState) cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:225450978 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18; n const long double Pi = 3.141592653589793116, EPS = 1e-9, n                   Gold = ((1 + sqrt(5)) / 2); n long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL}; n long long keyCount = sizeof(keymod) / sizeof(long long); n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcountll(s); n } n auto TimeStart = chrono::steady_clock::now(); n auto TimeEnd = chrono::steady_clock::now(); n void ControlIO(int argc, char* argv[]); n void TimerStart(); n void TimerStop(); n void Exit(); n long long n, k, d1, d2; n void Input() { cin >> n >> k >> d1 >> d2; } n void Solve() { n   long long A = LINF, B = 0, C = 0; n   for (long long i = 0; i < 2; i++) { n     for (long long j = 0; j < 2; j++) { n       if (i) n         B = A - d1; n       else n         B = A + d1; n       if (j) n         C = B - d2; n       else n         C = B + d2; n       if ((k - A - B - C + min(A, min(B, C)) * 3) % 3 != 0 || n           k - A - B - C + min(A, min(B, C)) * 3 < 0) n         continue; n       if ((A + B + C + n - k) % 3 == 0 && n           (A + B + C + n - k) / 3 >= max(A, max(B, C))) { n         cout <<  yes n ; n         return; n       } n     } n   } n   cout <<  no n ; n } n int main(int argc, char* argv[]) { n   ControlIO(argc, argv); n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int T; n   cin >> T; n   TimerStart(); n   while (T--) { n     Input(); n     Solve(); n   } n   TimerStop(); n   return 0; n } n void ControlIO(int argc, char* argv[]) {} n void TimerStart() {} n void TimerStop() {} n void Exit() { n   TimerStop(); n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(string s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> t) { n   return  (  + to_string(get<0>(t)) +  ,   + to_string(get<1>(t)) +  ,   + n          to_string(get<2>(t)) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void _debug() { cerr <<   n ; } n template <typename Head, typename... Tail> n void _debug(Head H, Tail... T) { n   cerr <<     << to_string(H); n   _debug(T...); n } n using ll = long long; n using ld = long double; n using ii = pair<int, int>; n using vi = vector<int>; n using vii = vector<ii>; n using vc = vector<char>; n using vb = vector<bool>; n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(9); n   int n, k; n   cin >> n >> k; n   int lo = 1, hi = n; n   while (lo < hi) { n     int mid = lo + (hi - lo) / 2; n     int total = 0; n     int cur = mid; n     while (cur) { n       total += cur; n       cur /= k; n     } n     if (total >= n) { n       hi = mid; n     } else { n       lo = mid + 1; n     } n   } n   cout << hi; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename S> n ostream &operator<<(ostream &os, const pair<T, S> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename C, typename T = decay<decltype(*begin(declval<C>()))>, n           typename enable_if<!is_same<C, string>::value>::type * = nullptr> n ostream &operator<<(ostream &os, const C &c) { n   bool f = true; n   os <<  [ ; n   for (const auto &x : c) { n     if (!f) os <<  ,  ; n     f = false; n     os << x; n   } n   return os <<  ] ; n } n template <typename T> n void debug(string s, T x) { n   cerr <<   033[1;35m  << s <<   033[0;32m =  033[33m  << x <<   033[0m n ; n } n template <typename T, typename... Args> n void debug(string s, T x, Args... args) { n   for (int i = 0, b = 0; i < (int)s.size(); i++) n     if (s[i] ==  (  || s[i] ==  { ) n       b++; n     else if (s[i] ==  )  || s[i] ==  } ) n       b--; n     else if (s[i] ==  ,  && b == 0) { n       cerr <<   033[1;35m  << s.substr(0, i) <<   033[0;32m =  033[33m  << x n            <<   033[31m |  ; n       debug(s.substr(s.find_first_not_of(   , i + 1)), args...); n       break; n     } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, d; n   cin >> n >> d; n   vector<array<int, 3>> x(n); n   for (int i = 0; i < n; i++) { n     int s, a; n     cin >> s >> a; n     x[i] = {max(s, a), s, a}; n   } n   int ret = 0; n   sort(x.begin(), x.end()); n   for (auto [_, s, a] : x) n     if (s >= d) { n       ret++; n       d = max(d, a); n     } n   cout << ret <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long; n using lld = long double; n using ulli = unsigned long long int; n using pll = pair<lli, lli>; n using ttt = pair<lli, pll>; n using vttt = vector<ttt>; n using vll = vector<pll>; n using vl = vector<lli>; n using vi = vector<int>; n using vvi = vector<vector<int>>; n using cd = complex<lld>; n void sanjeev(int kkkk) { n   int t; n   cin >> t; n   while (t > 0) { n     t--; n     long long int a, b, x, y; n     cin >> a >> b >> x >> y; n     long long int n; n     cin >> n; n     long long int sa = min(n, a - x); n     long long int sb = min(n, b - y); n     if (a - sa < b - sb) { n       a -= sa; n       n -= sa; n       sb = min(n, b - y); n       b -= sb; n       cout << a * b <<   n ; n     } else { n       b -= sb; n       n -= sb; n       sa = min(n, a - x); n       a -= sa; n       cout << a * b <<   n ; n     } n   } n } n int main() { n   if (!0) ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << fixed << setprecision(10); n   int t = 1; n   for (int i = 0; i < t; i++) { n     sanjeev(i + 1); n   } n   if (0) system( pause ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> x[100000], G[100000]; n int n, a[100000], col[100000], cnt, p[2001][1000]; n long long ret = 1, ret2 = 0; n long long gcd(long long r, long long s) { n   if (s == 0) return r; n   return gcd(s, r % s); n } n void dfs(int pos) { n   if (col[pos] >= 1) return; n   col[pos] = cnt; n   G[cnt].push_back(pos); n   for (int i = 0; i < x[pos].size(); i++) dfs(x[pos][i]); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n     x[i].push_back(a[i]); n     x[a[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     if (col[i] >= 1) continue; n     cnt++; n     dfs(i); n   } n   for (int i = 1; i <= cnt; i++) { n     for (int j = 0; j <= 2000; j++) { n       for (int k = 0; k < 1000; k++) p[j][k] = 0; n     } n     for (int j : G[i]) p[0][j] = j; n     for (int j = 0; j < 2000; j++) { n       for (int k : G[i]) { n         p[j + 1][k] = a[p[j][k]]; n       } n     } n     long long cnt2 = 0, cnt3 = 0; n     for (int j = 1; j <= 1000; j++) { n       bool OK = true; n       for (int k = 0; k < n; k++) { n         if (p[j][k] != p[2 * j][k]) OK = false; n       } n       if (OK == true) { n         if (cnt2 == 0) n           cnt2 = j; n         else { n           cnt3 = j; n           break; n         } n       } n     } n     ret = ret / gcd(ret, cnt3 - cnt2) * (cnt3 - cnt2); n     ret2 = max(ret2, cnt2); n   } n   long long B = ret; n   while (ret2 > B) B += ret; n   cout << B << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int MAXN = 50 + 10; n template <typename T> n inline void add(T& a, T b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n int main() { n   static long long g[MAXN][MAXN]; n   static long long f[MAXN][MAXN]; n   static long long F[MAXN][MAXN]; n   static long long inv[MAXN]; n   ios::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= n; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; n   f[0][1] = 1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= i + 1; ++j) { n       for (int p = 0; p <= i - 1; ++p) { n         for (int q = j; q <= p + 1; ++q) n           add(g[i][j], f[p][q] * f[i - 1 - p][j] % MOD); n         for (int q = j + 1; q <= i - p; ++q) n           add(g[i][j], f[p][j] * f[i - 1 - p][q] % MOD); n       } n       memset(F, 0, sizeof(F)); n       for (int k = 0; k <= n; ++k) { n         for (int p = 1; p <= k + 1; ++p) { n           long long c = 1; n           for (int q = 1; k + q * i <= n; ++q) { n             c = c * (g[i][j] + q - 1) % MOD * inv[q] % MOD; n             add(F[k + q * i][p + q * j], f[k][p] * c % MOD); n           } n         } n       } n       for (int k = 0; k <= n; ++k) n         for (int p = 1; p <= k + 1; ++p) add(f[k][p], F[k][p]); n     } n   } n   cout << f[n][m] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n; n   cin >> n; n   string s1, s2; n   cin >> s1 >> s2; n   long long a = 0, b = 0, c = 0, d = 0; n   vector<long long> a1, b1, c1, d1; n   for (long long i = 0; i < n; ++i) { n     if (s1[i] ==  1  && s2[i] ==  1 ) { n       d++; n       d1.push_back(i + 1); n     } n     if (s1[i] ==  1  && s2[i] ==  0 ) { n       c++; n       c1.push_back(i + 1); n     } n     if (s1[i] ==  0  && s2[i] ==  1 ) { n       b++; n       b1.push_back(i + 1); n     } n     if (s1[i] ==  0  && s2[i] ==  0 ) { n       a++; n       a1.push_back(i + 1); n     } n   } n   long long x; n   for (long long i = 0; i <= min(b, n / 2); ++i) { n     for (long long j = 0; j <= min(c, n / 2 - i); ++j) { n       if (j < b - i) { n         x = b - i - j; n         if (x > d || (d - x) % 2) continue; n         x = x + (d - x) / 2; n         if (x + i + j > n / 2 || (n / 2 - (x + i + j)) > a) continue; n       } else { n         x = j - b + i; n         if (x > d || (d - x) % 2) continue; n         x = (d - x) / 2; n         if (x + i + j > n / 2 || (n / 2 - (x + i + j)) > a) continue; n       } n       for (long long u = 0; u < i; ++u) { n         cout << b1[u] <<    ; n       } n       for (long long u = 0; u < j; ++u) { n         cout << c1[u] <<    ; n       } n       for (long long u = 0; u < x; ++u) { n         cout << d1[u] <<    ; n       } n       for (long long u = 0; u < n / 2 - i - j - x; ++u) { n         cout << a1[u] <<    ; n       } n       return 0; n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n, m; n map<int, bool> ma, mb; n int a[maxn], b[maxn]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (ma[a[i]] == true) { n       printf( 0 n ); n       return 0; n     } n     ma[a[i]] = true; n   } n   int ans = 3; n   for (int i = 1; i <= n; i++) { n     b[i] = (a[i] & m); n     if (ma[b[i]] == true && a[i] != b[i]) { n       ans = min(ans, 1); n       break; n     } n     if (mb[b[i]] == true) { n       ans = min(ans, 2); n     } n     mb[b[i]] = true; n   } n   if (ans == 3) ans = -1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1600 + 5; n const int dr[] = {0, 0, 1, -1, 1, -1, 1, -1}; n const int dc[] = {1, -1, 0, 0, 1, 1, -1, -1}; n int n, m, nr, nc, cnt, ct; n bool grid[MAX][MAX], x[MAX][MAX], y[MAX][MAX], z[MAX][MAX], val; n vector<int> ans; n pair<int, int> cur; n bool inside(int r, int c) { n   if (r > 0 && r <= n && c > 0 && c <= m) return 1; n   return 0; n } n void dfs(int r, int c) { n   grid[r][c] = 0; n   x[r][c] = 1; n   for (int i = 0; i <= 3; i++) { n     nr = r + dr[i], nc = c + dc[i]; n     if (!grid[nr][nc]) continue; n     dfs(nr, nc); n   } n } n void cs() { n   for (int r = 1; r <= n; r++) n     for (int c = 1; c <= m; c++) { n       if (!x[r][c]) continue; n       if (!inside(r - 1, c - 1)) continue; n       if (!inside(r + 1, c + 1)) continue; n       val = 1; n       for (int k = 0; k <= 7; k++) n         if (!x[r + dr[k]][c + dc[k]]) { n           val = 0; n           break; n         } n       if (val) { n         y[r][c] = 1; n         for (int k = 0; k <= 7; k++) y[r + dr[k]][c + dc[k]] = 1; n       } n     } n } n void cas() { n   for (int r = 1; r <= n; r++) n     for (int c = 1; c <= m; c++) { n       if (!x[r][c]) continue; n       if (!inside(r - 2, c - 2)) continue; n       if (!inside(r + 2, c + 2)) continue; n       val = 1; n       for (int i = r - 2; i <= r + 2; i++) n         for (int j = c - 2; j <= c + 2; j++) n           if (!x[i][j]) { n             val = 0; n             break; n           } n       if (val) n         for (int i = r - 2; i <= r + 2; i++) n           for (int j = c - 2; j <= c + 2; j++) z[i][j] = 1; n     } n   for (int r = 1; r <= n; r++) n     for (int c = 1; c <= m; c++) z[r][c] ^= y[r][c]; n } n void cast() { n   for (int r = 1; r <= n; r++) n     for (int c = 1; c <= m; c++) { n       if (!z[r][c]) continue; n       if (!inside(r - 1, c - 1)) continue; n       if (!inside(r + 1, c + 1)) continue; n       val = 1; n       for (int k = 0; k <= 7; k++) n         if (!z[r + dr[k]][c + dc[k]]) { n           val = 0; n           break; n         } n       y[r][c] = val; n     } n } n void df(int r, int c) { n   ct++; n   y[r][c] = 0; n   for (int i = 0; i <= 7; i++) { n     nr = r + dr[i], nc = c + dc[i]; n     if (!y[nr][nc]) continue; n     df(nr, nc); n   } n } n void hit() { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (!y[i][j]) continue; n       df(i, j); n       cnt++; n     } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> grid[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (!grid[i][j]) continue; n       memset(x, 0, sizeof x); n       memset(y, 0, sizeof y); n       memset(z, 0, sizeof z); n       dfs(i, j); n       cs(); n       cas(); n       memset(y, 0, sizeof y); n       cast(); n       cnt = 0; n       hit(); n       ans.push_back(cnt); n     } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200200], b[200200]; n set<pair<long long, long long> > s; n set<pair<long long, long long> >::iterator it, it2; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, i; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i], s.insert({a[i], i}); n   for (it = s.begin(); it != s.end();) { n     it2 = it; n     it2++; n     if (it2 == s.end()) break; n     if ((*it).first == (*it2).first) { n       s.insert({(*it).first * 2, (*it2).second}); n       s.erase(it2); n       it2 = it; n       it++; n       s.erase(it2); n     } else n       it++; n   } n   for (auto x : s) { n     b[x.second] = x.first; n   } n   cout << s.size() << endl; n   for (i = 0; i < n; i++) { n     if (b[i]) cout << b[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 101010 * 2; n int a[maxn], n, m; n int b[maxn]; n int p[maxn]; n int query(int poin, long long l, bool dir) { n   int tv; n   int cir; n   int las; n   if (dir) { n     tv = upper_bound(b + 1, b + 1 + n, b[poin] + l) - b - 1; n     if (tv == poin) return p[poin]; n     cir = l / (b[tv] - b[poin]); n     las = l % (b[tv] - b[poin]); n     if (cir & 1) n       return query(tv, las, !dir); n     else n       return query(poin, las, dir); n   } else { n     tv = lower_bound(b + 1, b + 1 + n, b[poin] - l) - b; n     if (tv == poin) return p[poin]; n     cir = l / (b[poin] - b[tv]); n     las = l % (b[poin] - b[tv]); n     if (cir & 1) n       return query(tv, las, !dir); n     else n       return query(poin, las, dir); n   } n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n   } n   sort(b + 1, b + 1 + n); n   for (int i = 1; i <= n; i++) { n     p[lower_bound(b + 1, b + 1 + n, a[i]) - b] = i; n   } n   while (m--) { n     int poin, l; n     scanf( %d%d , &poin, &l); n     poin = lower_bound(b + 1, b + 1 + n, a[poin]) - b; n     int tv = upper_bound(b + 1, b + 1 + n, b[poin] + l) - b - 1; n     printf( %d n , query(tv, l - (b[tv] - b[poin]), 0)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int64_t nearest_power_of_2(int64_t a) { n   int cnt = 0; n   while (a / 2 != 0) { n     a = a / 2; n     cnt++; n   } n   return cnt; n } n int main() { n   int64_t t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int64_t n; n     cin >> n; n     vector<int64_t> v; n     for (int j = 0; j < n; j++) { n       int64_t k; n       cin >> k; n       v.push_back(k); n     } n     int64_t max = 0; n     int64_t b = 0; n     for (int j = 0; j < n - 1; j++) { n       if (v[j + 1] < v[j]) { n         b++; n         int64_t d = v[j] - v[j + 1]; n         if (max < nearest_power_of_2(d)) { n           max = nearest_power_of_2(d); n         } n         v[j + 1] = v[j]; n       } n     } n     if (max == 0 && b == 0) { n       cout << 0 << endl; n     } else { n       cout << max + 1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 2000; n const int maxm = 1000 * 10; n struct edge { n   int v, w, next; n } e[maxm * 2]; n struct node { n   int d, u; n   bool operator<(const node &b) const { return d > b.d; } n }; n int head[maxn], d[maxn], d2[maxn]; n int tot; n bool vis[maxn]; n int g[maxn][maxn]; n void add(int u, int v, int w) { n   e[tot].v = v; n   e[tot].w = w; n   e[tot].next = head[u]; n   head[u] = tot++; n } n void dijkstra(int s, int t, int n, bool dir) { n   for (int i = 1; i <= n; i++) { n     if (dir) n       d[i] = INF; n     else n       d2[i] = INF; n   } n   if (dir) n     d[s] = 0; n   else n     d2[s] = 0; n   memset(vis, 0, sizeof(vis)); n   priority_queue<node> q; n   q.push(node{0, s}); n   while (!q.empty()) { n     node temp = q.top(); n     q.pop(); n     int u = temp.u; n     if (vis[u]) continue; n     vis[u] = 1; n     for (int i = head[u]; i != -1; i = e[i].next) { n       int v = e[i].v; n       int w = e[i].w; n       if (dir) { n         if (d[v] > d[u] + w) { n           d[v] = d[u] + w; n           q.push(node{d[v], v}); n         } n       } else { n         if (d2[v] > d2[u] + w) { n           d2[v] = d2[u] + w; n           q.push(node{d2[v], v}); n         } n       } n     } n   } n } n int main() { n   int n, m, s, t, u, v; n   cin >> n >> m >> s >> t; n   memset(head, -1, sizeof(head)); n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     g[u][v] = 1; n     g[v][u] = 1; n     add(u, v, 1); n     add(v, u, 1); n   } n   dijkstra(s, t, n, true); n   dijkstra(t, s, n, false); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if (!g[i][j] && i != j) { n         if (d[i] + d2[j] + 1 >= d[t] && d2[i] + d[j] + 1 >= d[t]) { n           ans++; n           g[i][j] = g[j][i] = true; n         } n       } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, h, f[100], g[100][100]; n long long fun(long long x) { n   int i; n   if (f[x] > -1) return 0; n   f[x] = 0; n   for (i = 0; i < x; ++i) { n     fun(i); n     fun(x - i - 1); n     f[x] += f[i] * f[x - i - 1]; n   } n   return 0; n } n long long rec(long long x, long long d) { n   int i; n   if (g[x][d] > -1) return 0; n   g[x][d] = 0; n   if (d > x) return 0; n   if (d == 1) { n     g[x][d] = f[x]; n     return 0; n   } n   for (i = 0; i < x; ++i) { n     rec(i, d - 1); n     rec(x - i - 1, d - 1); n     g[x][d] += g[i][d - 1] * f[x - i - 1] + g[x - i - 1][d - 1] * f[i] - n                g[i][d - 1] * g[x - i - 1][d - 1]; n   } n   return 0; n } n int main() { n   int i, j; n   cin >> n >> h; n   for (i = 0; i <= n; ++i) f[i] = -1; n   f[0] = 1; n   for (i = 0; i <= n; ++i) n     for (j = 0; j <= n; ++j) g[i][j] = -1; n   g[0][0] = 1; n   fun(n); n   rec(n, h); n   cout << g[n][h] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long r; n   cin >> r; n   if (r % 2 == 0 || r == 1 || r == 3) n     cout <<  NO ; n   else { n     long long x, y; n     long long up = (r - 1); n     for (int i = 1; i < 100000; i++) { n       if ((up / i - i - 1) % 2 == 0) { n         x = i; n         y = (up / i - i - 1) / 2; n         break; n       } n     } n     cout << x <<     << y; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n const long long N = 100011; n void solve() { n   long long n, ans = 0; n   cin >> n; n   pair<long long, long long> a[n + 17]; n   multiset<long long> s; n   for (long long i = 1; i <= n; i++) cin >> a[i].first; n   for (long long i = 1; i <= n; i++) cin >> a[i].second; n   sort(a + 1, a + n + 1); n   long long sum = 0; n   long long mn = 0; n   a[n + 1].first = -1; n   for (long long ind = 1; ind <= n; ind++) { n     mn += 1; n     if (s.empty()) mn = a[ind].first; n     long long j = ind; n     while (a[j].first == mn) { n       s.insert(a[j].second); n       sum += a[j].second; n       j += 1; n     } n     long long mx = *(--s.end()); n     sum -= mx; n     s.erase(s.find(mx)); n     ans += sum; n     ind = j - 1; n   } n   while (s.size()) { n     int mx = *(--s.end()); n     sum -= mx; n     s.erase(s.find(mx)); n     ans += sum; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c = 0; n   string s; n   cin >> n >> a >> b >> s; n   bool turnA; n   if (a > b) n     turnA = true; n   else n     turnA = false; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  . ) { n       if ((i == 0 || s[i - 1] ==  * ) && a > b) { n         s[i] =  a ; n         c++; n         a--; n         turnA = false; n       } else if ((i == 0 || s[i - 1] ==  * ) && a < b) { n         s[i] =  b ; n         c++; n         b--; n         turnA = true; n       } else if (turnA && a > 0) { n         s[i] =  a ; n         c++; n         a--; n         turnA = false; n       } else if (!turnA && b > 0) { n         c++; n         b--; n         s[i] =  b ; n         turnA = true; n       } else n         turnA = !turnA; n     } n   } n   cout << c << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n int main(){ n  long long int t,a1,b1,f1,a2,b2,f2,x; n  cin>>t; n  while(t--){ n   x=0; n   cin>>a1>>a2>>b1>>b2>>f1>>f2; n   if((a1==f1 && a2==f2) || (b1==f1 && b2==f2)){ n //   cout<< a ; n    cout<<0; n    continue; n   } n   if(a1<b1)swap(a1,b1); n   if(a2<b2)swap(a2,b2); n   if(a1==b1 && b1==f1 ){ n    if(a2>f2 && f2>b2)x+=2; n    x+=a2-b2; n //   cout<< b ; n   } n   else if(a2==b2 && b2==f2){ n    if(a1>f1 && f1>b1)x+=2; n    x+=a1-b1; n //   cout<< c ; n   } else { n    x=a1+a2-b1-b2; n //   cout<< d ; n   } n   cout<<x<<endl; n    n  } n   n  return 0; n }


#include <bits/stdc++.h> n const int maxn = 5e5 + 5; n int fa[maxn], t[maxn], n, m, k, v, u, ev, eu; n int find(int n) { return n == fa[n] ? n : (fa[n] = find(fa[n])); } n void merge(int u, int v) { n   ev = find(v); n   eu = find(u); n   if (ev != eu) { n     fa[ev] = eu; n     t[eu] += t[ev]; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) fa[i] = i, t[i] = 1; n   for (int i = 0; i < m; ++i) { n     scanf( %d , &k); n     if (k) scanf( %d , &u); n     for (int j = 0; j < k - 1; ++j) { n       scanf( %d , &v); n       merge(u, v); n     } n   } n   for (int i = 1; i <= n; ++i) printf( %d  , t[find(i)]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const int inf = 0x3f3f3f3f, N = 2e5 + 10; n const double pi = acos(-1); n using namespace std; n char s[N]; n int num[N]; n int main() { n   int n, k; n   while (cin >> n >> k) { n     scanf( %s , s + 1); n     memset(num, 0, sizeof(num)); n     for (int i = 1; s[i]; i++) { n       if (s[i] ==  0 ) n         num[i] = num[i - 1] + 1; n       else n         num[i] = num[i - 1]; n     } n     int ans = inf; n     int last = 0; n     if (k == 1) { n       for (int i = 1; i <= n; i++) { n         if (!last && s[i] ==  0 ) n           last = i; n         else if (last && s[i] ==  0 ) { n           ans = i - last; n           break; n         } n       } n     } n     for (int i = 1; i <= n; i++) { n       if (s[i] ==  1 ) continue; n       int l = 1, r = n; n       while (l <= r) { n         int mid = (l + r) >> 1; n         int st = max(1, i - mid); n         int en = min(n, i + mid); n         if (num[en] - num[st - 1] >= k + 1) n           r = mid - 1; n         else n           l = mid + 1; n       } n       ans = min(ans, l); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10; n long long a[MAXN]; n long long b[MAXN]; n int n, m; n long long c2(long long x) { return (x * (x - 1)) / 2; } n long long f(int l) { n   if (m < l * n) { n     return -1; n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] < i * l + n * c2(l)) { n       return -1; n     } n     b[i] = a[i] - (i * l + n * c2(l)); n   } n   long long res = 0, f = m; n   for (int i = l; i >= 1; i--) { n     for (int j = n; j >= 1; j--) { n       b[j] += j + (i - 1) * n; n       if (b[j] >= f) { n         res += f; n         b[j] -= f; n         f--; n       } else { n         res += b[j]; n         f = b[j] - 1; n         b[j] = 0; n       } n     } n   } n   return res; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int L = 1, R = (m / n) + 1; n   while (R - L > 5) { n     int mid = (L + R) / 2; n     if (f(mid) < f(mid + 1)) { n       L = mid; n     } else { n       R = mid; n     } n   } n   long long res = 0; n   for (int i = L; i <= R; i++) { n     res = max(res, f(i)); n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, r; n     cin >> n >> r; n     long long int sum = 0; n     if (n == 1) { n       sum = 1; n     } else if (n <= r) { n       sum = ((n - 1) * (n)) / 2 + 1; n     } else { n       sum = (r * (r + 1)) / 2; n     } n     cout << sum << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, p, q, r; n   cin >> n >> p >> q >> r; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long dp[n][3]; n   dp[0][0] = 1ll * p * a[0]; n   for (int i = 1; i < n; i++) { n     dp[i][0] = max(dp[i - 1][0], 1ll * p * a[i]); n   } n   dp[0][1] = dp[0][0] + 1ll * q * a[0]; n   for (int i = 1; i < n; i++) { n     dp[i][1] = max(dp[i - 1][1], dp[i][0] + 1ll * q * a[i]); n   } n   dp[0][2] = dp[0][1] + 1ll * r * a[0]; n   for (int i = 1; i < n; i++) { n     dp[i][2] = max(dp[i - 1][2], dp[i][1] + 1ll * r * a[i]); n   } n   cout << dp[n - 1][2] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chkmax(T &x, T y) { n   return x < y ? x = y, true : false; n } n template <typename T> n bool chkmin(T &x, T y) { n   return x > y ? x = y, true : false; n } n int readint() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n struct node { n   int val, id; n   node(int val = 0, int id = 0) : val(val), id(id) {} n   bool operator<(const node c) const { return val < c.val; } n }; n int n, m, g, r; n int a[10005]; n int d[10005][1005]; n bool vis[10005][1005]; n priority_queue<node> q; n int id(int x, int y) { return (x - 1) * g + y - 1; } n void dij() { n   memset(d, 0x3f, sizeof(d)); n   q.push(node(0, id(1, g))); n   d[1][g] = 0; n   while (!q.empty()) { n     int tid = q.top().id; n     q.pop(); n     pair<int, int> t = make_pair(tid / g + 1, tid % g + 1); n     if (vis[t.first][t.second]) continue; n     if (t.first == m) { n       if (t.second == g) n         printf( %d n , d[t.first][t.second] - r); n       else n         printf( %d n , d[t.first][t.second]); n       return; n     } n     vis[t.first][t.second] = 1; n     if (t.first > 1) { n       int tx = t.first - 1, ty = t.second - a[t.first] + a[t.first - 1]; n       if (ty > 0) { n         if (chkmin(d[tx][ty], n                    d[t.first][t.second] + a[t.first] - a[t.first - 1])) n           q.push(node(-d[tx][ty], id(tx, ty))); n       } n       if (t.second == a[t.first] - a[t.first - 1]) { n         ty = g; n         if (chkmin(d[tx][ty], n                    d[t.first][t.second] + a[t.first] - a[t.first - 1] + r)) n           q.push(node(-d[tx][ty], id(tx, ty))); n       } n     } n     if (t.first < m) { n       int tx = t.first + 1, ty = t.second - a[t.first + 1] + a[t.first]; n       if (ty > 0) { n         if (chkmin(d[tx][ty], n                    d[t.first][t.second] + a[t.first + 1] - a[t.first])) n           q.push(node(-d[tx][ty], id(tx, ty))); n       } n       if (t.second == a[t.first + 1] - a[t.first]) { n         ty = g; n         if (chkmin(d[tx][ty], n                    d[t.first][t.second] + a[t.first + 1] - a[t.first] + r)) n           q.push(node(-d[tx][ty], id(tx, ty))); n       } n     } n   } n   printf( -1 n ); n } n int main() { n   n = readint(); n   m = readint(); n   for (int i = 1; i <= m; i++) a[i] = readint(); n   sort(a + 1, a + m + 1); n   g = readint(); n   r = readint(); n   dij(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n bool vis[N]; n pair<int, int> pt[N]; n int dp[N]; n int main() { n   memset(dp, 0x3f, sizeof(dp)); n   int a, m, n; n   cin >> a >> n >> m; n   for (int i = 1; i <= n; i++) { n     int l, r; n     scanf( %d %d , &l, &r); n     for (int j = l + 1; j <= r; j++) { n       vis[j] = true; n     } n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &pt[i].first, &pt[i].second); n   } n   sort(pt + 1, pt + 1 + m); n   for (int i = 1; i <= pt[1].first; i++) { n     if (vis[i] == true) { n       puts( -1 ); n       return 0; n     } n   } n   dp[0] = 0; n   int cnt = 1; n   for (int i = 1; i <= a; i++) { n     while (cnt <= m && pt[cnt].first < i) cnt++; n     if (vis[i] == false) n       dp[i] = dp[i - 1]; n     else { n       for (int j = 1; j < cnt; j++) { n         dp[i] = min(dp[i], dp[pt[j].first] + pt[j].second * (i - pt[j].first)); n       } n     } n   } n   printf( %d n , dp[a]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <class T, class U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   os <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (int i = 0; i < (int)v.size(); i++) { n     if (i) { n       os <<  , ; n     } n     os << v[i]; n   } n   os <<  } ; n   return os; n } n ostream &operator<<(ostream &os, const set<int> &se) { n   os <<  { ; n   int now = 0; n   for (auto x : se) { n     if (now) { n       os <<  , ; n     } n     os << x; n     now++; n   } n   os <<  } ; n   return os; n } n void debugg() { cerr << endl; } n template <class T, class... Args> n void debugg(const T &x, const Args &...args) { n   cerr <<     << x; n   debugg(args...); n } n using namespace std; n const int inf = (1 << 30) - 1; n const long long linf = 1LL << 61; n const int MAX = 510000; n int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1}; n int dx[8] = {-1, 0, 1, 0, 1, -1, 1, -1}; n const double pi = acos(-1); n const double eps = 1e-8; n template <typename T1, typename T2> n inline bool chmin(T1 &a, T2 b) { n   if (a > b) { n     a = b; n     return true; n   } else n     return false; n } n template <typename T1, typename T2> n inline bool chmax(T1 &a, T2 b) { n   if (a < b) { n     a = b; n     return true; n   } else n     return false; n } n template <typename T> n inline void print(T &a) { n   int sz = a.size(); n   for (auto itr = a.begin(); itr != a.end(); itr++) { n     cout << *itr; n     sz--; n     if (sz) cout <<    ; n   } n   cout <<   n ; n } n template <typename T1, typename T2> n inline void print2(T1 a, T2 b) { n   cout << a <<     << b <<   n ; n } n template <typename T1, typename T2, typename T3> n inline void print3(T1 a, T2 b, T3 c) { n   cout << a <<     << b <<     << c <<   n ; n } n void mark() { n   cout <<  #  n        <<   n ; n } n long long pcount(long long x) { return __builtin_popcountll(x); } n const int mod = 998244353; n template <typename T> n struct BIT { n   vector<T> dat; n   long long sz; n   BIT(long long sz) : sz(sz) { dat.assign(++sz, 0); } n   T sum(long long k) { n     T ret = 0; n     for (++k; k > 0; k -= k & -k) ret += dat[k]; n     return (ret); n   } n   void add(long long k, T x) { n     for (++k; k < dat.size(); k += k & -k) dat[k] += x; n   } n   long long get(T k) { n     if (k <= 0) return 0; n     long long ret = 0; n     int n = 1; n     while (n < sz) n *= 2; n     for (int i = n / 2; i > 0; i /= 2) { n       if (ret + i < sz && dat[ret + i] < k) { n         k -= dat[ret + i]; n         ret += i; n       } n     } n     return ret; n   } n }; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<pair<long long, long long>> a(n * m); n   vector<long long> b(n * m); n   for (int i = 0; i < (n * m); i++) { n     cin >> a[i].first; n     a[i].second = i; n     b[i] = a[i].first; n   } n   sort((a).begin(), (a).end()); n   vector<vector<long long>> c(n, vector<long long>(m)); n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) { n       c[i][j] = a[i * m + j].second; n     } n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) { n       int k = j + 1; n       while (k < m && b[c[i][k]] == b[c[i][j]]) k++; n       reverse(c[i].begin() + j, c[i].begin() + k); n       j = k - 1; n     } n   vector<pair<long long, long long>> pos(n * m); n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) { n       pos[c[i][j]] = {i, j}; n     } n   long long ans = 0; n   vector<BIT<long long>> bit(n, BIT<long long>(m)); n   for (int i = 0; i < (n * m); i++) { n     ans += bit[pos[i].first].sum(pos[i].second); n     bit[pos[i].first].add(pos[i].second, 1); n   } n   cout << ans <<   n ; n } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include<bits/stdc++.h> n #define ll long long n #define R return n #define pb push_back n #define SI size() n  n using namespace std; n  n long long t,n,a[100030],ans,w[5030]; n  n int main(){ n     cin >> t; n     while(t--){ n         ans = 0; n         memset(w,0,sizeof w); n         cin >> n; n         for(int i=0;i<n;i++) n             cin >> a[i]; n         for(int i=0;i<n;i++){ n             if(a[i] - w[i] > 1) n                 ans += a[i] - w[i] - 1; n             if(a[i] - w[i] < 1) n                 w[i+1] += 1 - a[i] + w[i]; n             if(a[i] > 1) n                 for(int j=i+2;j<=min(i+a[i],n);j++) w[j]++; n         } n         cout << ans << endl; n     } n     return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(int64_t &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const int64_t &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n template <class T, class... U> n void DEBUG(const T &head, const U &...tail) {} n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 5e5 + 10; n long long mypow(long long x, long long y) { n   x %= MOD; n   long long res = 1 % MOD; n   while (y) { n     if (y & 1) res = res * x % MOD; n     y >>= 1; n     x = x * x % MOD; n   } n   return res; n } n int ll[2][SIZE]; n int d[SIZE], dn; n vector<pair<int, int> > seg[2]; n template <class T> n void maa(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n void mii(T &x, T y) { n   if (x > y) x = y; n } n int get_v(int x) { return lower_bound(d, d + dn, x) - d; } n int get_len(int x) { return d[x] - d[x - 1]; } n struct BIT { n   int _n; n   long long _d[SIZE]; n   void init(int __n) { n     _n = __n; n     memset(_d, 0, sizeof(long long) * (_n + 1)); n   } n   void ins(int x, long long v) { n     x++; n     for (; x <= _n; x += x & -x) ADD(_d[x], v); n   } n   long long qq(int x) { n     x++; n     long long res = 0; n     for (; x; x -= x & -x) ADD(res, _d[x]); n     return res; n   } n   long long qq_range(int x, int y) { n     long long ret = qq(y); n     if (x) ADD(ret, -qq(x - 1)); n     return ret; n   } n } bit[2]; n int main() { n   int K, N, M; n   R(K, N, M); n   if (N + M == 0) { n     W(mypow(2, K)); n     return 0; n   } n   d[dn++] = 0; n   for (int i = 0; i < (N); ++i) { n     int x, y; n     R(x, y); n     d[dn++] = x - 1; n     d[dn++] = y; n     seg[0].push_back(make_pair(x - 1, y)); n   } n   for (int i = 0; i < (M); ++i) { n     int x, y; n     R(x, y); n     d[dn++] = x - 1; n     d[dn++] = y; n     seg[1].push_back(make_pair(x - 1, y)); n   } n   sort(d, d + dn); n   dn = unique(d, d + dn) - d; n   for (int j = 0; j < (2); ++j) { n     for (int i = 0; i < (((int)(seg[j]).size())); ++i) { n       maa(ll[j][get_v(seg[j][i].second)], get_v(seg[j][i].first) + 1); n     } n   } n   for (int i = 0; i < (2); ++i) bit[i].init(dn + 2); n   bit[0].ins(1, mypow(2, get_len(1)) - 2); n   if (ll[0][1] == 0) { n     bit[1].ins(0, 1); n   } n   if (ll[1][1] == 0) { n     bit[0].ins(0, 1); n   } n   int bb[2] = {}; n   for (int i = (2); i < (dn); ++i) { n     long long both[2] = {}; n     int len = get_len(i); n     for (int j = 0; j < (2); ++j) ADD(both[j], bit[j].qq(i - 1)); n     while (bb[0] < ll[1][i]) { n       long long v = bit[0].qq_range(bb[0], bb[0]); n       bit[0].ins(bb[0], -v); n       bb[0]++; n     } n     if (ll[1][i] != i) bit[0].ins(i - 1, both[1]); n     while (bb[1] < ll[0][i]) { n       long long v = bit[1].qq_range(bb[1], bb[1]); n       bit[1].ins(bb[1], -v); n       bb[1]++; n     } n     if (ll[0][i] != i) bit[1].ins(i - 1, both[0]); n     bit[0].ins(i, (both[0] + both[1]) * (mypow(2, len) - 2) % MOD); n   } n   long long ans = 0; n   for (int i = 0; i < (2); ++i) ADD(ans, bit[i].qq(dn)); n   W(ans * mypow(2, (K - d[dn - 1])) % MOD); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:60000000 ) n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n const long long inf64 = ((long long)1 << 62) - 1; n const long double pi = 3.1415926535897932384626433832795; n const string task =   ; n template <class T> n T sqr(T x) { n   return x * x; n } n int n, m; n int a[100500]; n int last[100500]; n char ans[100500]; n int x[100500], y[100500]; n void solve() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (int)(m); i++) { n     scanf( %d , &a[i]); n     ans[i] =  N ; n     last[i] = -1; n   } n   for (int i = 0; i < (int)(n - 1); i++) { n     scanf( %d%d , &x[i], &y[i]); n     x[i]--; n     if (x[i] >= 0) last[x[i]] = i; n   } n   int s = 0; n   bool first = 1; n   for (int i = 0; i < (int)(n - 1); i++) { n     if (first && y[i] == 1) { n       first = 0; n       int mn = inf; n       for (int j = 0; j < (int)(m); j++) n         if (a[j] <= s && last[j] < i) { n           ans[j] =  Y ; n           mn = min(mn, a[j]); n         } n       s -= mn; n     } n     if (x[i] != -1) { n       a[x[i]]--; n     } else { n       s++; n     } n   } n   cerr << s << endl; n   for (int i = 0; i < (int)(m); i++) n     if (a[i] <= s) ans[i] =  Y ; n   for (int i = 0; i < (int)(m); i++) { n     printf( %c , ans[i]); n   } n   printf(  n ); n } n int main() { n   int tt; n   scanf( %d , &tt); n   for (int ii = 0; ii < (int)(tt); ii++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5, oo = 0x3f3f3f3f; n pair<int, int> stop[maxn]; n int nxt[maxn]; n int main(void) { n   int d, n, m; n   scanf( %d%d%d , &d, &n, &m); n   int x, y; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     stop[i] = pair<int, int>(x, y); n   } n   stop[0] = pair<int, int>(0, 0); n   stop[m + 1] = pair<int, int>(d, 0); n   sort(stop, stop + m + 1); n   stack<int> s; n   memset(nxt, -1, sizeof(nxt)); n   for (int i = m; i >= 1; i--) { n     while (!s.empty() && stop[i].second <= stop[s.top()].second) s.pop(); n     if (s.empty()) n       nxt[i] = m + 1; n     else n       nxt[i] = s.top(); n     s.push(i); n   } n   nxt[0] = s.top(); n   int after; n   long long ans = 0; n   int now = n; n   int dist; n   for (int i = 0; i <= m + 1; i++) { n     if (now < 0) return puts( -1 ), 0; n     dist = stop[nxt[i]].first - stop[i].first; n     if (dist > now) n       after = dist; n     else n       after = now; n     if (after > n) after = n; n     ans += (after - now) * 1ll * stop[i].second; n     after -= stop[i + 1].first - stop[i].first; n     now = after; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int a, int b) { return a > b; } n int h[2005]; n int main() { n   int i, n, a, b, ans = 0; n   scanf( %d %d %d , &n, &a, &b); n   for (i = 0; i < n; ++i) scanf( %d , &h[i]); n   sort(h, h + n, cmp); n   if (h[a - 1] > h[a]) ans = h[a - 1] - h[a]; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200100; n int t, n; n int MOD; n vector<pair<long long int, long long int> > v[N]; n long long int kol[N]; n long long int gotov[N], sad, a, b; n long long int c; n long long int sol; n vector<long long int> p; n long long int FI; n long long int inv[N], inv1[N]; n long long int powmod(long long int a, long long int x) { n   long long int rez = 1; n   while (x) { n     if (x % 2) { n       rez = ((rez % MOD) * (a % MOD)) % MOD; n     } n     a = ((a % MOD) * (a % MOD)) % MOD; n     x /= 2; n   } n   return rez % MOD; n } n long long int divide(long long int a, long long int dub) { n   return a * inv[dub] % MOD; n } n int djeca(int node, int par) { n   kol[node] = 0; n   for (int i = 0; i < v[node].size(); i++) { n     if (v[node][i].first != par && !gotov[v[node][i].first]) n       kol[node] += djeca(v[node][i].first, node); n   } n   kol[node]++; n   return kol[node]; n } n int centr(int node, int par, int sz) { n   for (int i = 0; i < v[node].size(); i++) { n     if (v[node][i].first != par && !gotov[v[node][i].first] && n         kol[v[node][i].first] > sz / 2) { n       return centr(v[node][i].first, node, sz); n     } n   } n   return node; n } n map<long long int, long long int> m; n map<long long int, long long int> pom[N]; n void dfs(int node, int par, long long int x, long long int y, int d, int turn) { n   int di = ((-y) % MOD + MOD) % MOD; n   di = divide(di, d) % MOD; n   if (par != -1) sol += (m[di] - pom[turn][di]); n   if (!x && par != -1) sol++; n   if (!y && par != -1) sol++; n   for (int i = 0; i < v[node].size(); i++) { n     if (v[node][i].first != par && !gotov[v[node][i].first]) { n       int kamo; n       if (par == -1) n         kamo = i; n       else n         kamo = turn; n       long long int novix = ((v[node][i].second * inv1[d]) % MOD + x) % MOD; n       long long int noviy = ((y * 10) % MOD + v[node][i].second) % MOD; n       dfs(v[node][i].first, node, novix, noviy, d + 1, kamo); n       if (par == -1) pom[i].clear(); n     } n   } n   return; n } n void broji(int node, int par, long long int x, int d, int turn) { n   if (par != -1) { n     m[x % MOD]++; n     pom[turn][x % MOD]++; n   } n   for (int i = 0; i < v[node].size(); i++) { n     if (v[node][i].first != par && !gotov[v[node][i].first]) { n       int kamo; n       if (par == -1) n         kamo = i; n       else n         kamo = turn; n       long long int novix = ((v[node][i].second * inv1[d]) % MOD + x) % MOD; n       broji(v[node][i].first, node, novix, d + 1, kamo); n     } n   } n   return; n } n void decomp(int node, int par) { n   kol[node] = djeca(node, par); n   int sad = centr(node, par, kol[node]); n   broji(sad, -1, 0, 0, 0); n   dfs(sad, -1, 0, 0, 0, 0); n   m.clear(); n   gotov[sad] = 1; n   for (int i = 0; i < v[sad].size(); i++) { n     if (!gotov[v[sad][i].first]) decomp(v[sad][i].first, node); n   } n   return; n } n void solve() { n   cin >> n >> MOD; n   for (int i = 0; i < n - 1; i++) { n     cin >> a >> b >> c; n     v[a].push_back(make_pair(b, c)); n     v[b].push_back(make_pair(a, c)); n   } n   long long int k = MOD; n   long long int result = MOD; n   for (int p = 2; p * p <= k; ++p) { n     if (k % p == 0) { n       while (k % p == 0) k /= p; n       result -= result / p; n     } n   } n   if (k > 1) result -= result / k; n   FI = result; n   for (int i = 0; i <= n; i++) { n     inv1[i] = powmod(10, i); n   } n   for (int i = 0; i <= n; i++) { n     inv[i] = powmod(inv1[i], FI - 1); n   } n   decomp(0, -1); n   cout << sol << endl; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n long long a[26], dp[26][26]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tc = 1; n   while (tc--) { n     string s; n     cin >> s; n     int n = s.length(); n     long long mx = 0; n     for (int i = 0; i < n; i++) { n       int x = s[i] -  a ; n       for (int j = 0; j < 26; j++) { n         dp[j][x] += a[j]; n         mx = mx > dp[j][x] ? mx : dp[j][x]; n       } n       a[x]++; n       mx = mx > a[x] ? mx : a[x]; n     } n     cout << mx; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool pal(string& s) { n   int j = s.size() - 1; n   int i = 0; n   while (i < j) { n     if (s[i++] != s[j--]) return false; n   } n   return true; n } n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   for (int len = n; len >= 0; len--) { n     for (int start = 0; start + len - 1 < n; start++) { n       string sub = s.substr(start, len); n       if (pal(sub)) n         continue; n       else { n         cout << len << endl; n         return 0; n       } n     } n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1e9 + 7; n bool isPoTwo(long long int x) { return x && (!(x & (x - 1))); } n long long int power(long long int x, unsigned long long int y, n                     long long int p) { n   long long int res = 1; n   x = x % p; n   if (x == 0) return 0; 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 void solve() { n   long long int n, x, y, d; n   cin >> n >> x >> y >> d; n   long long int ans = INT_MAX, ans1 = INT_MAX, ans2 = INT_MAX; n   if ((y - x) % d == 0) ans = (abs(y - x) / d); n   if ((y - 1) % d == 0 || (n - y) % d == 0) { n     if ((y - 1) % d == 0) { n       ans1 = (y - 1) / d; n       ans1 += ((x - 1) / d) + 1; n     } n     if ((n - y) % d == 0) { n       ans2 = (n - y) / d; n       ans2 += ((n - x) / d) + 1; n     } n     ans = min(ans, ans2); n     ans = min(ans, ans1); n   } n   if (ans == INT_MAX) ans = -1; n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, mn = INT_MAX, ans = INT_MAX; n   cin >> n; n   map<int, int> mp; n   map<int, int> mp_ans; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (mp[a] == 0) n       mp[a] = i; n     else { n       if (a <= mn) { n         mn = a; n         if (mp_ans[a] != 0) { n           ans = min(mp_ans[a], i - mp[a]); n           mp_ans[a] = ans; n         } else { n           mp_ans[a] = i - mp[a]; n         } n       } n       mp[a] = i; n     } n   } n   cout << mp_ans[mn]; n } n


#include <bits/stdc++.h> n using namespace std; n long long pow(int a, int b, long long MOD) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y); n       if (x > MOD) x %= MOD; n     } n     y = (y * y); n     if (y > MOD) y %= MOD; n     b /= 2; n   } n   return x; n } n long long InverseEuler(int n, long long MOD) { return pow(n, MOD - 2, MOD); } n int hcf(int n1, int n2) { n   if (n2 != 0) n     return hcf(n2, n1 % n2); n   else n     return n1; n } n int p[100090] = {}; n void prime() { n   for (int i = 2; i * i <= 100009; i++) n     if (p[i] == 0) n       for (int j = i + i; j <= 100009; j += i) p[j] = 1; n   p[0] = 1; n   p[1] = 1; n } n int main() { n   int a[100002]; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int b[100002], c[100002], f = -1; n   for (int i = 1; i < n; i++) { n     b[i] = abs(a[i] - a[i - 1]) * f; n     f *= -1; n   } n   f = -1; n   for (int i = 1; i < n; i++) { n     c[i] = b[i] * -1; n   } n   long long sum = 0, mx = INT_MIN, k = 1; n   for (int i = 1; i < n; i++) { n     sum += b[i]; n     if (sum < 0) sum = 0; n     mx = max(mx, sum); n   } n   sum = 0; n   long long mx1 = INT_MIN, j = 1; n   for (int i = 1; i < n; i++) { n     sum += c[i]; n     if (sum < 0) sum = 0; n     mx1 = max(mx1, sum); n   } n   cout << max(mx, mx1); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int fact(int n) { n   long long ans = n; n   for (int i = n - 1; i > 0; --i) { n     ans = (ans * i) % MOD; n   } n   return ans; n } n int pow2(int n) { n   long long ans = 1, b = 2; n   while (n > 0) { n     if (n % 2) { n       ans = (ans * b) % MOD; n     } n     b = (b * b) % MOD; n     n /= 2; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   cout << (fact(n) - pow2(n - 1) + MOD) % MOD; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10; n vector<int> v[maxn]; n int dp[maxn]; n int a[10][maxn]; n int main() { n   int n, K; n   cin >> n >> K; n   for (int i = 0; i < K; i++) { n     for (int j = 0; j < n; j++) scanf( %d , &a[i][j]), v[a[i][j]].push_back(j); n   } n   memset(dp, 0, sizeof(dp)); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < i; j++) { n       int p = 0; n       for (int k = 0; k < K; k++) n         if (v[a[0][j]][k] > v[a[0][i]][k]) { n           p = 1; n           break; n         } n       if (p == 0) dp[i] = max(dp[i], dp[j]); n     } n     dp[i]++; n     ans = max(ans, dp[i]); n   } n   printf( %d n , ans); n   return 0; n } n


n  n  n #include <iostream> n #include <cmath> n #include <vector> n #include <algorithm> n #include <set> n #include <cstdio> n #include <map> n #include <queue> n #include <ctime> n #include <iomanip> n #include <stack> n #include <assert.h> n #include <unordered_map> n  n using namespace std; n  n #define F first n #define ll long long n #define S second n #define ld long double n #define all(x) x.begin(), x.end() n #define rep(i, n) for (int i = 0; i < (int)n; ++i) n #define PI acos(-1) n  n ll a, b, c, d, e, fl, maxn, cnt, k, s; n vector <ll> ar; n  n  n  n int main() n { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     ll t = 1; n     cin >> t; n     while(t--) n     { n         cin >> a; n         ll last_time = -1; n         ll last_pos = 0; n         ll nap = 0; n         ll pos = 0; n         ll ans = 0; n         ll lst = 0, lsp = 0; n         queue <pair <ll, ll> > q; n         rep(i, a + 1) n         { n             ll p, p1; n             if (i != a) n                 cin >> p >> p1; n             else n                 p = (ll)1e15; n             if (!q.empty()) n             { n                 pair <ll, ll> pp = q.front(); n                 q.pop(); n                 ll st = last_pos + nap * (pp.F - last_time); n                 ll fin = last_pos + nap * (min(p, lst) - last_time); n                 //cout << i <<     << st <<     << fin <<     << pp.S <<   n ; n                 if (min(st, fin) <= pp.S && max(st, fin) >= pp.S) n                     ++ans; n             } n             if (i == a) n                 continue; n             if (p >= lst) n             { n                 last_time = p; n                 last_pos = lsp; n                 lsp = p1; n                 lst = abs(last_pos - p1) + p; n                 if (last_pos > p1) n                     nap = -1; n                 else n                     nap = 1; n             } n             q.push({p, p1}); n         } n         cout << ans <<   n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1005], b[1005]; n int main() { n   int n, l, r; n   cin >> n; n   long long bans = 1, aans = 1, mx = 0; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int l = 0; l < n; l++) { n     for (int r = l; r < n; r++) { n       aans = 0; n       bans = 0; n       for (int i = l; i <= r; i++) { n         aans |= a[i]; n         bans |= b[i]; n         mx = max(mx, aans + bans); n       } n     } n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, col[111111], used[111111], size; n vector<int> g[111111], L, R, mat[111111]; n void paint(int id, int c) { n   col[id] = c; n   c == 1 ? L.push_back(id) : R.push_back(id); n   for (vector<int>::iterator ii = g[id].begin(); ii != g[id].end(); ii++) n     if (!col[*ii]) paint(*ii, c ^ 3); n } n void construct() { n   int now = ++size, cnt = 0; n   for (vector<int>::iterator ii = L.begin(); ii != L.end(); ii++) { n     if (used[*ii]) continue; n     used[*ii] = now; n     cnt++; n     if (cnt == 3) { n       cnt = 0; n       now = ++size; n     } n   } n   for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++) { n     if (used[*ii]) continue; n     used[*ii] = now; n     cnt++; n     if (cnt == 3) { n       cnt = 0; n       now = ++size; n     } n   } n } n void print() { n   printf( YES n ); n   for (int i = 1; i <= n; i++) printf( %d  , used[i]); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     if (!col[i]) paint(i, 1); n   if (L.size() % 3 == 0 && R.size() % 3 == 0) { n     construct(); n     print(); n     return 0; n   } else { n     if (L.size() % 3 == 2 && R.size() % 3 == 1) L.swap(R); n     sort(L.begin(), L.end()); n     sort(R.begin(), R.end()); n     for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); n     for (vector<int>::iterator ii = L.begin(); ii != L.end(); ii++) n       if (R.size() - g[*ii].size() > 1) { n         int cnt = 0; n         used[*ii] = 1; n         vector<int>::iterator pp = g[*ii].begin(); n         for (vector<int>::iterator jj = R.begin(); jj != R.end() && cnt < 2; n              jj++) { n           while (pp != g[*ii].end() && *jj > *pp) pp++; n           if (pp != g[*ii].end() && *pp == *jj) continue; n           used[*jj] = 1; n           cnt++; n         } n         size = 1; n         construct(); n         print(); n         return 0; n       } n     for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++) n       if (L.size() - g[*ii].size() > 1) { n         int cnt = 0; n         vector<int>::iterator pp = g[*ii].begin(); n         for (vector<int>::iterator jj = L.begin(); jj != L.end() && cnt < 2; n              jj++) { n           while (pp != g[*ii].end() && *jj > *pp) pp++; n           if (pp != g[*ii].end() && *pp == *jj) continue; n           cnt++; n           mat[*ii].push_back(*jj); n         } n       } n     for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++) n       if (mat[*ii].size() > 1) n         for (vector<int>::iterator jj = ii + 1; jj != R.end(); jj++) n           if (mat[*jj].size() > 1) { n             used[*ii] = used[mat[*ii][0]] = used[mat[*ii][1]] = 1; n             used[*jj] = used[mat[*jj][0]] = used[mat[*jj][1]] = 2; n             size = 2; n             construct(); n             print(); n             return 0; n           } n   } n   printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long m, sum, p; n   cin >> m; n   for (int i = 5; i < 400006; i++) { n     p = 5, sum = 0; n     while (i / p) sum += i / p, p *= 5; n     if (sum == m) { n       cout <<  5 n  n            << i <<     << i + 1 <<     << i + 2 <<     << i + 3 <<     << i + 4; n       return 0; n     } n   } n   cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n using ulint = unsigned long long; n const int N = 1500 + 1; n int a[N]; n int bit[N]; n int back[N]; n int lsb(int x) { return x & -x; } n int query(int r) { n   int res = 0; n   for (; r > 0; r -= lsb(r)) { n     res = max(res, bit[r]); n   } n   return res; n } n void update(int r, int k, int id) { n   for (; r < N; r += lsb(r)) { n     if (k > bit[r]) { n       bit[r] = k; n       back[r] = id; n     } n   } n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] += a[i - 1]; n   } n   map<int, vector<pair<int, int>>> sums; n   for (int l = 1; l <= n; l++) { n     for (int r = l; r <= n; r++) { n       sums[a[r] - a[l - 1]].emplace_back(l, r); n     } n   } n   int best = -1, best_sum = -1; n   for (auto &sum : sums) { n     fill(begin(bit), end(bit), 0); n     auto &vec = sum.second; n     for (int i = 0; i < vec.size(); i++) { n       auto [l, r] = vec[i]; n       update(r, query(l - 1) + 1, i); n     } n     if (query(n) > best) { n       best = query(n); n       best_sum = sum.first; n     } n   } n   cout << best <<   n ; n   fill(begin(bit), end(bit), 0); n   fill(begin(back), end(back), -1); n   auto &vec = sums[best_sum]; n   for (int i = 0; i < vec.size(); i++) { n     auto [l, r] = vec[i]; n     int k = query(l - 1) + 1; n     update(r, k, i); n     if (k == best) { n       l = n + 1; n       for (int j = r; j >= 0; j--) { n         if (best > 0 && bit[j] == best && vec[back[j]].second < l) { n           cout << vec[back[j]].first <<     << vec[back[j]].second <<   n ; n           best--; n           l = vec[back[j]].first; n         } n       } n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int ma[10][10]; n bool vis[20]; n int isok[20][20], tmp[20][20]; n int n, m, ok = 0; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n void solve() { n   puts( YES ); n   if (n > 4) { n     for (int i = 1; i <= m; i++) { n       if (i % 2) { n         for (int j = 1; j <= n; j += 2) printf( %d  , (i - 1) * n + j); n         for (int j = 2; j <= n; j += 2) printf( %d  , (i - 1) * n + j); n       } else { n         for (int j = 2; j <= n; j += 2) printf( %d  , (i - 1) * n + j); n         for (int j = 1; j <= n; j += 2) printf( %d  , (i - 1) * n + j); n       } n       puts(  ); n     } n   } else { n     int s1 = 1; n     int s2 = 2; n     for (int i = 1; i <= m; i++) { n       for (int j = 1; j <= n; j++) { n         if (j % 2) { n           if (s1 > m) s1 = 2; n           printf( %d  , (s1 - 1) * n + j); n         } else { n           if (s2 > m) s2 = 1; n           printf( %d  , (s2 - 1) * n + j); n         } n       } n       s1 += 2; n       s2 += 2; n       puts(  ); n     } n   } n } n void init() { n   for (int i = 1; i < (m + 1); ++i) n     for (int j = 1; j < (n + 1); ++j) tmp[i][j] = (i - 1) * n + j; n   for (int i = 1; i < (n * m + 1); ++i) n     for (int j = 1; j < (m * n + 1); ++j) isok[i][j] = 1; n   for (int i = 1; i < (m + 1); ++i) n     for (int j = 1; j < (n + 1); ++j) { n       for (int k = 0; k < (4); ++k) { n         int xx = i + dx[k]; n         int yy = j + dy[k]; n         if (xx < 1 || xx > m || yy < 1 || yy > n) n           continue; n         else n           isok[tmp[xx][yy]][tmp[i][j]] = isok[tmp[i][j]][tmp[xx][yy]] = 0; n       } n     } n } n void dfs(int dep) { n   if (dep == n * m || ok) { n     ok = 1; n     return; n   } else { n     for (int i = 1; i < (n * m + 1); ++i) { n       int flag = 1; n       if (vis[i]) n         continue; n       else { n         int flag = 1; n         ++dep; n         int row = (dep - 1) / n + 1; n         int col = (dep - 1) % n + 1; n         if (row > 1) { n           int nxt1 = ma[row - 1][col]; n           if (!isok[nxt1][i]) flag = 0; n         } n         if (col > 1) { n           int nxt2 = ma[row][col - 1]; n           if (!isok[nxt2][i]) flag = 0; n         } n         if (flag == 0) { n           dep--; n           continue; n         } n         ma[row][col] = i; n         vis[i] = 1; n         dfs(dep); n         if (ok) return; n         ma[row][col] = 0; n         vis[i] = 0; n         --dep; n       } n     } n   } n } n int main() { n   scanf( %d%d , &m, &n); n   if (max(n, m) > 4) n     solve(); n   else { n     init(); n     for (int i = 1; i < (n * m + 1); ++i) n       if (!ok) { n         memset(vis, 0, sizeof(vis)); n         memset(ma, 0, sizeof(ma)); n         vis[i] = 1; n         ma[1][1] = i; n         dfs(1); n       } n     if (!ok) n       printf( NO ); n     else { n       puts( YES ); n       for (int i = 1; i < (m + 1); ++i) { n         for (int j = 1; j < (n + 1); ++j) printf( %d  , ma[i][j]); n         puts(  ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   for (int i = 0; i < 10; i++) { n     cout << i << endl; n     getline(cin, s); n     if (s ==  great  || s ==  cool  || s ==  not bad  || n         s ==  don t touch me  || s ==  don t think so ) { n       cout <<  normal n ; n       return 0; n     } else if (s ==  don t even  || s ==  are you serious  || s ==  no way  || n                s ==  worse  || s ==  terrible  || s ==  go die in a hole ) { n       cout <<  grumpy n ; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, pair<long long, long long> > mp; n pair<long long, long long> dfs(long long x) { n   if (x == 0) return make_pair(0, 0); n   if (mp.count(x)) return mp[x]; n   long long t = pow(x + 0.5, 1.0 / 3); n   pair<long long, long long> res = dfs(t * t * t - 1); n   pair<long long, long long> tmp = dfs(x - t * t * t); n   res = max(res, make_pair(tmp.first + 1, tmp.second + t * t * t)); n   mp[x] = res; n   return res; n } n int main() { n   long long m; n   scanf( %lld , &m); n   pair<long long, long long> ans = dfs(m); n   printf( %lld %lld n , ans.first, ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, m, t; n long long sum(int l, int r) { n   long long sl = a + (l - 1) * b; n   long long sr = a + (r - 1) * b; n   return (sl + sr) * (r - l + 1) / 2; n } n bool chk(int l, int x) { n   return a + 1LL * (x - 1) * b <= 1LL * t && sum(l, x) <= 1LL * m * t; n } n int main(void) { n   int q; n   cin >> a >> b >> q; n   while (q--) { n     int l; n     cin >> l >> t >> m; n     int lo = l - 1, hi = 1000000000; n     while (lo != hi) { n       int mi = (lo + hi + 1) / 2; n       if (chk(l, mi)) n         lo = mi; n       else n         hi = mi - 1; n     } n     if (lo < l) n       cout <<  -1 n ; n     else n       cout << lo <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n using namespace std; n template <typename T1, typename T2> n ostream& operator<<(ostream& os, pair<T1, T2> p) { n   cout <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T>& vec) { n   os <<  { ; n   for (int i = 0; i < vec.size(); i++) n     os << vec[i] << (i + 1 == vec.size() ?    :  ,  ); n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, set<T>& st) { n   cout <<  { ; n   for (auto itr = st.begin(); itr != st.end(); itr++) n     cout << *itr << (next(itr) != st.end() ?  ,   :   ); n   cout <<  } ; n   return os; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& os, map<T1, T2> mp) { n   cout <<  { ; n   for (auto itr = mp.begin(); itr != mp.end(); itr++) n     cout <<  (  << (itr->first) <<  ,   << (itr->second) <<  )  n          << (next(itr) != mp.end() ?  ,  :   ); n   cout <<  } ; n   return os; n } n void dump_func() { cout << endl; } n template <class Head, class... Tail> n void dump_func(Head&& head, Tail&&... tail) { n   cout << head; n   if (sizeof...(Tail) == 0) { n     cout <<    ; n   } else { n     cout <<  ,  ; n   } n   dump_func(std::move(tail)...); n } n template <class T> n inline bool chmax(T& a, T const& b) { n   if (a >= b) return false; n   a = b; n   return true; n } n template <class T> n inline bool chmin(T& a, T const& b) { n   if (a <= b) return false; n   a = b; n   return true; n } n void _main(); n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   _main(); n   return 0; n } n using ll = long long; n using P = pair<int, int>; n using Pl = pair<ll, ll>; n using vi = vector<int>; n using vvi = vector<vi>; n using vl = vector<ll>; n using vvl = vector<vl>; n const int mod = 1e9 + 7, INF = 1 << 29; n const double EPS = 1e-5, PI = 3.1415926535897932384626; n const ll lmod = 1e9 + 7, LINF = 1LL << 59; n template <int MOD> n struct ModInt { n   static const int Mod = MOD; n   unsigned x; n   ModInt() : x(0) {} n   ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } n   ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } n   int get() const { return (int)x; } n   ModInt& operator+=(ModInt that) { n     if ((x += that.x) >= MOD) x -= MOD; n     return *this; n   } n   ModInt& operator-=(ModInt that) { n     if ((x += MOD - that.x) >= MOD) x -= MOD; n     return *this; n   } n   ModInt& operator*=(ModInt that) { n     x = (unsigned long long)x * that.x % MOD; n     return *this; n   } n   ModInt& operator/=(ModInt that) { return *this *= that.inverse(); } n   ModInt operator+(ModInt that) const { return ModInt(*this) += that; } n   ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } n   ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } n   ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } n   ModInt inverse() const { n     long long a = x, b = MOD, u = 1, v = 0; n     while (b) { n       long long t = a / b; n       a -= t * b; n       std::swap(a, b); n       u -= t * v; n       std::swap(u, v); n     } n     return ModInt(u); n   } n   bool operator==(ModInt that) const { return x == that.x; } n   bool operator!=(ModInt that) const { return x != that.x; } n   ModInt operator-() const { n     ModInt t; n     t.x = x == 0 ? 0 : Mod - x; n     return t; n   } n }; n template <int MOD> n ostream& operator<<(ostream& st, const ModInt<MOD> a) { n   st << a.get(); n   return st; n }; n template <int MOD> n ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { n   ModInt<MOD> r = 1; n   while (k) { n     if (k & 1) r *= a; n     a *= a; n     k >>= 1; n   } n   return r; n } n template <typename T, int FAC_MAX> n struct Comb { n   vector<T> fac, ifac; n   Comb() { n     fac.resize(FAC_MAX, 1); n     ifac.resize(FAC_MAX, 1); n     for (int i = 1; i < FAC_MAX; i++) fac[i] = fac[i - 1] * i; n     ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1]; n     for (int i = FAC_MAX - 2; i >= 1; i--) ifac[i] = ifac[i + 1] * T(i + 1); n   } n   T aPb(int a, int b) { n     if (b < 0 || a < b) return T(0); n     return fac[a] * ifac[a - b]; n   } n   T aCb(int a, int b) { n     if (b < 0 || a < b) return T(0); n     return fac[a] * ifac[a - b] * ifac[b]; n   } n   T nHk(int n, int k) { n     if (n == 0 && k == 0) return T(1); n     if (n <= 0 || k < 0) return 0; n     return aCb(n + k - 1, k); n   } n }; n using mint = ModInt<998244353>; n using vm = vector<mint>; n void _main() { n   int N; n   cin >> N; n   int M; n   cin >> M; n   vvl g(N); n   for (int i = 0; i < M; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     g[a].push_back(b); n   } n   vl dist(N, LINF); n   for (int i = 0; i < N; i++) { n     for (auto u : g[i]) chmin(dist[i], (u - i + N) % N); n     if (g[i].size() != 0) dist[i] += ll(g[i].size() - 1) * ll(N); n     if (dist[i] == LINF) dist[i] = 0; n   } n   for (int i = 0; i < N; i++) { n     ll ans = 0; n     for (int j = 0; j < N; j++) n       if (g[j].size() != 0) chmax(ans, dist[j] + ll((j - i + N) % N)); n     cout << ans << (i == N - 1 ?   n  :    ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void PAUSE() { n   char tmp; n   cin >> tmp; n } n int M[100001]; n int f[100001]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i <= 100000; i++) { n     M[i] = -1; n   } n   int x, k; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x, &k); n     if (x - M[k] > 1) { n       cout <<  NO ; n       return 0; n     } n     M[k] = x > M[k] ? x : M[k]; n     f[k] = 1; n   } n   cout <<  YES ; n   PAUSE(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long ans = 0; n   long long a, b, c, s; n   cin >> s >> a >> b >> c; n   ans = s / c + (s / c / a) * b; n   cout << ans << endl; n } n int main() { n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[256], n, k, p; n   cin >> n >> k; n   memset(a, -1, sizeof(a)); n   for (int i = 0; i < n; i++) { n     cin >> p; n     if (a[p] == -1) { n       for (int j = max(0, p - k + 1); j <= p; j++) n         if (a[j] == -1 || a[j] == j) { n           for (int x = j; x <= p; x++) a[x] = j; n           break; n         } n     } n     cout << a[p] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const int mod = inf + 7; n const double eps = 1e-9; n int a[100][100]; n int n, x, y; n bool u[400], u1[400]; n vector<int> v; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n * n; i++) { n     cin >> x >> y; n     if (!u[x] && !u1[y]) { n       u[x] = 1; n       u1[y] = 1; n       v.push_back(i); n     } n   } n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, k; n   cin >> n >> k; n   long long int a[n + 2]; n   a[0] = 0; n   pair<long long int, long long int> min; n   min.first = INT_MAX; n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] += a[i - 1]; n     if (i >= k) { n       a[i] -= a[i - k]; n       if (a[i] < min.first) min.first = a[i], min.second = i - k + 1; n       a[i] += a[i - k]; n     } n   } n   cout << min.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1.01e9; n const double eps = 1e-9; n const int N = 22; n double RES = 0; n double rf, re, rs; n double df, de; n int cnt = 0; n int a[N]; n int ac = 0; n int b[N]; n int bc = 0; n int nf, ne, ns; n double e[N]; n double FI[200]; n double EN[200]; n double FIRE(int p) { n   double res = rf * 2; n   for (int i = 0; (i) < (bc); ++i) res += FI[abs(b[i] - p)]; n   res *= df; n   return res; n } n double ENERGY(int p) { n   double res = re * 2; n   for (int i = 0; (i) < (bc); ++i) res += EN[abs(b[i] - p)]; n   res *= de; n   return res; n } n void solve() { n   double val = 0; n   for (int i = 0; (i) < (ac); ++i) { n     double x = FIRE(a[i]); n     double y = ENERGY(a[i]); n     e[i] = x - y; n     val += y; n   } n   sort(e, e + ac); n   reverse(e, e + ac); n   for (int i = 0; (i) < (nf); ++i) val += e[i]; n   RES = max(RES, val); n } n void go(int x, int y, int pos, int cnt1 = 0, int cnt2 = 0) { n   if (x == 0 && y == 0) { n     cnt++; n     solve(); n     return; n   } n   if (x >= 1 && cnt1 < 2) { n     a[ac++] = pos; n     go(x - 1, y, pos + 1, cnt1 + 1, cnt2); n     ac--; n   } n   if (x >= 2) { n     a[ac++] = pos; n     a[ac++] = pos; n     go(x - 2, y, pos + 1, cnt1, cnt2); n     ac--; n     ac--; n   } n   if (y >= 1 && cnt2 < 1) { n     b[bc++] = pos; n     go(x, y - 1, pos + 1, cnt1, cnt2 + 1); n     bc--; n   } n   if (y >= 2) { n     b[bc++] = pos; n     b[bc++] = pos; n     go(x, y - 2, pos + 1, cnt1, cnt2); n     bc--; n     bc--; n   } n   if (x >= 1 && y >= 1) { n     a[ac++] = pos; n     b[bc++] = pos; n     go(x - 1, y - 1, pos + 1, cnt1, cnt2); n     ac--; n     bc--; n   } n } n int main() { n   scanf( %d%d%d , &nf, &ne, &ns); n   scanf( %lf%lf%lf , &rf, &re, &rs); n   scanf( %lf%lf , &df, &de); n   rf = sqrt(max(0., rf * rf - 1)); n   re = sqrt(max(0., re * re - 1)); n   rs = sqrt(max(0., rs * rs - 1)); n   for (int i = 0; i < 100; ++i) { n     double l1 = -rf; n     double r1 = rf; n     double l2 = i - rs; n     double r2 = i + rs; n     FI[i] = max(0., min(r1, r2) - max(l1, l2)); n   } n   for (int i = 0; i < 100; ++i) { n     double l1 = -re; n     double r1 = re; n     double l2 = i - rs; n     double r2 = i + rs; n     EN[i] = max(0., min(r1, r2) - max(l1, l2)); n   } n   go(nf + ne, ns, 0); n   fprintf(stderr,  %d n , cnt), fflush(stderr); n   printf( %.10f n , RES); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, l, n, m, last, res; n long long a[100009]; n queue<long long> q; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   q.push(1); n   for (i = 2; i <= n; i++) { n     if (!q.empty() && a[q.front()] < a[i]) { n       res++; n       q.pop(); n     } n     q.push(i); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<int, int> > s; n std::vector<int> vv; n std::vector<std::vector<int> > gr; n std::vector<int> vis; n int k; n pair<int, int> dfs(int u, int l) { n   vis[u] = l; n   vv.emplace_back(u); n   for (int v : gr[u]) { n     if ((vis[v]) && (vis[u] - vis[v]) >= k) { n       return {vis[v], vis[u]}; n     } else if (!vis[v]) { n       return dfs(v, l + 1); n     } n   } n   return {-1, -1}; n } n int32_t main() { n   int n; n   cin >> n; n   int m; n   cin >> m; n   cin >> k; n   gr.resize(n); n   for (int i = 0; i < m; ++i) { n     int u; n     cin >> u; n     u--; n     int v; n     cin >> v; n     v--; n     gr[u].emplace_back(v); n     gr[v].emplace_back(u); n   } n   vis.resize(n, 0); n   auto pp = dfs(0, 1); n   cout << pp.second - pp.first + 1 <<   n ; n   for (int i = pp.first - 1; i < pp.second; ++i) { n     cout << vv[i] + 1 <<    ; n   } n } n