#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n const long long mod = pow(10, 9) + 7; n const long long N = 3e5, M = N; n const long long inf = LLONG_MAX; n const long long infn = LLONG_MIN; n string convert(string s) { n   long long n = s.length(), i; n   while (true) { n     bool done = 0; n     for (auto it = s.begin(); it != s.end(); it++) { n       auto p = it; n       p++; n       if (p == s.end()) n         break; n       else if (*it ==  k  && *p ==  h ) { n         s.erase(it); n         it--; n         done = 1; n       } n     } n     if (!done) break; n   } n   string k =   ; n   for (long long i = 0; i < s.length(); i++) { n     if (s[i] ==  u ) { n       k +=  oo ; n     } else n       k += s[i]; n   } n   { n     string _s =  k ; n     replace(_s.begin(), _s.end(),  , ,    ); n     stringstream _ss(_s); n     istream_iterator<string> _it(_ss); n     err(_it, k); n   }; n   return k; n } n void iluzn() { n   long long n; n   cin >> n; n   long long k = n; n   set<string> st; n   while (n--) { n     string s; n     cin >> s; n     s = convert(s); n     st.insert(s); n   } n   cout << st.size() <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long t = 1; n   while (t--) iluzn(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e18; n int n, q; n int v[111111], c[111111]; n long long first[111111]; n long long calc(long long a, long long b) { n   int m1 = 0, m2 = 0; n   for (int i = 1; i <= n; ++i) first[i] = -inf; n   first[0] = 0; n   for (int i = 0; i < n; ++i) { n     int &cc = c[i], &vv = v[i]; n     long long cur = b * vv; n     if (first[cc] > -inf) { n       cur = max(cur, first[cc] + a * vv); n     } n     if (m1 != cc) { n       cur = max(cur, first[m1] + b * vv); n     } else if (m2 != cc) { n       cur = max(cur, first[m2] + b * vv); n     } n     if (cur > first[cc]) { n       first[cc] = cur; n       if (first[cc] > first[m1]) { n         m2 = m1, m1 = cc; n       } else if (first[cc] > first[m2] && cc != m1) { n         m2 = cc; n       } n     } n   } n   return first[m1]; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; ++i) scanf( %d , &v[i]); n   for (int i = 0; i < n; ++i) scanf( %d , &c[i]); n   while (q--) { n     int a, b; n     scanf( %d%d , &a, &b); n     cout << calc((long long)a, (long long)b) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > vp; n vector<long long> v; n set<long long> st; n long long ara[500005]; n map<string, long long> mp; n long long Set(long long N, long long pos) { return N = N | (1LL << pos); } n long long reset(long long N, long long pos) { return N = N & ~(1LL << pos); } n bool check(long long N, long long pos) { return (bool)(N & (1LL << pos)); } n vector<long long> e, o; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, n, i, j, p; n   cin >> t; n   while (t--) { n     cin >> n; n     v.clear(), vp.clear(); n     for (i = 0; i < n; i++) v.push_back(i + 1); n     long long a = n; n     for (i = n - 1; i >= 0; i--) { n       vp.push_back({a, i}); n       a = (a + i + 1) / 2; n     } n     cout << 2 << endl; n     for (i = 0; i < n - 1; i++) n       cout << vp[i].first <<     << vp[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Ev { n   int pos; n   bool st; n   Ev(int pos, bool st) : pos(pos), st(st) {} n   inline bool operator<(const Ev &rhs) const { return pos < rhs.pos; } n }; n int n, k; n vector<Ev> ev; n int f[200005], rf[200005]; n const int MOD = 1e9 + 7; n int c(int n, int k) { return (long long)f[n] * rf[k] % MOD * rf[n - k] % MOD; } n int binPow(long long x, int p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * x % MOD; n     p >>= 1; n     if (p) x = x * x % MOD; n   } n   return (int)res; n } n int main() { n   f[0] = rf[0] = 1; n   for (int i = 1; i < 200005; ++i) { n     f[i] = (long long)f[i - 1] * i % MOD; n     rf[i] = binPow(f[i], MOD - 2); n   } n   scanf( %d%d , &n, &k); n   for (int i = 0; i < (int)(n); ++i) { n     int from, to; n     scanf( %d%d , &from, &to); n     ev.push_back(Ev(from, true)); n     ev.push_back(Ev(to + 1, false)); n   } n   int pre = -1e9 - 1234; n   sort(ev.begin(), ev.end()); n   int cnt = 0; n   int ans = 0; n   for (Ev e : ev) { n     if (cnt >= k) { n       ans = (ans + (long long)c(cnt, k) * (e.pos - pre)) % MOD; n     } n     pre = e.pos; n     if (e.st) n       ++cnt; n     else n       --cnt; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, i; n   cin >> N; n   char ch; n   int o = 0, n = 0, e = 0, z = 0, r = 0; n   for (i = 0; i < N; i++) { n     cin >> ch; n     if (ch ==  o ) n       o++; n     else if (ch ==  n ) n       n++; n     else if (ch ==  e ) n       e++; n     else if (ch ==  z ) n       z++; n     else n       r++; n   } n   int m1, m2; n   m1 = min(o, min(n, e)); n   o -= m1; n   e -= m1; n   while (m1--) cout <<  1  ; n   m2 = min(z, min(e, min(r, o))); n   while (m2--) cout <<  0  ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 9; n const int Mod = 1000000007; n long long n, d, b; n long long a[N]; n int main() { n   cin >> n >> d >> b; n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n   } n   for (int i = 1; i <= n; i++) a[i] = a[i - 1] + a[i]; n   long long L = 0, R = 0, Lans = 0, Rans = 0; n   for (int i = 1; i <= (n + 1) / 2; i++) { n     if (a[min(n, (d + 1) * i)] >= (L + 1) * b) n       L++; n     else n       Lans++; n   } n   for (int i = n; i > n - (n + 1) / 2; i--) { n     if (a[n] - a[max(0ll, i - (n - i + 1) * d - 1)] >= (R + 1) * b) n       R++; n     else n       Rans++; n   } n   cout << max(Lans, Rans) << endl; n } n


#include <bits/stdc++.h> n int solve(int n, int m, std::vector<int> const& costs) { n   int chosen_row_minimum = -1; n   for (int i = 0; i < n; ++i) { n     auto min = n         std::min_element(costs.cbegin() + i * m, costs.cbegin() + i * m + m); n     if (*min > chosen_row_minimum) { n       chosen_row_minimum = *min; n     } n   } n   return chosen_row_minimum; n } n int main() { n   int n, m; n   std::cin >> n >> m; n   std::vector<int> costs; n   costs.reserve(n * m); n   costs.insert(costs.end(), std::istream_iterator<int>(std::cin), n                std::istream_iterator<int>()); n   assert(n * m == (int)costs.size()); n   int chosen_row_minimum = solve(n, m, costs); n   printf( %d n , chosen_row_minimum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 25; n const long long INF = 1e15 + 7; n const long long mod = 998244353; n long long dp[N]; n long long binpow(long long a, long long b) { n   a %= mod; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long mult(long long a, long long b) { return (a % mod * b % mod) % mod; } n long long add(long long a, long long b) { n   a += b; n   if (a >= mod) return a - mod; n   return a; n } n long long sub(long long a, long long b) { n   a -= b; n   if (a < 0) return a + mod; n   return a; n } n long long inverse(long long a) { return binpow(a, mod - 2); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     vector<pair<long long, long long> > ans; n     char a[n + 2][m + 3]; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) cin >> a[i][j]; n     } n     for (long long i = 0; i < n - 1; i++) { n       for (long long j = 0; j < m - 1; j++) { n         vector<pair<long long, long long> > z, o; n         if (a[i][j] ==  0 ) n           z.push_back(make_pair(i, j)); n         else n           o.push_back(make_pair(i, j)); n         if (a[i][j + 1] ==  0 ) n           z.push_back(make_pair(i, j + 1)); n         else n           o.push_back(make_pair(i, j + 1)); n         if (a[i + 1][j] ==  0 ) n           z.push_back(make_pair(i + 1, j)); n         else n           o.push_back(make_pair(i + 1, j)); n         if (a[i + 1][j + 1] ==  0 ) n           z.push_back(make_pair(i + 1, j + 1)); n         else n           o.push_back(make_pair(i + 1, j + 1)); n         if (o.size() == 0) continue; n         if (o.size() == 4) { n           ans.push_back(make_pair(i, j)); n           ans.push_back(make_pair(i, j + 1)); n           ans.push_back(make_pair(i + 1, j)); n           ans.push_back(make_pair(i + 1, j + 1)); n           ans.push_back(make_pair(i + 1, j)); n           ans.push_back(make_pair(i, j + 1)); n           ans.push_back(make_pair(i, j)); n           ans.push_back(make_pair(i, j + 1)); n           ans.push_back(make_pair(i + 1, j + 1)); n           ans.push_back(make_pair(i, j)); n           ans.push_back(make_pair(i + 1, j)); n           ans.push_back(make_pair(i + 1, j + 1)); n         } n         if (o.size() == 3) { n           for (long long k = 0; k < 3; k++) ans.push_back(o[k]); n         } n         if (o.size() == 2) { n           ans.push_back(o[0]); n           ans.push_back(z[0]); n           ans.push_back(z[1]); n           ans.push_back(o[1]); n           ans.push_back(z[0]); n           ans.push_back(z[1]); n         } n         if (o.size() == 1) { n           ans.push_back(o[0]); n           ans.push_back(z[1]); n           ans.push_back(z[2]); n           pair<long long, long long> p = o[0]; n           o.pop_back(); n           o.push_back(z[1]); n           o.push_back(z[2]); n           z.pop_back(); n           z.pop_back(); n           z.push_back(p); n           ans.push_back(o[0]); n           ans.push_back(z[0]); n           ans.push_back(z[1]); n           ans.push_back(o[1]); n           ans.push_back(z[0]); n           ans.push_back(z[1]); n         } n         a[i][j] =  0 ; n         a[i + 1][j] =  0 ; n         a[i][j + 1] =  0 ; n         a[i + 1][j + 1] =  0 ; n       } n     } n     cout << ans.size() / 3 << endl; n     for (long long i = 0; i < ans.size(); i += 3) { n       cout << ans[i].first + 1 <<     << ans[i].second + 1 <<     n            << ans[i + 1].first + 1 <<     << ans[i + 1].second + 1 <<     n            << ans[i + 2].first + 1 <<     << ans[i + 2].second + 1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, M = 30; n int a[M], b[M]; n char ans[26 * N]; n int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); } n int main() { n   ios::sync_with_stdio(0); n   int n, cnt = 0, sum = 0, pos = 0, qaq = 0, tmp = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     cnt += a[i] % 2; n     sum += a[i]; n   } n   if (cnt > 1) { n     cout << 0 << endl; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= a[i]; j++) cout << char( a  + i - 1); n     return 0; n   } n   for (int i = 1; i <= n; i++) qaq = gcd(a[i], qaq); n   if (!cnt) qaq /= 2; n   for (int i = 1; i <= n; i++) { n     a[i] /= qaq; n     if (a[i] % 2) tmp = i; n   } n   sum /= qaq; n   for (int i = 1; i <= n; i++) { n     if (tmp == i) continue; n     for (int j = 1; j <= a[i] / 2; j++) ans[++pos] =  a  + i - 1; n   } n   for (int i = 1; i <= n; i++) { n     if (tmp != i) continue; n     for (int j = 1; j <= a[i]; j++) ans[++pos] =  a  + i - 1; n   } n   for (int i = sum; i >= sum / 2 + 1; i--) ans[i] = ans[sum - i + 1]; n   if (!cnt) n     printf( %d n , qaq * 2); n   else n     printf( %d n , qaq); n   for (int i = 1; i <= qaq; i++) printf( %s , ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const bool print = false; n const int N = 5e5 + 7; n int n, m; n vector<int> gr[N]; n int cyk[N], odl[N], par[N]; n vector<int> mycyk[N]; n bool odw[N]; n int ans[N]; n vector<int> child[N]; n int dpd[N], dpu[N]; n void dfs1(int v) { n   odw[v] = 1; n   for (int i : gr[v]) { n     if (i == par[v]) continue; n     if (odw[i]) { n       if (odl[i] < odl[v]) { n         int w = v; n         while (w != i) { n           cyk[w] = i; n           mycyk[i].push_back(w); n           w = par[w]; n         } n         cyk[i] = i; n         mycyk[i].push_back(i); n         reverse((mycyk[i]).begin(), (mycyk[i]).end()); n       } n       continue; n     } n     odl[i] = odl[v] + 1; n     par[i] = v; n     dfs1(i); n     if (cyk[i] != cyk[v] || !cyk[v]) child[v].push_back(i); n   } n } n void dfs2(int v) { n   for (int i = (1); i <= ((int)mycyk[v].size() - 1); i++) { n     dfs2(mycyk[v][i]); n     dpd[v] = max(dpd[v], dpd[mycyk[v][i]] + min(i, (int)mycyk[v].size() - i)); n   } n   for (int i : child[v]) { n     dfs2(i); n     dpd[v] = max(dpd[v], dpd[i] + 1); n   } n } n deque<int> kol; n void clearit() { kol.clear(); } n void add(int v) { n   while (!kol.empty() && kol.back() < v) kol.pop_back(); n   kol.push_back(v); n } n void remove(int v) { n   if (kol.front() == v) kol.pop_front(); n } n int war[N]; n void solve(int v) { n   int k = mycyk[v].size(); n   clearit(); n   for (int i = (0); i <= (k - 1); i++) { n     war[i] = max(dpd[mycyk[v][i]], dpu[mycyk[v][i]]); n     if (!i) { n       war[0] = dpu[mycyk[v][i]]; n       for (int j : child[v]) war[0] = max(war[0], dpd[j] + 1); n     } n   } n   for (int i = (k / 2); i >= (1); i--) add(war[k - i] + i); n   for (int i = (0); i <= (k - 1); i++) { n     int x = mycyk[v][i]; n     if (i) dpu[x] = max(dpu[x], kol.front() + i); n     remove(war[(i - k / 2 + k) % k] - (i - k / 2)); n     add(war[i] - i); n   } n   clearit(); n   for (int i = (k / 2 - 1); i >= (0); i--) add(war[i] + k + i); n   for (int i = (k - 1); i >= (1); i--) { n     int x = mycyk[v][i]; n     dpu[x] = max(dpu[x], kol.front() - i); n     remove(war[(i + k / 2) % k] + (i + k / 2)); n     add(war[i] + i); n   } n } n void dfs3(int v) { n   priority_queue<int> pri; n   pri.push(dpu[v]); n   if (cyk[v] == v) { n     solve(v); n     int j = 0; n     for (int i : mycyk[v]) n       if (i != v) { n         dfs3(i); n         j++; n         pri.push(dpd[i] + min(j, (int)mycyk[v].size() - j)); n       } n   } n   for (int i : child[v]) pri.push(dpd[i] + 1); n   int x = pri.top(); n   pri.pop(); n   int y = -1; n   if (!pri.empty()) y = pri.top(); n   for (int i : child[v]) { n     if (dpd[i] + 1 != x) n       dpu[i] = x + 1; n     else n       dpu[i] = y + 1; n     dfs3(i); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (1); i <= (m); i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     gr[a].push_back(b); n     gr[b].push_back(a); n   } n   odl[1] = 1; n   dfs1(1); n   dfs2(1); n   dfs3(1); n   for (int i = (1); i <= (n); i++) ans[i] = max(dpd[i], dpu[i]); n   for (int i = (1); i <= (n); i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int a[n + 2]; n   int pos[n + 2]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     pos[a[i]] = i; n   } n   int swap = 0; n   for (int i = 1; i <= n; i++) { n     int p = pos[i]; n     if (p == i) continue; n     int prev = a[i]; n     a[i] = i; n     pos[prev] = p; n     a[p] = prev; n     pos[i] = i; n     swap++; n   } n   if (n % 2) { n     if (swap % 2) { n       cout <<  Petr n ; n     } else { n       cout <<  Um_nik n ; n     } n   } else { n     if (swap % 2) { n       cout <<  Um_nik n ; n     } else { n       cout <<  Petr n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int FFTMOD = 119 << 23 | 1; n const int INF = (int)1e9 + 23111992; n const long long LINF = (long long)1e18 + 23111992; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return val < a ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline unsigned long long isqrt(unsigned long long k) { n   unsigned long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); n inline int mrand() { return abs((int)mt()); } n inline int mrand(int k) { return abs((int)mt()) % k; } n void chemthan() { n   int C = 1e5 + 5; n   vector<int> sp(C); n   for (int i = (1); i < (C); ++i) sp[i] = i; n   for (int i = (2); i < (C); ++i) n     if (sp[i] == i) { n       for (int j = i + i; j < C; j += i) { n         chkmin(sp[j], i); n       } n     } n   vector<int> mob(C, 1); n   for (int i = (2); i < (C); ++i) { n     int t = i; n     int pv = -1; n     while (1 < t) { n       if (pv == sp[t]) { n         mob[i] = 0; n       } n       mob[i] *= -1; n       pv = sp[t]; n       t /= sp[t]; n     } n   } n   vector<vector<int> > dvs(C); n   vector<vector<int> > dvs2(C); n   for (int i = (1); i < (C); ++i) { n     for (int j = i; j < C; j += i) { n       dvs[j].push_back(i); n       if (mob[j / i]) dvs2[j].push_back(i); n     } n   } n   int n; n   cin >> n; n   vector<int> a(n); n   vector<vector<int> > g(C); n   for (int i = (0); i < (n); ++i) { n     cin >> a[i]; n     for (int d : dvs[i + 1]) { n       g[d].push_back(a[i]); n     } n   } n   vector<int> c(C); n   vector<int> f(C); n   vector<int> x(C); n   vector<int> y(C); n   int res = 0; n   for (int i = (1); i < (C); ++i) n     if (int((g[i]).size())) { n       vector<int> tmp2; n       vector<int> tmp; n       for (int u : g[i]) { n         c[u] = 0; n       } n       for (int u : g[i]) { n         if (!c[u]++) { n           tmp2.push_back(u); n         } n       } n       for (int u : tmp2) { n         for (int d : dvs[u]) { n           if (!f[d]) { n             tmp.push_back(d); n           } n           f[d] += c[u]; n         } n       } n       for (int u : tmp) { n         f[u] = mult(f[u], f[u]); n       } n       int sum = 0; n       for (int u : tmp) { n         for (int v : dvs2[u]) { n           if (mob[u / v]) { n             if (mob[u / v] == 1) { n               addmod(x[v], f[u]); n             } else { n               submod(x[v], f[u]); n             } n           } n         } n       } n       for (int u : tmp) { n         addmod(sum, mult(x[u], u)); n       } n       y[i] = sum; n       for (int u : tmp) f[u] = x[u] = 0; n     } n   for (int i = (C)-1; i >= (1); --i) { n     for (int j = i + i; j < C; j += i) { n       submod(y[i], y[j]); n     } n     addmod(res, mult(i, y[i])); n   } n   cout << res <<   n ; n } n int32_t main(int32_t argc, char* argv[]) { n   ios_base::sync_with_stdio(0), cin.tie(0); n   if (argc > 1) { n     assert(freopen(argv[1],  r , stdin)); n   } n   if (argc > 2) { n     assert(freopen(argv[2],  wb , stdout)); n   } n   chemthan(); n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n int gcd(int n, int m) { n   if (m == 0) return n; n   return gcd(m, n % m); n } n int lcm(int n, int m) { return n * m / gcd(n, m); } n ll fact(ll n) { n   if (n == 1 or n == 0) n     return 1; n   else n     return ((n % 1000000007) * (fact(n - 1) % 1000000007)) % 1000000007; n } n template <typename T> n T modpow(T base, T exp) { n   base %= 1000000007; n   T result = 1; n   while (exp > 0) { n     if (exp & 1) (result *= base) %= 1000000007; n     (base *= base) %= 1000000007; n     exp >>= 1; n   } n   return result; n } n int bin_search(vector<int>& a, int ele) { n   int lo = 0, hi = a.size() - 1; n   while (lo <= hi) { n     int mid = lo + (hi - lo) / 2; n     if (mid == 0 or mid == a.size() - 1) n       return mid; n     else if (a[mid] >= ele and a[mid - 1] < ele) n       return mid; n     else if (a[mid] >= ele) n       hi = mid - 1; n     else n       lo = mid + 1; n   } n } n bool ifPrime(int n) { n   for (int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n void solve(int testno) { n   string s; n   cin >> s; n   string orig = s; n   bool no = 1; n   for (char& c : s) { n     if (c ==  9  and no) { n       no = 0; n       continue; n     } n     no = 0; n     if (c -  0  >= 5) { n       c =  0  + ( 9  - c); n     } n   } n   cout << s; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin.exceptions(ios::badbit | ios::failbit); n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Node { n  public: n   Node *prev, *next; n   int k; n   Node(int K) { n     k = K; n     prev = next = NULL; n   }; n   void insert(int K) { n     Node *n = new Node(K); n     n->prev = this; n     if (this->next == NULL) n       this->next = n; n     else { n       Node *t = this->next; n       this->next = n; n       n->next = t; n       t->prev = n; n     } n   }; n   void insertBack(int K) { n     Node *n = new Node(K); n     n->next = this; n     if (this->prev == NULL) n       this->prev = n; n     else { n       Node *t = this->prev; n       this->prev = n; n       n->prev = t; n       t->next = n; n     } n   }; n   void del() { n     Node *p = this->prev, *n = this->next; n     if (p) p->next = n; n     if (n) n->prev = p; n     delete this; n   }; n   void delNext() { n     if (this->next) this->next->del(); n   }; n }; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   cin >> T; n   while (T) { n     T--; n     string s; n     cin >> s; n     Node *head = new Node(s[0] -  0 ); n     Node *curr = head; n     for (int i = 1; i < s.size(); i++) { n       curr->insert(s[i] -  0 ); n       curr = curr->next; n     } n     curr = head->next; n     Node *cache = head; n     while (curr) { n       if (cache->k % 2 != curr->k % 2) { n         while (cache->k < curr->k) cache = cache->next; n         if (cache == curr) { n           curr = curr->next; n           continue; n         } n         cache->insertBack(curr->k); n         if (curr->next) { n           curr = curr->next; n           curr->prev->del(); n         } else { n           curr = curr->prev; n           curr->next->del(); n           curr = NULL; n         } n       } else { n         curr = curr->next; n       } n     } n     while (cache->prev) cache = cache->prev; n     while (cache) { n       cout << cache->k; n       cache = cache->next; n       if (cache && cache->prev) cache->prev->del(); n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long n1, long long n2) { n   if (!n1) return n2; n   if (!n2) return n1; n   if (n1 % n2 == 0) return n2; n   return gcd(n2, n1 % n2); n } n long long powmod(long long base, long long exponent) { n   base %= 1000000007; n   long long ans = 1; n   while (exponent) { n     if (exponent & 1) ans = (ans * base) % 1000000007; n     base = (base * base) % 1000000007; n     exponent /= 2; n   } n   ans %= 1000000007; n   return ans; n } n int arr[1000100 + 1]; n int cnt[1000100 + 1]; n int pw[1000100 + 1]; n int dp[1000100 + 1]; n int main() { n   int n, i, j, k; n   pw[0] = 1; n   for (i = 1; i <= 1000100; i++) { n     pw[i] = (2 * pw[i - 1]) % 1000000007; n   } n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     cnt[arr[i]]++; n   } n   for (i = 1; i <= 1000100; i++) n     for (j = i; j <= 1000100; j += i) dp[i] += cnt[j]; n   for (i = 1; i <= 1000100; i++) n     if (dp[i]) { n       dp[i] = (1LL * dp[i] * pw[dp[i] - 1]) % 1000000007; n     } n   long long answer = 0; n   for (i = 1000100; i >= 2; i--) { n     for (j = 2 * i; j <= 1000100; j += i) { n       dp[i] -= dp[j]; n       if (dp[i] < 0) dp[i] += 1000000007; n     } n     answer = (answer + 1LL * i * dp[i]) % 1000000007; n   } n   printf( %lld n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void input() {} n const int N = 1e5 + 4, mod = 1e9 + 7; n int t, k, l, r; n long long dp[N]; n int solve(int i = 1e5) { n   if (i < 0) return 0; n   if (i == 0) return 1; n   long long &ret = dp[i]; n   if (~ret) return ret; n   ret = solve(i - 1) % mod; n   ret += solve(i - k) % mod; n   return ret % mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   input(); n   cin >> t >> k; n   memset(dp, -1, sizeof dp); n   int x = solve(); n   dp[0] = 0; n   for (int i = 1; i <= 1e5; i++) dp[i] += dp[i - 1]; n   while (t--) { n     cin >> l >> r; n     cout << (dp[r] - dp[l - 1]) % mod << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> x, y; n int vx[200006], vy[200006]; n set<int> mx, my; n set<int>::iterator it1, it2; n void gao(set<int> &t1, set<int> &t2, int *v, int x) { n   if (!t1.count(x)) { n     it1 = t1.lower_bound(x); n     it2 = it1, it1--; n     int l = *it2 - *it1; n     v[l]--; n     if (v[l] == 0) t2.erase(l); n     t2.insert(*it2 - x), v[*it2 - x]++; n     t2.insert(x - *it1), v[x - *it1]++; n     t1.insert(x); n   } n } n int main() { n   int w, h, n, t, T; n   while (~scanf( %d%d%d , &w, &h, &n)) { n     memset(vx, 0, sizeof(vx)); n     memset(vy, 0, sizeof(vy)); n     char c; n     x.clear(), y.clear(); n     mx.clear(), my.clear(); n     x.insert(w), y.insert(h); n     x.insert(0), y.insert(0); n     mx.insert(w), my.insert(h); n     vx[w]++, vy[h]++; n     while (n--) { n       cin >> c >> t; n       if (c ==  V ) n         gao(x, mx, vx, t); n       else n         gao(y, my, vy, t); n       it1 = mx.end(), it2 = my.end(); n       it1--, it2--; n       long long x1 = (*it1) * 1ll, x2 = (*it2) * 1ll; n       long long ans = x1 * x2; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[55]; n int main() { n   int n, i, j, k, ans, x; n   int len; n   while (cin >> a) { n     ans = 0; n     x = 0; n     len = strlen(a); n     for (i = 0; i < len; i++) { n       for (j = len - 1; j > i; j--) { n         for (k = i; k <= j; k++) { n           if (a[k] != a[j - k + i]) break; n         } n         if (k != j + 1) { n           ans = max(ans, j - i); n         } n       } n     } n     if (ans != 0) n       cout << ans + 1 << endl; n     else n       cout <<  0  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 * 1000 + 17, sq = 1000; n int n, a[MAXN]; n vector<pair<int, int> > p; n bool cmp(int i, int j) { n   i--, j--; n   if (p[i].first / sq != p[j].first / sq) return p[i].first < p[j].first; n   return ((p[i].first / sq) % 2 ? p[i].second > p[j].second n                                 : p[i].second < p[j].second); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     p.push_back({x, y}); n   } n   iota(a + 1, a + n + 1, 1); n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cin.get(); n   string s; n   deque<pair<string, bool>> storage; n   while (n--) { n     getline(cin, s); n     storage.push_back(pair<string, bool>(s, true)); n   } n   deque<pair<string, bool>>::iterator it1, it2; n   bool flag; n   for (it1 = storage.begin(); it1 < storage.end(); it1++) { n     if (it1->second == true) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n     } n     for (it2 = it1 + 1; it2 < storage.end(); it2++) { n       if (it1->first == it2->first) { n         it2->second = false; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using min_pq = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using max_pq = priority_queue<T>; n const int inf = 2e9 + 5; n const long long l_inf = 2e18 + 5; n const int mod_v = 1e9 + 7; n const int max_n = 1e5 + 5; n const int dx[4] = {-1, 0, 1, 0}; n const int dy[4] = {0, 1, 0, -1}; n template <typename T> n T gcd(T a, T b) { n   while (b) { n     T temp = a % b; n     a = b; n     b = temp; n   } n   return a; n } n template <typename T> n tuple<T, T, T> egcd(T a, T b) { n   T x1 = 1, x2 = 0, y1 = 0, y2 = 1; n   while (b) { n     T q = a / b, r = a % b; n     T new_x = x1 - q * x2, new_y = y1 - q * y2; n     x1 = x2, y1 = y2, x2 = new_x, y2 = new_y; n     a = b, b = r; n   } n   return make_tuple(a, x1, y1); n } n inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n template <typename T> n inline T mod(T a, T b = mod_v) { n   return (a % b + b) % b; n } n template <typename T> n inline T mod_inv(T a, T b = mod_v) { n   return mod(get<1>(egcd(a, b)), b); n } n template <typename T> n inline T sum(T a, T b, T m = mod_v) { n   return mod(mod(a, m) + mod(b, m), m); n } n template <typename T> n inline T difference(T a, T b, T m = mod_v) { n   return mod(mod(a, m) - mod(b, m), m); n } n inline long long product(long long a, long long b, long long m = mod_v) { n   return mod(mod(a, m) * mod(b, m), m); n } n inline long long quotient(long long a, long long b, long long m = mod_v) { n   return mod(mod(a, m) * mod_inv(b, m), m); n } n template <typename T, typename T2> n ostream &operator<<(ostream &s, const pair<T, T2> &p) { n   return s << p.first <<     << p.second <<    ; n } n template <typename T, typename T2> n istream &operator>>(istream &s, pair<T, T2> &p) { n   return s >> p.first >> p.second; n } n template <typename T> n ostream &operator<<(ostream &s, const vector<T> &v) { n   for (auto it : v) s << it <<    ; n   return s; n } n template <typename T> n istream &operator>>(istream &s, vector<T> &v) { n   for (auto it = (v).begin(), it_ = (v).end(); it != it_; ++it) s >> *it; n   return s; n } n template <typename T> n void read_range(T beg, T end) { n   while (beg != end) cin >> *beg++; n } n template <typename T> n void print_range(T beg, T end) { n   while (beg != end) cout << *beg++ <<    ; n } n struct reader { n   template <typename T> n   reader &operator,(T &v) { n     cin >> v; n     return *this; n   } n } rdr; n struct debugger { n   template <typename T> n   debugger &operator,(const T &v) { n     cerr << v <<  ,  ; n     return *this; n   } n } dbg; n map<pair<int, int>, long long> memo; n long long ar[max_n * 10], cnt[max_n * 10], cnt2[10 * max_n], comp[10 * max_n]; n vector<pair<int, int>> v; n long long f(int cur, int k) { n   if (k == 0) return 1; n   if (cur == -1) return 0; n   if (memo.find(make_pair(cur, k)) != memo.end()) n     return memo[make_pair(cur, k)]; n   return memo[make_pair(cur, k)] = n              (f(cur - 1, k) + (cnt[cur] * f(cur, k - 1)) % mod_v) % mod_v; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, l, k; n   rdr, n, l, k; n   ; n   read_range(ar, ar + n); n   for (int i = 0; i < n; ++i) v.emplace_back(ar[i], i); n   sort((v).begin(), (v).end()); n   int sz = 0; n   for (int i = 0; i < n; ++i) { n     while (i + 1 < n and v[i].first == v[i + 1].first) { n       comp[v[i].second] = sz; n       ++cnt[sz]; n       ++i; n     } n     comp[v[i].second] = sz; n     ++cnt[sz]; n     ++sz; n   } n   long long rem = l % n, seg = l / n; n   for (int i = 0; i < rem; ++i) ++cnt2[comp[i]]; n   long long ans = 0; n   for (int i = 1; i <= min(k, seg); ++i) { n     ans += ((seg - i + 1) % mod_v) * f(sz - 1, i); n     ans %= mod_v; n   } n   for (int i = 1; i <= min(k, seg + 1); ++i) { n     for (int j = 0; j < sz; ++j) { n       ans += cnt2[j] * f(j, i - 1); n       ans %= mod_v; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> V[100045]; n double size[100045]; n int par[100045]; n double ans[100045]; n void dfs(int v) { n   size[v] = 1; n   for (int i = 0; i < V[v].size(); i++) dfs(V[v][i]); n   for (int i = 0; i < V[v].size(); i++) size[v] += size[V[v][i]]; n } n void bfs(int st) { n   queue<int> q; n   q.push(st); n   while (!q.empty()) { n     int top = q.front(); n     q.pop(); n     if (top != 1) n       ans[top] = n           ans[par[top]] + 1.00 + 0.5 * (size[par[top]] - size[top] - 1.0); n     for (int i = 0; i < V[top].size(); i++) q.push(V[top][i]); n   } n } n int main() { n   ans[1] = 1.0; n   int n; n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     int u; n     scanf( %d , &u); n     V[u].push_back(i); n     par[i] = u; n   } n   dfs(1); n   bfs(1); n   for (int i = 1; i <= n; i++) n     cout << fixed << setprecision(7) << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = (int)1e8; n vector<vector<int>> groupCosts; n vector<int> oldDP, newDP; n void solveDP(int L, int R, int L1, int R1) { n   if (R < L) return; n   int mid = (L + R) / 2; n   int res = oo, pos = L1; n   for (int i = L1; i <= min(mid, R1); i++) { n     int now = groupCosts[i][mid]; n     int curCost = now + (i ? oldDP[i - 1] : 0); n     if (curCost < res) { n       res = curCost; n       pos = i; n     } n   } n   newDP[mid] = res; n   solveDP(L, mid - 1, L1, pos); n   solveDP(mid + 1, R, pos, R1); n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<vector<int>> costs(n, vector<int>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> costs[i][j]; n       if (j) costs[i][j] += costs[i][j - 1]; n     } n   } n   groupCosts.resize(n, vector<int>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       groupCosts[i][j] += groupCosts[i][j - 1]; n       groupCosts[i][j] += costs[j][j - 1] - (i ? costs[j][i - 1] : 0); n     } n   } n   oldDP.resize(n, oo); n   newDP.resize(n, oo); n   for (int curK = 0; curK < k; curK++) { n     solveDP(curK, n - 1, 0, n - 1); n     swap(oldDP, newDP); n   } n   cout << oldDP[n - 1] <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t, n, i, x; n   cin >> t; n   while (t--) { n     cin >> n; n     i = n - 1; n     x = n; n     cout <<  2  << endl; n     while (i != 0) { n       cout << i <<     << x << endl; n       x = ceil((i + x) / 2.0); n       i--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class DS { n  public: n   DS(int rows, int cols) : _rows(rows), _cols(cols), _ans(0) { n     int n = rows + cols; n     _p.resize(n, -1); n     _r.resize(n, 0); n     _nr.resize(n, 0); n     _nc.resize(n, 0); n     for (int i = 0; i < rows; ++i) _nr[i] = 1; n     for (int i = 0; i < cols; ++i) _nc[rows + i] = 1; n   } n   long long ans() { return _ans; } n   int find(int i) { n     while (_p[i] >= 0) i = _p[i]; n     return i; n   } n   void merge(int r, int c) { n     int i = find(r); n     int j = find(_rows + c); n     if (i == j) return; n     if (_r[i] > _r[j]) swap(i, j); n     _rb.push_back({i, j, _r[j]}); n     _p[i] = j; n     if (_r[i] == _r[j]) ++_r[j]; n     _ans += (long long)_nr[i] * _nc[j] + (long long)_nc[i] * _nr[j]; n     _nr[j] += _nr[i]; n     _nc[j] += _nc[i]; n   } n   int current_snapshot() { return _rb.size(); } n   void rollback(int snapshot) { n     while (_rb.size() > snapshot) { n       int i = _rb.back().i; n       int j = _rb.back().j; n       int r = _rb.back().r; n       _rb.pop_back(); n       _p[i] = -1; n       _r[j] = r; n       _nr[j] -= _nr[i]; n       _nc[j] -= _nc[i]; n       _ans -= (long long)_nr[i] * _nc[j] + (long long)_nc[i] * _nr[j]; n     } n   } n  n  private: n   int _rows, _cols; n   vector<int> _p, _r, _nr, _nc; n   struct rb_t { n     int i, j, r; n   }; n   vector<rb_t> _rb; n   long long _ans; n }; n class Solution { n  public: n   Solution(vector<pair<int, int>> const& p, int mx, int my) n       : _n(p.size()), _p(p), _seg(4 * _n), _ds(mx + 1, my + 1) {} n   void solve() { n     map<pair<int, int>, int> t; n     for (int i = 0; i < _n; ++i) { n       auto it = t.find(_p[i]); n       if (it == t.end()) { n         t[_p[i]] = i; n         continue; n       } n       int j = it->second; n       t.erase(it); n       add_pt(0, 0, _n, j, i, _p[i]); n     } n     for (const auto& ti : t) { n       add_pt(0, 0, _n, ti.second, _n, ti.first); n     } n     dfs(0, 0, _n); n   } n  n  private: n   void add_pt(int u, int a, int b, int A, int B, const pair<int, int>& pt) { n     if (a >= B || A >= b) return; n     if (A <= a && b <= B) { n       _seg[u].push_back(pt); n       return; n     } n     int c = (a + b) >> 1; n     add_pt(2 * u + 1, a, c, A, B, pt); n     add_pt(2 * u + 2, c, b, A, B, pt); n   } n   void dfs(int u, int a, int b) { n     int snapshot = _ds.current_snapshot(); n     for (const pair<int, int>& pt : _seg[u]) _ds.merge(pt.first, pt.second); n     if (a + 1 == b) { n       cout << _ds.ans() <<    ; n     } else { n       int c = (a + b) >> 1; n       dfs(2 * u + 1, a, c); n       dfs(2 * u + 2, c, b); n     } n     _ds.rollback(snapshot); n   } n  n  private: n   int _n; n   vector<pair<int, int>> const& _p; n   vector<vector<pair<int, int>>> _seg; n   DS _ds; n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> q; n   vector<pair<int, int>> p(q); n   int mx = 0, my = 0; n   for (pair<int, int>& pi : p) { n     cin >> pi.first >> pi.second; n     mx = max(mx, pi.first); n     my = max(my, pi.second); n   } n   Solution(p, mx, my).solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a; n   cin >> a; n   if (a[1] ==  8  || a[1] ==  1 ) { n     if (a[0] ==  a  || a[0] ==  h ) n       cout << 3 << endl; n     else n       cout << 5 << endl; n     return 0; n   } n   if (a[0] ==  a  || a[0] ==  h ) n     cout << 5 << endl; n   else n     cout << 8 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, b[110][10010], i, j, k, minn, l, p, w, ind; n string a[10005]; n int main() { n   cin >> n >> m; n   p = false; n   w = false; n   for (i = 0; i < n; i++) { n     k = -1; n     cin >> a[i]; n     for (j = 0; j < m; j++) { n       if (a[i][j] ==  1 ) { n         p = true; n         b[i][j] = 0; n         k = 0; n         k++; n         ind = j; n       } else { n         b[i][j] = k; n         if (k > 0) k++; n       } n     } n     j = 0; n     while (b[i][j] != 0) { n       b[i][j] = m - ind + j; n       j++; n     } n     if (p == false) { n       w = true; n       break; n     } n     p = false; n     k = -1; n     for (j = m - 1; j >= 0; j--) { n       if (a[i][j] ==  1 ) { n         b[i][j] = 0; n         k = 0; n         k++; n         ind = j; n       } else { n         if (b[i][j] < 0 || (k < b[i][j] && k >= 0)) b[i][j] = k; n         if (k > 0) k++; n       } n     } n     j = m - 1; n     while (b[i][j] != 0) { n       if (b[i][j] > ind + m - j) b[i][j] = ind + m - j; n       j--; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       b[i][j] = min(b[i][j], m - b[i][j]); n     } n   } n   minn = 9999999; n   for (i = 0; i < m; i++) { n     l = 0; n     for (j = 0; j < n; j++) l = l + b[j][i]; n     if (l < minn) minn = l; n   } n   if (w == true) n     cout <<  -1 ; n   else n     cout << minn; n } n


#include <bits/stdc++.h> n using namespace std; n char s[20][20]; n long long n; n long long tmp1[15][16400][65]; n long long tmp2[15][16400][65]; n long long visit[20]; n long long a[20]; n long long k; n long long fst; n long long all; n void dfs(long long pos, long long msk, long long ans) { n   if (pos == k) { n     tmp1[fst][msk][ans]++; n   } n   if (pos == n - k) { n     tmp2[visit[pos - 1]][msk][ans]++; n     return; n   } n   for (long long i = 0; i < n; i++) { n     if (msk & (1 << i)) continue; n     visit[pos] = i; n     dfs(pos + 1, msk | (1 << i), (ans << 1) | s[i][visit[pos - 1]]); n   } n } n long long cnt[20000]; n signed main() { n   cin >> n; n   all = (1 << n) - 1; n   k = n / 2; n   long long allk = (1 << (k - 1)); n   long long allkk = (1 << (n - k - 1)); n   for (long long i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) s[i][j] -=  0 ; n   for (fst = 0; fst < n; fst++) { n     visit[0] = fst; n     dfs(1, 1 << fst, 0); n   } n   for (long long msk = 0; msk < all; msk++) { n     long long c = 0; n     for (long long i = 0; i < n; i++) n       if (msk & (1 << i)) c++; n     if (c != k) continue; n     for (long long i = 0; i < n; i++) n       if (msk & (1 << i)) n         for (long long j = 0; j < n; j++) { n           if (msk & (1 << j)) continue; n           for (long long mk = 0; mk < allk; mk++) { n             if (tmp1[i][msk][mk] == 0) continue; n             for (long long mkk = 0; mkk < allkk; mkk++) { n               cnt[mkk << (k) | s[i][j] << (k - 1) | mk] += n                   tmp2[j][all ^ msk][mkk] * tmp1[i][msk][mk]; n             } n           } n         } n   } n   for (long long i = 0; i < (1 << (n - 1)); i++) printf( %lld  , cnt[i]); n   return ~~(0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int n, T; n int t[5005]; n double p[5005]; n double dp[5005][5005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> T; n   for (int i = 1; i <= n; i++) cin >> p[i] >> t[i], p[i] *= 0.01; n   dp[0][0] = 1; n   double ex = 0; n   for (int i = 1; i <= n; i++) { n     double r = 1.0; n     for (int j = 0; j < t[i]; j++) r *= (1.0 - p[i]); n     double sum = 0; n     for (int j = 1; j <= T; j++) { n       sum = sum * (1.0 - p[i]) + dp[i - 1][j - 1]; n       if (j > t[i]) sum = sum - r * dp[i - 1][j - t[i] - 1]; n       dp[i][j] = sum * p[i]; n       if (j >= t[i]) dp[i][j] += r * dp[i - 1][j - t[i]]; n       ex += dp[i][j]; n     } n   } n   cout << setprecision(13) << fixed << ex; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[10], A[300], B[300]; n int ct[300], mrk[300]; n vector<pair<int, int>> ans; n int main() { n   int n, x; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       scanf( %d , &x); n       A[i] |= (1 << j) * x; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       scanf( %d , &x); n       B[i] |= (1 << j) * x; n     } n   } n   vis[7] = ct[0] = vis[0] = mrk[0] = 1; n   for (int i = 1; i <= 7; i++) { n     for (int j = 1; j <= 7; j++) { n       for (int k = 0; !vis[j] && k < n; k++) { n         if (vis[A[k]] && (A[k] & B[k]) == j) { n           vis[j] = k + 1; n           ct[k] = mrk[k] = 1; n           ans.push_back({vis[A[k]], k + 1}); n         } n         for (int _k = (A[k] - 1) & A[k]; _k != 0 && !vis[j]; n              _k = (_k - 1) & A[k]) { n           if (vis[A[k] ^ _k] && vis[_k] && (B[k] & A[k]) == j) { n             vis[j] = k + 1; n             ct[k] = mrk[k] = 1; n             ans.push_back({vis[_k], k + 1}); n             ans.push_back({vis[A[k] ^ _k], k + 1}); n           } n           if (!vis[j] && vis[A[k] ^ _k] && vis[_k] && A[k] == j) { n             vis[j] = k + 1; n             mrk[k] = 1; n             ans.push_back({vis[_k], k + 1}); n             ans.push_back({vis[A[k] ^ _k], k + 1}); n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (!vis[A[i]]) return !printf( Impossible n ); n     if (!mrk[i] && A[i] != 0) ans.push_back({vis[A[i]], i + 1}); n   } n   printf( Possible n ); n   for (int i = 0; i < n; i++) printf( %d  , ct[i]); n   printf(  n%d n , (int)ans.size()); n   for (auto it : ans) printf( %d %d n , it.first, it.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n const int mxN = 1000; n int prnt[mxN]; n int rnk[mxN]; n vector<int> adj[mxN]; n vector<pair<int, int>> ans; n int vis[mxN]; n int find(int i) { n   if (prnt[i] != i) prnt[i] = find(prnt[i]); n   return prnt[i]; n } n void join(int x, int y) { n   int xroot = find(x); n   int yroot = find(y); n   if (rnk[xroot] < rnk[yroot]) n     prnt[xroot] = yroot; n   else if (rnk[xroot] > rnk[yroot]) n     prnt[yroot] = xroot; n   else { n     prnt[yroot] = xroot; n     rnk[xroot]++; n   } n } n void dfs(int u, int e) { n   for (auto ad = adj[u].begin(); ad != adj[u].end(); ad++) { n     if (*ad == e || vis[*ad]) continue; n     int adprnt = find(*ad); n     int uprnt = find(u); n     if (adprnt == uprnt) { n       ans.push_back(make_pair(u, *ad)); n       continue; n     } n     join(*ad, u); n     dfs(*ad, u); n   } n   vis[u] = 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (int i = 0; i < n; i++) { n     prnt[i] = i; n   } n   for (int i = 0; i < n; i++) { n     if (!vis[i]) { n       dfs(i, -1); n     } n   } n   cout << ans.size() << endl; n   int id = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       int x = find(i); n       int y = find(j); n       if (x != y) { n         cout << ans[id].first + 1 <<     << ans[id].second + 1 <<     << i + 1 n              <<     << j + 1 << endl; n         id++; n         join(i, j); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n clock_t time_p = clock(); n void time_taken() { n   time_p = clock() - time_p; n   cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ; n } n const long long mod = 1e9 + 7; n const long long INF = 1e18; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   int mn = *min_element((a).begin(), (a).end()); n   vector<int> inds, b; n   for (int i = 0; i < n; i++) { n     if (a[i] % mn == 0) inds.push_back(i), b.push_back(a[i]); n   } n   sort((b).begin(), (b).end()); n   int j = 0; n   for (int x : inds) { n     a[x] = b[j++]; n   } n   if (is_sorted((a).begin(), (a).end())) { n     cout <<  YES  <<   n ; n   } else { n     cout <<  NO  <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   time_taken(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.1415926535897932384626433832795; n const double EPS = 1e-9; n set<int> good; n void gen(int x) { n   good.insert(x); n   if (x > 100 * 1000 * 1000) { n     return; n   } n   gen(10 * x + 4); n   gen(10 * x + 7); n } n vector<pair<int, int> > swaps; n void addSwap(int i, int j) { n   if (i != j) { n     swaps.push_back(pair<int, int>(i, j)); n   } n } n bool fail; n vector<pair<int, int> > solve(const vector<int>& seq____) { n   vector<int> seq = seq____; n   swaps.clear(); n   fail = false; n   int where_good = -1; n   int n = seq.size(); n   bool sorted = true; n   for (int i = 0; i < n; ++i) { n     if (i > 0 && seq[i] < seq[i - 1]) { n       sorted = false; n     } n     if (good.count(seq[i])) { n       where_good = i; n     } n   } n   if (where_good == -1) { n     if (!sorted) { n       fail = true; n     } n     return swaps; n   } n   int goodNumber = seq[where_good]; n   int amountLess = 0; n   for (int i = 0; i < n; ++i) { n     if (seq[i] < goodNumber) { n       ++amountLess; n     } n   } n   vector<int> positions; n   for (int i = 0; i < n; ++i) { n     if (i != amountLess) { n       positions.push_back(i); n     } n   } n   set<pair<int, int> > unsorted; n   for (int i = 0; i < n; ++i) { n     if (i == where_good) { n       continue; n     } n     unsorted.insert(pair<int, int>(seq[i], i)); n   } n   int idInPositions = 0; n   while (!unsorted.empty()) { n     pair<int, int> cur = *unsorted.begin(); n     unsorted.erase(unsorted.begin()); n     if (cur.second == positions[idInPositions]) { n       ++idInPositions; n       continue; n     } n     int firstSwapPosition = positions[idInPositions]; n     bool firstErased = unsorted.count( n         pair<int, int>(seq[firstSwapPosition], firstSwapPosition)); n     unsorted.erase(pair<int, int>(seq[firstSwapPosition], firstSwapPosition)); n     swap(seq[where_good], seq[firstSwapPosition]); n     if (firstErased) { n       unsorted.insert(pair<int, int>(seq[where_good], where_good)); n     } n     addSwap(where_good, positions[idInPositions]); n     where_good = firstSwapPosition; n     int secondSwapPosition = cur.second; n     bool secondErased = unsorted.count( n         pair<int, int>(seq[secondSwapPosition], secondSwapPosition)); n     unsorted.erase(pair<int, int>(seq[secondSwapPosition], secondSwapPosition)); n     swap(seq[where_good], seq[secondSwapPosition]); n     if (secondErased) { n       unsorted.insert(pair<int, int>(seq[where_good], where_good)); n     } n     addSwap(where_good, secondSwapPosition); n     where_good = secondSwapPosition; n     ++idInPositions; n   } n   return swaps; n } n int main() { n   int n; n   gen(4); n   gen(7); n   while (cin >> n) { n     vector<int> seq(n); n     for (int i = 0; i < seq.size(); ++i) { n       scanf( %d , &seq[i]); n     } n     vector<pair<int, int> > res = solve(seq); n     if (fail) { n       cout << -1 << endl; n     } else { n       cout << res.size() << endl; n       for (int i = 0; i < res.size(); ++i) { n         printf( %d %d n , res[i].first + 1, res[i].second + 1); n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 2 * (1e5) + 7, mod = 1e9 + 7; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   { n     long long n, x1, x2; n     cin >> n >> x1 >> x2; n     vector<pair<long long, long long>> v; n     v.push_back({-1, -1}); n     for (long long i = 1; i <= n; i++) { n       long long x; n       cin >> x; n       v.push_back({x, i}); n     } n     sort(v.begin(), v.end()); n     bool ok = 0; n     for (long long i = 1; i <= n - 1; i++) { n       long long lo = i, hi = n - 1; n       while (lo <= hi) { n         long long mid = (lo + hi) / 2; n         long long x = (mid - i + 1); n         long long y = (n - mid); n         long long lo1 = v[i].first, lo2 = v[mid + 1].first; n         long double p = x1 / (long double)x; n         long double q = x2 / (long double)y; n         if (lo1 >= p && lo2 >= q) { n           ok = 1; n           cout <<  Yes  n                <<   n ; n           cout << x <<     << y <<   n ; n           for (long long j = i; j <= mid; j++) { n             cout << v[j].second <<    ; n           } n           cout <<   n ; n           for (long long j = mid + 1; j <= n; j++) { n             cout << v[j].second <<    ; n           } n           cout <<   n ; n           return 0; n         } n         if (p <= lo1) n           hi = mid - 1; n         else n           lo = mid + 1; n       } n     } n     swap(x1, x2); n     for (long long i = 1; i <= n - 1; i++) { n       long long lo = i, hi = n - 1; n       while (lo <= hi) { n         long long mid = (lo + hi) / 2; n         long long x = (mid - i + 1); n         long long y = (n - mid); n         long long lo1 = v[i].first, lo2 = v[mid + 1].first; n         long double p = x1 / (long double)x; n         long double q = x2 / (long double)y; n         if (lo1 >= p && lo2 >= q) { n           ok = 1; n           cout <<  Yes  n                <<   n ; n           cout << y <<     << x <<   n ; n           for (long long j = mid + 1; j <= n; j++) { n             cout << v[j].second <<    ; n           } n           cout <<   n ; n           for (long long j = i; j <= mid; j++) { n             cout << v[j].second <<    ; n           } n           cout <<   n ; n           return 0; n         } n         if (p <= lo1) n           hi = mid - 1; n         else n           lo = mid + 1; n       } n     } n     if (!ok) n       cout <<  No  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MX = 1e6 + 23; n const long long MOD = 998244353; n int read() { n   char k = getchar(); n   int x = 0; n   while (k <  0  || k >  9 ) k = getchar(); n   while (k >=  0  && k <=  9 ) x = x * 10 + k -  0 , k = getchar(); n   return x; n } n int n, m, c[MX], w[MX]; n int a[MX], b[MX]; n struct Choice { n   int w, id; n   bool operator<(const Choice &B) const { return w > B.w; } n }; n int cho[MX]; n void solve() { n   n = read(), m = read(); n   for (int i = 1; i <= n; ++i) { n     c[i] = read(); n     a[i] = c[i] / 100; n     b[i] = c[i] % 100; n     if (!b[i]) cho[i] = 1; n   } n   long long ans = 0; n   std::priority_queue<Choice> q; n   for (int i = 1; i <= n; ++i) { n     w[i] = read(); n     if (m >= b[i]) { n       if (b[i]) { n         q.push((Choice){(100 - b[i]) * w[i], i}); n       } n     } else { n       if (!q.empty() && q.top().w <= (100 - b[i]) * w[i]) { n         ans += q.top().w; n         q.pop(); n         q.push((Choice){(100 - b[i]) * w[i], i}); n       } else { n         ans += (100 - b[i]) * w[i]; n       } n       m += 100; n     } n     m -= b[i]; n   } n   printf( %lld n , ans); n   while (!q.empty()) { n     cho[q.top().id] = 1; n     q.pop(); n   } n   for (int i = 1; i <= n; ++i) { n     if (cho[i]) { n       printf( %d %d n , a[i], b[i]); n     } else n       printf( %d %d n , a[i] + 1, 0); n   } n } n int main() { n   int T = 1; n   for (int i = 1; i <= T; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, j = 1; n   string x; n   cin >> n >> x; n   for (int i = 1; i <= n; i += j) { n     cout << x[i - 1]; n     j++; n   } n   return 0; n } n


#include <bits/stdc++.h> n int Flag, sum, flag[105], n, m, a, b, A[105], map[105][105]; n void dfs(int last_last, int last) { n   int S; n   flag[last] = 1; n   for (int i = 1; i <= n; i++) { n     if (!map[last][i]) continue; n     if (map[i][last_last]) { n       S = A[i] + A[last] + A[last_last]; n       if (S < sum) sum = S; n     } n     if (!flag[i]) dfs(last, i); n   } n } n int main() { n   while (~scanf( %d%d , &n, &m)) { n     memset(flag, 0, sizeof(flag)); n     memset(map, 0, sizeof(map)); n     for (int i = 1; i <= n; i++) scanf( %d , &A[i]); n     for (int i = 1; i <= m; i++) { n       scanf( %d%d , &a, &b); n       map[a][b] = 1; n       map[b][a] = 1; n     } n     Flag = 0; n     sum = 99999999; n     dfs(0, 1); n     if (sum == 99999999) n       printf( -1 n ); n     else n       printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int vs, ve; n   int t, d; n   cin >> vs >> ve >> t >> d; n   int cur = vs; n   int sm = vs; n   int pre = vs; n   for (int i = 2; i <= t; i++) { n     while (cur < ve && abs(pre - cur) < d) cur++; n     if (cur >= ve) { n       while ((t - i) * d > cur - ve && abs(pre - cur) < d) cur++; n       while ((t - i) * d < cur - ve && abs(pre - cur) < d) cur--; n     } n     pre = cur; n     sm += cur; n   } n   assert(cur == ve); n   cout << sm; n   return 0; n } n


#include <bits/stdc++.h> n struct count_seg { n   struct node { n     std::array<int, 2> c; n     bool flip = false; n   }; n   std::vector<node> seg; n   count_seg() : seg() {} n   explicit count_seg(int N) : seg(2 * N) {} n   std::array<int, 2> query_all() { return seg[1].c; } n   void update_node(int a) { n     int N = int(seg.size()) >> 1; n     if (a >= N) { n       seg[a].c[0] = 0, seg[a].c[1] = 1; n     } else { n       seg[a].c[0] = seg[2 * a].c[0] + seg[2 * a + 1].c[0]; n       seg[a].c[1] = seg[2 * a].c[1] + seg[2 * a + 1].c[1]; n     } n     if (seg[a].flip) std::swap(seg[a].c[0], seg[a].c[1]); n   } n   void flip_node(int a) { n     seg[a].flip ^= 1; n     std::swap(seg[a].c[0], seg[a].c[1]); n   } n   void flip_leaf_lazy(int l) { n     int N = int(seg.size()) >> 1; n     seg[N + l].flip ^= 1; n   } n   void flip_range(int l, int r) { n     int N = int(seg.size()) >> 1; n     for (int a = l + N, b = r + N; a < b; a >>= 1, b >>= 1) { n       if (a & 1) flip_node(a++); n       if (b & 1) flip_node(--b); n     } n     for (int a = l + N; a; a >>= 1) update_node(a); n     for (int b = r + N - 1; b; b >>= 1) update_node(b); n   } n   void update_all() { n     for (int i = int(seg.size()) - 1; i; --i) update_node(i); n   } n }; n struct big_seg { n   struct mat { n     std::array<std::array<std::array<int, 2>, 2>, 2> v; n     friend mat operator*(const mat& a, const mat& b) { n       mat res; n       for (int i = 0; i < 2; i++) { n         for (int j = 0; j < 2; j++) { n           for (int z = 0; z < 2; z++) { n             res.v[i][j][z] = std::min(a.v[i][j][0] + b.v[i][j][z], n                                       a.v[i][j][1] + b.v[!i][!j][!z]); n           } n         } n       } n       return res; n     } n     int eval(std::array<int, 2> st, std::array<int, 2> en) const { n       int res = st[0] + v[0][0][0] + en[0]; n       for (int i = 0; i < 2; i++) { n         for (int j = 0; j < 2; j++) { n           res = std::min(res, st[i] + v[i][i][i ^ j] + en[j]); n         } n       } n       return res; n     } n   }; n   struct node { n     mat m; n     std::array<bool, 2> flip = {false, false}; n   }; n   int N, M; n   std::vector<node> seg; n   big_seg() : N(0), seg() {} n   big_seg(int N_, int M_) : N(N_), M(M_), seg() { n     int S = 1; n     while (S < N) S *= 2; n     seg.resize(2 * S); n   } n   mat query_all() { return seg[1].m; } n   void update_node(int a) { n     int S = int(seg.size()) >> 1; n     if (a >= S) { n       if (a - S < N) { n         for (int i = 0; i < 2; i++) { n           for (int j = 0; j < 2; j++) { n             seg[a].m.v[i][j][0] = i + j; n             seg[a].m.v[i][j][1] = i + j + M; n           } n         } n       } else { n         for (int i = 0; i < 2; i++) { n           for (int j = 0; j < 2; j++) { n             seg[a].m.v[i][j][0] = 0; n             seg[a].m.v[i][j][1] = 0 + M; n           } n         } n       } n     } else { n       seg[a].m = seg[2 * a].m * seg[2 * a + 1].m; n     } n     if (seg[a].flip[0]) { n       swap(seg[a].m.v[0], seg[a].m.v[1]); n     } n     if (seg[a].flip[1]) { n       swap(seg[a].m.v[0][0], seg[a].m.v[0][1]); n       swap(seg[a].m.v[1][0], seg[a].m.v[1][1]); n     } n   } n   void flip_node(int a, bool z) { n     seg[a].flip[z] ^= 1; n     if (z == 0) { n       swap(seg[a].m.v[0], seg[a].m.v[1]); n     } else if (z == 1) { n       swap(seg[a].m.v[0][0], seg[a].m.v[0][1]); n       swap(seg[a].m.v[1][0], seg[a].m.v[1][1]); n     } else n       assert(false); n   } n   void flip_leaf_lazy(int l, bool z) { n     int S = int(seg.size()) >> 1; n     seg[S + l].flip[z] ^= 1; n   } n   void flip_range(int l, int r, bool z) { n     int S = int(seg.size()) >> 1; n     for (int a = l + S, b = r + S; a < b; a >>= 1, b >>= 1) { n       if (a & 1) flip_node(a++, z); n       if (b & 1) flip_node(--b, z); n     } n     for (int a = l + S; a; a >>= 1) update_node(a); n     for (int b = r + S - 1; b; b >>= 1) update_node(b); n   } n   void update_all() { n     for (int i = int(seg.size()) - 1; i; --i) update_node(i); n   } n }; n int main() { n   using namespace std; n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   array<int, 2> N; n   int Q; n   cin >> N[0] >> N[1] >> Q; n   array<array<count_seg, 2>, 2> count_segs; n   array<big_seg, 2> big_segs; n   for (int a = 0; a < 2; a++) { n     big_segs[a] = big_seg(N[a], N[!a]); n     for (int b = 0; b < 2; b++) { n       count_segs[a][b] = count_seg(N[a]); n       string S; n       cin >> S; n       assert(int(S.size()) == N[a]); n       for (int i = 0; i < N[a]; i++) { n         if (S[i] ==  R ) { n           count_segs[a][b].flip_leaf_lazy(i); n           big_segs[a].flip_leaf_lazy(i, b); n         } n       } n       count_segs[a][b].update_all(); n     } n     big_segs[a].update_all(); n   } n   auto query = [&]() -> int { n     int ans = 2 * N[0] + 2 * N[1]; n     for (int z = 0; z < 2; z++) { n       int cnd = big_segs[!z].query_all().eval(count_segs[z][0].query_all(), n                                               count_segs[z][1].query_all()); n       ans = min(ans, cnd); n     } n     return ans; n   }; n   cout << query() <<   n ; n   for (int q = 0; q < Q; q++) { n     char side; n     int l, r; n     cin >> side >> l >> r; n     l--; n     bool dim, z; n     switch (side) { n       case  L : n         dim = 0, z = 0; n         break; n       case  R : n         dim = 0, z = 1; n         break; n       case  U : n         dim = 1, z = 0; n         break; n       case  D : n         dim = 1, z = 1; n         break; n       default: n         assert(false); n     } n     assert(0 <= l && l < r && r <= N[dim]); n     count_segs[dim][z].flip_range(l, r); n     big_segs[dim].flip_range(l, r, z); n     cout << query() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tree[4 * 100100]; n long long a[100100]; n int n; n void build(int from, int to, int node) { n   if (from == to) { n     tree[node] = a[from]; n     return; n   } n   int mid = (from + to) / 2; n   build(from, mid, 2 * node); n   build(mid + 1, to, 2 * node + 1); n   tree[node] = max(tree[2 * node], tree[2 * node + 1]); n } n long long val; n void update(int from, int to, int node) { n   if (from == to) { n     tree[node] = val; n     return; n   } n   int mid = (from + to) / 2; n   update(from, mid, 2 * node); n   tree[node] = max(tree[2 * node], tree[2 * node + 1]); n } n long long query(int from, int to, int node, int low, int high) { n   if (low > to || from > high) { n     return 0; n   } n   if (from >= low && to <= high) { n     return tree[node]; n   } n   int mid = (from + to) / 2; n   return max(query(from, mid, 2 * node, low, high), n              query(mid + 1, high, 2 * node + 1, low, high)); n } n long long b[100100]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   b[0] = a[0]; n   b[1] = a[1]; n   for (int i = 2; i < n; i++) { n     b[i] = max(b[i - 1], a[i]); n   } n   int Q; n   scanf( %d , &Q); n   while (Q--) { n     long long w, h; n     cin >> w >> h; n     cout << max(b[0], b[w - 1]) << endl; n     b[0] = (max(b[0], b[w - 1]) + h); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct gam { n   int o; n   long long k; n   int nez; n   long long bd; n   long long kd; n   gam() { n     o = 0; n     k = 0; n     nez = 0; n     bd = 0; n     kd = 0; n   } n }; n bool mycomp(gam i, gam j) { return (i.k < j.k); } n int main() { n   long long n; n   long long r; n   long long tr; n   long long sum = 0; n   cin >> n; n   cin >> r; n   cin >> tr; n   gam* a = new gam[n + 1]; n   a[0].k = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].o; n     cin >> a[i].k; n     a[i].nez = r - a[i].o; n     sum = sum + a[i].o; n   } n   sort(a + 1, a + n + 1, mycomp); n   long long nug = tr * n - sum; n   if (nug > 0) { n     int cur; n     for (int i = 1; i <= n; i++) { n       a[i].bd = a[i - 1].bd + a[i].nez; n       a[i].kd = a[i - 1].kd + a[i].k * a[i].nez; n       if (a[i].bd >= nug) { n         cur = i; n         break; n       } n     } n     long long enug = nug - a[cur - 1].bd; n     long long rez = a[cur - 1].kd + enug * a[cur].k; n     cout << rez; n   } else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename X> n inline X abs(const X& a) { n   return a < 0 ? -a : a; n } n template <typename X> n inline X sqr(const X& a) { n   return a * a; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   double first = (sqrt(8.0 * n + 1) - 1) / 2; n   int out = (first - floor(first)) * (first + 1); n   if (out == 0) n     cout << (int)first; n   else n     cout << out; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n int a[maxn]; n long long n, k; n bool ok(long long d) { n   long long ret = 0; n   for (int i = 0; i < n; ++i) { n     ret += (a[i] - 1 + d) / d; n   } n   return ret * d <= k; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) cin >> a[i], k += a[i]; n   long long ans = 0; n   for (long long i = 1; i <= sqrt(k) + 1; ++i) { n     if (ok(i)) ans = max(ans, i); n     if (ok(k / i)) ans = max(ans, k / i); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } else { n     return b; n   } n } n struct P { n   double x, y; n   P() {} n   P(double x, double y) : x(x), y(y) {} n   P operator+(const P &a) const { return P(x + a.x, y + a.y); } n   P operator-(const P &a) const { return P(x - a.x, y - a.y); } n   double operator^(const P &a) const { return x * a.x + y * a.y; } n   void in() { scanf( %lf%lf , &x, &y); } n   void out() { printf( REQUIRED %.7f %.7f n , x, y); } n   double dist(P a, P b) { n     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n   } n   double sqdist(P a, P b) { n     return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n   } n }; n int n, k; n int a[9999]; n int b[9999]; n int cc[9999]; n int bb[9999]; n long long res = -100000000; n long long ma[9999]; n long long query(long long l, long long r) { n   int length = r - l + 1; n   if (length <= k) return ma[length]; n   int cnt = 0; n   for (int i = (1); i <= (n); i++) { n     if ((i < l) || (i > r)) cc[++cnt] = a[i]; n   } n   int cnt2 = 0; n   for (int i = (l); i <= (r); i++) bb[++cnt2] = a[i]; n   sort(cc + 1, cc + cnt + 1); n   sort(bb + 1, bb + cnt2 + 1); n   int go = 0; n   for (int i = (1); i <= (min(min(cnt, cnt2), k)); i++) { n     if (cc[cnt - i + 1] > bb[i]) go = i; n   } n   for (int i = (1); i <= (go); i++) bb[i] = cc[cnt - i + 1]; n   long long sum = 0; n   for (int i = (1); i <= (cnt2); i++) sum += bb[i]; n   return sum; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (int i = (1); i <= (n); i++) b[i] = a[i]; n   sort(b + 1, b + n + 1); n   ma[0] = 0; n   for (int i = (n); i >= (1); i--) { n     ma[n - i + 1] = ma[n - i] + b[i]; n   } n   for (int i = (1); i <= (n); i++) { n     for (int j = (i); j <= (n); j++) { n       res = max(res, query(i, j)); n     } n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void DFS(int s); n vector<int> v[60]; n int d = 0; n int c[200]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i <= 50; i++) v[i].clear(); n   memset(c, 0, sizeof(c)); n   for (int i = 1; i <= m; i++) { n     int u, vv; n     scanf( %d %d , &u, &vv); n     v[u].push_back(vv); n     v[vv].push_back(u); n   } n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     if (c[i] == 0) { n       d = 0; n       DFS(i); n       mx = mx + d - 1; n     } n   } n   printf( %I64d n , (long long)pow(2, mx)); n   return 0; n } n void DFS(int s) { n   d++; n   c[s] = 1; n   int sz = v[s].size(); n   for (int i = 0; i < sz; i++) { n     int x = v[s][i]; n     if (c[x] == 0) { n       DFS(x); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxx = 1e9 + 7; n bool compare(const pair<long long, long long> &a, n              const pair<long long, long long> &b) { n   return a.first > b.first || (a.first == b.first && a.second < b.second); n } n long long inline power(long long a, long long b, long long p) { n   a %= p; n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans = (ans * a) % p; n     a = (a * a) % p; n     b >>= 1; n   } n   return ans; n } n long long inv(long long n, long long p) { return power(n, p - 2, p); } n bool inline isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n template <class T> n void printv(T &v, long long n) { n   if (n == -1) n = v.size(); n   for (long long i = 0; i < n; i++) cout << v[i] <<    ; n   cout <<   n ; n } n const long long maxn = 2e5 + 5; n vector<long long> parent(maxn, 0); n vector<vector<long long> > v; n long long start; n vector<long long> pos(maxn); n vector<pair<long long, long long> > gap(maxn); n void dfs(long long node) { n   start++; n   pos[start] = node; n   gap[node].first = start; n   for (auto &x : v[node]) { n     if (x == parent[node]) continue; n     dfs(x); n   } n   gap[node].second = start; n } n void solve() { n   long long n, q, u, k; n   cin >> n >> q; n   start = 0; n   v.assign(n + 1, vector<long long>()); n   for (long long i = 1; i <= n - 1; i++) { n     cin >> parent[i + 1]; n     v[i + 1].push_back(parent[i + 1]); n     v[parent[i + 1]].push_back(i + 1); n   } n   for (auto &x : v) sort(x.begin(), x.end()); n   dfs(1); n   for (long long i = 1; i <= q; i++) { n     cin >> u >> k; n     if (gap[u].second - gap[u].first + 1 < k) { n       cout << -1 <<   n ; n       continue; n     } n     cout << pos[((gap[u].first) + k - 1)] <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test = 1; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 2e5 + 10; n const long long INF = 0x3f3f3f3f; n const long long mod = 998244353; n int mans = INF; n int n, c, dp[M][2], f[M], l[M]; n int main() { n   scanf( %d%d , &n, &c); n   memset(dp, INF, sizeof(dp)); n   for (int i = 1; i < n; i++) scanf( %d , &f[i]); n   for (int i = 1; i < n; i++) scanf( %d , &l[i]); n   dp[0][0] = c; n   dp[0][1] = 0; n   for (int i = 1; i < n; i++) { n     dp[i][0] = min(dp[i - 1][0], dp[i - 1][1] + c) + l[i]; n     dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + f[i]; n   } n   for (int i = 0; i < n; i++) printf( %d  , min(dp[i][0], dp[i][1])); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const long long INF = (long long)1e18; n int n; n long long a[11], val[11]; n long long dp[1 << 8][1 << 8]; n int lastBit[1 << 8]; n long long del[11][1 << 8]; n long long get(long long v) { n   long long i = 2; n   long long res = 0; n   while (i * i <= v) { n     while (v % i == 0) { n       res++; n       v /= i; n     } n     i++; n   } n   if (v > 1) { n     res++; n   } n   return res; n } n long long dfs(int mask1, int mask2) { n   if (mask1 == 0 && mask2 == 0) return 0; n   if (mask2 == 0 && mask1 != 0) return INF; n   long long &res = dp[mask1][mask2]; n   if (res != -1) return res; n   res = INF; n   int last = lastBit[mask2]; n   for (int sub = mask1;; sub = (sub - 1) & mask1) { n     if (del[last][sub] != -1) { n       res = min(res, dfs(mask1 ^ sub, mask2 ^ (1 << last) ^ sub) + 1 + n                          (sub == 0 ? val[last] : del[last][sub])); n     } n     if (sub == 0) break; n   } n   return res; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     val[i] = get(a[i]); n     if (val[i] == 1) val[i] = 0; n   } n   for (int mask = 0; mask < (1 << n); mask++) { n     int last = -1; n     for (int i = 0; i < n; i++) n       if (mask & (1 << i)) { n         last = i; n       } n     lastBit[mask] = last; n   } n   for (int i = 0; i < n; i++) { n     for (int mask = 0; mask < (1 << n); mask++) { n       long long v = a[i]; n       for (int j = 0; j < n; j++) n         if (mask & (1 << j)) { n           if (v % a[j] != 0) { n             v = -1; n             break; n           } else { n             v /= a[j]; n           } n         } n       del[i][mask] = v == -1 ? -1 : get(v); n     } n   } n   memset(dp, -1, sizeof(dp)); n   long long res = INF; n   for (int mask = 1; mask < (1 << n); mask++) { n     res = min(res, dfs(((1 << n) - 1) ^ mask, mask) + n                        (__builtin_popcountll(mask) == 1 ? 0 : 1)); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, res; n int a[200005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (a[i] == 2) d++; n   } n   if (d <= n / 2) { n     res += d; n     n -= 2 * d; n     res += n / 3; n     cout << res; n     return 0; n   } else { n     cout << n - d; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   printf( %d n , min(n, m) + 1); n   for (int i = 0; i <= min(n, m); i++) { n     printf( %d %d n , min(n, m) - i, i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int n; n int a[10001], c[10001], d[10001]; n int ans = 0; n char str[101]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , str); n     if (str[0] ==  M ) a[i] = 1; n     scanf( %d %d , &c[i], &d[i]); n   } n   for (int i = 1; i <= 366; i++) { n     int x = 0, y = 0; n     for (int j = 1; j <= n; j++) { n       if (c[j] <= i && i <= d[j]) { n         if (a[j]) n           ++x; n         else n           ++y; n       } n     } n     ans = max(ans, min(x, y)); n   } n   printf( %d n , ans * 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s1[5010], s2[5010]; n int dp[5010][5010]; n int main() { n   int i, j, ind, n, m, ans; n   cin >> n >> m; n   cin >> s1; n   cin >> s2; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) - 1; n       if (s1[i - 1] == s2[j - 1]) { n         dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 2); n         dp[i][j] = max(dp[i][j], 2); n       } n     } n   } n   ans = 0; n   for (i = 0; i <= n; i++) { n     for (j = 0; j <= m; j++) ans = max(ans, dp[i][j]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxtt(T& t1, T t2) { n   t1 = max(t1, t2); n } n template <typename T> n void mintt(T& t1, T t2) { n   t1 = min(t1, t2); n } n bool debug = 0; n int n, m, k; n string direc =  URDL ; n const long long MOD2 = (long long)1000000007 * (long long)1000000007; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int& x, int y, int mod = 1000000007) { n   assert(y >= 0); n   x += y; n   if (x >= mod) x -= mod; n   assert(x >= 0 && x < mod); n } n void et(int x = -1) { n   printf( %d n , x); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } n vector<int> mp[44]; n int col[44]; n int C, I; n bool bip = 1; n void dfs(int x, int color) { n   col[x] = color; n   for (int c : mp[x]) n     if (col[c] == 0) n       dfs(c, 3 - color); n     else if (col[c] != 3 - color) n       bip = 0; n } n long long dp[(1 << 20) + 5], rdp[(1 << 20) + 5]; n int to[44]; n long long cal() { n   if (n == 1) return 2; n   int ln = n / 2, rn = n - ln; n   for (int(i) = 0; (i) < (int)(1 << ln); (i)++) dp[i] = 1; n   for (int(i) = 1; (i) <= (int)(ln); (i)++) n     for (int j : mp[i]) n       if (j <= ln) { n         dp[(1 << (i - 1)) | (1 << (j - 1))] = 0; n       } n   for (int s = 1; s < 1 << ln; s++) { n     for (int(j) = 0; (j) < (int)(ln); (j)++) n       if (s & (1 << j)) { n         if (dp[s ^ (1 << j)] == 0) { n           dp[s] = 0; n           break; n         } n       } n   } n   int msk = (1 << ln) - 1; n   for (int(i) = 0; (i) < (int)(1 << ln); (i)++) rdp[i] = dp[i ^ msk]; n   for (int j = 0; j < ln; j++) { n     for (int i = msk; i >= 0; i--) { n       if (~i & (1 << j)) { n         rdp[i] += rdp[i ^ (1 << j)]; n       } n     } n   } n   for (int(i) = 0; (i) < (int)(1 << rn); (i)++) dp[i] = 1; n   for (int i = ln + 1; i <= n; i++) n     for (int j : mp[i]) n       if (j > ln) { n         dp[(1 << (i - ln - 1)) | (1 << (j - ln - 1))] = 0; n       } else { n         to[i - ln - 1] |= 1 << (j - 1); n       } n   for (int s = 1; s < 1 << rn; s++) { n     for (int(j) = 0; (j) < (int)(rn); (j)++) n       if (s & (1 << j)) { n         if (dp[s ^ (1 << j)] == 0) { n           dp[s] = 0; n           break; n         } n       } n   } n   long long ans = 0; n   for (int(i) = 0; (i) < (int)(1 << rn); (i)++) n     if (dp[i]) { n       int s = 0; n       for (int(j) = 0; (j) < (int)(rn); (j)++) n         if (i & (1 << j)) s |= to[j]; n       ans += rdp[s]; n     } n   return ans; n } n void fmain(int tid) { n   scanf( %d%d , &n, &m); n   for (int i = 0, u, v; i < m; i++) { n     scanf( %d%d , &u, &v); n     mp[u].push_back(v); n     mp[v].push_back(u); n   }; n   for (int(i) = 1; (i) <= (int)(n); (i)++) n     if (col[i] == 0) { n       dfs(i, 1); n       C++; n     } n   for (int(i) = 1; (i) <= (int)(n); (i)++) n     if (mp[i].empty()) I++; n   long long all = 1LL << n; n   all -= 1LL << C; n   all += 1LL << I; n   if (bip) all += 1LL << C; n   all += 1LL << I; n   all -= m == 0 ? 1LL << n : 0; n   all -= cal() * 2; n   printf( %lld n , all); n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, x; n long long inp[200005]; n long long precom[64][2]; n long long num; n long long ans; n int main() { n   cin >> n >> k >> x; n   long long i, j, kl; n   for (i = 0; i < n; ++i) { n     scanf( %lld , &inp[i]); n     for (j = 0; j < 63; ++j) { n       if (((long long)1 << j) & inp[i]) precom[j][0]++; n     } n   } n   long long pos = 0; n   long long ans, tmpans; n   ans = 0; n   for (j = 0; j < n; ++j) { n     num = inp[j]; n     for (i = 0; i < k; ++i) { n       num *= x; n     } n     for (kl = 0; kl < 64; ++kl) { n       precom[kl][1] = 0; n       if ((num & ((long long)1 << kl)) && !(inp[j] & ((long long)1 << kl))) { n         precom[kl][1] = 1; n       } n       if (!(num & ((long long)1 << kl)) && (inp[j] & ((long long)1 << kl))) n         precom[kl][1] = -1; n     } n     tmpans = 0; n     for (kl = 0; kl < 64; ++kl) { n       if (precom[kl][0] + precom[kl][1]) tmpans += ((long long)1 << kl); n     } n     if (tmpans > ans) { n       ans = tmpans; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 3e5 + 5; n long long n, x1, x2; n array<long long, 2> c[nax]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> x1 >> x2; n   for (int i = 0; i < n; i++) cin >> c[i][0], c[i][1] = i; n   sort(c, c + n); n   for (int rep = 0; rep < 2; rep++) { n     int best = n - 1; n     for (; ~best; --best) n       if (c[best][0] >= (x2 + n - best - 1) / (n - best)) break; n     if (best == -1) continue; n     for (int k = 1; k < n; k++) { n       int j = lower_bound(c, c + n, array<long long, 2>{(x1 + k - 1) / k}) - c; n       if (j + k - 1 >= best) continue; n       cout <<  Yes n ; n       if (rep == 0) { n         cout << k <<     << n - best <<   n ; n         for (int i = j; i < j + k; i++) cout << c[i][1] + 1 <<    ; n         cout <<   n ; n         for (int i = best; i < n; i++) cout << c[i][1] + 1 <<    ; n         cout <<   n ; n       } else { n         cout << n - best <<     << k <<   n ; n         for (int i = best; i < n; i++) cout << c[i][1] + 1 <<    ; n         cout <<   n ; n         for (int i = j; i < j + k; i++) cout << c[i][1] + 1 <<    ; n         cout <<   n ; n       } n       exit(0); n     } n     swap(x1, x2); n   } n   cout <<  No n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int a[1000007]; n int st[1000007]; n int en[1000007]; n vector<pair<int, int> > v_st[1000007]; n long long ans[1000007]; n class Tree { n  public: n   pair<long long, int> tr[6 * 1000007]; n   void init(int where, int IL, int IR) { n     tr[where] = {0, 0}; n     if (IL == IR) { n       return; n     } n     int mid = (IL + IR) / 2; n     init(2 * where, IL, mid); n     init(2 * where + 1, mid + 1, IR); n   } n   void update(int where, int IL, int IR, int CURL, int CURR, n               pair<long long, int> add) { n     if (CURR < CURL) { n       return; n     } n     if (IR < CURL || CURR < IL) { n       return; n     } n     if (CURL <= IL && IR <= CURR) { n       tr[where].first += add.first, tr[where].second += add.second; n       return; n     } n     int mid = (IL + IR) / 2; n     update(2 * where, IL, mid, CURL, CURR, add); n     update(2 * where + 1, mid + 1, IR, CURL, CURR, add); n   } n   pair<long long, int> query(int where, int IL, int IR, int pos) { n     if (IR < pos || pos < IL) { n       return {0, 0}; n     } n     if (IL == IR) { n       return tr[where]; n     } n     int mid = (IL + IR) / 2; n     pair<long long, int> ret; n     if (pos <= mid) { n       ret = query(2 * where, IL, mid, pos); n     } else { n       ret = query(2 * where + 1, mid + 1, IR, pos); n     } n     ret = {ret.first + tr[where].first, ret.second + tr[where].second}; n     return ret; n   } n }; n Tree w; n void input() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= q; ++i) { n     scanf( %d , &st[i]); n   } n   for (int i = 1; i <= q; ++i) { n     scanf( %d , &en[i]); n     v_st[en[i]].push_back({st[i], i}); n   } n } n void calc() { n   stack<int> st; n   a[0] = n + 1; n   st.push(0); n   w.init(1, 1, n); n   for (int i = 1; i <= n; ++i) { n     while (a[st.top()] < a[i]) { n       st.pop(); n     } n     w.update(1, 1, n, 1, st.top(), {i - st.top() - 1, 0}); n     w.update(1, 1, n, st.top() + 1, i, {i, 1}); n     st.push(i); n     int sz = v_st[i].size(); n     for (int j = 0; j < sz; ++j) { n       pair<long long, int> ret = w.query(1, 1, n, v_st[i][j].first); n       ans[v_st[i][j].second] += ret.first - 1LL * ret.second * v_st[i][j].first; n     } n   } n } n void solve() { n   calc(); n   for (int i = 1; i <= (n >> 1); ++i) { n     swap(a[i], a[n - i + 1]); n   } n   for (int i = 1; i <= n; ++i) { n     v_st[i].clear(); n   } n   for (int i = 1; i <= q; ++i) { n     st[i] = (n - st[i] + 1); n     en[i] = (n - en[i] + 1); n     swap(st[i], en[i]); n     v_st[en[i]].push_back({st[i], i}); n   } n   calc(); n   for (int i = 1; i <= q; ++i) { n     printf( %I64d  , ans[i] + (en[i] - st[i] + 1)); n   } n   printf(  n ); n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 100005; n int a[maxn], b[maxn]; n int main() { n   long long d, k, a, b, t; n   scanf( %I64d%I64d%I64d%I64d%I64d , &d, &k, &a, &b, &t); n   if (d < k) { n     printf( %I64d n , d * a); n     return 0; n   } n   d -= k; n   long long ans = k * a; n   long long k1 = k * a + t, k2 = k * b; n   if (k1 >= k2) { n     ans += d * b; n   } else { n     ans += d / k * k1; n     d %= k; n     ans += min(t + d * a, d * b); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 2e9 + 10, MAX = 2100, MOD = 1e9 + 7, MAXL = 25; n void OUT(long double o, int x) { n   cout << fixed << setprecision(x) << o; n   return; n } n long long dp[MAX][MAX], ra[MAX], a[MAX], re[200000], w[MAX]; n vector<int> v[MAX]; n pair<long long, long long> c[MAX]; n set<int> st; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, t; n   cin >> n >> m >> t; n   for (int i = 1; i <= m; i++) { n     int l, r; n     cin >> l >> r; n     for (int j = l + 1; j <= r; j++) ra[j] = 1; n   } n   for (int i = 1; i <= t; i++) { n     cin >> c[i].first >> c[i].second; n     st.insert(c[i].second); n   } n   int cnt = 0; n   for (int i = 1; i <= 2000; i++) w[i] = INF; n   for (int u : st) { n     re[u] = ++cnt; n     w[cnt] = u; n   } n   for (int i = 1; i <= t; i++) v[c[i].first].push_back(re[c[i].second]); n   for (int i = 1; i <= 2000; i++) dp[0][i] = INF; n   for (int u : v[0]) dp[0][u] = 0; n   for (int i = 1; i <= n; i++) { n     long long mi = INF; n     for (int j = 1; j <= 2000; j++) mi = min(mi, dp[i - 1][j] + w[j]); n     if (!ra[i]) mi = min(mi, dp[i - 1][0]); n     dp[i][0] = mi; n     for (int j = 1; j <= 2000; j++) dp[i][j] = dp[i - 1][j] + w[j]; n     for (int u : v[i]) dp[i][u] = min(dp[i][u], mi); n   } n   long long ans = INF; n   for (int i = 0; i <= 2000; i++) ans = min(ans, dp[n][i]); n   if (ans < INF) n     cout << ans; n   else n     cout << -1; n   return 0; n } n


#include<bits/stdc++.h>  n using namespace std; n namespace Ruri{ n     #define ms(a,b) memset(a,b,sizeof(a)) n     #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ? n     #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i) n     #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to) n     #define ce(i,r) i==r?  n :    n     #define pb push_back  n     #define all(x) x.begin(),x.end() n     #define gmn(a,b) a=min(a,b) n     #define gmx(a,b) a=max(a,b) n     #define fi first n     #define se second                          n     typedef long long ll; n     typedef unsigned long long ull; n     typedef double db; n     const int infi=1e9;//infi较大，注意涉及inf相加时爆int n     const ll infl=4e18; n     inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; } n     inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; } n     //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0)); n } n using namespace Ruri; n namespace Read{ n     #define ss(a) scanf( %s ,a) n     inline int ri(){ int x; scanf( %d ,&x); return x; } n     inline ll rl(){ ll x;  scanf( %lld ,&x); return x; } n     inline db rd(){ db x; scanf( %lf ,&x); return x; } n } n namespace DeBug{ n     #define pr(x) cout<<#x<< :  <<(x)<<endl n     #define pra(x,a,b) cout<<#x<< :  <<endl;   n             repi(i,a,b) cout<<x[i]<<   ;       n             puts(  );    n     #define FR(a) freopen(a, r ,stdin) n     #define FO(a) freopen(a, w ,stdout) n } n using namespace Read; n using namespace DeBug; n const int MAX_N=105; n const int mod=1e9+7; n inline void Add(int &x,int y){ x+=y;if(x>=mod) x-=mod; } n int n,b[MAX_N],c[MAX_N]; n int sb[MAX_N]; n int f[MAX_N*MAX_N],g[MAX_N*MAX_N]; n map<int,int> ans; n int Solve(int x) n { n  repi(i,0,10001) g[i]=1; n  repi(i,1,n){ n   int lim=i*x+sb[i]; n   ms(f,0); n   for(int j=max(0,lim);j<=10001;++j) f[j]=(g[j]-(j-c[i]-1>=0?g[j-c[i]-1]:0)+mod)%mod; n   ms(g,0),g[0]=f[0]; n   for(int j=1;j<=10001;++j) g[j]=(1ll*g[j-1]+f[j])%mod; n  } n  return g[10001]; n } n int main() n { n  n=ri(); n  int prod=1; n  repi(i,1,n) c[i]=ri(),prod=1ll*prod*(c[i]+1)%mod; n  repi(i,1,n-1) b[i]=ri(); n  repi(i,1,n)repi(j,1,i-1) sb[i]+=(i-j)*b[j]; n  int l=0,r=infi,mx_c=*max_element(c+1,c+n+1); n  repi(i,1,n) gmn(l,-((sb[i]-1)/i+1)),gmn(r,(n*mx_c-sb[i]-1)/i+1); n  repi(i,l,r) ans[i]=Solve(i); n  int Q=ri(); n  while(Q--) n  { n   int x=ri(); n   if(x>r) puts( 0 ); n   else if(x<l) printf( %d n ,prod); n   else printf( %d n ,ans[x]); n  } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int blk_size; n struct node { n   int l, r, id; n   long long int ans = 0; n }; n bool compare(node a, node b) { n   if (a.l / blk_size != b.l / blk_size) n     return (a.l) / blk_size < (b.l) / blk_size; n   return a.r < b.r; n } n bool compare2(node a, node b) { return a.id < b.id; } n long long int freq[3000001]; n int main() { n   ios_base::sync_with_stdio(false); n   ; n   int n, m, k; n   cin >> n >> m >> k; n   std::vector<int> a(n), pxor(n + 1); n   for (int i = 0; i < n; i++) cin >> a[i]; n   pxor[0] = 0; n   for (int i = 1; i <= n; i++) pxor[i] = pxor[i - 1] ^ a[i - 1]; n   blk_size = sqrt(n + 1); n   std::vector<node> q(m); n   for (int i = 0; i < m; i++) { n     cin >> q[i].l >> q[i].r; n     q[i].id = i; n   } n   sort(q.begin(), q.end(), compare); n   int start = 0, end = 0; n   long long int ans = 0; n   freq[0] = 1; n   for (int i = 0; i < m; i++) { n     int l = q[i].l - 1, r = q[i].r; n     while (start > l) { n       start--; n       ans += freq[pxor[start] ^ k]; n       freq[pxor[start]]++; n     } n     while (start < l) { n       freq[pxor[start]]--; n       ans -= freq[pxor[start] ^ k]; n       start++; n     } n     while (end > r) { n       freq[pxor[end]]--; n       ans -= freq[pxor[end] ^ k]; n       end--; n     } n     while (end < r) { n       end++; n       ans += freq[pxor[end] ^ k]; n       freq[pxor[end]]++; n     } n     q[i].ans = ans; n   } n   sort(q.begin(), q.end(), compare2); n   for (int i = 0; i < m; i++) cout << q[i].ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = n, p = 1; i - 1; i >>= 1, p <<= 1) { n     for (int j = 0; j < (i + 1) / 2; j++) cout << p <<    ; n     if (n % p) n -= p / 2; n   } n   cout << n; n } n


#include <bits/stdc++.h> n using namespace std; n int nx[1000514 * 2][26], spt; n int fl[1000514 * 2], ll[1000514 * 2], lp[1000514 * 2]; n int newnode() { n   memset(nx[spt], 0, sizeof(nx[spt])); n   fl[spt] = ll[spt] = lp[spt] = 0; n   return spt++; n } n int dp[1000514 * 2]; n void add(int root, int& last, char A) { n   int a = A -  a ; n   int p = last; n   int np = last = newnode(); n   ll[np] = ll[p] + 1; n   lp[np] = 0; n   dp[np] = 1; n   for (; p && nx[p][a] == 0; p = fl[p]) nx[p][a] = np; n   int q = nx[p][a]; n   if (p == 0) n     fl[np] = root; n   else if (ll[q] == ll[p] + 1) n     fl[np] = q; n   else { n     int nq = spt++; n     memcpy(nx[nq], nx[q], sizeof(nx[q])); n     fl[nq] = fl[q]; n     ll[nq] = ll[p] + 1; n     lp[nq] = lp[q] + ll[q] - 1 - ll[p]; n     fl[q] = fl[np] = nq; n     for (; p && nx[p][a] == q; p = fl[p]) nx[p][a] = nq; n   } n } n char s[1000514], r[1000514 * 2]; n vector<int> bk[1000514]; n int vs[1000514 * 2], vt; n void make_z(int* z, char* s, int l) { n   int j = z[0] = 0; n   for (int i = 1; i < l; i++) { n     z[i] = (i > j + z[j] - 1) ? 0 : min(z[i - j], j + z[j] - i); n     for (; i + z[i] < l && s[z[i]] == s[i + z[i]]; z[i]++) n       ; n     if (i + z[i] > j + z[j]) j = i; n   } n } n int z[1000514 * 2]; n void sol() { n   spt = 1; n   int last; n   int root = last = newnode(); n   int sl = 0; n   for (; s[sl]; sl++) n     ; n   for (int i = 0; i < sl; i++) add(root, last, s[i]); n   for (int i = 1; i < spt; i++) { n     bk[ll[i]].push_back(i); n   } n   for (int i = sl; i >= 0; i--) { n     int sz = (int)bk[i].size(); n     for (int j = 0; j < sz; j++) { n       int w = bk[i][j]; n       dp[fl[w]] += dp[w]; n     } n   } n   int Q; n   scanf( %d , &Q); n   for (int T = 0; T < Q; T++) { n     scanf( %s , r); n     int rl = 0; n     for (; r[rl]; rl++) n       ; n     for (int i = 0; i < rl; i++) r[rl + i] = r[i]; n     r[rl + rl] = 0; n     make_z(z, r, rl * 2); n     int t = 0; n     for (int i = 1; i <= rl; i++) { n       if (rl % i) continue; n       int flag = 0; n       for (int j = i; j < rl; j += i) { n         if (z[j] < i) { n           flag = 1; n         } n       } n       if (flag) continue; n       t = i; n       break; n     } n     int p = root; n     int l = 0; n     for (int i = 0; i < rl; i++) { n       int a = r[i] -  a ; n       for (; p && nx[p][a] == 0; p = fl[p]) n         ; n       if (p == 0) { n         l = 0; n         p = root; n       } else { n         l = min(l, ll[p]) + 1; n         p = nx[p][a]; n       } n     } n     vt++; n     int ans = 0; n     if (l >= rl && vs[p] != vt) { n       vs[p] = vt; n       ans += dp[p]; n     } n     for (int i = 0; i < t - 1; i++) { n       int a = r[i] -  a ; n       for (; p && nx[p][a] == 0; p = fl[p]) n         ; n       if (p == 0) { n         l = 0; n         p = root; n       } else { n         l = min(l, ll[p]) + 1; n         p = nx[p][a]; n       } n       for (; p && ll[fl[p]] >= rl; p = fl[p]) n         ; n       l = min(l, ll[p]); n       if (l >= rl && vs[p] != vt) { n         vs[p] = vt; n         ans += dp[p]; n       } n     } n     printf( %d n , (int)ans); n   } n } n int main() { n   scanf( %s , s); n   sol(); n } n


#include <bits/stdc++.h> n using namespace std; n bool flag = 0; n void dfs(int u, vector<vector<int> > &g, vector<int> &mark, string &order) { n   mark[u] = 1; n   for (auto to : g[u]) { n     if (mark[to] == 1) n       flag = 1; n     else if (mark[to] == 0) n       dfs(to, g, mark, order); n   } n   order.push_back( a  + u); n   mark[u] = 2; n } n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<string> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<vector<int> > g(26); n   for (int i = 0; i < n - 1; i++) { n     int size = min((int((a[i]).size())), (int((a[i + 1]).size()))), j; n     for (j = 0; j < size; j++) n       if (a[i][j] != a[i + 1][j]) break; n     if (j < size) n       g[a[i][j] -  a ].push_back(a[i + 1][j] -  a ); n     else if ((int((a[i]).size())) > (int((a[i + 1]).size()))) n       flag = 1; n   } n   vector<int> mark(26); n   string order; n   for (int i = 0; i < 26; i++) n     if (mark[i] == 0) dfs(i, g, mark, order); n   if (flag) { n     cout <<  Impossible n ; n   } else { n     reverse(order.begin(), order.end()); n     cout << order <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 9, mod = 998244353; n template <const int32_t MOD> n struct modint { n   int32_t value; n   modint() = default; n   modint(int32_t value_) : value(value_) {} n   inline modint<MOD> operator+(modint<MOD> other) const { n     int32_t c = this->value + other.value; n     return modint<MOD>(c >= MOD ? c - MOD : c); n   } n   inline modint<MOD> operator-(modint<MOD> other) const { n     int32_t c = this->value - other.value; n     return modint<MOD>(c < 0 ? c + MOD : c); n   } n   inline modint<MOD> operator*(modint<MOD> other) const { n     int32_t c = (int64_t)this->value * other.value % MOD; n     return modint<MOD>(c < 0 ? c + MOD : c); n   } n   inline modint<MOD> &operator+=(modint<MOD> other) { n     this->value += other.value; n     if (this->value >= MOD) this->value -= MOD; n     return *this; n   } n   inline modint<MOD> &operator-=(modint<MOD> other) { n     this->value -= other.value; n     if (this->value < 0) this->value += MOD; n     return *this; n   } n   inline modint<MOD> &operator*=(modint<MOD> other) { n     this->value = (int64_t)this->value * other.value % MOD; n     if (this->value < 0) this->value += MOD; n     return *this; n   } n   inline modint<MOD> operator-() const { n     return modint<MOD>(this->value ? MOD - this->value : 0); n   } n   modint<MOD> pow(uint64_t k) const { n     modint<MOD> x = *this, y = 1; n     for (; k; k >>= 1) { n       if (k & 1) y *= x; n       x *= x; n     } n     return y; n   } n   modint<MOD> inv() const { return pow(MOD - 2); } n   inline modint<MOD> operator/(modint<MOD> other) const { n     return *this * other.inv(); n   } n   inline modint<MOD> operator/=(modint<MOD> other) { n     return *this *= other.inv(); n   } n   inline bool operator==(modint<MOD> other) const { n     return value == other.value; n   } n   inline bool operator!=(modint<MOD> other) const { n     return value != other.value; n   } n   inline bool operator<(modint<MOD> other) const { return value < other.value; } n   inline bool operator>(modint<MOD> other) const { return value > other.value; } n }; n template <int32_t MOD> n modint<MOD> operator*(int64_t value, modint<MOD> n) { n   return modint<MOD>(value) * n; n } n template <int32_t MOD> n modint<MOD> operator*(int32_t value, modint<MOD> n) { n   return modint<MOD>(value % MOD) * n; n } n template <int32_t MOD> n istream &operator>>(istream &in, modint<MOD> &n) { n   return in >> n.value; n } n template <int32_t MOD> n ostream &operator<<(ostream &out, modint<MOD> n) { n   return out << n.value; n } n using mint = modint<mod>; n vector<mint> Thomas(vector<vector<mint>> a) { n   int n = a.size(); n   for (int i = 1; i < n; i++) { n     mint x = a[i][0] * a[i - 1][1].inv(); n     a[i][1] -= x * a[i - 1][2]; n     a[i][3] -= x * a[i - 1][3]; n   } n   for (int i = n - 2; i >= 0; i--) { n     mint x = a[i][2] * a[i + 1][1].inv(); n     a[i][3] -= x * a[i + 1][3]; n   } n   vector<mint> ans; n   for (int i = 0; i < n; i++) { n     ans.push_back(a[i][3] * a[i][1].inv()); n   } n   return ans; n } n int a[N]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   mint m = 0; n   for (int i = 0; i < n; i++) cin >> a[i], m += a[i]; n   vector<vector<mint>> mat(m.value + 1); n   for (int i = 0; i <= m.value; i++) { n     vector<mint> v(4, 0); n     if (i) v[0] = mint(i) / m; n     v[1] = (m - i) * (n - 2) / m / (n - 1) - 1; n     if (i + 1 <= m.value) v[2] = (m - i) / m / (n - 1); n     v[3] = mint(-i) / m; n     mat[i] = v; n   } n   vector<mint> f = Thomas(mat); n   mint ans = 0; n   for (int i = 0; i < n; i++) ans += f[a[i]]; n   ans -= (n - 1) * f[0] + f[m.value]; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[105], ma = 0, mi = 110, maxi, mini, tc; n   cin >> tc; n   for (int i = 0; i < tc; i++) { n     cin >> a[i]; n     if (a[i] > ma) { n       ma = a[i]; n       maxi = i; n     } n     if (a[i] <= mi) { n       mi = a[i]; n       mini = i; n     } n   } n   if (maxi > mini) { n     mini++; n   } n   cout << maxi + (tc - 1) - mini << endl; n } n