#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:67108864 ) n const int MOD = 1000000007; n const long long INF = 3000000000000000001; n const double EPS = 1e-6; n const int HASH_POW = 29; n const double PI = acos(-1.0); n using namespace std; n double workTime() { return double(clock()) / CLOCKS_PER_SEC; } n void my_return(int code) { exit(code); } n int cnt[1000000]; n void add(string s) { n   reverse(s.begin(), s.end()); n   int len = s.length(); n   int ptr = 0; n   for (int i = 0; i < 18; ++i) { n     int bit; n     if (i < len) n       bit = (s[i] -  0 ) % 2; n     else n       bit = 0; n     ptr += bit * (1 << i); n   } n   ++cnt[ptr]; n } n void destroy(string s) { n   reverse(s.begin(), s.end()); n   int len = s.length(); n   int ptr = 0; n   for (int i = 0; i < 18; ++i) { n     int bit; n     if (i < len) n       bit = (s[i] -  0 ) % 2; n     else n       bit = 0; n     ptr += bit * (1 << i); n   } n   --cnt[ptr]; n } n int count(string s) { n   reverse(s.begin(), s.end()); n   int len = s.length(); n   int ptr = 0; n   for (int i = 0; i < 18; ++i) { n     int bit; n     if (i < len) n       bit = (s[i] -  0 ) % 2; n     else n       bit = 0; n     ptr += bit * (1 << i); n   } n   return cnt[ptr]; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   mt19937 mt_rand(time(0)); n   int q; n   cin >> q; n   while (q--) { n     char c; n     string v; n     cin >> c >> v; n     if (c ==  + ) n       add(v); n     else if (c ==  - ) n       destroy(v); n     else if (c ==  ? ) n       cout << count(v) << endl; n     else n       return 1; n   } n   my_return(0); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, i, j, a[1000000], b[1000000], x, res = 1; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     cin >> x; n     b[x] = i; n   } n   while (b[a[res]] > b[a[res - 1]]) { n     res++; n   } n   res = n - res; n   cout << res; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n struct Dinic { n   struct Edge { n     long long flow; n     int to, next; n   }; n   vector<Edge> edges; n   vector<int> adia, at, dist; n   int S, D; n   void add_Edge(int from, int to, long long cap) { n     edges.push_back({cap, to, adia[from]}); n     adia[from] = edges.size() - 1; n     edges.push_back({0, from, adia[to]}); n     adia[to] = edges.size() - 1; n   } n   bool bfs() { n     queue<int> q; n     fill(dist.begin(), dist.end(), 1e9); n     dist[S] = 0; n     q.push(S); n     while (!q.empty()) { n       int x = q.front(); n       q.pop(); n       for (int i = adia[x]; i != -1; i = edges[i].next) { n         if (dist[edges[i].to] > dist[x] + 1 && edges[i].flow) { n           dist[edges[i].to] = 1 + dist[x]; n           q.push(edges[i].to); n         } n       } n     } n     return dist[D] < 1e9; n   } n   long long dfs(int nod, long long fmax) { n     if (nod == D) return fmax; n     while (at[nod] != -1) { n       Edge& e = edges[at[nod]]; n       long long f; n       if (dist[e.to] == dist[nod] + 1 && e.flow && n           (f = dfs(e.to, min(fmax, e.flow)))) { n         e.flow -= f; n         edges[at[nod] ^ 1].flow += f; n         return f; n       } n       at[nod] = edges[at[nod]].next; n     } n     return 0; n   } n   long long GetFlow() { n     long long f = 0; n     while (bfs()) { n       at = adia; n       while (long long x = dfs(S, 1e18)) f += x; n     } n     return f; n   } n   Dinic(int n = 0, int s = 0, int d = 0) { n     S = s, D = d; n     at = dist = adia = vector<int>(n + 1, -1); n   } n }; n const int NMAX = 110; n int dmin[NMAX][NMAX]; n struct Object { n   int cost, nod, power, id, fuel, atac; n }; n vector<Object> ships, bases; n vector<pair<int, int>> dependencies; n int n; n int best_cost[NMAX]; n void get_cost_nave() { n   fill(best_cost, best_cost + n + 1, -1); n   vector<int> nv(ships.size()); n   iota(nv.begin(), nv.end(), 0); n   sort(nv.begin(), nv.end(), n        [](int a, int b) { return ships[a].power < ships[b].power; }); n   sort(bases.begin(), bases.end(), n        [](Object a, Object b) { return a.power < b.power; }); n   int it = 0; n   for (auto i : nv) { n     while (it != bases.size() && ships[i].power >= bases[it].power) { n       best_cost[bases[it].nod] = max(best_cost[bases[it].nod], bases[it].cost); n       it++; n     } n     int best = -1; n     for (int nod = 1; nod <= n; nod++) n       if (dmin[nod][ships[i].nod] <= ships[i].fuel) n         best = max(best, best_cost[nod]); n     ships[i].atac = best; n   } n } n void read() { n   int m, a, b; n   scanf( %d%d , &n, &m); n   fill(dmin[0], dmin[n + 1], 1e9); n   for (int i = 1; i <= n; i++) dmin[i][i] = 0; n   while (m--) { n     scanf( %d%d , &a, &b); n     dmin[a][b] = dmin[b][a] = 1; n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         dmin[i][j] = min(dmin[i][j], dmin[i][k] + dmin[k][j]); n   int s, k; n   scanf( %d%d%d , &s, &b, &k); n   ships.resize(s); n   bases.resize(b); n   dependencies.resize(k); n   for (auto& i : ships) { n     scanf( %d%d%d%d , &i.nod, &i.power, &i.fuel, &i.cost); n     i.fuel = min(i.fuel, 1000000); n   } n   for (auto& i : bases) scanf( %d%d%d , &i.nod, &i.power, &i.cost); n   for (auto& i : dependencies) scanf( %d%d , &i.first, &i.second); n } n int main() { n   read(); n   get_cost_nave(); n   long long ans = 0; n   int cnt = 2, S = 1, D = 2; n   const long long inf = 1e18; n   for (auto& i : dependencies) i.first--, i.second--; n   for (int i = 0; i < ships.size(); i++) ships[i].id = ++cnt; n   Dinic flow(cnt + 1, S, D); n   for (auto i : ships) { n     if (i.atac == -1) n       flow.add_Edge(i.id, D, inf); n     else { n       int castig = i.atac - i.cost; n       if (castig > 0) { n         ans += castig; n         flow.add_Edge(S, i.id, castig); n       } else n         flow.add_Edge(i.id, D, -castig); n     } n   } n   for (auto i : dependencies) n     flow.add_Edge(ships[i.first].id, ships[i.second].id, inf); n   ans -= flow.GetFlow(); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, cen; n   long long n, num; n   while (scanf( %d%I64d , &h, &n) != EOF) { n     num = (long long)pow(2.0, h) - 1 + n; n     int flag; n     long long ans = 0; n     if (num % 2 == 0) n       flag = 0; n     else n       flag = 1; n     cen = h - 1; n     while (1) { n       ans++; n       num /= (long long)2; n       if (num % 2 == 0) { n         if (flag == 0) ans += (long long)pow(2.0, h - cen) - (long long)1.0; n         flag = 0; n       } else { n         if (flag == 1) ans += (long long)pow(2.0, h - cen) - (long long)1.0; n         flag = 1; n       } n       if (num == 1) break; n       cen--; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int used[110]; n int v[110][110]; n vector<int> ans; n int main() { n   int n, x; n   memset(used, 0, sizeof(used)); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> x; n       if (x == 1) { n         used[i] = 1; n       } else if (x == 2) { n         used[j] = 1; n       } else if (x == 3) { n         used[i] = used[j] = 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       ans.push_back(i); n     } n   } n   cout << ans.size() << endl; n   if (!ans.size()) return 0; n   bool cond = true; n   for (int i = 0; i < (int)ans.size(); i++) { n     if (!cond) cout <<    ; n     cout << ans[i]; n     cond = false; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long P = 998244353; n double PI = 3.141592653589793; n const int sqr = 500; n const int arrn = 2e5 + 5; n const int arrn2 = 1e3 + 5; n long long inv[arrn] = {}; n long long fac[arrn] = {}; n void prefinv(int n, int p) { n   inv[0] = 1; n   inv[1] = 1; n   fac[1] = 1; n   fac[0] = 1; n   for (int i = 2; i <= n; i++) { n     fac[i] = fac[i - 1] * i; n     fac[i] %= p; n     inv[i] = (p - (((p / i) * inv[p % i]) % p)) % p; n   } n   for (int i = 2; i <= n; i++) { n     inv[i] = inv[i] * inv[i - 1]; n     inv[i] %= p; n     if (inv[i] < 0) inv[i] += p; n   } n } n long long comb(int n, int r, int mod) { n   if (n < r) n     return 0; n   else { n     long long temp = fac[n]; n     temp *= inv[r]; n     temp %= mod; n     temp *= inv[n - r]; n     temp %= mod; n     return temp; n   } n } n long long binpow(long long a, long long n, long long p) { n   long long ret = 1; n   while (n) { n     if (n & 1) ret *= a; n     ret %= p; n     a *= a; n     a %= p; n     n /= 2; n   } n   return ret; n } n void solve() { n   prefinv(arrn - 1, P); n   int n; n   long long k; n   cin >> n >> k; n   if (k > n - 1) { n     cout << 0; n     return; n   } n   long long out = 0; n   for (int i = k; i < n; i++) { n     if ((i - k) & 1) { n       out = (out - ((comb(n - k, i - k, P) * binpow(n - i, n, P)) % P)) % P; n     } else { n       out = (out + ((comb(n - k, i - k, P) * binpow(n - i, n, P)) % P)) % P; n     } n   } n   out = (out * comb(n, k, P)) % P; n   if (k > 0) out *= 2; n   out %= P; n   cout << (out + P) % P; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int T = 1; n   for (int c = 0; c < T; c++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int dif; n   cin >> s >> dif; n   set<char> st; n   for (int i = (0); i < s.length(); i++) st.insert(s[i]); n   if (s.length() < dif) { n     cout <<  impossible  << endl; n   } else if (st.size() >= dif) n     cout << 0; n   else n     cout << dif - st.size() << endl; 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 a, b; n     cin >> a >> b; n     int a1 = 2 * min(a, b); n     int a2 = max(a, b); n     cout << pow(max(a1, a2), 2) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, viz[1005], n; n int sol[100005], nrsol; n int main(void) { n   cin >> n; n   for (i = 2; i <= n; ++i) n     if (viz[i] == 0) { n       int nrp = i; n       int put = nrp; n       while (put <= n) { n         sol[++nrsol] = put; n         put *= nrp; n       } n       for (j = 2; j * i <= n; ++j) viz[i * j] = 1; n     } n   cout << nrsol <<   n ; n   for (i = 1; i <= nrsol; ++i) cout << sol[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n long long a00, a01, a10, a11; n long long newton(long long n) { return n * (n - 1) / 2; } n long long sqrNewton(long long x) { n   long long n = 0; n   while (newton(n) < x) n++; n   return newton(n) == x ? n : -1; n } n int W[MAXN]; n bool policz(long long zero, long long one) { n   long long ile = zero * one; n   long long n = zero + one; n   if (zero < 0 || one < 0 || (newton(n) != a00 + a01 + a10 + a11) || ile < a01) n     return false; n   int suf = 0; n   while (one > 0 && ile - one >= a01) { n     suf++; n     ile -= one; n     if (suf > zero) return false; n   } n   for (int i = 1; i <= n; i++) W[i] = 1; n   for (int i = 1; i <= zero - suf; i++) W[i] = 0; n   for (int i = 1; i <= suf; i++) W[n - i + 1] = 0; n   long long p = ile - a01; n   if (p > 0) { n     W[zero - suf + p] = 0; n     W[zero - suf] = 1; n   } n   for (int i = 1; i <= n; i++) cout << W[i]; n   return true; n } n int main() { n   cin >> a00 >> a01 >> a10 >> a11; n   if (a00 == 0 && a01 == 0 && a10 == 0 && a11 == 0) { n     cout <<  0 ; n     return 0; n   } n   vector<long long> zero; n   vector<long long> one; n   zero.push_back(sqrNewton(a00)); n   one.push_back(sqrNewton(a11)); n   if (a00 == 0) zero.push_back(1); n   if (a11 == 0) one.push_back(1); n   int d1 = zero.size(); n   int d2 = one.size(); n   for (int i = 0; i < d1; i++) n     for (int j = 0; j < d2; j++) n       if (policz(zero[i], one[j])) return 0; n   cout <<  Impossible ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e6; n const long long LINF = 8e18; n const int MOD = 1e9 + 7; n const long double EPS = 1e-9; n int n; n vector<long long> a; n struct triplet { n   int ti, i, j; n }; n vector<struct triplet> s; n int cnt[INF]; n int main() { n   scanf( %d , &n); n   ; n   if (n == 1) { n     printf( 0 ); n     return 0; n   } n   for (int i = 0; i < INF; i++) cnt[i] = 0; n   for (int i = 0; i < n; i++) { n     long long X; n     scanf( %lld , &X); n     a.push_back(X); n     cnt[X]++; n   } n   int mx = -1, mx_i = 0; n   for (int i = 0; i < n; i++) mx = max(mx, cnt[a[i]]); n   for (int i = 0; i < n; i++) n     if (mx == cnt[a[i]]) mx_i = i; n   for (int i = mx_i + 1; i < n; i++) { n     if (a[i] < a[mx_i]) s.push_back({1, i, i - 1}); n     if (a[i] > a[mx_i]) s.push_back({2, i, i - 1}); n   } n   for (int i = mx_i - 1; i > -1; i = i - 1) { n     if (a[i] < a[mx_i]) s.push_back({1, i, i + 1}); n     if (a[i] > a[mx_i]) s.push_back({2, i, i + 1}); n   } n   printf( %ld , s.size()); n   for (struct triplet it : s) { n     printf(  n ); n     printf( %d %d %d , it.ti, it.i + 1, it.j + 1); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, b, i, rez, a[2013], minn[2013], maxx[2013]; n int main() { n   minn[0] = 123456890; n   cin >> n >> b; n   for (i = 1; i <= n; ++i) { n     cin >> a[i]; n     minn[i] = min(minn[i - 1], a[i]); n   } n   for (i = n; i >= 1; --i) { n     maxx[i] = max(maxx[i + 1], a[i]); n   } n   for (i = 1; i <= n; ++i) { n     rez = max(rez, b / minn[i] * maxx[i] + b % minn[i]); n   } n   cout << rez; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second < b.second) return true; n   if (a.second == b.second) return a.first < b.first; n   return false; n } n bool isprime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return false; n   return true; n } n bool isPowerOfTwo(int n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int lcm(long long int a, long long int b) { n   return (a / gcd(a, b)) * b; n } n long long int ncr(long long int n, long long int r) { n   if (r == 0) return 1; n   r = min(r, n - r); n   long long int ans = 1; n   for (int i = 1; i <= r; i++) { n     ans *= (n - i + 1); n     ans /= i; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<pair<int, int>> v; n     int x, y; n     for (int i = 0; i < m; i++) { n       cin >> x >> y; n       v.push_back(make_pair(x, y)); n     } n     int ans = 0; n     if (v[0].first != 1) { n       cout << 1 <<     << 0 <<   n ; n     } else { n       int dayno = -1; n       for (int i = 1; i < v.size(); i++) { n         if (v[i].first == v[i - 1].second) { n           ans = 2; n           dayno = v[i].first; n           int j = i + 1; n           while (j < v.size() && v[j].first == v[i].first) ans++, j++; n           break; n         } else if (v[i].first != v[i - 1].second + 1) { n           dayno = v[i - 1].second + 1; n           ans = 0; n           break; n         } n       } n       if (dayno != -1) n         cout << dayno <<     << ans <<   n ; n       else { n         if (v[v.size() - 1].second != n) n           cout << v[v.size() - 1].second + 1 <<     << 0 <<   n ; n         else n           cout <<  OK  n                <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<array<int, 2>> a(n); n   map<int, int> hhor; n   map<int, int> hver; n   set<array<int, 2>> hor[n]; n   set<array<int, 2>> ver[n]; n   int idhor = 0, idver = 0; n   for (int i = 0; i < n; i++) { n     auto &[x, y] = a[i]; n     cin >> x >> y; n     if (hhor.find(y) == hhor.end()) hhor[y] = idhor++; n     if (hver.find(x) == hver.end()) hver[x] = idver++; n     hor[hhor[y]].insert({x, i}); n     ver[hver[x]].insert({y, i}); n   } n   auto one = [&](int x, int y) -> array<int, 2> { n     array<int, 2> tmp = {-1, -1}; n     int idx = 0; n     if (hhor.find(y) == hhor.end()) return tmp; n     int id = hhor[y]; n     auto it = hor[id].lower_bound({x, -1}); n     if (it != hor[id].end()) tmp[idx++] = ((*it)[1]); n     if (it != hor[id].begin()) { n       it--; n       tmp[idx++] = ((*it)[1]); n     } n     return tmp; n   }; n   auto two = [&](int x, int y) -> array<int, 2> { n     array<int, 2> tmp = {-1, -1}; n     int idx = 0; n     if (hver.find(x) == hver.end()) return tmp; n     int id = hver[x]; n     auto it = ver[id].lower_bound({y, -1}); n     if (it != ver[id].end()) tmp[idx++] = ((*it)[1]); n     if (it != ver[id].begin()) { n       it--; n       tmp[idx++] = ((*it)[1]); n     } n     return tmp; n   }; n   vector<vector<vector<array<int, 4>>>> pre( n       n, vector<vector<array<int, 4>>>(n, vector<array<int, 4>>(2))); n   auto ini = [&](int x, int y, int i, int j, int e) { n     array<int, 4> &now = pre[i][j][e]; n     array<int, 2> tmp = one(x, y); n     now[0] = tmp[0]; n     now[1] = tmp[1]; n     tmp = two(x, y); n     now[2] = tmp[0]; n     now[3] = tmp[1]; n   }; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       auto [x1, y1] = a[i]; n       auto [x2, y2] = a[j]; n       if (x1 == x2) n         ini(x1, (y1 + y2) / 2, i, j, 0); n       else if (y1 == y2) n         ini((x1 + x2) / 2, y1, i, j, 0); n       else { n         ini(x1, y2, i, j, 0); n         ini(x2, y1, i, j, 1); n       } n     } n   } n   const long long lim = (1ll << 31); n   long long l = 0, r = lim; n   while (l < r - 1) { n     int m = (l + r) / 2; n     vector<int> v[n]; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         auto [x1, y1] = a[i]; n         auto [x2, y2] = a[j]; n         if ((x1 == x2 && abs(y1 - y2) <= m) || n             (y1 == y2 && abs(x1 - x2) <= m)) { n           v[i].push_back(j); n           v[j].push_back(i); n         } n       } n     } n     vector<int> c(n, -1); n     int now = 0; n     for (int i = 0; i < n; i++) { n       if (c[i] != -1) continue; n       c[i] = now; n       queue<int> q; n       q.push(i); n       while (!q.empty()) { n         int e = q.front(); n         q.pop(); n         for (int to : v[e]) { n           if (c[to] != -1) continue; n           c[to] = now; n           q.push(to); n         } n       } n       now++; n     } n     bool ok = (now == 1); n     if (now <= 4) { n       auto go = [&](int x, int y, int i, int j, int e) { n         vector<int> z(now, 0); n         for (int b : pre[i][j][e]) n           if (b != -1 && abs(x - a[b][0]) + abs(y - a[b][1]) <= m) z[c[b]] = 1; n         bool b = 1; n         for (int k = 0; k < now; k++) b &= z[k]; n         if (b) ok = 1; n       }; n       for (int i = 0; i < n; i++) { n         for (int j = i + 1; j < n; j++) { n           auto [x1, y1] = a[i]; n           auto [x2, y2] = a[j]; n           if (x1 == x2) n             go(x1, (y1 + y2) / 2, i, j, 0); n           else if (y1 == y2) n             go((x1 + x2) / 2, y1, i, j, 0); n           else { n             go(x1, y2, i, j, 0); n             go(x2, y1, i, j, 1); n           } n         } n       } n     } n     if (ok) n       r = m; n     else n       l = m; n   } n   if (r < lim) n     cout << r <<   n ; n   else n     cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int dp[100005], x[105], s[105]; n bool vis[100005]; n int main() { n   int n, m, minv = INF; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &x[i], &s[i]); n     minv = min(minv, x[i]); n     for (int j = max(x[i] - s[i], 1); j <= min(x[i] + s[i], m); ++j) { n       vis[j] = true; n     } n   } n   memset(dp, INF, sizeof(dp)); n   dp[0] = 0; n   for (int i = 1; i <= m; ++i) { n     if (vis[i]) n       dp[i] = min(dp[i - 1], dp[i]); n     else { n       if (i >= minv) dp[i] = min(dp[i], dp[i - 1] + 1); n       for (int j = 0; j < n; ++j) { n         if (x[j] > i) { n           int tem = x[j] - s[j] - i; n           dp[min(m, x[j] + s[j] + tem)] = n               min(dp[min(x[j] + s[j] + tem, m)], dp[i - 1] + tem); n         } n       } n     } n   } n   printf( %d n , dp[m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:16777216 ) n const int fin = 111110; n struct T { n   int cnt, val, l, r, mx, y, ans; n   T() : cnt(0), val(-1), l(fin), r(fin), mx(-1000000000), y(-1), ans(-1) {} n }; n T a[111111]; n void upd(int v) { n   a[v].cnt = 1 + a[a[v].r].cnt + a[a[v].l].cnt; n   a[v].mx = max(a[v].val, max(a[a[v].r].mx, a[a[v].l].mx)); n } n void split(int v, int &l, int &r, int key) { n   l = r = fin; n   if (v == fin) return; n   if (key && (a[v].l == -1 || a[a[v].l].cnt < key)) { n     l = v; n     split(a[v].r, a[v].r, r, key - a[a[v].l].cnt - 1); n     upd(l); n   } else { n     r = v; n     split(a[v].l, l, a[v].l, key); n     upd(r); n   } n } n int merge(int l, int r) { n   if (l == fin) return r; n   if (r == fin) return l; n   if (a[l].y < a[r].y) { n     a[l].r = merge(a[l].r, r); n     upd(l); n     return l; n   } else { n     a[r].l = merge(l, a[r].l); n     upd(r); n     return r; n   } n } n int root, m; n void push(int val) { n   a[m].y = (rand() << 16) + rand(); n   a[m].val = val; n   a[m].cnt = 1; n   a[m].mx = val; n   a[m].ans = m + 1; n   root = merge(root, m++); n } n int tot = 0; n void print(int v) { n   if (v == fin) return; n   if (a[v].l != fin) print(a[v].l); n   { n     if (tot) n       printf(   ); n     else n       tot = 1; n     printf( %d , a[v].ans); n   } n   if (a[v].r != fin) print(a[v].r); n } n int fmin(int st, int fn) { n   int r1, r2, r3, r4; n   split(root, r1, r2, st); n   split(r2, r3, r4, fn - st); n   int rez = (r3 != -1) ? (a[r3].mx) : (-1000000000); n   root = merge(r1, r3); n   root = merge(root, r4); n   return rez; n } n int n, A, C; n int find(int v, int maxim, int value, int lft) { n   if (a[a[v].r].cnt + 1 > maxim) n     return find(a[v].r, maxim, value, lft + 1 + a[a[v].l].cnt); n   else if (a[a[v].r].mx > value) n     return find(a[v].r, maxim, value, lft + 1 + a[a[v].l].cnt); n   else if (a[v].val > value) n     return lft + a[a[v].l].cnt; n   else n     return find(a[v].l, maxim - 1 - a[a[v].r].cnt, value, lft); n } n int main() { n   a[fin].cnt = 0; n   a[fin].mx = -1000000000; n   scanf( %d , &n); n   for (int(i) = 0; (i) < (n); ++(i)) { n     scanf( %d%d , &A, &C); n     if (i) { n       int high = max(i - C, 0); n       if (fmin(high, i) > A) high = find(root, C, A, 0) + 1; n       int r1, r2; n       split(root, r1, r2, high); n       root = r1; n       push(A); n       root = merge(root, r2); n     } else { n       a[0].val = A; n       a[0].ans = 1; n       a[0].cnt = 1; n       a[0].mx = A; n       root = 0; n       m = 1; n     } n   } n   print(root); n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, k, l, x = 1; n   cin >> n >> m >> k >> l; n   if (k + l > n) n     cout <<  -1 ; n   else if ((k + l) % m == 0 && (k + l) <= n) n     cout << (k + l) / m; n   else if ((k + l) % m != 0 && ((k + l) / m + 1) * m <= n) n     cout << ((k + l) / m) + 1; n   else n     cout <<  -1 ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxk = 105, maxn = 2e5 + 5, inf = 1e9; n int n, k, l[maxk], r[maxk], dp[2][maxn]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= k; ++i) scanf( %d %d , l + i, r + i); n   if (l[k] != 2 * n) { n     ++k; n     l[k] = r[k] = 2 * n; n   } n   for (int t = 0; t < maxn; ++t) dp[1][t] = inf; n   dp[1][l[1]] = 0; n   for (int j = 2; j <= k; ++j) { n     for (int t = 0; t < maxn; ++t) dp[j & 1][t] = inf; n     for (int t = l[j] - l[j - 1]; t <= l[j]; ++t) { n       dp[j & 1][t] = min(dp[j & 1][t], dp[(j - 1) & 1][t - l[j] + l[j - 1]]); n     } n     deque<pair<int, int> > minq; n     for (int s = 0; s <= l[j]; ++s) { n       int val = dp[(j - 1) & 1][s] + 1; n       while (!minq.empty() && minq.back().first >= val) minq.pop_back(); n       minq.push_back({val, s}); n       while (!minq.empty() && minq.front().second + r[j - 1] - l[j - 1] < s) n         minq.pop_front(); n       int t = l[j] - s; n       dp[j & 1][t] = min(dp[j & 1][t], minq.front().first); n     } n     while (!minq.empty()) minq.pop_back(); n     for (int s = 0; s <= r[j - 1]; ++s) { n       int val = dp[(j - 1) & 1][s] + 2; n       while (!minq.empty() && minq.back().first >= val) minq.pop_back(); n       minq.push_back({val, s}); n       while (!minq.empty() && minq.front().second + r[j - 1] - l[j - 1] < s) n         minq.pop_front(); n       int t = s + l[j] - r[j - 1]; n       dp[j & 1][t] = min(dp[j & 1][t], minq.front().first); n     } n   } n   if (dp[k & 1][n] == inf) n     printf( Hungry n ); n   else n     printf( Full n%d n , dp[k & 1][n]); n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %i , &n); n   printf( %li , (long int)n / 2 * 3); n   return 0; n } n


#include <bits/stdc++.h> n long long mod = 1e9 + 7; n int main() { n   long long n, m; n   std::cin >> n >> m; n   std::string s; n   std::cin >> s; n   std::vector<std::vector<long long>> dp(n - m + 1, n                                          std::vector<long long>(n - m + 1)); n   dp[0][0] = 1; n   for (long long i = 1; i < n - m + 1; ++i) n     for (long long j = 0; j < n - m + 1; ++j) { n       dp[i][j] = 0; n       if (j > 0) dp[i][j] += dp[i - 1][j - 1]; n       if (j < n - m) dp[i][j] += dp[i - 1][j + 1]; n       dp[i][j] %= mod; n     } n   long long min = 0, balance = 0; n   for (long long i = 0; i < m; ++i) n     if (s[i] ==  ( ) n       ++balance; n     else if (--balance < min) n       min = balance; n   long long res = 0; n   for (long long left = 0; left <= n - m; ++left) n     for (long long here = -min; here <= left; ++here) n       if (here + balance <= n - m - left) n         res = (res + dp[left][here] * dp[n - m - left][here + balance]) % mod; n   std::cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1E9 + 7; n const long long N = 2E5 + 6; n long long n, m, a[N], b[N], ans, zeros[N]; n long long dp[N]; n long long modexp(long long b, long long e) { n   long long res = 1; n   while (e) { n     if (e & 1) res = (res * b) % MOD; n     e >>= 1; n     b = (b * b) % MOD; n   } n   return res; n } n long long recur(long long ind) { n   if (dp[ind] != -1) return dp[ind]; n   if (ind == n + 1) { n     dp[ind] = 0; n     return 0; n   } n   long long ans = 0; n   if (a[ind] == 0 && b[ind] != 0) { n     ans = (m - b[ind] + MOD) % MOD; n     ans = (ans * modexp(m, zeros[ind + 1]) % MOD) % MOD; n     ans = (ans + recur(ind + 1)) % MOD; n   } else if (a[ind] != 0 && b[ind] == 0) { n     ans = (a[ind] - 1 + MOD) % MOD; n     ans = (ans * modexp(m, zeros[ind + 1]) % MOD) % MOD; n     ans = (ans + recur(ind + 1)) % MOD; n   } else if (a[ind] == 0 && b[ind] == 0) { n     ans = ((m * (m - 1)) % MOD * 500000004LL) % MOD; n     ans = (ans * modexp(m, zeros[ind + 1])) % MOD; n     ans = (ans + (m * recur(ind + 1)) % MOD) % MOD; n   } else { n     if (a[ind] > b[ind]) { n       ans = modexp(m, zeros[ind + 1]) % MOD; n     } else if (a[ind] == b[ind]) n       ans = recur(ind + 1) % MOD; n   } n   dp[ind] = ans; n   return ans; n } n long long modinv(long long x) { return modexp(x, MOD - 2); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   if (fopen( inp.txt ,  r )) { n     freopen( inp.txt ,  r , stdin); n     freopen( outp.txt ,  w , stdout); n   } n   cin >> n >> m; n   long long i; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) cin >> b[i]; n   memset(dp, -1, sizeof(dp)); n   zeros[n + 1] = 0; n   zeros[n] = (b[n] == 0) + (a[n] == 0); n   for (i = n - 1; i >= 1; i--) n     zeros[i] = zeros[i + 1] + (a[i] == 0) + (b[i] == 0); n   long long total = modexp(m, zeros[1]); n   cout << (recur(1) * modinv(total)) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n } n inline void write(int x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 1000005, P = 1e9 + 7; n inline int power(int x, long long y) { n   x %= P; n   static int r; n   r = 1; n   while (y) { n     if (y & 1) r = (long long)r * x % P; n     x = (long long)x * x % P; n     y >>= 1; n   } n   return r; n } n inline long long powerc(long long x, long long y, long long k) { n   static long long r; n   r = 1; n   while (y) { n     if (y & 1) r = r * x; n     x = x * x; n     y >>= 1; n     r = min(r, k), x = min(x, k); n   } n   r = min(r, k); n   return r; n } n int n, p, a[N], b[N], ans; n inline void solve() { n   int i; n   long long now, lst, dt; n   read(n), read(p); n   for (i = 1; i <= n; ++i) read(a[i]); n   if (p == 1) { n     ans = n & 1; n     write(ans), putchar(  n ); n     return; n   } n   sort(a + 1, a + n + 1); n   ans = 0; n   for (i = 1; i <= n; ++i) b[i] = power(p, a[i]); n   for (now = 0, lst = -1, i = n; i >= 1; --i) { n     if (lst == -1) { n       lst = a[i], now = 1, ans = (ans + b[i]) % P; n       continue; n     } n     if (now != 0 && lst != a[i]) { n       dt = lst - a[i]; n       now *= powerc(p, dt, n); n       now = min(now, n * 1ll); n     } n     if (now) { n       --now, ans = (ans + P - b[i]) % P; n       lst = a[i]; n     } else { n       ans = (ans + b[i]) % P; n       now = 1, lst = a[i]; n     } n   } n   write(ans), putchar(  n ); n } n int main() { n   int T; n   read(T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI(acos((long double)-1.0)); n struct cpx { n   long double r, i; n   cpx() : r(0), i(0) {} n   cpx(long double a, long double b) : r(a), i(b) {} n   cpx conj() { return cpx(r, -i); } n } u[20], iu[20], a[524288], b[524288]; n cpx operator+(const cpx &l, const cpx &r) { return cpx(l.r + r.r, l.i + r.i); } n cpx operator-(const cpx &l, const cpx &r) { return cpx(l.r - r.r, l.i - r.i); } n cpx operator*(const cpx &l, const cpx &r) { n   return cpx(l.r * r.r - l.i * r.i, l.r * r.i + r.r * l.i); n } n void dif(cpx *a, cpx *unt, int l) { n   int lv, i, j; n   cpx u, v, w; n   for (lv = l; lv; --lv) n     for (i = 0; i < 1 << l; i += 1 << lv) n       for (j = i, w = {1, 0}; j < i + (1 << lv - 1); ++j, w = w * unt[lv]) { n         u = a[j]; n         v = a[j + (1 << lv - 1)]; n         a[j] = u + v; n         a[j + (1 << lv - 1)] = (u - v) * w; n       } n } n void dit(cpx *a, cpx *unt, int l) { n   int lv, i, j; n   cpx u, v, w; n   for (lv = 1; lv <= l; ++lv) n     for (i = 0; i < 1 << l; i += 1 << lv) n       for (j = i, w = {1, 0}; j < i + (1 << lv - 1); ++j, w = w * unt[lv]) { n         u = a[j]; n         v = a[j + (1 << lv - 1)] * w; n         a[j] = u + v; n         a[j + (1 << lv - 1)] = u - v; n       } n } n int main() { n   int tn, n, x, i, j, l; n   long long a0; n   scanf( %d%d , &n, &x); n   for (tn = 0, l = -1, a0 = 0, i = 0; i < n; ++i) { n     scanf( %d , &j); n     if (j < x) { n       a0 += (long long)(i - l) * (i - l - 1); n       a[++tn].r = i - l; n       l = i; n     } n   } n   a0 += (long long)(n - l) * (n - l - 1); n   a[++tn].r = n - l; n   for (l = 1; 1 << l < tn; ++l) n     ; n   ++l; n   for (i = 1; i < tn; ++i) b[(1 << l) - i].r = a[i].r; n   for (i = 1; i <= l; ++i) n     iu[i] = n         (u[i] = cpx(cos(2 * PI / (1 << i)), -sin(2 * PI / (1 << i)))).conj(); n   dif(a, u, l); n   dif(b, u, l); n   for (i = 0; i < 1 << l; ++i) a[i] = a[i] * b[i]; n   dit(a, iu, l); n   long double t((long double)1.0 / (1 << l)); n   printf( %lld , a0 / 2); n   for (i = 1; i < tn && i <= n; ++i) n     printf(  %lld , (long long)(a[i].r * t + 0.1)); n   for (; i <= n; ++i) printf(  0 ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 5010; n namespace SuffixArray { n const int MAXN = 1 << 21; n string S; n int N, gap; n int sa[MAXN], pos[MAXN], tmp[MAXN], lcp[MAXN]; n bool sufCmp(int i, int j) { n   if (pos[i] != pos[j]) return pos[i] < pos[j]; n   i += gap; n   j += gap; n   return (i < N && j < N) ? pos[i] < pos[j] : i > j; n } n void buildSA() { n   N = S.size(); n   for (int i = 0; i < (int)(N); ++i) sa[i] = i, pos[i] = S[i]; n   for (gap = 1;; gap *= 2) { n     sort(sa, sa + N, sufCmp); n     for (int i = 0; i < (int)(N - 1); ++i) n       tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]); n     for (int i = 0; i < (int)(N); ++i) pos[sa[i]] = tmp[i]; n     if (tmp[N - 1] == N - 1) break; n   } n } n void buildLCP() { n   for (int i = 0, k = 0; i < N; ++i) n     if (pos[i] != N - 1) { n       for (int j = sa[pos[i] + 1]; S[i + k] == S[j + k];) ++k; n       lcp[pos[i]] = k; n       if (k) --k; n     } n } n }  // namespace SuffixArray n using namespace SuffixArray; n bool dp[M][M]; n void pre(string s, int n) { n   for (int i = 1; i <= n; i++) dp[i][i] = 1; n   for (int i = 1; i < n; i++) { n     if (s[i] == s[i + 1]) dp[i][i + 1] = 1; n     if (i == (n - 1)) continue; n     if (s[i] == s[i + 2]) dp[i][i + 2] = 1; n     if (i == (n - 2)) continue; n     if (s[i] == s[i + 3]) dp[i][i + 3] = 1; n   } n   for (int k = 5; k <= n; k++) { n     for (int i = 1; i + k - 1 <= n; i++) { n       int j = i + k - 1; n       if ((s[i] == s[j]) && dp[i + 2][j - 2]) dp[i][j] = 1; n     } n   } n } n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   s =  c  + s; n   pre(s, n); n   int k; n   cin >> k; n   S = s; n   buildSA(); n   buildLCP(); n   int match = 0; n   for (int i = 0; i < n; i++) { n     int j = sa[i]; n     for (int l = match + 1; l + j - 1 <= n; l++) { n       int x = l + j - 1; n       if (!dp[j][x]) continue; n       int cnter = 1; n       for (int f = i; f < n - 1; f++) { n         if (lcp[f] < l) break; n         cnter++; n       } n       k -= cnter; n       if (k <= 0) { n         for (int f = j; f <= x; f++) cout << s[f]; n         cout << endl; n         return 0; n       } n     } n     match = lcp[i]; n   } n   assert(false); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 250005; n int n, k, P, ans, f[30][N], fac[N], ifac[N], inv[N]; n inline int ksm(register int x, register int L) { n   register int ans = 1; n   while (L) n     L& 1 ? ans = (0ll + ans) * x % P, 0 : 0, x = (0ll + x) * x % P, L >>= 1; n   return ans; n } n inline int dfs(const int x, const int s) { n   if (x > k) return 0; n   if (~f[x][s]) return f[x][s]; n   if (!s) return f[x][s] = (0ll + fac[n]) * fac[k] % P * ifac[k - x] % P; n   f[x][s] = 0; n   const int ss = s - (s & -s); n   for (register int i = ss; i; i = (i - 1) & ss) n     f[x][s] = (f[x][s] + (0ll + dfs(x + 1, i)) * ifac[s - i]) % P; n   f[x][s] = (f[x][s] + (0ll + dfs(x + 1, 0)) * ifac[s]) % P; n   return f[x][s]; n } n int main() { n   scanf( %d%d%d , &n, &k, &P), ans = ksm(k, n); n   if (n & 1) return printf( %d , ans), 0; n   for (register int i = n            (fac[0] = fac[1] = ifac[0] = ifac[1] = inv[0] = inv[1] = 1) + 1; n        i <= n; ++i) n     fac[i] = (0ll + fac[i - 1]) * i % P, n     inv[i] = (0ll + inv[P % i]) * (P - P / i) % P, n     ifac[i] = (0ll + ifac[i - 1]) * inv[i] % P; n   memset(f, -1, sizeof(f)), ans -= dfs(0, n), n       printf( %d , ans < 0 ? ans + P : ans); n   return 0; n } n


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


#include <bits/stdc++.h> n double eps = 1e-9; n double cal(int x, int n, int m) { n   double tmp = 1; n   for (int i = 0; i < n; i++) { n     tmp *= (double)x / m; n     if (tmp < eps) return 0.0; n   } n   return tmp; n } n int main() { n   int m, n; n   scanf( %d %d , &m, &n); n   double tmp = 0.0; n   for (int i = m - 1; i >= 1; i--) tmp += cal(i, n, m); n   printf( %.9lf n , m - tmp); n } n


#include <bits/stdc++.h> n using namespace std; n long long save[100005]; n long long aa[100005]; n int hihi[100005]; n int main() { n   int n; n   scanf( %d , &n); n   memset(aa, 0, sizeof(aa)); n   memset(aa, 0, sizeof(aa)); n   int first = 0, second = 0, sum = 1; n   long long a; n   scanf( %I64d , &a); n   if (a != 1) { n     printf( -1 n ); n     return 0; n   } n   hihi[second] = 1; n   aa[second++] = 1; n   for (int i = 1; i < n; i++) { n     scanf( %I64d , &a); n     if (a == 1) n       hihi[second] = 1, aa[second++] = 1, sum = second; n     else if (a == aa[second] * 2) n       hihi[second]++, aa[second++] = a; n     else if (second != 0 && a == aa[second - 1] * 2) n       hihi[0]++, aa[0] = a, second = 1; n     else { n       save[first++] = a; n     } n   } n   if (first > sum) { n     printf( -1 n ); n     return 0; n   } n   int Min, Max = sum + 1; n   int add = first; n   for (int i = sum - 1; i >= 0 && add <= i + 1; i--) add += hihi[i], Min = i; n   long long v[100005]; n   while (Min < Max - 1) { n     memset(v, 0, sizeof(v)); n     int xx = 0; n     for (xx = 0; xx < first; xx++) v[xx] = save[xx]; n     int mid = (Max + Min) / 2; n     for (int i = mid; i < sum; i++) { n       for (long long j = 1; j <= aa[i]; j *= 2) n         v[xx++] = j, assert(xx < 100000); n       ; n     } n     sort(v, v + xx); n     reverse(v, v + xx); n     int ok = 1; n     for (int i = 0; i < xx; i++) { n       if (v[i] > aa[i] * 2) ok = 0; n     } n     if (ok) { n       Max = mid; n     } else { n       Min = mid; n     } n   } n   if (Min == sum) { n     printf( -1 n ); n   } else { n     printf( %d , Min + 1); n     for (int i = Min + 2; i <= sum; i++) printf(  %d , i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n int lim; n int n; n int p[maxn] = {0}; n int res[maxn] = {0}; n bool vis[maxn] = {0}; n int cnt = 0; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &p[i]); n   } n   lim = n; n   res[cnt++] = 1; n   for (int i = 1; i <= n; ++i) { n     int temp = i + 1 + lim - n; n     vis[p[i]] = 1; n     if (p[i] == lim) { n       temp--; n       lim--; n       for (int j = p[i] - 1;; --j) { n         if (vis[j]) { n           temp--; n           lim--; n         } else n           break; n       } n     } n     res[cnt++] = temp; n   } n   for (int i = 0; i <= n; ++i) { n     cout << res[i] <<    ; n   } n   cout << endl; 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 n; n     cin >> n; n     for (int i = 1; i <= n; i++) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, ans; n char signs[3]; n void r1(long long a) { ans = min(ans, a); } n void r2(long long a, long long b) { n   if (signs[2] ==  * ) { n     r1(a * b); n   } else { n     r1(a + b); n   } n } n void r3(long long a, long long b, long long c) { n   if (signs[1] ==  * ) { n     r2(a * b, c); n     r2(a * c, b); n     r2(b * c, a); n   } else { n     r2(a + b, c); n     r2(a + c, b); n     r2(b + c, a); n   } n } n void r4(long long a, long long b, long long c, long long d) { n   if (signs[0] ==  * ) { n     r3(a * b, c, d); n     r3(a * c, b, d); n     r3(a * d, b, c); n     r3(b * c, a, d); n     r3(b * d, a, c); n     r3(c * d, a, b); n   } else { n     r3(a + b, c, d); n     r3(a + c, b, d); n     r3(a + d, b, c); n     r3(b + c, a, d); n     r3(b + d, a, c); n     r3(c + d, a, b); n   } n } n int main() { n   cin >> a >> b >> c >> d >> signs[0] >> signs[1] >> signs[2]; n   ans = 1000000000001; n   r4(a, b, c, d); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, x; n   cin >> n >> x; n   int sum = 0, infected = 0; n   for (int i = 1; i <= n; i++) { n     int val; n     cin >> val; n     sum += val; n     infected += (val == x); n   } n   if (infected == n) n     cout << 0 <<   n ; n   else if (infected > 0 || n * x == sum) n     cout << 1 <<   n ; n   else n     cout << 2 <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int t = 1, no = 0; 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; n   cin >> n; n   long long int a[n + 1]; n   a[0] = 0ll; n   long long int ans = 0ll; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] > a[i - 1]) ans += (1ll * (a[i] - a[i - 1]) * (n - a[i] + 1)); n     if (a[i] < a[i - 1]) ans += (1ll * a[i] * (a[i - 1] - a[i])); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n; n   cin >> n; n   vector<int> a(n); n   int maxNum = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     maxNum = max(a[i], maxNum); n   } n   vector<vector<bool> > board(maxNum, vector<bool>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = maxNum - 1; j > maxNum - 1 - a[i]; j--) { n       board[j][i] = true; n     } n   } n   for (int i = 0; i < maxNum; i++) { n     for (int j = n - 1; j >= 0; j--) { n       if (board[i][j] == false) { n         for (int k = j - 1; k >= 0; k--) { n           if (board[i][k] == true) { n             swap(board[i][j], board[i][k]); n             break; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int count = 0; n     for (int j = 0; j < maxNum; j++) { n       if (board[j][i]) { n         count++; n       } n     } n     cout << count <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long q; n   cin >> q; n   while (q--) { n     long long n, a, b; n     cin >> n >> a >> b; n     if (a + b >= n) n       cout << max(a, b) - (a + b - 1 - n) << endl; n     else n       cout << min(a, b) + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int A[101]; n string s; n long long d[101][101][101]; n long long f(int lo, int hi, int p) { n   if (lo == hi) return A[p]; n   if (d[lo][hi][p]) return d[lo][hi][p]; n   long long ret = f(lo + 1, hi, 1) + A[p]; n   if (s[lo] == s[lo + 1]) ret = max(ret, f(lo + 1, hi, p + 1)); n   for (int i = lo + 2; i <= hi; i++) n     if (s[i] == s[lo]) ret = max(ret, f(lo + 1, i - 1, 1) + f(i, hi, p + 1)); n   return d[lo][hi][p] = ret; n } n int main() { n   cout << setprecision(10); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   cin >> s; n   s =  #  + s; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 1; i <= n; i++) n     for (int k = 1; k < i; k++) A[i] = max(A[i], A[i - k] + A[k]); n   cout << f(1, n, 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC diagnostic ignored  -Wmissing-declarations  n double triangle_area(const complex<double> p1, const complex<double> p2, n                      const complex<double> p3) { n   return abs((p2.real() - p1.real()) * (p3.imag() - p1.imag()) - n              (p2.imag() - p1.imag()) * (p3.real() - p1.real())) / n          2.0; n } n pair<double, complex<double> > getCenter(const complex<double> a, n                                          const complex<double> b, n                                          const complex<double> c) { n   complex<double> res; n   res += a; n   res += b; n   res += c; n   return {triangle_area(a, b, c), res / 3.0}; n } n complex<double> getCenter(const vector<complex<double> >& v) { n   double area = 0; n   complex<double> res; n   const int n = v.size(); n   for (int i = 1; i + 1 < n; ++i) { n     auto p = getCenter(v[0], v[i], v[i + 1]); n     area += p.first; n     res += p.second * p.first; n   } n   return res / area; n } n double getDist(const complex<double> p1, const complex<double> p2) { n   return norm(p1 - p2); n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   vector<complex<double> > v(n); n   for (auto& p : v) { n     double x, y; n     cin >> x >> y; n     p = {x, y}; n   } n   complex<double> center = getCenter(v); n   stringstream out; n   out << fixed << setprecision(10); n   int curP = 1; n   for (auto& p : v) p -= center; n   complex<double> angle = 1.0; n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int f; n       cin >> f; n       --f; n       curP ^= f; n       complex<double> p1 = center + angle * v[curP]; n       complex<double> np{p1.real(), p1.imag() - abs(v[curP])}; n       angle = complex<double>{0, abs(v[curP])} / v[curP]; n       center = np; n       int t; n       cin >> t; n       --t; n       curP ^= t; n     } else { n       int t; n       cin >> t; n       --t; n       complex<double> p = center + angle * v[t]; n       out << p.real() <<     << p.imag() <<   n ; n     } n   } n   cout << out.str(); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   int l = v.size(); n   for (int i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n template <typename T> n void trace(const char* name, T&& arg1) { n   cout << name <<   :   << arg1 << endl; n } n template <typename T, typename... Args> n void trace(const char* names, T&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   trace(comma + 1, args...); n } n const int inf = 1e9 + 10; n const long long int INF = 1e18 + 10; n const int MOD = 1e9 + 7; n const int MAXN = 2e5 + 10; n void solve() { n   int n; n   cin >> n; n   int a[MAXN] = {0}; n   for (int i = 0; i < n; i += 1) cin >> a[i]; n   sort(a, a + n); n   a[n + 1] = inf; n   long long int sum = 0; n   int ans = 0; n   for (int i = 0; i < n; i += 1) { n     if (a[i] >= sum) n       ans++; n     else { n       int j = i; n       while (j < n && a[j] < sum) j++; n       if (a[j] >= sum) { n         ans++; n       } n       i = j; n     } n     sum += a[i]; n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000; n int dist[N * N], memo[N * N]; n vector<pair<int, int>> H, T; n vector<int> ladj[N * N]; n int vis[N * N]; n inline int f(int x, int y) { return x * N + y; } n int bfs(int start) { n   int n = N * N; n   fill(dist, dist + n, INT_MAX); n   fill(memo, memo + n, -1); n   dist[start] = 0; n   queue<int> Q; n   Q.push(start); n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int i = 0; i < (int)ladj[u].size(); i++) { n       int v = ladj[u][i]; n       if (dist[v] > dist[u] + 1) { n         dist[v] = dist[u] + 1; n         Q.push(v); n       } n     } n   } n   return dist[0]; n } n bool hasCycle(int start) { n   if (vis[start] == -1) return true; n   if (vis[start]) return false; n   vis[start] = -1; n   for (int i = 0; i < (int)ladj[start].size(); i++) { n     int u = ladj[start][i]; n     if (hasCycle(u)) return true; n   } n   vis[start] = 1; n   return false; n } n int dp(int u) { n   int &ans = memo[u]; n   if (ans != -1) return ans; n   ans = 0; n   if (ladj[u].empty()) return 0; n   for (int i = 0; i < (int)ladj[u].size(); i++) { n     int v = ladj[u][i]; n     ans = max(ans, dp(v)); n   } n   return ++ans; n } n int main() { n   int h, t, r; n   cin >> h >> t >> r; n   int n; n   cin >> n; n   H.resize(n); n   for (int i = 0; i < n; i++) cin >> H[i].first >> H[i].second; n   int m; n   cin >> m; n   T.resize(m); n   for (int i = 0; i < m; i++) cin >> T[i].first >> T[i].second; n   for (int i = 0; i <= r; i++) n     for (int j = 0; j <= r; j++) n       if (i + j <= r) { n         for (int k = 1; k <= n && k <= i; k++) n           ladj[f(i, j)].push_back( n               f(i - k + H[k - 1].first, j + H[k - 1].second)); n         for (int k = 1; k <= m && k <= j; k++) n           ladj[f(i, j)].push_back( n               f(i + T[k - 1].first, j - k + T[k - 1].second)); n       } n   int start = f(h, t), ans = bfs(start); n   if (ans < INT_MAX) n     cout <<  Ivan  << endl << ans << endl; n   else if (hasCycle(start)) n     cout <<  Draw  << endl; n   else n     cout <<  Zmey  << endl << dp(start) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char x = getchar(); n   int ans = 0, flag = 1; n   while (!isdigit(x)) n     if (x ==  - ) n       flag = -1, x = getchar(); n     else n       x = getchar(); n   while (isdigit(x)) ans = ans * 10 + x -  0 , x = getchar(); n   return ans * flag; n } n int n, m, d, head[100005], nxt[200005], pnt[200005], E, dp[100005][2]; n void add_edge(int u, int v) { n   pnt[E] = v; n   nxt[E] = head[u]; n   head[u] = E++; n } n void dfs1(int u, int pre) { n   for (int i = head[u]; i != -1; i = nxt[i]) { n     int v = pnt[i]; n     if (v == pre) continue; n     dfs1(v, u); n     if (dp[v][0] == -1) continue; n     if (dp[u][0] < dp[v][0] + 1) { n       swap(dp[u][0], dp[u][1]); n       dp[u][0] = dp[v][0] + 1; n     } else if (dp[u][1] < dp[v][0] + 1) { n       dp[u][1] = dp[v][0] + 1; n     } n   } n } n void dfs2(int u, int pre, int val) { n   if (dp[u][0] <= val) { n     swap(dp[u][0], dp[u][1]); n     dp[u][0] = val; n   } else if (dp[u][1] <= val) { n     dp[u][1] = val; n   } n   for (int i = head[u]; i != -1; i = nxt[i]) { n     int v = pnt[i]; n     if (v == pre) continue; n     int tmp = dp[u][0]; n     if (tmp == dp[v][0] + 1) tmp = dp[u][1]; n     dfs2(v, u, tmp + 1); n   } n } n signed main() { n   memset(head, -1, sizeof(head)); n   n = read(), m = read(), d = read(); n   memset(dp, 128, sizeof(dp)); n   for (int i = 1; i <= m; i++) { n     int u = read(); n     dp[u][0] = dp[u][1] = 0; n   } n   for (int i = 1; i < n; i++) { n     int u = read(), v = read(); n     add_edge(u, v); n     add_edge(v, u); n   } n   dfs1(1, 0); n   dfs2(1, 0, -1e9); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (dp[i][0] <= d) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using std::cerr; n using std::cin; n using std::cos; n using std::cout; n using std::endl; n using std::make_pair; n using std::map; n using std::max; n using std::min; n using std::next_permutation; n using std::pair; n using std::priority_queue; n using std::queue; n using std::random_shuffle; n using std::set; n using std::sin; n using std::sort; n using std::sqrt; n using std::stack; n using std::string; n using std::stringstream; n using std::swap; n using std::vector; n const long double PI = 3.14159265358979323846; n int main() { n   string s; n   cin >> s; n   string s1 =  127 ; n   if ((s.size() < s1.size()) || ((s.size() == s1.size()) && (s <= s1))) { n     cout <<  byte  << endl; n     return 0; n   } n   s1 =  32767 ; n   if ((s.size() < s1.size()) || ((s.size() == s1.size()) && (s <= s1))) { n     cout <<  short  << endl; n     return 0; n   } n   s1 =  2147483647 ; n   if ((s.size() < s1.size()) || ((s.size() == s1.size()) && (s <= s1))) { n     cout <<  int  << endl; n     return 0; n   } n   s1 =  9223372036854775807 ; n   if ((s.size() < s1.size()) || ((s.size() == s1.size()) && (s <= s1))) { n     cout <<  long  << endl; n     return 0; n   } n   cout <<  BigInteger  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n long long power(long long base, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = ret * base % mod; n     base = base * base % mod; n     b >>= 1; n   } n   return ret; n } n int n, m; n long long ans, cur; n int main() { n   scanf( %d%d , &n, &m); n   ans = 1LL * (n + m + 1) * power(m + 1, mod - 2) % mod; n   for (int i = 1; i <= n; ++i) { n     cur = (cur + power(i, mod - 2)) % mod; n   } n   cur = cur * m % mod + 1; n   printf( %lld , ans * cur % mod); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 + 5; n const long long MOD = 998244353; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const double PI = acos(-1); n const double ERR = 1e-10; n const int L = 40; n const int M = 30; n using namespace std; n long long mx = -1e18; n long long mn = 1e18; n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   long long a, b[MAXN], c = 0; n   cin >> a; n   if (a == 1) { n     cout <<  0 ; n     return 0; n   } n   for (int i = 0; i < a; i++) { n     cin >> b[i]; n   } n   sort(b, b + a); n   for (int i = 0; i < a; i++) { n     c += b[a - 1] - b[i]; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T getint() { n   T x = 0, p = 1; n   char ch; n   do { n     ch = getchar(); n   } while (ch <=    ); n   if (ch ==  - ) p = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * p; n } n mt19937 gen(chrono::system_clock::now().time_since_epoch().count()); n template <typename T1, typename T2> n bool umin(T1 &x, const T2 &y) { n   if (x > y) return x = y, true; n   return false; n } n template <typename T1, typename T2> n bool umax(T1 &x, const T2 &y) { n   if (x < y) return x = y, true; n   return false; n } n const int maxn = (int)3e5 + 10; n const int inf = (int)1e9 + 5; n const int mod = (int)1e9 + 7; n const long long llinf = (long long)1e18 + 5; n const double pi = acos(-1.0); n int t[4 * maxn]; n int pu[4 * maxn]; n void push(int v) { n   t[v + v] += pu[v]; n   pu[v + v] += pu[v]; n   t[v + v + 1] += pu[v]; n   pu[v + v + 1] += pu[v]; n   pu[v] = 0; n } n void add(int v, int l, int r, int x, int tl, int tr) { n   if (l <= tl && tr <= r) { n     t[v] += x; n     pu[v] += x; n     return; n   } n   push(v); n   int tm = (tl + tr) / 2; n   if (l <= tm) add(v + v, l, r, x, tl, tm); n   if (r > tm) add(v + v + 1, l, r, x, tm + 1, tr); n   t[v] = min(t[v + v], t[v + v + 1]); n } n int a[maxn], b[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, len, h; n   cin >> n >> len >> h; n   vector<int> x; n   for (int i = (0); i < (len); i++) { n     cin >> b[i]; n     b[i] = h - b[i]; n     x.push_back(b[i]); n   } n   for (int i = (0); i < (n); i++) { n     cin >> a[i]; n     x.push_back(a[i]); n   } n   sort((x).begin(), (x).end()); n   x.erase(unique((x).begin(), (x).end()), x.end()); n   for (int i = (0); i < (len); i++) n     b[i] = lower_bound((x).begin(), (x).end(), b[i]) - x.begin(); n   for (int i = (0); i < (n); i++) n     a[i] = lower_bound((x).begin(), (x).end(), a[i]) - x.begin(); n   for (int i = (0); i < (len); i++) add(1, 0, b[i], -1, 0, maxn - 1); n   int res = 0; n   for (int i = (0); i < (n); i++) { n     add(1, 0, a[i], 1, 0, maxn - 1); n     if (i - len >= 0) add(1, 0, a[i - len], -1, 0, maxn - 1); n     if (i + 1 >= len && t[1] >= 0) res++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[201][201] = {0}; n int n, m; n int main(int argc, char *argv[]) { n   int x = 100; n   int y = 100; n   int ok = 0; n   cin >> n >> m; n   a[x][y] = n; n   do { n     ok = 0; n     int max = y; n     int min = x; n     for (int i = x; i <= y; i++) n       for (int j = x; j <= y; j++) n         if (a[i][j] >= 4) { n           ok = 1; n           int numofant = a[i][j]; n           int mods = numofant % 4; n           int temp = (numofant - mods) / 4; n           a[i][j - 1] += temp; n           a[i][j + 1] += temp; n           a[i - 1][j] += temp; n           a[i + 1][j] += temp; n           a[i][j] = mods; n         } n     if (a[x - 1][100] >= 4) { n       x--; n       y++; n     } n   } while (ok == 1); n   long long u, v; n   int k[50005]; n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     u += 100; n     v += 100; n     if (u > 200 || u < 0 || v > 200 || v < 0) n       k[i] = 0; n     else n       k[i] = a[u][v]; n   } n   for (int i = 0; i < m; i++) cout << k[i] << endl; n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n bool dp[501][501]; n long long a[501], b[501]; n signed main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   for (long long i = 1; i < n + 1; i++) { n     cin >> a[i] >> b[i]; n   } n   dp[0][0] = true; n   long long tot = 0; n   for (long long i = 1; i < n + 1; i++) { n     tot += (a[i] + b[i]); n     for (long long j = 0; j < min(k, a[i] + 1); j++) { n       long long x = (a[i] - j) % k; n       if (x + b[i] < k) continue; n       for (long long l = 0; l < k; l++) { n         if (!dp[i - 1][l]) continue; n         long long rem = (l + j) % k; n         dp[i][rem] = true; n       } n     } n     long long j = a[i] % k; n     for (long long l = 0; l < k; l++) { n       if (!dp[i - 1][l]) continue; n       long long rem = (l + j) % k; n       dp[i][rem] = true; n     } n   } n   for (long long i = 0; i < k; i++) { n     if (dp[n][i]) { n       long long ans = (tot - i) / k; n       cout << ans; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, l, m, n; n   cin >> n; n   if (n & 1) n     cout << 1 << endl; n   else n     cout << 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long i, a = 0, b, j, b1[26] = {}, b2[26] = {}; n   string s1, s2; n   vector<long> v1, v2; n   cin >> s1 >> s2; n   if (s1.size() != s2.size()) { n     cout <<  NO ; n     return 0; n   } n   for (i = 0; i < s1.size(); i++) { n     if (s1[i] != s2[i]) { n       a++; n       v1.push_back(i); n     } n   } n   if (a != 2) { n     cout <<  NO ; n     return 0; n   } n   if ((s1[v1[0]] == s2[v1[1]]) && (s2[v1[0]] == s1[v1[1]])) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n using namespace std; n const int N = 7005; n const int M = 200000; n const long long mod = 1e9 + 7; n const long long inf = 0x3f3f3f3f; n namespace FastIO { n template <typename tp> n inline void read(tp& x) { n   x = 0; n   register char c = getchar(); n   register bool f = 0; n   for (; c <  0  || c >  9 ; f |= (c ==  - ), c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; x = (x << 3) + (x << 1) + c -  0 , c = getchar()) n     ; n   if (f) x = -x; n } n template <typename tp> n inline void write(tp x) { n   if (x == 0) return (void)(putchar( 0 )); n   if (x < 0) putchar( - ), x = -x; n   long long pr[20]; n   register long long cnt = 0; n   for (; x; x /= 10) pr[++cnt] = x % 10; n   while (cnt) putchar(pr[cnt--] +  0 ); n } n template <typename tp> n inline void writeln(tp x) { n   write(x); n   putchar(  n ); n } n }  // namespace FastIO n using namespace FastIO; n struct line { n   int l, r, val; n   line() {} n   line(int l, int r, int val) { n     this->l = l; n     this->r = r; n     this->val = val; n   } n }; n int n, m, lx[M], ly[M]; n vector<line> xx[N]; n int solve(int d, int x, int y) { n   set<int> ans; n   for (int i = d; i <= n; i++) { n     for (int j = 0; j < xx[i].size(); j++) { n       line& c = xx[i][j]; n       if (c.l <= y && c.r >= x) ans.insert(c.val); n     } n     x = (lx[x] == -1 ? ly[x] : lx[x]); n     y = ly[y]; n   } n   return ans.size(); n } n void init() { n   int cnt = 1, p = 3; n   memset(lx, -1, sizeof(lx)); n   memset(ly, -1, sizeof(ly)); n   lx[1] = 1; n   ly[1] = 2; n   for (int i = 2; i < M; i++) { n     if ((1 << cnt) == i) cnt++, lx[i] = p++; n     ly[i] = p++; n   } n } n int flag, d, x, y, val; n int main() { n   init(); n   read(n), read(m); n   for (int i = 0; i < m; i++) { n     read(flag); n     if (flag == 1) n       read(d), read(x), read(y), read(val), xx[d].push_back(line(x, y, val)); n     else n       read(d), read(x), writeln(solve(d, x, x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int binSearch(int *a, int n, int x) { n   int l = 0; n   int r = n - 1; n   while (l < r) { n     int mid = (l + r) / 2; n     if (a[mid] == x) n       return mid; n     else if (a[mid] < x) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return -1; n } n int main() { n   map<long long, long long> mp; n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]] = i + 1; n   } n   int q; n   cin >> q; n   long long cntFirst = 0, cntSecond = 0; n   for (int i = 0; i < q; i++) { n     int x; n     cin >> x; n     long long res = mp[x]; n     cntFirst += res; n     cntSecond += (n - res + 1); n   } n   cout << cntFirst <<     << cntSecond; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx ) n using namespace std; n const int mod = 1e9 + 7; n const int N = 2e5 + 4; n int a[N], v[N], s[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     v[i] = 1; n   } n   sort(a, a + n); n   int j = 0; n   int res = 0; n   int cur = 0; n   for (int i = 0; i < n; i++) { n     while (j < n && a[j] - a[i] < m) { n       cur += v[j]; n       s[j] = cur; n       if (cur >= k && v[j] == 1) { n         v[j] = 0; n         res++; n         cur--; n       } n       j++; n     } n     if (v[i]) cur--; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 10000; n struct Big { n   vector<int> *data; n   int size() const { return data->size(); } n   bool operator<(const Big &a) const { n     if (size() != a.size()) return size() < a.size(); n     for (int i = (int)size() - 1; i >= 0; i--) n       if ((*data)[i] != (*a.data)[i]) return (*data)[i] < (*a.data)[i]; n     return false; n   } n   void Carry() { n     for (int i = 0; i < size(); i++) n       if ((*data)[i] >= MOD) { n         if (i + 1 == size()) data->push_back(0); n         (*data)[i + 1] += (*data)[i] / MOD; n         (*data)[i] %= MOD; n       } n   } n   Big operator*(const Big &a) const { n     Big ans; n     ans.data->resize((int)(size() + a.size()) - 1, 0); n     for (int i = 0; i < (int)size(); i++) n       for (int j = 0; j < (int)a.size(); j++) n         (*ans.data)[i + j] += (*data)[i] * (*a.data)[j]; n     ans.Carry(); n     return ans; n   } n   void operator*=(const Big &a) { (*this) = (*this) * a; } n   void operator++() { n     (*data)[0]++; n     Carry(); n   } n   Big() : data(new vector<int>()) {} n   Big(const int b) : data(new vector<int>()) { n     data->push_back(b); n     Carry(); n   } n   Big(const Big &a) : data(new vector<int>()) { n     data->resize(a.size()); n     for (int i = 0; i < a.size(); i++) (*data)[i] = (*a.data)[i]; n   } n }; n void getmax(Big &a, const Big &b) { n   if (a < b) a = b; n } n void Print(const Big &a) { n   bool printed = false; n   for (int i = (int)a.size() - 1; i >= 0; i--) { n     const int v = (*a.data)[i]; n     printf(printed ?  %04d  :  %d , v); n     printed = true; n   } n } n int N; n Big DP[700][3]; n vector<int> ET[700]; n struct Type { n   Big yes, no, one; n   Type() {} n   Type(const Big &_yes, const Big &_no, const Big &_one) n       : yes(_yes), no(_no), one(_one) {} n   bool operator<(const Type &t) const { return no * t.yes < yes * t.no; } n }; n Big Dfs(const int u, const int fa, const int dep) { n   Big &ans = DP[u][dep]; n   if (!(ans.data->empty())) return ans; n   if (true) { n     ans = Big(1); n     for (const int nxt : ET[u]) n       if (nxt != fa) ans *= Dfs(nxt, u, 2); n   } n   if (dep == 0) return ans; n   vector<Type> chinfo; n   for (const int nxt : ET[u]) n     if (nxt != fa) n       chinfo.push_back(Type(Dfs(nxt, u, 0), Dfs(nxt, u, 2), Dfs(nxt, u, 1))); n   sort(chinfo.begin(), chinfo.end()); n   const int sz = chinfo.size(); n   if (true) { n     vector<Big> yes, no, part; n     yes.resize(sz + 1), no.resize(sz + 1), part.resize(sz + 1); n     if (true) { n       Big v1 = Big(1), v2 = Big(dep == 1 ? 2 : 1); n       yes[0] = Big(v1), part[0] = Big(v2); n       for (int i = 1; i <= sz; i++) n         v1 *= chinfo[i - 1].yes, ++v2, yes[i] = Big(v1), part[i] = Big(v2); n       v1 = Big(1); n       no[sz] = Big(v1); n       for (int i = sz - 1; i >= 0; i--) v1 *= chinfo[i].no, no[i] = Big(v1); n     } n     for (int i = 0; i <= sz; i++) getmax(ans, yes[i] * no[i] * part[i]); n   } n   if (dep == 1) return ans; n   if (sz) { n     vector<Big> left, right; n     left.resize(sz), right.resize(sz); n     Big tmp = Big(1); n     left[0] = Big(tmp); n     for (int i = 1; i < sz; i++) tmp *= chinfo[i - 1].no, left[i] = Big(tmp); n     tmp = Big(1); n     right[sz - 1] = Big(tmp); n     for (int i = sz - 2; i >= 0; i--) n       tmp *= chinfo[i + 1].no, right[i] = Big(tmp); n     for (int i = 0; i < sz; i++) n       getmax(ans, left[i] * right[i] * chinfo[i].one); n   } n   assert(dep == 2); n   return ans; n } n int main() { n   while (scanf( %d , &N) == 1) { n     for (int i = 0; i < N; i++) ET[i].clear(); n     for (int i = 0, a, b; i < N - 1; i++) { n       scanf( %d%d , &a, &b), a--, b--; n       ET[a].push_back(b), ET[b].push_back(a); n     } n     Big ans = Big(0); n     for (int i = 0; i <= 0; i++) { n       for (int j = 0; j < N; j++) n         for (int k = 0; k < 3; k++) DP[j][k].data->clear(); n       Big ta = Dfs(i, -1, 2); n       getmax(ans, ta); n     } n     Print(ans); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   string s[105]; n   set<char> ms; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   long long ans = 1; n   for (int i = 0; i < m; i++) { n     ms.clear(); n     for (int j = 0; j < n; j++) ms.insert(s[j][i]); n     ans = ans * ms.size() % 1000000007; n   } n   cout << ans % 1000000007; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void read(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void readc(char &x) { n   char c; n   while (((c = getchar()) ==    ) || c ==   n ) n     ; n   x = c; n } n inline void writes(string s) { puts(s.c_str()); } n inline void writeln() { writes(  ); } n inline void writei(int x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline void writell(long long x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n int n, m, i, j, lst, t[100005], w[100005], col[100005], dfn[100005], n     fa[100005][19], dep[100005], stk[100005], tp, blk, ti, a[100005], n     ixt[100005], ans[100005], cur, dup[100005], ixf[100005], cq, duv[100005]; n vector<int> e[100005], ee[100005], all, tmp; n vector<pair<int, int> > dur[100005]; n int seq[100005]; n void add(int x, int y) { n   all.push_back(x); n   all.push_back(y); n   ee[x].push_back(y); n   ee[y].push_back(x); n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) { n     swap(x, y); n   } n   int t = dep[x] - dep[y], i; n   for ((i) = (18); (i) >= (0); (i)--) { n     if ((t >> i) & 1) { n       x = fa[x][i]; n     } n   } n   if (x == y) return x; n   for ((i) = (18); (i) >= (0); (i)--) { n     if (fa[x][i] != fa[y][i]) { n       x = fa[x][i]; n       y = fa[y][i]; n     } n   } n   return fa[x][0]; n } n void dfs(int x) { n   dfn[x] = ++ti; n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { n     dep[*it] = dep[x] + 1; n     dfs(*it); n   } n } n void dfs2(int x) { n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { n     dfs2(*it); n     w[x] += w[*it]; n   } n } n bool cmp(int x, int y) { return dfn[x] < dfn[y]; } n void ins(int x) { n   if (tp <= 1) { n     stk[++tp] = x; n     return; n   } n   int z = lca(stk[tp], x); n   if (z == stk[tp]) { n     stk[++tp] = x; n     return; n   } n   while (tp > 1 && dfn[stk[tp - 1]] >= dfn[z]) { n     add(stk[tp], stk[tp - 1]); n     tp--; n   } n   if (stk[tp] != z) { n     add(stk[tp], z); n     stk[tp] = z; n   } n   stk[++tp] = x; n } n void ine(int x, int y) { n   int id = y; n   duv[id] = 0; n   y = fa[y][0]; n   tmp.clear(); n   while (y != x) { n     ixt[y] = 1; n     if (!col[y]) tmp.push_back(t[y] - w[y]); n     y = fa[y][0]; n   } n   sort(tmp.begin(), tmp.end()); n   dur[id].clear(); n   for (__typeof((tmp).begin()) it = (tmp).begin(); it != (tmp).end(); it++) { n     if (dur[id].empty()) { n       dur[id].push_back(make_pair(*it, 1)); n     } else if (dur[id].back().first == *it) { n       dur[id].back().second++; n     } else { n       dur[id].push_back(make_pair(*it, dur[id].back().second + 1)); n     } n   } n   for (dup[id] = 0; dup[id] < dur[id].size(); dup[id]++) n     if (duv[id] <= dur[id][dup[id]].first) break; n   dup[id]--; n   cur += (dup[id] == -1 ? 0 : dur[id][dup[id]].second); n } n void dfs3(int x, int fa) { n   ixf[x] = fa; n   ixt[x] = 1; n   if (!col[x]) cur += (w[x] > t[x]); n   for (__typeof((ee[x]).begin()) it = (ee[x]).begin(); it != (ee[x]).end(); n        it++) n     if (*it != fa) { n       ine(x, *it); n       dfs3(*it, x); n     } n } n void dfs4(int x) { n   if (!ixt[x]) { n     if (!col[x]) cur += (w[x] > t[x]); n   } n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { n     dfs4(*it); n   } n } n void upe(int x, int dlt) { n   duv[x] += dlt; n   cur -= (dup[x] == -1 ? 0 : dur[x][dup[x]].second); n   while (dup[x] + 1 < dur[x].size() && duv[x] > dur[x][dup[x] + 1].first) n     dup[x]++; n   while (dup[x] > -1 && duv[x] <= dur[x][dup[x]].first) dup[x]--; n   cur += (dup[x] == -1 ? 0 : dur[x][dup[x]].second); n } n void work(int x) { n   int dlt = x / abs(x), fst = 1; n   x = abs(x); n   while (x) { n     if (!col[x]) cur -= (w[x] > t[x]); n     if (fst) col[x] += dlt; n     fst = 0; n     w[x] += dlt; n     if (!col[x]) cur += (w[x] > t[x]); n     if (x != 1) upe(x, dlt); n     x = ixf[x]; n   } n } n void solve(int ed) { n   int i; n   cur = 0; n   all.clear(); n   memset(w, 0, sizeof(w)); n   memset(ixt, 0, sizeof(ixt)); n   for (((i)) = (1); ((i)) <= ((lst)); ((i))++) { n     if (seq[i] > 0) { n       w[seq[i]] = col[seq[i]] = 1; n     } else { n       w[-seq[i]] = col[-seq[i]] = 0; n     } n   } n   dfs2(1); n   for ((i) = (lst + 1); (i) <= (ed); (i)++) { n     a[i - lst] = abs(seq[i]); n   } n   sort(a + 1, a + (ed - lst) + 1, cmp); n   stk[tp = 1] = 1; n   for (((i)) = (1); ((i)) <= ((ed - lst)); ((i))++) { n     if (a[i] != 1 && a[i] != a[i - 1]) { n       ins(a[i]); n     } n   } n   while (tp >= 2) { n     add(stk[tp], stk[tp - 1]); n     tp--; n   } n   dfs3(1, 0); n   dfs4(1); n   for ((i) = (lst + 1); (i) <= (ed); (i)++) { n     work(seq[i]); n     printf( %d  , cur); n   } n   for (__typeof((all).begin()) it = (all).begin(); it != (all).end(); it++) { n     ee[*it].clear(); n   } n } n int main() { n   read(n); n   read(m); n   blk = sqrt(m) * 2; n   for ((i) = (2); (i) <= (n); (i)++) { n     scanf( %d , &fa[i][0]); n     e[fa[i][0]].push_back(i); n   } n   for (((j)) = (1); ((j)) <= ((18)); ((j))++) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n       fa[i][j] = fa[fa[i][j - 1]][j - 1]; n     } n   for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n     scanf( %d , &t[i]); n   } n   dfs(1); n   lst = 0; n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n     scanf( %d , &seq[i]); n     if (i % blk == 0) { n       solve(i); n       lst = i; n     } n   } n   if (lst < m) solve(m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int main() { n   int t; n   int n, m; n   long long s = 0; n   cin >> n >> m; n   vector<long long> a(m); n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   s += (a[0] - 1); n   for (int i = 1; i < m; i++) { n     if (a[i] >= a[i - 1]) { n       s += (a[i] - a[i - 1]); n     } else { n       s += (n - a[i - 1]); n       s += (a[i]); n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int id; n   bool ok; n   int row; n   bool operator<(const point &p) const { n     return (id == p.id) ? ok < p.ok : id < p.id; n   } n }; n point a[600100]; n vector<int> edge[300100]; n int dp[300100]; n int truoc[300100]; n bool ok[300100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     a[i << 1] = {y, 1, x}; n     a[i << 1 | 1] = {z + 1, 0, x}; n   } n   sort(a, a + 2 * m); n   multiset<int, greater<int> > b; n   multiset<int> c; n   for (int i = 0; i < 2 * m; i++) { n     if (a[i].ok) { n       if (b.upper_bound(a[i].row) != b.end()) { n         edge[*b.upper_bound(a[i].row)].push_back(a[i].row); n       } n       if (c.upper_bound(a[i].row) != c.end()) { n         edge[a[i].row].push_back(*c.upper_bound(a[i].row)); n       } n       b.insert(a[i].row); n       c.insert(a[i].row); n     } else { n       b.erase(b.find(a[i].row)); n       c.erase(c.find(a[i].row)); n     } n   } n   for (int i = 1; i < n; i++) { n     dp[i] = max(dp[i], 1); n     for (auto x : edge[i]) { n       if (dp[x] < dp[i] + 1) { n         dp[x] = dp[i] + 1; n         truoc[x] = i; n       } n     } n   } n   int max1 = -1; n   int id; n   for (int i = 1; i <= n; i++) { n     if (dp[i] > max1) { n       max1 = dp[i]; n       id = i; n     } n   } n   cout << n - max1 <<   n ; n   int cur = id; n   while (cur != 0) { n     ok[cur] = 1; n     cur = truoc[cur]; n   } n   for (int i = 1; i <= n; i++) { n     if (!ok[i]) { n       cout << i <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, m; n   cin >> w >> m; n   vector<int> v; n   while (m > 0) { n     v.push_back(m % w); n     m /= w; n   } n   int ok = 1; n   for (int i = 0; i < v.size(); i++) { n     if (v[i] != 0 && v[i] != 1) { n       v[i] -= w; n       if (v[i] != -1 && v[i] != 0) { n         ok = 0; n         break; n       } n       if (i + 1 != v.size()) v[i + 1]++; n     } n   } n   if (ok == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, sum = 0; n   cin >> a >> b; n   sum = (a * b) / 2; n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pow_mod(long long int a, long long int b) { n   long long int res = 1; n   while (b != 0) { n     if (b & 1) { n       res = (res * a) % 1000000007; n     } n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res; n } n void solve() { n   long long int x1, y1, z1; n   cin >> x1 >> y1 >> z1; n   long long int x2, y2, z2; n   cin >> x2 >> y2 >> z2; n   long long int ans = 0; n   ans = min(z1, y2); n   ans *= 2; n   z1 -= min(z1, y2); n   z2 -= min(z2, z1); n   z2 -= min(z2, x1); n   ans -= z2 * 2; n   cout << ans << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t1, t2, x1, x2, t0, ans1, ans2, z1, z2, y20; n inline void trr(const long long& y1_gdssdfjsdgf, const long long& y2) { n   if (y1_gdssdfjsdgf == 0 && y2 == 0) return; n   if (ans1 == -1) { n     if ((y1_gdssdfjsdgf * t1 + y2 * t2) >= t0 * (y1_gdssdfjsdgf + y2)) { n       ans1 = y1_gdssdfjsdgf; n       ans2 = y2; n     } n     return; n   } n   z1 = (ans1 * t1 + ans2 * t2) * (y1_gdssdfjsdgf + y2) - n        t0 * (y1_gdssdfjsdgf + y2) * (ans1 + ans2); n   z2 = (y1_gdssdfjsdgf * t1 + y2 * t2) * (ans1 + ans2) - n        t0 * (y1_gdssdfjsdgf + y2) * (ans1 + ans2); n   if ((z1 >= 0 && z2 >= 0) && n       (z1 > z2 || ((z1 == z2) && (y1_gdssdfjsdgf + y2 > ans1 + ans2)))) { n     ans1 = y1_gdssdfjsdgf; n     ans2 = y2; n   } n } n int main() { n   cin >> t1 >> t2 >> x1 >> x2 >> t0; n   ans1 = -1; n   ans2 = -1; n   trr(0, x2); n   trr(x1, 0); n   trr(x1, x2); n   for (long long y1_gdssdfjsdgf = 0; y1_gdssdfjsdgf <= x1; y1_gdssdfjsdgf++) n     if (t2 != t0) { n       y20 = ((t0 - t1) * y1_gdssdfjsdgf) / (t2 - t0); n       for (long long y2 = max(0LL, y20 - 2); y2 <= min(x2, y20 + 2); y2++) { n         trr(y1_gdssdfjsdgf, y2); n       } n     } n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10; n const int mod = 998244353; n int f[N][N], g[N][N]; n int mn[N][N], indl[N], indr[N]; n bool bz[N][N]; n int a[1000001]; n int n, m; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   int x = m; n   m = 1; n   for (int i = 2; i <= x; i++) n     if (a[i] != a[m]) a[++m] = a[i]; n   for (int i = 1; i <= m; i++) { n     if (!indl[a[i]]) indl[a[i]] = i; n     indr[a[i]] = i; n   } n   if (m > 3 * n) { n     printf( 0 n ); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     for (int j = indl[i]; j <= indr[i]; j++) n       if (a[j] < i) { n         printf( 0 n ); n         return 0; n       } n   for (int i = 1; i <= m; i++) { n     mn[i][i] = a[i]; n     for (int j = i + 1; j <= m; j++) mn[i][j] = min(mn[i][j - 1], a[j]); n   } n   for (int i = 1; i <= m + 1; i++) f[i][i - 1] = g[i][i - 1] = 1; n   for (int i = 1; i <= m; i++) n     for (int l = 1; l + i - 1 <= m; l++) { n       int r = i + l - 1; n       int x = mn[l][r], idl = indl[x], idr = indr[x]; n       if (idl > r || idr < l) continue; n       int v = 1; n       int lll = idl; n       while (lll != idr) { n         int rrr = lll + 1; n         while (a[rrr] != a[lll]) rrr++; n         v = (long long)v * f[lll + 1][rrr - 1] % mod; n         lll = rrr; n       } n       for (int mid = idr; mid <= r; mid++) n         g[l][r] = (g[l][r] + (long long)f[l][idl - 1] * f[mid + 1][r] % mod * n                                  v % mod * f[idr + 1][mid] % mod) % n                   mod; n       for (int ll = l; ll <= idl; ll++) n         f[l][r] = (f[l][r] + (long long)g[ll][r] * f[l][ll - 1]) % mod; n     } n   printf( %d n , f[1][m]); n   return 0; n } n


#include <bits/stdc++.h> n int a[300004], pos[300004]; n using namespace std; n int main() { n   int n, temp; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     pos[a[i]] = i; n   } n   bool flag = false; n   for (int i = 1; i <= n && !flag; i++) n     for (int j = i + 1; j <= min(n, i + 5) && !flag; j++) { n       temp = 2 * a[j] - a[i]; n       if (temp <= n && temp > 0 && pos[temp] > j) flag = 1; n       temp = 2 * a[i] - a[j]; n       if (temp <= n && temp > 0 && pos[temp] < i) flag = 1; n     } n   if (flag) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


/* n ID: c4ts0up n LANG: C++ n TASK:  n URL:  n */ n  n #include <bits/stdc++.h> n using namespace std; n  n typedef long long ll; n typedef long double ld; n #define pb push_back n #define ff first n #define ss second n #define endl   n  n  n void fast_io() { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n } n  n const ll MOD = 1e9+7, NMAX = 1005; n ll t, n, k; n  n ll dp[NMAX][NMAX][2]; n bool vis[NMAX][NMAX][2]; n  n // devuelve la cantidad de particulas generadas por esta n ll Generadas(ll tam, ll pla, bool dir) { n     // Caso base n     if (tam == 1) return 1LL; n     else if (pla < 0 || pla >= n) return 1LL; n     // Caso dp n     else if (vis[tam][pla][dir]) return dp[tam][pla][dir]; n     // Caso recursivo n     else { n         if (dir) dp[tam][pla][dir] = (Generadas(tam-1, pla-1, !dir) + Generadas(tam, pla+1, dir))%MOD; n         else dp[tam][pla][dir] = (Generadas(tam-1, pla+1, !dir) + Generadas(tam, pla-1, dir))%MOD; n  n         vis[tam][pla][dir] = true; n         return dp[tam][pla][dir]; n     } n } n  n int main() { n     /*// n     freopen( entrada.in ,  r , stdin); n     freopen( salida.out ,  w , stdout); n     //*/ n  n     cin >> t; n     while (t--) { n         cin >> n >> k; n         for (int i=0; i<=n; i++) { n             for (int j=0; j<=k; j++) vis[j][i][0] = vis[j][i][1] = false;  n         } n         cout << Generadas(k, 0, 1) << endl; n     } n     //system( pause ); n     return 0; n }


#include <bits/stdc++.h> n std::string colors( RGBYW ); n struct Card { n   int color; n   int number; n   bool operator<(const Card& rhs) const { n     if (color == rhs.color) n       return (number < rhs.number); n     else n       return color < rhs.color; n   } n }; n int mapColor(char color) { n   size_t pos = colors.find_first_of(color); n   if (pos != std::string::npos) { n     return pos; n   } n   return -1; n } n bool isNumberHinted(int number, long long hint) { n   long long mask = (1 << number) << 5; n   return hint & mask; n } n bool isColorHinted(int color, long long hint) { n   long long mask = (1 << color); n   return hint & mask; n } n int bitCount(long long n) { n   int count = 0; n   while (n > 0) { n     count++; n     n = n & (n - 1); n   } n   return count; n } n void codeforces442a() { n   int n = 0; n   int minAns = 10; n   std::cin >> n; n   std::set<Card> cards; n   for (int i = 0; i < n; ++i) { n     Card card; n     char color; n     char number; n     std::cin >> color; n     std::cin >> number; n     card.color = mapColor(color); n     card.number = number -  1 ; n     cards.insert(card); n   } n   if (cards.size() == 1) { n     std::cout <<  0 ; n     return; n   } n   for (long long hint = 0; hint < (1 << 10); ++hint) { n     if (bitCount(hint) >= minAns) continue; n     bool isValid = true; n     for (std::set<Card>::iterator i = cards.begin(); n          i != cards.end() && isValid; ++i) n       for (std::set<Card>::iterator j = cards.begin(); n            j != cards.end() && isValid; ++j) { n         if (i != j) { n           bool areHintedAsDifferent = false; n           Card firstCard = *i; n           Card secondCard = *j; n           if (firstCard.color == secondCard.color) { n             areHintedAsDifferent = isNumberHinted(firstCard.number, hint) || n                                    isNumberHinted(secondCard.number, hint); n           } else if (firstCard.number == secondCard.number) { n             areHintedAsDifferent = isColorHinted(firstCard.color, hint) || n                                    isColorHinted(secondCard.color, hint); n           } else { n             areHintedAsDifferent = isNumberHinted(firstCard.number, hint) || n                                    isNumberHinted(secondCard.number, hint) || n                                    isColorHinted(firstCard.color, hint) || n                                    isColorHinted(secondCard.color, hint); n           } n           if (!areHintedAsDifferent) { n             isValid = false; n           } n         } n       } n     if (isValid) { n       int numHints = bitCount(hint); n       if (minAns > numHints) { n         minAns = numHints; n       } n     } n   } n   std::cout << minAns; n } n int main() { n   codeforces442a(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, m[1001][1001]; n void solve() { n   cin >> n >> a >> b; n   if (a > 1 and b > 1) { n     cout <<  NO  <<   n ; n     return; n   } n   if (a == 1 and b == 1) { n     if (n == 1) { n       cout <<  YES  <<   n ; n       cout << 0; n       return; n     } n     if (n <= 3) { n       cout <<  NO  <<   n ; n       return; n     } n     for (int i = 1; i < n; i++) { n       m[i][i + 1] = 1; n       m[i + 1][i] = 1; n     } n     cout <<  YES  <<   n ; n     for (int i = 1; i < n + 1; i++) { n       for (int j = 1; j < n + 1; j++) { n         cout << m[i][j]; n       } n       cout <<   n ; n     } n     return; n   } n   if (a == 1) { n     for (int i = 1; i < b; i++) { n       for (int j = 1; j < n + 1; j++) { n         if (i == j) continue; n         m[i][j] = 1; n         m[j][i] = 1; n       } n     } n     cout <<  YES  <<   n ; n     for (int i = 1; i < n + 1; i++) { n       for (int j = 1; j < n + 1; j++) { n         cout << m[i][j]; n       } n       cout <<   n ; n     } n     return; n   } n   if (b == 1) { n     for (int i = a; i < n + 1; i++) { n       for (int j = a; j < n + 1; j++) { n         if (i == j) continue; n         m[i][j] = 1; n         m[j][i] = 1; n       } n     } n     cout <<  YES  <<   n ; n     for (int i = 1; i < n + 1; i++) { n       for (int j = 1; j < n + 1; j++) { n         cout << m[i][j]; n       } n       cout <<   n ; n     } n     return; n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0}; n long long k; n int n, m, h[N][N]; n bool mark[N][N]; n int pre[N * N], sz[N * N]; n int find(int x) { return pre[x] = (pre[x] == x ? x : find(pre[x])); } n bool merge(int x, int y) { n   int a = find(x), b = find(y); n   if (a == b) return false; n   if (rand() % 2) swap(a, b); n   sz[b] += sz[a]; n   pre[a] = b; n   return true; n } n inline int id(int i, int j) { return i * m + j; } n int res[N][N]; n void dfs(int i, int j, int v) { n   if (i < 0 || i >= n || j < 0 || j >= m || h[i][j] < v) return; n   if (res[i][j] || !k) return; n   res[i][j] = v; n   k -= v; n   for (int d = 0; d < 4; d++) { n     int ni = i + di[d], nj = j + dj[d]; n     dfs(ni, nj, v); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   vector<tuple<int, int, int>> pos; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       pre[id(i, j)] = id(i, j); n       sz[id(i, j)] = 1; n       cin >> h[i][j]; n       pos.emplace_back(h[i][j], i, j); n     } n   sort(pos.rbegin(), pos.rend()); n   bool good = false; n   for (auto p : pos) { n     int v, i, j; n     tie(v, i, j) = p; n     for (int d = 0; d < 4; d++) { n       int ni = i + di[d], nj = j + dj[d]; n       if (ni < 0 || ni >= n || nj < 0 || nj >= m || !mark[ni][nj]) continue; n       merge(id(i, j), id(ni, nj)); n     } n     mark[i][j] = true; n     if (k % v == 0 && k / v <= sz[find(id(i, j))]) { n       dfs(i, j, h[i][j]); n       good = true; n       break; n     } n   } n   if (good) { n     cout <<  YES n ; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) cout << res[i][j] <<    ; n       cout <<   n ; n     } n   } else n     cout <<  NO n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long n, m, dp[1 << 19][21], a[22][22], ans; n long long lowbit(long long x) { return (x & (-x)); } n int main() { n   memset(a, 0, sizeof(a)); n   memset(dp, 0, sizeof(dp)); n   scanf( %lld%lld , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     a[x - 1][y - 1] = a[y - 1][x - 1] = 1; n   } n   for (int i = 0; i <= n; i++) dp[1 << i][i] = 1; n   for (int mask = 1; mask < (1 << n); mask++) { n     for (int ed = 0; ed < n; ed++) { n       if (!dp[mask][ed]) continue; n       for (int nxt = 0; nxt < n; nxt++) { n         if (!a[ed][nxt]) continue; n         if (lowbit(mask) > (1 << nxt)) continue; n         if (mask & (1 << nxt)) { n           if ((lowbit(mask)) == (1 << nxt)) ans += dp[mask][ed]; n         } else n           dp[(mask | (1 << nxt))][nxt] += dp[mask][ed]; n       } n     } n   } n   printf( %lld , (ans - m) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename P, typename Q> n ostream& operator<<(ostream& os, pair<P, Q> p) { n   os <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n int main(int argc, char* argv[]) { n   int n, k; n   while (scanf( %d%d , &n, &k) == 2) { n     vector<int> v; n     for (int i = 0; i < n; ++i) { n       int m; n       scanf( %d , &m); n       v.push_back(m); n     } n     long long int sum = 0; n     for (int i = 0; i < n; ++i) { n       long long int s = sum + i + 1; n       if (k <= s) { n         printf( %d n , v[k - sum - 1]); n         break; n       } n       sum = s; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[256]; n signed main() { n   long long n; n   scanf( %lld , &n); n   stack<long long> cur; n   set<long long> si; n   long long tmp = 1, ans = 0; n   for (long long i = 0; i < n * 2; i++) { n     scanf( %s , buf); n     string s(buf); n     if (s ==  add ) { n       long long x; n       scanf( %lld , &x); n       cur.push(x); n     } n     if (s ==  remove ) { n       if (cur.empty() || cur.top() == tmp) { n         if (!cur.empty()) cur.pop(); n         tmp++; n       } else { n         while (!cur.empty()) { n           si.insert(cur.top()); n           cur.pop(); n         } n         si.erase(tmp); n         tmp++; n         ans++; n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300100, mod = 998244353; n inline void Add(int &a, int b) { a = a + b >= mod ? a + b - mod : a + b; } n int head[maxn], nxt[maxn << 1], ver[maxn << 1], tot; n inline void addedge(int a, int b) { n   nxt[++tot] = head[a]; n   ver[tot] = b; n   head[a] = tot; n   nxt[++tot] = head[b]; n   ver[tot] = a; n   head[b] = tot; n } n int n, f[maxn][2], g[maxn][2], h[maxn]; n inline void treedp(int x, int fat) { n   f[x][0] = f[x][1] = g[x][0] = g[x][1] = h[x] = 1; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (y == fat) continue; n     treedp(y, x); n     f[x][0] = (0ll + f[y][0] + f[y][1] + g[y][0] + g[y][1] + h[y]) % mod * n               f[x][0] % mod; n     f[x][1] = (0ll + f[y][0] + g[y][0] + g[y][1] + h[y]) % mod * f[x][1] % mod; n     h[x] = (0ll + g[y][0] + g[y][1] + h[y]) % mod * h[x] % mod; n   } n   g[x][0] = (f[x][0] + mod - h[x]) % mod; n   g[x][1] = (f[x][1] + mod - h[x]) % mod; n } n int main() { n   scanf( %d , &n); n   for (int i = 1, a, b; i < n; i++) scanf( %d%d , &a, &b), addedge(a, b); n   treedp(1, 0); n   int ans = (0ll + g[1][0] + g[1][1] + h[1] - 1 + mod) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int newMask[1000000][4]; n bool canMove[1000000][4]; n int mv[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n int len; n int getMove(int x, int y) { n   if (x == 1 && y == 0) return 0; n   if (x == 0 && y == 1) return 1; n   if (x == -1 && y == 0) return 2; n   return 3; n } n vector<pair<int, int> > getLoc(int mask) { n   vector<pair<int, int> > ret; n   ret.push_back(make_pair(0, 0)); n   for (int i = 0; i + 1 < len; i++) { n     int j = mask % 4; n     mask /= 4; n     int x = ret[ret.size() - 1].first + mv[j][0]; n     int y = ret[ret.size() - 1].second + mv[j][1]; n     ret.push_back(make_pair(x, y)); n   } n   reverse(ret.begin(), ret.end()); n   return ret; n } n int getMask(vector<pair<int, int> > loc) { n   int ret = 0; n   for (int i = 0; i + 1 < loc.size(); i++) { n     int x = loc[i].first - loc[i + 1].first; n     int y = loc[i].second - loc[i + 1].second; n     ret = ret * 4 + getMove(x, y); n   } n   return ret; n } n void precalc() { n   int num = len - 1; n   for (int i = 0; i < (1 << (2 * num)); i++) { n     vector<pair<int, int> > loc = getLoc(i); n     for (int x = 0; x < 4; x++) { n       bool ok = 1; n       for (int j = 1; j < loc.size(); j++) { n         if (mv[x][0] == loc[j].first && mv[x][1] == loc[j].second) ok = 0; n       } n       if (ok) { n         canMove[i][x] = true; n         vector<pair<int, int> > newLoc = loc; n         newLoc.erase(newLoc.begin()); n         newLoc.push_back(make_pair(mv[x][0], mv[x][1])); n         newMask[i][x] = getMask(newLoc); n       } n     } n   } n } n pair<int, int> l[10]; n int n, m; n char a[100][100]; n int dist[16][16][70000]; n bool vis[16][16][70000]; n int X, Y; n int ans; n int bfs(pair<int, int> v, int mask) { n   ans = 1000000000; n   queue<pair<int, int> > q1; n   queue<int> q2; n   q1.push(v); n   q2.push(mask); n   vis[v.first][v.second][mask] = 1; n   while (!q1.empty()) { n     v = q1.front(); n     q1.pop(); n     mask = q2.front(); n     q2.pop(); n     if (v == make_pair(X, Y)) ans = min(ans, dist[v.first][v.second][mask]); n     for (int x = 0; x < 4; x++) { n       if (!canMove[mask][x]) continue; n       pair<int, int> u = v; n       u.first += mv[x][0]; n       u.second += mv[x][1]; n       if (u.first < 0 || u.first >= n || u.second < 0 || u.second >= m || n           a[u.first][u.second] ==  # ) n         continue; n       int uMask = newMask[mask][x]; n       if (vis[u.first][u.second][uMask]) continue; n       dist[u.first][u.second][uMask] = 1 + dist[v.first][v.second][mask]; n       vis[u.first][u.second][uMask] = 1; n       q1.push(u); n       q2.push(uMask); n     } n   } n   if (ans == 1000000000) return -1; n   return ans; n } n void getInput() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] >=  1  && a[i][j] <=  9 ) n         len++, l[a[i][j] -  0 ] = make_pair(i, j); n       if (a[i][j] ==  @ ) X = i, Y = j; n     } n   } n   vector<pair<int, int> > fLoc; n   for (int i = len; i >= 1; i--) fLoc.push_back(l[i]); n   precalc(); n   int mask = getMask(fLoc); n   cout << bfs(fLoc[fLoc.size() - 1], mask) << endl; n } n int main() { getInput(); } n


#include <bits/stdc++.h> n using namespace std; n int a[120], n, t; n bool check(); n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     sort(a + 1, a + n + 1); n     if (check()) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n bool check() { n   for (int i = 1; i < n; i++) { n     if (a[i + 1] == a[i] || a[i + 1] == a[i] + 1) n       continue; n     else n       return false; n   } n   return true; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n string s; n int n, cnt1, cnt0, cntq, m, p; n char last0, last1; n int may01, may10; n int main() { n   cin >> s; n   n = s.size(); n   for (int i = 0; i < n; i++) n     if (s[i] ==  1 ) n       cnt1++; n     else if (s[i] ==  0 ) n       cnt0++; n     else n       cntq++; n   m = n / 2 + n % 2 - 1; n   p = n - m - 2; n   if (cnt1 <= m) puts( 00 ); n   if (m + 1 - cnt1 <= cntq && m + 1 >= cnt1) { n     int q1 = (m + 1 - cnt1); n     int q0 = cntq - q1; n     if (q0 + cnt0 == p + 1) { n       if (s[n - 1] ==  1  || (s[n - 1] ==  ?  && q1 > 0)) { n         may01 = 1; n       } n       if (s[n - 1] ==  0  || (s[n - 1] ==  ?  && q0 > 0)) { n         may10 = 1; n       } n       if (may01) puts( 01 ); n       if (may10) puts( 10 ); n     } n   } n   if (cnt0 <= p) puts( 11 ); n } n