#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) { n     if (ch ==  - ) f = -1; n   } n   for (; isdigit(ch); ch = getchar()) { n     x = x * 10 + ch - 48; n   } n   return x * f; n } n const int mxN = 1e5; n const int P = 998244353; n struct Element { n   long long x, v, p; n } a[mxN + 3]; n struct Event { n   int u, x, y; n   long long t1, t2; n   bool operator<(const Event &arg) const { return t1 * arg.t2 < t2 * arg.t1; } n } ev[mxN * 2 + 3]; n int f[mxN + 3][2][2]; n int n, m; n long long quickpow(long long x, long long y) { n   long long cur = x, ret = 1ll; n   for (int i = 0; y; i++) { n     if (y & (1ll << i)) { n       y -= (1ll << i); n       ret = ret * cur % P; n     } n     cur = cur * cur % P; n   } n   return ret; n } n long long mulinv(long long x) { return quickpow(x, P - 2); } n struct SgTNode { n   long long x[2][2]; n } sgt[mxN * 4 + 3]; n void pushup(int u, int mid) { n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) sgt[u].x[i][j] = 0ll; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) n       if (!f[mid][i][j]) { n         for (int ii = 0; ii < 2; ii++) n           for (int jj = 0; jj < 2; jj++) { n             sgt[u].x[ii][jj] += sgt[u << 1].x[ii][i] * sgt[u << 1 | 1].x[j][jj]; n           } n       } n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) { n       sgt[u].x[i][j] %= P; n     } n } n void build(int u, int le, int ri) { n   if (le == ri) { n     sgt[u].x[0][0] = P + 1 - a[le].p, sgt[u].x[1][1] = a[le].p; n     return; n   } n   int mid = (le + ri) >> 1; n   build(u << 1, le, mid); n   build(u << 1 | 1, mid + 1, ri); n   pushup(u, mid); n } n void update(int u, int le, int ri, int pos) { n   if (le == ri) { n     return; n   } n   int mid = (le + ri) >> 1; n   if (pos <= mid) { n     update(u << 1, le, mid, pos); n   } else { n     update(u << 1 | 1, mid + 1, ri, pos); n   } n   pushup(u, mid); n } n long long query() { n   return (sgt[1].x[0][0] + sgt[1].x[0][1] + sgt[1].x[1][0] + sgt[1].x[1][1]) % n          P; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d%I64d%I64d , &a[i].x, &a[i].v, &a[i].p); n     a[i].p = a[i].p * mulinv(100) % P; n   } n   for (int i = 1; i < n; i++) { n     ev[++m].u = i, ev[m].x = 1, ev[m].y = 0, ev[m].t1 = a[i + 1].x - a[i].x, n     ev[m].t2 = a[i].v + a[i + 1].v; n     if (a[i].v < a[i + 1].v) { n       ev[++m].u = i, ev[m].x = 0, ev[m].y = 0, ev[m].t1 = a[i + 1].x - a[i].x, n       ev[m].t2 = a[i + 1].v - a[i].v; n     } else if (a[i].v > a[i + 1].v) { n       ev[++m].u = i, ev[m].x = 1, ev[m].y = 1, ev[m].t1 = a[i + 1].x - a[i].x, n       ev[m].t2 = a[i].v - a[i + 1].v; n     } n   } n   sort(ev + 1, ev + m + 1); n   long long ans = 0ll, prob = 1ll; n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     f[ev[i].u][ev[i].x][ev[i].y] = 1; n     update(1, 1, n, ev[i].u); n     long long tmp = query(), tim = ev[i].t1 * mulinv(ev[i].t2) % P; n     ans = (ans + (prob - tmp + P) * tim) % P; n     prob = tmp; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gi() { n   T x = 0; n   bool f = 0; n   char c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) f = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return f ? -x : x; n } n const int N = 2e5 + 10; n struct node { n   int to, nxt, w; n } g[N << 1]; n int lst[N], gl, w[N]; n void add(int z, int y, int x) { n   g[++gl] = (node){y, lst[x], z}; n   lst[x] = gl; n   g[++gl] = (node){x, lst[y], z}; n   lst[y] = gl; n } n int ans1; n double ans2; n int siz[N], mx, rt, sum; n bool vis[N]; n void getroot(int u, int ff) { n   siz[u] = 1; n   int s = 0; n   for (int i = lst[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == ff || vis[v]) continue; n     getroot(v, u); n     siz[u] += siz[v]; n     s = max(s, siz[v]); n   } n   s = max(s, sum - siz[u]); n   if (s < mx) mx = s, rt = u; n } n double sd[N], sumd, ans; n void dfs(int u, int ff, int tp, int dep) { n   ans += pow(dep, 1.5) * w[u]; n   sumd += 1.5 * sqrt(dep) * w[u]; n   sd[tp] += 1.5 * sqrt(dep) * w[u]; n   for (int i = lst[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == ff) continue; n     dfs(v, u, tp, dep + g[i].w); n   } n } n void solve(int u) { n   vis[u] = 1; n   ans = sumd = 0; n   for (int i = lst[u]; i; i = g[i].nxt) n     sd[g[i].to] = 0, dfs(g[i].to, u, g[i].to, g[i].w); n   if (ans2 > ans) ans1 = u, ans2 = ans; n   for (int i = lst[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (vis[v]) continue; n     if (sumd - sd[v] > sd[v]) continue; n     mx = sum = siz[v]; n     getroot(v, u); n     solve(rt); n   } n } n int main() { n   int n = gi<int>(); n   for (int i = 1; i <= n; i++) w[i] = gi<int>(); n   for (int i = 1; i < n; i++) add(gi<int>(), gi<int>(), gi<int>()); n   ans2 = 1e20; n   mx = sum = n; n   getroot(1, 0); n   solve(rt); n   printf( %d %.10lf n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i; n   double sum = 0, num, av, av1; n   cin >> n >> k; n   int a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     sum = sum + a[i]; n   } n   av1 = sum / n; n   if (av1 >= (k - .5)) { n     cout << 0 << endl; n     return 0; n   } else { n     for (i = 1;; i++) { n       sum = sum + k; n       num = n + i; n       av = sum / num; n       if (av >= (k - 0.5)) { n         cout << i << endl; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n namespace Mymath { n long long qp(long long x, long long p, long long mod) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = ans * x % mod; n     x = x * x % mod; n     p >>= 1; n   } n   return ans; n } n long long inv(long long x, long long mod) { return qp(x, mod - 2, mod); } n long long C(long long N, long long K, long long fact[], long long mod) { n   return fact[N] * inv(fact[K], mod) % mod * inv(fact[N - K], mod) % mod; n } n template <typename Tp> n Tp gcd(Tp A, Tp B) { n   if (B == 0) return A; n   return gcd(B, A % B); n } n template <typename Tp> n Tp lcm(Tp A, Tp B) { n   return A * B / gcd(A, B); n } n };  // namespace Mymath n namespace fwt { n using namespace Mymath; n void FWT(int a[], int n, long long mod) { n   for (int d = 1; d < n; d <<= 1) n     for (int m = d << 1, i = 0; i < n; i += m) n       for (int j = 0; j < d; j++) { n         int x = a[i + j], y = a[i + j + d]; n         a[i + j] = (x + y) % mod, a[i + j + d] = (x - y + mod) % mod; n       } n } n void UFWT(int a[], int n, long long mod) { n   long long rev = inv(2, mod); n   for (int d = 1; d < n; d <<= 1) n     for (int m = d << 1, i = 0; i < n; i += m) n       for (int j = 0; j < d; j++) { n         int x = a[i + j], y = a[i + j + d]; n         a[i + j] = 1LL * (x + y) * rev % mod, n               a[i + j + d] = (1LL * (x - y) * rev % mod + mod) % mod; n       } n } n void solve(int a[], int b[], int n, long long mod) { n   FWT(a, n, mod); n   FWT(b, n, mod); n   for (int i = 0; i < n; i++) a[i] = 1LL * a[i] * b[i] % mod; n   UFWT(a, n, mod); n } n };  // namespace fwt n namespace Maxflow { n const int Maxn = 1005; n struct edge { n   int to, rev, cap; n   edge(int _to, int _rev, int _cap) { to = _to, rev = _rev, cap = _cap; } n }; n int src = 1002, snk = 1003; n vector<edge> G[Maxn]; n int dist[Maxn], seen[Maxn]; n int n; n void Clear() { n   for (int i = 0; i < Maxn; i++) G[i].clear(); n } n void bfs(int s) { n   for (int i = 0; i < Maxn; i++) dist[i] = -1; n   dist[s] = 0; n   queue<int> Q; n   Q.push(s); n   while (!Q.empty()) { n     int x = Q.front(); n     Q.pop(); n     for (int i = 0; i < G[x].size(); i++) { n       edge &e = G[x][i]; n       if (e.cap && dist[e.to] == -1) { n         dist[e.to] = dist[x] + 1; n         Q.push(e.to); n       } n     } n   } n } n int dfs(int now, int dest, int fl) { n   if (now == dest) return fl; n   for (int &i = seen[now]; i < G[now].size(); i++) { n     edge &ed = G[now][i]; n     if (ed.cap && dist[ed.to] == dist[now] + 1) { n       int f = dfs(ed.to, dest, min(fl, ed.cap)); n       if (f) { n         ed.cap -= f; n         G[ed.to][ed.rev].cap += f; n         return f; n       } n     } n   } n   return 0; n } n int dinic(int s = src, int t = snk) { n   int ret = 0; n   while (1) { n     memset(seen, 0, sizeof(seen)); n     bfs(s); n     if (dist[t] == -1) return ret; n     int f = dfs(s, t, 2147483647); n     while (f) { n       ret += f; n       f = dfs(s, t, 2147483647); n     } n   } n } n };  // namespace Maxflow n namespace Geometry { n struct iP { n   long long x, y; n   iP(long long _x, long long _y) { x = _x, y = _y; } n   iP() { x = y = 0; } n   iP operator+(iP p) { return iP(x + p.x, y + p.y); } n   iP operator-(iP p) { return iP(x - p.x, y - p.y); } n   iP operator*(long long k) { return iP(x * k, y * k); } n   long long det(iP p) { return x * p.y - y * p.x; } n   long long dot(iP p) { return x * p.x + y * p.y; } n   long long dist() { return x * x + y * y; } n   bool operator<(const iP &p) const { n     if (x != p.x) return x < p.x; n     return y < p.y; n   } n }; n struct dP { n   double x, y; n   dP(double _x, double _y) { x = _x, y = _y; } n   dP() { x = y = 0.0; } n   dP operator+(dP p) { return dP(x + p.x, y + p.y); } n   dP operator-(dP p) { return dP(x - p.x, y - p.y); } n   dP operator*(double k) { return dP(x * k, y * k); } n   double det(dP p) { return x * p.y - y * p.x; } n   double dot(dP p) { return x * p.x + y * p.y; } n   double dist() { return x * x + y * y; } n   bool operator<(const dP &p) const { n     if (x != p.x) return x < p.x; n     return y < p.y; n   } n }; n vector<iP> convex_hull(iP X[], int l) { n   vector<iP> ret; n   sort(X + 1, X + 1 + l); n   ret.push_back(X[1]); n   for (int i = 2; i <= l; i++) { n     while (ret.size() >= 2 && n            (ret[ret.size() - 2] - X[i]).det(ret[ret.size() - 1] - X[i]) >= 0) { n       ret.pop_back(); n     } n     ret.push_back(X[i]); n   } n   int rs = ret.size() + 1; n   for (int i = l - 1; i >= 1; i--) { n     while (ret.size() >= rs && n            (ret[ret.size() - 2] - X[i]).det(ret[ret.size() - 1] - X[i]) >= 0) { n       ret.pop_back(); n     } n     ret.push_back(X[i]); n   } n   return ret; n } n };  // namespace Geometry n const int Maxn = 2e5 + 5; n const int Maxm = 5500005; n int n, q; n int a[Maxn]; n bool isp[Maxm]; n vector<int> P; n vector<int> p[Maxn]; n void _init() { n   memset(isp, true, sizeof(isp)); n   isp[0] = isp[1] = false; n   for (int i = 2; i < Maxm; i++) { n     if (isp[i]) { n       P.push_back(i); n       for (int j = i + i; j < Maxm; j += i) { n         isp[j] = false; n       } n     } n   } n } n long long Ha(vector<int> &V) { n   long long ret = 12384722; n   for (int i = 0; i < V.size(); i++) { n     ret = ret * 94333337 + V[i]; n   } n   return ret; n } n vector<long long> H[Maxn]; n struct node { n   vector<pair<int, int> > st; n   long long hh; n   node *nxt; n   node(long long _hh) { n     hh = _hh; n     nxt = NULL; n     st.clear(); n   } n }; n const int ss = 5000000; n node *Mp[ss]; n void Add(long long HH, int Siz, int Pos) { n   long long pp = HH % (long long)ss; n   if (pp < 0) pp += ss; n   if (Mp[pp] == NULL) { n     Mp[pp] = new node(HH); n     Mp[pp]->st.push_back(make_pair(Siz, Pos)); n     return; n   } n   node *tmp = Mp[pp]; n   bool Find = false; n   while (tmp) { n     if (tmp->hh == HH) { n       Find = true; n       break; n     } n     if (tmp->nxt == NULL) break; n     tmp = tmp->nxt; n   } n   if (Find) { n     while (!tmp->st.empty() && tmp->st.back().first >= Siz) { n       tmp->st.pop_back(); n     } n     tmp->st.push_back(make_pair(Siz, Pos)); n     return; n   } n   tmp->nxt = new node(HH); n   tmp->nxt->st.push_back(make_pair(Siz, Pos)); n } n vector<pair<int, int> > Get(long long HH) { n   long long pp = HH % ss; n   if (pp < 0) pp += ss; n   vector<pair<int, int> > ept; n   if (!Mp[pp]) return ept; n   node *tmp = Mp[pp]; n   while (tmp) { n     if (tmp->hh == HH) return tmp->st; n     if (tmp->nxt == NULL) return ept; n     tmp = tmp->nxt; n   } n   return ept; n } n void Ckm(int &x, int y) { n   if (y < x) x = y; n } n int imp[Maxn][19]; n vector<pair<int, int> > Q[Maxn]; n int ans[2000000]; n int tree[Maxn * 4], tag[Maxn * 4]; n void Push(int p) { n   tag[p * 2] = min(tag[p * 2], tag[p]); n   tag[p * 2 + 1] = min(tag[p * 2 + 1], tag[p]); n   tree[p * 2] = min(tree[p * 2], tag[p]); n   tree[p * 2 + 1] = min(tree[p * 2 + 1], tag[p]); n   tag[p] = 1e9; n } n void modify(int p, int l, int r, int lo, int hi, int val) { n   if (lo > hi) return; n   if (hi > r) hi = r; n   if (lo <= l && r <= hi) { n     tag[p] = min(tag[p], val); n     tree[p] = min(tree[p], val); n     return; n   } n   Push(p); n   int mid = l + r >> 1; n   if (lo <= mid) { n     modify(p * 2, l, mid, lo, min(hi, mid), val); n   } n   if (hi > mid) { n     modify(p * 2 + 1, mid + 1, r, max(lo, mid + 1), hi, val); n   } n   tree[p] = min(tree[p * 2], tree[p * 2 + 1]); n } n int query(int p, int l, int r, int lo, int hi) { n   if (lo > hi) return 1e9; n   if (lo <= l && r <= hi) { n     return tree[p]; n   } n   Push(p); n   int mid = l + r >> 1; n   int ans = 1e9; n   if (lo <= mid) { n     ans = min(ans, query(p * 2, l, mid, lo, min(hi, mid))); n   } n   if (hi > mid) { n     ans = min(ans, query(p * 2 + 1, mid + 1, r, max(lo, mid + 1), hi)); n   } n   tree[p] = min(tree[p * 2], tree[p * 2 + 1]); n   return ans; n } n int main() { n   _init(); n   n = read(); n   q = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     int tmp = a[i]; n     for (int j = 0; j < P.size(); j++) { n       if (P[j] * P[j] > tmp) break; n       int f = 0; n       while (tmp % P[j] == 0) { n         tmp /= P[j]; n         f ^= 1; n       } n       if (f) p[i].push_back(P[j]); n     } n     if (tmp != 1) p[i].push_back(tmp); n     int s = p[i].size(); n     vector<int> T; n     for (int j = 0; j < (1 << s); j++) { n       T.clear(); n       for (int k = 0; k < s; k++) { n         if (j >> k & 1) T.push_back(p[i][k]); n       } n       H[i].push_back(Ha(T)); n     } n   } n   for (int i = n; i >= 1; i--) { n     int s = p[i].size(); n     vector<int> T; n     for (int j = 0; j < 16; j++) imp[i][j] = 1e9; n     for (int j = 0; j < (1 << s); j++) { n       int vv = __builtin_popcount(j); n       vector<pair<int, int> > GG = Get(H[i][j]); n       for (int k = 0; k < GG.size(); k++) { n         Ckm(imp[i][s - vv * 2 + GG[k].first], GG[k].second); n       } n     } n     for (int j = 0; j < H[i].size(); j++) { n       Add(H[i][j], s, i); n     } n     for (int j = 1; j < 16; j++) { n       imp[i][j] = min(imp[i][j], imp[i][j - 1]); n     } n   } n   for (int i = 0; i < q; i++) { n     int l, r; n     l = read(); n     r = read(); n     Q[l].push_back(make_pair(r, i)); n   } n   for (int i = 0; i < 4 * Maxn; i++) { n     tag[i] = 1e9; n     tree[i] = 1e9; n   } n   for (int i = n; i >= 1; i--) { n     if (i != n) { n       int lst = i + 1; n       for (int j = 14; j >= 0; j--) { n         if (imp[i][j] == imp[i][j + 1]) continue; n         modify(1, 1, n, lst, imp[i][j] - 1, j + 1); n         lst = imp[i][j]; n       } n       modify(1, 1, n, lst, n, 0); n     } n     for (int j = 0; j < Q[i].size(); j++) { n       int id = Q[i][j].second, r = Q[i][j].first; n       ans[id] = query(1, 1, n, i + 1, r); n     } n   } n   for (int i = 0; i < q; i++) { n     printf( %d n , ans[i]); n   } n   return ~~(0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> k; n     sum += (int)abs(k); n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   bool check = false; n   int left, right; n   cin >> left >> right; n   for (int i = left; i <= right; i++) { n     int num = i; n     bool ch[10] = {false}; n     while (num > 0) { n       if (ch[num % 10]) break; n       ch[num % 10] = true; n       num /= 10; n     } n     if (num == 0) { n       cout << i; n       check = true; n       break; n     } n   } n   if (!check) cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char *name, Arg1 &&arg1) { n   cerr << name <<   :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char *names, Arg1 &&arg1, Args &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n using ll = long long; n using P = pair<int, int>; n using vi = vector<int>; n const char nl =   n ; n const int inf = 2e9; n const ll INF = (ll)2e18; n const ll mod = 1e9 + 7; n const int N = 2e6 + 15; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, x; n   cin >> n; n   vi a(n), pos(n + 1); n   vi bucket(n, 0); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) { n     cin >> x; n     pos[x] = i; n   } n   for (int i = 0; i < n; ++i) { n     if (pos[a[i]] <= i) { n       bucket[i - pos[a[i]]]++; n     } else { n       bucket[i + n - pos[a[i]]]++; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     ans = max(ans, bucket[i]); n   } n   cout << ans << nl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using minpq = priority_queue<T, vector<T>, greater<T>>; n long long pl, pr, vl, vr; n int k; n long long inter(long long a, long long b, long long c, long long d) { n   return max(0LL, min(b, d) - max(a, c) + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> pl >> pr >> vl >> vr >> k; n   long double ans = 0; n   vector<long long> lucky; n   for (int len = (0); len < (11); len++) { n     for (int x = (0); x < ((1 << len)); x++) { n       long long y = 0; n       for (int i = (0); i < (len); i++) { n         if ((x >> i) & 1) { n           y = y * 10 + 4; n         } else { n           y = y * 10 + 7; n         } n       } n       lucky.push_back(y); n     } n   } n   sort((lucky).begin(), (lucky).end()); n   int n = ((int)(lucky).size()); n   for (int i = (1); i < (n - k); i++) { n     int j = i + k; n     ans += 1.0 * inter(pl, pr, lucky[i - 1] + 1, lucky[i]) * n            inter(vl, vr, lucky[j - 1], lucky[j] - 1); n     ans += 1.0 * inter(vl, vr, lucky[i - 1] + 1, lucky[i]) * n            inter(pl, pr, lucky[j - 1], lucky[j] - 1); n     if (k == 1 && pl <= lucky[i] && lucky[i] <= pr && vl <= lucky[i] && n         lucky[i] <= vr) n       ans--; n   } n   ans /= 1.0 * (pr - pl + 1) * (vr - vl + 1); n   cout << fixed << setprecision(11) << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {1, -1, 0, 0}; n const int maxn = 1e5 + 10; n char dir[4] = { R ,  L ,  D ,  U }; n vector<vector<int> > a, ind, nxt; n pair<int, int> rh[maxn]; n vector<int> vect[maxn], pos_undo; n int nxt2[maxn], pos[maxn], n, m, mt[maxn], cnt, r; n bool outside(int x, int y) { n   if (x > n || x < 1 || y > m || y < 1) return true; n   return false; n } n void add_edge(int x, int y) { n   vect[x].push_back(y); n   vect[y].push_back(x); n } n bool srt(int x, int y) { return nxt2[x] < nxt2[y]; } n int kuhn(int x, int tip) { n   if (pos[x]) return 0; n   pos[x] = 1; n   pos_undo.push_back(x); n   for (int i = 0; i < vect[x].size(); i++) { n     int id = vect[x][i]; n     if (tip == 1 && nxt2[id] == 1) break; n     if (mt[id] == -1 || kuhn(mt[id], tip)) { n       mt[id] = x; n       mt[x] = id; n       return 1; n     } n   } n   return 0; n } n void random_matching() { n   for (int i = 1; i <= cnt; i++) { n     if (mt[i] != -1) continue; n     if (nxt2[i] != 0) continue; n     for (int j = 0; j < vect[i].size(); j++) { n       int id = vect[i][j]; n       if (mt[id] != -1) continue; n       if (nxt2[id] != 0) continue; n       mt[i] = id; n       mt[id] = i; n       r -= 2; n       break; n     } n   } n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &n, &m); n     a.clear(); n     nxt.clear(); n     ind.clear(); n     a.resize(n + 2, vector<int>(m + 2)); n     nxt.resize(n + 2, vector<int>(m + 2)); n     ind.resize(n + 2, vector<int>(m + 2)); n     cnt = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         scanf( %d , &a[i][j]); n         nxt[i][j] = -1; n         nxt2[cnt] = 0; n         ind[i][j] = ++cnt; n         rh[cnt] = {i, j}; n         mt[cnt] = -1; n         vect[cnt].clear(); n       } n     int fucked = 0; n     r = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         int e = 0; n         for (int k = 0; k < 4; k++) { n           int idx = i + dx[k]; n           int idy = j + dy[k]; n           if (outside(idx, idy)) continue; n           if (a[idx][idy] < a[i][j]) { n             nxt[i][j] = k; n             nxt2[ind[i][j]] = 1; n           } else if (a[idx][idy] == a[i][j]) { n             e = 1; n             if ((i + j) % 2) add_edge(ind[i][j], ind[idx][idy]); n           } n         } n         if (e == 0 && nxt[i][j] == -1) fucked = 1; n         if (nxt[i][j] == -1) r++; n       } n     if (fucked) { n       printf( NO n ); n       continue; n     } n     for (int i = 1; i <= cnt; i++) sort(vect[i].begin(), vect[i].end(), srt); n     random_matching(); n     for (int i = 1; i <= cnt; i++) { n       if (nxt[rh[i].first][rh[i].second] != -1 || mt[i] != -1) continue; n       if (kuhn(i, 1)) r -= 2; n       for (int j = 0; j < pos_undo.size(); j++) pos[pos_undo[j]] = 0; n       pos_undo.clear(); n     } n     for (int i = 1; i <= cnt; i++) { n       if (nxt[rh[i].first][rh[i].second] != -1 || mt[i] != -1) continue; n       if (kuhn(i, 0)) r -= 1; n       for (int j = 0; j < pos_undo.size(); j++) pos[pos_undo[j]] = 0; n       pos_undo.clear(); n     } n     if (r > 0) { n       printf( NO n ); n       continue; n     } n     printf( YES n ); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (mt[ind[i][j]] != -1) { n           if ((i + j) % 2) n             printf( %d  , a[i][j] / 2 + (a[i][j] % 2)); n           else n             printf( %d  , a[i][j] / 2); n         } else { n           printf( %d  , a[i][j] - a[i + dx[nxt[i][j]]][j + dy[nxt[i][j]]]); n         } n       } n       printf(  n ); n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (mt[ind[i][j]] != -1) { n           for (int k = 0; k < 4; k++) { n             int idx = i + dx[k]; n             int idy = j + dy[k]; n             if (ind[idx][idy] == mt[ind[i][j]]) { n               printf( %c  , dir[k]); n               break; n             } n           } n         } else { n           printf( %c  , dir[nxt[i][j]]); n         } n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e6 + 10; n using namespace std; n inline int min(int a, int b) { return a < b ? a : b; } n int main() { n   int a, b, c; n   while (~scanf( %d%d%d , &a, &b, &c)) { n     int k = min(min(a / 3, b / 2), c / 2); n     a -= 3 * k; n     b -= 2 * k; n     c -= 2 * k; n     if (a == 0 && b == 0 && c == 0) { n       printf( %d n , k * 7); n     } else { n       if (a == 0) { n         if (b == 0 || c == 0) { n           printf( %d n , k * 7 + 1); n         } else { n           printf( %d n , k * 7 + 2); n         } n       } else if (b == 0) { n         if (c == 0) { n           if (a > 2) { n             printf( %d n , k * 7 + 2); n           } else { n             printf( %d n , k * 7 + 1); n           } n         } else { n           if (c >= 2) { n             printf( %d n , k * 7 + 3); n           } else { n             printf( %d n , k * 7 + 2); n           } n         } n       } else if (c == 0) { n         if (a >= 2 && b >= 2) { n           printf( %d n , k * 7 + 4); n         } else if (b < 2 && a >= 2) { n           printf( %d n , k * 7 + 3); n         } else if (a < 2) { n           printf( %d n , k * 7 + 2); n         } n       } else { n         int kk = min(min(a, b), c), sum = 0; n         if (kk == a) { n           if (a == 1) { n             if (c >= 2) { n               if (b >= 2) { n                 b = 2; n               } n               printf( %d n , k * 7 + a + b + 2); n             } else { n               printf( %d n , k * 7 + 3); n             } n           } else { n             printf( %d n , k * 7 + 6); n           } n         } else { n           if (b == 1) { n             if (c >= 2) { n               if (a >= 3) { n                 a = 3; n               } n               printf( %d n , k * 7 + a + 3); n             } else if (c == 1) { n               if (a >= 3) { n                 a = 3; n               } n               printf( %d n , k * 7 + a + 2); n             } n           } else if (c == 1) { n             if (b >= 2) { n               if (a >= 3) { n                 a = 3; n               } n               printf( %d n , k * 7 + a + 3); n             } else if (b == 1) { n               if (a >= 3) { n                 a = 3; n               } n               printf( %d n , k * 7 + a + 2); n             } n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n bool isV(char x) { n   if (x ==  a  || x ==  e  || x ==  i  || x ==  o  || x ==  u ) { n     return 1; n   } n   return 0; n } n bool isP(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n void fio() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n void print(long long* a, long long n) { n   long long i; n   for (i = 0; i < (long long)n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n void print2d(long long a[][1000], long long m, long long n) { n   long long i, j; n   for (i = 0; i < (long long)m; i++) { n     for (j = 0; j < (long long)n; j++) { n       cout << setw(2) << a[i][j]; n     } n     cout << endl; n   } n } n int32_t main() { n   fio(); n   long long n; n   cin >> n; n   long long* a = new long long[n]; n   long long i; n   for (i = 0; i < (long long)n; i++) { n     cin >> a[i]; n   } n   long long* suf = new long long[n]; n   suf[n - 1] = 0; n   stack<long long> s; n   for (i = n - 1; i >= (long long)0; i--) { n     while (s.size() > 0 && a[i] <= a[s.top()]) s.pop(); n     if (s.size() != 0) { n       suf[i] = suf[s.top()] + (s.top() - i - 1) * a[i] + a[s.top()]; n     } else { n       suf[i] = (n - (i + 1)) * a[i]; n     } n     s.push(i); n   } n   long long* temp = new long long[n]; n   long long* pref = new long long[n]; n   pref[0] = 0; n   long long m = 0, mi = -1; n   stack<long long> x; n   for (i = 0; i < (long long)n; i++) { n     while (x.size() > 0 && a[x.top()] > a[i]) x.pop(); n     if (x.size() == 0) n       pref[i] = i * a[i]; n     else n       pref[i] = pref[x.top()] + (i - x.top() - 1) * a[i] + a[x.top()]; n     x.push(i); n     if (m < pref[i] + suf[i] + a[i]) { n       m = pref[i] + suf[i] + a[i]; n       mi = i; n     } n   } n   for (i = mi + 1; i < (long long)n; i++) { n     if (a[i] > a[i - 1]) a[i] = a[i - 1]; n   } n   for (i = mi - 1; i >= (long long)0; i--) { n     if (a[i] > a[i + 1]) a[i] = a[i + 1]; n   } n   for (i = 0; i < (long long)n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int N = 1234567; n int x[N], y[N]; n int n; n double areas[N]; n void line(int i, int j, double &a, double &b, double &c) { n   i %= n; n   j %= n; n   a = y[j] - y[i]; n   b = x[i] - x[j]; n   c = -a * x[i] - b * y[i]; n } n void intersect(double a, double b, double c, double aa, double bb, double cc, n                double &dx, double &dy) { n   double d = a * bb - b * aa; n   dx = (b * cc - c * bb) / d; n   dy = (c * aa - a * cc) / d; n } n int main() { n   int q; n   scanf( %d %d , &n, &q); n   for (int i = 0; i < n; i++) scanf( %d %d , x + i, y + i); n   reverse(x, x + n); n   reverse(y, y + n); n   areas[0] = 0; n   for (int i = 0; i < 4 * n; i++) n     areas[i + 1] = areas[i] + (x[i % n] - x[(i + 1) % n]) * 1.0 * n                                   (y[i % n] + y[(i + 1) % n]); n   double total_area = 0.0; n   for (int i = 0; i < n; i++) n     total_area += (x[i] - x[(i + 1) % n]) * 1.0 * (y[i] + y[(i + 1) % n]); n   for (int i = 0; i < q; i++) { n     int qx, qy; n     scanf( %d %d , &qx, &qy); n     double low = 0, high = pi; n     double zero = -1.0; n     bool found = false; n     double ans = -1.0; n     for (int it = 0; it < 120; it++) { n       double mid = 0.5 * (low + high); n       if (mid == low || mid == high) break; n       if (it == 0) mid = low; n       double dx = cos(mid); n       double dy = sin(mid); n       double a = dy; n       double b = -dx; n       double c = -a * qx - b * qy; n       double u0 = a * x[0] + b * y[0] + c; n       double u1 = a * x[1] + b * y[1] + c; n       bool flag = false; n       if (u0 > u1) { n         flag = true; n         u0 = -u0; n         u1 = -u1; n         a = -a; n         b = -b; n         c = -c; n       } n       int ll = 0, rr = n - 1; n       while (ll < rr) { n         int i = (ll + rr + 1) >> 1; n         int j = (i + 1) % n; n         double ui = a * x[i] + b * y[i] + c; n         double uj = a * x[j] + b * y[j] + c; n         if (ui <= uj && ui >= 0.5 * (u0 + u1)) n           ll = i; n         else n           rr = i - 1; n       } n       int id_max = (ll + 1) % n; n       ll = id_max + 1, rr = id_max + n - 1; n       while (ll < rr) { n         int i = (ll + rr) >> 1; n         int j = i + 1; n         double ui = a * x[i % n] + b * y[i % n] + c; n         double uj = a * x[j % n] + b * y[j % n] + c; n         if (ui > uj) n           ll = i + 1; n         else n           rr = i; n       } n       int id_min = ll; n       int last, first; n       { n         ll = id_max, rr = id_min; n         while (ll < rr) { n           int i = (ll + rr + 1) >> 1; n           double ui = a * x[i % n] + b * y[i % n] + c; n           if (ui > 0) n             ll = i; n           else n             rr = i - 1; n         } n         last = ll + n; n       } n       { n         ll = id_min, rr = id_max + n; n         while (ll < rr) { n           int i = (ll + rr) >> 1; n           double ui = a * x[i % n] + b * y[i % n] + c; n           if (ui < 0) n             ll = i + 1; n           else n             rr = i; n         } n         first = ll; n       } n       double xf, yf, xl, yl; n       double aa, bb, cc; n       line(first - 1, first, aa, bb, cc); n       intersect(a, b, c, aa, bb, cc, xf, yf); n       line(last, last + 1, aa, bb, cc); n       intersect(a, b, c, aa, bb, cc, xl, yl); n       double res = 0.0; n       res += (xf - x[first % n]) * (yf + y[first % n]); n       res += areas[last] - areas[first]; n       res += (x[last % n] - xl) * (y[last % n] + yl); n       res += (xl - xf) * (yl + yf); n       if (flag) res = total_area - res; n       if (it == 0) { n         zero = res; n         continue; n       } n       { n         double res1 = 0.5 * res; n         double res2 = 0.5 * (total_area - res); n         double diff = fabs(res1 - res2); n         double sum = 0.5 * total_area; n         if (diff / sum < 1e-6) { n           found = true; n           ans = mid; n           break; n         } n       } n       if ((res > 0.5 * total_area) == (zero > 0.5 * total_area)) n         low = mid; n       else n         high = mid; n     } n     printf( %.17f n , found ? ans : (0.5 * (low + high))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n queue<int> q; n priority_queue<pair<int, int> > Q; n vector<pair<int, int> > V[maxn]; n struct edge { n   int obj, last; n } e[maxn]; n int n, N, m, p, top, cnt, point[maxn], in[maxn], ord[maxn], L[maxn], H[maxn], n     ans[maxn]; n bool vis[maxn]; n void add(int x, int y) { n   cnt++; n   e[cnt].obj = y; n   e[cnt].last = point[x]; n   point[x] = cnt; n } n void Bye() { n   puts( -1 ); n   exit(0); n } n int main() { n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 1, x; i <= n; i++) { n     scanf( %d , &x); n     if (x) n       L[i] = x, H[i] = x; n     else n       L[i] = 1, H[i] = p; n   } n   for (int i = 1, x, y; i <= m; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n     in[y]++; n   } n   for (int i = 1; i <= n; i++) n     if (!in[i]) q.push(i), ord[++N] = i, vis[i] = true; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = point[u], v; i; i = e[i].last) { n       v = e[i].obj; n       in[v]--; n       if (!in[v]) q.push(v), ord[++N] = v, vis[v] = true; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) Bye(); n   for (int i = 1, u; i <= n; i++) { n     u = ord[i]; n     for (int j = point[u], v; j; j = e[j].last) { n       v = e[j].obj; n       H[v] = min(H[v], H[u] - 1); n     } n   } n   for (int i = n, u; i >= 1; i--) { n     u = ord[i]; n     for (int j = point[u], v; j; j = e[j].last) { n       v = e[j].obj; n       L[u] = max(L[u], L[v] + 1); n     } n   } n   for (int i = 1; i <= n; i++) n     if (L[i] > H[i]) Bye(); n   for (int i = 1; i <= n; i++) V[H[i]].push_back(pair<int, int>(L[i], i)); n   top = p; n   while (top) { n     for (int i = 0, I = V[top].size(); i < I; i++) Q.push(V[top][i]); n     if (Q.empty()) Bye(); n     ans[Q.top().second] = top; n     Q.pop(); n     while (!Q.empty() && Q.top().first == top) { n       ans[Q.top().second] = top; n       Q.pop(); n     } n     top--; n   } n   for (int i = 1; i < n; i++) printf( %d  , ans[i]); n   printf( %d n , ans[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[8] = {0, 0, -1, -1, -1, 1, 1, 1}; n const int dy[8] = {-1, 1, -1, 0, 1, -1, 0, 1}; n int r, c, n, res, a, b, f[18000005]; n bool vis[3001][6001], g[3001][6001]; n stack<pair<int, int> > stk; n void init() { n   for (int i = 0; i < r * c; ++i) f[i] = i; n } n int F(int x) { n   if (f[x] == x) return x; n   stk.push(pair<int, int>(x, f[x])); n   return f[x] = F(f[x]); n } n void U(int a, int b) { n   int fa = F(a), fb = F(b); n   stk.push(pair<int, int>(fa, f[fa])); n   f[fa] = fb; n } n int main() { n   scanf( %d%d%d , &r, &c, &n); n   c <<= 1; n   init(); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &a, &b); n     --a; n     --b; n     while (!stk.empty()) stk.pop(); n     vis[a][b] = vis[a][b + c / 2] = true; n     for (int i = 0; i < 8; ++i) { n       int tx = a + dx[i], ty = b + dy[i]; n       if (ty == -1) ty = c - 1; n       if (ty == c) ty = 0; n       if (tx < 0 || tx >= r || !vis[tx][ty]) continue; n       U(tx * c + ty, a * c + b); n     } n     b += c / 2; n     for (int i = 0; i < 8; ++i) { n       int tx = a + dx[i], ty = b + dy[i]; n       if (ty == -1) ty = c - 1; n       if (ty == c) ty = 0; n       if (tx < 0 || tx >= r || !vis[tx][ty]) continue; n       U(tx * c + ty, a * c + b); n     } n     b -= c / 2; n     if (F(a * c + b) == F(a * c + b + c / 2)) { n       vis[a][b] = vis[a][b + c / 2] = false; n       while (!stk.empty()) { n         f[stk.top().first] = stk.top().second; n         stk.pop(); n       } n     } else { n       vis[a][b] = vis[a][b + c / 2] = true; n       ++res; n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool check_diags(int x0, int y0, int x, int y) { n   return abs(x0 - x) == abs(y0 - y); n } n inline bool check_lines(int x0, int y0, int x, int y) { n   return x - x0 == 0 || y - y0 == 0; n } n vector<int> a, b, c, d; n int n, x, s, m, k; n void read() { n   for (int i = 0; i < m; ++i) { n     int t; n     scanf(  %d , &t); n     a.push_back(t); n   } n   for (int i = 0; i < m; ++i) { n     int t; n     scanf(  %d , &t); n     b.push_back(t); n   } n   a.push_back(x); n   b.push_back(0); n   c.push_back(0); n   d.push_back(0); n   for (int i = 0; i < k; ++i) { n     int t; n     scanf(  %d , &t); n     c.push_back(t); n   } n   for (int i = 0; i < k; ++i) { n     int t; n     scanf(  %d , &t); n     d.push_back(t); n   } n } n int main() { n   scanf( %d %d %d %d %d , &n, &m, &k, &x, &s); n   read(); n   int64_t ans = numeric_limits<int64_t>::max(); n   for (int i = 0; i < m + 1; ++i) { n     int avail = s - b[i]; n     if (avail < 0) continue; n     auto pos = upper_bound(d.begin(), d.end(), avail); n     if (pos != d.begin()) { n       int j = distance(d.begin(), pos - 1); n       int64_t val = ((int64_t)a[i]) * (n - c[j]); n       ans = min(ans, val); n     } n   } n   printf( %lld n , ans); n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 3333; n const int mod = 998244353; n long long dp[M][M]; n char s[M], t[M]; n int main() { n   scanf( %s%s , s + 1, t + 1); n   int n = strlen(s + 1), m = strlen(t + 1); n   for (int i = 1; i <= n + 1; i++) dp[i][i - 1] = 1; n   for (int i = 1; i <= n; i++) { n     for (int l = 1, r = l + i - 1; r <= n; l++, r++) { n       if (l > m || s[i] == t[l]) dp[l][r] += dp[l + 1][r] % mod; n       if (r > m || s[i] == t[r]) dp[l][r] += dp[l][r - 1] % mod; n     } n   } n   long long ans = 0; n   for (int i = m; i <= n; i++) ans += dp[1][i], ans %= mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n struct Edge { n   int to, w; n }; n const int N = 3e5 + 5, L = 20; n vector<Edge> v[N]; n int a[N], tin[N], tout[N], p[N][L]; n long long maxPath[N], maxUpPath[N], sum[N], pt[N], ancw[N]; n int timer = 0; n bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } n int getBeforeLca(int a, int b) { n   if (upper(a, b)) return -1; n   for (int i = L - 1; i >= 0; i--) { n     if (!upper(p[a][i], b)) a = p[a][i]; n   } n   return a; n } n void dfs(int node, int anc) { n   p[node][0] = anc; n   for (int i = 1; i < L; i++) { n     p[node][i] = p[p[node][i - 1]][i - 1]; n   } n   tin[node] = timer++; n   sum[node] = maxPath[node] = a[node]; n   for (auto e : v[node]) { n     if (e.to != anc) { n       ancw[e.to] = e.w; n       dfs(e.to, node); n       maxPath[e.to] = max(0ll, maxPath[e.to] - e.w * 2); n       maxPath[node] += maxPath[e.to]; n       sum[node] += maxPath[e.to]; n     } n   } n   tout[node] = timer; n } n void deep(int node, long long x, long long y, int anc) { n   maxUpPath[node] = x - y; n   x += sum[node]; n   for (auto e : v[node]) { n     if (e.to != anc) { n       pt[e.to] = pt[node] - e.w + sum[node] - maxPath[e.to]; n       long long nx = x - e.w * 2 - maxPath[e.to]; n       deep(e.to, nx, min(y, nx), node); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b, w; n     cin >> a >> b >> w; n     a--, b--; n     v[a].push_back({b, w}); n     v[b].push_back({a, w}); n   } n   dfs(0, 0); n   deep(0, 0, 0, -1); n   for (int i = 0; i < q; i++) { n     int x, y, xx, yy, lca; n     cin >> x >> y; n     x--, y--; n     xx = getBeforeLca(x, y); n     yy = getBeforeLca(y, x); n     lca = xx == -1 ? x : p[xx][0]; n     long long ans = maxUpPath[lca] + sum[lca]; n     if (x != lca) { n       ans += sum[x] - maxPath[xx] + pt[x] - pt[xx] - ancw[xx]; n     } n     if (y != lca) { n       ans += sum[y] - maxPath[yy] + pt[y] - pt[yy] - ancw[yy]; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, sum, a, b, c; n int main() { n   for (i = 0; i < 4; i++) { n     cin >> a; n     if (i == 0) { n       b = a; n       c = a; n     } n     if (a < b) { n       b = a; n     } n     if (a > c) { n       c = a; n     } n     sum += a; n   } n   if (sum % 2 == 1) { n     cout <<  NO ; n   } else if (c + b == sum / 2 || c == sum / 2) { n     cout <<  YES ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l; n   cin >> l; n   string s; n   cin >> s; n   if (l < 26) { n     cout <<  NO n ; n     return 0; n   } n   int f[26] = {}; n   for (int i = 0; i < l; i++) f[tolower(s[i]) -  a ]++; n   for (int i = 0; i < 26; i++) n     if (f[i] == 0) { n       cout <<  NO n ; n       return 0; n     } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010, LOG = 20; n int n, m, k, q, u, v, x, y, t, a, b, ans; n int stt[MAXN], fnt[MAXN], ras[MAXN], timer = 1; n int sz[MAXN], par[MAXN], head[MAXN], h[MAXN]; n long long fen[MAXN]; n pair<long long, int> seg[MAXN << 2]; n long long lazy[MAXN << 2]; n set<int> st[MAXN]; n vector<int> G[MAXN]; n void add(int pos, long long val) { n   for (; pos < MAXN; pos += pos & -pos) fen[pos] += val; n } n long long get(int pos) { n   long long res = 0; n   for (; pos; pos -= pos & -pos) res += fen[pos]; n   return res; n } n int dfs1(int node) { n   h[node] = h[par[node]] + 1; n   for (int v : G[node]) n     if (v != par[node]) { n       par[v] = node; n       sz[node] += dfs1(v); n     } n   return ++sz[node]; n } n void dfs2(int node, int hd) { n   head[node] = hd; n   stt[node] = timer++; n   ras[stt[node]] = node; n   int big = 0; n   for (int v : G[node]) n     if (v != par[node] && sz[v] > sz[big]) big = v; n   if (big) dfs2(big, hd); n   for (int v : G[node]) n     if (v != par[node] && v != big) dfs2(v, v); n   fnt[node] = timer; n } n pair<long long, int> Build(int id, int tl, int tr) { n   if (tr - tl == 1) return seg[id] = {INF, ras[tl]}; n   int mid = (tl + tr) >> 1; n   return seg[id] = min(Build(id << 1, tl, mid), Build(id << 1 | 1, mid, tr)); n } n void add_lazy(int id, long long val) { n   seg[id].first += val; n   lazy[id] += val; n } n void shift(int id) { n   if (!lazy[id]) return; n   add_lazy(id << 1, lazy[id]); n   add_lazy(id << 1 | 1, lazy[id]); n   lazy[id] = 0; n } n void Add(int id, int tl, int tr, int l, int r, long long val) { n   if (r <= tl || tr <= l) return; n   if (l <= tl && tr <= r) { n     add_lazy(id, val); n     return; n   } n   shift(id); n   int mid = (tl + tr) >> 1; n   Add(id << 1, tl, mid, l, r, val); n   Add(id << 1 | 1, mid, tr, l, r, val); n   seg[id] = min(seg[id << 1], seg[id << 1 | 1]); n } n pair<long long, int> Get(int id, int tl, int tr, int l, int r) { n   if (r <= tl || tr <= l) return seg[0]; n   if (l <= tl && tr <= r) return seg[id]; n   shift(id); n   int mid = (tl + tr) >> 1; n   return min(Get(id << 1, tl, mid, l, r), Get(id << 1 | 1, mid, tr, l, r)); n } n void Set(int id, int tl, int tr, int pos, long long val) { n   if (pos < tl || tr <= pos) return; n   if (tr - tl == 1) { n     seg[id].first = val; n     return; n   } n   shift(id); n   int mid = (tl + tr) >> 1; n   Set(id << 1, tl, mid, pos, val); n   Set(id << 1 | 1, mid, tr, pos, val); n   seg[id] = min(seg[id << 1], seg[id << 1 | 1]); n } n pair<long long, int> GetPath(int u, int v) { n   pair<long long, int> res = {INF, -1}; n   while (head[u] != head[v]) { n     if (h[head[u]] > h[head[v]]) swap(u, v); n     res = min(res, Get(1, 1, n + 1, stt[head[v]], stt[v] + 1)); n     v = par[head[v]]; n   } n   if (h[u] > h[v]) swap(u, v); n   res = min(res, Get(1, 1, n + 1, stt[u], stt[v] + 1)); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= m; i++) cin >> x, st[x].insert(i); n   dfs1(1); n   dfs2(1, 1); n   Build(1, 1, n + 1); n   seg[0] = {INF, 0}; n   for (int i = 1; i <= n; i++) n     if (st[i].size()) Set(1, 1, n + 1, stt[i], *st[i].begin()); n   while (q--) { n     cin >> t; n     if (t == 2) { n       cin >> v >> x; n       Add(1, 1, n + 1, stt[v], fnt[v], x); n       add(stt[v], +x); n       add(fnt[v], -x); n       continue; n     } n     vector<int> out; n     cin >> u >> v >> k; n     while (k--) { n       pair<long long, int> p = GetPath(u, v); n       if (p.first >= INF) break; n       int v = p.second, id = *st[v].begin(); n       out.push_back(id); n       st[v].erase(id); n       if (st[v].empty()) n         Set(1, 1, n + 1, stt[v], INF); n       else n         Set(1, 1, n + 1, stt[v], *st[v].begin() + get(stt[v])); n     } n     cout << out.size() <<    ; n     for (int i : out) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long sqr(int a) { return a * 1ll * a; } n int main() { n   int k; n   cin >> k; n   int len = k; n   int cnt = 0; n   int x = 0; n   long long res = 0; n   while (x <= 2 * k) { n     if (!(cnt & 1)) { n       int best = 0; n       int l = 1, r = k; n       while (l <= r) { n         int m = (l + r) / 2; n         if ((sqr(x + 2) + sqr(m - 1) * 12 <= sqr(k) * 4) && n             (sqr(x + 1) + sqr(2 * m - 1) * 3 <= sqr(k) * 4)) { n           best = m; n           l = m + 1; n         } else n           r = m - 1; n       } n       if (best) { n         if (x) res += best * 2 - 1; n         res += best * 2 - 1; n       } n     } else { n       int best = 0; n       int l = 1, r = k; n       while (l <= r) { n         int m = (l + r) / 2; n         if ((sqr(x + 2) + sqr(2 * m - 1) * 3 <= sqr(k) * 4) && n             (sqr(x + 1) + sqr(m) * 12 <= sqr(k) * 4)) { n           best = m; n           l = m + 1; n         } else n           r = m - 1; n       } n       if (x) res += best * 2; n       res += best * 2; n     } n     x += 3; n     cnt++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(string a, string b) { return a.size() < b.size(); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   string arr[n]; n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   sort(arr, arr + n, comp); n   string st = arr[0]; n   vector<string> ans; n   set<int> chosen; n   chosen.insert(0); n   while (ans.size() < n) { n     if (ans.size() > 0) { n       int mini = INT_MAX; n       for (int j = 0; j < n; ++j) { n         if (arr[j].find(st) != string::npos) { n           if (arr[j].size() < mini && chosen.find(j) == chosen.end()) { n             chosen.insert(j); n             mini = arr[j].size(); n             st = arr[j]; n           } n         } n       } n       if (mini == INT_MAX) { n         return cout <<  NO n , 0; n       } n     } n     ans.push_back(st); n   } n   cout <<  YES  << endl; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i] << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n struct Hash { n   const long long int mod = 1791791791; n   const long long int base = 255; n   vector<long long int> h, p; n   Hash(const string& s) { n     long long int n = s.size(); n     h.resize(n + 1); n     p.resize(n + 1, 1); n     for (long long int i = 1; i <= n; i++) { n       h[i] = (h[i - 1] * base + s[i - 1]) % mod; n       p[i] = (p[i - 1] * base) % mod; n     } n   } n   long long int getSub(long long int l, long long int r) { n     return (h[r] - (h[l - 1] * p[r - l + 1]) % mod + mod) % mod; n   } n   ~Hash(){}; n }; n void rev(string& s) { n   for (auto& i : s) { n     if (i ==  0 ) n       i =  1 ; n     else n       i =  0 ; n   } n } n signed main() { n   string s, t; n   cin >> s >> t; n   long long int n = t.size(); n   Hash h(t); n   long long int cnt0 = 0, cnt1 = 0; n   for (auto i : s) { n     cnt0 += (i ==  0 ); n     cnt1 += (i ==  1 ); n   } n   if (cnt0 < cnt1) { n     swap(cnt0, cnt1); n     rev(s); n   } n   long long int ans = 0; n   for (long long int len0 = 1; len0 < t.size() / cnt0; len0++) { n     if (cnt1 == 0 || (n - len0 * cnt0) % cnt1 == 0) { n       long long int len1 = 0; n       if (cnt1 > 0) len1 = (n - len0 * cnt0) / cnt1; n       long long int last = 1; n       unordered_set<long long int> se0, se1; n       for (long long int j = 0; j < s.size(); j++) { n         if (s[j] ==  0 ) { n           se0.insert(h.getSub(last, last + len0 - 1)); n           last += len0; n         } else { n           se1.insert(h.getSub(last, last + len1 - 1)); n           last += len1; n         } n       } n       if (se0.size() <= 1 && se1.size() <= 1) ans++; n       if (se0.size() == 1 && se1.size() == 1 && n           (*se1.begin()) == (*se0.begin())) n         ans--; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000010; n int n, m, tot, ans; n int mx[maxn], mn[maxn], fir[maxn]; n struct edge { n   int a, b, l, r; n } p[maxn << 1]; n queue<int> q; n vector<int> v[maxn]; n inline void add(int a, int b, int c, int d) { n   p[++tot].a = a, p[tot].b = b, p[tot].l = c + ((c ^ a) & 1), n   p[tot].r = d - ((d ^ b) & 1); n } n bool cmp(const edge &a, const edge &b) { return a.l < b.l; } n inline void solve(int x) { n   int a = p[x].a, b = p[x].b, l = p[x].l = max(p[x].l, mn[a]), r = p[x].r; n   if (mx[a] < l) { n     v[a].push_back(x); n     return; n   } n   q.push(x); n   while (!q.empty()) { n     x = q.front(), q.pop(), a = p[x].a, b = p[x].b, l = p[x].l, r = p[x].r; n     if (l >= r) continue; n     if (mx[b] + 2 < l) n       mn[b] = l + 1, mx[b] = r; n     else n       mx[b] = max(mx[b], r); n     if ((b >> 1) == n) ans = min(ans, mn[b]); n     while (fir[b] < (int)v[b].size()) { n       int t = v[b][fir[b]]; n       if (mx[b] >= p[t].l) n         fir[b]++, q.push(t), p[t].l = mn[b]; n       else n         break; n     } n   } n } n inline int rd() { n   int ret = 0, f = 1; n   char gc = getchar(); n   while (gc <  0  || gc >  9 ) { n     if (gc ==  - ) f = -f; n     gc = getchar(); n   } n   while (gc >=  0  && gc <=  9 ) ret = ret * 10 + (gc ^  0 ), gc = getchar(); n   return ret * f; n } n int main() { n   n = rd(), m = rd(); n   if (n == 1) { n     puts( 0 ); n     return 0; n   } n   int i, a, b, c, d; n   for (i = 1; i <= m; i++) { n     a = rd(), b = rd(), c = rd(), d = rd(); n     add(a << 1, b << 1 | 1, c, d); n     add(a << 1 | 1, b << 1, c, d); n     add(b << 1, a << 1 | 1, c, d); n     add(b << 1 | 1, a << 1, c, d); n   } n   sort(p + 1, p + tot + 1, cmp); n   memset(mx, 0xc0, sizeof(mx)), memset(mn, 0xc0, sizeof(mn)); n   mn[2] = mx[2] = 0, ans = 1 << 30; n   for (i = 1; i <= tot; i++) solve(i); n   if (ans == (1 << 30)) n     puts( -1 ); n   else n     printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_CITIES = 1e5; n int n, m, c, q; n int dsu[MAX_CITIES + 1] = {0}; n std::set<int> adj[MAX_CITIES + 1]; n std::map<int, int>* city_rainbows; n int getDsuRoot(int v) { n   int root = v; n   while (dsu[root] != 0) { n     root = dsu[root]; n   } n   while (v != root) { n     int p = dsu[v]; n     dsu[v] = root; n     v = p; n   } n   return root; n } n int mergeDsu(int x, int y) { n   int rootx = getDsuRoot(x); n   int rooty = getDsuRoot(y); n   if (adj[rootx].size() < adj[rooty].size()) std::swap(rootx, rooty); n   if (rootx != rooty) { n     dsu[rooty] = rootx; n     adj[rootx].insert(adj[rooty].begin(), adj[rooty].end()); n     adj[rooty].clear(); n   } n   return rootx; n } n bool isReachable(int x, int y) { n   bool ans = false; n   int rootx = getDsuRoot(x), rooty = getDsuRoot(y); n   if (rootx == rooty || adj[rootx].find(y) != adj[rootx].end()) ans = true; n   return ans; n } n void addEdge(int x, int y, int z) { n   std::map<int, int>&r1 = city_rainbows[x], &r2 = city_rainbows[y]; n   if (r1.find(z) == r1.end()) { n     r1.emplace(std::make_pair(z, y)); n   } else { n     mergeDsu(r1[z], y); n   } n   if (r2.find(z) == r2.end()) { n     r2.emplace(std::make_pair(z, x)); n   } else { n     mergeDsu(r2[z], x); n   } n   int rootx = getDsuRoot(x); n   int rooty = getDsuRoot(y); n   if (rootx != rooty) { n     adj[rootx].insert(y); n     adj[rooty].insert(x); n   } n } n int main() { n   scanf( %d%d%d%d , &n, &m, &c, &q); n   city_rainbows = new std::map<int, int>[n + 1]; n   for (int i = 0; i < m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     addEdge(x, y, z); n   } n   for (int i = 0; i < q; i++) { n     char type; n     int x, y, z; n     std::cin >> type; n     if (type ==  + ) { n       scanf( %d%d%d , &x, &y, &z); n       addEdge(x, y, z); n     } else if (type ==  ? ) { n       scanf( %d%d , &x, &y); n       if (isReachable(x, y)) { n         printf( Yes n ); n       } else { n         printf( No n ); n       } n     } n   } n   delete[] city_rainbows; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<char, long long> store; n class thisway { n  public: n   bool operator()(const char& a, const char& b) { n     if (store[a] > store[b]) return true; n     return false; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   for (long long i = 0; i < n; i++) { n     store[s[i]]++; n   } n   multiset<char, thisway> ss; n   for (auto x : store) ss.insert(x.first); n   long long sum = 0; n   while (!ss.empty() && k > 0) { n     long long pp = store[*(ss.begin())]; n     ss.erase(ss.begin()); n     long long req = min(k, pp); n     sum += req * req; n     pp -= req; n     k -= req; n     if (pp == 0) store.erase(pp); n   } n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n vector<int> edge[300010]; n vector<int> ve[300010]; n int parent[300010]; n int hap1[300010], hap2[300010]; n int pwp1[300010], pwp2[300010]; n int id[300010]; n int ite = 300010; n map<int, bool> used; n int find_parent(int u) { n   if (parent[u] == u) { n     return u; n   } n   parent[u] = find_parent(parent[u]); n   return parent[u]; n } n void merge(int a, int b) { n   a = find_parent(a); n   b = find_parent(b); n   if (a < b) { n     swap(a, b); n   } n   if (a == b) { n     return; n   } n   parent[a] = b; n   return; n } n void dfs(int u, int val) { n   if (id[u]) { n     return; n   } n   id[u] = val; n   used[val] = true; n   if (ve[u].size() == 0) { n     return; n   } n   if (ve[u].size() == 1) { n     int v = ve[u][0]; n     if (id[v]) { n       return; n     } n     if (used[val + 1]) { n       dfs(v, val - 1); n     } else { n       dfs(v, val + 1); n     } n     return; n   } n   if (ve[u].size() == 2) { n     int v = ve[u][0]; n     int w = ve[u][1]; n     if (id[v]) { n       swap(v, w); n     } n     if (id[v]) { n       return; n     } n     if (!id[w]) { n       dfs(w, val + 1); n       dfs(v, val - 1); n       return; n     } n     if (used[val + 1]) { n       dfs(v, val - 1); n     } else { n       dfs(v, val + 1); n     } n     return; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   if (fopen( cf794d.in ,  r )) { n     freopen( cf794d.in ,  r , stdin); n     freopen( cf794d.out ,  w , stdout); n   } n   cin >> N >> M; n   pwp1[0] = 1; n   pwp2[0] = 1; n   for (int i = 1; i < 300010; i++) { n     pwp1[i] = pwp1[i - 1] * 2; n     pwp2[i] = pwp2[i - 1] * 2; n     pwp1[i] %= 1000000007; n     pwp2[i] %= 1000000861; n   } n   for (int i = 0; i < M; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   for (int i = 0; i < N; i++) { n     parent[i] = i; n     edge[i].push_back(i); n     sort(edge[i].begin(), edge[i].end()); n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < edge[i].size(); j++) { n       hap1[i] += pwp1[edge[i][j]]; n       hap2[i] += pwp2[edge[i][j]]; n       hap1[i] %= 1000000007; n       hap2[i] %= 1000000861; n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < edge[i].size(); j++) { n       int v = edge[i][j]; n       if (i == v) { n         continue; n       } n       if (hap1[i] == hap1[v] && hap2[i] == hap2[v]) { n         merge(i, v); n       } n     } n   } n   for (int i = 0; i < N; i++) { n     if (parent[i] != i) { n       continue; n     } n     for (int j = 0; j < edge[i].size(); j++) { n       int u = find_parent(edge[i][j]); n       if (i == u) { n         continue; n       } n       ve[i].push_back(u); n       ve[u].push_back(i); n     } n   } n   for (int i = 0; i < N; i++) { n     sort(ve[i].begin(), ve[i].end()); n     ve[i].erase(unique(ve[i].begin(), ve[i].end()), ve[i].end()); n   } n   for (int i = 0; i < N; i++) { n     if (ve[i].size() >= 3) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < N; i++) { n     if (parent[i] != i) { n       continue; n     } n     dfs(i, ite); n     ite += 300010; n   } n   for (int i = 0; i < N; i++) { n     int u = find_parent(i); n     cout << id[u] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INTlog2(unsigned long long number) { n   int logval = 0; n   while (number >>= 1) logval++; n   return logval; n } n int main() { n   unsigned long long n, k; n   cin >> n >> k; n   if (k == 1) n     cout << n << endl; n   else n     cout << (1LL << (INTlog2(n) + 1LL)) - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long A[1048576]; n long long B[30]; n long long C[30]; n long P[30]; n void f(long n, long o) { n   if (n > 1) { n     f(n - 1, o); n     f(n - 1, o + P[n - 1]); n   } n   sort(A + o, A + o + P[n - 1]); n   sort(A + o + P[n - 1], A + o + P[n]); n   long i = o; n   long j = o + P[n - 1]; n   while (i != o + P[n - 1] && j != o + P[n]) { n     if (A[i] < A[j]) { n       B[n] += o + P[n] - j; n       i += 1; n     } else { n       j += 1; n     } n   } n   i = o; n   j = o + P[n - 1]; n   while (i != o + P[n - 1] && j != o + P[n]) { n     if (A[i] <= A[j]) { n       i += 1; n     } else { n       C[n] += o + P[n - 1] - i; n       j += 1; n     } n   } n } n int main(int, char**) { n   ios_base::sync_with_stdio(false); n   P[0] = 1; n   for (long i = (1); i <= (29); ++i) P[i] = 2 * P[i - 1]; n   long n; n   cin >> n; n   for (long i = 0; i < (P[n]); ++i) cin >> A[i]; n   for (long i = 0; i < (29); ++i) B[i] = 0; n   for (long i = 0; i < (29); ++i) C[i] = 0; n   f(n, 0); n   bitset<30> BS; n   long m; n   cin >> m; n   for (long i = 0; i < (m); ++i) { n     long q; n     cin >> q; n     while (q != 0) { n       BS[q] = not BS[q]; n       q -= 1; n     } n     long long r = 0; n     for (long j = 0; j < (n + 1); ++j) { n       r += BS[j] ? B[j] : C[j]; n     } n     cout << r <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool helper(pair<int, int>* arr, int x, int y, int m) { n   if (m == 0) { n     return true; n   } n   int res = false; n   if (x != -1) { n     if (arr->first == x || arr->second == x) res = true; n   } n   if (y != -1) { n     if (arr->second == y || arr->first == y) res = true; n   } n   if (!res) { n     if (x == -1) { n       return helper(arr + 1, arr->first, y, m - 1) || n              helper(arr + 1, arr->second, y, m - 1); n     } n     if (y == -1) { n       return helper(arr + 1, x, arr->second, m - 1) || n              helper(arr + 1, x, arr->first, m - 1); n     } n     return false; n   } n   return helper(arr + 1, x, y, m - 1); n } n int main() { n   int n, m; n   cin >> n >> m; n   pair<int, int> arr[m]; n   for (int i = 0; i < m; i++) { n     cin >> arr[i].first >> arr[i].second; n   } n   if (helper(arr, -1, -1, m)) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 1010000, mm = 10100; n int n, m, i, j, k, t, a[mm], la[30], sa[30]; n map<int, int> ans; n char s[mn], c[30]; n bool cmp(int i, int j) { return la[i] > la[j]; } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   scanf( %d , &m); n   for (i = 1; i <= m; ++i) { n     scanf( %s , c + 1); n     k = strlen(c + 1); n     t = 0; n     for (j = 1; j <= k; ++j) t |= 1 << (c[j] - 97); n     ans[t] = 0, a[i] = t; n   } n   for (i = 0; i <= 25; ++i) sa[i] = i; n   for (i = 1; i <= n; ++i) { n     if (s[i] == s[i + 1]) continue; n     la[s[i] - 97] = i; n     k = s[i + 1] - 97, t = 0; n     sort(sa, sa + 26, cmp); n     for (j = 0; j <= 25; ++j) { n       if (!la[sa[j]] || sa[j] == k) break; n       t |= 1 << sa[j]; n       if (t == 5) { n         ++la[30]; n       } n       if (ans.count(t)) ++ans[t]; n     } n   } n   for (i = 1; i <= m; ++i) printf( %d n , ans[a[i]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int nn = 1; n   for (int i = 1; i < n; i++) { n     int c = 0; n     for (int j = 0; (1 << j) <= nn; j++) c++; n     nn += c; n   } n   vector<int> P; n   for (int i = 0; i < nn; i++) { n     bool f = false; n     for (int j = 0; (1 << j) <= i + 1; j++) n       if ((1 << j) == i + 1) f = true; n     for (int j = 0; j < (f ? 2 : 1); j++) P.push_back(i); n   } n   vector<int> type(m), t(m), l(m), r(m), x(m), v(m); n   for (int i = 0; i < m; i++) { n     cin >> type[i]; n     if (type[i] == 1) { n       cin >> t[i] >> l[i] >> r[i] >> x[i]; n       t[i]--, l[i]--, r[i]--; n     } else { n       cin >> t[i] >> v[i]; n       t[i]--, v[i]--; n     } n   } n   vector<set<int>> S(m); n   vector<vector<int>> qn(n); n   for (int i = m - 1; i >= 0; i--) { n     if (type[i] == 1) { n       int tt = t[i]; n       int tl = l[i]; n       int tr = r[i]; n       for (; tt >= 0; tt--) { n         for (int j = 0; j < (int)qn[tt].size(); j++) n           if (tl <= v[qn[tt][j]] && v[qn[tt][j]] <= tr) n             S[qn[tt][j]].insert(x[i]); n         tl = P[tl]; n         tr = P[tr]; n       } n     } else { n       qn[t[i]].push_back(i); n     } n   } n   for (int i = 0; i < m; i++) n     if (type[i] == 2) cout << S[i].size() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int m1[4] = {1, 1, 1, 1}; n int m2[4] = {1, 2, 4, 3}; n int m3[4] = {1, 3, 4, 2}; n int m4[4] = {1, 4, 1, 4}; n int main() { n   string s; n   cin >> s; n   int b = s[s.size() - 1] -  0 ; n   int a; n   if (s.size() > 1) n     a = s[s.size() - 2] -  0 ; n   else n     a = 0; n   int n = (10 * a + b) % 4; n   cout << (m1[n] + m2[n] + m3[n] + m4[n]) % 5; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr LL LINF = 334ll << 53; n constexpr int INF = 15 << 26; n constexpr LL MOD = 1E9 + 7; n struct Problem { n   LL n; n   vector<LL> p; n   Problem(LL n) : n(n), p(n){}; n   void solve() { n     LL ans = 0, minsum = LINF, sum = 0; n     for (int i = 0; i < n; ++i) { n       cin >> p[i]; n       p[i]--; n     } n     multiset<LL> s; n     for (int i = 0; i < n; ++i) { n       if (p[i] - i > 0) s.insert(p[i] - i); n       sum += abs(p[i] - i); n     } n     minsum = sum; n     ; n     for (int i = 1; i < n; ++i) { n       LL cnt; n       if (p[n - i] - (n - 1) > 0) n         cnt = (int)s.size() - 1; n       else n         cnt = (int)s.size(); n       sum += n - 1 - 2 * cnt; n       ; n       sum = sum - abs(p[n - i] - (n - 1)) + abs(p[n - i]); n       s.erase(i); n       if (p[n - i] > 0) s.insert(p[n - i] + i); n       ; n       ; n       if (sum < minsum) { n         minsum = sum; n         ans = i; n       } n     } n     cout << minsum <<     << ans <<   n ; n   } n }; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   long long n = 0; n   cin >> n; n   Problem p(n); n   p.solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, tt, sz, res = 1e9, a[55][100005], out[100005], cnt[100005], n                   mul[100005], que[1000005]; n int read() { n   int x = 0, fl = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fl = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * fl; n } n int check(int p, int len) { n   for (int i = 1; i <= len; i++) n     if (a[p][i]) return 0; n   return 1; n } n void dfs(int dep, int sum, int len) { n   if (sum > res || dep > 51) return; n   int now = check(dep, len); n   if (now) { n     res = sum; n     for (int i = 1; i <= t; i++) que[i] = cnt[i]; n     sz = t; n     return; n   } n   int fl = 1; n   for (int i = 1; i <= len; i++) n     if (a[dep][i] % 2) fl = 0; n   if (fl) { n     cnt[++t] = 3; n     for (int i = 1; i <= len; i++) a[dep + 1][i] = a[dep][i] / 2; n     int l0 = unique(a[dep + 1] + 1, a[dep + 1] + len + 1) - a[dep + 1] - 1; n     dfs(dep + 1, sum, l0); n     t--; n   } else { n     cnt[++t] = 1; n     for (int i = 1; i <= len; i++) n       a[dep + 1][i] = a[dep][i] + ((a[dep][i] & 1) > 0); n     int l0 = unique(a[dep + 1] + 1, a[dep + 1] + len + 1) - a[dep + 1] - 1; n     dfs(dep + 1, sum + 1, l0); n     t--; n     cnt[++t] = 2; n     for (int i = 1; i <= len; i++) n       a[dep + 1][i] = a[dep][i] - ((a[dep][i] & 1) > 0); n     l0 = unique(a[dep + 1] + 1, a[dep + 1] + len + 1) - a[dep + 1] - 1; n     dfs(dep + 1, sum + 1, l0); n     t--; n   } n   return; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[0][i] = read(); n   sort(a[0] + 1, a[0] + n + 1); n   dfs(0, 0, n); n   printf( %d n , res); n   for (int i = 1; i <= sz; i++) mul[i] = mul[i - 1] + (que[i] == 3); n   for (int i = 1; i <= sz; i++) { n     if (que[i] == 1) n       out[++tt] = -1 * pow(2, mul[i]); n     else if (que[i] == 2) n       out[++tt] = pow(2, mul[i]); n   } n   for (int i = 1; i <= tt; i++) printf( %d  , out[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n typedef struct smirror { n   int left, right, score; n } mirror; n int max(int a, int b) { return a > b ? a : b; } n int shoot_in(double x, mirror a[]) { n   for (int i = 1; i <= a[0].score; ++i) n     if (a[i].left <= x && x <= a[i].right) return i; n   return -1; n } n int deal(int n, mirror A[], mirror B[], int hl, int hr) { n   int counter = 1, hx = hl, tag, score0 = 0; n   int h0; n   if (n % 2 == 1) n     h0 = hl + hr + (n - 1) * 100; n   else n     h0 = n * 100 + hl - hr; n   bool ba[105], bb[105]; n   memset(ba, 0, sizeof(ba)); n   memset(bb, 0, sizeof(bb)); n   double d; n   for (counter = 1; counter <= n; counter++) { n     d = (hx * 100000) / (1.0 * h0); n     if (counter % 2 == 1) { n       tag = shoot_in(d, B); n       if (tag >= 0 && !bb[tag]) { n         score0 += B[tag].score; n         bb[tag] = 1; n       } else { n         score0 = 0; n         break; n       } n     } else { n       tag = shoot_in(d, A); n       if (tag >= 0 && !ba[tag]) { n         score0 += A[tag].score; n         ba[tag] = 1; n       } else { n         score0 = 0; n         break; n       } n     } n     hx += 100; n   } n   return score0; n } n int main() { n   mirror mi[2][105]; n   int num[2] = {0, 0}, hl, hr, n; n   scanf( %d%d%d , &hl, &hr, &n); n   while (n--) { n     int score, set = 1; n     char t; n     scanf( %d %c , &score, &t); n     if (t ==  T ) set = 0; n     ++num[set]; n     scanf( %d %d , &mi[set][num[set]].left, &mi[set][num[set]].right); n     mi[set][num[set]].score = score; n   } n   mi[0][0].score = num[0], mi[1][0].score = num[1]; n   int ans = 0; n   for (int i = 1; i <= num[0] + num[1]; ++i) n     ans = max(ans, deal(i, mi[0], mi[1], hl, hr)); n   for (int i = 1; i <= num[0] + num[1]; ++i) n     ans = max(ans, deal(i, mi[1], mi[0], 100 - hl, 100 - hr)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n const int maxn = 1e5 + 5; n map<long long, int> mp; n long long sum[maxn], ans; n int n, k; n void solve() { n   long long maxx = 0, minn = 0; n   scanf( %d%d , &n, &k); n   mp[0] = 1; n   for (int i = 1; i <= n; i++) { n     long long tmp; n     scanf( %I64d , &tmp); n     sum[i] = sum[i - 1] + tmp; n     mp[sum[i]]++; n     maxx = max(maxx, sum[i]); n     minn = min(minn, sum[i]); n     for (long long j = 1; - 1e15 <= j && j <= 1e15; j *= k) { n       if (mp.find(sum[i] - j) != mp.end()) { n         ans += mp[sum[i] - j]; n       } n       if (k == 1) break; n       if (j == -1 && k == -1) break; n     } n   } n   printf( %I64d n , ans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 2e5 + 9; n void test_case(int tc) { n   int k; n   cin >> k; n   int n = 12; n   int arr[12]; n   for (int i = 0; i <= 11; ++i) cin >> arr[i]; n   sort(arr, arr + 12, greater<int>()); n   int ans = 0; n   int temp = 0; n   if (k == 0) { n     cout << k <<   n ; n     return; n   } n   for (int i = 0; i <= n - 1; ++i) { n     temp += arr[i]; n     ans++; n     if (temp >= k) { n       cout << ans; n       return; n     } n   } n   cout << -1; n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int t = 1; n   for (int tc = 1; tc <= t; ++tc) test_case(tc); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, m; n long long ans, a[N], b[N]; n char c; n long long read() { n   long long x = 0; n   int sig = 1; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) sig = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) x = x * 10 + c - 48; n   return x * sig; n } n long long calc(int p) { n   b[n] = min(a[n], (long long)m * p - (long long)p * (p - 1) / 2 * n); n   for (int i = n - 1; i; i--) b[i] = min(a[i], b[i + 1] - p); n   if (b[1] < p + (long long)p * (p - 1) / 2 * n) return 0; n   long long sum = 0, n             s = max(1ll, b[1] - (long long)(m + 1) * (p - 1) + n                              (long long)p * (p - 1) / 2 * n), n             Buy; n   sum = Buy = b[1]; n   int Free = m - n * p - s + 1; n   for (int i = 2; i <= n; i++) { n     Buy += p; n     int Shift = min(b[i] - Buy, (long long)Free); n     Free -= Shift; n     Buy += Shift; n     sum += Buy; n   } n   return sum; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   ans = 0; n   for (int i = 1; i * n <= m; i++) ans = max(ans, calc(i)); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long x = 0, f = 1; n   char ch =    ; n   while (!isdigit(ch)) { n     ch = getchar(); n     if (ch ==  - ) f = -1; n   } n   while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch - 48), ch = getchar(); n   return x * f; n } n const int mod = 998244353; n int t, n, a[100005]; n vector<pair<long long, long long> > g[2]; n int main() { n   cin >> t; n   while (t--) { n     n = read(); n     long long ans = 0; n     for (int i = 1; i <= n; i++) a[i] = read(); n     g[n % 2].clear(); n     g[n % 2].push_back({a[n], 1}); n     for (int i = n; i > 1; i--) { n       int las = (i % 2), now = (las ^ 1); n       g[now].clear(); n       int las1 = 0; n       for (auto tmp : g[las]) { n         ans += ((a[i - 1] - 1) / tmp.first) * (i - 1) % mod * tmp.second % mod; n         int tmp1 = a[i - 1] / ((a[i - 1] - 1) / tmp.first + 1); n         if (tmp1 == las1) n           g[now][g[now].size() - 1].second += tmp.second % mod; n         else n           g[now].push_back({tmp1, tmp.second}), las1 = tmp1; n       } n       if (a[i - 1] == las1) n         g[now][g[now].size() - 1].second++; n       else n         g[now].push_back({a[i - 1], 1ll}); n       g[las] = g[now]; n     } n     cout << ans % mod << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, j, k, x, y, col; n vector<long long> ans; n long long last[100100]; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> y; n     col = 0; n     j = 1; n     while (j * j <= x) { n       if (x % j == 0) { n         k = x / j; n         if (last[j] < i - y + 1) { n           last[j] = i + 1; n           col++; n         } n         last[j] = i + 1; n         if (k != j && last[k] < i - y + 1) { n           col++; n         } n         last[k] = i + 1; n       } n       j++; n     } n     ans.push_back(col); n   } n   for (i = 0; i < n; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, i, j = -100, k, *a, cnt = 0, cnt1 = 0; n   cin >> n; n   a = new int[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n - 2; i++) { n     if (a[i] == 1 && a[i + 1] == 0 && a[i + 2] == 1) { n       if (i - j > 2) { n         cnt++; n         j = i; n         cnt1 = 0; n       } else { n         cnt1++; n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j; n   char map[9][9]; n   bool ok = false, ok1 = false; n   for (i = 0; i < 8; ++i) { n     scanf( %s , map[i]); n     for (j = 1; j < 8; ++j) { n       if (i == 0 && map[i][j] ==  W ) ok1 = true; n       if (map[i][j] == map[i][j - 1]) ok = true; n     } n     if (i == 0 && map[i][0] ==  W ) ok1 = true; n     if (map[i][0] == map[i][7]) ok = true; n   } n   if (!ok1 || ok) n     puts( NO ); n   else n     puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200007, inf = 0x3f3f3f3f; n vector<int> edges[N]; n int d[N]; n void bfs(int s) { n   queue<int> q; n   q.push(s); n   memset(d, -1, sizeof d); n   d[s] = 0; n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int v : edges[u]) { n       if (d[v] == -1) { n         q.push(v); n         d[v] = d[u] + 1; n       } n     } n   } n } n void no() { n   cout <<  No  << endl; n   exit(0); n } n int main() { n   ios::sync_with_stdio(false); n   srand(time(NULL)); n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  Yes  << endl; n     return 0; n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     edges[u].push_back(v); n     edges[v].push_back(u); n   } n   vector<int> vec; n   int a; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     vec.push_back(a); n   } n   memset(d, -1, sizeof d); n   int t = 0; n   d[1] = 0; n   if (vec[0] != 1) no(); n   for (int i = 0; i < n; ++i) { n     if (d[vec[i]] == -1) no(); n     ++t; n     for (int v : edges[vec[i]]) { n       if (d[v] == -1) d[v] = t; n     } n   } n   for (int i = 1; i < n; ++i) { n     if (d[vec[i]] < d[vec[i - 1]]) no(); n   } n   cout <<  Yes  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, j, a, b, c, t, cnt; n int main() { n   cin >> a >> b >> c; n   n = 0; n   t = 0; n   cnt = 0; n   while ((a > 0) || (b > 0) || (c > 0)) { n     switch (t) { n       case 0: { n         if (a > 0) a -= 2; n         break; n       } n       case 1: { n         if (b > 0) b -= 2; n         break; n       } n       case 2: { n         if (c > 0) c -= 2; n         break; n       } n     } n     cnt++; n     n++; n     t++; n     t %= 3; n   } n   n--; n   n += 30; n   cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<bool> prime(1001, true); n void sieve() { n   prime[0] = false; n   prime[1] = false; n   for (int i = 2; i <= 1000; i++) { n     if (prime[i]) { n       int j = 2; n       while (i * j <= 1000) { n         prime[i * j] = false; n         j++; n       } n     } n   } n } n long long int ceil_sqrt(long long int a) { n   long long int i = 1; n   while (i * i < a) i++; n   return i; n } n void solve() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i += 2) cout << a[i + 1] <<     << -1 * a[i] <<    ; n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); 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   if (a == 0 && b == 0) { n     cout <<  NO ; n     return 0; n   } n   if (abs(a - b) <= 1) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[300000]; n double cord[3], best[3]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   sort(arr, arr + n); n   int l = 0, r = 2000000000, res = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     int idx = 0; n     for (int i = 0; i < 3; i++) { n       cord[i] = (double)arr[idx] + (double)mid / 2.; n       while (idx < n && abs(arr[idx] - cord[i]) - 1e-7 < (double)mid / 2.) n         idx++; n     } n     if (idx == n) { n       for (int i = 0; i < 3; i++) best[i] = cord[i]; n       res = mid, r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %.6lf n , (double)res / 2.); n   printf( %.6lf  , (double)best[0]); n   for (int i = 1; i < 3; i++) printf( % .6lf , (double)best[i]); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x[n], y[n], h[n], a[n]; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n   } n   for (int i = 0; i < n; i++) { n     h[i] = n - 1; n     a[i] = 0; n   } n   int home[1000000] = {0}, away[1000000] = {0}; n   for (int i = 0; i < n; i++) { n     home[x[i]]++; n     away[y[i]]++; n   } n   for (int i = 0; i < n; i++) { n     if (home[y[i]] > 0) h[i] += home[y[i]]; n   } n   for (int i = 0; i < n; i++) { n     a[i] = 2 * (n - 1) - h[i]; n   } n   for (int i = 0; i < n; i++) cout << h[i] <<     << a[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n long long a[MAXN], b[MAXN]; n long long n, s; n bool check(long long k) { n   for (int i = 1; i <= n; i++) b[i] = 0; n   long long sum = s - 1, p = 2, q = 1, h = 1; n   a[1] = b[1] = 1; n   while (p <= n) { n     q *= k, h++; n     for (int i = 1; i <= q && p <= n; i++, p++) a[p] = h, sum -= h, b[h]++; n   } n   if (sum < 0) return 0; n   p = n; n   while (sum > 0) { n     h++; n     if (b[a[p]] == 1) --p; n     long long t = min(sum, h - a[p]); n     sum -= t, b[a[p]]--, a[p] += t, p--; n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> s; n   if (s < 2 * n - 1 || s > n * (n + 1) / 2) { n     cout <<  No  << endl; n     exit(0); n   } n   int l = 1, r = n - 1; n   while (l < r) { n     int m = (l + r) >> 1; n     if (check(m)) n       r = m; n     else n       l = m + 1; n   } n   check(l); n   sort(a + 1, a + n + 1); n   cout <<  Yes  << endl; n   for (int p = 1, i = 2, cnt = 0; i <= n; i++) { n     while (a[p] < a[i] - 1) p++, cnt = 0; n     cout << p <<    ; n     ++cnt; n     if (cnt == l) cnt = 0, ++p; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1505; n const int MOD = 1000000007; n inline int add(int x, int y) { return x + y >= MOD ? x + y - MOD : x + y; } n inline int sub(int x, int y) { return x - y < 0 ? x - y + MOD : x - y; } n inline int mul(int x, int y) { return 1ll * x * y % MOD; } n inline void inc(int &x, int y) { x = add(x, y); } n inline void dec(int &x, int y) { x = sub(x, y); } n inline void grow(int &x, int y) { x = mul(x, y); } n int bpow(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) grow(res, a); n     grow(a, a); n     b >>= 1; n   } n   return res; n } n int inv(int x) { return bpow(x, MOD - 2); } n int n, m, p, k; n int f[N], dp[N][N], sdp[N][N], sf[N], sfdp[N], fact[100005]; n int comb(int a, int b) { n   return mul(fact[a], mul(inv(fact[b]), inv(fact[a - b]))); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   int _a, _b; n   cin >> _a >> _b; n   p = mul(_a, inv(_b)); n   cin >> k; n   fact[0] = 1; n   for (int i = 1; i <= k; i++) fact[i] = mul(fact[i - 1], i); n   for (int i = 0; i <= min(k, m); i++) n     f[i] = mul(comb(k, i), mul(bpow(p, i), bpow(sub(1, p), k - i))); n   sf[0] = f[0]; n   for (int i = 1; i <= m; i++) sf[i] = add(sf[i - 1], f[i]); n   dp[0][m] = sdp[0][m] = 1; n   for (int i = 1; i <= n; i++) { n     memset(sfdp, 0, sizeof sfdp); n     for (int r = 1; r <= m; r++) n       sfdp[r] = add(sfdp[r - 1], mul(f[r], sdp[i - 1][r])); n     for (int r = 1; r <= m; r++) n       dp[i][r] = mul(f[m - r], n                      sub(mul(sf[r - 1], sub(sdp[i - 1][m], sdp[i - 1][m - r])), n                          sfdp[r - 1])); n     for (int r = 1; r <= m; r++) sdp[i][r] = add(sdp[i][r - 1], dp[i][r]); n   } n   cout << sdp[n][m] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read_int() { n   int x; n   scanf( %i , &x); n   return x; n } n inline int endline() { return printf(  n ); } n inline int space() { return printf(   ); } n inline int write_int(const int x) { return printf( %i , x); } n class color_ice_cream : public pair<bitset<500000>, bitset<500000> > { n   int n, m, last_used_color, *color; n   vector<int> *ice_cream, *neighbor; n  n  public: n   color_ice_cream() n       : n(read_int()), n         m(read_int()), n         last_used_color(-1), n         color(new int[m]), n         ice_cream(new vector<int>[n]), n         neighbor(new vector<int>[n]) { n     for (int i = 0; i < n; i++) { n       int k = read_int(); n       for (int j = 0; j < k; j++) ice_cream[i].push_back(read_int() - 1); n     } n     for (int i = 1; i < n; i++) { n       int u = read_int() - 1, v = read_int() - 1; n       neighbor[v].push_back(u); n       neighbor[u].push_back(v); n     } n     for (int i = 0; i < m; i++) color[i] = -1; n   } n   void solve(const int v = 0) { n     first.set(v); n     int i, j = -1, k = ice_cream[v].size(), l; n     for (i = 0; i < k; i++) n       if ((l = color[ice_cream[v][i]]) != -1) second.set(l); n     for (i = 0; i < k; i++) n       if (color[l = ice_cream[v][i]] == -1) { n         while (second[++j]) n           ; n         color[l] = j; n       } n     second.reset(); n     if (j > last_used_color) last_used_color = j; n     for (i = 0, k = neighbor[v].size(); i < k; i++) n       if (!first[l = neighbor[v][i]]) solve(l); n   } n   void write_result() { n     write_int(max(last_used_color, 0) + 1), endline(); n     for (int i = 0; i < m; i++) write_int(max(color[i], 0) + 1), space(); n   } n }; n int main() { n   color_ice_cream problem; n   problem.solve(); n   problem.write_result(); n } n


#include <bits/stdc++.h> n template <class T> n T _diff(T a, T b) { n   return (a < b ? b - a : a - b); n } n template <class T> n T _abs(T a) { n   return (a < 0 ? -a : a); n } n template <class T> n T _max(T a, T b) { n   return (a > b ? a : b); n } n template <class T> n T _min(T a, T b) { n   return (a < b ? a : b); n } n template <class T> n T max3(T a, T b, T c) { n   return (_max(a, b) > c ? _max(a, b) : c); n } n template <class T> n T min3(T a, T b, T c) { n   return (_min(a, b) < c ? _min(a, b) : c); n } n template <class T> n T GCD(T a, T b) { n   a = _abs(a); n   b = _abs(b); n   T tmp; n   while (a % b) { n     tmp = a % b; n     a = b; n     b = tmp; n   } n   return b; n } n template <class T> n T LCM(T a, T b) { n   a = _abs(a); n   b = _abs(b); n   return (a / GCD(a, b)) * b; n } n using namespace std; n int x, y, xp, yp, xn, ynw, xl, yl, chk, si, t; n int main() { n   cin >> x >> y; n   xn = ynw = 0; n   chk = 1; n   xl = 0; n   yl = 0; n   t = 0; n   si = 1; n   int row = 1, col = 0; n   t = 0; n   while (1) { n     xp = xn; n     yp = ynw; n     if (chk == 1) { n       si = 1; n       chk++; n       xl++; n       yl++; n     } else if (chk == 2) { n       si = 1; n       chk++; n     } else if (chk == 3) { n       si = -1; n       chk++; n     } else if (chk == 4) { n       si = -1; n       chk = 1; n     } n     if (row) { n       xn = (si) * (xl); n       row = 0; n       col = 1; n     } else { n       ynw = (si) * (yl); n       row = 1; n       col = 0; n     } n     if (x == xn && x == xp) { n       if (si == 1 && y >= yp && y <= ynw) n         break; n       else if (si == -1 && y >= ynw && y <= yp) n         break; n     } else if (y == ynw && y == yp) { n       if (si == 1 && x >= xp && x <= xn) n         break; n       else if (si == -1 && x <= xp && x >= xn) n         break; n     } n     t++; n   } n   cout << t << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(const pair<char, int>& x, const pair<char, int>& y) { n   int u = max(x.first -  a ,  z  - x.first); n   int v = max(y.first -  a ,  z  - y.first); n   return u > v; n } n int main() { n   if (0 & 1) freopen( input ,  r , stdin); n   if (0 & 2) freopen( output ,  w , stdout); n   int n, k; n   scanf( %d %d , &n, &k); n   vector<pair<char, int> > a(n); n   int id = 0; n   for (auto& e : a) { n     scanf(  %c , &e.first); n     e.second = id++; n   } n   sort(a.begin(), a.end(), cmp); n   string ans(n,    ); n   for (auto& e : a) { n     int u = e.first -  a , v =  z  - e.first, cur; n     if (u > v) { n       cur = min(k, u); n       ans[e.second] = e.first - cur; n     } else { n       cur = min(k, v); n       ans[e.second] = e.first + cur; n     } n     k -= cur; n   } n   if (k) n     printf( -1 n ); n   else n     printf( %s n , ans.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int M = 998244853, maxn = 4005; n int n, m; n void madd(int& a, int b) { n   a += b; n   if (a >= M) a -= M; n } n int ch[maxn][maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   ch[0][0] = 1; n   for (int i = 1; i < maxn; i++) { n     for (int j = 0; j <= i; j++) { n       madd(ch[i][j], ch[i - 1][j]); n       if (j > 0) madd(ch[i][j], ch[i - 1][j - 1]); n     } n   } n   int ans = 0; n   for (int k = 1; k <= n; k++) { n     if (n >= m + k) { n       madd(ans, ch[n + m][n]); n     } else { n       madd(ans, ch[n + m][m + k]); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int NAX = 3e5 + 1; n const int INF = 1e9 + 1; n const long long UMAX = 2e18 + 1; n long long sm(long long n) { n   int res = 0; n   while (n > 0) { n     res += n % 10; n     n /= 10; n   } n   return res; n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   vector<long long> ans; n   for (int i = 0; i < n; ++i) cin >> v[i]; n   sort(v.begin(), v.end()); n   long long p1 = v[0] * v[1] * v[2] * v[3] * v[n - 1]; n   long long p2 = v[n - 1] * v[n - 2] * v[n - 3] * v[n - 4] * v[n - 5]; n   long long p3 = v[0] * v[1] * v[n - 1] * v[n - 2] * v[n - 3]; n   ans.push_back(p1); n   ans.push_back(p2); n   ans.push_back(p3); n   cout << *max_element(ans.begin(), ans.end()) <<   n ; n   ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1; n   cin >> tc; n   while (tc--) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct Node { n   vector<Node*> edges; n   bool term; n   vector<int> last; n   Node() { n     edges.resize(5); n     fill(edges.begin(), edges.end(), nullptr); n     term = false; n   } n }; n void add_string(Node* root, string s) { n   for (char c : s) { n     c -=  a ; n     if (!root->edges[c]) { n       Node* node = new Node(); n       root->edges[c] = node; n     } n     root = root->edges[c]; n   } n   root->term = true; n   root->last.push_back(0); n } n void get_ans(Node* node, string s, int cnt, int& ans, int num) { n   if (cnt >= s.size()) { n     if (node->term) { n       for (int i = 0; i < node->last.size(); i++) { n         if (node->last[i] < num) { n           node->last[i] = num; n           ans++; n         } n       } n     } n     return; n   } n   char c = s[cnt]; n   c -=  a ; n   if (s[cnt] !=  ? ) { n     if (node->edges[c]) { n       get_ans(node->edges[c], s, cnt + 1, ans, num); n     } n   } else { n     get_ans(node, s, cnt + 1, ans, num); n     for (int i = 0; i < 5; i++) { n       if (node->edges[i]) { n         get_ans(node->edges[i], s, cnt + 1, ans, num); n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   Node* root = new Node(); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     add_string(root, s); n   } n   for (int i = 0; i < m; i++) { n     string s; n     cin >> s; n     int ans = 0; n     get_ans(root, s, 0, ans, i + 1); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long a[510]; n long long dp[510][510]; n long long stt[510]; n long long mod = 998244353; n long long solve(long long st, long long en) { n   if (st >= en) { n     return 1; n   } n   if (dp[st][en] != -1) return dp[st][en]; n   long long c = 1e9; n   for (int i = st; i <= en; i++) c = min(c, a[i]); n   long long sol = 0; n   long long sol1 = 0; n   for (int i = st; i <= stt[c]; i++) { n     sol += solve(i, stt[c] - 1) * solve(st, i - 1); n     sol %= mod; n   } n   for (int i = en; i >= stt[c]; i--) { n     sol1 += solve(stt[c] + 1, i) * solve(i + 1, en); n     sol1 %= mod; n   } n   sol *= sol1; n   sol %= mod; n   return dp[st][en] = sol; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   memset(stt, -1, sizeof stt); n   memset(dp, -1, sizeof dp); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     stt[a[i]] = i; n   } n   long long ss = solve(0, n - 1); n   cout << ss << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse4,avx ) n void run(std::istream &in, std::ostream &out) { n   int n; n   in >> n; n   std::vector<int> as(n); n   int sum = 0; n   for (int i = 0; i < n; i++) { n     in >> as[i]; n     sum += as[i]; n   } n   if (sum % 2 == 0) { n     for (int j = 2; j <= sum; j += 2) { n       if (sum % j != 0) continue; n       int d = sum / j; n       bool good = true; n       for (int k = 0; k < n; k++) { n         if ((as[k] % (2 * d)) != 0) { n           good = false; n           break; n         } n       } n       if (good) { n         out << 2 * d << std::endl; n         std::string s; n         for (int k = 0; k < n; k++) { n           s.append(std::string(as[k] / (2 * d),  a  + k)); n         } n         std::string ss = s; n         std::reverse(ss.begin(), ss.end()); n         s.append(ss); n         std::string res; n         for (int k = 0; k < d; k++) { n           res.append(s); n         } n         out << res << std::endl; n         return; n       } n     } n   } else { n     for (int j = 1; j <= sum; j += 2) { n       if (sum % j != 0) continue; n       int d = sum / j; n       int countOdd = 0; n       int oddIdx = 0; n       bool good = true; n       for (int k = 0; k < n; k++) { n         if ((as[k] % d) != 0) { n           good = false; n           break; n         } n         if (((as[k] / d) % 2) != 0) { n           countOdd++; n           oddIdx = k; n         } n       } n       if (!good) continue; n       if (countOdd != 1) continue; n       out << d << std::endl; n       std::string s; n       for (int k = 0; k < n; k++) { n         s.append(std::string(as[k] / d / 2,  a  + k)); n       } n       std::string ss = s; n       std::reverse(ss.begin(), ss.end()); n       s.append(ss); n       s.push_back((char)( a  + oddIdx)); n       std::string res; n       for (int k = 0; k < d; k++) { n         res.append(s); n       } n       out << res << std::endl; n       return; n     } n   } n   out << 0 << std::endl; n   std::string s; n   for (int k = 0; k < n; k++) { n     s.append(std::string(as[k],  a  + k)); n   } n   out << s << std::endl; n } n int main() { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   run(std::cin, std::cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int i, n, fl = 0; n     cin >> n; n     int *a = new int[n]; n     for (i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     sort(a, a + n); n     for (i = 1; i < n; ++i) { n       if (a[i] == a[i - 1]) { n         fl = 1; n         break; n       } n     } n     if (fl == 1) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   for (int i = s.size() - 1; i >= 0; i--) { n     int x = s[i] -  0 ; n     if (x >= 5) n       printf( -O| ); n     else n       printf( O-| ); n     x = x % 5; n     if (x == 0) n       printf( -OOOO n ); n     else if (x == 1) n       printf( O-OOO n ); n     else if (x == 2) n       printf( OO-OO n ); n     else if (x == 3) n       printf( OOO-O n ); n     else n       printf( OOOO- n ); n   } n } n