#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, Q = 1e5 + 10; n int n, q; n struct edg { n   int x, y, t; n   bool operator==(edg a) { return x == a.x && y == a.y; } n } e[Q]; n bool cmp(edg a, edg b) { n   return a.x < b.x || (a.x == b.x && a.y < b.y) || n          (a.x == b.x && a.y == b.y && a.t < b.t); n } n class stk { n  private: n   int tp; n   struct elm { n     int x, y, rnk; n   } e[Q << 2]; n  n  public: n   void push(int x, int y, int rnk) { n     e[++tp] = (elm){x, y, rnk}; n     return; n   } n   elm top() { return e[tp]; } n   int size() { return tp; } n   void pop() { n     if (tp) tp--; n     return; n   } n } s; n class ufs { n  private: n   struct elm { n     int fth, rnk; n   } d[N << 1]; n  n  public: n   void init() { n     for (int i = 1; i <= (n << 1); i++) d[i].fth = i, d[i].rnk = 0; n     return; n   } n   inline int find(int x) { n     while (x != d[x].fth) x = d[x].fth; n     return x; n   } n   bool check(int x, int y) { return find(x) == find(y); } n   void unite(int x, int y) { n     x = find(x), y = find(y); n     if (x == y) return; n     if (d[x].rnk < d[y].rnk) swap(x, y); n     s.push(x, y, d[x].rnk == d[y].rnk); n     d[y].fth = x; n     d[x].rnk += d[x].rnk == d[y].rnk; n     return; n   } n   void divide() { n     int x = s.top().x, y = s.top().y; n     d[y].fth = y; n     d[x].rnk -= s.top().rnk; n     s.pop(); n     return; n   } n } u; n class sgt { n  private: n   struct elm { n     int x, y; n   }; n   vector<elm> v[Q << 2]; n  n  public: n   void insert(int i, int l, int r, int lq, int rq, int x, int y) { n     if (l > rq || r < lq) return; n     if (l >= lq && r <= rq) { n       v[i].push_back((elm){x, y}); n       return; n     } n     int mid = l + r >> 1; n     insert(i << 1, l, mid, lq, rq, x, y); n     insert(i << 1 | 1, mid + 1, r, lq, rq, x, y); n     return; n   } n   void query(int i, int l, int r) { n     int sz = s.size(); n     bool flag = true; n     for (int j = 0; j < v[i].size() && flag; j++) { n       int x = v[i][j].x, y = v[i][j].y; n       if (u.check(x, y)) n         flag = false; n       else n         u.unite(x, y + n), u.unite(x + n, y); n     } n     if (flag) { n       if (l == r) n         puts( YES ); n       else { n         int mid = l + r >> 1; n         query(i << 1, l, mid); n         query(i << 1 | 1, mid + 1, r); n       } n     } else n       for (int i = l; i <= r; i++) puts( NO ); n     while (s.size() > sz) u.divide(); n     return; n   } n } t; n inline int rd() { n   int f = 1, x = 0; n   char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   return f * x; n } n int main() { n   n = rd(), q = rd(); n   u.init(); n   for (int i = 1; i <= q; i++) e[i].x = rd(), e[i].y = rd(), e[i].t = i; n   sort(e + 1, e + 1 + q, cmp); n   for (int i = 1; i <= q; i++) { n     if (e[i] == e[i + 1]) n       t.insert(1, 1, q, e[i].t, e[i + 1].t - 1, e[i].x, e[i].y), i++; n     else n       t.insert(1, 1, q, e[i].t, q, e[i].x, e[i].y); n   } n   t.query(1, 1, q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const long long mo = 1e9 + 7; n const long long inf = 1e9; n const long long Inf = 1e18; n long long w[N], a[N]; n int x[N], y[N]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &k, &m); n   for (int i = (1); i <= (n); ++i) scanf( %lld , &w[i]); n   sort(w + 1, w + n + 1); n   for (int i = (1); i <= (m); ++i) w[i] += w[i - 1], a[i] = w[i]; n   for (int i = (1); i <= (k); ++i) scanf( %d%d , &x[i], &y[i]); n   for (int i = (1); i <= (m); ++i) n     for (int j = (1); j <= (k); ++j) n       if (x[j] <= i) a[i] = min(a[i - x[j]] + w[i] - w[i - x[j] + y[j]], a[i]); n   printf( %lld n , a[m]); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll LINF = 1001002003004005006ll; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n void solve() { n   ll n, m; n   cin >> n >> m; n   vector<tuple<ll, ll, ll>> v(n); n   for (int i = 0; i < n; i++) { n     ll t, l, h; n     cin >> t >> l >> h; n     v[i] = make_tuple(t, l, h); n   } n   sort(v.begin(), v.end()); n   ll L = m, H = m, T = 0; n   for (int i = 0; i < n; i++) { n     ll now = get<0>(v[i]), l = get<1>(v[i]), h = get<2>(v[i]); n     L = L - (now - T); n     H = H + (now - T); n     if (h < L or H < l) { n       cout <<  NO  << endl; n       return; n     } n     chmax(L, l); n     chmin(H, h); n     T = now; n   } n   cout <<  YES  << endl; n   return; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int q; n   cin >> q; n   while (q--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(int x) { n   int sum = 0; n   while (x >= 5) { n     sum += x / 5; n     x /= 5; n   } n   return sum; n } n int main() { n   int m; n   cin >> m; n   int k = 0; n   vector<long long> vec; n   for (int i = 1; i <= 1000000; i++) { n     long long sum = solve(i); n     if (sum == m) { n       k++; n       vec.push_back(i); n     } else if (sum > m) n       break; n   } n   cout << k << endl; n   for (int i = 0; i < k; i++) cout << vec[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int INF = (int)1E9; n const long long INF64 = (long long)1E18; n const long double EPS = 1E-8; n const long double PI = 3.1415926535897932384626433832795; n const int MAXN = 5200; n const int MAXF = 1001000; n long long d1[MAXN], d[MAXN], D[MAXN][MAXN], p; n long long f[MAXF], a[MAXF]; n vector<long long> l; n int n, m; n long long get(int n, int c) { n   if (n == 0 && c == 0) return 1; n   if (n == 0 || c == 0) return 0; n   long long &ans = D[n][c]; n   if (ans != -1) return ans; n   ans = (get(n - 1, c - 1) + get(n - 1, c) * (c - 1)) % p; n   return ans; n } n int main() { n   cin >> n >> m >> p; n   f[0] = a[0] = 1; n   for (int i = 1; i < MAXF; i++) { n     f[i] = (f[i - 1] * i) % p; n     a[i] = (a[i - 1] * (m - i + 1)) % p; n   } n   int prev = -1; n   long long sum = 0; n   memset(D, 255, sizeof D); n   for (int step = 0; step < (int)(n); step++) { n     int l; n     scanf( %d , &l); n     for (int i = 1; i <= l; i++) n       if (prev == -1) { n         long long cur = get(l, i) % p; n         cur = (cur * a[i]) % p; n         d1[i] = cur; n       } else { n         long long cur = get(l, i) % p; n         cur = (cur * a[i]) % p; n         cur = (cur * sum) % p; n         long long ppp = 0; n         if (i <= prev) { n           ppp = get(l, i) % p; n           ppp = (ppp * f[i]) % p; n           ppp = (ppp * d[i]) % p; n         } n         d1[i] = (cur - ppp + p) % p; n       } n     sum = 0; n     for (int i = 0; i < (int)(l + 1); i++) { n       sum = (sum + d1[i]) % p; n       d[i] = d1[i]; n     } n     prev = l; n   } n   long long ans = 0; n   for (int i = 0; i < (int)(prev + 1); i++) ans = (ans + d[i]) % p; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 10; n const int M = 20; n vector<int> soln[M][M]; n int compute(vector<int> sha) { n   vector<int> sz(sha.size(), 1); n   vector<int> out[sha.size()]; n   for (int i = sha.size() - 1; i > 0; i--) { n     sz[sha[i]] += sz[i]; n     out[sha[i]].push_back(i); n   } n   int l1, l2; n   int bb = 0; n   for (int i = 0; i < sha.size(); i++) { n     if (out[i].size() == 2) { n       l1 = sz[out[i][0]]; n       l2 = sz[out[i][1]]; n       if (l1 > l2) swap(l1, l2); n       if (l1 * 2 <= l2) { n         bb++; n       } n     } n   } n   return bb; n } n void brute(int cid, vector<int> pp) { n   if (cid == pp.size()) { n     int bal = compute(pp); n     soln[pp.size()][bal] = pp; n     return; n   } n   brute(cid + 1, pp); n   if (pp.size() + 2 < M) { n     pp.push_back(cid); n     pp.push_back(cid); n     brute(cid + 1, pp); n   } n } n bool ish(int n) { n   if ((n & (n + 1)) == 0) { n     return true; n   } n   return false; n } n int res[N]; n bool valid(int n, int s1, int s2) { n   if (s1 > s2) swap(s1, s2); n   if (ish(s1) && ish(s2)) return false; n   int cn = !ish(s1) + !ish(s2) + (s1 * 2 <= s2); n   if (cn > 1) return false; n   return true; n } n int cnt = 1; n void bins(int n, int c) { n   int root = cnt; n   res[cnt] = c; n   cnt++; n   if (n == 1) { n     return; n   } n   bins(n / 2, root); n   bins(n / 2, root); n } n void solve1(int n, int c) { n   int root = cnt; n   cnt++; n   res[root] = c; n   int k = 1; n   while ((k * 2 - 1) < n) { n     k *= 2; n   } n   int sz1 = (k - 1); n   int sz2 = (n - 1 - sz1); n   if (!valid(n, sz1, sz2)) { n     k /= 2; n     sz1 = k - 1; n     sz2 = (n - 1 - sz1); n   } n   if (ish(sz1)) n     bins(sz1, root); n   else n     solve1(sz1, root); n   if (ish(sz2)) n     bins(sz2, root); n   else n     solve1(sz2, root); n } n void extend(vector<int> pp, int c) { n   res[cnt] = c; n   for (int i = 1; i < pp.size(); i++) { n     res[cnt + i] = pp[i] + cnt; n   } n   cnt += pp.size(); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.tie(0); n   ; n   int n, k; n   cin >> n >> k; n   if (n % 2 == 0) { n     cout <<  NO n ; n     return 0; n   } n   if (k > max((n - 3) / 2, 0)) { n     cout <<  NO n ; n     return 0; n   } n   brute(0, {-1}); n   int par = 0; n   while (n > M && k > 1) { n     if (k == 2) { n       if (!ish(n - 2)) { n         n -= 2; n         res[cnt + 1] = cnt; n         res[cnt + 2] = cnt; n         par = cnt; n         cnt += 2; n       } else { n         n -= 4; n         res[cnt + 1] = cnt; n         res[cnt + 2] = cnt + 1; n         res[cnt + 3] = cnt + 1; n         res[cnt + 4] = cnt; n         par = cnt; n         cnt += 4; n       } n     } else { n       n -= 2; n       res[cnt + 1] = cnt; n       res[cnt + 2] = cnt; n       par = cnt; n       cnt += 2; n     } n     k--; n   } n   if (k == 0) { n     if (!ish(n)) { n       cout <<  NO n ; n       return 0; n     } n     bins(n, par); n   } else if (k == 1) { n     if (ish(n)) { n       cout <<  NO n ; n       return 0; n     } n     solve1(n, par); n   } else { n     if (soln[n][k].empty()) { n       cout <<  NO n ; n       return 0; n     } n     extend(soln[n][k], par); n   } n   cout <<  YES n ; n   for (int i = 1; i < cnt; i++) { n     cout << res[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string op; n   int x = 0; n   for (int k = 0; k < n; k++) { n     cin >> op; n     if (op ==  ++X  || op ==  X++ ) { n       x++; n     } else if (op ==  --X  || op ==  X-- ) { n       x--; n     } n   } n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(long long k) { n   for (long long i = 2; i <= sqrt(k); i++) n     if (k % i == 0) return false; n   return true; n } n long long bm(long long a, long long b, long long mod) { n   if (b == 0) return 1; n   long long t = bm(a, b / 2, mod); n   t = t * t % mod; n   return (b % 2 == 1 ? t * a % mod : t); n } n long long inv(long long a, long long mod) { return bm(a, mod - 2, mod); } n void gay(long long TC) { cout <<  Case #  << TC <<  :  ; } n void solve(long long TC) { n   string s; n   cin >> s; n   long long n = s.length(); n   if (n % 2) { n     cout <<  NO n ; n     return; n   } n   if (s.substr(0, n / 2) != s.substr(n / 2, n / 2)) { n     cout <<  NO n ; n   } else n     cout <<  YES n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   for (long long i = 1; i <= t; i++) solve(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, i, a[1000021], j; n int main() { n   scanf( %d%d , &n, &k); n   if (n / k <= 2) { n     cout << -1; n     exit(0); n   } n   for (i = 1; i <= k * ((n / k) - 1); i++) a[i] = (i - 1) / ((n / k) - 1) + 1; n   j = ((n / k) - 1) * k; n   while (j <= n) n     for (i = 1; i <= k; i++) n       if (j <= n) a[++j] = i; n   for (i = 1; i <= n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n int aa[100005]; n int main(void) { n   int n; n   while (~scanf( %d , &n)) { n     for (int i = 0; i < n; i++) { n       scanf( %d , &aa[i]); n     } n     for (int i = 0; i < n - 1; i++) { n       printf( %d  , aa[i] + aa[i + 1]); n     } n     printf( %d n , aa[n - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n static const int maxn = 3e5 + 5; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   multiset<int> ms; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     ms.insert(x); n   } n   int ans = 0; n   while (ms.empty() == false) { n     int mini = *ms.begin(); n     ++ans; n     vector<int> del; n     for (int x : ms) { n       if (x % mini == 0) del.push_back(x); n     } n     for (int x : del) ms.erase(ms.find(x)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n int vis[200005]; n int comp_size; n int cycle_length; n void dfs(int cur, int prev, int t, vector<vector<int>> &edges) { n   vis[cur] = t; n   comp_size++; n   for (int e : edges[cur]) { n     if (vis[e] == -1) { n       dfs(e, cur, t + 1, edges); n     } else if (e != prev) { n       if (cycle_length == 0) cycle_length = t - vis[e] + 1; n     } n   } n } n long long mod_exp(int a, int b) { n   if (b == 0) { n     return 1; n   } else if (b == 1) { n     return a; n   } n   long long c; n   if (b % 2 == 0) { n     c = mod_exp(a, b / 2); n     c = c * c % MOD; n   } else { n     c = mod_exp(a, (b - 1) / 2); n     c = c * c * a % MOD; n   } n   return c; n } n int main() { n   int n; n   memset(vis, -1, sizeof vis); n   cin >> n; n   vector<vector<int>> edges(n); n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     a--; n     edges[i].push_back(a); n     edges[a].push_back(i); n   } n   long long ans = 1; n   for (int i = 0; i < n; i++) { n     if (vis[i] == -1) { n       comp_size = 0; n       cycle_length = 0; n       dfs(i, -1, 1, edges); n       cycle_length = cycle_length == 0 ? 2 : cycle_length; n       ans = (ans * mod_exp(2, comp_size - cycle_length) % MOD) * n             (mod_exp(2, cycle_length) - 2) % MOD; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw[16], t[20], na[30], ma[30], ta[30], per[30]; n vector<long long> rem; n void calc_pw() { n   long long i; n   pw[0] = 1; n   for (i = 1; i <= 11; i++) pw[i] = 7 * pw[i - 1]; n } n long long conv(long long num) { n   long long ret = 0, temp; n   temp = num; n   while (temp > 0) { n     t[ret++] = temp % 7; n     temp /= 7; n   } n   return ret; n } n int main() { n   long long i, j, n, m, temp, mx_n, mx_m, cur, ans = 0, idx, k, x, t1 = 0, n                                                t2 = 0; n   cin >> n >> m; n   calc_pw(); n   for (i = 0; i < 7; i++) rem.push_back(i); n   mx_n = max(1LL, conv(n - 1)); n   mx_m = max(1LL, conv(m - 1)); n   for (k = 0; k < (1 << rem.size()); k++) { n     temp = 0; n     for (x = 0; x < rem.size(); x++) n       if (k & (1 << x)) temp++; n     if (temp == mx_m + mx_n && temp > 0) { n       idx = 0; n       for (j = 0; j < rem.size(); j++) n         if (k & (1 << j)) per[idx++] = rem[j]; n       do { n         idx = 0; n         t1 = 0; n         t2 = 0; n         for (j = mx_m - 1; j >= 0; j--) t1 += pw[j] * per[j]; n         for (j = mx_m; j < mx_m + mx_n; j++) t2 += pw[idx++] * per[j]; n         if (t1 < m && t2 < n && (t1 != 0 || t2 != 0)) ans++; n       } while (next_permutation(per, per + mx_m + mx_n)); n     } n   } n   cout << ans; 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   int t; n   cin >> t; n   while (t--) { n     long long x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (x1 == x2) { n       cout << abs(y1 - y2) << endl; n     } else if (y1 == y2) { n       cout << abs(x1 - x2) << endl; n     } else n       cout << abs(x1 - x2) + abs(y1 - y2) + 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, s; n     cin >> n >> s; n     long long cnt = 0, x = n; n     while (x) { n       cnt += x % 10; n       x /= 10; n     } n     long long ans = 0, now = 1; n     while (1) { n       if (cnt <= s) { n         cout << ans << endl; n         break; n       } n       long long p = n % 10; n       if (!p) p = 10; n       ans += (10 - p) * now; n       n += 10 - p; n       n /= 10; n       x = n; n       cnt = 0; n       while (x) { n         cnt += x % 10; n         x /= 10; n       } n       now *= 10; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, count = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %1d , &x); n     if (x) { n       count++; n       continue; n     } n     break; n   } n   cout << min(count + 1, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y; n int work() { n   int ans = 0; n   int b = x % m; n   if (b == 0) b = m; n   int a = x / m + (b != m); n   int d = y % m; n   if (d == 0) d = m; n   int c = y / m + (d != m); n   if (a == c) return 1; n   if (b == 1 && (d == m || y == n)) return 1; n   if (x == 1) { n     if (c == 1) return 1; n     if (d == m) return 1; n     if (y == n) return 1; n     return 2; n   } n   if (a + 1 == c) return 2; n   if (d + 1 == b) return 2; n   if (b == 1) return 2; n   if (d == m) return 2; n   if (y == n) return 2; n   return 3; n } n int main() { n   while (cin >> n >> m >> x >> y) printf( %d , work()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 10; n map<long long, long long> mp; n int main() { n   long long n, k, p, x, ans = 0; n   scanf( %lld%lld%lld , &n, &p, &k); n   for (long long i = 0; i < n; ++i) { n     scanf( %lld , &x); n     mp[((x * x % p * x % p * x % p - x * k % p) % p + p) % p]++; n   } n   for (auto e : mp) { n     x = e.second; n     ans += x * (x - 1) / 2; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int maxc = 0; n   for (int i = 0; i < n; i++) { n     int minc = INF; n     for (int j = 0; j < m; j++) { n       int cij; n       scanf( %d , &cij); n       if (minc > cij) minc = cij; n     } n     if (maxc < minc) maxc = minc; n   } n   printf( %d n , maxc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long gcd(long long a, long long b) { n   a = ((a) < 0 ? -(a) : (a)); n   b = ((b) < 0 ? -(b) : (b)); n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n const long long inf = 2147383647; n const long long mod = 1000000007; n const double pi = 2 * acos(0.0); n const double eps = 1e-9; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   int t; n   cin >> t; n   int x, y, z, a, b, c, ans; n   for (int kas = 1; kas <= t; kas++) { n     scanf( %d , &x); n     scanf( %d , &y); n     scanf( %d , &z); n     a = min(x, y); n     b = max(x, y); n     c = b - a; n     if (z <= a) { n       ans = (x + y + z) / 3; n       ans = min(ans, a); n     } else n       ans = a; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  9  || s[i] ==  H  || s[i] ==  Q ) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n int Pos(int a) { n   if (a < 0) { n     return 0; n   } n   return a; n } n int main() { n   int n = 0, p1 = 0, t1 = 0, p2 = 0, t2 = 0, p3 = 0; n   int energy = 0; n   std::vector<int> timeDatal; n   std::vector<int> timeDatar; n   std::cin >> n >> p1 >> p2 >> p3 >> t1 >> t2; n   for (int i = 0; i < n; i++) { n     int input; n     std::cin >> input; n     timeDatal.push_back(input); n     std::cin >> input; n     timeDatar.push_back(input); n     energy += p1 * (timeDatar[i] - timeDatal[i]); n     if (i != 0) { n       int delta = timeDatal[i] - timeDatar[i - 1]; n       if (delta - t1 >= 0) { n         energy += t1 * p1; n       } else if (delta >= 0) { n         energy += delta * p1; n       } n       delta -= t1; n       if (delta - t2 >= 0) { n         energy += t2 * p2; n       } else if (delta >= 0) { n         energy += delta * p2; n       } n       delta -= t2; n       if (delta > 0) { n         energy += p3 * delta; n       } n     } n   } n   std::cout << energy; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010, mod = 998244353; n inline int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= ch ==  - , ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n int n, fa[maxn], dep[maxn], last = 1, chg, ans[maxn], al, seq[maxn], sl; n set<int> s[maxn]; n int main() { n   n = read(); n   for (int i = (2); i <= (n); i++) { n     dep[i] = dep[fa[i] = read() + 1] + 1; n     s[fa[i]].insert(i); n     if (dep[i] > dep[last]) last = i; n   } n   chg = last; n   while (chg != 1) { n     while (chg != 1 && s[fa[chg]].size() == 1) chg = fa[chg]; n     if (chg != 1) { n       int was = fa[chg]; n       set<int>::iterator hhh = s[was].find(chg); n       for (set<int>::iterator it = s[was].begin(); it != s[was].end(); it++) { n         int f = *it; n         if (f == chg) continue; n         s[f].insert(chg); n         fa[chg] = f; n         ans[++al] = chg - 1; n         s[was].erase(hhh); n         break; n       } n     } n   } n   for (int i = last; i; i = fa[i]) seq[++sl] = i - 1; n   for (int i = (n); i >= (1); i--) printf( %d  , seq[i]); n   printf(  n%d n , al); n   for (int i = (al); i >= (1); i--) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n * k); n   for (auto &it : a) cin >> it; n   reverse(a.begin(), a.end()); n   long long ans = 0; n   int mid = n / 2, cnt = 0; n   for (int i = mid; i < (int)a.size() && cnt++ != k; i += mid + 1) { n     ans += a[i]; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 101111, maxm = 5011, inf = 2147000000; n struct orz { n   int d, num; n } a[maxn]; n int n, v, ans; n bool cmp(orz x, orz y) { return (x.d < y.d); } n int main() { n   scanf( %d%d , &n, &v); n   int i, j, x, y; n   for (i = 1; i <= n; i++) scanf( %d%d , &a[i].d, &a[i].num); n   sort(a + 1, a + n + 1, cmp); n   x = 1; n   for (i = 2; i <= n; i++) n     if (a[i].d == a[x].d) n       a[x].num += a[i].num; n     else n       a[++x] = a[i]; n   n = x; n   a[0].d = -10; n   int res; n   int now = 1; n   a[n + 1].d = -1; n   for (i = 1; i <= 3100; i++) { n     while (a[now].d < i - 1 && now < n) now++; n     res = v; n     if (a[now].d == i - 1 || a[now].d == i) { n       if (a[now].num <= res) { n         res -= a[now].num; n         a[now].num = 0; n       } else { n         a[now].num -= res; n         res = 0; n       } n     } n     if (a[now].d == i - 1 && res) { n       now++; n       if (a[now].d == i) { n         if (a[now].num <= res) { n           res -= a[now].num; n           a[now].num = 0; n         } else { n           a[now].num -= res; n           res = 0; n         } n       } n     } n     ans += v - res; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MXN = 1e2 * 2; n const int TMXN = 1048576; n const long long INF = 1e9 + 7; n const long long INFL = 1e18; n struct query { n   int l, r, id; n }; n int n; n int a[TMXN]; n long long ans[TMXN]; n long long res; n int cnt[TMXN]; n int m, k; n query q[TMXN]; n bool cmp(query a, query b) { n   if (a.l / 447 != b.l / 447) return (a.l / 447 < b.l / 447); n   return a.r < b.r; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].l; n     q[i].id = i; n   } n   int L = 0, R = -1; n   sort(q + 1, q + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     while (q[i].r > R) { n       R++; n       res += (((cnt[a[R]] + 1) * (cnt[a[R]] + 1) * a[R])) - n              ((cnt[a[R]] * cnt[a[R]]) * a[R]); n       cnt[a[R]]++; n     } n     while (q[i].l < L) { n       L--; n       res += (((cnt[a[L]] + 1) * (cnt[a[L]] + 1) * a[L])) - n              ((cnt[a[L]] * cnt[a[L]]) * a[L]); n       cnt[a[L]]++; n     } n     while (q[i].r < R) { n       res -= ((cnt[a[R]] * cnt[a[R]]) * a[R]) - n              (((cnt[a[R]] - 1) * (cnt[a[R]] - 1) * a[R])); n       cnt[a[R]]--; n       R--; n     } n     while (q[i].l > L) { n       res -= ((cnt[a[L]] * cnt[a[L]]) * a[L]) - n              (((cnt[a[L]] - 1) * (cnt[a[L]] - 1) * a[L])); n       cnt[a[L]]--; n       L++; n     } n     ans[q[i].id] = res; n   } n   for (int i = 1; i <= m; i++) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int a, s, d; n   long long x, y; n   scanf( %d%d%d , &a, &s, &d); n   x = a / d; n   y = s / d; n   if (x * d < a) x++; n   if (y * d < s) y++; n   printf( %lld , x * y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a, b, c, res = LONG_LONG_MAX; n int main() { n   cin >> n >> a >> b >> c; n   if (n % 4 == 0) return puts( 0 ); n   if (n % 4 == 1) { n     res = min(res, a * 3); n     res = min(res, a + b); n     res = min(res, c); n   } else if (n % 4 == 2) { n     res = min(res, a * 2); n     res = min(res, b); n     res = min(res, c * 2); n   } else { n     res = min(res, a); n     res = min(res, b + c); n     res = min(res, c * 3); n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n vector<int> v[N]; n vector<pair<int, int> > ans; n int deg[N], maxT; n void dfs(int x, int T, int u) { n   ans.push_back({x, T}); n   int beginT = T; n   for (auto it : v[x]) n     if (it != u) { n       if (T == maxT) { n         T = maxT - deg[x]; n         ans.push_back({x, T}); n       } n       dfs(it, T + 1, x); n       ans.push_back({x, ++T}); n     } n   if (T >= beginT) ans.push_back({x, beginT - 1}); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n     deg[x]++; n     deg[y]++; n   } n   for (int i = 1; i <= n; i++) maxT = max(maxT, deg[i]); n   dfs(1, 0, 0); n   ans.pop_back(); n   cout << ans.size() <<   n ; n   for (auto it : ans) cout << it.first <<     << it.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n const lint linf = 1e18 + 7; n const lint inf = 1e9 + 7; n const int MOD = 1000000007; n void press(vector<long long>& x) { n   map<int, int> memo; n   int n = x.size(); n   for (int i = 0; i < n; ++i) { n     memo[x[i]] += 1; n   } n   int cur = 1; n   for (auto& val : memo) { n     val.second = cur; n     ++cur; n   } n   for (int i = 0; i < n; ++i) { n     x[i] = memo[x[i]]; n   } n } n void solve() { n   int n; n   cin >> n; n   vector<lint> a(n, 0); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   auto b = a; n   press(b); n   vector<vector<int>> dp(n + 5, vector<int>(3, 0)); n   vector<int> cnt(n + 5, 0); n   for (int i = 0; i < n; ++i) cnt[b[i]] += 1; n   int res = n - 1; n   for (int i = 0; i < n; ++i) { n     if (dp[b[i]][0] == 0) { n       dp[b[i]][0] = max(dp[b[i] - 1][1], dp[b[i] - 1][2]) + 1; n     } n     if (dp[b[i]][1] + 1 < cnt[b[i]]) { n       ++dp[b[i]][1]; n     } else { n       dp[b[i]][2] = dp[b[i]][0] + dp[b[i]][1]; n       dp[b[i]][1] += 1; n     } n     int hoge = dp[b[i]][1] + cnt[b[i] + 1] - dp[b[i] + 1][1]; n     res = min(res, n - hoge); n     hoge = cnt[b[i]] - dp[b[i]][1] + dp[b[i] - 1][2] + 1; n     res = min(res, n - hoge); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < 3; ++j) { n       res = min(res, n - dp[i][j]); n     } n   } n   cout << res <<   n ; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 9; n struct edge { n   int to, nxt; n } e[N * 2]; n int hd[N], tot; n void add(int u, int v) { n   e[++tot] = (edge){v, hd[u]}; n   hd[u] = tot; n } n int n, sz[N], msz[N]; n void pre(int u, int fa) { n   for (int i = hd[u], v; i; i = e[i].nxt) n     if ((v = e[i].to) != fa) { n       pre(v, u); n       sz[u] += sz[v]; n       if (sz[v] > sz[msz[u]]) msz[u] = v; n     } n   sz[u]++; n } n int f[N][2], mx[N]; n void dfs1(int u, int fa) { n   for (int i = hd[u], v; i; i = e[i].nxt) n     if ((v = e[i].to) != fa) { n       dfs1(v, u); n       int tmp = (sz[v] > n / 2 ? f[v][0] : sz[v]); n       if (tmp > f[u][0]) n         f[u][1] = f[u][0], f[u][0] = tmp, mx[u] = v; n       else if (tmp > f[u][1]) n         f[u][1] = tmp; n     } n } n int g[N]; n void dfs2(int u, int fa) { n   for (int i = hd[u], v; i; i = e[i].nxt) n     if ((v = e[i].to) != fa) { n       int tmp = (n - sz[v] > n / 2 ? g[u] : n - sz[v]); n       g[v] = max(g[v], tmp); n       if (v == mx[u]) n         g[v] = max(g[v], f[u][1]); n       else n         g[v] = max(g[v], f[u][0]); n       dfs2(v, u); n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; i++) scanf( %d%d , &u, &v), add(u, v), add(v, u); n   pre(1, 0), dfs1(1, 0), dfs2(1, 0); n   for (int i = 1; i <= n; i++) { n     bool centroid = 1; n     if (sz[msz[i]] > n / 2) n       centroid = (sz[msz[i]] - f[i][0] <= n / 2); n     else if (n - sz[i] > n / 2) n       centroid = (n - sz[i] - g[i] <= n / 2); n     cout << centroid <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n inline T range(T l, T r) { n   return uniform_int_distribution<T>(l, r)(rng); n } n inline void setin(string s) { freopen(s.c_str(),  r , stdin); } n inline void setout(string s) { freopen(s.c_str(),  w , stdout); } n template <typename T> n void Min(T &a, T b) { n   a = min(a, b); n } n template <typename T> n void Max(T &a, T b) { n   a = max(a, b); n } n const int mod = 998244353; n const int inf = 0x3f3f3f3f; n const int N = 1e6 + 15; n int n; n vector<int> g[N]; n int cnt[N], cl[N]; n void dfs(int v, int p = -1) { n   cnt[cl[v]]++; n   for (int to : g[v]) { n     if (to != p) { n       cl[to] = cl[v] ^ 1; n       dfs(to, v); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1); n   cout << min(cnt[0], cnt[1]) - 1 <<   n ; 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 long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int n, m, len1 = 1, len2 = 1, ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int a = 7; a < n; a *= 7) len1 += 1; n   for (int b = 7; b < m; b *= 7) len2 += 1; n   if (len1 + len2 <= 7) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) { n         vector<int> used(7, 0); n         for (int a = i, k = 0; k < len1; a /= 7, k++) used[a % 7] += 1; n         for (int b = j, k = 0; k < len2; b /= 7, k++) used[b % 7] += 1; n         if (*max_element(used.begin(), used.end()) <= 1) ans++; n       } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char n[1000 + 2]; n char m[1000 + 2]; n int color_n[30]; n int color_m[30]; n int main() { n   while (scanf( %s , n) != EOF) { n     scanf( %s , m); n     memset(color_n, 0, sizeof(color_n)); n     memset(color_m, 0, sizeof(color_m)); n     for (int i = 0; i < strlen(n); i++) color_n[n[i] -  a ]++; n     for (int j = 0; j < strlen(m); j++) color_m[m[j] -  a ]++; n     int ans = 0; n     int flag = 1; n     for (int i = 0; i < 30; i++) { n       ans += min(color_n[i], color_m[i]); n       if (color_m[i] && color_n[i] == 0) flag = 0; n     } n     if (ans == 0 || flag == 0) n       printf( -1 n ); n     else n       printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int N, X, sum = 0; n   std::cin >> N >> X; n   for (int i = 0; i < N; i++) { n     int tmp; n     std::cin >> tmp; n     sum += tmp; n   } n   if (sum + N - 1 == X) n     std::cout <<  YES n ; n   else n     std::cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200000 + 10; n inline long long rd() { n   long long x = 0, w = 1; n   char ch = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * w; n } n int to[N], nt[N], hd[N], tot = 1; n inline void add(int x, int y) { n   ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot; n } n int n, m, k, a[N], len[N], ans; n int st[N], tt = -1; n inline void dfs(int x) { n   ++tt, st[tt] = st[tt - 1] + len[x]; n   ans = max(ans, st[tt] - st[max(0, tt - m)]); n   if (tt < m) n     for (int i = hd[x]; i; i = nt[i]) dfs(to[i]); n   --tt; n } n int main() { n   n = rd(), m = rd(), k = rd(); n   for (int i = 1; i <= n; i++) a[i] = rd(); n   for (int i = 1, su = a[1], j = 1; i <= n; i++) { n     while (j < n && su + a[j + 1] <= k) su += a[++j]; n     len[i] = j - i + 1, add(j + 1, i), su -= a[i]; n   } n   dfs(n + 1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[1010]; n int sum = 0; n int cnt = 0; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   for (int i = 0; i < n; i++) { n     if ((sum % 2) == (a[i] % 2)) { n       cnt++; n     } n   } n   printf( %d n , cnt); n } n


#include <bits/stdc++.h> n using namespace std; n istream& in = cin; n int n; n int a[1000010]; n void input() { n   in >> n; n   for (int i = 1; i <= n; ++i) in >> a[i]; n } n long long mod = (long long)1e9 + 7; n long long fac[1000010], inv[1000010]; n long long quick_mod(long long a, long long p) { n   long long ret = 1; n   a %= mod; n   while (p) { n     if (p & 1) ret = ret * a % mod; n     a = a * a % mod; n     p /= 2; n   } n   return ret; n } n long long C(int n, int m) { return fac[n] * inv[m] % mod * inv[n - m] % mod; } n long long cale(int k) { n   long long large = fac[k]; n   long long less = 1; n   less = fac[n] * inv[k + 1] % mod; n   return large * less % mod; n } n int main() { n   fac[0] = 1; n   for (int i = 1; i < 1000010; ++i) fac[i] = fac[i - 1] * i % mod; n   inv[1000009] = quick_mod(fac[1000009], mod - 2); n   for (int i = 1000008; i >= 0; --i) inv[i] = inv[i + 1] * (i + 1) % mod; n   int TEST_CASE = 1; n   while (TEST_CASE-- > 0) { n     input(); n     sort(a + 1, a + n + 1); n     long long ans = 0; n     a[0] = -1; n     int pos = 1, K = n; n     while (pos <= n) { n       int val = a[pos]; n       int num = 0; n       while (a[pos] == val) { n         pos++; n         num++; n       } n       if (val != a[n]) ans = (ans + cale(K - 1) * num % mod * val % mod) % mod; n       K -= num; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n int mod = 1000000007; n using namespace std; n bool isp(long long x) { n   for (long long i = 2; i * i <= x; ++i) n     if (x % i == 0) return false; n   return true; n } n void pog() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n } n bool sortinrev(const pair<long long, long long> &a, n                const pair<long long, long long> &b) { n   return (a.first > b.first); n } n bool prime_check(long long n) { n   bool flag = 1; n   for (long long i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       flag = 0; n       break; n     } n   } n   return flag; n } n long long power(long long x, unsigned long long y, long long p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int32_t main() { n   pog(); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long arr[n]; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n     }; n     long long sum = arr[0] + arr[1]; n     long long flag = 0; n     for (long long i = 2; i < n; i++) n       if (sum <= arr[i]) { n         flag = i + 1; n         break; n       } n     if (flag) n       cout << 1 <<     << 2 <<     << flag <<   n ; n     else n       cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t1, t2; n int s1[100010], s2[100010]; n pair<int, int> c1[100010], c2[100010]; n bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; } n int main() { n   int n, v; n   cin >> n >> v; n   for (int i = 0; i < n; i++) { n     int t, p; n     cin >> t >> p; n     if (t == 1) { n       c1[t1++] = make_pair(p, i + 1); n     } else n       c2[t2++] = make_pair(p, i + 1); n   } n   sort(c1, c1 + t1, cmp); n   sort(c2, c2 + t2, cmp); n   s1[0] = s2[0] = 0; n   for (int i = 0; i < t1; i++) { n     s1[i + 1] = s1[i] + c1[i].first; n   } n   for (int i = 0; i < t2; i++) { n     s2[i + 1] = s2[i] + c2[i].first; n   } n   int ans = -1; n   int tmp = -1; n   for (int i = 0; i <= t2; i++) { n     if (i * 2 <= v && s1[min(v - i * 2, t1)] + s2[i] > ans) { n       ans = s1[min(v - i * 2, t1)] + s2[i]; n       tmp = i; n     } n   } n   cout << ans << endl; n   for (int i = 0; i < min(v - tmp * 2, t1); i++) cout << c1[i].second << endl; n   for (int i = 0; i < tmp; i++) cout << c2[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n const long long N = 107, MOD = 1e9 + 7; n long long n, K, c; n bool s[N]; n struct Mat { n   long long val[N][N]; n   long long Max_n, Max_m; n   Mat() { n     memset(val, 0, sizeof(val)); n     Max_n = Max_m = 0; n   } n   Mat operator*(const Mat el) const { n     Mat c; n     for (long long i = 0; i <= Max_n; ++i) n       for (long long j = 0; j <= el.Max_m; ++j) n         for (long long k = 0; k <= Max_m; ++k) n           c.val[i][j] = n               (c.val[i][j] + ((val[i][k] * el.val[k][j]) % MOD)) % MOD; n     c.Max_n = Max_n, c.Max_m = el.Max_m; n     return c; n   } n   inline void I(long long n) { n     Max_n = Max_m = n; n     for (long long i = 0; i <= n; ++i) val[i][i] = 1; n   } n }; n inline long long power(long long x, long long y) { n   long long res = 1, base = x; n   while (y) { n     if (y & 1) res = (res * base) % MOD; n     base = (base * base) % MOD; n     y >>= 1; n   } n   return res; n } n signed main() { n   n = read(), K = read(); n   for (long long i = 1; i <= n; ++i) s[i] = read(), c += (s[i] == 0); n   Mat A, F; n   long long t = 0; n   for (long long i = 1; i <= c; ++i) n     if (s[i] == 0) ++t; n   F.val[0][t] = 1; n   F.Max_n = 1, F.Max_m = c; n   for (long long i = 0; i <= c; ++i) { n     if (i != 0) A.val[i - 1][i] = (c - i + 1) * (c - i + 1); n     A.val[i][i] = (c * (c - 1) / 2) + ((n - c) * (n - c - 1) / 2) + n                   (i * (c - i)) + (c - i) * (n - c - c + i); n     if (i != c) A.val[i + 1][i] = (i + 1) * (n - c - c + i + 1); n   } n   A.Max_n = A.Max_m = c; n   long long y = K; n   Mat res; n   res.I(c); n   while (y) { n     if (y & 1) res = res * A; n     A = A * A; n     y >>= 1; n   } n   F = F * res; n   long long ans = F.val[0][c], sum = 0; n   for (long long i = 0; i <= c; ++i) sum = (sum + F.val[0][i]) % MOD; n   ans = (ans * power(sum, MOD - 2)) % MOD; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inft = 1000000009; n const int mod = 1000000007; n const int MAXN = 100006; n int f[MAXN]; n const int N = MAXN, P = MAXN; n vector<int> G[N]; n pair<int, int> E[MAXN], Q[MAXN]; n int C[MAXN]; n namespace LCAO { n vector<pair<int, int> > pyt[N]; n int LCA[P]; n int kolor[N], REP[N], przodek[N]; n int find(int first) { n   return REP[first] < 0 ? first : REP[first] = find(REP[first]); n } n void DfsTarjan(int first, int par) { n   kolor[first] = 1; n   REP[first] = -1; n   przodek[first] = first; n   for (typeof((G[first]).begin()) it = (G[first]).begin(); n        it != (G[first]).end(); ++it) n     if (*it != par) { n       DfsTarjan(*it, first); n       int a = find(first), b = find(*it); n       if (REP[a] < REP[b]) swap(a, b); n       REP[b] += REP[a]; n       REP[a] = b; n       przodek[b] = first; n     } n   for (typeof((pyt[first]).begin()) it = (pyt[first]).begin(); n        it != (pyt[first]).end(); ++it) n     if (kolor[it->first] == 2) { n       int lca = przodek[find(it->first)]; n       LCA[it->second] = lca; n     } n   kolor[first] = 2; n } n void dodaj_krawedz(int a, int b) { n   G[a].push_back(b); n   G[b].push_back(a); n } n int nr = 0; n void dodaj_pytanie(int a, int b) { n   if (a == b) n     LCA[nr] = a; n   else { n     pyt[a].push_back(pair<int, int>(b, nr)); n     pyt[b].push_back(pair<int, int>(a, nr)); n   } n   nr++; n } n void czysc(int n = N) { n   for (int i = 0; i < (n); ++i) G[i].clear(), pyt[i].clear(), kolor[i] = 0; n   nr = 0; n } n }  // namespace LCAO n pair<int, pair<int, int> > rev(pair<int, pair<int, int> > a) { n   return pair<int, pair<int, int> >( n       a.first, pair<int, int>(a.second.second, a.second.first)); n } n pair<int, pair<int, int> > merge(pair<int, pair<int, int> > t1, n                                  pair<int, pair<int, int> > t2) { n   ; n   if (t1.first == -inft) { n     if (t2.first == -inft) { n       ; n       return pair<int, pair<int, int> >( n           -inft, pair<int, int>(t1.second.first + t2.second.first, n                                 t1.second.first + t2.second.first)); n     } else { n       ; n       return pair<int, pair<int, int> >( n           t2.first, n           pair<int, int>(t1.second.first + t2.second.first, t2.second.second)); n     } n   } else if (t2.first == -inft) { n     ; n     return pair<int, pair<int, int> >( n         t1.first, n         pair<int, int>(t1.second.first, t1.second.second + t2.second.second)); n   }; n   return pair<int, pair<int, int> >( n       t1.first + t2.first + f[t1.second.second + t2.second.first], n       pair<int, int>(t1.second.first, t2.second.second)); n } n int eval(pair<int, pair<int, int> > t1) { n   ; n   if (t1.first == -inft) return f[t1.second.first]; n   return f[t1.second.first] + f[t1.second.second] + t1.first; n } n struct drzewo { n   int T; n   vector<pair<int, pair<int, int> > > drz; n   void init(int n) { n     T = 1; n     while (T < n) T *= 2; n     drz.resize(2 * T, pair<int, pair<int, int> >(0, pair<int, int>(0, 0))); n   } n   void update(int a, int b) { n     assert(a == b); n     a += T; n     drz[a] = pair<int, pair<int, int> >(-inft, pair<int, int>(1, 1)); n     a /= 2; n     while (a) { n       drz[a] = merge(drz[2 * a], drz[2 * a + 1]); n       a /= 2; n     } n   } n   pair<int, pair<int, int> > get(int a, int b) { n     pair<int, pair<int, int> > ret1 = n         pair<int, pair<int, int> >(-inft, pair<int, int>(0, 0)); n     pair<int, pair<int, int> > ret2 = n         pair<int, pair<int, int> >(-inft, pair<int, int>(0, 0)); n     a += T; n     b += T; n     while (a <= b) { n       if (a % 2 == 1) { n         ret1 = merge(ret1, drz[a]); n         a++; n       } n       if (b % 2 == 0) { n         ret2 = merge(drz[b], ret2); n         b--; n       } n       a /= 2; n       b /= 2; n     } n     return merge(ret1, ret2); n   } n }; n namespace HLD { n drzewo CS[N]; n int nrc[N]; n int ind[N]; n int hop[N]; n int siz[N]; n int sciezek; n pair<int, pair<int, int> > get(int a, int anc) { n   ; n   pair<int, pair<int, int> > ret(-inft, pair<int, int>(0, 0)); n   while (nrc[a] != nrc[anc]) { n     ret = merge(ret, rev(CS[nrc[a]].get(0, ind[a]))); n     a = hop[nrc[a]]; n   } n   ret = merge(ret, rev(CS[nrc[a]].get(ind[anc] + 1, ind[a]))); n   ; n   return ret; n } n void update(int a, int anc) { n   ; n   while (nrc[a] != nrc[anc]) { n     CS[nrc[a]].update(0, ind[a]); n     a = hop[nrc[a]]; n   } n   CS[nrc[a]].update(ind[anc], ind[a]); n } n int rozm(int u, int pr) { n   siz[u] = 1; n   for (typeof((G[u]).begin()) it = (G[u]).begin(); it != (G[u]).end(); ++it) n     if (*it != pr) siz[u] += rozm(*it, u); n   return siz[u]; n } n void ciezka(int u, int pr, int csc, int index) { n   nrc[u] = csc; n   ind[u] = index; n   int dz = -1; n   for (typeof((G[u]).begin()) it = (G[u]).begin(); it != (G[u]).end(); ++it) n     if (*it != pr && (dz == -1 || siz[*it] > siz[dz])) dz = *it; n   for (typeof((G[u]).begin()) it = (G[u]).begin(); it != (G[u]).end(); ++it) n     if (*it == dz) n       ciezka(*it, u, csc, index + 1); n     else if (*it != pr) { n       hop[sciezek++] = u; n       ciezka(*it, u, sciezek - 1, 0); n     } n   if (dz == -1) { n     CS[csc].init(index + 1); n   } n } n void init(int root) { n   rozm(root, -1); n   sciezek = 1; n   hop[0] = -1; n   ciezka(root, -1, 0, 0); n } n }  // namespace HLD n void solve() { n   int n, q; n   scanf( %d%d , &n, &q); n   f[0] = 0; n   for (int i = 0; i < (n - 1); ++i) { n     scanf( %d , &f[i + 1]); n   } n   vector<pair<int, int> > EV; n   for (int i = 0; i < (n - 1); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     LCAO::dodaj_krawedz(a, b); n     E[i] = pair<int, int>(a, b); n     EV.push_back(pair<int, int>(c + 1, -(i + 1))); n   } n   for (int i = 0; i < (q); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     LCAO::dodaj_pytanie(a, b); n     Q[i] = pair<int, int>(a, b); n     EV.push_back(pair<int, int>(c, i)); n   } n   vector<int> ANS(q); n   LCAO::DfsTarjan(0, -1); n   HLD::init(0); n   for (int i = 0; i < (n - 1); ++i) n     if (HLD::siz[E[i].first] > HLD::siz[E[i].second]) n       swap(E[i].first, E[i].second); n   sort((EV).begin(), (EV).end()); n   reverse((EV).begin(), (EV).end()); n   for (typeof((EV).begin()) it = (EV).begin(); it != (EV).end(); ++it) { n     if (it->second >= 0) { n       ; n       int u = LCAO::LCA[it->second], u1 = Q[it->second].first, n           u2 = Q[it->second].second; n       pair<int, pair<int, int> > t1 = HLD::get(u1, u), t2 = HLD::get(u2, u); n       t1 = merge(t1, rev(t2)); n       ANS[it->second] = eval(t1); n     } else { n       int u = -it->second - 1; n       ; n       HLD::update(E[u].first, E[u].first); n     } n   } n   for (typeof((ANS).begin()) it = (ANS).begin(); it != (ANS).end(); ++it) n     printf( %d n , *it); n } n int main() { n   int te = 1; n   for (int ti = 0; ti < (te); ++ti) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   double lift[n], land[n]; n   for (int i = 0; i < (n); i++) cin >> lift[i] >> land[i]; n   double ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     ans = (ans + m / land[(i + 1) % n]) / (1 - 1 / land[(i + 1) % n]); n     if (ans < 0 || ans > 1e9) return !(cout << -1); n     ans = (ans + m / lift[i]) / (1 - 1 / lift[i]); n     if (ans < 0 || ans > 1e9) return !(cout << -1); n   } n   cout << fixed << setprecision(7) << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n long long int a[N], l[N]; n int main() { n   int n; n   long long int ans(0); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lld , &a[i]), a[i]++; n   long long int o(a[n] + 1); n   for (int i = n; i >= 1; --i) l[i] = o = max(--o, a[i]); n   for (int i = 1; i <= n; ++i) l[i] = max(l[i], l[i - 1]); n   for (int i = 1; i <= n; ++i) ans += (l[i] - a[i]); n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int add(int a, int b, int m = 1000000007) { n   a += b; n   if (a >= m) a -= m; n   return a; n } n inline int mul(int a, int b, int m = 1000000007) { n   return (int)(((long long)a * (long long)b) % m); n } n inline int norm(int x, int m = 1000000007) { n   if (x >= m) x %= m; n   if (x < 0) x += m; n   return x; n } n inline int neg(int x, int m = 1000000007) { n   x = -x; n   return norm(x); n } n int c[501]; n int dp[501][501]; n int f(int i, int j) { n   if (i == j) return 1; n   if (i == j - 1) return 2 - (int)(c[i] == c[j]); n   int &ans = dp[i][j]; n   if (ans != -1) return ans; n   ans = 1000000007; n   for (int k = i; k <= j - 1; ++k) { n     ans = min(ans, f(i, k) + f(k + 1, j)); n   } n   if (c[i] == c[j]) ans = min(ans, f(i + 1, j - 1)); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> c[i]; n   memset(dp, -1, sizeof(dp)); n   cout << f(1, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acosl(-1.0); n const double EPS = 1e-12; n void combination(long long com[][1005]) { n   com[0][0] = 1; n   int sz = 1000; n   for (int i = 1; i <= sz; i++) { n     com[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       com[i][j] = com[i - 1][j] + com[i - 1][j - 1]; n       com[i][j] = com[i][j] % 1000000007; n     } n   } n } n int find_par(int cn) {} n int gcd(int u, int v) { n   if (v == 0) return u; n   return gcd(v, u % v); n } n void insert_ind(int cs, int ci, int s, int e) { n   if (s == e) { n     return; n   } n   int mid = (s + e) / 2; n   if (ci <= mid) n     insert_ind(2 * cs, ci, s, mid); n   else n     insert_ind(2 * cs + 1, ci, mid + 1, e); n } n void update_ind(int cs, int ci, int s, int e, int val) { n   if (s == e) { n     return; n   } n   int mid = (s + e) / 2; n   if (ci <= mid) n     update_ind(2 * cs, ci, s, mid, val); n   else n     update_ind(2 * cs + 1, ci, mid + 1, e, val); n } n void update_seg(int cs, int ds, int de, int s, int e, int val) { n   if (ds == s && de == e) { n     return; n   } n   int mid = (s + e) / 2; n   if (ds <= mid) update_seg(2 * cs, ds, min(mid, de), s, mid, val); n   if (de > mid) update_seg(2 * cs + 1, max(mid + 1, ds), de, mid + 1, e, val); n } n void pattern_match(char pat[], const int len, int pi[]) { n   pi[1] = 0; n   for (int i = 2; i <= len; i++) { n     int st = i - 1; n     while (st >= 0 && pat[pi[st] + 1] != pat[i]) st = pi[st]; n     if (st == 0) n       pi[i] = 0; n     else n       pi[i] = pi[st] + 1; n   } n } n void pattern_match(char pat[], int pi[], char tar[], const int len1, int gi[]) { n   if (pat[0] == tar[0]) gi[0] = 1; n   for (int i = 1; i < len1; i++) { n     int st = gi[i - 1]; n     while (pat[st] != tar[i]) { n       if (st == 0) { n         st = -1; n         break; n       } n       st = pi[st - 1]; n     } n     if (st == -1) n       gi[i] = 0; n     else n       gi[i] = st + 1; n   } n } n struct myArray { n   int mat[10][10]; n }; n myArray matrix_mul(myArray mat1, myArray mat2, const int sz) { n   int tmp[sz][sz]; n   memset((tmp), 0, sizeof((tmp))); n   for (int r = 0; r < sz; r++) n     for (int c = 0; c < sz; c++) n       for (int cr = 0; cr < sz; cr++) n         tmp[r][c] += mat1.mat[r][cr] * mat2.mat[c][cr]; n   myArray res; n   for (int r = 0; r < sz; r++) n     for (int c = 0; c < sz; c++) res.mat[r][c] = tmp[r][c]; n   return res; n } n myArray unit_matrix(const int sz) { n   myArray ma; n   memset((ma.mat), 0, sizeof((ma.mat))); n   for (int i = 0; i < sz; i++) ma.mat[i][i] = 1; n   return ma; n } n myArray matrix_expo(int p, myArray mat, const int sz) { n   if (p == 0) { n     return unit_matrix(sz); n   } n   if (p & 1) return matrix_mul(mat, matrix_expo(p - 1, mat, sz), sz); n   myArray tmp = matrix_expo(p >> 1, mat, sz); n   return matrix_mul(tmp, tmp, sz); n } n int X[] = {0, 0, 1, -1}; n int Y[] = {1, -1, 0, 0}; n string s; n unsigned long long int l, r; n int main() { n   cin >> s; n   int ln = s.length(); n   unsigned long long int pos = 0; n   for (int i = 0; i < ln; i++) n     if (s[i] ==  ^ ) pos = i; n   for (int i = 0; i < ln; i++) { n     if (i < pos) { n       if (s[i] !=  = ) l = l + (s[i] -  0 ) * (pos - i); n     } else { n       if (s[i] !=  = ) r = r + (s[i] -  0 ) * (i - pos); n     } n   } n   if (l > r) n     cout <<  left  << endl; n   else if (r > l) n     cout <<  right  << endl; n   else n     cout <<  balance  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, rs, j; n vector<long long> ball; n vector<long long>::iterator pos; n int main() { n   scanf( %lld , &n); n   for (i = 0; i < n; i++) scanf( %lld , &j), ball.push_back(j); n   sort(ball.begin(), ball.end()); n   rs = 0; n   i = 0; n   if (n % 2 == 0) { n     j = ball[0] + ball[1]; n     pos = lower_bound(ball.begin() + 2, ball.end(), j); n     ball.insert(pos, j); n     rs += j; n     i = 2; n   } n   for (; i < n - 1; i += 3) { n     j = ball[i] + ball[i + 1] + ball[i + 2]; n     pos = lower_bound(ball.begin() + i + 3, ball.end(), j); n     ball.insert(pos, j); n     rs += j; n     n++; n   } n   printf( %lld , rs); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n int main() { n   char c; n   scanf( %I64d%1c , &n, &c); n   c -=  a ; n   long long ans = 0; n   n -= 1; n   if (n % 4 <= 1) { n     ans += ((n / 4) * 2 + n % 4) * 6 + n; n   } else { n     ans += ((n / 4) * 2 + (n % 4 - 2)) * 6 + n - 2; n   } n   if (c <= 2) { n     ans += 4 + c; n   } else { n     ans += 6 - c; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:10240000 ) n using namespace std; n const int MAXN = 100010; n int a[MAXN], cnt[MAXN]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   memset(cnt, 0, sizeof(cnt)); n   int sta = 1, num = 0; n   while (num < k) { n     if (cnt[a[sta]] == 0) { n       num++; n     } n     cnt[a[sta]]++; n     sta++; n     if (sta > n) { n       break; n     } n   } n   if (num < k) { n     printf( -1 -1 n ); n   } else { n     int old = 1, l = 1, r = sta - 1; n     int m = r - l + 1; n     for (int i = sta; i <= n; i++) { n       int j = old + 1; n       cnt[a[old]]--; n       if (j < i) { n         while (a[j] == a[old]) { n           cnt[a[old]]--; n           j++; n           if (j == i) { n             break; n           } n         } n       } n       if (cnt[a[old]] == 0) { n         num--; n       } n       old = j; n       j = i; n       while (num < k) { n         if (cnt[a[j]] == 0) { n           num++; n         } n         cnt[a[j]]++; n         j++; n         if (j > n) { n           break; n         } n       } n       if (num < k) { n         break; n       } n       i = j - 1; n       int tmp = i - old + 1; n       if (tmp < m) { n         m = tmp; n         l = old; n         r = i; n       } n     } n     memset(cnt, 0, sizeof(cnt)); n     for (int i = l; i <= r; i++) { n       cnt[a[i]]++; n     } n     while (cnt[a[l]] > 1) { n       cnt[a[l]]--; n       l++; n     } n     printf( %d %d n , l, r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 3005; n const int MOD = 1000000007; n const double eps = 1e-8; n const double PI = acos(-1.0); n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n, long long p = MOD) { n   long long r = 1 % p; n   for (a %= p; n; a = a * a % p, n >>= 1) n     if (n & 1) r = r * a % p; n   return r; n } n long long inv(long long x, long long p = MOD) { n   return x <= 1 ? 1 : inv(p % x, p) * (p - p / x) % p; n } n struct node { n   int val, id; n } a[3005]; n int da[3005]; n bool cmpa(node a, node b) { return a.val > b.val; } n bool cmpb(node a, node b) { return a.id < b.id; } n struct RMQ { n   int f[MAXN][21], pos[MAXN][21]; n   int n; n   void init(int a[], int sz) { n     n = sz; n     for (int i = 0; i < n; i++) { n       f[i][0] = a[i]; n       pos[i][0] = i; n     } n     for (int j = 1; (1 << j) <= n; j++) { n       for (int i = 0; i + (1 << j) - 1 < n; i++) { n         if (f[i][j - 1] < f[i + (1 << (j - 1))][j - 1]) { n           f[i][j] = f[i + (1 << (j - 1))][j - 1]; n           pos[i][j] = pos[i + (1 << (j - 1))][j - 1]; n         } else { n           f[i][j] = f[i][j - 1]; n           pos[i][j] = pos[i][j - 1]; n         } n       } n     } n   } n   int query(int l, int r) { n     int k = 0; n     while ((1 << (k + 1)) <= r - l + 1) { n       k++; n     } n     return f[l][k] >= f[r - (1 << k) + 1][k] ? pos[l][k] n                                              : pos[r - (1 << k) + 1][k]; n   } n } myRMQ; n int ans[4]; n inline void update(int i, int j, int k) { ans[1] = i, ans[2] = j, ans[3] = k; } n inline bool check(int i, int j, int k) { n   if (da[i] > da[ans[1]]) { n     return 1; n   } else if (da[i] == da[ans[1]]) { n     if (da[j] > da[ans[2]]) { n       return 1; n     } else if (da[j] == da[ans[2]] && da[k] > da[ans[3]]) { n       return 1; n     } n   } n   return 0; n } n int main(int argc, char* argv[]) { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].val); n     a[i].id = i; n   } n   sort(a + 1, a + n + 1, cmpa); n   for (int i = 1; i <= n; i++) { n     da[i] = a[i].val - a[i + 1].val; n   } n   myRMQ.init(da, n + 1); n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       int cnt[4]; n       cnt[1] = i, cnt[2] = j - i; n       if (cnt[1] > 2 * cnt[2] || cnt[2] > 2 * cnt[1] || n           min(cnt[1], cnt[2]) > 2 * (n - j)) { n         continue; n       } n       int l = j + (max(cnt[1], cnt[2]) + 1) / 2, n           r = j + 2 * min(cnt[1], cnt[2]); n       l = max(l, j + 1), r = min(n, r); n       if (l > r) { n         continue; n       } n       int k = myRMQ.query(l, r); n       if (ans[1] == 0 || check(i, j, k)) { n         update(i, j, k); n       } n     } n   } n   for (int i = 1; i <= ans[1]; i++) { n     a[i].val = 1; n   } n   for (int i = ans[1] + 1; i <= ans[2]; i++) { n     a[i].val = 2; n   } n   for (int i = ans[2] + 1; i <= ans[3]; i++) { n     a[i].val = 3; n   } n   for (int i = ans[3] + 1; i <= n; i++) { n     a[i].val = -1; n   } n   sort(a + 1, a + n + 1, cmpb); n   for (int i = 1; i <= n; i++) { n     printf( %d  , a[i].val); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   for (long long i = 1; i <= n; i++) n     if (!(n % i)) reverse(s.begin(), (s.begin() + i)); n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010, inf = 1e9; n int n, ty, l, r, x; n bitset<maxn> v[26], ans; n char s[maxn], s2[maxn], c; n inline void read(int &k) { n   int f = 1; n   k = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c ==  -  && (f = -1), c = getchar(); n   while (c <=  9  && c >=  0 ) k = k * 10 + c -  0 , c = getchar(); n   k *= f; n } n int main() { n   scanf( %s , s + 1); n   int len = strlen(s + 1); n   for (int i = 1; i <= len; i++) v[s[i] -  a ][i] = 1; n   read(n); n   for (int i = 1; i <= n; i++) { n     read(ty); n     if (ty == 1) { n       read(x); n       scanf( %c , &c); n       v[s[x] -  a ][x] = 0; n       v[(s[x] = c) -  a ][x] = 1; n     } else { n       read(l); n       read(r); n       scanf( %s , s2); n       int m = strlen(s2); n       if (r - l + 1 < m) { n         puts( 0 ); n         continue; n       } n       ans.set(); n       for (int j = 0; j < m; j++) ans &= (v[s2[j] -  a ] >> j); n       printf( %d n , n              (int)(ans >> (l)).count() - (int)(ans >> (r - m + 2)).count()); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int arr[500005], brr[500005]; n vector<int> vv[500005], ans; n int Now[500005]; n int total[500005]; n int tree[500005 * 4]; n void update(int node, int b, int e, int i, int v) { n   if (i < b || i > e) return; n   if (b == e && b == i) { n     tree[node] = v; n     return; n   } n   int left = 2 * node, right = left + 1, mid = (b + e) / 2; n   update(left, b, mid, i, v); n   update(right, mid + 1, e, i, v); n   tree[node] = tree[left] + tree[right]; n } n int query(int node, int b, int e, int v) { n   if (b == e) return b; n   int left = 2 * node, right = left + 1, mid = (b + e) / 2; n   int ret; n   if (tree[left] >= v) n     ret = query(left, b, mid, v); n   else n     ret = query(right, mid + 1, e, v - tree[left]); n   return ret; n } n int main() { n   int n, k, m, s; n   scanf( %d %d %d %d , &n, &k, &m, &s); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     vv[arr[i]].push_back(i); n   } n   for (int i = 1; i <= s; i++) { n     int x; n     scanf( %d , &x); n     brr[x]++; n   } n   if (k < s) { n     cout <<  -1 ; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (total[arr[i]] == brr[arr[i]]) continue; n     Now[arr[i]] = i; n     update(1, 1, n, i, 1); n     total[arr[i]]++; n   } n   if (tree[1] < s) { n     cout <<  -1 ; n     return 0; n   } n   bool flag = 0; n   int id = 0, id2 = 0; n   for (int i = 1; i <= n; i++) { n     int pp = query(1, 1, n, s); n     int z = 0; n     z = i / k; n     if (i % k == 0) z--; n     int ppp = max(pp, i + k - 1); n     z++; n     z += ((n - ppp) / k); n     if (z >= m) { n       flag = 1; n       id = i; n       id2 = pp; n       break; n     } n     update(1, 1, n, i, 0); n     int idd = upper_bound(vv[arr[i]].begin(), vv[arr[i]].end(), Now[arr[i]]) - n               vv[arr[i]].begin(); n     if (idd == vv[arr[i]].size() && brr[arr[i]]) break; n     if (brr[arr[i]]) { n       update(1, 1, n, vv[arr[i]][idd], 1); n       Now[arr[i]] = vv[arr[i]][idd]; n     } n   } n   if (flag == 0) { n     printf( -1 n ); n   } else { n     for (int i = 1; i <= 500000; i++) total[i] = 0; n     for (int i = 1; i < id; i++) { n       if ((id - i) % k) n         ans.push_back(i); n       else n         break; n     } n     if (id2 - id + 1 > k) { n       int tt = id2 - id + 1; n       for (int i = id; i <= id2 && tt > k; i++) { n         if (total[arr[i]] >= brr[arr[i]]) { n           tt--; n           ans.push_back(i); n         } else n           total[arr[i]]++; n       } n     } n     printf( %d n , ans.size()); n     for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2) { n     cout << 7; n     n -= 3; n   } n   while (n) { n     cout << 1; n     n -= 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long llinf = (1LL << 62); n const int inf = (1 << 30); n const long long nmax = 3e5 + 50; n const int mod = 1e9 + 7; n const int lim = 1e6; n using namespace std; n int n, m, a[nmax], b[nmax], st[4 * lim + 50], i, t, x, lzy[4 * lim + 50], q; n void push(int nod, int l, int r) { n   if (l > r) return; n   if (!lzy[nod]) return; n   st[nod] += lzy[nod]; n   if (l != r) { n     lzy[nod * 2] += lzy[nod]; n     lzy[nod * 2 + 1] += lzy[nod]; n   } n   lzy[nod] = 0; n } n void upd(int nod, int l, int r, int tl, int tr, int v) { n   push(nod, l, r); n   if (tl <= l && r <= tr) { n     lzy[nod] += v; n     push(nod, l, r); n     return; n   } n   int mid = (l + r) / 2; n   if (tl <= mid) upd(nod * 2, l, mid, tl, tr, v); n   if (mid < tr) upd(nod * 2 + 1, mid + 1, r, tl, tr, v); n   push(nod * 2, l, mid); n   push(nod * 2 + 1, mid + 1, r); n   st[nod] = max(st[nod * 2], st[nod * 2 + 1]); n } n int qry(int nod, int l, int r) { n   push(nod, l, r); n   if (l == r) return l; n   int mid = (l + r) / 2; n   push(2 * nod, l, mid); n   push(2 * nod + 1, mid + 1, r); n   if (st[2 * nod + 1] > 0) return qry(2 * nod + 1, mid + 1, r); n   return qry(2 * nod, l, mid); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cerr.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     upd(1, 1, lim, 1, a[i], 1); n   } n   for (i = 1; i <= m; i++) { n     cin >> b[i]; n     upd(1, 1, lim, 1, b[i], -1); n   } n   cin >> q; n   while (q--) { n     cin >> t >> i >> x; n     if (t == 1) { n       upd(1, 1, lim, 1, a[i], -1); n       a[i] = x; n       upd(1, 1, lim, 1, a[i], 1); n     } else { n       upd(1, 1, lim, 1, b[i], 1); n       b[i] = x; n       upd(1, 1, lim, 1, b[i], -1); n     } n     if (st[1] <= 0) n       cout << -1 <<   n ; n     else n       cout << qry(1, 1, lim) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, s1, s2; n bool fn() { n   int i, j, k, flag = 0; n   int len = s.size(); n   int len1 = s1.size(); n   int len2 = s2.size(); n   for (i = 0; i < len; i++) { n     for (j = i, k = 0; k < len1; j++, k++) { n       if (s[j] != s1[k] || j >= len) break; n     } n     if (k == len1) { n       flag++; n       break; n     } n   } n   for (i = j; i < len; i++) { n     for (j = i, k = 0; k < len2; j++, k++) { n       if (s[j] != s2[k] || i >= len) break; n     } n     if (k == len2) { n       flag++; n       break; n     } n   } n   if (flag == 2) n     return true; n   else n     return false; n } n int main() { n   cin >> s >> s1 >> s2; n   bool Forward = false; n   if (fn() == true) Forward = true; n   reverse(s.begin(), s.end()); n   bool Backward = false; n   if (fn() == true) Backward = true; n   if (Forward && Backward) n     cout <<  both  << endl; n   else if (Forward) n     cout <<  forward  << endl; n   else if (Backward) n     cout <<  backward  << endl; n   else n     cout <<  fantasy  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005, MOD = 1e9 + 7; n long long fact[N], inv[N]; n long long qpow(long long a, int b) { n   long long ans = 1; n   do { n     if (b & 1) ans = ans * a % MOD; n     a = a * a % MOD; n   } while (b /= 2); n   return ans; n } n int main() { n   int n; n   cin >> n; n   fact[0] = 1; n   for (int i = 1; i <= n * 2; i++) fact[i] = fact[i - 1] * i % MOD; n   inv[n * 2] = qpow(fact[n * 2], MOD - 2); n   for (int i = n * 2 - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % MOD; n   long long ans = 0; n   for (int i = 0; i < n; i++) n     ans += fact[n - 1 + i] * inv[n - 1] % MOD * inv[i] % MOD; n   cout << (ans * 2 - n) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n // #define int  long long int n #define mod 1000000007 n #define pb push_back n #define mp make_pair n #define bs binary_search n #define lb lower_bound n #define ub upper_bound n #define gcd __gcd n #define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); n #define min3(a,b,c) min(a,min(b,c)) n #define max3(a,b,c) max(a,max(b,c)) n #define all(i) i.begin(),i.end() n #define rep(i,a,b) for(int i=a;i<b;i++) n #define set_bit __builtin_popcount //gives number of set_bits n #define ff first n #define ss second n #define cases(t) int t;cin>>t; rep(casn,1,t+1) n #define el cout<<  n ; n #define deci(n) int n; scanf( %lld ,&n); n #define decs(n) string n; cin>>n; n #define sz(v) v.size() n #define repit(m) for(auto it=m.begin();it!=m.end();it++) n #define debx cout<< -> ; n #define deb(x) cout<<#x<<   <<x<<endl; n #define cYES cout<< YES n ; n #define cNO cout<< NO n ; n #define ins insert n #define imax 800000000000000000 n #define imin -1000000000000000000 n //  cerr <<   nTime :   << (double)clock() / (double)CLOCKS_PER_SEC <<  s n ; n //  IIITA n //SUNEET KUMAR -->  ਸੁਨੀਤ n // scanning numbers extremely fast///////// n  n void fs(int &number) n { n     bool negative = false; n     register int c; n     number = 0; n     c = getchar(); n     if (c== - ) n     { n         negative = true; n         c = getchar(); n     } n     for (; (c>47 && c<58); c=getchar()) n         number = number *10 + c - 48; n     if (negative) n         number *= -1; n } n // ======================================== n int power(int a,int x) n { n     a%=mod; n     int res=1; n     while(x>0) n     { n         if(x&1) n         res=res*a%mod; n         a=a*a%mod; n         x>>=1; n     } n     return res; n } n  n int max(int a,int b) n { n     if(a>b) n     { n         return a; n     } n     else n     { n         return b; n     } n } n  n int min(int a,int b) n { n     if(a>b) n     { n         return b; n     } n     else n     { n         return a; n     } n } n int madd(int a,int b) n { n     int ans; n     ans=(a%mod+b%mod)%mod; n     return ans; n } n int mmult(int a,int b) n { n     int ans=1; n     ans=((a%mod)*(b%mod))%mod; n     return ans%mod; n } n int modinverse(int a) n { n     return power(a,mod-2); n } n // int fact[500004]; n // void Factorial() n // { n //     fact[0]=1; n //     for(int i=1;i<=500001;i++) n //     { n //         fact[i]=mmult(fact[i-1],i); n //     } n // } n // int ncr(int n,int r) n // { n //     if(r>n) n //     { n //         return 0; n //     } n //     if(n==r||r==0) n //     { n //         return 1; n //     } n //     if(n==0) n //     { n //         return 0; n //     } n //     mmult(fact[n],modinverse(mmult(fact[r],fact[n-r]))); n // } n void solve() n { n     int n;cin>>n; n     int a[n+10][n+10]; n     string s; n     for(int i=1;i<=n;i++) n     {    n         string s;cin>>s; n         for(int j=0;j<s.length();j++) n         { n             a[i][j+1]=s[j]- 0 ; n         } n     } n     // map<int,int>m; n     int m[12],mxr[12],mxc[12],mir[12],mic[12]; n     memset(m,0,sizeof(m)); n     memset(mxr,0,sizeof(mxr));memset(mxc,0,sizeof(mxc)); n     // memset(mic,0,sizeof(mic)); n     // memset(mir,0,sizeof(mir)); n     for(int i=0;i<=10;i++) n     { n         mic[i]=100000; n         mir[i]=1000000; n     } n     for(int i=1;i<=n;i++) n     { n         for(int j=1;j<=n;j++) n         { n             int x=a[i][j]; n             mir[x]=min(mir[x],i); n             mic[x]=min(mic[x],j); n             mxr[x]=max(mxr[x],i); n             mxc[x]=max(mxc[x],j); n         } n     } n     for(int i=1;i<=n;i++) n     { n         for(int j=1;j<=n;j++) n         { n             int x=a[i][j]; n             m[x]=max(m[x],max(mxr[x]-i,i-mir[x])*(max(n-j,j-1))); n             m[x]=max(m[x],max(mxc[x]-j,j-mic[x])*(max(n-i,i-1))); n         } n     } n     // debx; n     for(int i=0;i<10;i++) n     { n         cout<<m[i]<<   ; n     } n     el n } n signed main() n { n     // fastio; n     cases(t) n     { n         // cout<< Case # <<i<< :  ; n         solve(); n         // cerr <<   nTime :   << (double)clock() / (double)CLOCKS_PER_SEC <<  s n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int n, d[100005], g[100005]; n bool vis[100005]; n char str[100005]; n void get() { n   bool sign = 1; n   int cur = 1; n   while (cur >= 1 && cur <= n) { n     if (vis[cur]) { n       sign = 0; n       break; n     } n     vis[cur] = 1; n     cur += g[cur] * d[cur]; n   } n   if (!sign) n     cout <<  INFINITE  << endl; n   else n     cout <<  FINITE  << endl; n } n int main() { n   cin >> n >> str + 1; n   for (int i = 1; i <= n; i++) cin >> d[i]; n   for (int i = 1; i <= n; i++) g[i] = str[i] ==  >  ? 1 : -1; n   get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<char> s; n int main() { n   cin >> n >> k; n   if (k > n) { n     cout << -1; n     cin >> n; n     return 0; n   } n   if (k == 1 && n > 1) { n     cout << -1; n     cin >> n; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       s.push_back( a ); n     } else { n       s.push_back( b ); n     } n   } n   int x = 98 + k - 2; n   for (int i = n - 1; i >= n - k + 2; i--) { n     s[i] = (char)x; n     x--; n   } n   for (int i = 0; i < n; i++) { n     cout << s[i]; n   } n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7fffffff; n inline int reint() { n   int d; n   scanf( %d , &d); n   return d; n } n inline long relong() { n   long l; n   scanf( %ld , &l); n   return l; n } n inline char rechar() { n   scanf(   ); n   return getchar(); n } n inline double redouble() { n   double d; n   scanf( %lf , &d); n   return d; n } n inline string restring() { n   string s; n   cin >> s; n   return s; n } n int mask(int a) { n   char str[10]; n   sprintf(str,  %d , a); n   char ans[10]; n   int j = 0; n   for (int i = (0); i < (strlen(str)); i++) { n     if (str[i] ==  4  || str[i] ==  7 ) { n       ans[j++] = str[i]; n     } n   } n   int res = 0; n   for (int i = (0); i < (j); i++) { n     res += ans[i] -  0 ; n     if (i != j - 1) { n       res *= 10; n     } n   } n   return res; n } n int c, b; n int main(void) { n   cin >> c >> b; n   c++; n   while (mask(c) != b) { n     c++; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define int long long n void solve() n { n     int n; n     cin >> n; n     int a[n]; n      n     for(int i = 0; i < n; i++) cin >> a[i]; n     cout<<(n/2)*6<<endl; n     for(int i = 1; i <= n; i+=2){ n         int j = i + 1; n         cout<<1<<   <<i<<   <<j<<endl; n         cout<<2<<   <<i<<   <<j<<endl; n         cout<<2<<   <<i<<   <<j<<endl; n         cout<<1<<   <<i<<   <<j<<endl; n         cout<<2<<   <<i<<   <<j<<endl; n         cout<<2<<   <<i<<   <<j<<endl; n     } n } n  n int32_t main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n     int t; n     cin>> t; n     while(t--)solve(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int N = 2.5e5; n vector<int> adj[N]; n int dep[N], par[N]; n bool leaf[N]; n bool dfs(int u, int p = -1, int d = 1) { n   if (dep[u] > 0) return false; n   dep[u] = d; n   par[u] = p; n   leaf[u] = true; n   for (int v : adj[u]) { n     if (dfs(v, u, d + 1)) leaf[u] = false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(0); n   int r = 0; n   for (int u = 0; u < n; u++) { n     if (dep[u] > dep[r]) r = u; n   } n   dfs(r); n   const int plen = ((n + k - 1) / k); n   int leaves = 0; n   for (int u = 0; u < n; u++) { n     if (dep[u] >= plen) { n       cout <<  PATH n  << dep[u] <<   n ; n       while (u != -1) { n         cout << (u + 1) <<    ; n         u = par[u]; n       } n       cout <<   n ; n       return 0; n     } n     if (leaf[u]) leaves++; n   } n   assert(leaves >= k); n   cout <<  CYCLES n ; n   int values[3]; n   vector<int> cy; n   cy.reserve(N); n   for (int u = 0; u < n && k > 0; u++) { n     if (!leaf[u]) continue; n     int top = -1; n     int id = 0; n     for (int v : adj[u]) { n       if (v == par[u]) continue; n       if ((dep[u] - dep[v]) >= 3 && (dep[u] - dep[v] + 1) % 3 != 0) { n         top = v; n         break; n       } n       values[id++] = v; n       if (id == 2) break; n     } n     if (top >= 0) { n       for (int v = u; v != top; v = par[v]) { n         cy.push_back(v); n       } n       cy.push_back(top); n     } else { n       cy.push_back(u); n       if (dep[values[0]] < dep[values[1]]) swap(values[0], values[1]); n       for (int v = values[0]; v != values[1]; v = par[v]) { n         cy.push_back(v); n       } n       cy.push_back(values[1]); n     } n     cout << cy.size() <<   n ; n     for (int u : cy) cout << (u + 1) <<    ; n     cout <<   n ; n     cy.clear(); n     k--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000010], a[1000010], b[1000010]; n char perm[300]; n char used[300]; n char savedPerm[300]; n char savedUsed[300]; n int k; n bool fit(char from, char to) { n   if (perm[from] != 0) { n     if (perm[from] != to) return false; n   } else { n     if (used[to] != 0) return false; n     used[to] = 1; n     perm[from] = to; n   } n   return true; n } n void fillUnused() { n   char i, j =  a ; n   for (i =  a ; i <  a  + k; i++) { n     if (perm[i] == 0) { n       while (used[j] == 1) j++; n       perm[i] = j; n       j++; n     } n   } n } n void solve() { n   scanf( %d%s%s%s , &k, s, a, b); n   memset(perm, 0, sizeof(perm)); n   memset(used, 0, sizeof(used)); n   int n = strlen(s); n   int state = 0; n   int conflict = -1; n   for (int i = 0; i < n; i++) { n     switch (state) { n       case 0: n         if (a[i] == b[i]) { n           if (!fit(s[i], a[i])) { n             printf( NO n ); n             return; n           } n         } else { n           for (char ch = a[i] + 1; ch < b[i]; ch++) { n             if (fit(s[i], ch)) { n               state = -1; n               break; n             } n           } n           if (state != -1) { n             memcpy(savedPerm, perm, sizeof(perm)); n             memcpy(savedUsed, used, sizeof(used)); n             if (conflict == -1) { n               if (fit(s[i], a[i])) { n                 conflict = i; n                 state = 1; n               } else if (fit(s[i], b[i])) n                 state = 2; n               else { n                 printf( NO n ); n                 return; n               } n             } else if (fit(s[i], b[i])) n               state = 2; n             else { n               printf( NO n ); n               return; n             } n           } n         } n         break; n       case 1: n         for (char ch = a[i] + 1; ch <  a  + k; ch++) { n           if (fit(s[i], ch)) { n             state = -1; n             break; n           } n         } n         if (state != -1) { n           if (!fit(s[i], a[i])) { n             state = 0; n             i = conflict - 1; n             memcpy(perm, savedPerm, sizeof(perm)); n             memcpy(used, savedUsed, sizeof(used)); n             break; n           } n         } n         break; n       case 2: n         for (char ch =  a ; ch < b[i]; ch++) { n           if (fit(s[i], ch)) { n             state = -1; n             break; n           } n         } n         if (state != -1) { n           if (!fit(s[i], b[i])) { n             printf( NO n ); n             return; n           } n         } n         break; n     } n     if (state == -1) break; n   } n   fillUnused(); n   printf( YES n ); n   for (int i =  a ; i <  a  + k; i++) printf( %c , perm[i]); n   printf(  n ); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int B = 20; n const int N = 1e5 + 7; n int n, m, a[N]; n struct ND { n   int l0, r0; n   ND() {} n   ND(int l0, int r0) : l0(l0), r0(r0) {} n   void init(int p, int v) { n     l0 = v == 0 ? p : n + 1; n     r0 = v == 0 ? p : 0; n   } n   void merge(const ND &a, const ND &b) { n     l0 = min(a.l0, b.l0); n     r0 = max(a.r0, b.r0); n   } n }; n struct Tree { n   static const int N = ::N << 2; n   ND nd[N]; n   void chg(int o, int l, int r, int qx, int qv) { n     if (l == r) { n       nd[o].init(qx, qv); n       return; n     } n     int mid = l + r >> 1; n     if (qx <= mid) n       chg(o << 1, l, mid, qx, qv); n     else n       chg(o << 1 | 1, mid + 1, r, qx, qv); n     nd[o].merge(nd[o << 1], nd[o << 1 | 1]); n   } n   ND qry(int o, int l, int r, int ql, int qr) { n     if (ql <= l && qr >= r) return nd[o]; n     int mid = l + r >> 1; n     ND qryl(n + 1, 0); n     if (ql <= mid) qryl = qry(o << 1, l, mid, ql, qr); n     ND qryr(n + 1, 0); n     if (qr > mid) qryr = qry(o << 1 | 1, mid + 1, r, ql, qr); n     ND ret; n     ret.merge(qryl, qryr); n     return ret; n   } n } tree[B]; n long long dc(int len) { return 1ll * (len + 1) * len / 2; } n long long getv(int p, int vv) { n   long long ret = 0; n   for (int b = (0); b < (B); ++b) { n     int v = vv >> b & 1; n     int lenl = p - 1 - (p > 1 ? tree[b].qry(1, 1, n, 1, p - 1).r0 : 0); n     int lenr = (p < n ? tree[b].qry(1, 1, n, p + 1, n).l0 : n + 1) - p - 1; n     long long tmp = v == 0 ? dc(lenl) + dc(lenr) : dc(lenl + lenr + 1); n     ret += tmp * (1 << b); n   } n   return ret; n } n void solve() { n   for (int i = (1); i < (n + 1); ++i) scanf( %d , &a[i]); n   for (int i = (1); i < (n + 1); ++i) n     for (int b = (0); b < (B); ++b) tree[b].chg(1, 1, n, i, a[i] >> b & 1); n   long long ans = 0; n   for (int b = (0); b < (B); ++b) { n     int tmp = 0; n     for (int i = (1); i < (n + 2); ++i) n       if (a[i] >> b & 1) n         tmp++; n       else { n         ans += dc(tmp) * (1 << b); n         tmp = 0; n       } n   } n   while (m--) { n     int p, v; n     scanf( %d%d , &p, &v); n     ans += -getv(p, a[p]) + getv(p, v); n     a[p] = v; n     for (int b = (0); b < (B); ++b) tree[b].chg(1, 1, n, p, a[p] >> b & 1); n     printf( %lld n , ans); n   } n } n int main() { n   while (~scanf( %d%d , &n, &m)) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200010]; n int main() { n   int n; n   long long sum = 0, MAX = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , a + i); n     sum += a[i]; n     MAX = max(a[i], MAX); n   } n   long long ans; n   if (sum % (n - 1) == 0) n     ans = sum / (n - 1); n   else n     ans = sum / (n - 1) + 1; n   ans = max(MAX, ans); n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d, b; n int nl[100100], nr[100100]; n int find(int *next, int x) { n   if (next[x] == x) return x; n   return next[x] = find(next, next[x]); n } n long long bit[100100]; n inline int lowbit(int x) { return x & -x; } n void add(int x, long long y) { n   while (x <= n) { n     bit[x] += y; n     x += lowbit(x); n   } n } n long long sum(int x) { n   long long res = 0; n   while (x > 0) { n     res += bit[x]; n     x -= lowbit(x); n   } n   return res; n } n long long get(int x) { return sum(x) - sum(x - 1); } n int make(int x, int t) { n   int nx = 0; n   int l = max(1ll, x - t * d), r = min(n, x + t * d); n   if (sum(r) - sum(l - 1) < b) return 1; n   if (x == t) { n     for (int i = l; i <= r; i = find(nl, i)) { n       int gi = get(i); n       nx += gi; n       if (nx < b) { n         add(i, -get(i)); n         nl[i] = i + 1; n         nr[i] = i - 1; n       } else { n         add(i, nx - gi - b); n         break; n       } n     } n   } else { n     for (int i = r; i >= l; i = find(nr, i)) { n       int gi = get(i); n       nx += gi; n       if (nx < b) { n         add(i, -get(i)); n         nl[i] = i + 1; n         nr[i] = i - 1; n       } else { n         add(i, nx - gi - b); n         break; n       } n     } n   } n   return 0; n } n int main() { n   scanf( %d%d%d , &n, &d, &b); n   memset(nl, 0, sizeof(nl)); n   memset(nr, 0, sizeof(nr)); n   for (int i = 1; i <= n; i++) { n     int a; n     scanf( %d , &a); n     add(i, a); n     if (a != 0) { n       nl[i] = i; n       nr[i] = i; n     } else { n       nl[i] = i + 1; n       nr[i] = i - 1; n     } n   } n   int x1 = 0, x2 = 0, l = 1, r = n; n   while (l <= r) { n     if (l == r) n       x1 += make(l, l); n     else if (x1 > x2) { n       x1 += make(l, l); n       x2 += make(r, l); n     } else { n       x2 += make(r, l); n       x1 += make(l, l); n     } n     l++; n     r--; n   } n   printf( %d n , max(x1, x2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n; ++i) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cmp(char a, char b) { return a > b; } n int main() { n   char s[300050]; n   char str[300050]; n   char ans[300050]; n   scanf( %s%s , s, str); n   int len = strlen(s); n   sort(s, s + len); n   sort(str, str + len, cmp); n   if (len == 1) { n     printf( %s n , s); n     return 0; n   } n   int l, r, L, R; n   l = L = 0; n   r = (len + 1) / 2 - 1; n   if (len & 1) { n     R = len / 2 - 1; n   } else n     R = r; n   int sum = 0; n   int i = 0; n   int j = len - 1; n   while (i <= j) { n     if ((sum & 1) == 0) { n       if (s[l] < str[L]) { n         ans[i++] = s[l++]; n       } else { n         ans[j--] = s[r--]; n       } n       sum++; n     } else { n       if (s[l] >= str[L]) { n         ans[j--] = str[R--]; n       } else { n         ans[i++] = str[L++]; n       } n       sum++; n     } n   } n   ans[len] =   0 ; n   printf( %s n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[50000]; n int p[50000]; n pair<pair<double, int>, int> prod[50000]; n int main() { n   std::ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> p[i]; n     if (p[i] == 100) n       prod[i].first.first = -1000000000; n     else n       prod[i].first.first = -l[i] * p[i] * 1.0 / (100 - p[i]); n     prod[i].first.second = p[i]; n     prod[i].second = i; n   } n   sort(prod, prod + n); n   double F = l[prod[0].second] * p[prod[0].second] / 100.0; n   double E = l[prod[0].second]; n   for (int i = 1; i < n; i++) { n     E = E + l[prod[i].second] + (1 - p[prod[i].second] / 100.0) * F; n     F += p[prod[i].second] / 100.0 * l[prod[i].second]; n   } n   cout << setprecision(25) << E << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sip; n char A[100005], B[100005]; n int C[35], D[35], tam1, tam2, res = 0, puede = 0, quedan; n int main() { n   scanf( %s%s , A, B); n   for (tam1 = 0; A[tam1] !=   0 ; tam1++) n     ; n   for (tam2 = 0; B[tam2] !=   0 ; tam2++) n     ; n   if (tam2 > tam1) { n     printf( 0 n ); n     return 0; n   } n   for (int i = 0; i < tam2; i++) { n     ++D[B[i] -  a ]; n   } n   for (int i = 0; i < tam2; i++) { n     if (A[i] !=  ? ) { n       ++C[A[i] -  a ]; n     } else { n       ++puede; n     } n   } n   for (int i = 0; i <= tam1 - tam2; i++) { n     quedan = puede; n     sip = true; n     for (int e = 0; e < 26; e++) { n       if (C[e] > D[e] or D[e] - C[e] > quedan) { n         sip = false; n         break; n       } else { n         quedan -= (D[e] - C[e]); n       } n     } n     if (sip) { n       ++res; n     } n     if (i == tam1 - tam2) { n       continue; n     } n     if (A[i] ==  ? ) { n       --puede; n     } else { n       --C[A[i] -  a ]; n     } n     if (A[i + tam2] ==  ? ) { n       ++puede; n     } else { n       ++C[A[i + tam2] -  a ]; n     } n   } n   printf( %d n , res); 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   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if (b < a) swap(a, b); n     if (b - a > 10) { n       cout << fixed << (int)ceil((double)(b - a) / 10) <<   n ; n     } else if (a != b) n       cout <<  1 n ; n     else n       cout <<  0 n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void sprint(string s) { n   for (int i = 0; i < (int)s.size(); ++i) printf( %c , s[i]); n   printf(  n ); n } n long long f(long long n, long long r) { n   long long ans = 1; n   for (long long i = 0; i < r; ++i) { n     ans = (ans * (n - i)) / (i + 1); n   } n   return ans; n } n int main() { n   long long n; n   cin >> n; n   long long ans = f(n + 4, 5) * f(n + 2, 3); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 2; n const int INF = 1e9 + 1; n const double EPS = 1e-9; n int n, m; n int a[MAXN]; n int r[MAXN], t[MAXN]; n int b[MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   int it = 0; n   for (int i = 0; i < m; ++i) { n     int ct, crt; n     cin >> ct >> crt; n     while (it > 0 && crt >= r[it - 1]) --it; n     t[it] = ct; n     r[it++] = crt; n   } n   r[it] = 0; n   int bl = 0, br = r[0]; n   for (int i = 0; i < br; ++i) { n     b[i] = a[i]; n   } n   sort(b, b + br); n   for (int i = 1; i < it + 1; ++i) { n     for (int j = r[i - 1]; j > r[i]; --j) { n       if (t[i - 1] == 1) { n         a[j - 1] = b[--br]; n       } else n         a[j - 1] = b[bl++]; n     } n   } n   for (int i = 0; i < n; ++i) { n     cout << a[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int a[1005]; n int main() { n   int x, i, j; n   while (scanf( %d%*c , &x) != EOF) a[x] = 1; n   bool flag = false; n   for (i = j = 1; i < 1001; i = ++j) n     if (a[i]) { n       for (j = i; a[j]; ++j) n         ; n       --j; n       if (!flag) n         flag = true; n       else n         printf( , ); n       if (i == j) n         printf( %d , i); n       else n         printf( %d-%d , i, j); n     } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long logN = 20; n const long long M = 1000000007; n const long long INF = 1e12; n const long long N = 505; n const long long K = 250505; n long long dp[K]; n void solve() { n   long long n, k; n   cin >> n >> k; n   long long A[n], B[n]; n   for (long long i = 0; i < n; i++) cin >> A[i] >> B[i]; n   long long r = 0, g = 0; n   for (long long i = 0; i < n; i++) r += A[i], g += B[i]; n   dp[0] = 1; n   for (long long i = 0; i < n; i++) { n     long long r0 = A[i]; n     long long g0 = B[i]; n     if ((r0 + g0) < k) continue; n     long long downr = max(k - g0, 0LL); n     long long upr = min(r0, k); n     long long mx = K; n     for (long long i = K - 1; i >= 0; i--) { n       if (dp[i] != 0) { n         for (long long j = i + downr + 1; j < i + upr + 1; j++) { n           if (j >= mx) break; n           dp[j] = dp[i] + 1; n         } n         dp[i + downr] = dp[i] + 1; n         mx = i + downr; n       } n     } n   } n   long long ans = 0; n   for (long long i = 0; i < K; i++) { n     if (dp[i] != 0) { n       long long cnt = dp[i] - 1; n       long long r0 = i; n       long long g0 = cnt * k - r0; n       cnt += (r - r0) / k + (g - g0) / k; n       ans = max(ans, cnt); n     } n   } n   cout << ans <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long no; n   no = 1; n   while (no--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, vector<long long> > mp; n long long a[200005]; n long long t[200005]; n set<long long> s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i], s.insert(a[i]); n   for (long long i = 0; i < n; i++) { n     cin >> t[i]; n     mp[a[i]].push_back(t[i]); n   } n   for (auto i : s) sort(mp[i].begin(), mp[i].end()); n   long long ans = 0; n   multiset<long long> temp; n   long long sum = 0; n   while (s.size()) { n     long long v = *s.begin(); n     s.erase(s.begin()); n     for (auto i : mp[v]) temp.insert(i), sum += i; n     auto p = s.lower_bound(v); n     long long nex = 1e18; n     if (p != s.end()) nex = *p; n     long long d = nex - v; n     long long siz = min(d, (long long)temp.size()); n     long long c = 0; n     while (siz--) { n       auto po = prev(temp.end()); n       ans += (*po) * c; n       c++; n       sum -= (*po); n       temp.erase(po); n     } n     ans += sum * c; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e6 + 7; n int N, a[maxN], f[(1LL << 22) + 7]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N; n   memset(f, -1, sizeof f); n   for (int i = 1; i <= N; i++) { n     cin >> a[i]; n     f[a[i]] = a[i]; n   } n   for (int i = 0; i <= 22; i++) n     for (int mask = 0; mask < (1LL << 22); mask++) n       if (mask & (1LL << i)) f[mask] = max(f[mask], f[mask ^ (1LL << i)]); n   for (int i = 1; i <= N; i++) { n     cout << f[(1LL << 22) - 1 - a[i]] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long rd() { n   char c; n   while (c = getchar(), c <  0  || c >  9 ) n     ; n   long long X = c - 48; n   while (c = getchar(), c >=  0  && c <=  9 ) X = X * 10 + c - 48; n   return X; n } n template <class T> n inline void print(T x) { n   if (x < 0) { n     putchar( - ); n     return print(-x); n   } n   if (x < 10) { n     putchar( 0  + x); n     return; n   } n   print(x / 10); n   putchar(x % 10 +  0 ); n } n string s; n void solve(int n, vector<int> &a) { n   if (n == 1) { n     s =  B ; n     return; n   } n   vector<int> idx(n + 1); n   s = string(n,  B ); n   for (__typeof(0) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - (((0) > (n)) << 1)) n     idx[a[i]] = i; n   for (__typeof(n + 1) i = (n + 1) - ((n + 1) > (1)); n        i != (1) - ((n + 1) > (1)); i += 1 - (((n + 1) > (1)) << 1)) { n     for (int j = idx[i] - i; j >= 0; j -= i) { n       if (a[j] > i and s[j] ==  B ) { n         s[idx[i]] =  A ; n         break; n       } n     } n     for (int j = idx[i] + i; j < n; j += i) { n       if (a[j] > i and s[j] ==  B ) { n         s[idx[i]] =  A ; n         break; n       } n     } n   } n } n int32_t main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int &i : a) cin >> i; n   solve(n, a); n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const long long INF = (long long)1e14 + 5; n long long N, cur = 1, last = 0, ans[MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   for (int i = 1; i <= N; i += 2) { n     long long x; n     cin >> x; n     while (cur * cur <= INF) { n       long long l = last + 1, r = cur - 1, found = -1; n       while (l <= r) { n         long long m = (l + r) >> 1; n         if (cur * cur - m * m == x) { n           found = m; n           break; n         } else if (cur * cur - m * m > x) n           l = m + 1; n         else n           r = m - 1; n       } n       if (found != -1) { n         ans[i] = found * found - last * last; n         ans[i + 1] = x; n         last = cur; n         cur++; n         break; n       } n       cur++; n     } n     if (cur * cur > INF) { n       cout <<  No n ; n       return 0; n     } n   } n   cout <<  Yes n ; n   for (int i = 1; i <= N; i++) { n     cout << ans[i] << (i + 1 <= N ?     :   n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n typedef int nod[100010]; n typedef int edg[200010]; n int n, m; n int e = 1; n nod h; n edg link, v, u; n nod vis; n nod fz; n int ju; n void creat(int x, int y) { n   link[++e] = h[x]; n   h[x] = e; n   v[e] = y; n   link[++e] = h[y]; n   h[y] = e; n   v[e] = x; n } n int dfs(int x) { n   if (vis[x]) n     return 0; n   else n     vis[x] = 1; n   for (int i = h[x]; i; i = link[i]) n     if (!u[i]) { n       u[i] = u[i ^ 1] = 1; n       if ((ju = dfs(v[i]))) n         printf( %d %d %d n , x, v[i], ju); n       else if (!fz[x]) n         fz[x] = v[i]; n       else n         printf( %d %d %d n , fz[x], x, v[i]), fz[x] = 0; n     } n   return fz[x]; n } n int main() { n   scanf( %d%d , &n, &m); n   if (m & 1) { n     printf( No solution n ); n     return 0; n   } n   for (int i = 1, x, y; i <= m; ++i) scanf( %d%d , &x, &y), creat(x, y); n   dfs(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 5; n void NO() { n   puts( unfair ); n   exit(0); n } n int main() { n   int n, b, q; n   scanf( %d%d%d , &n, &b, &q); n   vector<pair<int, int>> w; n   w.push_back(make_pair(b, n)); n   while (q--) { n     int a, b; n     scanf( %d%d , &a, &b); n     w.push_back(make_pair(a, b)); n   } n   sort(w.begin(), w.end()); n   for (int mask = 0; mask < (1 << K); ++mask) { n     int at_least = 0, at_most = 0; n     int prev_upto = 0, prev_quan = 0; n     for (pair<int, int> query : w) { n       int now_upto = query.first, now_quan = query.second; n       int places_matching = 0; n       int places_other = 0; n       for (int i = prev_upto + 1; i <= now_upto; ++i) { n         if (mask & (1 << (i % K))) n           ++places_matching; n         else n           ++places_other; n       } n       if (now_quan < prev_quan) NO(); n       int quan = now_quan - prev_quan; n       int places_total = now_upto - prev_upto; n       assert(places_total == places_matching + places_other); n       if (quan > places_total) NO(); n       at_least += max(0, quan - places_other); n       at_most += min(quan, places_matching); n       prev_upto = now_upto; n       prev_quan = now_quan; n     } n     int must_be = n / K * __builtin_popcount(mask); n     if (!(at_least <= must_be && must_be <= at_most)) NO(); n   } n   puts( fair ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char W[1000 + 10]; n int N[1000 + 10]; n int main() { n   int n, m; n   scanf( %d%d%s , &n, &m, W); n   for (int i = 0; i < n; ++i) (N[i] = W[i] -  a ); n   bool fixed = false; n   for (int i = n - 1; i >= 0; --i) { n     while (true) { n       N[i]++; n       if (N[i] == m) break; n       if (i - 1 >= 0 && N[i] == N[i - 1]) continue; n       if (i - 2 >= 0 && N[i] == N[i - 2]) continue; n       fixed = true; n       for (int j = i + 1; j < n; ++j) { n         N[j] = -1; n         while (true) { n           N[j]++; n           if (j - 1 >= 0 && N[j] == N[j - 1]) continue; n           if (j - 2 >= 0 && N[j] == N[j - 2]) continue; n           break; n         } n       } n       break; n     } n     if (fixed) break; n   } n   if (!fixed) n     printf( NO n ); n   else { n     for (int i = 0; i < n; ++i) printf( %c ,  a  + N[i]); n     printf(  n ); n   } n   return 0; n } n