#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y) { n   long long res = 1; n   x = x % 1000000007; n   while (y > 0) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res % 1000000007; n } n long long inv(long long n) { return power(n, 1000000007 - 2) % 1000000007; } n long long isprime(long long n) { n   if (n < 2) return 0; n   long long i; n   for (i = 2; i * i <= n; i++) n     if (n % i == 0) return 0; n   return 1; n } n void solve() { n   long long ans = 1e18; n   string s; n   long long n; n   cin >> n >> s; n   for (char ch =  a ; ch <=  z ; ch++) { n     long long i = 0, j = n - 1; n     long long moves = 0; n     long long ok = 1; n     while (i < j) { n       if (s[i] == s[j]) { n         i++, j--; n       } else if (s[i] == ch) { n         i++; n         moves++; n       } else if (s[j] == ch) { n         j--; n         moves++; n       } else { n         ok = 0; n         break; n       } n     } n     if (ok) { n       ans = min(ans, moves); n     } n   } n   if (ans > n) ans = -1; n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   long long tc = 0; n   cin >> t; n   while (t--) { n     tc++; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 3e6 + 44; n const int X[] = {1, 0, -1, 0, 1, -1, 1, -1}; n const int Y[] = {0, 1, 0, -1, 1, 1, -1, -1}; n const int mod = 998244353; n const int INF = 1e9; n ll ans[N], add[N]; n vector<int> g[N]; n vector<pair<int, int> > query[N]; n void dfs(int v = 0, int p = -1, int h = 0, ll sum = 0) { n   for (auto pairr : query[v]) { n     int d = pairr.first, x = pairr.second; n     add[h] += x; n     if (h + d + 1 < N) add[h + d + 1] -= x; n   } n   sum += add[h]; n   ans[v] = sum; n   for (int to : g[v]) n     if (to != p) { n       dfs(to, v, h + 1, sum); n     } n   for (auto pairr : query[v]) { n     int d = pairr.first, x = pairr.second; n     add[h] -= x; n     if (h + d + 1 < N) add[h + d + 1] += x; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   ; n   int n; n   cin >> n; n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     int v, d, x; n     cin >> v >> d >> x; n     v--; n     query[v].push_back({d, x}); n   } n   dfs(); n   for (int i = 0; i < n; ++i) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n int a[25]; n LL dp[25][5], l, r; n LL dfs(int pos, int cnt, bool zero, bool limit) { n   if (pos == -1) return 1; n   if (!limit && !zero && dp[pos][cnt] != -1) return dp[pos][cnt]; n   int up = limit ? a[pos] : 9; n   if (cnt == 3) up = 0; n   LL ans = 0; n   for (int i = 0; i <= up; ++i) n     ans += dfs(pos - 1, cnt + (i == 0 ? 0 : 1), zero && i == 0, n                limit && i == a[pos]); n   if (!limit && !zero) dp[pos][cnt] = ans; n   return ans; n } n LL solve(LL x) { n   int pos = 0; n   while (x) { n     a[pos++] = x % 10; n     x /= 10; n   } n   return dfs(pos - 1, 0, true, true); n } n int main() { n   int T; n   LL l, r; n   scanf( %d , &T); n   while (T--) { n     memset(dp, -1, sizeof dp); n     scanf( %I64d%I64d , &l, &r); n     printf( %I64d n , solve(r) - solve(l - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(0.0) * 2.0; n const long double eps = 1e-10; n const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1}, n                         {-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; n template <class T> n inline T abs1(T a) { n   return a < 0 ? -a : a; n } n template <typename t, typename t1> n t min1(t a, t1 b) { n   return a < b ? a : b; n } n template <typename t, typename... arg> n t min1(t a, arg... arr) { n   return min1(a, min1(arr...)); n } n template <typename t, typename t1> n t max1(t a, t1 b) { n   return a > b ? a : b; n } n template <typename t, typename... arg> n t max1(t a, arg... arr) { n   return max1(a, max1(arr...)); n } n inline int jud(double a, double b) { n   if (abs(a) < eps && abs(b) < eps) n     return 0; n   else if (abs1(a - b) / abs1(a) < eps) n     return 0; n   if (a < b) return -1; n   return 1; n } n template <typename t> n inline int jud(t a, t b) { n   if (a < b) return -1; n   if (a == b) return 0; n   return 1; n } n template <typename it, typename t1> n inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { n   int be = 0, en = na - 1; n   if (*a <= *(a + na - 1)) { n     if (f_lb == 0) n       while (be < en) { n         int mid = (be + en + 1) / 2; n         if (jud(*(a + mid), val) != 1) n           be = mid; n         else n           en = mid - 1; n       } n     else n       while (be < en) { n         int mid = (be + en) / 2; n         if (jud(*(a + mid), val) != -1) n           en = mid; n         else n           be = mid + 1; n       } n     if (f_small && jud(*(a + be), val) == 1) be--; n     if (!f_small && jud(*(a + be), val) == -1) be++; n   } else { n     if (f_lb) n       while (be < en) { n         int mid = (be + en + 1) / 2; n         if (jud(*(a + mid), val) != -1) n           be = mid; n         else n           en = mid - 1; n       } n     else n       while (be < en) { n         int mid = (be + en) / 2; n         if (jud(*(a + mid), val) != 1) n           en = mid; n         else n           be = mid + 1; n       } n     if (!f_small && jud(*(a + be), val) == -1) be--; n     if (f_small && jud(*(a + be), val) == 1) be++; n   } n   return be; n } n template <class T> n inline T lowb(T num) { n   return num & (-num); n } n inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); } n inline int bitnum(int nValue) { return __builtin_popcount(nValue); } n inline int bitnum(unsigned long long nValue) { n   return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); n } n inline int bitnum(long long nValue) { n   return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); n } n inline int bitmaxl(unsigned int a) { n   if (a == 0) return 0; n   return 32 - __builtin_clz(a); n } n inline int bitmaxl(int a) { n   if (a == 0) return 0; n   return 32 - __builtin_clz(a); n } n inline int bitmaxl(unsigned long long a) { n   int temp = a >> 32; n   if (temp) return 32 - __builtin_clz(temp) + 32; n   return bitmaxl(int(a)); n } n inline int bitmaxl(long long a) { n   int temp = a >> 32; n   if (temp) return 32 - __builtin_clz(temp) + 32; n   return bitmaxl(int(a)); n } n long long pow(long long n, long long m, long long mod = 0) { n   if (m < 0) return 0; n   long long ans = 1; n   long long k = n; n   while (m) { n     if (m & 1) { n       ans *= k; n       if (mod) ans %= mod; n     } n     k *= k; n     if (mod) k %= mod; n     m >>= 1; n   } n   return ans; n } n template <class t1, class t2> n inline void add(t1 &a, t2 b, int mod = -1) { n   if (mod == -1) mod = 1000000007; n   a += b; n   while (a >= mod) a -= mod; n   while (a < 0) a += mod; n } n template <class t> n void output1(t arr) { n   for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] <<    ; n   cerr << endl; n } n template <class t> n void output2(t arr) { n   for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]); n } n const int maxn = 100100; n struct edge { n   int to, nxt, val; n } e[maxn * 2]; n int head[maxn], le; n int fa[maxn], anc[maxn], sz[maxn], hv[maxn], deep[maxn], loc[maxn]; n int orig[maxn], lorig; n int n, nq; n int f[maxn]; n pair<int, int> st[2][maxn]; n int lst[2]; n int ans[maxn]; n struct node { n   int a, b, cate; n   int yul; n   bool operator<(const node &a) const { n     if (yul == a.yul) return cate < a.cate; n     return yul < a.yul; n   } n } arr[maxn * 2]; n int larr; n struct segment_node { n   int be, en; n   int sumv, lef, rig; n   bool flag; n }; n struct segment_tree { n   int l; n   segment_node tree[maxn * 4]; n   inline int gleft(int no) { return no << 1; } n   inline int gright(int no) { return (no << 1) + 1; } n   inline int gfa(int no) { return no >> 1; } n   inline segment_tree() { l = 0; } n   void build(int no, int l, int r, int orig = 0, int *a = NULL) { n     if (l > r) r = l; n     if (l == r) { n       tree[no].be = tree[no].en = l; n       tree[no].sumv = 0; n       tree[no].lef = tree[no].rig = 0; n       tree[no].flag = 0; n       return; n     } n     tree[no].be = l; n     tree[no].en = r; n     int mid = (l + r) / 2; n     build(gleft(no), l, mid, orig, a); n     build(gright(no), mid + 1, r, orig, a); n     tree[no].sumv = 0; n     tree[no].lef = tree[no].rig = 0; n   } n   inline void pushup(segment_node *no, segment_node *nl, segment_node *nr) { n     if (nl->flag && nr->flag) { n       no->lef = no->rig = nr->lef + nl->rig; n       no->sumv = 0; n       no->flag = 1; n       return; n     } n     if (nl->flag && nl->lef == 0) { n       memcpy(no, nr, sizeof(segment_node)); n       return; n     } n     if (nr->flag && nr->lef == 0) { n       memcpy(no, nl, sizeof(segment_node)); n       return; n     } n     no->flag = 0; n     if (nl->flag) { n       no->lef = nr->lef + nl->rig; n       no->rig = nr->rig; n       no->sumv = nr->sumv; n       return; n     } n     if (nr->flag) { n       no->lef = nl->lef; n       no->rig = nl->rig + nr->lef; n       no->sumv = nl->sumv; n       return; n     } n     no->lef = nl->lef; n     no->rig = nr->rig; n     no->sumv = nl->sumv + nr->sumv + f[nl->rig + nr->lef]; n   } n   void down(int loc, int no) { n     if (loc <= tree[no].be && loc >= tree[no].en) { n       tree[no].rig = tree[no].lef = 1; n       tree[no].flag = 1; n       tree[no].sumv = 0; n       return; n     } n     int mid = (tree[no].be + tree[no].en) >> 1; n     if (loc <= mid) n       down(loc, gleft(no)); n     else n       down(loc, gright(no)); n     pushup(tree + no, tree + gleft(no), tree + gright(no)); n   } n   void getsum(int l, int r, int no, int pp1) { n     if (l > r) return; n     int pp2 = pp1 + 100; n     if (l <= tree[no].be && r >= tree[no].en) { n       pushup(tree + pp2, tree + pp1, tree + no); n       tree[pp1] = tree[pp2]; n       return; n     } n     int mid = (tree[no].be + tree[no].en) >> 1; n     if (r >= tree[no].be && l <= mid) getsum(l, r, gleft(no), pp1); n     if (r >= mid + 1 && l <= tree[no].en) getsum(l, r, gright(no), pp1); n   } n } sgt; n void dfs(int no) { n   sz[no] = 1; n   hv[no] = -1; n   for (int i = head[no]; i != -1; i = e[i].nxt) n     if (e[i].to != fa[no]) { n       fa[e[i].to] = no; n       deep[e[i].to] = deep[no] + 1; n       dfs(e[i].to); n       sz[no] += sz[e[i].to]; n       if (hv[no] == -1 || sz[hv[no]] < sz[e[i].to]) hv[no] = e[i].to; n     } n } n void dfs1(int no) { n   orig[lorig] = no; n   loc[no] = lorig++; n   if (hv[no] != -1) { n     anc[hv[no]] = anc[no]; n     dfs1(hv[no]); n   } n   for (int i = head[no]; i != -1; i = e[i].nxt) n     if (e[i].to != fa[no] && e[i].to != hv[no]) { n       anc[e[i].to] = e[i].to; n       dfs1(e[i].to); n     } n } n void addedge(int a, int b, int c) { n   e[le].to = b; n   e[le].nxt = head[a]; n   e[le].val = c; n   head[a] = le++; n } n int main() { n   ios_base::sync_with_stdio(0); n   memset(head, -1, sizeof(head)); n   scanf( %d%d , &n, &nq); n   for (int i = 1; i < n; i++) scanf( %d , f + i); n   for (int i = 0; i < n - 1; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     addedge(a, b, c); n     addedge(b, a, c); n     arr[i].a = a; n     arr[i].b = b; n     arr[i].yul = -c; n     arr[i].cate = -(i + 1); n   } n   sgt.build(1, 0, n); n   dfs(0); n   dfs1(0); n   for (int i = 0; i < nq; i++) { n     scanf( %d%d%d , &arr[i + n - 1].a, &arr[i + n - 1].b, &arr[i + n - 1].yul); n     arr[i + n - 1].cate = i; n     arr[i + n - 1].a--; n     arr[i + n - 1].b--; n     arr[i + n - 1].yul = -arr[i + n - 1].yul; n   } n   sort(arr, arr + nq + n - 1); n   for (int i = 0; i < nq + n - 1; i++) { n     if (arr[i].cate < 0) { n       int no = arr[i].a; n       if (deep[arr[i].b] > deep[no]) no = arr[i].b; n       sgt.down(loc[no], 1); n     } else { n       int a = arr[i].a, b = arr[i].b; n       int l = 0; n       lst[0] = lst[1] = 0; n       while (anc[a] != anc[b]) { n         int no = a, cate = 0; n         if (deep[anc[a]] < deep[anc[b]]) { n           no = b; n           cate = 1; n         } n         st[cate][lst[cate]++] = make_pair(loc[anc[no]], loc[no]); n         l += abs(loc[anc[no]] - loc[no]) + 1; n         if (deep[anc[a]] < deep[anc[b]]) n           b = fa[anc[no]]; n         else n           a = fa[anc[no]]; n       } n       if (a != b) { n         if (deep[a] > deep[b]) n           st[0][lst[0]++] = make_pair(loc[b] + 1, loc[a]); n         else n           st[1][lst[1]++] = make_pair(loc[a] + 1, loc[b]); n         l += abs(deep[a] - deep[b]); n       } n       int ff1 = 400000, ff2 = ff1 + 1, ff3 = ff1 + 2; n       sgt.tree[ff1].rig = sgt.tree[ff1].lef = sgt.tree[ff1].sumv = 0; n       sgt.tree[ff1].flag = 1; n       sgt.tree[ff2].rig = sgt.tree[ff2].lef = sgt.tree[ff2].sumv = 0; n       sgt.tree[ff2].flag = 1; n       for (int i = lst[0] - 1; i >= 0; i--) n         sgt.getsum(st[0][i].first, st[0][i].second, 1, ff1); n       for (int i = lst[1] - 1; i >= 0; i--) n         sgt.getsum(st[1][i].first, st[1][i].second, 1, ff2); n       swap(sgt.tree[ff1].lef, sgt.tree[ff1].rig); n       sgt.pushup(sgt.tree + ff3, sgt.tree + ff1, sgt.tree + ff2); n       int rans = sgt.tree[ff3].sumv + f[sgt.tree[ff3].lef]; n       if (!sgt.tree[ff3].flag) rans += f[sgt.tree[ff3].rig]; n       ans[arr[i].cate] = rans; n     } n   } n   for (int i = 0; i < nq; i++) printf( %d n , ans[i]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double pi = 2 * acos(0); n const int oo = 2e9; n const int N = 205; n const int AL = 26; n vector<int> idx[AL]; n vector<int> adj[N]; n int clr[N]; n bool dfs(int u, int c = 0); n int main() { n   int n; n   scanf( %d , &(n)); n   string s(n,    ); n   for (int i = 0; i < n; i++) scanf(  %c , &s[i]); n   memset(clr, -1, sizeof(clr)); n   for (int i = 0; i < n; i++) { n     int x = s[i] -  a ; n     for (int c = x + 1; c < AL; c++) { n       for (auto& j : idx[c]) { n         adj[i].push_back(j); n         adj[j].push_back(i); n       } n     } n     idx[x].push_back(i); n   } n   bool valid = true; n   for (int i = 0; i < n; i++) { n     if (clr[i] == -1) valid &= dfs(i); n   } n   if (valid) { n     puts( YES ); n     for (int i = 0; i < n; i++) { n       printf( %d , clr[i]); n     } n     printf(  n ); n   } else n     printf( NO n ); n   return 0; n } n bool dfs(int u, int c) { n   clr[u] = c; n   bool ret = true; n   for (auto& v : adj[u]) { n     if (clr[v] == -1) n       ret &= dfs(v, !c); n     else if (c == clr[v]) n       return false; n   } n   return ret; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n long long int rev[N], fac[N], mod = 1e9 + 7; n long long int wop(long long int x, long long int y) { n   if (y == 0) return 1; n   long long int ret = wop(x, y / 2); n   ret %= mod; n   ret *= ret; n   ret %= mod; n   if (y & 1) ret *= x; n   ret %= mod; n   return ret; n } n long long int chos(long long int r, long long int n) { n   return (fac[n] * ((rev[r] * rev[n - r]) % mod)) % mod; n } n int main() { n   long long int n, m; n   cin >> n >> m; n   rev[0] = fac[0] = 1; n   for (int i = 1; i < 2e3; i++) { n     fac[i] = fac[i - 1] * i; n     fac[i] %= mod; n     rev[i] = wop(fac[i], mod - 2); n   } n   long long int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       long long int f1 = chos(i - 1, i + m - 2); n       long long int f2 = chos(n - j, n + m - j - 1); n       ans += (f1 * f2) % mod; n       ans %= mod; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 505 * 505 * 4; n int n; n using vi = vector<int>; n vi G[maxn]; n int dfn[maxn]; n int low[maxn]; n int scc[maxn]; n int scnt[maxn]; n int ntime; n int scc_cnt; n int sta[maxn]; n int sz; n void tarjan(int u) { n   dfn[u] = low[u] = ++ntime; n   sta[sz++] = u; n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (!dfn[v]) { n       tarjan(v); n       low[u] = min(low[u], low[v]); n     } else if (!scc[v]) { n       low[u] = min(low[u], dfn[v]); n     } n   } n   if (low[u] == dfn[u]) { n     scc_cnt++; n     int tmp; n     do { n       --sz; n       tmp = sta[sz]; n       scc[tmp] = scc_cnt; n       scnt[scc_cnt]++; n     } while (tmp != u); n   } n } n bool find_bcc() { n   sz = 0; n   memset(scc, 0, sizeof(scc)); n   memset(dfn, 0, sizeof(dfn)); n   memset(scnt, 0, sizeof(scnt)); n   ntime = scc_cnt = 0; n   for (int i = 1; i <= n; ++i) n     if (!dfn[i]) tarjan(i); n   for (int i = 1; i <= n; i += 2) { n     if (scc[i] == scc[i + 1]) return false; n   } n   return true; n } n char s[505][505]; n int w, h; n int id(int x, int y) { return (x - 1) * w + y; } n int res[505][505]; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n int main() { n   scanf( %d %d , &h, &w); n   n = 2 * h * w; n   for (int i = 1; i <= h; i += 1) { n     scanf( %s , s[i] + 1); n   } n   for (int i = 1; i <= h; i += 1) { n     for (int j = 1; j <= w; j += 1) { n       if (s[i][j] ==  . ) continue; n       int cnt = 0; n       vector<int> lst; n       for (int k = 0; k < 4; k += 1) { n         int nx = i + dx[k]; n         int ny = j + dy[k]; n         if (s[nx][ny] ==  . ) { n           cnt += 1; n           lst.emplace_back(id(nx, ny)); n         } n       } n       if (cnt == 1 || cnt == 3) { n         puts( NO ); n         return 0; n       } n       if (!cnt) continue; n       if (cnt == 2) { n         G[lst[0] * 2 - 1].emplace_back(lst[1] * 2); n         G[lst[1] * 2 - 1].emplace_back(lst[0] * 2); n         G[lst[0] * 2].emplace_back(lst[1] * 2 - 1); n         G[lst[1] * 2].emplace_back(lst[0] * 2 - 1); n       } n       if (cnt == 4) { n         for (int i = 0; i < 2; i += 1) { n           G[2 * lst[i]].emplace_back(2 * lst[i + 2]); n           G[2 * lst[i] - 1].emplace_back(2 * lst[i + 2] - 1); n           G[2 * lst[i + 2]].emplace_back(2 * lst[i]); n           G[2 * lst[i + 2] - 1].emplace_back(2 * lst[i] - 1); n         } n         for (int i = 0; i < 4; i += 2) { n           int nx = lst[(i + 1) % 4]; n           G[2 * lst[i] - 1].emplace_back(2 * nx); n           G[2 * lst[i]].emplace_back(2 * nx - 1); n           nx = lst[(i + 3) % 4]; n           G[2 * lst[i] - 1].emplace_back(2 * nx); n           G[2 * lst[i]].emplace_back(2 * nx - 1); n         } n       } n     } n   } n   if (!find_bcc()) n     puts( NO ); n   else { n     puts( YES ); n     for (int i = 1; i <= h; i += 1) { n       for (int j = 1; j <= w; j += 1) { n         if (s[i][j] ==  X ) continue; n         int cur = id(i, j); n         if (scc[2 * cur - 1] < scc[2 * cur]) n           res[i][j] = 1; n         else n           res[i][j] = 4; n       } n     } n     for (int i = 1; i <= h; i += 1) { n       for (int j = 1; j <= w; j += 1) { n         if (s[i][j] ==  . ) continue; n         for (int k = 0; k < 4; k += 1) { n           int nx = i + dx[k]; n           int ny = j + dy[k]; n           if (s[nx][ny] ==  X ) continue; n           res[i][j] += res[nx][ny]; n         } n       } n     } n     for (int i = 1; i <= h; i += 1) { n       for (int j = 1; j <= w; j += 1) { n         printf( %d%c , res[i][j],    n [j == w]); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( tune=native ) n using namespace std; n const int MAX = 1e6 + 5; n const long long MOD = 1000000007; n const long long MOD2 = 2010405347; n const long long INF = 2e18; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; n const double pi = acos(-1); n const double EPS = 1e-9; n const int block = 315; n inline long long pw(long long x, long long y, long long md = MOD) { n   long long ret = 1; n   while (y) { n     if (y & 1) ret = ret * x % md; n     x = x * x % md, y >>= 1; n   } n   return ret; n } n int n, x[MAX], y[MAX], z[MAX], nw; n long long ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     cin >> x[i]; n     y[i] = max(y[i - 1], x[i] + 1); n   } n   for (int i = (n); i >= (1); --i) { n     nw = max(nw - 1, x[i] + 1); n     z[i] = nw; n   } n   for (int i = (1); i <= (n); ++i) { n     nw = max(y[i], z[i]); n     ans += nw - x[i] - 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkmax(T& x, T y) { n   if (x < y) x = y; n } n template <typename T> n void chkmin(T& x, T y) { n   if (x > y) x = y; n } n inline int read() { n   int x = 0; n   char c = getchar(); n   bool f = 0; n   while (c < 48) f |= c ==  - , c = getchar(); n   while (c > 47) x = x * 10 + (c ^ 48), c = getchar(); n   return f ? -x : x; n } n int N, p[114]; n long long getv(long long x) { n   long long res = 0; n   for (int i = (N - 1), iend = (0); i >= iend; --i) { n     if ((long double)res * x + p[i] > 1e18) return -1; n     res = res * x + p[i]; n   } n   return res; n } n void solve() { n   long long t, a, b; n   cin >> t >> a >> b; n   int res = a == b ? 1 : 0, tt = -1, ct = 0; n   if (t > a || a > b || (t == a && a == 1 && b > 1)) n     return printf( %d n , res), void(); n   if (b == 1) return puts( inf ), void(); n   if (a != 1) { n     for (long long tp = b; tp; tp /= a) p[N++] = tp % a, ct += tp % a != 0; n     res += getv(t) == a && getv(a) == b; n     if (ct == 1) tt = N - 1; n   } n   long long zzh = 1, yyh = 1; n   for (int i = (1), iend = (60); i <= iend; ++i) { n     if ((long double)yyh * a > 1e18) break; n     zzh *= t, yyh *= a; n     if (i != tt && a % zzh == 0 && b % yyh == 0 && a / zzh == b / yyh) res++; n   } n   cout << res << endl; n } n signed main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n long long dp[2001][2001]; n long long dp1[2001][2001]; n int main() { n   int N, K; n   string S; n   cin >> N >> K >> S; n   memset(dp, 0, sizeof(dp)); n   memset(dp1, 0, sizeof(dp1)); n   dp[0][0] = dp1[0][0] = 1; n   for (int n = 1; n <= N; ++n) { n     for (int k = 0; k <= K; ++k) { n       dp[n][k] = dp1[n - 1][k] * (long long)(S[n - 1] -  a ) % MOD; n     } n     for (int i = 0; i < n; ++i) { n       int delta = (n - i) * (N - n + 1); n       for (int k = 0; k + delta <= K; ++k) { n         dp[n][k + delta] += dp[i][k] * (long long)( z  - S[n - 1]) % MOD; n         dp[n][k + delta] %= MOD; n       } n     } n     for (int k = 0; k <= K; ++k) { n       dp1[n][k] = (dp1[n - 1][k] + dp[n][k]) % MOD; n     } n   } n   cout << dp1[N][K] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n int type[N], n, p[N], out[N]; n int go(int u, bool print = false) { n   int res = p[u] == -1 || out[p[u]] > 1 ? 1 : go(p[u], print) + 1; n   if (print) cout << u + 1 <<    ; n   return res; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> type[i]; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n     if (--p[i] >= 0) ++out[p[i]]; n   } n   int res = -1; n   for (int i = 0; i < n; ++i) n     if (type[i] == 1) n       if (res == -1 || go(i) > go(res)) res = i; n   cout << go(res) << endl; n   go(res, true); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n const int MAXN = 1e5 + 5; n using namespace std; n struct node { n   int i; n   node *l, *r; n }; n node *newN(int i) { n   node *x = new node; n   x->i = i; n   x->r = x->l = NULL; n   return x; n } n node *insert(node *actual, int i, bool k) { n   if (!actual) return newN(i); n   if (k) n     return actual->r = newN(i); n   else n     return actual->l = newN(i); n } n void inorder(node *root) { n   if (!root) return; n   inorder(root->l); n   cout << root->i <<   n ; n   inorder(root->r); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string a; n   cin >> a; n   node *root = newN(1), *actual = root; n   for (int i = 0; i < (int)a.size() - 1; i++) { n     if (a[i] ==  l ) n       actual = insert(actual, i + 2, 0); n     else n       actual = insert(actual, i + 2, 1); n   } n   inorder(root); n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-10; n const float epsf = 1e-6; n using namespace std; n inline long long int __gcd(long long int a, long long int b) { n   if (a == 0 || b == 0) { n     return max(a, b); n   } n   long long int tempa, tempb; n   while (1) { n     if (a % b == 0) n       return b; n     else { n       tempa = a; n       tempb = b; n       a = tempb; n       b = tempa % tempb; n     } n   } n } n inline int compfloat(float& x, float& y) { n   if (fabs(x - y) < epsf) n     return 0; n   else if (x - y > 0) n     return 1; n   return -1; n } n inline int compdouble(double x, double y) { n   if (fabs(x - y) < eps) n     return 0; n   else if (x - y > 0) n     return 1; n   else n     return -1; n } n bool prime(long long int k) { n   for (long long int i = 2; i * i <= k; i++) n     if (k % i == 0) { n       return false; n     } n   return true; n } n void pdash(int n = 1) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 30; j++) { n       cout <<  - ; n     } n     cout <<   n ; n   } n } n void solve() { n   long long int n; n   cin >> n; n   long long int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   long long int dpi[n], dpd[n]; n   dpi[0] = 0; n   dpd[n - 1] = 0; n   for (int i = 1; i < n; i++) n     dpi[i] = max(dpi[i - 1] + arr[i - 1] + 1 - arr[i], (long long int)0); n   for (int i = (int)n - 2; i >= 0; i--) n     dpd[i] = max(dpd[i + 1] + arr[i + 1] + 1 - arr[i], (long long int)0); n   long long int sumi[n], sumd[n]; n   sumi[0] = dpi[0]; n   sumd[n - 1] = dpd[n - 1]; n   for (int i = 1; i < n; i++) sumi[i] = sumi[i - 1] + dpi[i]; n   for (int i = (int)n - 2; i >= 0; i--) sumd[i] = sumd[i + 1] + dpd[i]; n   long long int ans = numeric_limits<long long int>::max(); n   for (int i = 1; i < n - 1; i++) n     ans = min(ans, sumi[i - 1] + sumd[i + 1] + max(dpi[i], dpd[i])); n   ans = min({sumd[0], sumi[n - 1], ans}); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100007; n struct LinkCutTree { n   struct Node { n     int L, R, P, lazyFlip; n     int PP; n   }; n   Node LCT[MAXN]; n   void normalize(int u) { n     assert(u != -1); n     if (LCT[u].L != -1) LCT[LCT[u].L].P = u; n     if (LCT[u].R != -1) LCT[LCT[u].R].P = u; n   } n   void setLeftChild(int p, int v) { n     LCT[p].L = v; n     normalize(p); n   } n   void setRightChild(int p, int v) { n     LCT[p].R = v; n     normalize(p); n   } n   void pushLazy(int u) { n     if (!LCT[u].lazyFlip) return; n     swap(LCT[u].L, LCT[u].R); n     LCT[u].lazyFlip = 0; n     if (LCT[u].L != -1) LCT[LCT[u].L].lazyFlip ^= 1; n     if (LCT[u].R != -1) LCT[LCT[u].R].lazyFlip ^= 1; n   } n   void make_tree(int v) { n     LCT[v].L = LCT[v].R = LCT[v].P = LCT[v].PP = -1; n     LCT[v].lazyFlip = 0; n   } n   void rotate(int v) { n     if (LCT[v].P == -1) return; n     int p = LCT[v].P; n     int g = LCT[p].P; n     if (LCT[p].L == v) { n       setLeftChild(p, LCT[v].R); n       setRightChild(v, p); n     } else { n       setRightChild(p, LCT[v].L); n       setLeftChild(v, p); n     } n     LCT[v].P = g; n     if (g != -1) { n       if (LCT[g].L == p) { n         setLeftChild(g, v); n       } else { n         setRightChild(g, v); n       } n     } n     LCT[v].PP = LCT[p].PP; n     LCT[p].PP = -1; n   } n   void pushEmAll(int v) { n     if (LCT[v].P != -1) pushEmAll(LCT[v].P); n     pushLazy(v); n   } n   void splay(int v) { n     pushLazy(v); n     while (LCT[v].P != -1) { n       int p = LCT[v].P; n       int g = LCT[p].P; n       if (g != -1) pushLazy(g); n       pushLazy(p); n       pushLazy(v); n       if (g == -1) { n         rotate(v); n       } else if ((LCT[p].L == v) == (LCT[g].L == p)) { n         rotate(p); n         rotate(v); n       } else { n         rotate(v); n         rotate(v); n       } n     } n   } n   vector<int> access(int v, bool doIt = false) { n     splay(v); n     int _v = v, _r = -1; n     if (LCT[v].R != -1) { n       if (doIt) { n         _r = LCT[v].R; n       } n       LCT[LCT[v].R].PP = v; n       LCT[LCT[v].R].P = -1; n       setRightChild(v, -1); n     } n     vector<int> ret; n     while (LCT[v].PP != -1) { n       int w = LCT[v].PP; n       splay(w); n       ret.push_back(w); n       if (LCT[w].R != -1) { n         LCT[LCT[w].R].PP = w; n         LCT[LCT[w].R].P = -1; n       } n       LCT[v].PP = -1; n       setRightChild(w, v); n       splay(v); n     } n     if (_r != -1) { n       splay(_v); n       assert(LCT[_v].R == -1); n       setRightChild(_v, _r); n       LCT[_r].PP = -1; n     } n     return ret; n   } n   int find_root(int v) { n     access(v); n     int ret = v; n     while (LCT[ret].L != -1) { n       ret = LCT[ret].L; n       pushLazy(ret); n     } n     access(ret); n     return ret; n   } n   void link(int v, int w) { n     access(w); n     setLeftChild(v, w); n     LCT[w].PP = -1; n   } n   void cut(int v) { n     access(v); n     if (LCT[v].L != -1) { n       LCT[LCT[v].L].P = -1; n       LCT[LCT[v].L].PP = -1; n       setLeftChild(v, -1); n     } n   } n   void make_root(int v) { n     access(v); n     int l = LCT[v].L; n     if (l != -1) { n       setLeftChild(v, -1); n       LCT[l].P = -1; n       LCT[l].PP = v; n       LCT[l].lazyFlip ^= 1; n     } n   } n   bool isConnected(int p, int q) { return find_root(p) == find_root(q); } n   int LCA(int p, int q) { n     access(p); n     return access(q).back(); n   } n }; n vector<pair<int, int> > edg[MAXN]; n long long depth[MAXN]; n LinkCutTree lct; n int bap[MAXN]; n vector<int> accessOrder; n int st[MAXN], ed[MAXN]; n int tym; n void dfs(int u, int p) { n   st[u] = ++tym; n   bap[u] = p; n   bool called = false; n   for (pair<int, int> pr : edg[u]) { n     if (p == pr.first) continue; n     called = true; n     depth[pr.first] = depth[u] + pr.second; n     lct.link(pr.first, u); n     dfs(pr.first, u); n   } n   if (!called) accessOrder.push_back(u); n   ed[u] = tym; n } n map<long long, long long> vacancy; n bool makePlace(long long l, long long r) { n   auto it = vacancy.lower_bound(l); n   if (l <= prev(it)->second) { n     it = prev(it); n     long long _l = it->first; n     long long _r = it->second; n     vacancy.erase(it); n     if (_l < l) vacancy[_l] = l - 1; n     if (l < _r) vacancy[l + 1] = _r; n     return true; n   } else { n     assert(it != vacancy.end()); n     long long _l = it->first; n     long long _r = it->second; n     if (r < _l) return false; n     vacancy.erase(it); n     if (_l < _r) vacancy[_l + 1] = _r; n     return true; n   } n   assert(false); n } n set<int> tr[4 * MAXN]; n void update(int u, int l, int r, int i, int t) { n   tr[u].insert(t); n   if (l == r) return; n   int mid = (l + r) / 2; n   if (i <= mid) n     update(u * 2, l, mid, i, t); n   else n     update(u * 2 + 1, mid + 1, r, i, t); n } n int ask(int u, int l, int r, int x, int y, int t) { n   if (y < l || r < x) return 0; n   if (x <= l && r <= y) { n     auto it = tr[u].lower_bound(t); n     if (it == tr[u].begin()) return 0; n     return *prev(it); n   } n   int mid = (l + r) / 2; n   return max(ask(u * 2, l, mid, x, y, t), ask(u * 2 + 1, mid + 1, r, x, y, t)); n } n void printEdges(int s) { n   while (s != 1) { n     cout << s <<  @  << depth[s] <<   <-  ; n     s = bap[s]; n   } n   cout << 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i < n; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     edg[u].emplace_back(v, w); n     edg[v].emplace_back(u, w); n   } n   for (int i = 1; i <= n; i++) lct.make_tree(i); n   dfs(1, 0); n   for (int leaf : accessOrder) { n     lct.access(leaf); n   } n   map<long long, vector<long long> > mp; n   for (int _m = 0; _m < m; _m++) { n     int s, t; n     cin >> s >> t; n     if (s == 1) continue; n     vector<int> need = lct.access(s, true); n     for (int p : need) { n       int l = ask(1, 1, n, st[p], ed[p], t); n       if (l > 0) n         mp[t + depth[p]].push_back(l + 1 + depth[p]); n       else n         mp[t + depth[p]].push_back(1); n     } n     update(1, 1, n, st[bap[s]], t); n   } n   vacancy[0] = 1e15; n   int ans = 0; n   for (pair<const long long, vector<long long> >& pr : mp) { n     sort(pr.second.begin(), pr.second.end()); n     bool okay = true; n     for (long long x : pr.second) { n       if (!makePlace(x, pr.first)) { n         okay = false; n         break; n       } n     } n     if (!okay) { n       cout << pr.first <<     << ans << endl; n       return 0; n     } n     ans += pr.second.size(); n   } n   cout << -1 <<     << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n long long arr[MAX], arr2[MAX]; n int n, m; n bool check(long long mid) { n   int prv = 0; n   for (int i = 0; i < n; ++i) { n     while (prv < m && arr2[prv] + mid < arr[i]) ++prv; n     if (prv == m || arr2[prv] - mid > arr[i]) return 0; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   for (int i = 0; i < m; ++i) cin >> arr2[i]; n   sort(arr, arr + n); n   sort(arr2, arr2 + m); n   long long l = 0, r = 2e9; n   long long ans = r; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   return cout << ans <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 2; n int a[maxn << 1]; n long long vis[maxn]; n int main(void) { n   std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n, m; n   while (cin >> n >> m) { n     int k = 0; n     int t; n     vector<int> a(n + m + 1); n     vector<long long> vis(m); n     for (int i = 1; i <= n + m; i++) cin >> a[i]; n     for (int i = 1; i <= n + m; i++) cin >> t, t ? vis[k++] = a[i] : 1; n     vis[k] = (int)1e18; n     for (int i = 1, j = 0; i <= n + m; j++) { n       int cnt = 0; n       while (a[i] < vis[j]) cnt++, i++; n       i++; n       while (a[i] - vis[j] <= vis[j + 1] - a[i] && i <= n + m) cnt++, i++; n       cout << cnt <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n bool isprime[N] = {1, 1, 0}; n int sum[N] = {}; n void YesIcan() { n   for (int i = 2; i * i <= N; ++i) { n     for (int j = i * i; j <= N; j += i) isprime[j] = true; n   } n } n int main() { n   YesIcan(); n   int a, b, k; n   cin >> a >> b >> k; n   for (int i = a; i <= b; ++i) { n     sum[i] = sum[i - 1]; n     if (!isprime[i]) ++sum[i]; n   } n   if (sum[b] < k) { n     cout << -1; n     return 0; n   } n   int i = a - 1, l = 1; n   for (; i <= b - l; ++i) { n     while (sum[i + l] - sum[i] < k && l <= b - a + 1 && i + l <= b) ++l; n   } n   cout << l; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n #ifdef LOCAL n   freopen( ../IO/d.in ,  r , stdin); n //  freopen( ../IO/d.out ,  w , stdout); n #else n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.tie(nullptr); n #endif n   int number_of_tests; n   cin >> number_of_tests; n  n   while (number_of_tests--) { n     int n; n     cin >> n; n     std::vector<int> b(n); n     std::vector<int> used(2 * n); n     for (int &i : b) cin >> i, --i, used[i] = 1; n     std::vector<int> unused; n     unused.reserve(n); n     for (int i = 0; i < 2 * n; ++i) { n       if (!used[i]) unused.push_back(i); n     } n     vector<int> c(n + 1, true); n     int l = -1, r = -1; n     auto check1 = [&](int x) { n       deque<int> dq(unused.begin(), unused.end()); n       bool ok = true; n       for (int i = x - 1; i >= 0; --i) { n         ok &= b[i] < dq.back(); n         dq.pop_back(); n       } n       return ok; n     }; n     auto check2 = [&](int x) { n       deque<int> dq(unused.begin(), unused.end()); n       bool ok = true; n       for (int i = x; i < n; ++i) { n         ok &= b[i] > dq.front(); n         dq.pop_front(); n       } n       return ok; n     }; n     int lo = -1, hi = n + 1; n     while (hi - lo > 1) { n       int x = (lo + hi) / 2; n       if (check1(x)) { n         lo = x; n       } else { n         hi = x; n       } n     } n     l = 0, r = lo; n     lo = -1, hi = n + 1; n     while (hi - lo > 1) { n       int x = (lo + hi) / 2; n       if (check2(x)) { n         hi = x; n       } else { n         lo = x; n       } n     } n     l = std::max(l, hi); n //    std::cout << l <<     << r <<   n ; n     std::cout << std::max(0, r - l + 1) <<   n ; n   } n   return 0; n } n  n  n


#include <bits/stdc++.h> n int gcd(int a, int b) { n   if (b == 0) return a; n   gcd(b, a % b); n } n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     if (gcd(a, b) != 1) n       cout <<  INFINITE  << endl; n     else n       cout <<  FINITE  << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16000000 ) n using namespace std; n const int Maxn = 1000005; n const int Maxm = 4194304; n const int Inf = 1000000000; n int n, c; n pair<int, int> lef[Maxn], rig[Maxn]; n int me[Maxn]; n int res[Maxn], rlen; n int st[Maxm]; n void Insert(int v, int l, int r, int x, int val) { n   if (l == r) n     st[v] = val; n   else { n     int m = l + r >> 1; n     if (x <= m) n       Insert(2 * v, l, m, x, val); n     else n       Insert(2 * v + 1, m + 1, r, x, val); n     st[v] = max(st[2 * v], st[2 * v + 1]); n   } n } n int Get(int v, int l, int r, int a, int b) { n   if (l == a && r == b) n     return st[v]; n   else { n     int res = -Inf; n     int m = l + r >> 1; n     if (a <= m) res = max(res, Get(2 * v, l, m, a, min(m, b))); n     if (m + 1 <= b) res = max(res, Get(2 * v + 1, m + 1, r, max(m + 1, a), b)); n     return res; n   } n } n bool isNull(const pair<int, int> &p) { return p == pair<int, int>(Inf, -Inf); } n void Print(int l, int r) { n   if (l > r) return; n   if (l == r) n     res[rlen++] = l; n   else { n     int from = l + 1; n     if (!isNull(lef[l])) { n       Print(l + 1, lef[l].second); n       from = lef[l].second + 1; n     } n     res[rlen++] = l; n     Print(from, r); n   } n } n int main() { n   scanf( %d %d , &n, &c); n   for (int i = 1; i <= n; i++) { n     lef[i] = pair<int, int>(Inf, -Inf); n     rig[i] = pair<int, int>(Inf, -Inf); n   } n   int a, b; n   char s[10]; n   while (c--) { n     scanf( %d %d %s , &a, &b, s); n     if (s[0] ==  L ) { n       lef[a].first = min(lef[a].first, b); n       lef[a].second = max(lef[a].second, b); n     } else { n       rig[a].first = min(rig[a].first, b); n       rig[a].second = max(rig[a].second, b); n     } n   } n   for (int i = n; i >= 1; i--) { n     if (isNull(lef[i]) && isNull(rig[i])) n       me[i] = i; n     else if (isNull(lef[i])) n       if (rig[i].first <= i) { n         printf( IMPOSSIBLE n ); n         return 0; n       } else n         me[i] = Get(1, 1, n, i + 1, rig[i].second); n     else if (isNull(rig[i])) n       if (lef[i].first <= i) { n         printf( IMPOSSIBLE n ); n         return 0; n       } else { n         lef[i].second = Get(1, 1, n, i + 1, lef[i].second); n         me[i] = lef[i].second; n       } n     else if (lef[i].first <= i) { n       printf( IMPOSSIBLE n ); n       return 0; n     } else { n       lef[i].second = Get(1, 1, n, i + 1, lef[i].second); n       if (lef[i].second >= rig[i].first) { n         printf( IMPOSSIBLE n ); n         return 0; n       } else n         me[i] = Get(1, 1, n, lef[i].second + 1, rig[i].second); n     } n     Insert(1, 1, n, i, me[i]); n   } n   Print(1, n); n   for (int i = 0; i < rlen; i++) n     printf( %d%c , res[i], i + 1 < rlen ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int sz = 7e5; n long long int sum[sz]; n long long int mi[sz]; n long long int arr[sz]; n void build(long long int node, long long int l, long long int r) { n   if (l == r) { n     sum[node] = arr[l]; n   } else { n     long long int mid = (l + r) / 2; n     build(2 * node, l, mid); n     build(2 * node + 1, mid + 1, r); n     sum[node] = sum[node * 2] + sum[node * 2 + 1]; n   } n } n long long int query(long long int node, long long int l, long long int r, n                     long long int tl, long long int tr) { n   if (l > tr || r < tl) n     return 0; n   else if (l >= tl && r <= tr) n     return sum[node]; n   else n     return (query(node * 2, l, (l + r) / 2, tl, tr) + n             query(node * 2 + 1, (l + r) / 2 + 1, r, tl, tr)); n } n void build2(long long int node, long long int l, long long int r) { n   if (l == r) { n     mi[node] = arr[l]; n   } else { n     long long int mid = (l + r) / 2; n     build2(2 * node, l, mid); n     build2(2 * node + 1, mid + 1, r); n     mi[node] = min(mi[node * 2], mi[node * 2 + 1]); n   } n } n long long int query2(long long int node, long long int l, long long int r, n                      long long int tl, long long int tr) { n   if (l > tr || r < tl) n     return 0; n   else if (l >= tl && r <= tr) n     return mi[node]; n   else n     return min(query2(node * 2, l, (l + r) / 2, tl, tr), n                query2(node * 2 + 1, (l + r) / 2 + 1, r, tl, tr)); n } n long long int pre[sz]; n int main() { n   std::ios::sync_with_stdio(false); n   long long int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   build(1, 1, n); n   build2(1, 1, n); n   sort(arr, arr + n + 1); n   for (int i = 1; i <= n; i++) { n     pre[i] = pre[i - 1] + arr[i]; n   } n   long long int q; n   cin >> q; n   while (q--) { n     long long int t; n     cin >> t; n     if (t == 1) { n       long long int l, r; n       cin >> l >> r; n       cout << query(1, 1, n, l, r) << endl; n     } else { n       long long int l, r; n       cin >> l >> r; n       cout << pre[r] - pre[l - 1] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int inf = 0x3f3f3f3f; n const long long INF = 1e18; n template <typename T> n void read(T &x) { n   x = 0; n   int s = 1, c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) s = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; n   x *= s; n } n template <typename T> n void write(T x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void writeln(T x) { n   write(x); n   puts(  ); n } n const int maxn = 1e5 + 5, maxm = 1e6 + 5; n struct Edge { n   int u, v, c, id; n   bool operator<(const Edge &rhs) const { return c < rhs.c; } n } e[maxm]; n int n, m; n int fa[maxn]; n vector<pair<int, int> > adj[maxn]; n int f[maxn][20], dis[maxn][20], d[maxn]; n vector<Edge> v; n int find(int v) { return v == fa[v] ? v : fa[v] = find(fa[v]); } n void dfs(int u, int fa) { n   d[u] = d[fa] + 1; n   for (pair<int, int> x : adj[u]) n     if (x.first ^ fa) { n       int v = x.first, c = x.second; n       f[v][0] = u; n       dis[v][0] = c; n       dfs(v, u); n     } n } n int LCA(int x, int y, int op) { n   int res = 0; n   if (d[x] > d[y]) swap(x, y); n   for (int t = 19; t >= 0; t--) n     if (d[x] <= d[f[y][t]]) { n       res = max(res, dis[y][t]); n       y = f[y][t]; n     } n   if (x == y) return op ? res : x; n   for (int t = 19; t >= 0; t--) n     if (f[x][t] != f[y][t]) { n       res = max(res, max(dis[x][t], dis[y][t])); n       x = f[x][t], y = f[y][t]; n     } n   res = max(res, max(dis[x][0], dis[y][0])); n   return op ? res : f[x][0]; n } n int main() { n   read(n), read(m); n   for (int i = 1; i <= m; i++) { n     read(e[i].u), read(e[i].v), read(e[i].c); n     e[i].id = i; n   } n   sort(e + 1, e + 1 + m); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int p = find(e[i].u), t = find(e[i].v); n     if (p != t) { n       adj[e[i].u].emplace_back(e[i].v, e[i].c); n       adj[e[i].v].emplace_back(e[i].u, e[i].c); n       fa[p] = t; n     } else n       v.emplace_back(e[i]); n   } n   dfs(1, 0); n   for (int j = 1; j <= 19; j++) n     for (int i = 1; i <= n; i++) { n       f[i][j] = f[f[i][j - 1]][j - 1]; n       dis[i][j] = max(dis[i][j - 1], dis[f[i][j - 1]][j - 1]); n     } n   sort((v.begin()), (v.end()), [&](Edge x, Edge y) { return x.id < y.id; }); n   for (auto i : v) { n     int lca = LCA(i.u, i.v, 0); n     printf( %d n , max(LCA(i.u, lca, 1), LCA(i.v, lca, 1))); n   } n } n


#include <bits/stdc++.h> n const size_t max_vertices = 1 << 16; n int degree[max_vertices], xor_sum[max_vertices]; n std::queue<int> queue; n std::vector<std::pair<int, int>> result; n int main() { n   int vcount = 0; n   std::cin >> vcount; n   for (int i = 0; i < vcount; i++) { n     std::cin >> degree[i] >> xor_sum[i]; n     if (degree[i] == 1) { n       queue.push(i); n     } n   } n   while (!queue.empty()) { n     int cur = queue.front(); n     queue.pop(); n     int next = xor_sum[cur]; n     if (degree[cur] > 0 && degree[next] > 0) { n       result.push_back(std::make_pair(cur, next)); n       degree[next]--; n       degree[cur]--; n       xor_sum[next] ^= cur; n     } n     if (degree[next] == 1) { n       queue.push(next); n     } n   } n   std::cout << result.size() << std::endl; n   for (int i = 0; i < result.size(); i++) { n     std::cout << result[i].first <<     << result[i].second << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long isPrime(long long x) { n   long long i, flag = 1; n   if (x == 0 || x == 1) { n     flag = 0; n   } n   for (i = 2; i <= sqrt(x); i++) { n     if (x % i == 0) { n       flag = 0; n       break; n     } n   } n   return flag; n } n long long fact(long long n) { n   long long ans = 1; n   for (long long i = 2; i <= n; i++) { n     ans *= i; n   } n   return ans; n } n const long long INF = 1e18; n const long long N = 2 * 1e5; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   { n     long long n; n     cin >> n; n     if (n == 0) { n       cout << 1 <<   n ; n       return 0; n     } n     if (n % 4 == 1) { n       cout << 8 <<   n ; n     } else if (n % 4 == 2) { n       cout << 4 <<   n ; n     } else if (n % 4 == 3) { n       cout << 2 <<   n ; n     } else if (n % 4 == 0) { n       cout << 6 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[6], b[6], ord[6], c[6]; n vector<pair<int, int> > v, nv; n struct S { n   vector<pair<int, int> > v; n   int from; n   S(vector<pair<int, int> > v, int from) : v(v), from(from) {} n   S() {} n }; n vector<S> s; n void pr(int a, int b) { n   if (a == 1) { n     printf( inc %d n , ord[b] + 1); n   } else { n     printf( dec %d n , ord[b] + 1); n   } n } n S hor, ver, res; n bool invI, invJ; n void _incI() { n   assert(ver.from < (int)ver.v.size()); n   res.v.push_back(ver.v[ver.from++]); n } n void _decI() { n   assert(ver.from > 0); n   pair<int, int> p = ver.v[--ver.from]; n   p.first = -p.first; n   res.v.push_back(p); n } n void _incJ() { n   assert(hor.from < (int)hor.v.size()); n   res.v.push_back(hor.v[hor.from++]); n } n void _decJ() { n   assert(hor.from > 0); n   pair<int, int> p = hor.v[--hor.from]; n   p.first = -p.first; n   res.v.push_back(p); n } n void decI() { n   if (invI) n     _incI(); n   else n     _decI(); n } n void incI() { n   if (invI) n     _decI(); n   else n     _incI(); n } n void decJ() { n   if (invJ) n     _incJ(); n   else n     _decJ(); n } n void incJ() { n   if (invJ) n     _decJ(); n   else n     _incJ(); n } n S merge() { n   res.v.clear(); n   res.from = 0; n   int i = ver.from; n   int j = hor.from; n   int n = (int)ver.v.size() + 1; n   int m = (int)hor.v.size() + 1; n   assert(i % 2 == j % 2); n   invI = invJ = false; n   if (i % 2 == 1) { n     for (int jj = j; jj >= 0; --jj) { n       if (jj & 1) { n         for (int _ = 0; _ < (int)(i); ++_) decI(); n       } else { n         for (int _ = 0; _ < (int)(i); ++_) incI(); n       } n       if (jj != 0) n         decJ(); n       else n         incI(); n     } n     for (int ii = i + 1; ii < n; ++ii) { n       if (ii & 1) { n         for (int _ = 0; _ < (int)(j); ++_) decJ(); n       } else { n         for (int _ = 0; _ < (int)(j); ++_) incJ(); n       } n       if (ii != n - 1) n         incI(); n       else n         incJ(); n     } n     for (int jj = j + 1; jj < m; ++jj) { n       if (jj & 1) { n         for (int _ = 0; _ < (int)(n - 1); ++_) incI(); n       } else { n         for (int _ = 0; _ < (int)(n - 1); ++_) decI(); n       } n       if (jj != m - 1) incJ(); n     } n   } else { n     if (i == n - 1) { n       i = 0; n       invI = true; n     } n     if (j == 0) { n       j = m - 1; n       invJ = true; n     } n     for (int _ = 0; _ < (int)(i); ++_) decI(); n     decJ(); n     for (int ii = 0; ii < (int)(i + 1); ++ii) { n       if (ii & 1) { n         for (int _ = 0; _ < (int)(j - 1); ++_) incJ(); n       } else { n         for (int _ = 0; _ < (int)(j - 1); ++_) decJ(); n       } n       incI(); n     } n     for (int jj = 0; jj < (int)(j + 1); ++jj) { n       if (jj & 1) { n         for (int _ = 0; _ < (int)(n - i - 2); ++_) decI(); n       } else { n         for (int _ = 0; _ < (int)(n - i - 2); ++_) incI(); n       } n       if (jj != j) incJ(); n     } n     if (j != m - 1) { n       incJ(); n       for (int jj = j + 1; jj < m; ++jj) { n         if (jj & 1) { n           for (int _ = 0; _ < (int)(n - 1); ++_) decI(); n         } else { n           for (int _ = 0; _ < (int)(n - 1); ++_) incI(); n         } n         if (jj != m - 1) incJ(); n       } n     } n   } n   return res; n } n int main() { n   cin >> n; n   for (int i = 0; i < (int)(n); ++i) cin >> a[i]; n   for (int i = 0; i < (int)(n); ++i) cin >> b[i]; n   if (n == 1) { n     if (a[0] == 2) { n       cout <<  Cycle  << endl; n       if (b[0] == 1) { n         cout <<  inc 1  << endl; n         cout <<  dec 1  << endl; n       } else { n         cout <<  dec 1  << endl; n         cout <<  inc 1  << endl; n       } n     } else { n       if (b[0] == 1) { n         cout <<  Path  << endl; n         for (int i = 0; i < (int)(a[0] - 1); ++i) { n           cout <<  inc 1 n ; n         } n       } else if (b[0] == a[0]) { n         cout <<  Path  << endl; n         for (int i = 0; i < (int)(a[0] - 1); ++i) { n           cout <<  dec 1 n ; n         } n       } else { n         cout <<  No  << endl; n       } n     } n     return 0; n   } n   for (int i = 0; i < (int)(n); ++i) ord[i] = i; n   for (int i = 0; i < (int)(n - 1); ++i) n     if (a[i] % 2 == 0) { n       swap(a[i], a[n - 1]); n       swap(b[i], b[n - 1]); n       swap(ord[i], ord[n - 1]); n       break; n     } n   if (a[n - 1] % 2 == 0) { n     for (int i = 0; i < (int)(n - 1); ++i) { n       nv.clear(); n       for (int j = 0; j < (int)(a[i]); ++j) { n         if (j & 1) { n           reverse(v.begin(), v.end()); n           for (pair<int, int> p : v) { n             nv.push_back(make_pair(-p.first, p.second)); n           } n           reverse(v.begin(), v.end()); n         } else { n           for (pair<int, int> p : v) { n             nv.push_back(p); n           } n         } n         if (j != a[i] - 1) { n           nv.push_back(make_pair(1, i)); n         } n       } n       v = nv; n     } n     nv.clear(); n     for (pair<int, int> p : v) { n       nv.push_back(p); n     } n     for (int i = 0; i < (int)(a[n - 1] - 1); ++i) { n       nv.push_back(make_pair(1, n - 1)); n       if (i & 1) { n         for (int j = 1; j < (int)v.size(); ++j) { n           nv.push_back(v[j]); n         } n       } else { n         for (int j = (int)v.size() - 1; j >= 1; --j) { n           nv.push_back(make_pair(-v[j].first, v[j].second)); n         } n       } n     } n     nv.push_back(make_pair(-v[0].first, v[0].second)); n     for (int i = 0; i < (int)(a[n - 1] - 1); ++i) { n       nv.push_back(make_pair(-1, n - 1)); n     } n     for (int i = 0; i < (int)(n); ++i) c[i] = 1; n     v = nv; n     int ind = 0; n     while (true) { n       bool ok = true; n       for (int j = 0; j < (int)(n); ++j) n         if (c[j] != b[j]) { n           ok = false; n           break; n         } n       if (ok) break; n       c[v[ind].second] += v[ind].first; n       ++ind; n     } n     printf( Cycle n ); n     for (int i = ind; i < (int)v.size(); ++i) { n       pr(v[i].first, v[i].second); n     } n     for (int i = 0; i < ind; ++i) { n       pr(v[i].first, v[i].second); n     } n     return 0; n   } n   int sum = 0; n   for (int i = 0; i < (int)(n); ++i) sum += b[i] - 1; n   if (sum % 2 == 1) { n     cout <<  No  << endl; n     return 0; n   } n   cout <<  Path  << endl; n   for (int i = 0; i < (int)(n); ++i) { n     vector<pair<int, int> > cur; n     for (int j = 0; j < (int)(a[i] - 1); ++j) { n       cur.push_back(make_pair(1, i)); n     } n     s.push_back(S(cur, b[i] - 1)); n   } n   while ((int)s.size() > 1) { n     bool done = false; n     for (int i = 0; i < (int)(s.size()); ++i) { n       for (int j = 0; j < (int)(i); ++j) { n         if ((s[i].from + s[j].from) % 2 == 0) { n           hor = s[i]; n           ver = s[j]; n           s[i] = merge(); n           swap(s[j], s.back()); n           s.pop_back(); n           done = true; n           break; n         } n       } n       if (done) { n         break; n       } n     } n     assert(done); n   } n   for (pair<int, int> p : s[0].v) { n     pr(p.first, p.second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3fffffff; n const int inf = -INF; n const int N = 100005; n const int M = 2005; n const int mod = 1000000007; n const double pi = acos(-1.0); n int mp1[N], mp2[N]; n int main() { n   int n, m; n   int d, p; n   char s[2]; n   scanf( %d %d , &n, &m); n   memset(mp1, 0, sizeof(mp1)); n   memset(mp2, 0, sizeof(mp2)); n   for (int i = 1; i <= n; i++) { n     scanf( %s %d %d , &s, &p, &d); n     if (s[0] ==  S ) n       mp1[p] += d; n     else n       mp2[p] += d; n   } n   int k = 0, t = 0; n   for (int it = 0; it < N, t < m; it++) { n     if (it > 100000) break; n     if (mp1[it] > 0) t++, k = it; n   } n   for (int j = k; j >= 0; j--) n     if (mp1[j] > 0) printf( S %d %d n , j, mp1[j]); n   t = 0; n   for (int it = N; it >= 0 && t < m; it--) { n     if (mp2[it] > 0) printf( B %d %d n , it, mp2[it]), t++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char arr[300001]; n int main(void) { n   int N; n   scanf( %d , &N); n   scanf( %s , arr); n   int i; n   char _max = 0; n   int idx = -1; n   for (i = 0; i < N; i++) { n     if (_max < arr[i]) { n       _max = arr[i]; n       idx = i; n     } n     if (idx != -1 && _max > arr[i]) { n       printf( YES n ); n       printf( %d %d n , idx + 1, i + 1); n       return 0; n     } n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mo = 1000000007; n const int Maxn = 1010; n int f[110000], g[110000]; n int a[Maxn * 2], b[Maxn * 2]; n int sum[110000]; n char st[Maxn]; n int n, m, cnt; n bool Check(int s) { n   for (int i = 0; i < n; ++i) n     if (a[s + i] != b[i]) return false; n   return true; n } n void Update(int &x, int y) { n   if (y < 0) y += mo; n   x += y; n   if (x >= mo) x -= mo; n } n int main() { n   scanf( %s , st); n   n = strlen(st); n   for (int i = 0; i < n; ++i) a[i] = st[i] -  a ; n   for (int i = 0; i < n; ++i) a[i + n] = a[i]; n   scanf( %s , st); n   for (int i = 0; i < n; ++i) b[i] = st[i] -  a ; n   scanf( %d , &m); n   sum[0] = 1; n   for (int i = 1; i <= m; ++i) sum[i] = (long long)sum[i - 1] * (n - 1) % mo; n   f[0] = 0; n   f[1] = 1; n   for (int i = 2; i <= m; ++i) { n     f[i] = sum[i - 1] - f[i - 1]; n     if (f[i] < 0) f[i] += mo; n   } n   g[0] = 1; n   g[1] = 0; n   for (int i = 2; i <= m; ++i) { n     g[i] = sum[i - 1] - g[i - 1]; n     if (g[i] < 0) g[i] += mo; n   } n   if (!m) { n     if (Check(0)) n       printf( 1 n ); n     else n       printf( 0 n ); n     return 0; n   } n   int ans = 0; n   if (Check(0)) Update(ans, g[m]); n   for (int i = 1; i < n; ++i) n     if (Check(i)) Update(ans, f[m]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int w = 1, s = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     s = (s << 1) + (s << 3) + ch -  0 ; n     ch = getchar(); n   } n   return w ? s : -s; n } n const int N = 1001; n struct point { n   int x, y, num; n } p[N]; n struct square { n   int x, y, r; n } s[N]; n int opp[N], bel[N]; n bool num[N]; n int n, m, k, ans; n vector<int> f[N]; n bool cmp1(point a, point b) { return a.x < b.x; } n bool cmp2(square a, square b) { return a.x - a.r < b.x - b.r; } n bool solve(int x) { n   if (x == 0) return 1; n   return 0; n } n void content() { n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read(), p[i].num = i; n   sort(p + 1, p + n + 1, cmp1); n   for (int i = 1; i <= n; i++) opp[p[i].num] = i; n   for (int i = 1; i <= m; i++) { n     s[i].r = read(); n     s[i].x = read(), s[i].y = read(); n   } n   sort(s + 1, s + m + 1, cmp2); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       if (p[j].x < s[i].x - s[i].r) continue; n       if (p[j].x > s[i].x + s[i].r) break; n       int a = p[j].x, b = p[j].y, c = s[i].x, d = s[i].y; n       double lc = sqrt(pow((a - c), 2) + pow((b - d), 2)); n       if (lc < s[i].r) f[p[j].num].push_back(i); n     } n   } n   for (int i = 1; i <= k; i++) { n     int x = read(), y = read(); n     for (int j = 0; j < f[x].size(); j++) num[f[x][j]] = solve(num[f[x][j]]); n     for (int j = 0; j < f[y].size(); j++) num[f[y][j]] = solve(num[f[y][j]]); n     for (int j = 1; j <= m; j++) n       if (num[j]) ans++; n     printf( %d n , ans); n     ans = 0; n     memset(num, 0, sizeof(num)); n   } n } n int main() { n   content(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k[3]; n long long t[200000]; n long long c[200000]; n int n; n long long T[3]; n long long nt[200000]; n multiset<long long> s; n int main(void) { n   for (int i = 0; i < 3; i++) scanf(  %d , &k[i]); n   for (int i = 0; i < 3; i++) scanf(  %I64d , &T[i]); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %I64d , &c[i]), nt[i] = c[i]; n   for (int i = 0; i < 3; i++) k[i] = min(k[i], n + 1); n   for (int step = 0; step < 3; step++) { n     s.clear(); n     for (int i = 0; i < k[step]; i++) { n       s.insert(0ll); n     } n     for (int i = 0; i < n; i++) t[i] = nt[i]; n     for (int i = 0; i < n; i++) { n       long long TM = max(*s.begin(), t[i]); n       nt[i] = TM + T[step]; n       s.erase(s.begin()); n       s.insert(nt[i]); n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     ans = max(ans, nt[i] - c[i]); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void print(T a) { n   for (auto x : a) cout << x <<    ; n   cout <<   n ; n } n void solve() { n   int n, m; n   cin >> n >> m; n   char a[n][m]; n   short down[n][m], up[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       down[i][j] = 0; n       up[i][j] = 0; n     } n   } n   short l[m], r[m]; n   for (int j = 0; j < m; j++) up[0][j] = 1; n   int ans = 0; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (j == 0 || (a[i][j - 1] != a[i][j])) n         l[j] = j; n       else n         l[j] = l[j - 1]; n     } n     for (int j = m - 1; j >= 0; j--) { n       if (j == m - 1 || (a[i][j + 1] != a[i][j])) n         r[j] = j; n       else n         r[j] = r[j + 1]; n     } n     for (int j = 0; j < m; j++) { n       int x = 1; n       if (a[i - 1][j] == a[i][j]) x = up[i - 1][j] + 1; n       x = min(x, j - l[j] + 1); n       x = min(x, r[j] - j + 1); n       up[i][j] = x; n     } n   } n   for (int j = 0; j < m; j++) down[n - 1][j] = 1; n   for (int i = n - 2; i >= 0; i--) { n     for (int j = 0; j < m; j++) { n       if (j == 0 || (a[i][j - 1] != a[i][j])) n         l[j] = j; n       else n         l[j] = l[j - 1]; n     } n     for (int j = m - 1; j >= 0; j--) { n       if (j == m - 1 || (a[i][j + 1] != a[i][j])) n         r[j] = j; n       else n         r[j] = r[j + 1]; n     } n     for (int j = 0; j < m; j++) { n       int x = 1; n       if (a[i + 1][j] == a[i][j]) x = down[i + 1][j] + 1; n       x = min(x, j - l[j] + 1); n       x = min(x, r[j] - j + 1); n       down[i][j] = x; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       short res = min(up[i][j], down[i][j]); n       ans += res; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n const int p = 10000007; n pair<long long, int> c[500005]; n pair<long long, long long> sum[500005]; n int id[500005]; n long long a[500005], b[500005], ans[500005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = (int)1; i <= (int)n; i++) { n     scanf( %I64d%I64d , &a[i], &b[i]); n     c[i] = make_pair(a[i] - b[i], i); n   } n   sort(c + 1, c + n + 1); n   for (int i = (int)1; i <= (int)n; i++) id[c[i].second] = i; n   sum[0] = make_pair(0, 0); n   sum[n + 1] = make_pair(0, 0); n   for (int i = (int)1; i <= (int)n; i++) n     sum[i].first = sum[i - 1].first + a[c[i].second]; n   for (int i = (int)n; i >= (int)1; i--) n     sum[i].second = sum[i + 1].second + b[c[i].second]; n   for (int i = (int)1; i <= (int)n; i++) n     ans[i] = a[i] * (n - id[i]) + b[i] * (id[i] - 1) + sum[id[i] - 1].first + n              sum[id[i] + 1].second; n   for (int times = (int)1; times <= (int)m; times++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (id[x] < id[y]) n       ans[x] -= a[x] + b[y], ans[y] -= a[x] + b[y]; n     else n       ans[x] -= a[y] + b[x], ans[y] -= a[y] + b[x]; n   } n   for (int i = (int)1; i <= (int)n; i++) printf( %I64d  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   string s; n   cin >> s; n   while (b--) { n     for (int i = 0; i < a - 1; i++) { n       if (s[i] ==  B  && s[i + 1] ==  G ) { n         int t; n         t = s[i]; n         s[i] = s[i + 1]; n         s[i + 1] = t; n         i++; n       } n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int abss(int n) { n   if (n < 0) n     return -n; n   else n     return n; n } n const int maxn = 3e5 + 10; n int _min(int a, int b) { n   if (a < b) n     return a; n   else n     return b; n } n int _max(int a, int b) { n   if (a > b) n     return a; n   else n     return b; n } n char s[maxn]; n int main() { n   int a, b, x; n   cin >> a >> b >> x; n   int w = _min(a, b); n   if (a > b) { n     int c0 = 0, c1 = 0; n     int tail = 0; n     printf( 0 ); n     c0++; n     for (int i = 1; i <= x - 1; i++) { n       if (i % 2 == 1) n         printf( 1 ), c1++, tail = 1; n       else n         printf( 0 ), c0++, tail = 0; n     } n     if (tail == 0 || x - 1 == 0) { n       int temp = a - c0; n       while (temp--) printf( 0 ); n       temp = b - c1; n       while (temp--) printf( 1 ); n     } else { n       int temp = b - c1; n       while (temp--) printf( 1 ); n       temp = a - c0; n       while (temp--) printf( 0 ); n     } n     printf(  n ); n   } else { n     int c0 = 0, c1 = 0; n     printf( 1 ); n     c1++; n     int tail = 1; n     for (int i = 1; i <= x - 1; i++) { n       if (i % 2 == 1) n         printf( 0 ), c0++, tail = 0; n       else n         printf( 1 ), c1++, tail = 1; n     } n     if (tail == 0 && x - 1 != 0) { n       int temp = a - c0; n       while (temp > 0) printf( 0 ), temp--; n       temp = b - c1; n       while (temp > 0) printf( 1 ), temp--; n     } else { n       int temp = b - c1; n       while (temp > 0) printf( 1 ), temp--; n       temp = a - c0; n       while (temp > 0) printf( 0 ), temp--; n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int a, b, c, d, m, v; n   cin >> a >> b >> c >> d; n   m = max((3 * a) / 10, a - (a / 250) * c); n   v = max((3 * b) / 10, b - (b / 250) * d); n   if (m > v) n     cout <<  Misha ; n   else if (v > m) n     cout <<  Vasya ; n   else n     cout <<  Tie ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nn[2000000]; n int ans[2000000]; n string ss; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> nn[i]; n   int se = 0, en = n - 1; n   int noa = 0; n   ans[noa] = 0; n   while ((nn[se] > ans[noa] || nn[en] > ans[noa]) && en != se) { n     if (nn[se] > ans[noa] && nn[se] < nn[en]) { n       ans[++noa] = nn[se]; n       se++; n       ss.append( L ); n     } else if (nn[en] > ans[noa]) { n       ans[++noa] = nn[en]; n       en--; n       ss.append( R ); n     } else { n       ans[++noa] = nn[se]; n       se++; n       ss.append( L ); n     } n   } n   if (en == se) n     if (nn[se] > ans[noa]) { n       noa++; n       ss.append( L ); n     } n   cout << noa << endl; n   cout << ss << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 355001; n const int SQRTN = 320; n const int LOGN = 20; n const long double PI = acos(-1); n const long double TAU = 2 * PI; n int n, a[N], idx[N], last, m, an[N]; n long long ans; n signed main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     idx[a[i]] = i; n     an[i] = n + 1; n   } n   for (int j = 0, x, y; j < m; j++) { n     scanf( %d%d , &x, &y); n     if (idx[x] > idx[y]) swap(x, y); n     an[idx[x]] = min(an[idx[x]], idx[y]); n   } n   int x = n + 1; n   ans = -(n * 1ll * (n + 1)) >> 1; n   for (int i = n; i > 0; i--) x = min(x, an[i]), ans += x; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void propagate(int node, const map<int, set<int> >& idToChildren, n                set<int>& leaves) { n   map<int, set<int> >::const_iterator it = idToChildren.find(node); n   if (it == idToChildren.end()) { n     leaves.insert(node); n     return; n   } n   const set<int>& s = (*it).second; n   for (set<int>::const_iterator cit = s.begin(); cit != s.end(); ++cit) { n     propagate(*cit, idToChildren, leaves); n   } n } n int main() { n   int n; n   cin >> n; n   int f[n + 1]; n   char visited[n + 1]; n   char inLoop[n + 1]; n   int tag[n + 1]; n   char hasParent[n + 1]; n   char hasChild[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> f[i]; n     visited[i] = inLoop[i] = 0; n     tag[i] = i; n     hasParent[i] = hasChild[i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     if (visited[i]) continue; n     bool found = false; n     set<int> traversed; n     visited[i] = 1; n     traversed.insert(i); n     int x = i; n     int confluence = -1; n     while (1) { n       int xTemp = f[x]; n       if (inLoop[xTemp]) { n         f[x] = tag[xTemp]; n         break; n       } else if (traversed.find(xTemp) != traversed.end()) { n         found = true; n         confluence = xTemp; n         break; n       } else if (visited[xTemp]) { n         break; n       } else { n         x = xTemp; n         visited[x] = 1; n         traversed.insert(x); n       } n     } n     if (found) { n       int y = confluence; n       do { n         tag[y] = confluence; n         inLoop[y] = 1; n         y = f[y]; n       } while (y != confluence); n     } n   } n   map<int, set<int> > idToChildren; n   map<int, set<int> > rootToLeaves; n   for (int i = 1; i <= n; i++) { n     if (inLoop[i]) continue; n     hasParent[i] = hasChild[f[i]] = 1; n     idToChildren[f[i]].insert(i); n   } n   set<int> roots; n   set<int> leaves; n   for (int i = 1; i <= n; i++) { n     if (tag[i] != i) continue; n     if (!hasParent[i]) { n       roots.insert(i); n     } n     if (!hasChild[i]) { n       leaves.insert(i); n     } n   } n   for (set<int>::const_iterator it = roots.begin(); it != roots.end(); ++it) { n     propagate(*it, idToChildren, rootToLeaves[*it]); n   } n   if (leaves.size() == 1 && *leaves.begin() == *roots.begin()) { n     cout << 0 << endl; n     return 0; n   } n   cout << leaves.size() << endl; n   int firstRoot = *roots.begin(); n   int prevRoot = firstRoot; n   { n     set<int>::const_iterator it = roots.begin(); n     advance(it, 1); n     for (; it != roots.end(); ++it) { n       for (set<int>::const_iterator lit = rootToLeaves[*it].begin(); n            lit != rootToLeaves[*it].end(); ++lit) { n         cout << prevRoot <<     << *lit << endl; n       } n       prevRoot = *it; n     } n   } n   for (set<int>::const_iterator it = rootToLeaves[firstRoot].begin(); n        it != rootToLeaves[firstRoot].end(); ++it) { n     cout << prevRoot <<     << *it << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 600010, inf = N; n int n, m; n int head[N], cnt = 1; n struct edge { n   int to, next; n } e[N << 1]; n void adde(int a, int b) { n   e[++cnt].to = b; n   e[cnt].next = head[a]; n   head[a] = cnt; n } n int nxt[N], st[N], vis[N], top, mn, mx, in[N]; n void dfs(int k, int fa) { n   vis[k] = 1; n   st[++top] = k; n   for (int i = head[k]; i; i = e[i].next) { n     int v = e[i].to; n     if (v == fa) continue; n     if (!vis[v]) n       dfs(v, k); n     else if (!in[v]) { n       mx = 0; n       mn = inf; n       while (top) { n         mx = max(st[top], mx); n         mn = min(mn, st[top]); n         in[st[top]] = 1; n         top--; n         if (st[top + 1] == v) break; n       } n       nxt[mn] = min(nxt[mn], mx); n     } n   } n   if (!in[k]) top--; n } n long long sum[N]; n int main() { n   scanf( %d%d , &n, &m); n   int u, v, q; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     adde(u, v); n     adde(v, u); n   } n   for (int i = 1; i <= n + 1; i++) nxt[i] = n + 1; n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i, 0); n   for (int i = n; i; i--) nxt[i] = min(nxt[i + 1], nxt[i]); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nxt[i]; n   scanf( %d , &q); n   int L, R; n   while (q--) { n     scanf( %d%d , &L, &R); n     int l = L, r = R, res = L - 1; n     while (l <= r) { n       int mid = l + r >> 1; n       if (nxt[mid] > R) n         r = mid - 1; n       else n         l = mid + 1, res = mid; n     } n     printf( %lld n , 1ll * (R - res) * (R - res + 1) / 2 + sum[res] - n                          sum[L - 1] - 1ll * (res + L) * (res - L + 1) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s1, s2; n   cin >> s1 >> s2; n   int a, b, c, d; n   a = s1[0] -  0 ; n   b = s1[1] -  0 ; n   c = s2[1] -  0 ; n   d = s2[0] -  0 ; n   vector<int> v1({a, b, c, d}); n   map<vector<int>, int> mp; n   mp[{a, b, c, d}] = 1; n   n--; n   while (n--) { n     string s; n     cin >> s; n     cin >> s1 >> s2; n     a = s1[0] -  0 ; n     b = s1[1] -  0 ; n     c = s2[1] -  0 ; n     d = s2[0] -  0 ; n     vector<int> v1({a, b, c, d}); n     vector<int> v2({b, c, d, a}); n     vector<int> v3({c, d, a, b}); n     vector<int> v4({d, a, b, c}); n     if (mp.find({a, b, c, d}) != mp.end()) { n       continue; n     } else if (mp.find({b, c, d, a}) != mp.end()) { n       continue; n     } else if (mp.find({c, d, a, b}) != mp.end()) { n       continue; n     } else if (mp.find({d, a, b, c}) != mp.end()) { n       continue; n     } else n       mp[{a, b, c, d}] = 1; n   } n   cout << mp.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   long long t, j = 0; n   cin >> t; n   cin >> s; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  F ) n       if (s[i + 1] ==  S ) j--; n     if (s[i] ==  S ) n       if (s[i + 1] ==  F ) j++; n   } n   if (j <= 0) n     cout <<  NO ; n   else if (j > 0) n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mas[1010][1010]; n int mas1[1001][1001]; n bool check(int i, int j) { n   return mas[i][j] && mas[i + 1][j] && mas[i + 2][j] && mas[i + 2][j + 1] && n          mas[i + 2][j + 2] && mas[i + 1][j + 2] && mas[i][j + 2] && n          mas[i][j + 1]; n } n void draw(int i, int j) { n   mas1[i][j] = mas1[i + 1][j] = mas1[i + 2][j] = mas1[i + 2][j + 1] = n       mas1[i + 2][j + 2] = mas1[i + 1][j + 2] = mas1[i][j + 2] = n           mas1[i][j + 1] = 1; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       char temp; n       cin >> temp; n       if (temp ==  # ) n         mas[i][j] = 1; n       else n         mas[i][j] = 0; n     } n   for (int i = 1; i <= n - 2; ++i) n     for (int j = 1; j <= m - 2; ++j) { n       if (check(i, j)) draw(i, j); n     } n   bool f = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (mas[i][j] && !mas1[i][j]) f = 0; n     } n   if (f) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2004; n int pa[N]; n int n, m, x, y, op, pos = 1, ans = 0; n pair<int, int> p[N]; n bool b[N]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n * 2; i++) { n     scanf( %d , &p[i].first); n     p[i].second = i; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &x, &y); n     pa[x] = y; n     pa[y] = x; n   } n   sort(p + 1, p + 2 * n + 1, greater<pair<int, int> >()); n   scanf( %d , &op); n   for (int i = 1; i <= n; i++) { n     if (op == 1) { n       bool g = 0; n       for (int i = pos; i <= 2 * n; i++) n         if (!b[p[i].second] && pa[p[i].second] != 0) { n           ans = p[i].second; n           g = 1; n           break; n         } n       if (!g) { n         for (; pos <= 2 * n && b[p[pos].second]; ++pos) n           ; n         ans = p[pos].second; n       } n       b[ans] = 1; n       cout << ans << endl; n       scanf( %d , &x); n       b[x] = 1; n     } else { n       scanf( %d , &x); n       b[x] = 1; n       if (pa[x] == 0 || b[pa[x]]) { n         bool g = 0; n         for (int i = pos; i <= 2 * n; i++) n           if (!b[p[i].second] && pa[p[i].second] != 0) { n             ans = p[i].second; n             g = 1; n             break; n           } n         if (!g) { n           for (; pos <= 2 * n && b[p[pos].second]; ++pos) n             ; n           ans = p[pos].second; n         } n       } else { n         ans = pa[x]; n         b[pa[x]] = 1; n       } n       b[ans] = 1; n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> segs; n   int numNeg = 0; n   int curLen = 0; n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     if (a < 0) { n       if (numNeg == 2) { n         segs.push_back(curLen); n         curLen = 1; n         numNeg = 1; n       } else { n         numNeg += 1; n         curLen += 1; n       } n     } else { n       curLen += 1; n     } n   } n   if (curLen != 0) { n     segs.push_back(curLen); n   } n   printf( %d n , segs.size()); n   for (int s : segs) { n     printf( %d  , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000, INF = 2000000000; n struct edge { n   int x; n   int y; n   int w; n   int o; n }; n vector<vector<edge> > G(MAXN + 1); n int n, m, depth[MAXN + 1], low[MAXN + 1], s, t, previous[MAXN + 1], n     prevCost[MAXN + 1], cntEdges[MAXN + 1][MAXN + 1], c, sons[MAXN + 1], n     p1 = -1, p2; n bool vis[MAXN + 1]; n edge removedEdge = {0, 0, 0, -1}, e1, e2, prevEdge[MAXN + 1]; n vector<edge> edgesOnPath, currentPath, allEdges; n bool eq(edge a, edge b) { n   return a.w == b.w && min(a.x, a.y) == min(b.x, b.y) && n          max(a.x, a.y) == max(b.x, b.y); n } n void redo() { n   memset(prevCost, 0, sizeof(prevCost)); n   memset(previous, 0, sizeof(previous)); n   memset(vis, 0, sizeof(vis)); n   memset(depth, 0, sizeof(depth)); n   memset(low, 0, sizeof(low)); n   memset(sons, 0, sizeof(sons)); n   currentPath.clear(); n } n bool isBridge(edge e) { n   if (cntEdges[e.x][e.y] == 1) { n     if (low[e.x] > depth[e.y] || low[e.y] > depth[e.x]) return true; n   } else { n     if (cntEdges[e.x][e.y] != 2) return false; n     if (min(removedEdge.x, removedEdge.y) != min(e.x, e.y) || n         max(removedEdge.x, removedEdge.y) != max(e.x, e.y)) n       return false; n     if (low[e.x] > depth[e.y] || low[e.y] > depth[e.x]) return true; n   } n   return false; n } n int getEdgeOrder(edge e) { n   for (int i = 0; i < allEdges.size(); i++) n     if (eq(e, allEdges[i]) == true && (i + 1 != p1)) return i + 1; n } n void dfs(int node) { n   vis[node] = true; n   for (int i = 0; i < G[node].size(); i++) { n     int nextNode = G[node][i].y; n     if (G[node][i].o != removedEdge.o) { n       if (vis[nextNode] == false) { n         sons[node]++; n         depth[nextNode] = depth[node] + 1; n         low[nextNode] = depth[nextNode]; n         previous[nextNode] = node; n         prevCost[nextNode] = G[node][i].w; n         prevEdge[nextNode] = G[node][i]; n         dfs(nextNode); n         low[node] = min(low[node], low[nextNode]); n       } else { n         if (previous[node] != nextNode) n           low[node] = min(low[node], low[nextNode]); n       } n     } n   } n } n void findPath(int x, int y, vector<edge> &e) { n   int node = t; n   while (node != x) { n     e.push_back(prevEdge[node]); n     node = previous[node]; n   } n } n int answer = -1; n void solve() { n   if (m == 0) { n     cout << 0 << endl << 0; n     return; n   } n   dfs(s); n   if (vis[t] == false) { n     cout << 0 << endl << 0; n     return; n   } n   findPath(s, t, edgesOnPath); n   for (int i = 0; i < edgesOnPath.size(); i++) n     if (isBridge(edgesOnPath[i]) == true) { n       if (answer == -1 || answer > edgesOnPath[i].w) { n         answer = edgesOnPath[i].w; n         c = 1; n         e1 = edgesOnPath[i]; n       } n     } n   for (int i = 0; i < edgesOnPath.size(); i++) { n     removedEdge = edgesOnPath[i]; n     redo(); n     dfs(s); n     if (vis[t] == true) findPath(s, t, currentPath); n     for (int j = 0; j < currentPath.size(); j++) n       if (isBridge(currentPath[j]) == true) { n         if (answer == -1 || removedEdge.w + currentPath[j].w < answer) { n           answer = removedEdge.w + currentPath[j].w; n           c = 2; n           e1 = removedEdge; n           e2 = currentPath[j]; n         } n       } n   } n   cout << answer << endl; n   if (c != 0) cout << c << endl; n   if (c == 1) cout << e1.o; n   if (c == 2) { n     cout << e2.o <<     << e1.o; n   } n } n void readData() { n   scanf( %d %d %d %d , &n, &m, &s, &t); n   for (int i = 1; i <= m; i++) { n     int x, y, w; n     scanf( %d %d %d , &x, &y, &w); n     allEdges.push_back({min(x, y), max(x, y), w, i}); n     if (x != y) { n       cntEdges[x][y]++; n       cntEdges[y][x]++; n       G[x].push_back({x, y, w, i}); n       G[y].push_back({y, x, w, i}); n       G[x][G[x].size() - 1].o = i; n       G[y][G[y].size() - 1].o = i; n     } n   } n } n int main() { n   readData(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   long long p; n   cin >> p; n   long long keys[2001], people[1001]; n   for (int i = 0; i < n; i++) cin >> people[i]; n   for (int i = 0; i < k; i++) cin >> keys[i]; n   sort(keys, keys + k); n   sort(people, people + n); n   long long fin = 3e9; n   for (int i = 0; i + n <= k; i++) { n     long long ans = -1; n     for (int j = 0; j < n; j++) n       ans = max(ans, abs(people[j] - keys[i + j]) + abs(keys[i + j] - p)); n     fin = min(fin, ans); n   } n   cout << fin << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100100], d[100100]; n int main() { n   cin >> n; n   int mn = 1000111222, mx = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     mn = min(mn, a[i]); n     mx = max(mx, a[i]); n   } n   for (int i = 0; i < n; i++) { n     a[i] -= mn; n     if (a[i] > n) { n       puts( NO ); n       return 0; n     } n     d[a[i]]++; n   } n   mx -= mn; n   d[0]--; n   d[mx]--; n   for (int i = 1; i <= mx - 1; i++) n     if (d[i] < 2) { n       puts( NO ); n       return 0; n     } else n       d[i] -= 2; n   for (int i = 0; i <= mx; i++) n     if (d[i]) { n       if (d[i] > d[i + 1]) { n         puts( NO ); n         return 0; n       } n       d[i + 1] -= d[i]; n     } n   puts( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n void read() { freopen( input.txt ,  r , stdin); } n bool check(string s) { n   long long ans = s[0] -  0 ; n   ans = ans * 10ll + (s[1] -  0 ); n   if (ans % 4 == 0) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   long long ans = 0; n   for (long long i = 0; i < (long long)s.size(); i++) { n     if ((s[i] -  0 ) % 4 == 0) ans++; n   } n   string temp =   ; n   for (long long i = 0; i < (long long)s.size() - 1; i++) { n     temp += s.substr(i, 2); n     if (check(temp)) { n       ans += i + 1; n     } n     temp =   ; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n inline long long quick(long long mul, long long b) { n   long long c = 1; n   while (b) { n     if (b & 1) { n       c = c * mul % mod; n     } n     mul = mul * mul % mod; n     b /= 2; n   } n   return c; n } n int main() { n   long long n, m; n   cin >> n >> m; n   cout << (quick(2 * n + 2, m) * (n - m + 1) % mod) * quick(n + 1, mod - 2) % n               mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace FGF { n int n, m; n int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) w = -w; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     s = s * 10 + ch -  0 ; n     ch = getchar(); n   } n   return s * w; n } n const int N = 1e5 + 5; n const int mo = 1e9 + 7; n char s[N]; n int num[60]; n inline int getnum(char c) { return c >=  a  ? c -  a  + 1 : c -  A  + 27; } n long long f[N], fac[N], inv[N], g[N], h[N], ans[60][60]; n void init() { n   fac[0] = inv[0] = inv[1] = 1; n   for (int i = 1; i <= n / 2; i++) fac[i] = fac[i - 1] * i % mo; n   for (int i = 2; i <= n / 2; i++) inv[i] = inv[mo % i] * (mo - mo / i) % mo; n   for (int i = 2; i <= n / 2; i++) inv[i] = inv[i - 1] * inv[i] % mo; n } n void work() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; i++) num[getnum(s[i])]++; n   init(); n   f[0] = 1; n   for (int i = 1; i <= 52; i++) n     for (int j = n / 2; j >= num[i]; j--) n       if (num[i]) f[j] = (f[j] + f[j - num[i]]) % mo; n   for (int i = 1; i <= 52; i++) n     if (num[i]) { n       ans[i][i] = f[n / 2]; n       for (int j = 0; j <= n / 2; j++) n         if (j >= num[i]) n           g[j] = (f[j] - g[j - num[i]] + mo) % mo; n         else n           g[j] = f[j]; n       for (int j = i + 1; j <= 52; j++) n         if (num[j]) { n           for (int k = 0; k <= n / 2; k++) n             if (k >= num[j]) n               h[k] = (g[k] - h[k - num[j]] + mo) % mo; n             else n               h[k] = g[k]; n           ans[j][i] = ans[i][j] = h[n / 2] * 2 % mo; n         } n     } n   long long res = fac[n / 2] * fac[n / 2] % mo; n   for (int i = 1; i <= 52; i++) res = res * inv[num[i]] % mo; n   m = read(); n   while (m--) { n     int x = read(), y = read(); n     printf( %lld n , res * ans[getnum(s[x])][getnum(s[y])] % mo); n   } n } n }  // namespace FGF n int main() { n   FGF::work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = (int)1e5 + 55; n const int INF = (int)1e9 + 77; n const int MOD = (int)1e9 + 7; n const double PI = 2 * acos(0.0); n const double EPS = 1e-7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   string s; n   cin >> n >> m >> s; n   for (int i = 0; i < m; ++i) { n     int l, r; n     char ch1, ch2; n     cin >> l >> r >> ch1 >> ch2; n     --l; n     --r; n     for (int j = l; j <= r; ++j) { n       if (s[j] == ch1) s[j] = ch2; n     } n   } n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int t; n   edge *next; n }; n struct obj { n   int v, a, b; n   obj() {} n   obj(int a0, int b0, int v0) { v = v0, a = a0, b = b0; } n }; n inline bool cmpObj(obj a, obj b) { return a.v < b.v; } n const int maxn = 1010; n const int maxm = 4010; n obj o[maxm]; n edge *ep, *head[maxn]; n int n, m, v[maxn], vis[maxn], vo, s; n void addEdge(int u, int v) { n   ep->t = v; n   ep->next = head[u]; n   head[u] = ep++; n } n bool reach(int st, int te) { n   queue<int> q; n   q.push(st); n   vis[st] = vo; n   while (!q.empty() && vis[te] != vo) { n     int p = q.front(); n     q.pop(); n     for (edge *g = head[p]; g; g = g->next) n       if (vis[g->t] != vo) { n         vis[g->t] = vo; n         q.push(g->t); n       } n   } n   return vis[te] == (vo++); n } n int main() { n   memset(vis, -1, sizeof(vis)); n   vo = 0; n   ep = new edge[maxm]; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , v + i); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     o[i << 1] = obj(a, b, v[b]); n     o[(i << 1) + 1] = obj(b, a, v[a]); n   } n   sort(o, o + m * 2, cmpObj); n   s = 0; n   for (int i = 0; i < m * 2; i++) n     if (!reach(o[i].b, o[i].a)) { n       s += o[i].v; n       addEdge(o[i].a, o[i].b); n     } n   printf( %d n , s); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string in; n   cin >> in; n   int sz = in.size(); n   long long max = -1; n   for (int i = 1; i <= sz - 2; i++) { n     for (int j = 1; j <= sz - 1 - i; j++) { n       long long x, y, z; n       { n         stringstream ss(in.substr(0, i)); n         ss >> x; n       } n       { n         stringstream ss; n         ss << in.substr(i, j); n         ss >> y; n       } n       { n         stringstream ss; n         ss << in.substr(i + j, sz - i - j); n         ss >> z; n       } n       if ((in.substr(0, i)[0] ==  0  && i > 1) || n           (in.substr(i, j)[0] ==  0  && j > 1) || n           (in.substr(i + j, sz - i - j)[0] ==  0  && sz - i - j > 1)) n         continue; n       if (x <= 1000000 && y <= 1000000 && z <= 1000000 && x + y + z > max) n         max = x + y + z; n     } n   } n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (1e5) + 15; n const int mod = (1e9) + 7; n const int inf = (1e9) + 3; n long long int n, k, ans; n string s; n long long int a[maxn]; n int main() { n   ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   ; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     if (a[i] + a[i - 1] < k) n       ans += (k - a[i] - a[i - 1]), a[i] += (k - a[i] - a[i - 1]); n   } n   cout << ans << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n #define F first n #define S second n const int MXN = 200040; n int cnt[MXN]; n bool b[MXN]; n  n ll n,d; n int main(){ n  n     int t; n     cin >> t; n     while(t--){ n          n         string s; n         cin >> s; n         int n = s.size(); n         int ans = 0; n         bool b[n+3]; n         for(int i = 0 ; i < n+3 ; i++){ n             b[i] = 0; n         } n         for(int i = 1 ; i < n ; i++){ n             if(i == 1){ n                 if(s[i] == s[i-1]){ n                     b[i] = 1; n                     ans++; n                 } n                 continue; n             } n             if(s[i] != s[i-1] && s[i] != s[i-2])continue; n             if(s[i] == s[i-1] && s[i] == s[i-2]){ n                 if(b[i-1] && b[i-2])continue; n                 b[i] = 1; n                 ans++; n                 continue; n             } n             if(s[i] == s[i-1] && s[i] != s[i-2]){ n                 if(b[i-1])continue; n                 b[i] = 1; n                 ans++; n                 continue; n             } n             if(s[i] == s[i-2] && s[i] != s[i-1]){ n                 if(b[i-2])continue; n                 b[i] = 1; n                 ans++; n                 continue; n             } n         } n          n         cout << ans <<   n ; n          n     } n  n }


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int N = 1e6 + 5; n long long n, pw[N]; n int t = 3; n void Solve() { n   pw[n + 1] = 1; n   for (long long i = n; i >= 3; i--) pw[i] = (pw[i + 1] * i) % mod; n   long long res = 0; n   res = (pw[3] * (n - 1)) % mod; n   for (long long i = n - 2; i >= 1; i--) { n     res = res + ((pw[t + 1] * (t - 1)) % mod * i) % mod; n     t++; n   } n   cout << (res + 1) % mod <<   n ; n } n int main() { n   cin >> n; n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef unsigned int uint; n #define endl   n  n #define lowbit(x) (x & -x) n #define ls(x) (x << 1) n #define rs(x) (x << 1 | 1) n #define ck(x) (x >= mod ? x - mod : x) n #define inf 0x3f3f3f3f n #define eps 1e-6 n #define maxn 300200 n #define maxm 10000100 n #define G 3 n #define invG 332748118 n const int mod = 1e9 + 7; n const double PI = 4 * atan2(1.0, 1.0); n int t, n, dis1[maxn], dis2[maxn], dis3[maxn], dis4[maxn]; n char s1[maxn], s2[maxn]; n signed main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     cout.tie(nullptr); n     cin >> t; n     while (t--) { n         cin >> n; n         cin >> s1 + 1; n         for (int i = 1; i <= n; i++) { n             s2[i] = s1[i] ==  L  ?  R  :  L ; n         } n         for (int i = 0; i <= n; i++) { n             dis1[i] = dis2[i] = dis3[i] = dis4[i] = 0; n         } n         dis1[0] = dis2[0] = 0; n         for (int i = 1; i <= n; i++) { n             if (s1[i] ==  L ) dis1[i] = dis2[i - 1] + 1; n             if (s2[i] ==  L ) dis2[i] = dis1[i - 1] + 1; n         } n         dis3[n] = dis4[n] = 0; n         for (int i = n; i >= 1; i--) { n             if (s1[i] ==  R ) dis3[i - 1] = dis4[i] + 1; n             if (s2[i] ==  R ) dis4[i - 1] = dis3[i] + 1; n         } n         for (int i = 0; i <= n; i++) { n             cout << dis1[i] + dis3[i] + 1 <<    ; n         } n         cout << endl; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n using namespace rel_ops; n using ll = int64_t; n using Pii = pair<int, int>; n using ull = uint64_t; n using Vi = vector<int>; n void run(); n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(10); n   run(); n   return 0; n } n int uplg(int n) { return 32 - __builtin_clz(n); } n int uplg(ll n) { return 64 - __builtin_clzll(n); } n int cmp(double a, double b, double eps = 1e-9) { n   return (a > b + eps) - (a + eps < b); n } n char board[2001][2001]; n int n, k; n int dp[2001][2001]; n int lex[2001][2001]; n bool toRight[2001][2001]; n void run() { n   cin >> n >> k; n   for (int i = (0); i < (n); i++) cin >> board[i]; n   for (int i = (0); i < (n); i++) n     for (int j = (0); j < (n); j++) { n       if (i == 0 && j == 0) { n         dp[i][j] = 0; n       } else { n         dp[i][j] = INT_MAX; n       } n       if (i > 0) { n         dp[i][j] = min(dp[i][j], dp[i - 1][j]); n       } n       if (j > 0) { n         dp[i][j] = min(dp[i][j], dp[i][j - 1]); n       } n       if (board[i][j] !=  a ) { n         dp[i][j]++; n       } n     } n   if (dp[n - 1][n - 1] <= k) { n     for (int i = (0); i < (n * 2 - 1); i++) cout <<  a ; n     cout << endl; n     return; n   } n   lex[n][n] = 0; n   Pii from{0, 0}; n   for (int sum = n * 2 - 1; sum >= 0; sum--) { n     vector<pair<Pii, int>> pairs; n     int best = INT_MAX; n     Pii bestPos = {-1, -1}; n     for (int i = (0); i < (n); i++) { n       int j = sum - i; n       if (j < 0 || j >= n) continue; n       int better = INT_MAX; n       if (j + 1 < n) better = min(better, lex[i][j + 1]); n       if (i + 1 < n) { n         better = min(better, lex[i + 1][j]); n         if (better == lex[i + 1][j]) toRight[i][j] = 1; n       } n       pairs.push_back({{board[i][j], better}, i}); n       if (dp[i][j] <= k) { n         if (better < best) { n           best = better; n           bestPos = {i, j}; n         } n       } n     } n     if (best < INT_MAX) { n       from = bestPos; n       if (toRight[bestPos.first][bestPos.second]) n         from.first++; n       else n         from.second++; n       break; n     } n     sort((pairs).begin(), (pairs).end()); n     Pii last = {-1, -1}; n     int ind = 0; n     for (auto& p : (pairs)) { n       if (p.first != last) { n         ind++; n         last = p.first; n       } n       lex[p.second][sum - p.second] = ind; n     } n   } n   for (int i = (0); i < (from.first + from.second); i++) cout <<  a ; n   while (from.first != n - 1 || from.second != n - 1) { n     cout << board[from.first][from.second]; n     if (toRight[from.first][from.second]) n       from.first++; n     else n       from.second++; n   } n   cout << board[n - 1][n - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l; n double d; n double arr[1001]; n int main() { n   cin >> n >> l; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   d = 0; n   for (int i = 0; i < n - 1; i++) d = max(d, arr[i + 1] - arr[i]); n   cout << fixed << setprecision(9) << max(d / 2, max(arr[0], l - arr[n - 1])); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n > 36) n     puts( -1 ); n   else { n     int x = n / 2; n     if (n & 1) printf( 4 ); n     while (x--) printf( 8 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n const int N = 1005; n vector<pair<int, int>> graph[N]; n vector<int> ans; n int vis[N]; n int v[N]; n void dfs(int pos) { n   vis[pos] = 1; n   v[pos] = 1; n   for (auto& e : graph[pos]) { n     if (vis[e.first] == 0) { n       if (e.second == 0) { n         int c = max(ans[e.first], ans[pos]); n         ans[e.first] = c; n         ans[pos] = c; n       } else { n         if (ans[e.first] <= ans[pos]) { n           ans[e.first] = ans[pos] + 1; n         } n       } n       dfs(e.first); n     } n   } n   vis[pos] = 0; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     int n; n     cin >> n; n     string str; n     cin >> str; n     int cnt = 1; n     for (int i = 0; i < n; i++) { n       ans.push_back(1); n     } n     for (int i = 0; i < str.length(); i++) { n       if (str[i] ==  = ) { n         graph[cnt].push_back({cnt - 1, 0}); n         graph[cnt - 1].push_back({cnt, 0}); n       } else if (str[i] ==  L ) { n         graph[cnt].push_back({cnt - 1, 1}); n       } else { n         graph[cnt - 1].push_back({cnt, 1}); n       } n       cnt++; n     } n     for (int i = 0; i < n; i++) { n       if (v[i] == 0) { n         dfs(i); n       } n     } n     for (auto& e : ans) { n       cout << e <<    ; n     } n     cout << endl; n   } n   return 0; n } n