#include <bits/stdc++.h> n using namespace std; n struct segtree { n   using T = long long; n   int n; n   vector<T> node; n   T e() { return (long long)1e18; } n   T op(T x, T y) { return min(x, y); } n   segtree() : segtree(0) {} n   segtree(int _n) { n     if (_n <= 1) { n       n = _n; n     } else { n       n = 1 << (32 - __builtin_clz(_n - 1)); n     } n     node.resize(2 * n, e()); n   } n   segtree(vector<T> v) { n     if ((int)v.size() <= 1) { n       n = (int)v.size(); n     } else { n       n = 1 << (32 - __builtin_clz((int)v.size() - 1)); n     } n     node.resize(2 * n, e()); n     for (int i = 0; i < (int)v.size(); i++) { n       node[i + n] = v[i]; n     } n     for (int i = n - 1; i > 0; i--) { n       node[i] = op(node[2 * i], node[2 * i + 1]); n     } n   } n   void update(int k, T v) { n     k += n; n     node[k] = v; n     while (k > 1) { n       k /= 2; n       node[k] = op(node[2 * k], node[2 * k + 1]); n     } n   } n   T get(int x, int y, int k, int l, int r) { n     if (r <= x || y <= l) { n       return e(); n     } n     if (x <= l && r <= y) { n       return node[k]; n     } n     T vl = get(x, y, 2 * k, l, (l + r) / 2); n     T vr = get(x, y, 2 * k + 1, (l + r) / 2, r); n     return op(vl, vr); n   } n   T get(int x, int y) { return get(x, y, 1, 0, n); } n   T get(int x) { return node[x + n]; } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     int k; n     cin >> k; n     vector<int> a, b; n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       if (x > 0) { n         a.emplace_back(x); n       } else if (x < 0) { n         b.emplace_back(-x); n       } n     } n     sort(a.begin(), a.end()); n     sort(b.begin(), b.end()); n     long long ans = 0; n     for (int loop = 0; loop < 2; loop++) { n       segtree seg((int)a.size() + 1); n       seg.update(0, 0); n       for (int i = 0; i < (int)a.size(); i++) { n         long long t = a[i] * 2 + seg.get(max(0, i - k + 1), i + 1); n         seg.update(i + 1, t); n       } n       ans += seg.get((int)a.size()); n       swap(a, b); n     } n     if (a.empty()) { n       a.emplace_back(0); n     } n     if (b.empty()) { n       b.emplace_back(0); n     } n     ans -= max(a.back(), b.back()); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[101][51][201][2] = {0}; n bool used[101][51][201][2] = {false}; n int n; n int sz; n string commands; n int rec(int cur, int left, int pos, int dir) { n   if (cur == sz) { n     d[cur][left][100 + pos][(dir + 1) % 2] = abs(pos); n     used[cur][left][100 + pos][(dir + 1) % 2] = true; n     return pos; n   } n   if (used[cur][left][100 + pos][(dir + 1) % 2]) { n     return d[cur][left][100 + pos][(dir + 1) % 2]; n   } n   used[cur][left][100 + pos][(dir + 1) % 2] = true; n   int res = 0; n   for (int i = 0; i <= left; ++i) { n     if (cur == sz - 1 && left != 0) { n       i = left; n     } n     int next_pos = pos; n     int next_dir = dir; n     if (i % 2 == 0) { n       if (commands[cur] ==  T ) { n         next_dir = -dir; n       } else { n         next_pos = pos + dir; n       } n     } else { n       if (commands[cur] ==  T ) { n         next_pos = pos + dir; n       } else { n         next_dir = -dir; n       } n     } n     res = max(res, abs(rec(cur + 1, left - i, next_pos, next_dir))); n   } n   d[cur][left][100 + pos][(dir + 1) % 2] = res; n   return res; n } n int main() { n   int res; n   cin >> commands; n   cin >> n; n   sz = commands.size(); n   res = rec(0, n, 0, 1); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LLINF = 1e18; n const int INF = 1e9; n const int MOD = 20101009; n const int HMOD = 999959; n const int VMOD = 5000000; n const int MAXN = 2e3 + 10; n const int MAXM = 5e5 + 10; n const int INV = 10050505; n const long long LLINV = 1e18; n const double eps = 1e-3; n int dirx[4] = {-1, 1, 0, 0}, diry[4] = {0, 0, -1, 1}; n int n, m; n int r, c; n int limx, limy; n char G[MAXN][MAXN]; n int dl[MAXN][MAXN]; n int dr[MAXN][MAXN]; n void bfs() { n   memset(dl, 0x3f3f3f3f, sizeof(dl)); n   memset(dr, 0x3f3f3f3f, sizeof(dr)); n   deque<pair<int, int> > que; n   que.push_front(make_pair(r, c)); n   dl[r][c] = dr[r][c] = 0; n   while (!que.empty()) { n     int x = que.front().first; n     int y = que.front().second; n     que.pop_front(); n     for (int i = 0; i < 4; ++i) { n       int tx = x + dirx[i]; n       int ty = y + diry[i]; n       if (tx < 1 || tx > n || ty < 1 || ty > m) continue; n       if (G[tx][ty] ==  * ) continue; n       int cl = dl[x][y] + (diry[i] == -1); n       int cr = dr[x][y] + (diry[i] == 1); n       if (cl < dl[tx][ty] || cr < dr[tx][ty]) { n         dl[tx][ty] = cl; n         dr[tx][ty] = cr; n         if (dirx[i] == 0) n           que.push_back(make_pair(tx, ty)); n         else n           que.push_front(make_pair(tx, ty)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   scanf( %d%d , &n, &m); n   scanf( %d%d , &r, &c); n   scanf( %d%d , &limx, &limy); n   for (int i = 1; i <= n; ++i) scanf( %s , G[i] + 1); n   bfs(); n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (G[i][j] ==  * ) continue; n       if (dl[i][j] <= limx && dr[i][j] <= limy) ++ans; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105000; n const int mod = 1e9 + 7; n vector<int> xloc[maxn], po[maxn]; n vector<int> side[maxn], blk; n long long ans = 1; n long long c[maxn][2]; n int vis[maxn], vis_cnt; n int d[maxn]; n long long g[maxn][2][2]; n long long f[maxn][2]; n void dfs(int u, int id) { n   if (vis[u]) return; n   vis[u] = id; n   blk.push_back(u); n   for (int i = (0), iend = ((int)side[u].size() - 1); i <= iend; i++) { n     int v = side[u][i]; n     if (vis[v]) continue; n     dfs(v, id); n   } n } n void dp(int id, int l1, int r1, int l2, int r2) { n   for (int i = (0), iend = (blk.size() + 10); i <= iend; i++) n     for (int j = (0), jend = (1); j <= jend; j++) n       for (int k = (0), kend = (1); k <= kend; k++) g[i][j][k] = 0; n   for (int i = (l1), iend = (r1); i <= iend; i++) g[0][0][i] = 1; n   if ((!po[blk[0]][1]) || (abs(po[blk[0]][0]) == abs(po[blk[1]][0]) || n                            abs(po[blk[0]][0]) == abs(po[blk[1]][1]))) n     swap(po[blk[0]][0], po[blk[0]][1]); n   for (int i = (1), iend = ((int)blk.size() - 1); i <= iend; i++) n     if (abs(po[blk[i]][1]) == abs(po[blk[i - 1]][1])) n       swap(po[blk[i]][0], po[blk[i]][1]); n   for (int i = (1), iend = (blk.size()); i <= iend; i++) { n     int x = blk[i - 1]; n     for (int j = (0), jend = (1); j <= jend; j++) { n       for (int p = (0), pend = (1); p <= pend; p++) { n         for (int q = (0), qend = (1); q <= qend; q++) { n           g[i][((((po[x][0]) < 0) ^ p) | (((po[x][1]) < 0) ^ q)) ^ j][q] += n               g[i - 1][j][p]; n           g[i][((((po[x][0]) < 0) ^ p) | (((po[x][1]) < 0) ^ q)) ^ j][q] %= mod; n         } n       } n     } n   } n   for (int j = (0), jend = (1); j <= jend; j++) { n     for (int k = (l2), kend = (r2); k <= kend; k++) { n       c[id][j] += g[blk.size()][j][k]; n       c[id][j] %= mod; n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = (1), iend = (n); i <= iend; i++) { n     int k, loc; n     scanf( %d , &k); n     while (k--) { n       scanf( %d , &loc); n       po[i].push_back(loc); n       xloc[abs(loc)].push_back(i); n     } n   } n   for (int i = (1), iend = (m); i <= iend; i++) { n     if (xloc[i].size() == 2) { n       side[xloc[i][0]].push_back(xloc[i][1]); n       side[xloc[i][1]].push_back(xloc[i][0]); n       d[xloc[i][0]]++; n       d[xloc[i][1]]++; n     } else if (!xloc[i].size()) n       ans *= 2, ans %= mod; n   } n   for (int i = (1), iend = (n); i <= iend; i++) { n     if (vis[i]) continue; n     blk.clear(); n     vis_cnt++; n     dfs(i, vis_cnt); n     if (blk.size() == 1) { n       if (po[blk[0]].size() == 1) n         c[vis_cnt][0] = 1, c[vis_cnt][1] = 1; n       else if (abs(po[blk[0]][0]) != abs(po[blk[0]][1])) n         c[vis_cnt][0] = 1, c[vis_cnt][1] = 3; n       else if (po[blk[0]][0] == po[blk[0]][1]) n         c[vis_cnt][0] = 1, c[vis_cnt][1] = 1; n       else n         c[vis_cnt][0] = 0, c[vis_cnt][1] = 2; n       continue; n     } n     int rt = 0; n     for (int j = (0), jend = ((int)blk.size() - 1); j <= jend; j++) { n       if (d[blk[j]] == 1) { n         rt = blk[j]; n         break; n       } n     } n     if (rt) { n       for (int j = (0), jend = ((int)blk.size() - 1); j <= jend; j++) n         vis[blk[j]] = 0; n       blk.clear(); n       dfs(rt, vis_cnt); n       int x = blk[0]; n       int l1 = 0, r1 = 1, l2 = 0, r2 = 1; n       if (po[x].size() == 1) po[x].push_back(0), r1 = 0; n       x = blk.back(); n       if (po[x].size() == 1) po[x].push_back(0), r2 = 0; n       dp(vis_cnt, l1, r1, l2, r2); n       continue; n     } n     dp(vis_cnt, 0, 0, 0, 0); n     dp(vis_cnt, 1, 1, 1, 1); n   } n   f[0][0] = 1; n   for (int i = (0), iend = (vis_cnt - 1); i <= iend; i++) { n     for (int l = (0), lend = (1); l <= lend; l++) { n       for (int x = (0), xend = (1); x <= xend; x++) { n         f[i + 1][l ^ x] += f[i][l] * c[i + 1][x] % mod; n         f[i + 1][l ^ x] %= mod; n       } n     } n   } n   printf( %lld , f[vis_cnt][1] * ans % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   getline(cin, s); n   int x = s.length(); n   x = (x + 1) * 26 - x; n   printf( %d , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> adj[200005]; n vector<int> visited(200005, 0); n vector<long long int> ans; n void topo(long long int s) { n   visited[s] = 1; n   long long int sz = adj[s].size(); n   for (long long int i = 0; i < sz; i++) { n     if (visited[adj[s][i]] == 0) topo(adj[s][i]); n   } n   ans.push_back(s); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int w; n   cin >> w; n   while (w--) { n     long long int t, x, y, n, m; n     cin >> n >> m; n     vector<pair<long long int, long long int> > anso; n     vector<pair<long long int, long long int> > edg; n     for (long long int i = 1; i <= n; i++) { n       visited[i] = 0; n       adj[i].clear(); n     } n     ans.clear(); n     for (long long int i = 0; i <= m - 1; i++) { n       cin >> t >> x >> y; n       if (t == 1) { n         adj[x].push_back(y); n         anso.push_back({x, y}); n       } else { n         edg.push_back({x, y}); n       } n     } n     long long int fou = 0; n     for (long long int i = 1; i <= n; i++) { n       if (!visited[i]) topo(i); n     } n     reverse((ans).begin(), (ans).end()); n     vector<long long int> pre(n + 1, 0); n     for (long long int i = 0; i < n; i++) { n       pre[ans[i]] = i + 1; n     } n     for (long long int i = 0; i < anso.size(); i++) { n       if (pre[anso[i].first] > pre[anso[i].second]) { n         fou = 1; n         break; n       } n     } n     if (fou) { n       cout <<  NO  <<   n ; n       continue; n     } else { n       cout <<  YES  <<   n ; n     } n     long long int sz = edg.size(); n     for (long long int i = 0; i < sz; i++) { n       if (pre[edg[i].first] < pre[edg[i].second]) { n         anso.push_back({edg[i].first, edg[i].second}); n       } else { n         anso.push_back({edg[i].second, edg[i].first}); n       } n     } n     for (auto i : anso) { n       cout << i.first <<     << i.second <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n std ::map<int, int>::iterator x, l, s; n int main() { n   int n, p; n   cin >> n; n   mp[0] = 0; n   mp[1 << 30] = 0; n   for (int i = 0; i < n; i++) { n     cin >> p; n     x = mp.lower_bound(p); n     l = x--; n     s = x->second > l->second ? x : l; n     if (i) cout << s->first <<    ; n     mp[p] = s->second + 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, N; n   cin >> m >> n; n   N = floor((m * n) / 2); n   cout << N; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   for (int a = 0; a < t; a++) { n     int n; n     scanf( %d , &n); n     string attack, prepend; n     cin >> attack; n     while (!attack.empty() and attack.back() == attack.front()) { n       prepend += attack.back(); n       attack.pop_back(); n     } n     if (attack.empty()) { n       printf( %d n , (n + 2) / 3); n       continue; n     } n     attack = prepend + attack; n     int answer = 0, streak = 1; n     for (int b = 1; b < n; b++) { n       if (attack[b] == attack[b - 1]) { n         streak++; n       } else { n         streak = 1; n       } n       if (streak % 3 == 0) { n         answer++; n       } n     } n     printf( %d n , answer); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int& x) { n   char c = getchar(); n   x = 0; n   while (c <  0  || c >  9 ) c = getchar(); n   while (c <=  9  && c >=  0 ) x = x * 10 + c - 48, c = getchar(); n   return; n } n inline void write(int x) { n   if (!x) { n     putchar( 0 ); n     putchar(  n ); n     return; n   } n   int num = 0; n   char c[15]; n   while (x) c[++num] = (x % 10) + 48, x /= 10; n   while (num) putchar(c[num--]); n   putchar(  n ); n   return; n } n inline int min(int a, int b) { n   if (a < b) return a; n   return b; n } n const int maxn = int(3e5) + 10; n const int K = 31; n const int INF = int(1e9) + 10; n struct tri { n   int a, b, c; n } q[maxn]; n int top = 0; n inline bool cmp(const tri& A, const tri& B) { return A.b < B.b; } n int n, qnum; n int b[maxn]; n int ans[maxn]; n struct Node { n   int* a; n   int ans; n } node[maxn * 4]; n int* getnew(int siz) { return new int[siz]; } n void build(int k, int l, int r) { n   node[k].a = getnew(r - l + 1); n   if (l == r) { n     node[k].a[0] = b[l]; n     node[k].ans = INF; n     return; n   } n   int mid = (l + r) >> 1, siz = r - l + 1; n   build((k << 1), l, mid); n   build(((k << 1) ^ 1), mid + 1, r); n   std::merge(node[(k << 1)].a, node[(k << 1)].a + mid - l + 1, n              node[((k << 1) ^ 1)].a, node[((k << 1) ^ 1)].a + r - mid, n              node[k].a); n   int res = INF; n   for (int i = 1; i < siz; i++) res = min(res, node[k].a[i] - node[k].a[i - 1]); n   node[k].ans = res; n   return; n } n int query(int, int, int, int, int); n void update(int k, int l, int r, int to, int x, int& d) { n   if (l == r) { n     node[k].ans = min(node[k].ans, abs(x - node[k].a[0])); n     d = min(d, node[k].ans); n     return; n   } n   int mid = (l + r) >> 1, siz = r - l + 1; n   int* it = std::lower_bound(node[k].a, node[k].a + siz, x); n   if ((it == node[k].a + siz || *it >= x + d) && n       (it == node[k].a || *(it - 1) <= x - d)) { n     d = min(d, query(k, l, r, l, to)); n     return; n   } n   if (to > mid) { n     update(((k << 1) ^ 1), mid + 1, r, to, x, d), n         update((k << 1), l, mid, mid, x, d); n   } else n     update((k << 1), l, mid, to, x, d); n   node[k].ans = n       min(node[k].ans, min(node[(k << 1)].ans, node[((k << 1) ^ 1)].ans)); n   return; n } n int query(int k, int l, int r, int x, int y) { n   if (l == x && r == y) return node[k].ans; n   int mid = (l + r) >> 1; n   if (y <= mid) n     return query((k << 1), l, mid, x, y); n   else if (x > mid) n     return query(((k << 1) ^ 1), mid + 1, r, x, y); n   else n     return min(query((k << 1), l, mid, x, mid), n                query(((k << 1) ^ 1), mid + 1, r, mid + 1, y)); n } n int main() { n   read(n); n   for (int i = 0; i < n; i++) read(b[i]); n   read(qnum); n   for (int i = 0; i < qnum; i++) { n     int l, r; n     read(l), read(r); n     l--, r--; n     const int K = 31; n     if (r - l <= K) { n       int cur[K + 1], siz = r - l + 1; n       for (int j = l; j <= r; j++) cur[j - l] = b[j]; n       sort(cur, cur + siz); n       int res = INF; n       for (int j = 1; j < siz; j++) res = min(res, cur[j] - cur[j - 1]); n       ans[i] = res; n     } else n       q[top++] = {l, r, i}; n   } n   sort(q, q + top, cmp); n   int curr = 0; n   build(1, 0, n - 1); n   for (int k = 0; k < top; k++) { n     int l = q[k].a, r = q[k].b, index = q[k].c; n     while (curr < r) { n       ++curr; n       int d = INF; n       update(1, 0, n - 1, curr - 1, b[curr], d); n     } n     ans[index] = query(1, 0, n - 1, l, r); n   } n   for (int i = 0; i < qnum; i++) write(ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 3e2 + 2, N = 1e5 + 7, MOD = 1e9 + 7, Mod = 1e9 + 6, n                 INF = 1LL << 56; n long long n, m, k, x, y, z, dp[maxN][maxN], ans[maxN][maxN]; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) dp[i][j] = INF, ans[i][j] = INF; n   } n   for (int i = 1; i <= m; i++) cin >> x >> y >> z, dp[x][y] = min(dp[x][y], z); n   for (long long i = n; i >= 1; i--) { n     for (long long j = n; j >= 1; j--) n       dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]), n              dp[i][j - 1] = min(dp[i][j], dp[i][j - 1]); n   } n   for (int len = 1; len <= n - 1; len++) { n     for (int i = 1; i <= n - len; i++) { n       long long j = len - i; n       for (int u = i; u <= j; u++) n         dp[i][j] = min(dp[i][u] + dp[u + 1][j], dp[i][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i; j++) { n       if (i != 1) ans[i][j] = min(ans[i][j], ans[i - 1][j]); n       for (int u = 1; u <= j; u++) n         ans[i][j] = min(ans[i - u][j - u] + dp[i - u + 1][i], ans[i][j]); n     } n   } n   if (ans[n][k] == INF) return cout <<  -1 n , 0; n   return cout << ans[n][k] <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n void RD(int &x) { scanf( %d , &x); } n void RD(long long &x) { scanf( %I64d , &x); } n void RD(double &x) { scanf( %lf , &x); } n void RD(int &x, int &y) { scanf( %d%d , &x, &y); } n void RD(long long &x, long long &y) { scanf( %I64d%I64d , &x, &y); } n void RD(double &x, double &y) { scanf( %lf%lf , &x, &y); } n void RD(char *s) { scanf( %s , s); } n void RD(char &s) { scanf( %c , &s); } n void RD(string &s) { cin >> s; } n void PR(int x) { printf( %d n , x); } n void PR(int x, int y) { printf( %d %d n , x, y); } n void PR(long long x) { printf( %I64d n , x); } n void PR(char x) { printf( %c n , x); } n void PR(char *x) { printf( %s n , x); } n void PR(string x) { cout << x << endl; } n const int inf = 1e9; n const long long mod = 1LL; n int dp[1010][1010]; n char s1[1010], s2[1010]; n int len1, len2; n int solve(int n, int m) { n   if (n == len1) return dp[n][m] = len2 - m; n   if (m == len2) return dp[n][m] = len1 - n; n   int &ret = dp[n][m]; n   if (ret != inf) return ret; n   if (s1[n] == s2[m]) n     ret = solve(n + 1, m + 1); n   else n     ret = min(min(1 + solve(n + 1, m + 1), 1 + solve(n, m + 1)), n               1 + solve(n + 1, m)); n   return ret; n } n void path(int n, int m, int p, int q) { n   if (n == len1) { n     int i, j; n     for (i = m, j = q; i < len2; i++, j++) n       printf( INSERT %d %c n , j + 1, s2[i]); n     return; n   } n   if (m == len2) { n     int i; n     for (i = n; i < len1; i++) printf( DELETE %d n , p + 1); n     return; n   } n   if (s1[n] == s2[m]) return path(n + 1, m + 1, p + 1, q + 1); n   if (dp[n][m] == 1 + dp[n + 1][m + 1]) { n     printf( REPLACE %d %c n , p + 1, s2[m]); n     return path(n + 1, m + 1, p + 1, q + 1); n   } else if (dp[n][m] == 1 + dp[n][m + 1]) { n     printf( INSERT %d %c n , p + 1, s2[m]); n     return path(n, m + 1, p + 1, q + 1); n   } else { n     printf( DELETE %d n , p + 1); n     return path(n + 1, m, p, q); n   } n } n int main() { n   int i, j; n   while (scanf( %s %s , s1, s2) == 2) { n     len1 = strlen(s1); n     len2 = strlen(s2); n     for ((i) = 0; (i) < (int)(len1 + 1); (i)++) n       for ((j) = 0; (j) < (int)(len2 + 1); (j)++) dp[i][j] = inf; n     PR(solve(0, 0)); n     path(0, 0, 0, 0); n   } n   return 0; n } n


#include <algorithm> n #include <iostream> n #include <climits> n #include <iomanip> n #include <cstring> n #include <string> n #include <vector> n #include <queue> n #include <cmath> n #include <set> n #include <map> n  n using namespace std; n  n template <typename T> istream &operator>> n (istream &is, vector<T> &vec)  n {   for (auto &v : vec) is >> v; return is; } n  n #define all(x) (x).begin(),(x).end() n #define rall(x) (x).rbegin(),(x).rend() n #define ll long long n #define mod 1000000007 n #define nod 1000000007 n #define vi vector<int> n #define vll vector<ll> n #define pb push_back n  n  n // D R U L n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n  n const int mx = 1e6 + 5; n const int INF = mod; n  n ll power(ll x, ll y){ n     ll res = 1; n     while(y > 0){ n         if(y & 1) res = (res * x) % nod; n         y >>= 1; n         x = (x * x) % nod; n     } n     return (res % nod); n } n  n  n // Check for number of Cases!! n void solve() { n     ll n; cin >> n; n     string str; cin >> str; n     str +=  $ ; n      n     vector<int> freq(26, -1); n      n     for(int i = 1; i < str.length(); i++) { n         if(str[i] != str[i - 1]) { n             if(freq[str[i - 1] -  A ] == -1) { n                 freq[str[i - 1] -  A ] = i; n             }else { n                 cout <<  NO n ; n                 return; n             } n         } n     } n      n     cout <<  YES n ; n } n  n int main(){ n  n     #ifndef ONLINE_JUDGE n     freopen( /ATOMCODES/input.txt ,  r , stdin); n     freopen( /ATOMCODES/output.txt ,  w , stdout); n     freopen( /ATOMCODES/err_output.txt ,  w , stderr); n     #endif n  n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n      n     int t = 1; n     cin >> t; n     while(t--) { n       solve(); n     } n }


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n void solve() { n   long long v, c, vv, cc; n   cin >> v >> c >> vv >> cc; n   long long f = 0; n   if (cc > min(v, c)) { n     cout <<  NO n ; n     return; n   } n   v += c; n   vv += cc; n   if (vv > v) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   if (f == 1) { n   } else { n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, c = 0, p = 0; n     cin >> n; n     long long int s = (n / 2); n     for (int i = 1; i <= s; i++) { n       p += 8; n       c += (p * i); n     } n     cout << c << endl; n   } n } n


#include <bits/stdc++.h> n namespace imzzy { n class fastin { n  private: n   int _ch, _f; n  n  public: n   inline fastin &operator>>(char &c) { n     c = getchar(); n     return *this; n   } n   template <typename _Tp> n   inline fastin &operator>>(_Tp &_x) { n     _x = 0; n     while (!isdigit(_ch)) _f |= (_ch == 45), _ch = getchar(); n     while (isdigit(_ch)) n       _x = (_x << 1) + (_x << 3) + (_ch ^ 48), _ch = getchar(); n     _f && (_x = -_x, _f = 0); n     return *this; n   } n   fastin() { _ch = _f = 0; } n } fin; n }  // namespace imzzy n using namespace imzzy; n const int maxn = 1004, maxm = 500004; n int nxt[maxn], pre[maxn], p[5]; n bool g1[maxn][maxn], g2[maxn][maxn]; n std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > > ans1, ans2; n inline std::pair<std::pair<int, int>, std::pair<int, int> > rev( n     int u, int v, int n, bool g[maxn][maxn]) { n   int indx = 0; n   for (register int i = 1; i <= n; ++i) n     if (g[u][i] && g[v][i]) p[++indx] = i; n   g[u][v] = g[v][u] = 0, g[p[1]][p[2]] = g[p[2]][p[1]] = 1; n   return std::make_pair(std::make_pair(u, v), std::make_pair(p[1], p[2])); n } n inline void work( n     int n, bool g[maxn][maxn], n     std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > > &res) { n   for (register int i = 2; i <= n; ++i) { n     for (register int j = n; j > 1; --j) { n       if (j == i || j == pre[i] || j == nxt[i]) continue; n       if (g[i][j]) res.push_back(rev(i, j, n, g)); n     } n   } n } n signed main() { n   int n, u, v; n   fin >> n; n   for (register int i = 1; i <= n; ++i) pre[i] = i - 1, nxt[i] = i + 1; n   pre[1] = n, nxt[n] = 1; n   for (register int i = 1; i <= n; ++i) n     g1[i][pre[i]] = g1[i][nxt[i]] = g2[i][pre[i]] = g2[i][nxt[i]] = 1; n   for (register int i = 1; i <= n - 3; ++i) n     fin >> u >> v, g1[u][v] = g1[v][u] = 1; n   for (register int i = 1; i <= n - 3; ++i) n     fin >> u >> v, g2[u][v] = g2[v][u] = 1; n   work(n, g1, ans1), work(n, g2, ans2); n   std::cout << ans1.size() + ans2.size() <<   n ; n   for (register int i = 0; i < ans1.size(); ++i) n     std::cout << ans1[i].first.first <<     << ans1[i].first.second <<   n ; n   for (register int i = ans2.size() - 1; i >= 0; --i) n     std::cout << ans2[i].second.first <<     << ans2[i].second.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a <= b) n     cout <<  Second ; n   else if (a > b) n     cout <<  First ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, f; n char x; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> x; n       if (x !=  B  && x !=  W  && x !=  G ) f = 1; n     } n   if (f == 0) n     cout <<  #Black&White ; n   else n     cout <<  #Color ; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int u, v, w; n   Edge() {} n   Edge(int tu, int tv, int tw) : u(tu), v(tv), w(tw) {} n   int operator<(const Edge e1) const { return w > e1.w; } n } p[100007]; n int n, m; n struct uset { n   int p[100007], sz[100007]; n   uset() {} n   uset(int n) { n     for (int i = 1; i <= n; i++) p[i] = i, sz[i] = 1; n   } n   int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } n } us; n double kruskal() { n   us = uset(n); n   sort(p, p + m); n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     int u = p[i].u, v = p[i].v; n     int fx = us.find(u), fy = us.find(v); n     long long s1 = us.sz[fx], s2 = us.sz[fy]; n     if (fx != fy) { n       long long cur = (s1 + s2) * (s1 + s2 - 1) - s1 * (s1 - 1) - s2 * (s2 - 1); n       ans += cur * (long long)p[i].w; n       us.p[fx] = fy; n       us.sz[fy] += us.sz[fx]; n     } n   } n   return double(ans) / n / (n - 1); n } n int main() { n   int c[100007], u, v; n   for (; cin >> n >> m;) { n     for (int i = 1; i <= n; i++) scanf( %d , c + i); n     for (int i = 0; i < m; i++) { n       scanf( %d%d , &u, &v); n       p[i] = Edge(u, v, min(c[u], c[v])); n     } n     printf( %.9lf n , kruskal()); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 200100; n const int M = 1000100; n const long long mod = 998244353; n int main() { n   ios::sync_with_stdio(false); n   int q; n   cin >> q; n   while (q--) { n     string s; n     cin >> s; n     int l = 0, r = 0, u = 0, d = 0; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  L ) l++; n       if (s[i] ==  R ) r++; n       if (s[i] ==  U ) u++; n       if (s[i] ==  D ) d++; n     } n     int ss = 0; n     int minl = min(l, r); n     int okl = 0, oku = 0; n     if (minl != 0) { n       okl = 1; n     } n     ss += minl; n     int minu = min(u, d); n     if (minu != 0) { n       oku = 1; n     } n     ss += minu; n     if (okl && oku) { n       cout << ss * 2 << endl; n       for (int i = 1; i <= minl; i++) { n         cout <<  L ; n       } n       for (int i = 1; i <= minu; i++) { n         cout <<  U ; n       } n       for (int i = 1; i <= minl; i++) { n         cout <<  R ; n       } n       for (int i = 1; i <= minu; i++) { n         cout <<  D ; n       } n       cout << endl; n     } else if (okl && !oku) { n       cout << 2 << endl; n       cout <<  LR  << endl; n     } else if (!okl && oku) { n       cout << 2 << endl; n       cout <<  UD  << endl; n     } else { n       cout << 0 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t = 1; n   while (t--) { n     long long n, min, cnt = 0, x; n     cin >> n; n     vector<long long> v(n); n     for (int i = 0; i < n; ++i) cin >> v[i]; n     min = *min_element(v.begin(), v.end()); n     for (int i = 0; i < n; ++i) { n       if (min == v[i]) { n         cnt++; n         x = i; n       } n     } n     if (cnt > 1) n       cout <<  Still Rozdil  << endl; n     else n       cout << x + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string a, b, c; n     cin >> a >> b >> c; n     bool ok = true; n     for (int i = 0; i < a.size(); i++) { n       ok &= a[i] == c[i] || b[i] == c[i]; n     } n     cout << (ok ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4e5 + 5; n int fa[maxn]; n int m, n, q, i, x, y; n int getfa(int x) { n   if (x == fa[x]) return x; n   return fa[x] = getfa(fa[x]); n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (i = 1; i <= m + n; i++) fa[i] = i; n   for (i = 1; i <= q; i++) { n     scanf( %d%d , &x, &y); n     fa[getfa(x)] = getfa(y + n); n   } n   int num = 0; n   for (i = 1; i <= m + n; i++) { n     if (fa[i] == i) num++; n   } n   cout << num - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int arr[300001]; n bool check(int val) { n   if (val < 0) return false; n   if (val >= m - 1) return true; n   int prev = 0; n   for (int i = int(1); i <= int(n); i++) { n     if (arr[i] + val < prev) return false; n     int r = (arr[i] + val) % m; n     if (r < arr[i]) { n       if (r >= prev) n         continue; n       else n         prev = (prev > arr[i] ? prev : arr[i]); n     } else { n       prev = (prev > arr[i] ? prev : arr[i]); n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = int(1); i <= int(n); i++) cin >> arr[i]; n   int l = 0, r = m - 1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid) && (!check(mid - 1))) { n       cout << mid; n       return 0; n     } else if (!check(mid)) n       l = mid + 1; n     else n       r = mid - 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100100; n const int INF = 0x7f7f7f7f; n const double eps = 1e-8; n const double pi = 3.1415926535897932384626433832795; n int main() { n   int n; n   ios::sync_with_stdio(0); n   cin >> n; n   cout << n * n / 4 << endl; n   for (int i = 1; i <= n / 2; i++) n     for (int j = n / 2 + 1; j <= n; j++) cout << i <<     << j << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   ,  ; n   err(++it, args...); n } n namespace fastIO { n bool IOerror = 0; n inline char nc() { n   static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; n   if (p1 == pend) { n     p1 = buf; n     pend = buf + fread(buf, 1, 100000, stdin); n     if (pend == p1) { n       IOerror = 1; n       return -1; n     } n   } n   return *p1++; n } n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n inline void read(int &x) { n   char ch; n   while (blank(ch = nc())) n     ; n   if (IOerror) return; n   for (x = ch -  0 ; (ch = nc()) >=  0  && ch <=  9 ; x = x * 10 + ch -  0 ) n     ; n } n }  // namespace fastIO n using namespace fastIO; n const double PI = acos(-1.0); n const double eps = 1e-8; n const int maxn = 2e6 + 7; n const long long mod = 1e9 + 7; n long long fac[maxn], finv[maxn]; n long long qp(long long b, long long n) { n   long long r = 1; n   if (n <= 0) n += mod - 1; n   b %= mod; n   for (; n; n >>= 1, b = b * b % mod) n     if (n & 1) r = r * b % mod; n   return r; n } n long long pw[maxn]; n void init() { n   fac[1] = fac[0] = finv[0] = 1; n   for (int i = 2; i < maxn; ++i) fac[i] = fac[i - 1] * i % mod; n   finv[maxn - 1] = qp(fac[maxn - 1], mod - 2); n   for (int i = maxn - 2; i >= 1; --i) finv[i] = finv[i + 1] * (i + 1) % mod; n } n long long C(int n, int m) { n   return n >= m ? fac[n] * finv[n - m] % mod * finv[m] % mod : 0; n } n long long A(int n, int m) { return n >= m ? fac[n] * finv[n - m] % mod : 0; } n int main() { n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   init(); n   long long res = 0; n   pw[0] = 1; n   for (int i = 1; i < maxn; ++i) pw[i] = pw[i - 1] * m % mod; n   for (int i = 0; i <= n - 2; ++i) { n     long long tmp = A(n - 2, i) * C(m - 1, i) % mod * pw[n - 2 - i] % mod * n                     (i + 2) % mod * qp(n, n - i - 3) % mod; n     res = (res + tmp) % mod; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:60000000 ) n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n const long long inf64 = ((long long)1 << 62) - 1; n const long double pi = 3.1415926535897932384626433832795; n template <class T> n T sqr(T x) { n   return x * x; n } n int n, m, k; n vector<pair<int, int> > a; n vector<int> g[300500]; n int u[300500]; n vector<int> q; n void go(int v) { n   u[v] = 1; n   for (int i = 0; i < (int)(g[v].size()); i++) n     if (u[g[v][i]] == 0) n       go(g[v][i]); n     else if (u[g[v][i]] == 1) { n       cout << -1 << endl; n       exit(0); n     } n   u[v] = 2; n   q.push_back(v); n } n int main() { n   cin >> n >> m; n   k = m; n   for (int i = 0; i < (int)(n); i++) { n     a.clear(); n     for (int j = 0; j < (int)(m); j++) { n       int x; n       scanf( %d , &x); n       if (x != -1) a.push_back(make_pair(x, j)); n     } n     sort(a.begin(), a.end()); n     if (a.size() == 0) continue; n     int l = 0; n     int r = l; n     while (r + 1 < (int)a.size() && a[r + 1].first == a[l].first) r++; n     while (r + 1 < (int)a.size()) { n       int l2 = r + 1; n       int r2 = l2; n       while (r2 + 1 < (int)a.size() && a[r2 + 1].first == a[l2].first) r2++; n       for (int j = l; j <= r; j++) g[a[j].second].push_back(k); n       for (int j = l2; j <= r2; j++) g[k].push_back(a[j].second); n       k++; n       l = l2; n       r = r2; n     } n   } n   memset(u, 0, sizeof(u)); n   q.clear(); n   for (int i = 0; i < (int)(m); i++) n     if (!u[i]) go(i); n   for (int i = (int)(q.size()) - 1; i >= 0; i--) n     if (q[i] < m) printf( %d  , q[i] + 1); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline void sub(long long &a, long long b) { n   a -= b; n   if (a < 0) a += 1000000007; n } n inline void add(long long &a, long long b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n } n template <typename T> n inline T const &MAX(T const &a, T const &b) { n   return a > b ? a : b; n } n template <typename T> n inline T const &MIN(T const &a, T const &b) { n   return a < b ? a : b; n } n inline long long qp(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % 1000000007; n     a = a * a % 1000000007, b >>= 1; n   } n   return ans; n } n inline long long qp(long long a, long long b, long long c) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % c; n     a = a * a % c, b >>= 1; n   } n   return ans; n } n using namespace std; n const unsigned long long ba = 233; n const double eps = 1e-5; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int N = 1000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f; n vector<long long> v; n int main() { n   long long n; n   scanf( %lld , &n); n   if (n % 3 != 0) return 0 * puts( 0 ); n   n /= 3; n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       v.push_back(i); n       if (i * i != n) v.push_back(n / i); n     } n   } n   sort(v.begin(), v.end()); n   int ans = 0; n   for (int i = 0; i < v.size() && v[i] * v[i] * v[i] <= n; i++) { n     for (int j = i; j < v.size() && v[i] * v[j] * v[j] <= n; j++) { n       long long x = v[i], y = v[j], z = n / v[i] / v[j], a, b, c; n       if (z < y || x * y * z != n) continue; n       if ((x - y + z) % 2 != 0) continue; n       b = (x - y + z) / 2; n       a = x - b; n       c = z - b; n       if (a > b || a > c || b > c || a <= 0 || b <= 0 || c <= 0) continue; n       set<int> s; n       s.insert(a), s.insert(b), s.insert(c); n       if (s.size() == 3) n         ans += 6; n       else if (s.size() == 2) n         ans += 3; n       else n         ans += 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 10; n int mod = 1e9 + 7; n long long power(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long INV(long long a) { return power(a, mod - 2); } n mt19937 Rand(123456); n int Range(int l, int r) { return l + Rand() % (r - l + 1); } n map<long long, long long> mp; n void work(int l, int r, int k) { n   if (k == 1 || l == r) { n     ++mp[r - l + 1]; n     return; n   } n   int mid = (l + r) / 2; n   work(l, mid, k - 1); n   work(mid + 1, r, k - 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k >> mod; n   vector<long long> inv(2 * n), inv_sum; n   for (int i = 0; i <= 2 * n - 1; i++) inv[i] = INV(i); n   inv_sum = inv; n   for (int i = 1; i <= 2 * n - 1; i++) inv_sum[i] += inv_sum[i - 1]; n   for (auto& I : inv_sum) I %= mod; n   auto merge = [&](int l, int r, const vector<long long>& inv_sum) { n     long long res = (long long)l * r % mod * INV(2) % mod; n     for (int i = 1; i <= l; i++) res -= inv_sum[i + r] - inv_sum[i]; n     res %= mod; n     res += mod; n     return res % mod; n   }; n   work(1, n, k); n   long long res = 0; n   for (auto I : mp) n     res += (I.first * (I.first - 1) % mod * INV(4) % mod) * I.second % mod, n         res += I.second * (I.second - 1) % mod * INV(2) % mod * n                merge(I.first, I.first, inv_sum) % mod; n   for (auto I : mp) n     for (auto J : mp) n       if (I.first < J.first) n         res += n             merge(I.first, J.first, inv_sum) * I.second % mod * J.second % mod; n   res %= mod; n   res += mod; n   res %= mod; n   cout << res; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ,  fast-math ,  unroll-loops ,   n                       no-stack-protector ) n #pragma GCC target( popcnt,tune=native ) n using namespace std; n namespace io { n const int SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, n                                        qu[55]; n int f, qr; n inline void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n inline void putc(char x) { n   *oS++ = x; n   if (oS == oT) flush(); n } n template <class T> n inline void getc(T &x) { n   for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++); n        !((c >=  0  && c <=  9 ) || (c >=  a  && c <=  z ) || n          (c >=  A  && c <=  Z )); n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     ; n   x = c; n } n template <class I> n inline void rd(I &x) { n   for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) f = -1; n   for (x = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     x = x * 10 + (c & 15); n   x *= f; n } n template <class I> n inline void print(I x) { n   if (!x) putc( 0 ); n   if (x < 0) putc( - ), x = -x; n   while (x) qu[++qr] = x % 10 +  0 , x /= 10; n   while (qr) putc(qu[qr--]); n } n struct Flusher_ { n   ~Flusher_() { flush(); } n } io_flusher_; n }  // namespace io n using io ::getc; n using io ::print; n using io ::putc; n using io ::rd; n const int MAXN = 100 + 5; n const int MOD = 1e9 + 7; n int mo(int x, int y) { return (x + y) >= MOD ? (x + y - MOD) : (x + y); } n int sub(int x, int y) { return x < y ? (x - y + MOD) : (x - y); } n int ksm(int x, int y) { n   int res = 1; n   for (; y; y >>= 1, x = 1ll * x * x % MOD) n     if (y & 1) res = 1ll * res * x % MOD; n   return res; n } n void fix(int &x, const int &v) { x = (x + v) >= MOD ? (x + v - MOD) : (x + v); } n int n, m; n bool vis[MAXN][MAXN]; n vector<int> e[MAXN][MAXN]; n void add(int x, int y, vector<int> t) { n   vis[x][y] = 1; n   e[x][y] = t; n } n struct Node { n   vector<int> g[MAXN][MAXN]; n   vector<int> solvemid() { n     vector<int> nxt, res, now; n     for (int x = 1; x <= n; x++) n       for (int y = 1; y <= n; y++) n         for (int i = 0; i + 1 < e[x][y].size(); i++) n           if (e[x][y][i] == x && e[x][y][i + 1] == y) { n             now.clear(); n             nxt = e[x][y]; n             bool flag = 1; n             do { n               now = nxt; n               nxt.clear(); n               flag = (now.size() <= (2 * n + 1)); n               for (int i = 0; i + 1 < now.size() && flag; i++) { n                 if (!vis[now[i]][now[i + 1]]) { n                   flag = 0; n                   break; n                 } n                 nxt.insert(nxt.end(), e[now[i]][now[i + 1]].begin(), n                            e[now[i]][now[i + 1]].end()); n               } n             } while (flag && now != nxt); n             if (flag) { n               if (!res.size()) res = nxt; n               g[nxt[0]][nxt.back()].push_back(nxt.size() - 1); n             } n             break; n           } n     return res; n   } n   void solvefront() { n     vector<int> nxt, res, now, Cnt; n     for (int x = 1; x <= n; x++) n       for (int y = 1; y <= n; y++) n         if (e[x][y].size() && e[x][y].back() == x) { n           now.clear(); n           nxt.clear(); n           nxt.push_back(x); n           bool flag = 1; n           do { n             now = nxt; n             nxt.clear(); n             flag = (now.size() <= (2 * n + 1)); n             nxt = e[now[0]][y]; n             reverse(nxt.begin(), nxt.end()); n             for (int i = 0; i + 1 < now.size() && flag; i++) { n               if (!vis[now[i + 1]][now[i]]) { n                 flag = 0; n                 break; n               } n               nxt.insert(nxt.end(), e[now[i + 1]][now[i]].rbegin(), n                          e[now[i + 1]][now[i]].rend()); n             } n           } while (flag && now != nxt); n           if (flag) g[nxt.back()][y].push_back(nxt.size()); n         } n   } n   void solveend() { n     vector<int> nxt, res, now, Cnt; n     for (int x = 1; x <= n; x++) n       for (int y = 1; y <= n; y++) n         if (e[x][y].size() && e[x][y][0] == y) { n           now.clear(); n           nxt.clear(); n           nxt.push_back(y); n           bool flag = 1; n           do { n             now = nxt; n             nxt.clear(); n             flag = (now.size() <= (2 * n + 1)); n             nxt = e[x][now[0]]; n             for (int i = 0; i + 1 < now.size() && flag; i++) { n               if (!vis[now[i]][now[i + 1]]) { n                 flag = 0; n                 break; n               } n               nxt.insert(nxt.end(), e[now[i]][now[i + 1]].begin(), n                          e[now[i]][now[i + 1]].end()); n             } n           } while (flag && now != nxt); n           if (flag) g[x][nxt.back()].push_back(nxt.size()); n         } n   } n   int dp[MAXN][MAXN][MAXN]; n   void solveA() { n     for (int i = 1; i <= n; i++) dp[i][i][0] = 1; n     for (int i = 0; i <= n * 2; i++) n       for (int s = 1; s <= n; s++) n         for (int l = 1; l <= n; l++) n           for (int r = 1; r <= n; r++) n             for (auto &len : g[l][r]) n               if (i + len <= 2 * n + 1) fix(dp[s][r][i + len], dp[s][l][i]); n   } n } A, B, C; n int dp[MAXN][MAXN][MAXN], dp0[MAXN][MAXN][MAXN], ans[MAXN][MAXN]; n int main() { n   rd(n); n   rd(m); n   for (int i = 1, a, b, K; i <= m; i++) { n     rd(a); n     rd(b); n     rd(K); n     vector<int> v; n     v.resize(K); n     for (int i = 0; i < K; i++) rd(v[i]); n     add(a, b, v); n   } n   A.solvefront(); n   vector<int> res = B.solvemid(); n   C.solveend(); n   A.solveA(); n   for (int i = 0; i <= n * 2; i++) n     for (int s = 1; s <= n; s++) n       for (int l = 1; l <= n; l++) n         for (int r = 1; r <= n; r++) n           for (auto &len : B.g[l][r]) n             if (i + len <= 2 * n) fix(dp[s][r][i + len], A.dp[s][l][i]); n   for (int i = 0; i <= n * 2; i++) n     for (int s = 1; s <= n; s++) n       for (int l = 1; l <= n; l++) n         for (int r = 1; r <= n; r++) n           for (auto &len : C.g[l][r]) n             if (i + len <= 2 * n) fix(dp[s][r][i + len], dp[s][l][i]); n   for (int l = 1; l <= n; l++) n     for (int mid = 1; mid <= n; mid++) n       if (vis[l][mid] && e[l][mid].empty()) n         for (int r = 1; r <= n; r++) n           for (int i = 0; i + 1 <= 2 * n; i++) n             fix(dp0[l][r][i + 1], dp[mid][r][i]); n   for (int l = 1; l <= n; l++) n     for (int r = 1; r <= n; r++) n       for (int len = 1; len <= 2 * n; len++) fix(ans[r][len], dp[l][r][len]); n   for (int i = 1; i <= 2 * n; i++) n     for (int l = 1; l <= n; l++) n       for (int r = 1; r <= n; r++) n         for (int len = 1; len + i <= 2 * n; len++) n           fix(ans[r][len + i], 1ll * ans[l][i] * dp0[l][r][len] % MOD); n   for (int i = 1; i <= 2 * n; i++) { n     int aa = 0; n     for (int s = 1; s <= n; s++) fix(aa, ans[s][i]); n     print(aa); n     putc(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double t, y, j, n; n   cin >> t; n   while (t--) { n     cin >> n; n     j = n * n; n     j = j - 4 * n; n     if (j >= 0) { n       double x = sqrt(j); n       if ((-n - x) > 0) n         cout <<  N ; n       else { n         x = (n - x) / 2; n         if (x != 0) n           y = n / x; n         else n           y = 0; n         std::cout << std::fixed <<  Y  n                   <<     << std::setprecision(9) << x <<     << y; n       } n     } else n       cout <<  N ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long INF = 2e18; n constexpr long long MOD = 1e9 + 7; n constexpr long long MAXN = 2e5 + 3; n void solve() { n   long long n; n   cin >> n; n   map<long long, long long> m; n   long long ans = 0; n   for (long long i = 1; i <= n; ++i) { n     long long x; n     cin >> x; n     m[x - i] += x; n     ans = max(ans, m[x - i]); n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n float S, a, b, c; n int main() { n   ios ::sync_with_stdio(false); n   cin >> S >> a >> b >> c; n   float sum = a + b + c; n   if (sum == 0) n     cout <<  0 0 0 ; n   else n     printf( %.12f %.12f %.12f , a * S / sum, b * S / sum, c * S / sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 20; n const long long mod = 1e9 + 7; n int w[(maxn * 4) << 2], n; n vector<pair<int, int> > col[(maxn * 4) << 2]; n int query(int L, int R, int l, int r, int rt); n void pushup(int rt) { w[rt] = w[rt << 1] + w[rt << 1 | 1]; } n void pushdown(int rt) { n   for (int i = 0; i < col[rt].size(); i++) { n     int r = col[rt][i].second, l = col[rt][i].first; n     int m = (l + r) >> 1; n     col[rt << 1].push_back(make_pair(r + l - m, r)); n     col[rt << 1 | 1].push_back(make_pair(l, r + l - (m + 1))); n     w[rt << 1] += query(r + l - m, r, 0, n, 1); n     w[rt << 1 | 1] += query(l, r + l - (m + 1), 0, n, 1); n   } n   col[rt].clear(); n } n void build(int l, int r, int rt) { n   col[rt].clear(); n   w[rt] = 1; n   if (l == r) return; n   int m = (l + r) >> 1; n   build(l, m, rt << 1); n   build(m + 1, r, rt << 1 | 1); n   pushup(rt); n } n void update(int L0, int R0, int L, int R, int l, int r, int rt) { n   if (L <= l && r <= R) { n     int R1 = R0 - (l - L), L1 = L0 + R - r; n     col[rt].push_back(make_pair(L1, R1)); n     w[rt] += query(L1, R1, 0, n, 1); n     return; n   } n   pushdown(rt); n   int m = (l + r) >> 1; n   if (L <= m) update(L0, R0, L, R, l, m, rt << 1); n   if (m < R) update(L0, R0, L, R, m + 1, r, rt << 1 | 1); n   pushup(rt); n } n int query(int L, int R, int l, int r, int rt) { n   if (L <= l && r <= R) { n     return w[rt]; n   } n   pushdown(rt); n   int m = (l + r) >> 1; n   int ret = 0; n   if (L <= m) ret += query(L, R, l, m, rt << 1); n   if (m < R) ret += query(L, R, m + 1, r, rt << 1 | 1); n   return ret; n } n int main() { n   int q; n   while (~scanf( %d%d , &n, &q)) { n     int curl = 0, curr = n - 1; n     build(0, n, 1); n     int ra = 1; n     for (int i = 0; i < q; i++) { n       int x; n       scanf( %d , &x); n       if (ra) { n         if (x == 1) { n           int p; n           scanf( %d , &p); n           if (curl + 2 * p - 1 <= curr) { n             update(curl, curl + p - 1, curl + p, curl + 2 * p - 1, 0, n, 1); n             curl += p; n           } else { n             p = curr - curl + 1 - p; n             if (p) n               update(curr - p + 1, curr, curr - 2 * p + 1, curr - p, 0, n, 1); n             curr -= p; n             ra = 0; n           } n         } n         if (x == 2) { n           int l, r; n           scanf( %d%d , &l, &r); n           int ans = query(curl + l, curl + r - 1, 0, n, 1); n           printf( %d n , ans); n         } n       } else { n         if (x == 1) { n           int p; n           scanf( %d , &p); n           if (curr - 2 * p + 1 >= curl) { n             update(curr - p + 1, curr, curr - 2 * p + 1, curr - p, 0, n, 1); n             curr -= p; n           } else { n             p = curr - curl + 1 - p; n             if (p) n               update(curl, curl + p - 1, curl + p, curl + 2 * p - 1, 0, n, 1); n             curl += p; n             ra = 1; n           } n         } n         if (x == 2) { n           int l, r; n           scanf( %d%d , &l, &r); n           int ans = query(curr - r + 1, curr - l, 0, n, 1); n           printf( %d n , ans); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MM = 120000; n vector<int> G[MM], T[MM]; n int P[MM], L[MM]; n void dfs(int u, int par) { n   P[u] = par; n   if (par != -1) L[u] = L[par] + 1; n   for (auto v : G[u]) { n     if (v != par) { n       dfs(v, u); n     } n   } n } n int N, K; n bool rip = 0; n void dfs2(int u, int par, int lev) { n   int kek = 0; n   for (auto v : G[u]) { n     if (v != par) { n       kek++; n       dfs2(v, u, lev + 1); n     } n   } n   if (kek < 3 && kek != 0) { n     rip = 1; n   } n   if (kek == 0 && lev != K) { n     rip = 1; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N >> K; n   for (int i = 0; i < N - 1; i++) { n     int u, v; n     cin >> u >> v; n     G[u - 1].push_back(v - 1); n     G[v - 1].push_back(u - 1); n   } n   dfs(0, -1); n   int mx = 0; n   int el = -1; n   for (int i = 0; i < N; i++) { n     mx = max(mx, L[i]); n     if (mx == L[i]) { n       el = i; n     } n   } n   if (mx >= K) { n     mx = K; n     while (mx--) el = P[el]; n     int rt = el; n     dfs2(rt, -1, 0); n     if (rip) { n       cout <<  No ; n     } else { n       cout <<  Yes ; n     } n   } else { n     cout <<  No ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get(int x) { n   int cnt = 0; n   while (x >= 10) { n     int sum = 0; n     while (x) { n       sum += x % 10; n       x /= 10; n     } n     cnt++; n     x = sum; n   } n   return cnt; n } n int main() { n   string s; n   cin >> s; n   if (s.size() == 1) n     cout << 0 << endl; n   else { n     int n = 0; n     for (int i = 0; i < s.size(); i++) n += s[i] -  0 ; n     cout << get(n) + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long ans = 1; n   ans += (3 * n * (n + 1)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, k; n   cin >> n >> k; n   long long int ans = k / n; n   long long int t1 = ans * n; n   if (t1 < k) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.push_back(move(x)); n   return move(v); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<   n ; n   err(++it, args...); n } n int N, M; n int X[5013]; n pair<long long, int> H[5013]; n long long dp[5013][5013]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> N >> M; n   for (int i = 0; i < N; i++) { n     cin >> X[i]; n   } n   long long housing = 0; n   for (int i = 1; i <= M; i++) { n     cin >> H[i].first >> H[i].second; n     housing += H[i].second; n   } n   if (N > housing) { n     cout << -1 << endl; n     return 0; n   } n   sort(X, X + N); n   sort(H + 1, H + M + 1); n   for (int i = 0; i < N; i++) { n     dp[0][i] = 1123456789123456789LL; n   } n   for (int i = 1; i <= M; i++) { n     long long allin = 0; n     deque<pair<long long, int> > mpq; n     mpq.push_back({0, -1}); n     for (int j = 0; j < N; j++) { n       allin += abs(X[j] - H[i].first); n       long long diff = dp[i - 1][j] - allin; n       while (mpq.size() && diff <= mpq.back().first) { n         mpq.pop_back(); n       } n       mpq.push_back({diff, j}); n       while (j - mpq.front().second > H[i].second) { n         mpq.pop_front(); n       } n       dp[i][j] = allin + mpq.front().first; n     } n   } n   cout << dp[M][N - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005], dp[1000005]; n int main() { n   int n, i, j; n   while (~scanf( %d , &n)) { n     memset(dp, 0, sizeof(dp)); n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     for (i = 0; i < n; i++) { n       dp[a[i]] = max(dp[a[i]], 1); n       for (j = a[i] + a[i]; j <= a[n - 1]; j += a[i]) n         dp[j] = max(dp[j], dp[a[i]] + 1); n     } n     int ans = 1; n     for (i = 0; i < n; i++) ans = max(ans, dp[a[i]]); n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char **argv) { n   int n; n   long long m; n   cin >> n >> m; n   m -= 1; n   vector<int> xs; n   for (int i = 0; i < n; i++) { n     xs.push_back(-1); n   } n   int i = 0; n   int u = 0; n   int v = n - 1; n   while (u <= v) { n     long long h = (1LL << (n - i - 2)); n     if (m >= h) { n       xs[v] = i; n       v -= 1; n       m -= h; n     } else { n       xs[u] = i; n       u += 1; n     } n     i += 1; n   } n   for (int i = 0; i < n; i++) printf( %d  , xs[i] + 1); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[222222]; n long long sum[222222]; n long long max1[222222]; n long long max2[222222]; n long long get(long long l, long long r) { n   long long t = sum[r]; n   if (l >= 1) { n     t -= sum[l - 1]; n   } n   return t; n } n int main() { n   long long n, k; n   cin >> n >> k; n   for (long long i = 0; i < n; i++) { n     cin >> p[i]; n     if (i) { n       sum[i] = sum[i - 1] + p[i]; n     } else { n       sum[i] = p[i]; n     } n   } n   for (long long i = 0; i < n; i++) { n     if (i < k - 1) { n       max1[i] = 0; n     } else { n       max1[i] = max(max1[i - 1], get(i + 1 - k, i)); n     } n   } n   long long b = -1; n   for (long long i = 0; i < n; i++) { n     if (i < 2 * k - 1) { n       max2[i] = 0; n     } else { n       max2[i] = max(max2[i - 1], get(i + 1 - k, i) + max1[i - k]); n       ; n     } n   } n   long long i = n - 1; n   while (i >= 0) { n     if (max2[i - 1] >= get(i + 1 - k, i) + max1[i - k]) { n       i--; n     } else { n       b = i + 1 - k; n       break; n     } n   } n   long long se = max2[n - 1] - get(b, b + k - 1); n   for (long long i = 0; i < n; i++) { n     if (get(i, i + k - 1) == se) { n       cout << i + 1 <<    ; n       cout << b + 1 <<   n ; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 9223000000000000000; n struct DATA { n   long long v; n   long long c; n   long double p; n }; n long long n, l; n DATA data[31]; n bool cmp(DATA x, DATA y) { return x.p < y.p; } n void init() { n   scanf( %lld%lld , &n, &l); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &data[i].c); n     data[i].v = pow(2, i - 1); n     data[i].p = (long double)data[i].c / data[i].v; n   } n   sort(data + 1, data + n + 1, cmp); n } n long long process(long long left) { n   long long ans = maxn; n   if (left == 0) return 0; n   for (int i = 1; i <= n; i++) { n     if (data[i].v >= left) ans = min(ans, data[i].c); n   } n   int id = -1; n   for (int i = 1; i <= n; i++) n     if (data[i].v < left) { n       id = i; n       break; n     } n   if (id != -1) { n     long long sum = 0; n     long long k = left / data[id].v; n     sum = k * data[id].c + process(left - k * data[id].v); n     ans = min(ans, sum); n   } n   return ans; n } n int main() { n   init(); n   printf( %lld , process(l)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T1, typename T2> n ostream &operator<<(ostream &out, const pair<T1, T2> &item) { n   out <<  (  << item.first <<  ,   << item.second <<  ) ; n   return out; n } n template <typename T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   for (const auto &item : v) out << item <<    ; n   return out; n } n int x[2][2], y[2][2]; n void mul(int a[2][2], int b[2][2], int c[2][2], int mod) { n   for (int i = 0; i < 2; ++i) n     for (int j = 0; j < 2; ++j) n       c[i][j] = (1LL * a[i][0] * b[0][j] + 1LL * a[i][1] * b[1][j]) % mod; n } n void pw(ll exp, int mod) { n   int aux[2][2]; n   y[0][0] = y[1][1] = 1 % mod; n   y[0][1] = y[1][0] = 0; n   for (; exp; exp >>= 1) { n     if (exp & 1) { n       mul(x, y, aux, mod); n       memcpy(y, aux, sizeof aux); n     } n     mul(x, x, aux, mod); n     memcpy(x, aux, sizeof aux); n   } n } n int fib(ll n, int mod) { n   if (n <= 2) return 1 % mod; n   x[0][0] = 0; n   x[0][1] = x[1][0] = x[1][1] = 1 % mod; n   pw(n - 1, mod); n   return (y[0][0] + y[1][0]) % mod; n } n ll l, r, k; n ll collect(ll n) { n   if (n == 0) return 0; n   ll kk, res = 0; n   for (ll x = 1; x * x <= n; ++x) { n     if (r / x - (l - 1) / x >= k) res = max(res, x); n     kk = n / x; n   } n   for (; kk; --kk) { n     ll x = n / kk; n     if (r / x - (l - 1) / x >= k) res = max(res, x); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   ll mod; n   cin >> mod >> l >> r >> k; n   ll ans = collect(l - 1); n   ans = max(ans, collect(r)); n   cout << fib(ans, mod) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     long long a[100001]; n     long long y = 0; n     long long thissum = 0, maxsum = 0; n     int count = 1; n     for (int i = 0; i < n; i++) { n       scanf( %lld , &a[i]); n       y += a[i]; n     } n     thissum = a[0]; n     maxsum = a[0]; n     int temp = 1; n     for (int i = 1; i < n; i++) { n       if (thissum > 0) { n         thissum += a[i]; n         temp++; n       } else { n         thissum = a[i]; n         temp = 1; n       } n       if (thissum > maxsum) { n         maxsum = thissum; n         count = temp; n       } n     } n     int flag; n     if (y > maxsum) { n       flag = 1; n     } else { n       if (y == maxsum && count == n) { n         maxsum = maxsum - min(a[0], a[n - 1]); n         if (y > maxsum) { n           flag = 1; n         } else { n           flag = 0; n         } n       } else { n         flag = 0; n       } n     } n     if (flag == 1) { n       printf( YES n ); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long x, y; n     cin >> x >> y; n     if (x == y + 1) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int recpow(long long int base, long long int exp) { n   long long int result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long int)result * base) % 1000000007; n     base = n         ((long long int)(base % 1000000007) * (base % 1000000007)) % 1000000007; n     exp >>= 1; n   } n   return (result % 1000000007); n } n bool comp(const pair<long long int, long long int>& a, n           const pair<long long int, long long int>& b) { n   return ((a.first == b.first and a.second > b.second) or a.first < b.first); n } n bool chk(long long int p, vector<pair<long long int, long long int>>& v) { n   int n = v.size(); n   long long int i = 0; n   while (i < n) { n     if (p < v[i].first) return 0; n     p += v[i].second; n     i++; n   } n   return 1; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int tc = 1; n   while (tc--) { n     long long int n, m; n     cin >> n >> m; n     vector<long long int> dp(n + 1, 0), pref(n + 1, 0); n     dp[1] = 1, pref[1] = 1; n     for (long long int i = 2; i < n + 1; i++) { n       dp[i] = (dp[i] + pref[i - 1]) % m; n       for (long long int j = 1; j * j <= i; ++j) { n         long long int tmp = i / j - i / (j + 1); n         dp[i] = (dp[i] + (tmp * dp[j]) % m) % m; n       } n       for (long long int j = 2; j * j <= i; ++j) { n         long long int tmp = i / j; n         if (tmp * tmp > i) { n           dp[i] = (dp[i] + dp[tmp]) % m; n         } n       } n       pref[i] = (pref[i - 1] + dp[i]) % m; n     } n     cout << dp[n] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int N; n vector<int> adj[MAXN]; n map<int, int> dp[MAXN]; n int cid; n map<vector<int>, int> idmp; n int getid(vector<int> v) { n   if (idmp.count(v)) { n     return idmp[v]; n   } n   return idmp[v] = cid++; n } n int dfs(int x, int p); n vector<int> getv(int x, int p) { n   vector<int> v; n   for (int y : adj[x]) { n     if (y == p) { n       continue; n     } n     v.push_back(dfs(y, x)); n   } n   sort((v).begin(), (v).end()); n   return v; n } n int dfs(int x, int p) { n   if (dp[x].count(p)) { n     return dp[x][p]; n   } n   return dp[x][p] = getid(getv(x, p)); n } n int main() { n   idmp[vector<int>()] = cid++; n   scanf( %d , &N); n   for (int i = 1; i < N; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (int i = 1; i <= N; i++) { n     for (int j : adj[i]) { n       dfs(j, i); n     } n   } n   set<vector<int>> st; n   for (int i = 1; i <= N; i++) { n     if (adj[i].size() < 4) { n       st.insert(getv(i, -1)); n     } n   } n   printf( %lu n , st.size()); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int t = 1; n   while (t--) { n     int n; n     cin >> n; n     map<int, vector<int>> m; n     vector<int> v(n); n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n       m[v[i]].push_back(i); n     } n     vector<pair<int, int>> res; n     for (auto p : m) { n       vector<int> tmp = p.second; n       if (tmp.size() == 1) { n         res.push_back(make_pair(p.first, 0)); n         continue; n       } n       int d = tmp[1] - tmp[0]; n       int flag = 0; n       for (int i = 1; i < tmp.size() - 1; i++) { n         int curr = tmp[i + 1] - tmp[i]; n         if (curr != d) { n           flag = 1; n           break; n         } n       } n       if (flag == 1) { n         continue; n       } n       res.push_back(make_pair(p.first, d)); n     } n     sort((res).begin(), (res).end()); n     cout << res.size() << endl; n     for (auto p : res) { n       cout << p.first <<     << p.second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double x, y; n   Point() {} n   Point(double x, double y) : x(x), y(y) {} n   Point operator+(const Point& other) const { n     return {x + other.x, y + other.y}; n   } n   Point operator-(const Point& other) const { n     return {x - other.x, y - other.y}; n   } n   Point operator*(double c) const { return {x * c, y * c}; } n   double len() const { return sqrt(x * x + y * y); } n   bool operator<(const Point& other) const { n     if (x != other.x) { n       return x < other.x; n     } n     return y < other.y; n   } n   double sqrlen() const { return x * x + y * y; } n }; n int n, m; n vector<Point> red, blue; n double det(const Point& a, const Point& b, const Point& c) { n   return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); n } n double dist(const Point& a, const Point& b) { return (b - a).len(); } n ostream& operator<<(ostream& cout, const Point& a) { n   cout << a.x <<     << a.y; n   return cout; n } n vector<Point> convex_hull(vector<Point>& v) { n   sort(v.begin(), v.end()); n   vector<Point> ch; n   int st[2010]; n   int k = 0; n   for (int i = 0; i < v.size(); ++i) { n     while (k > 1 && det(v[st[k - 1]], v[st[k]], v[i]) <= 0) --k; n     st[++k] = i; n   } n   for (int i = 1; i <= k; ++i) { n     ch.push_back(v[st[i]]); n   } n   k = 0; n   for (int i = v.size() - 1; i >= 0; --i) { n     while (k > 1 && det(v[st[k - 1]], v[st[k]], v[i]) <= 0) --k; n     st[++k] = i; n   } n   for (int i = 2; i < k; ++i) { n     ch.push_back(v[st[i]]); n   } n   return ch; n } n Point inverse(const Point& a, const Point& b) { n   Point c = b + (a - b) * (1 / (a - b).sqrlen()); n   return c; n } n double check(int bi) { n   auto inv_blue = blue; n   inv_blue.erase(inv_blue.begin() + bi); n   for (auto& p : inv_blue) { n     p = inverse(p, blue[bi]); n   } n   auto inv_red = red; n   for (auto& p : inv_red) { n     p = inverse(p, blue[bi]); n   } n   inv_blue = convex_hull(inv_blue); n   if (inv_blue.size() == 0) { n     return -1; n   } n   double closest = 1e20; n   if (inv_blue.size() > 1) { n     for (int i = 0; i < inv_blue.size(); ++i) { n       int ii = (i + 1) % inv_blue.size(); n       int iii = (i + 2) % inv_blue.size(); n       double poly_det = det(inv_blue[i], inv_blue[ii], inv_blue[iii]); n       double my_det = det(inv_blue[i], inv_blue[ii], blue[bi]); n       if (poly_det * my_det < 0) continue; n       for (int j = 0; j < inv_red.size(); ++j) { n         if (poly_det * det(inv_blue[i], inv_blue[ii], inv_red[j]) <= 0) { n           double d = fabs(my_det) / dist(inv_blue[i], inv_blue[ii]); n           closest = min(closest, d); n           break; n         } n       } n     } n   } n   for (int i = 0; i < inv_blue.size(); ++i) { n     int li = (i - 1 + inv_blue.size()) % inv_blue.size(); n     int ii = (i + 1) % inv_blue.size(); n     for (int j = 0; j < inv_red.size(); ++j) { n       double poly_det = det(inv_blue[i], inv_red[j], inv_blue[ii]); n       double l_poly_det = det(inv_blue[i], inv_red[j], inv_blue[li]); n       double my_det = det(inv_blue[i], inv_red[j], blue[bi]); n       if (l_poly_det * poly_det >= 0) { n         if (my_det * poly_det <= 0 && my_det * l_poly_det <= 0) { n           closest = 0; n         } else { n           double d = fabs(my_det) / dist(inv_blue[i], inv_red[j]); n           closest = min(closest, d); n         } n       } n     } n   } n   if (closest == 1e20) { n     return 0; n   } else if (closest == 0) { n     return -1; n   } else { n     return 1 / (closest * 2); n   } n } n void read(int n, vector<Point>& v) { n   for (int i = 1; i <= n; ++i) { n     int x, y; n     cin >> x >> y; n     v.push_back(Point(x, y)); n   } n } n void solve() { n   cin >> n >> m; n   read(n, red); n   read(m, blue); n   double ans = 0; n   for (int i = 0; i < m; ++i) { n     double cans = check(i); n     if (cans == -1) { n       cout << -1 <<   n ; n       return; n     } else { n       ans = max(ans, cans); n     } n   } n   cout << fixed << setprecision(9) << ans <<   n ; n } n void reset() { n   red.clear(); n   blue.clear(); n } n int main() { n   int k = 1; n   for (; k <= 1; ++k) { n     reset(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, t0 = 0, t1 = 0, a[200009], h = 100000, mx = 0; n string s; n void f(char x) { n   if (x ==  0 ) n     t0++; n   else n     t1++; n } n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     f(s[i]); n     a[100000 + t1 - t0] = i; n     if (t0 == t1) mx = i + 1; n   } n   for (int i = 1; i < n; i++) { n     if (s[i - 1] ==  0 ) n       h--; n     else n       h++; n     if (a[h] != 0) mx = max(mx, a[h] - i + 1); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Flandre_Scarlet { n long long mod; n class Matrix { n  private: n   long long a[5][5]; n  n  public: n   long long n; n   Matrix() { n     memset(a, 0, sizeof(a)); n     n = 0; n   } n   Matrix(long long _n) { n     memset(a, 0, sizeof(a)); n     n = _n; n   } n   Matrix(long long _n, long long _x) { n     _x %= mod; n     n = _n; n     for (long long i = 0; i < 5; ++i) { n       for (long long j = 0; j < 5; ++j) { n         a[i][j] = _x; n       } n     } n   } n   long long* operator[](long long i) { return *(a + i); } n   void Set(long long x) { n     x %= mod; n     for (long long i = 0; i < 5; ++i) { n       for (long long j = 0; j < 5; ++j) { n         a[i][j] = x; n       } n     } n   } n   void Identity() { n     memset(a, 0, sizeof(a)); n     for (long long i = 0; i < 5; ++i) { n       a[i][i] = 1; n     } n   } n }; n Matrix operator*(Matrix x, Matrix y) { n   Matrix ans(x.n, 0); n   long long n = ans.n; n   for (long long i = 1; i <= n; ++i) { n     for (long long j = 1; j <= n; ++j) { n       for (long long k = 1; k <= n; ++k) { n         ans[i][j] += x[i][k] * y[k][j]; n         ans[i][j] %= mod; n       } n     } n   } n   return ans; n } n Matrix operator^(Matrix x, long long p) { n   Matrix ans(x.n, 1); n   ans.Identity(); n   while (p) { n     if (p & 1) ans = ans * x; n     x = x * x, p >>= 1; n   } n   return ans; n } n long long Fib(long long x) { n   Matrix Init(2, 0); n   Init[1][1] = 0, Init[1][2] = 1; n   Matrix Trans(2, 1); n   Trans[1][1] = 0; n   Matrix Ans = Init * (Trans ^ x); n   return Ans[1][1]; n } n long long l, r, k; n void R1(long long& x) { n   x = 0; n   char c = getchar(); n   long long f = 1; n   while (c <  0  or c >  9 ) f = (c ==  - ) ? -1 : 1, c = getchar(); n   while (c >=  0  and c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   x = (f == 1) ? x : -x; n } n void Input() { n   R1(mod); n   R1(l), R1(r), R1(k); n } n bool cxk(long long x) { return (r / x - (l - 1) / x) >= k; } n void Soviet() { n   long long ans = -0x3f3f3f3f; n   for (long long i = 1; i * i <= r; ++i) { n     if (cxk(i)) ans = max(ans, i); n     if (cxk(r / i)) ans = max(ans, r / i); n   } n   printf( %lld n , Fib(ans)); n } n void IsMyWife() { n   Input(); n   Soviet(); n } n }  // namespace Flandre_Scarlet n int main() { n   Flandre_Scarlet::IsMyWife(); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int grid[201][201]{0}; n int main() { n   string s; n   cin >> s; n   int x = 100; n   int y = 100; n   grid[x][y] = true; n   for (auto& it : s) { n     switch (it) { n       case  L : n         x--; n         break; n       case  R : n         x++; n         break; n       case  U : n         y--; n         break; n       case  D : n         y++; n     } n     if (grid[x][y] || (it !=  L  && grid[x + 1][y]) || n         (it !=  R  && grid[x - 1][y]) || (it !=  U  && grid[x][y + 1]) || n         (it !=  D  && grid[x][y - 1])) { n       cout <<  BUG  << endl; n       return 0; n     } n     grid[x][y] = true; n   } n   cout <<  OK  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, _m, ans = 0; n map<string, int> m; n string s[1010], u[1010], v[1010]; n int main() { n   cin >> n >> _m; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   sort(s + 1, s + n + 1); n   for (int i = 1; i <= n; i++) m[s[i]] = i - 1; n   for (int i = 1; i <= _m; i++) cin >> u[i] >> v[i]; n   for (int i = 1; i <= (1 << n); i++) { n     bool flag = 1; n     for (int j = 1; j <= _m; j++) n       if (((i - 1) & (1 << m[u[j]])) && ((i - 1) & (1 << m[v[j]]))) flag = 0; n     if (flag && __builtin_popcount(ans) < __builtin_popcount(i - 1)) n       ans = i - 1; n   } n   cout << __builtin_popcount(ans) <<   n ; n   for (int i = 1; i <= n; i++) n     if (ans & (1 << (i - 1))) cout << s[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const int N = 1e5 + 1; n vector<int> co[N]; n long long ans = 0; n void dfs(int x, int p) { n   ans += (int)co[x].size() * ((int)co[x].size() - 1) / 2; n   for (auto y : co[x]) { n     if (y == p) continue; n     dfs(y, x); n   } n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     co[a].push_back(b); n     co[b].push_back(a); n   } n   dfs(1, 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<double> first, second; n double eps = 0.0000001; n double ai; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ai; n     first.push_back(ai); n   } n   for (int i = 0; i < n; i++) { n     cin >> ai; n     second.push_back(ai); n   } n   double s1 = 0; n   double s2 = 0; n   vector<double> a1, a2; n   for (int i = 0; i < n; i++) { n     double x = first[i]; n     double y = second[i]; n     double a = 1; n     double b = -(s1 + x + y - s2); n     double c = -(x * s2 + y * s2 - x); n     double disc = b * b - 4 * a * c; n     if (disc >= -eps && disc < 0) { n       disc = 0; n     } n     disc = sqrt(disc); n     double k1 = (-b + disc) / 2; n     double k2 = (-b - disc) / 2; n     double nn = 0; n     if (k1 <= k2) swap(k1, k2); n     if (k1 >= -eps && k1 <= x + y + eps) { n       s2 += max(k1, nn); n       s1 += max(nn, x + y - k1); n       a2.push_back(max(k1, nn)); n       a1.push_back(max(nn, x + y - k1)); n     } else { n       s2 += max(k2, nn); n       s1 += max(nn, x + y - k2); n       a2.push_back(max(k2, nn)); n       a1.push_back(max(nn, x + y - k2)); n     } n   } n   cout.precision(25); n   for (int i = 0; i < n; i++) { n     cout << a1[i] <<    ; n   } n   cout << endl; n   for (int i = 0; i < n; i++) { n     cout << a2[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, c[8005][8005], fal[8005], dp[8005], len[8005]; n char s[8005]; n void Init(char *s, int n, int *c) { n   fal[1] = 0, fal[2] = 1; n   for (int i = 2; i <= n; i++) { n     int j = fal[i]; n     while (j && s[i] != s[j]) j = fal[j]; n     fal[i + 1] = j + 1; n   } n   for (int i = 1; i <= n; i++) { n     int t = 1, k = i - fal[i + 1] + 1; n     if (i % k == 0) t = i / k; n     c[i] = i / t + len[t]; n   } n } n int main() { n   scanf( %s , s + 1), n = strlen(s + 1); n   for (int i = 1; i <= n; i++) len[i] = len[i / 10] + 1; n   for (int i = 1; i <= n; i++) Init(s + i - 1, n - i + 1, c[i] + i - 1); n   for (int i = 1; i <= n; i++) { n     dp[i] = n + 1; n     for (int j = 1; j <= i; j++) dp[i] = min(dp[i], dp[j - 1] + c[j][i]); n   } n   printf( %d n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const long long MOD = 1e9 + 7; n long long h[26][N], p[N]; n int a[30], b[30]; n char s[N + 10]; n long long get(int i, int l, int r) { n   return (h[i][r] - h[i][l - 1] * p[r - l + 1]) % MOD; n } n int main() { n   int n, m, x, y, len; n   while (scanf( %d%d , &n, &m) != EOF) { n     p[0] = 1; n     for (int i = 1; i <= n; i++) p[i] = (p[i - 1] * 2) % MOD; n     scanf(  %s , s + 1); n     for (int i = 0; i < 26; i++) n       for (int j = 1; j <= n; j++) n         h[i][j] = (h[i][j - 1] * 2 + (s[j] ==  a  + i)) % MOD; n     while (m--) { n       scanf( %d%d%d , &x, &y, &len); n       for (int i = 0; i < 26; i++) { n         a[i] = (get(i, x, x + len - 1) + MOD) % MOD; n         b[i] = (get(i, y, y + len - 1) + MOD) % MOD; n       } n       sort(a, a + 26); n       sort(b, b + 26); n       bool flag = true; n       for (int i = 0; i < 26; i++) n         if (a[i] != b[i]) { n           flag = false; n           break; n         } n       printf( %s n , flag ?  YES  :  NO ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, q[5000]; n   cin >> n >> a >> b; n   for (int i = 0; i < n; i++) cin >> q[i]; n   sort(q, q + n); n   cout << q[b] - q[b - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_NM = 1000 * 1000; n int dl; n char S[MAX_NM]; n void dodaj(char l, int x) { n   while (x--) { n     dl++; n     S[dl] = l; n   } n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   if (k > 4 * n * m - 2 * n - 2 * m) { n     cout <<  NO n ; n     return 0; n   } n   for (int i = 1; i < m; i++) { n     dodaj( D , n - 1); n     dodaj( U , n - 1); n     dodaj( R , 1); n   } n   dodaj( D , n - 1); n   for (int i = n; i > 1; i--) { n     dodaj( L , m - 1); n     dodaj( R , m - 1); n     dodaj( U , 1); n   } n   dodaj( L , m - 1); n   vector<pair<char, int> > w; n   char l = S[1]; n   int a = 1; n   for (int i = 2; i <= k; i++) { n     if (S[i] != S[i - 1]) { n       w.push_back(make_pair(l, a)); n       l = S[i]; n       a = 1; n     } else { n       a++; n     } n   } n   w.push_back(make_pair(l, a)); n   cout <<  YES  << endl; n   cout << w.size() << endl; n   for (pair<char, int> p : w) { n     cout << p.second <<     << p.first << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const long long mod1 = 998244353; n const long long mod = 1e9 + 7; n long long mod_mul(long long a, long long b) { n   a = a % mod; n   b = b % mod; n   return (((a * b) % mod) + mod) % mod; n } n long long inv(long long i) { n   if (i == 1) return 1; n   return (mod - ((mod / i) * inv(mod % i)) % mod) % mod; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long pwr(long long a, long long b) { n   a %= mod; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long findMinNumber(long long n) { n   long long count = 0, ans = 1; n   while (n % 2 == 0) { n     count++; n     n /= 2; n   } n   if (count % 2) ans *= 2; n   for (int i = 3; i <= sqrt(n); i += 2) { n     count = 0; n     while (n % i == 0) { n       count++; n       n /= i; n     } n     if (count % 2) ans *= i; n   } n   if (n > 2) ans *= n; n   return ans; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k, i, j; n     cin >> n >> k; n     map<long long, long long> m; n     vector<long long> ans; n     vector<pair<long long, long long> > v; n     long long a[k][2]; n     for (i = 0; i < k; i++) { n       cin >> a[i][0] >> a[i][1]; n       v.push_back({a[i][0], a[i][1]}); n       m[a[i][0]]++; n       m[a[i][1]]++; n     } n     for (i = 1; i < 2 * n + 1; i++) n       if (m[i] == 0) ans.push_back(i); n     for (i = 0; i < ans.size() / 2; i++) { n       v.push_back({ans[i], ans[ans.size() / 2 + i]}); n     } n     long long cnt = 0; n     sort((v).begin(), (v).end()); n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         if (i != j) { n           long long x = (v[i].second - v[j].first) * (v[i].first - v[j].first); n           long long y = n               (v[i].second - v[j].second) * (v[i].first - v[j].second); n           if (x * y < 0) { n             cnt++; n           } n         } n       } n     } n     cout << cnt / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, f[50010]; n struct node { n   int f[12], g[12], size; n } a[50010]; n struct cmp { n   bool operator()(node a, node b) { n     for (int i = 1; i <= m; i++) n       if (a.f[i] > b.g[i]) return 0; n     return 1; n   } n }; n multiset<node, cmp> q; n inline void up(node &a, node b) { n   for (int i = 1; i <= m; i++) n     a.f[i] = max(a.f[i], b.f[i]), a.g[i] = min(a.g[i], b.g[i]); n   a.size += b.size; n } n inline bool operator==(node a, node b) { n   int t = 0; n   for (int i = 1; i <= m; i++) t |= (a.f[i] > b.g[i]) | (2 * (a.g[i] < b.f[i])); n   return t == 3; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) f[i] = i; n   node p1, p2; n   for (int i = 1; i <= m; i++) p1.f[i] = p1.g[i] = 0; n   for (int i = 1; i <= m; i++) p2.f[i] = p2.g[i] = 1999999999; n   q.insert(p1); n   q.insert(p2); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       scanf( %d , &a[i].f[j]), a[i].g[j] = a[i].f[j], a[i].size = 1; n   for (int i = 1; i <= n; i++) { n     set<node>::iterator it = q.lower_bound(a[i]), it2; n     while (*it == a[i]) it2 = it, it2--, up(a[i], *it), q.erase(it), it = it2; n     it = q.lower_bound(a[i]); n     while (*it == a[i]) it2 = it, it2++, up(a[i], *it), q.erase(it), it = it2; n     q.insert(a[i]); n     printf( %d n , (--(--q.end()))->size); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n struct State { n   int delta; n   map<int, int> pos; n   map<int, int> neg; n   set<pair<int, pair<int, int>>> matches; n   void init() { n     delta = 0; n     pos.clear(); n     neg.clear(); n     matches.clear(); n   } n   int findmatchforpos(int x) { n     auto it = neg.lower_bound(x + delta); n     return it == neg.end() ? INT_MAX : it->first - delta; n   } n   int findmatchforneg(int y) { n     auto it = pos.upper_bound(y - delta); n     return it == pos.begin() ? INT_MIN : prev(it)->first + delta; n   } n   void erasepos(int x) { n     assert(pos.erase(x - delta)); n     int y = findmatchforpos(x); n     if (y != INT_MAX) { n       int xx = findmatchforneg(y); n       if (xx == INT_MIN || xx <= x) n         assert(matches.erase( n             make_pair(y - x + 2 * delta, make_pair(x - delta, y + delta)))); n       if (xx != INT_MIN && xx <= x && findmatchforpos(xx) == y) n         matches.insert( n             make_pair(y - xx + 2 * delta, make_pair(xx - delta, y + delta))); n     } n   } n   void eraseneg(int y) { n     assert(neg.erase(y + delta)); n     int x = findmatchforneg(y); n     if (x != INT_MIN) { n       int yy = findmatchforpos(x); n       if (yy == INT_MAX || yy >= y) n         assert(matches.erase( n             make_pair(y - x + 2 * delta, make_pair(x - delta, y + delta)))); n       if (yy != INT_MAX && yy >= y && findmatchforneg(yy) == x) n         matches.insert( n             make_pair(yy - x + 2 * delta, make_pair(x - delta, yy + delta))); n     } n   } n   void insertpos(int x, int val) { n     if (pos.count(x - delta)) { n       pos[x - delta] += val; n       return; n     } n     int y = findmatchforpos(x); n     if (y != INT_MAX) { n       int xx = findmatchforneg(y); n       if (xx != INT_MIN && xx < x && findmatchforpos(xx) == y) n         assert(matches.erase( n             make_pair(y - xx + 2 * delta, make_pair(xx - delta, y + delta)))); n       if (xx == INT_MIN || xx < x) n         matches.insert( n             make_pair(y - x + 2 * delta, make_pair(x - delta, y + delta))); n     } n     pos[x - delta] = val; n   } n   void insertneg(int y, int val) { n     if (neg.count(y + delta)) { n       neg[y + delta] += val; n       return; n     } n     int x = findmatchforneg(y); n     if (x != INT_MIN) { n       int yy = findmatchforpos(x); n       if (yy != INT_MAX && yy > y && findmatchforneg(yy) == x) n         assert(matches.erase( n             make_pair(yy - x + 2 * delta, make_pair(x - delta, yy + delta)))); n       if (yy == INT_MAX || yy > y) n         matches.insert( n             make_pair(y - x + 2 * delta, make_pair(x - delta, y + delta))); n     } n     neg[y + delta] = val; n   } n   void grow(int by) { n     while (((int)(matches).size()) != 0 && n            matches.begin()->first - 2 * delta < 2 * by) { n       int x = matches.begin()->second.first + delta, n           y = matches.begin()->second.second - delta; n       assert(pos.count(x - delta)); n       int xval = pos[x - delta]; n       assert(neg.count(y + delta)); n       int yval = neg[y + delta]; n       if (xval < yval) { n         neg[y + delta] -= xval; n         erasepos(x); n       } else if (xval > yval) { n         pos[x - delta] -= yval; n         eraseneg(y); n       } else { n         erasepos(x); n         eraseneg(y); n       } n     } n     delta += by; n   } n   void print() { n     printf(  tneg: ); n     for (auto it = neg.begin(); it != neg.end(); ++it) n       printf(  %d=%d , it->first - delta, it->second); n     puts(  ); n     printf(  tpos: ); n     for (auto it = pos.begin(); it != pos.end(); ++it) n       printf(  %d=%d , it->first + delta, it->second); n     puts(  ); n     printf(  tmatches: ); n     for (auto it = matches.begin(); it != matches.end(); ++it) n       printf(  %d:%d-%d , it->first - 2 * delta, it->second.first + delta, n              it->second.second - delta); n     puts(  ); n   } n }; n const int MAXN = 100000; n int n; n vector<pair<int, int>> e[MAXN]; n int nq; n vector<pair<int, int>> obs[MAXN]; n State state[MAXN]; n int merge(int a, int b) { n   int asz = ((int)(state[a].pos).size()) + ((int)(state[a].neg).size()), n       bsz = ((int)(state[b].pos).size()) + ((int)(state[b].neg).size()); n   if (asz < bsz) swap(a, b), swap(asz, bsz); n   for (auto it = state[b].pos.begin(); it != state[b].pos.end(); ++it) n     state[a].insertpos(it->first + state[b].delta, it->second); n   for (auto it = state[b].neg.begin(); it != state[b].neg.end(); ++it) n     state[a].insertneg(it->first - state[b].delta, it->second); n   return a; n } n int dfs(int at, int par, int parlen) { n   int ret = at; n   state[ret].init(); n   for (int i = (0); i < (((int)(e[at]).size())); ++i) { n     int to = e[at][i].first; n     if (to == par) continue; n     int cur = dfs(to, at, e[at][i].second); n     ret = merge(ret, cur); n   } n   vector<pair<int, int>> extra; n   for (int i = (0); i < (((int)(obs[at]).size())); ++i) { n     int t = obs[at][i].first, cnt = obs[at][i].second, rem = cnt; n     { n       int cur = (state[ret].pos.count(t - state[ret].delta) n                      ? state[ret].pos[t - state[ret].delta] n                      : 0) + n                 (state[ret].pos.count(t - state[ret].delta - 1) n                      ? state[ret].pos[t - state[ret].delta - 1] n                      : 0); n       rem = min(rem, max(0, cnt - cur)); n     } n     { n       int cur = (state[ret].neg.count(t + state[ret].delta) n                      ? state[ret].neg[t + state[ret].delta] n                      : 0) + n                 (state[ret].neg.count(t + state[ret].delta + 1) n                      ? state[ret].neg[t + state[ret].delta + 1] n                      : 0); n       rem = min(rem, max(0, cnt - cur)); n     } n     if (rem > 0) extra.push_back(make_pair(t, rem)); n   } n   state[ret].grow(1); n   for (int i = (0); i < (((int)(extra).size())); ++i) { n     int t = extra[i].first, cnt = extra[i].second; n     state[ret].insertneg(t - 1, cnt); n     state[ret].insertpos(t + 1, cnt); n   } n   state[ret].grow(parlen - 1); n   return ret; n } n int solve() { n   for (int i = (0); i < (n); ++i) sort(obs[i].begin(), obs[i].end()); n   int res = dfs(0, -1, 1); n   map<int, int> all; n   for (auto it = state[res].pos.begin(); it != state[res].pos.end(); ++it) n     all[2 * (it->first + state[res].delta) + 0] -= it->second; n   for (auto it = state[res].neg.begin(); it != state[res].neg.end(); ++it) n     all[2 * (it->first - state[res].delta) + 1] += it->second; n   int ret = 0, cur = 0; n   for (auto it = all.begin(); it != all.end(); ++it) { n     cur += it->second; n     ret = max(ret, cur); n   } n   return ret; n } n void run() { n   scanf( %d , &n); n   for (int i = (0); i < (n - 1); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     --a, --b, c *= 2; n     e[a].push_back(make_pair(b, c)); n     e[b].push_back(make_pair(a, c)); n   } n   scanf( %d , &nq); n   for (int i = (0); i < (nq); ++i) { n     int t, cnt, idx; n     scanf( %d%d%d , &t, &cnt, &idx); n     t *= 2; n     --idx; n     obs[idx].push_back(make_pair(t, cnt)); n   } n   printf( %d n , solve()); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x, t; n   getline(cin, x); n   string temp =   ; n   int index = -1; n   for (int i = 0; i < x.size(); ++i) { n     if (x[i] ==  = ) index = i; n   } n   index += 2; n   for (int i = index; i < x.size(); ++i) temp.push_back(x[i]); n   int n = stoi(temp.c_str()); n   int pos = 0; n   int neg = 0; n   int i = 0; n   if (x[0] ==  ? ) { n     ++pos; n     ++i; n   } else { n     ++neg; n     i += 2; n   } n   for (int i = 0; i < x.size(); ++i) n     if (x[i] ==  + ) n       ++pos; n     else if (x[i] ==  - ) n       ++neg; n   vector<int> ne(neg, 1); n   vector<int> po(pos, 1); n   int sum = (pos * 1) - neg; n   int j = 0, z = 0; n   bool in = false; n   while (1) { n     if (sum == n) break; n     if (sum > n) { n       if (z == neg) { n         cout <<  Impossible  << endl; n         return 0; n       } n       if (ne[z] == n) { n         ++z; n         continue; n       } n       in = true; n       ++ne[z]; n       sum -= 1; n     } else if (sum < n) { n       if (j == pos) { n         cout <<  Impossible  << endl; n         return 0; n       } n       if (po[j] == n) { n         ++j; n         continue; n       } n       in = true; n       ++po[j]; n       sum += 1; n     } n   } n   cout <<  Possible  << endl; n   j = 0, z = 0; n   string last =   ; n   for (int i = 0; i < x.size(); ++i) { n     if (x[i] ==  -  || x[i] ==  +  || i == 0) { n       if (i > 0) { n         if (x[i] ==  - ) { n           last.push_back(x[i]); n           stringstream ss; n           ss << ne[z++]; n           last += ss.str(); n           ; n         } else { n           last.push_back(x[i]); n           stringstream ss; n           ss << po[j++]; n           last += ss.str(); n         } n       } n       if (i == 0) { n         if (x[i] ==  - ) { n           last.push_back(x[i]); n           stringstream ss; n           ss << ne[z++]; n           last += ss.str(); n           ; n         } else { n           stringstream ss; n           ss << po[j++]; n           last += ss.str(); n         } n       } n     } else if (x[i] !=  ? ) n       last.push_back(x[i]); n   } n   cout << last << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void setIO(string NAME) {} n inline long long gI() { n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   long long flag = 1, p = 0; n   if (c ==  - ) flag = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) p = p * 10 + (c -  0 ), c = getchar(); n   return p * flag; n } n int gs(char* C) { n   char c = getchar(); n   while (c ==     || c ==   n ) c = getchar(); n   int l = 0; n   while (c !=     && c !=   n ) C[l++] = c, c = getchar(); n   C[l] = 0; n   return l; n } n template <class T> n void debug(const T a, const int& n) { n   for (int i = 0; i < n; ++i) printf( %d%c , a[i], (i == n - 1) ?   n  :    ); n } n const int inf = ~0U >> 1, maxn = 100000 + 10; n int n, m; n long long a[maxn]; n vector<long long> b[maxn]; n int main() { n   setIO( test ); n   n = gI(), m = gI(); n   long long ans = 0; n   for (int i = 0; i < m; ++i) { n     a[i] = gI(); n     if (i) ans += abs(a[i] - a[i - 1]); n   } n   long long MAX = 0; n   for (int i = 0; i < m; ++i) { n     if (i && a[i - 1] != a[i]) b[a[i]].push_back(a[i - 1]); n     if (i < m - 1 && a[i] != a[i + 1]) b[a[i]].push_back(a[i + 1]); n   } n   for (int i = 1; i <= n; ++i) n     if (b[i].size()) { n       sort(b[i].begin(), b[i].end()); n       int sz = b[i].size(); n       int m = (sz - 1) / 2; n       long long origin = 0, now = 0; n       for (int j = 0; j < sz; ++j) { n         origin += abs((long long)b[i][j] - i); n         now += abs(b[i][j] - b[i][m]); n       } n       MAX = max(MAX, origin - now); n     } n   ans -= MAX; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int dx[] = {0, -1, 0, 1, -1, 1, 1, -1, -2, -2, 2, 2, -1, -1, 1, 1}; n const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1, -1, 1, -1, 1, -2, 2, -2, 2}; n using namespace std; n int main(int argc, char const *argv[]) { n   int n, m; n   scanf( %d %d , &n, &m); n   while (n) { n     printf( 0 ); n     n--; n     if (!n) break; n     printf( 1 ); n     n--; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ACfixed { n   static const int ASZ = 10; n   struct node { n     array<int, ASZ> to; n     int link = -1; n     int tlink = -1; n     int id = -1; n   }; n   vector<node> d = {{}}; n   int add(string s, int id) { n     int v = 0; n     for (auto C : s) { n       int c = C -  0 ; n       if (!d[v].to[c]) d[v].to[c] = d.size(), d.emplace_back(); n       v = d[v].to[c]; n     } n     d[v].id = id; n     return v; n   } n   void init() { n     d[0].link = -1; n     queue<int> q; n     q.push(0); n     while (q.size()) { n       int v = q.front(); n       q.pop(); n       for (int c = 0; c < ASZ; c++) { n         int u = d[v].to[c]; n         if (!u) continue; n         d[u].link = d[v].link == -1 ? 0 : d[d[v].link].to[c]; n         d[u].tlink = (d[d[u].link].id == -1) ? d[d[u].link].tlink : d[u].link; n         q.push(u); n       } n       if (v) n         for (int c = 0; c < ASZ; c++) n           if (!d[v].to[c]) d[v].to[c] = d[d[v].link].to[c]; n     } n   } n   vector<vector<int>> getAll(string &word) { n     vector<vector<int>> res(d.size(), vector<int>()); n     int v = 0; n     for (int i = 0; i < word.length(); i++) { n       v = d[v].to[word[i] -  0 ]; n       int cur = v; n       while (cur >= 0) { n         if (d[cur].id != -1) { n           res[d[cur].id].push_back(i); n         } n         cur = d[cur].tlink; n       } n     } n     return res; n   } n }; n vector<string> arr; n int n, x; n string s; n string curstr =   ; n ACfixed ac; n void gen(int cur) { n   if (cur == 0) { n     arr.push_back(curstr); n     return; n   } n   for (int i = 1; i <= 9; i++) { n     if (i > cur) continue; n     curstr.push_back( 0  + i); n     int csum = 0; n     bool work = true; n     for (int j = curstr.size() - 1; j >= 0; j--) { n       csum += curstr[j] -  0 ; n       if (csum != x && x % csum == 0) { n         work = false; n         break; n       } n     } n     if (work) gen(cur - i); n     curstr.pop_back(); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> s >> x; n   n = s.length(); n   gen(x); n   for (int i = 0; i < arr.size(); i++) ac.add(arr[i], i); n   ac.init(); n   vector<int> dp(ac.d.size(), 1000000009); n   dp[0] = 0; n   for (auto ch : s) { n     vector<int> nextdp(ac.d.size(), 1000000009); n     for (int state = 0; state < ac.d.size(); state++) { n       if (dp[state] == 1000000009) continue; n       nextdp[state] = min(nextdp[state], dp[state] + 1); n       int trans = ac.d[state].to[ch -  0 ]; n       if (ac.d[trans].id == -1) nextdp[trans] = min(nextdp[trans], dp[state]); n     } n     swap(dp, nextdp); n   } n   cout << *min_element(dp.begin(), dp.end()) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 5; n const int INF = 0x7fffffff; n bool p(long long a) { n   for (long long i = 2; i <= sqrt(a); i++) { n     if (a % i == 0) return 0; n   } n   return 1; n } n int main() { n   int n; n   cin >> n; n   int c = n; n   while (!p(c)) { n     c++; n   } n   cout << c <<   n ; n   for (int i = 1; i < n; i++) cout << i <<     << i + 1 <<   n ; n   cout << 1 <<     << n <<   n ; n   for (int i = 1; i <= c - n; i++) cout << i <<     << n - i <<   n ; n } n