#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1 << 30; n int n, m, str[100100], lim[100100], up[100100], D[100100], col[100100 * 4]; n long long ans; n vector<int> F[100100 * 4]; n vector<long long> sum2[100100 * 4], sum[100100 * 4]; n bool cmp(int x, int y) { return D[x] > D[y]; } n void build(int node, int l, int r) { n   col[node] = -2; n   if (l == r) { n     F[node].push_back(l); n     sum[node].push_back(up[l]); n     sum2[node].push_back(lim[l]); n     return; n   } n   int mid = (l + r) >> 1, son = node << 1; n   build(son, l, mid); n   build(son | 1, mid + 1, r); n   F[node].resize(r - l + 1); n   sum[node].resize(r - l + 1); n   sum2[node].resize(r - l + 1); n   merge(F[son].begin(), F[son].end(), F[son | 1].begin(), F[son | 1].end(), n         F[node].begin(), cmp); n   sum[node][0] = up[F[node][0]], sum2[node][0] = lim[F[node][0]]; n   for (int i = 1, size = F[node].size(); i < size; i++) { n     sum[node][i] = sum[node][i - 1] + up[F[node][i]]; n     sum2[node][i] = sum2[node][i - 1] + lim[F[node][i]]; n   } n } n void update(int node) { n   if (col[node] > 0) col[node << 1] = col[(node << 1) | 1] = col[node]; n } n void getans(int node, int l, int r, int ll, int rr, int t) { n   if (l > rr || ll > r) return; n   if (ll <= l && rr >= r && (col[node] > 0 || col[node] == -2)) { n     if (col[node] > 0) { n       int dif = t - col[node]; n       int l = -1, r = F[node].size() - 1; n       while (l < r) { n         int mid = (l + r + 1) >> 1; n         if (D[F[node][mid]] >= dif) n           l = mid; n         else n           r = mid - 1; n       } n       if (l >= 0) ans += sum[node][l] * dif - sum2[node][l]; n       ans += sum2[node][F[node].size() - 1]; n     } else { n       for (int i = 0, size = F[node].size(); i < size; i++) n         ans += min((long long)lim[F[node][i]], n                    (long long)t * up[F[node][i]] + str[F[node][i]]); n     } n     col[node] = t; n     return; n   } n   update(node); n   int mid = (l + r) >> 1, son = node << 1; n   getans(son, l, mid, ll, rr, t); n   getans(son | 1, mid + 1, r, ll, rr, t); n   if (col[son] == col[son | 1]) n     col[node] = col[son]; n   else n     col[node] = -1; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) n     scanf( %d %d %d , &str[i], &lim[i], &up[i]), n         D[i] = up[i] == 0 ? Maxn : lim[i] / up[i]; n   build(1, 1, n); n   int t, l, r; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d , &t, &l, &r); n     ans = 0; n     getans(1, 1, n, l, r, t); n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int la, ra, ta, lb, rb, tb; n   scanf( %d%d%d , &la, &ra, &ta), ++ra; n   scanf( %d%d%d , &lb, &rb, &tb), ++rb; n   int g = gcd(ta, tb); n   ra -= la / g * g, la %= g; n   rb -= lb / g * g, lb %= g; n   if (lb > la) lb -= g, rb -= g; n   int ans = 0; n   ans = std::max(ans, std::min(ra, rb) - std::max(la, lb)); n   lb += g, rb += g; n   ans = std::max(ans, std::min(ra, rb) - std::max(la, lb)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1E4 + 10; n bool st[MAXN]; n int n, k, l, f[1 << 20], dis[25][25], id[MAXN], d[MAXN], tot; n vector<int> g[MAXN]; n queue<int> q; n inline void add(int u, int v) { n   g[u].push_back(v); n   g[v].push_back(u); n } n void bfs(int s) { n   memset(d, 0x3f, sizeof d); n   d[s] = 0; n   q.push(s); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int v : g[x]) n       if (d[v] == 0x3f3f3f3f) { n         d[v] = d[x] + 1; n         q.push(v); n       } n   } n   for (int i = 1; i <= n; i++) n     if (id[i]) dis[id[i]][id[s]] = min(dis[id[i]][id[s]], d[i]); n } n int main() { n   scanf( %d%d%d , &n, &k, &l); n   for (int i = 1, x; i <= k; i++) { n     scanf( %d , &x); n     st[x - 1] ^= 1; n     st[x] ^= 1; n   } n   for (int i = 0; i <= n; i++) n     if (st[i]) id[i] = ++tot; n   for (int i = 1, x; i <= l; i++) { n     scanf( %d , &x); n     for (int j = 1; j <= n - x + 1; j++) add(j - 1, j + x - 1); n   } n   memset(dis, 0x3f, sizeof dis); n   for (int i = 0; i <= n; i++) n     if (id[i]) bfs(i); n   memset(f, 0x3f, sizeof f); n   f[0] = 0; n   for (int s = 1; s < (1 << tot); s++) n     for (int i = 0; i < tot; i++) n       if ((s >> i) & 1) n         for (int j = 0; j < tot; j++) n           if ((s >> j) & 1) n             f[s] = min(f[s], f[s ^ (1 << i) ^ (1 << j)] + dis[i + 1][j + 1]); n   if (f[(1 << tot) - 1] == 0x3f3f3f3f) n     puts( -1 ); n   else n     printf( %d n , f[(1 << tot) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 3e5 + 5; n const int inf = 1 << 29; n inline int min(int a, int b) { return a < b ? a : b; } n inline int max(int a, int b) { return a > b ? a : b; } n inline int R() { n   char ch = getchar(); n   int x = 0; n   bool f = 0; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = 1; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); n   if (f == 1) x = -x; n   return x; n } n int a[Maxn], n, t, L[Maxn], N[Maxn], ans[Maxn]; n bool vis[Maxn]; n int main() { n   t = R(); n   while (t--) { n     n = R(); n     for (int i = 0; i <= n + 1; i++) { n       ans[i] = inf; n       N[i] = vis[i] = 0; n     } n     for (int i = 1; i <= n; i++) { n       a[i] = R(); n       L[a[i]] = 0; n     } n     for (int i = 1; i <= n; i++) { n       if (!L[a[i]]) n         L[a[i]] = i; n       else { n         N[L[a[i]]] = i; n         L[a[i]] = i; n       } n     } n     for (int i = 1; i <= n; i++) n       if (N[i] == 0) N[i] = n + 1; n     for (int i = 1; i <= n; i++) n       if (!vis[i]) { n         int M = i; n         for (int p = i; N[p] != 0; p = N[p]) { n           vis[p] = 1; n           M = max(M, N[p] - p); n         } n         ans[M] = min(ans[M], a[i]); n       } n     ans[0] = inf; n     for (int i = 1; i <= n; i++) ans[i] = min(ans[i], ans[i - 1]); n     for (int i = 1; i <= n; i++) { n       if (ans[i] == inf) ans[i] = -1; n       printf( %d  , ans[i]); n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void work(long long ang) { n   for (long long n = 3;; n++) { n     if (ang * n % 180 == 0) { n       if (ang * n / 180 <= n - 2) { n         printf( %I64d n , n); n         return; n       } n     } n     if (n == 998244353) break; n   } n } n int main() { n   int T; n   int n; n   scanf( %d , &T); n   for (; T--;) { n     scanf( %d , &n); n     work(n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n string s; n int n, ans; n int T, A[1010], B[1010], cnt, head[2010], nxt[100010], dis[100010], id; n bool w[100010]; n void link(int a, int b, int c) { n   nxt[++id] = head[a], head[a] = id, dis[id] = b, w[id] = c; n   nxt[++id] = head[b], head[b] = id, dis[id] = a, w[id] = c; n } n int power[2010]; n bool vis[2010], col[2010]; n bool dfs(int x) { n   for (int i = head[x]; i; i = nxt[i]) { n     if (!vis[dis[i]]) { n       vis[dis[i]] = 1; n       col[dis[i]] = col[x] ^ w[i]; n       if (dfs(dis[i])) return 1; n     } else if (col[dis[i]] ^ col[x] ^ w[i]) n       return 1; n   } n   return 0; n } n int main() { n   cin >> s; n   n = s.size(); n   s =     + s; n   power[0] = 1; n   for (int i = 1; i <= 2000; ++i) power[i] = power[i - 1] * 2 % MOD; n   for (int S = 2; S <= n; ++S) { n     memset(head, 0, sizeof(head)); n     id = cnt = 0; n     T = cnt++; n     for (int i = S; i <= n; ++i) A[i] = cnt++; n     for (int i = 1; i <= n; ++i) B[i] = cnt++; n     link(B[1], T, 1); n     link(A[S], T, 1); n     for (int i = S; i <= n; ++i) { n       if (s[i] ==  0 ) n         link(A[i], B[i], 0); n       else if (s[i] ==  1 ) n         link(A[i], B[i], 1); n     } n     for (int i = 1; i < S; ++i) { n       if (s[i] ==  0 ) n         link(B[i], T, 0); n       else if (s[i] ==  1 ) n         link(B[i], T, 1); n     } n     for (int i = S, j = n; i < j; ++i, --j) link(A[i], A[j], 0); n     for (int i = 1, j = n; i < j; ++i, --j) link(B[i], B[j], 0); n     memset(vis, 0, sizeof(vis)); n     int C = 0; n     for (int i = 0; i < cnt; ++i) n       if (!vis[i]) { n         ++C; n         if (dfs(i)) { n           C = -1; n           break; n         } n       } n     if (~C) ans = (ans + power[C - 1]) % MOD; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 4e6 + 5; n long long n, a[N], b[N], sum[N], I, lim = 1, mx = -1, Ans; n long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return f * x; n } n long long qpow(long long x, long long y) { n   long long ret = 1; n   while (y) { n     if (y & 1) ret = ret * x; n     y >>= 1; n     x = x * x; n   } n   return ret; n } n long long LG2(long long x) { n   long long ret = log2(x); n   if (qpow(2, ret) >= x) n     return ret; n   else n     return ret + 1; n } n int main() { n   memset(sum, 0, sizeof(sum)); n   memset(a, 0, sizeof(a)); n   memset(b, 0, sizeof(b)); n   n = read(), I = read(); n   for (register long long i = (1); i <= (n); i++) a[i] = read(); n   sort(a + 1, a + n + 1); n   a[0] = -1; n   for (register long long i = (1); i <= (n); i++) { n     if (a[i] != a[i - 1]) n       b[++b[0]] = 1; n     else n       b[b[0]]++; n   } n   for (register long long i = (2); i <= (b[0]); i++) { n     if (LG2(i) * n <= 8 * I) n       lim = i; n     else n       break; n   } n   if (lim >= b[0]) n     printf( 0 n ); n   else { n     for (register long long i = (1); i <= (b[0]); i++) n       sum[i] = sum[i - 1] + b[i]; n     for (register long long i = (lim); i <= (b[0]); i++) n       mx = max(mx, sum[i] - sum[i - lim]); n     Ans = sum[b[0]] - mx; n     printf( %lld n , Ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long w = 0, f = 1; n   char c =    ; n   while (c <  0  || c >  9 ) c = getchar(), f = c ==  -  ? -1 : f; n   while (c >=  0  && c <=  9 ) w = w * 10 + c - 48, c = getchar(); n   return w * f; n } n long long read14() { n   char c = getchar(); n   while (c !=  U  && c !=  D  && c !=  L  && c !=  R ) c = getchar(); n   if (c ==  U ) return 1; n   if (c ==  D ) return 2; n   if (c ==  L ) return 3; n   return 4; n } n long long n, m, p[200005], dl[200005], dr[200005]; n vector<long long> a[200005], e[200005]; n long long pos(long long x, long long y) { n   if (x >= 1 && x <= n && y >= 1 && y <= m) return (x - 1) * m + y; n   return n * m + 1 + ((x + y) & 1); n } n long long minn[200005 << 2], tag[200005 << 2], val[200005 << 2]; n void build(long long p, long long l, long long r) { n   if (l == r) { n     val[p] = 1; n     return; n   } n   long long mid = (l + r) >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   val[p] = val[p << 1] + val[p << 1 | 1]; n } n void down(long long p, long long l, long long r) { n   if (!tag[p]) return; n   minn[p << 1] += tag[p]; n   minn[p << 1 | 1] += tag[p]; n   tag[p << 1] += tag[p]; n   tag[p << 1 | 1] += tag[p]; n   tag[p] = 0; n } n void update(long long p, long long x, long long y, long long v, long long l, n             long long r) { n   if (x <= l && y >= r) { n     minn[p] += v, tag[p] += v; n     return; n   } n   down(p, l, r); n   long long mid = (l + r) >> 1; n   if (x <= mid) update(p << 1, x, y, v, l, mid); n   if (y > mid) update(p << 1 | 1, x, y, v, mid + 1, r); n   if (minn[p << 1] == minn[p << 1 | 1]) n     val[p] = val[p << 1] + val[p << 1 | 1]; n   else if (minn[p << 1] < minn[p << 1 | 1]) n     val[p] = val[p << 1]; n   else n     val[p] = val[p << 1 | 1]; n   minn[p] = min(minn[p << 1], minn[p << 1 | 1]); n } n long long now, ans; n void dfs1(long long u) { n   dl[u] = ++now; n   for (auto v : e[u]) dfs1(v); n   dr[u] = now; n } n void dfs2(long long u, long long rt) { n   if (u != rt) update(1, dl[p[u]], dr[p[u]], 1, 1, now), ans -= val[1] - 1; n   for (auto v : e[u]) dfs2(v, rt); n   if (u != rt) update(1, dl[p[u]], dr[p[u]], -1, 1, now); n } n signed main() { n   n = read(), m = read(), ans = n * n * m * m / 4; n   for (long long i = 1; i <= n; i++) { n     a[i].push_back(0); n     for (long long j = 1; j <= m; j++) { n       a[i].push_back(read14()); n       if (a[i][j] == 1) n         e[pos(i + 2, j)].push_back(pos(i, j)), p[pos(i, j)] = pos(i + 1, j); n       if (a[i][j] == 2) n         e[pos(i - 2, j)].push_back(pos(i, j)), p[pos(i, j)] = pos(i - 1, j); n       if (a[i][j] == 3) n         e[pos(i, j + 2)].push_back(pos(i, j)), p[pos(i, j)] = pos(i, j + 1); n       if (a[i][j] == 4) n         e[pos(i, j - 2)].push_back(pos(i, j)), p[pos(i, j)] = pos(i, j - 1); n     } n   } n   dfs1(n * m + 1); n   build(1, 1, now); n   dfs2(n * m + 2, n * m + 2); n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, apple[200000], a, b; n long long tot = 0; n vector<int> adj[200000]; n int GCD(long long x, long long y) { n   if (y == 0) return x; n   return GCD(y, x % y); n } n pair<long long, long long> DFS(int ant, int act) { n   if (adj[act].size() == 1) return make_pair(apple[act], 1); n   vector<pair<long long, long long> > V; n   for (int i = 0; i < adj[act].size(); i++) { n     if (adj[act][i] == ant) continue; n     V.push_back(DFS(act, adj[act][i])); n   } n   long long mn = V[0].first; n   for (int i = 0; i < V.size(); i++) mn = min(mn, V[i].first); n   long long mcm = V[0].second; n   for (int i = 1; i < V.size(); i++) { n     if (mcm > mn) break; n     mcm = (mcm * V[i].second) / GCD(mcm, V[i].second); n   } n   return make_pair(V.size() * mcm * (mn / mcm), V.size() * mcm); n } n int main() { n   scanf( %d , &(n)); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &(apple[i])); n     tot += apple[i]; n   } n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &(a), &(b)); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   adj[1].push_back(0); n   cout << tot - DFS(0, 1).first << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   int k, n; n   cin >> n >> k; n   int val[100000]; n   int awake[100000]; n   for (int i = 0; i < n; ++i) { n     cin >> val[i]; n   } n   int sum = 0; n   int extra = 0; n   int max = 0; n   for (int i = 0; i < n; ++i) { n     cin >> awake[i]; n     if (awake[i]) { n       sum += val[i]; n       val[i] = 0; n     } n     if (i < k) { n       extra += val[i]; n     } else { n       extra = extra + val[i] - val[i - k]; n     } n     if (extra > max) { n       max = extra; n     } n   } n   sum += max; n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int c, e = 0; n   long long int s = 0, a; n   for (long long int i = 0; i < n; i++) { n     cin >> a; n     s += a; n     if (a % 2 != 0) { n       e++; n       if (e == 1) n         c = a; n       else if (e > 1) n         c = min(c, a); n     } n   } n   if (e % 2 != 0) s -= c; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = int(2e5 + 5); n int n, m, S; n int POW[25]; n int f[MAXN]; n bool vis[MAXN]; n bool flag[MAXN]; n long long tmp, tot, Ans, t; n struct edge { n   int u, v, id; n   int w, c; n } e[MAXN]; n struct node { n   int to, id; n   node() {} n   node(int T, int I) : to(T), id(I) {} n }; n vector<node> G[MAXN]; n void Addedge(int u, int v, int id) { n   G[u].push_back(node(v, id)); n   G[v].push_back(node(u, id)); n } n void Init() { n   POW[0] = 1; n   for (int i = 1; i <= 25; i++) POW[i] = 2 * POW[i - 1]; n } n int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); } n bool cmp(edge a, edge b) { return a.w < b.w; } n bool cmp2(edge a, edge b) { return a.id < b.id; } n void Kruskal() { n   sort(e + 1, e + m + 1, cmp); n   for (int i = 1; i <= n; i++) f[i] = i; n   t = 0; n   e[0].c = int(1e9 + 5); n   for (int i = 1; i <= m; i++) { n     int u = e[i].u, v = e[i].v; n     int fx = Find(u), fy = Find(v); n     if (fx != fy) { n       Addedge(u, v, i); n       f[fx] = fy; n       vis[i] = 1; n       tmp += e[i].w; n       if (e[i].c < e[t].c) t = i; n     } n   } n   tot = tmp; n   tmp -= S / e[t].c; n } n int MaxId(int a, int b) { return (e[a].w < e[b].w) ? b : a; } n int Max[MAXN][25 + 5]; n int fa[MAXN][25 + 5]; n int dep[MAXN]; n void Dfs(int u) { n   for (int i = 1; i <= 25; i++) { n     fa[u][i] = fa[fa[u][i - 1]][i - 1]; n     Max[u][i] = MaxId(Max[u][i - 1], Max[fa[u][i - 1]][i - 1]); n   } n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i].to; n     if (v == fa[u][0]) continue; n     dep[v] = dep[u] + 1; n     Max[v][0] = G[u][i].id; n     fa[v][0] = u; n     Dfs(v); n   } n } n int LCA(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   int t = dep[u] - dep[v]; n   for (int i = 0; POW[i] <= t; i++) n     if (POW[i] & t) u = fa[u][i]; n   if (u == v) return u; n   for (int i = 25; i >= 0; i--) n     if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i]; n   return fa[u][0]; n } n int Query(int u, int v) { n   int t = dep[u] - dep[v]; n   int ret = 0; n   for (int i = 0; POW[i] <= t; i++) n     if (POW[i] & t) { n       ret = MaxId(ret, Max[u][i]); n       u = fa[u][i]; n     } n   return ret; n } n int main() { n   Init(); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     e[i].id = i; n     scanf( %d , &e[i].w); n   } n   for (int i = 1; i <= m; i++) scanf( %d , &e[i].c); n   for (int i = 1; i <= m; i++) scanf( %d%d , &e[i].u, &e[i].v); n   scanf( %d , &S); n   Kruskal(); n   e[0].w = -int(1e9 + 5); n   fill(Max[1], Max[1] + 25 + 1, 0); n   Dfs(1); n   long long Ans = tmp; n   int Last = -1, now = -1; n   for (int i = 1; i <= m; i++) n     if (e[i].c < e[t].c) { n       int u = e[i].u, v = e[i].v; n       int q = LCA(u, v); n       int x = MaxId(Query(u, q), Query(v, q)); n       tmp = (long long)tot - e[x].w + e[i].w - S / e[i].c; n       if (Ans > tmp) { n         Ans = tmp; n         vis[(Last != -1) ? Last : 0] = 1; n         vis[x] = 0; n         Last = x; n         vis[(now != -1) ? now : 0] = 0; n         vis[i] = 1; n         now = i; n       } n     } n   printf( %I64d n , Ans); n   if (now == -1) now = t; n   for (int i = 1; i <= m; i++) n     if (vis[i]) { n       if (now == i) e[i].w -= S / e[i].c; n       flag[e[i].id] = 1; n     } n   sort(e + 1, e + m + 1, cmp2); n   for (int i = 1; i <= m; i++) n     if (flag[i]) printf( %d %d n , i, e[i].w); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10; n const long long inf = 1e14 + 10; n long long n, k; n long long a[N]; n long long ps[N]; n long long fen[N]; n vector<long long> vec; n void upd(long long x) { n   for (; x < N; x += (x & (-x))) { n     fen[x]++; n   } n } n long long get(long long x) { n   long long ret = 0; n   for (; x > 0; x -= (x & (-x))) { n     ret += fen[x]; n   } n   return ret; n } n bool check(long long K) { n   long long res = 0; n   for (long long i = 0; i < N; i++) { n     fen[i] = 0; n   } n   for (long long i = 1; i <= n; i++) { n     long long val = ps[i] - K; n     long long ind = upper_bound(vec.begin(), vec.end(), val) - vec.begin(); n     res += get(ind); n     ind = upper_bound(vec.begin(), vec.end(), ps[i]) - vec.begin(); n     upd(ind); n     if (ps[i] >= K) { n       res++; n     } n   } n   if (res >= k) { n     return true; n   } else { n     return false; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     ps[i] = ps[i - 1] + a[i]; n     vec.push_back(ps[i]); n   } n   sort(vec.begin(), vec.end()); n   long long L = -inf; n   long long R = inf; n   while (R - L > 1) { n     long long mid = (L + R) / 2; n     if (check(mid)) { n       L = mid; n     } else { n       R = mid; n     } n   } n   cout << L; n   return 0; n } n


#include <bits/stdc++.h> n int cmpfunc(const void* a, const void* b) { return (*(int*)a - *(int*)b); } n int main() { n   int t, x, i, j, c; n   scanf( %d , &t); n   while (t--) { n     char n[101]; n     int m[101]; n     scanf( %s , &n); n     x = strlen(n); n     j = 0; n     c = 0; n     for (i = 0; i < x; i++) { n       if (n[i] ==  1 ) n         c++; n       else if (c > 0) { n         m[j++] = c; n         c = 0; n       } n     } n     if (c > 0) m[j++] = c; n     qsort(m, j, sizeof(int), cmpfunc); n     c = 0; n     for (i = j - 1; i >= 0; i = i - 2) c = c + m[i]; n     printf( %d n , c); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ans[100004] = {0}; n vector<long long> v[100005]; n int main() { n   long long n, m, i, j, ms = 0, sum = 0, res = 0; n   scanf( %lld %lld , &n, &m); n   for (i = 0; i < n; i++) { n     long long a, b; n     scanf( %lld %lld , &a, &b); n     v[a].push_back(b); n   } n   for (i = 1; i <= m; i++) { n     sum = 0; n     sort(v[i].begin(), v[i].end()); n     long long si = v[i].size(); n     ms = max(ms, si); n     for (j = si - 1; j >= 0; j--) { n       sum += v[i][j]; n       if (sum > 0) n         ans[si - j] += sum; n       else n         break; n     } n   } n   for (i = 1; i <= ms; i++) res = max(res, ans[i]); n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int cmx = 1e5 + 5; n int n, k; n char a[505][505]; n int id[505][505]; n int cc_sz[505 * 505]; n int seen[505 * 505]; n bool inside(int x, int y) { n   if (0 <= min(x, y) and max(x, y) < n) return true; n   return false; n } n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n void dfs(int x, int y, int cc_no) { n   id[x][y] = cc_no; n   cc_sz[cc_no]++; n   for (int i = 0; i < 4; i++) { n     int nx = x + dx[i]; n     int ny = y + dy[i]; n     if (inside(nx, ny) and (id[nx][ny] == 0 and a[nx][ny] ==  . )) n       dfs(nx, ny, cc_no); n   } n } n void add(int x, int y, int &ans, int flag) { n   if (a[x][y] ==  .  and seen[id[x][y]] != flag) { n     seen[id[x][y]] = flag; n     ans += cc_sz[id[x][y]]; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = (0); i <= (n - 1); ++i) n     for (int j = (0); j <= (n - 1); ++j) cin >> a[i][j]; n   int which_cc = 0; n   for (int i = (0); i <= (n - 1); ++i) n     for (int j = (0); j <= (n - 1); ++j) { n       if (id[i][j] == 0 and a[i][j] ==  . ) { n         dfs(i, j, ++which_cc); n       } n     } n   int counter = 1; n   int best_ans = 0; n   for (int y_low = 0; (y_low + k) <= n; y_low++) { n     for (int x = 0; x < k; x++) n       for (int y = y_low; y < (y_low + k); y++) --cc_sz[id[x][y]]; n     for (int x_low = 0; (x_low + k) <= n; x_low++) { n       int ans = k * k; n       for (int x = 0; x < k; x++) { n         if (inside(x_low + x, y_low - 1)) n           add(x_low + x, y_low - 1, ans, counter); n         if (inside(x_low + x, y_low + k)) n           add(x_low + x, y_low + k, ans, counter); n       } n       for (int y = 0; y < k; y++) { n         if (inside(x_low - 1, y_low + y)) n           add(x_low - 1, y_low + y, ans, counter); n         if (inside(x_low + k, y_low + y)) n           add(x_low + k, y_low + y, ans, counter); n       } n       counter++; n       best_ans = max(best_ans, ans); n       if ((x_low + k) != n) { n         for (int y = 0; y < k; y++) { n           ++cc_sz[id[x_low][y + y_low]]; n           --cc_sz[id[x_low + k][y + y_low]]; n         } n       } n     } n     for (int x = n - k; x < n; x++) n       for (int y = 0; y < k; y++) ++cc_sz[id[x][y_low + y]]; n   } n   cout << best_ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int mark = 1; n   for (int i = 1; i < s.size(); i++) { n     if (s[i] != s[i - 1]) { n       mark = -1; n       break; n     } n   } n   if (mark == 1) { n     cout << s; n     for (int i = 1; i < k; i++) { n       cout << s[0]; n     } n     return 0; n   } n   for (int i = s.size() - 1; i >= 1; i--) { n     string t1 = s.substr(0, i); n     string t2 = s.substr(s.size() - i, i); n     if (t1 == t2) { n       int len = s.size() - i; n       string ost = s.substr(i, len); n       string ans = s; n       for (int j = 1; j < k; j++) { n         ans += ost; n       } n       int kol = 0; n       for (int j = 0; j + s.size() <= ans.size(); j++) { n         string mr = ans.substr(j, s.size()); n         if (mr == s) { n           kol++; n           if (kol == k) { n             string prt = ans.substr(0, j + s.size()); n             cout << prt << endl; n             return 0; n           } n         } n       } n     } n   } n   for (int i = 1; i <= k; i++) { n     cout << s; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s[200003], m, cnt, ans[200003]; n vector<pair<int, int> > v[200003], g[200003]; n bool vis[200003]; n int dfs(int x) { n   vis[x] = 1; n   int ret = 0, tmp; n   for (int i = 0; i < v[x].size(); i++) n     if (!vis[v[x][i].first]) { n       tmp = dfs(v[x][i].first); n       ret += tmp; n       ans[v[x][i].second] = -tmp; n     } n   for (int i = 0; i < g[x].size(); i++) n     if (!vis[g[x][i].first]) { n       tmp = dfs(g[x][i].first); n       ret += tmp; n       ans[g[x][i].second] = tmp; n     } n   return ret - s[x]; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , s + i), cnt += s[i]; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     v[x].push_back(make_pair(y, i)); n     g[y].push_back(make_pair(x, i)); n   } n   if (cnt) { n     cout <<  Impossible ; n     return 0; n   } n   cout <<  Possible ; n   dfs(0); n   for (int i = 0; i < m; i++) printf(  n%d , ans[i]); n } n


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


#include <bits/stdc++.h> n using namespace std; n struct Counter { n   static int k; n   Counter() { k++; } n   ~Counter() { k--; } n }; n int Counter::k = 0; n template <typename T> n void pr(const string& name, T t) { n   cout << name <<   =   << t << endl; n } n template <typename T, typename... Types> n void pr(const string& names, T t, Types... rest) { n   auto comma_pos = names.find( , ); n   cout << names.substr(0, comma_pos) <<   =   << t <<  ,  ; n   auto next_name_pos = names.find_first_not_of(   t n , comma_pos + 1); n   pr(string(names, next_name_pos), rest...); n } n void mod(long long& a, long long b) { n   a %= b; n   if (a < 0) a += b; n } n const long long MOD = 1000000007; n deque<long long> DQ; n long long Days[15], N, M; n int main() { n   cin >> N >> M; n   string line; n   Days[1] = 0; n   Days[2] = 31; n   Days[3] = 59; n   Days[4] = 90; n   Days[5] = 120; n   Days[6] = 151; n   Days[7] = 181; n   Days[8] = 212; n   Days[9] = 243; n   Days[10] = 273; n   Days[11] = 304; n   Days[12] = 334; n   getline(cin, line); n   while (true) { n     getline(cin, line); n     if (cin.eof()) break; n     long long year = 0, month = 0, day = 0; n     for (int i = (0); i <= (3); i++) year = 10 * year + line[i] -  0 ; n     for (int i = (5); i <= (6); i++) month = 10 * month + line[i] -  0 ; n     for (int i = (8); i <= (9); i++) day = 10 * day + line[i] -  0 ; n     long long days = 365 * (year - 1) + Days[month] + day - 1; n     days += (year - 1) / 4; n     if (year % 4 == 0 && month > 2) days++; n     long long hr = 10 * (line[11] -  0 ) + line[12] -  0 ; n     long long mn = 10 * (line[14] -  0 ) + line[15] -  0 ; n     long long sc = 10 * (line[17] -  0 ) + line[18] -  0 ; n     long long sec = days * 60 * 60 * 24 + 3600 * hr + 60 * mn + sc; n     if (!DQ.empty() && sec - DQ.front() >= N) DQ.pop_front(); n     DQ.push_back(sec); n     if (DQ.size() >= M) { n       cout << line.substr(0, 19) <<   n ; n       return 0; n     } n   } n   cout <<  -1 ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[102], b[102]; n int main(void) { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; ++i) scanf( %d %d , &a[i], &b[i]); n   for (int x = 1; x <= n; ++x) { n     int c = 0; n     for (int i = 0; i < m; ++i) n       if (x >= a[i] && x <= b[i]) ++c; n     if (c != 1) { n       printf( %d %d n , x, c); n       return 0; n     } n   } n   puts( OK ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n long double a[maxn]; n int main() { n   int n, s; n   long long k, b; n   while (~scanf( %d , &n)) { n     s = 0; n     for (int i = 0; i < n; i++) { n       scanf( %I64d %I64d , &k, &b); n       if (k != 0) { n         a[s++] = -(double)(b * 1.0) / (k * 1.0); n       } n     } n     sort(a, a + s); n     int count = 0; n     for (int i = 1; i < s; i++) { n       if (fabs(a[i] - a[i - 1]) > 0) { n         count++; n       } n     } n     if (s > 0) { n       count++; n     } n     printf( %d n , count); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long bip(long long x, long long n, long long mod) { n   long long res = 1; n   while (n) { n     if (n & 1) { n       res = (res * x) % mod; n     } n     x = (x * x) % mod; n     n >>= 1; n   } n   return res; n } n const int maxn = INT_MAX; n const long long inf = 1e18; n const int mod = 998244353; n const int N = 1e5 + 10; n void solve() { n   string a, b; n   cin >> a >> b; n   if ((int)b.size() > (int)a.size()) { n     sort(a.begin(), a.end()); n     reverse(a.begin(), a.end()); n     cout << a; n     return; n   } n   vector<int> cnt(10, 0); n   for (int i = 0; i < (int)a.size(); i++) cnt[a[i] -  0 ]++; n   vector<int> ans; n   for (int i = 0; i < (int)b.size(); i++) { n     int k = b[i] -  0 ; n     bool ok = 0; n     for (int j = k; j >= 0; j--) { n       if (cnt[j]) { n         ok = 1; n         cnt[j]--; n         ans.push_back(j); n         if (j < k) { n           for (int l = 9; l >= 0; l--) { n             while (cnt[l]--) ans.push_back(l); n           } n           for (auto it : ans) cout << it; n           return; n         } n         break; n       } n     } n     if (ok == 0) { n       for (int p = i - 1; p >= 0; p--) { n         int k = b[p] -  0 ; n         ans.pop_back(); n         cnt[k]++; n         bool ok = 0; n         for (int j = k - 1; j >= 0; j--) { n           if (cnt[j]) { n             ok = 1; n             break; n           } n         } n         if (ok) { n           for (int j = k - 1; j >= 0; j--) { n             if (cnt[j]) { n               ans.push_back(j); n               cnt[j]--; n               for (int l = 9; l >= 0; l--) { n                 while (cnt[l]--) ans.push_back(l); n               } n               for (auto it : ans) cout << it; n               return; n             } n           } n         } n       } n     } n   } n   for (auto it : ans) cout << it; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int tt = 1; n   while (tt--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100000 + 19; n int a[Maxn], Le[Maxn], Ri[Maxn], cnt, L, R, n, i, ans; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   Le[cnt = 1] = 1; n   a[++n] = a[n - 1] - 1; n   for (i = 1; i <= n; i++) n     if (a[i] <= a[i - 1]) { n       Ri[cnt] = i - 1; n       ans = max(ans, Ri[cnt] - Le[cnt] + 1); n       Le[++cnt] = i; n     } n   for (i = 1; i < cnt; i++) { n     if (Ri[i] < n - 1 || Le[i] > 1) ans = max(ans, Ri[i] - Le[i] + 2); n     if (i > 1) n       if (Ri[i - 1] > Le[i - 1] && Ri[i] > Le[i]) { n         if (a[Ri[i - 1] - 1] < a[Le[i]] - 1 || a[Ri[i - 1]] < a[Le[i] + 1] - 1) n           ans = max(ans, Ri[i] - Le[i - 1] + 1); n       } else { n         if (Ri[i - 1] == Le[i - 1]) ans = max(ans, Ri[i] - Le[i] + 2); n         if (Ri[i] == Le[i]) ans = max(ans, Ri[i - 1] - Le[i - 1] + 2); n       } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> path[1005]; n long long d[105], t[105]; n int n, m, u, v; n queue<int> q; n void work(int now) { n   for (int i = 1; i <= n; i++) { n     d[i] = 1 << 30; n     t[i] = 0; n   } n   d[now] = 0; n   t[now] = 1; n   q.push(now); n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     for (int i = 0; i < path[u].size(); i++) { n       v = path[u][i]; n       if (d[v] > d[u] + 1) { n         d[v] = d[u] + 1; n         t[v] = t[u]; n         q.push(v); n       } else if (d[v] == d[u] + 1) { n         t[v] += t[u]; n       } n     } n   } n } n int main() { n   long long len = 0, tmp = 0; n   double ans = 1.0; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &u, &v); n     path[u].push_back(v); n     path[v].push_back(u); n   } n   int start = 1; n   work(start), start++; n   len = d[n], tmp = t[n]; n   for (int i = start; i < n; i++) { n     start = 1; n     work(i); n     int see = 0; n     see = d[start] + d[n]; n     if (see == len) { n       double k = 0; n       k = (double)(2.0 * t[start] * t[n]) / (1.0 * tmp); n       ans = max(ans, k); n     } n   } n   printf( %.12lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p, q, b, n, tmp; n long long gcd(int x, int y) { return y ? gcd(y, x % y) : x; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p >> q >> b; n     q = q / gcd(q, p); n     while ((tmp = gcd(q, b)) != 1) n       while (q % tmp == 0) q /= tmp; n     if (q == 1) n       cout <<  Finite  << endl; n     else n       cout <<  Infinite  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n long long binpow(long long a, long long b) { n   if (b == 0) return 1; n   long long res = binpow(a, b / 2); n   if (b % 2) n     return res * res * a; n   else n     return res * res; n } n vector<int> adj[200006]; n bool vis[200006]; n bool color[200006]; n bool dfs(int x) { n   vis[x] = 1; n   for (int i = 0; i < adj[x].size(); i++) { n     if (!vis[adj[x][i]]) { n       color[adj[x][i]] = !color[x]; n       if (!dfs(adj[x][i])) return false; n     } else if (color[adj[x][i]] == color[x]) { n       return false; n     } n   } n   return true; n } n bool check(int a, int b) { n   while (a != 0) { n     int x = a % 10; n     if (x == 7) return 1; n     a /= 10; n   } n   while (b != 0) { n     int xx = b % 10; n     if (xx == 7) return 1; n     b /= 10; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt; n   cin >> tt; n   vector<pair<pair<int, int>, char>> vv; n   int ewq = 0; n   while (tt--) { n     char c; n     int r, l; n     cin >> c >> l >> r; n     vv.push_back({{l, r}, c}); n   } n   sort(vv.begin(), vv.end()); n   int F = 0; n   int M = 0; n   for (int i = 1; i <= 366; i++) { n     F = 0; n     M = 0; n     for (int j = 0; j < vv.size(); j++) { n       if (i >= vv[j].first.first && i <= vv[j].first.second) { n         if (vv[j].second ==  F ) n           F++; n         else n           M++; n       } n     } n     ewq = max(min(F, M), ewq); n   } n   cout << ewq * 2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   string x; n   cin >> x; n   string ans = x; n   string y =   ; n   long long p = n; n   for (long long i = 1; i < n; i++) { n     long long j; n     y =   ; n     for (j = 0; i + j < n; j++) { n       if (x[i + j] != ans[j]) break; n       y += ans[j]; n     } n     if (i + j == n) { n       p = i; n       break; n     } n   } n   p = n - p; n   while (p--) { n     ans.erase(ans.begin()); n   } n   cout << x; n   while (--k) { n     cout << ans; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int W = 1005; n const long long inf = 1e18; n struct dsu { n   vector<int> pr; n   vector<int> weights; n   vector<long long> beauties; n   set<int> roots; n   dsu(int _n) { n     pr.resize(_n + 5); n     weights.resize(_n + 5); n     beauties.resize(_n + 5); n     iota((pr).begin(), (pr).end(), 0); n     for (int u = (1); u <= ((_n)); u += (+1)) { n       roots.insert(u); n     } n   } n   int find(int u) { return pr[u] == u ? u : pr[u] = find(pr[u]); } n   bool unite(int u, int v) { n     u = find(u); n     v = find(v); n     if (u != v) { n       pr[u] = v; n       beauties[v] += beauties[u]; n       weights[v] += weights[u]; n       roots.erase(u); n       return true; n     } n     return false; n   } n }; n set<int> who[W]; n long long dp[W]; n int weights[N]; n long long beauties[N]; n bool done[W]; n int n, m, max_w; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15); n   cin >> n >> m >> max_w; n   dsu dg(n); n   for (int u = (1); u <= ((n)); u += (+1)) { n     int w; n     cin >> w; n     dg.weights[u] = weights[u] = w; n   } n   for (int u = (1); u <= ((n)); u += (+1)) { n     long long b; n     cin >> b; n     dg.beauties[u] = beauties[u] = b; n   } n   for (int i = (0); i <= ((m)-1); i += (+1)) { n     int u, v; n     cin >> u >> v; n     dg.unite(u, v); n   } n   done[0] = true; n   for (int u : dg.roots) { n     for (int w = (max_w); w >= (0); w += (-1)) { n       if (w - dg.weights[u] < 0) { n         break; n       } n       int ru = dg.find(u); n       if (dp[w - dg.weights[u]] + dg.beauties[u] > dp[w] && n           !who[w - dg.weights[ru]].count(ru) && done[w - dg.weights[u]]) { n         done[w] = true; n         dp[w] = dp[w - dg.weights[u]] + dg.beauties[u]; n         who[w] = who[w - dg.weights[u]]; n         who[w].insert(ru); n       } n     } n   } n   for (int u = (1); u <= ((n)); u += (+1)) { n     for (int w = (max_w); w >= (0); w += (-1)) { n       if (w - weights[u] < 0) { n         break; n       } n       int ru = dg.find(u); n       if (dp[w - weights[u]] + beauties[u] > dp[w] && n           !who[w - weights[u]].count(ru) && done[w - weights[u]]) { n         done[w] = true; n         dp[w] = dp[w - weights[u]] + beauties[u]; n         who[w] = who[w - weights[u]]; n         who[w].insert(ru); n       } n     } n   } n   cout << *max_element(dp, dp + max_w + 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 152; n const int maxp = 51123987; n static int A[maxn], len; n static int opt[maxn][52][52][52], n, Ne[maxn][4]; n char s[maxn]; n static int idx[maxn * 10]; n int main() { n   scanf( %d n , &n); n   for (int i = 1; i <= n; i++) { n     s[++len] = getchar(); n     if (s[len] == s[len - 1]) len--; n   } n   for (int i = len; i; i--) { n     int kd = s[i] ==  a ; n     if (s[i] ==  b ) kd = 2; n     if (s[i] ==  c ) kd = 3; n     Ne[i][1] = Ne[i + 1][1]; n     Ne[i][2] = Ne[i + 1][2]; n     Ne[i][3] = Ne[i + 1][3]; n     Ne[i][kd] = i; n   } n   int mxl = (n - 1) / 3 + 1; n   opt[Ne[1][1]][1][0][0] = 1; n   opt[Ne[1][2]][0][1][0] = 1; n   opt[Ne[1][3]][0][0][1] = 1; n   for (int i = 1; i <= len; i++) n     for (int j = 0; j <= mxl; j++) n       for (int k = 0; k <= mxl; k++) n         for (int l = 0; l <= mxl; l++) { n           opt[Ne[i][1]][j + 1][k][l] = n               opt[Ne[i][1]][j + 1][k][l] + opt[i][j][k][l]; n           if (opt[Ne[i][1]][j + 1][k][l] >= maxp) n             opt[Ne[i][1]][j + 1][k][l] -= maxp; n           opt[Ne[i][2]][j][k + 1][l] = n               opt[Ne[i][2]][j][k + 1][l] + opt[i][j][k][l]; n           if (opt[Ne[i][2]][j][k + 1][l] >= maxp) n             opt[Ne[i][2]][j][k + 1][l] -= maxp; n           opt[Ne[i][3]][j][k][l + 1] = n               opt[Ne[i][3]][j][k][l + 1] + opt[i][j][k][l]; n           if (opt[Ne[i][3]][j][k][l + 1] >= maxp) n             opt[Ne[i][3]][j][k][l + 1] -= maxp; n         } n   int ans = 0; n   for (int i = 1; i <= len; i++) { n     if (n % 3 == 0) ans = (ans + opt[i][mxl][mxl][mxl]) % maxp; n     if (n % 3 == 1) n       ans = (ans + opt[i][mxl - 1][mxl - 1][mxl] + n              opt[i][mxl - 1][mxl][mxl - 1] + opt[i][mxl][mxl - 1][mxl - 1]) % n             maxp; n     if (n % 3 == 2) n       ans = (ans + opt[i][mxl][mxl - 1][mxl] + opt[i][mxl - 1][mxl][mxl] + n              opt[i][mxl][mxl][mxl - 1]) % n             maxp; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char c[600]; n vector<int> check; n int n; n vector<int> cal; n int calculate() { n   int num = 0; n   cal.clear(); n   int minimum = INT_MAX; n   for (int i = 1; i <= n; i++) { n     if (c[i - 1] ==  ( ) { n       num++; n       minimum = min(minimum, num); n       cal.push_back(num); n     } else { n       num--; n       minimum = min(minimum, num); n       cal.push_back(num); n     } n   } n   int ret = 0; n   for (int i = 1; i <= n; i++) { n     if (minimum == cal[i - 1]) { n       ret++; n     } n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   scanf( %s , c); n   int ans_l = 1, ans_r = 1; n   int beauty = INT_MIN; n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (c[i - 1] ==  ( ) { n       cnt++; n     } else { n       cnt--; n     } n   } n   if (cnt != 0) { n     printf( 0 n1 1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       char temp = c[j - 1]; n       c[j - 1] = c[i - 1]; n       c[i - 1] = temp; n       int now_beauty = calculate(); n       if (beauty < now_beauty) { n         beauty = now_beauty; n         ans_l = i; n         ans_r = j; n       } n       temp = c[j - 1]; n       c[j - 1] = c[i - 1]; n       c[i - 1] = temp; n     } n   } n   printf( %d n%d %d , beauty, ans_l, ans_r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n const int N = 1e6; n int T, n, r, A[1005]; n void solve() { n   cin >> n >> r; n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n   } n   int ans = 0; n   int z; n   for (int i = 1; i <= n; i = z + r) { n     bool can_warm = false; n     z = -1; n     int left = max(i - r + 1, 0), right = min(i + r, n + 1); n     for (int j = left; j < right; j++) { n       if (A[j]) { n         can_warm = true; n         z = j; n       } n     } n     if (!can_warm) { n       cout << -1; n       return; n     } n     ans++; n   } n   cout << ans; n } n int(main)() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   T = 1; n   while (T--) { n     solve(); n   } n } n


#include<iostream> n #include<bits/stdc++.h> n using namespace std; n #define boost ios_base::sync_with_stdio(false); cin.tie(NULL) n #define ll unsigned long long int n #define tinput int t; cin>>t; while(t--) n #define rep(i,n,s) for((i)=(s); (i)<(n); (i)++) n  n template <typename T> n T getmini(T x, T y) { return (x<y)?x:y; } n template <typename T> n T getmaxi(T x, T y) { return (x>y)?x:y; } n  n int main(){ n     #ifndef ONLINE_JUDGE n     freopen( b_input.txt ,  r , stdin);  freopen( b_output.txt ,  w , stdout); n     #endif n     boost; n      n     tinput { n      int x, y; n      cin>>x>>y; n      int ans = x+y; n      if(abs(x-y)>1) { n       ans = ans + abs(x-y) - 1; n      } n      cout<<ans<<endl; n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int pa[maxn], a[maxn], b[maxn]; n struct Edge { n   int u, v, w; n   friend bool operator<(const Edge& a, const Edge& b) { return a.w > b.w; } n } e[maxn]; n void init(int n) { n   for (int i = 1; i <= n; i++) pa[i] = i, b[i] = 1; n } n int find(int x) { n   if (x == pa[x]) return x; n   return pa[x] = find(pa[x]); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   init(n); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &e[i].u, &e[i].v); n     e[i].w = min(a[e[i].u], a[e[i].v]); n   } n   sort(e + 1, e + m + 1); n   double sum = 0; n   for (int i = 1; i <= m; i++) { n     int fu = find(e[i].u); n     int fv = find(e[i].v); n     if (fu != fv) { n       sum += 1.0 * e[i].w * b[fu] * b[fv]; n       pa[fv] = fu; n       b[fu] += b[fv]; n       b[fv] = 1; n     } n   } n   printf( %.6f n , sum * 2 / (1.0 * n * (n - 1))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M = 1e9 + 7; n long long power(long long a, long long p) { n   if (p == 0) return 1; n   long long ans = power(a, p / 2); n   ans = (ans * ans) % M; n   if (p % 2) ans = (ans * a) % M; n   return ans; n } n int nC2(int n) { return (n * (n - 1)) / 2; } n int fact(int n) { n   if (n == 0 || n == 1) { n     return 1; n   } else { n     return n * fact(n - 1); n   } n } n int countOne(int n) { return __builtin_popcount(n); } n int checkParity(int n) { return __builtin_parity(n); } n int digitCount(int n) { return floor(log10(n)) + 1; } n int log2(int x) { n   int res = 0; n   while (x >>= 1) res++; n   return res; n } n int isPowerof2(int x) { return (x && !(x & x - 1)); } n bool is_prime(int n) { n   if (n == 1) { n     return false; n   } n   int i = 2; n   while (i * i <= n) { n     if (n % i == 0) { n       return false; n     } n     i += 1; n   } n   return true; n } n int main() { n   int test, n, m, x, y, counter = 0, sum = 0, ans = 0, res = 0, rem = 0; n   bool flag = true; n   bool check = true; n   string s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0 && flag == true) { n       cout <<  a ; n       check = true; n     } else if (i % 2 != 0 && check == true) { n       cout <<  b ; n       flag = false; n     } else if (i % 2 == 0 && flag == false) { n       cout <<  b ; n       check = false; n     } else if (i % 2 != 0 && check == false) { n       cout <<  a ; n       flag = true; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void test_case(); n long long power(int n, int p); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     test_case(); n   } n   return 0; n } n void test_case() { n   int n, x; n   cin >> n >> x; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr.begin(), arr.end()); n   int teams = 0; n   int cnt = 1; n   for (int ptr = n - 1; ptr >= 0; ptr--) { n     if (arr[ptr] * cnt >= x) { n       teams++; n       cnt = 1; n     } else n       cnt++; n   } n   cout << teams <<   n ; n } n long long power(int n, int p) { n   long long ans = 1; n   if (p == 0) return 1; n   long long temp = power(n, p / 2); n   if (p % 2 == 0) n     ans = (temp * temp) % 1000000007; n   else { n     ans = (ans * n) % 1000000007; n     ans *= (temp * temp) % 1000000007; n   } n   return ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct rec { n   int l, r; n }; n rec f[2222][2223]; n bool can[2222][2223]; n int n, m, k, x, y, xx, yy; n void del(int x, int y) { n   can[x][y] = false; n   for (int i = 1; i <= m; i++) { n     f[x][i].l = f[x][i - 1].l; n     if (can[x][i - 1]) f[x][i].l = i - 1; n   } n   for (int i = m; i >= 1; i--) { n     f[x][i].r = f[x][i + 1].r; n     if (can[x][i + 1]) f[x][i].r = i + 1; n   } n   return; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       f[i][j].l = j - 1; n       f[i][j].r = j + 1; n       can[i][j] = true; n     } n   for (int i = 1; i <= n; i++) { n     f[i][m + 1].r = m + m; n   } n   while (k--) { n     int ans = n + m + n + m; n     scanf( %d%d , &x, &y); n     for (int i = 1; i <= n; i++) { n       int u = abs(i - x); n       if (can[i][y]) { n         if (ans > u) { n           ans = u; n           xx = i; n           yy = y; n         } n       } else { n         if (f[i][y].l > 0 && ans > u + abs(f[i][y].l - y)) { n           ans = u + abs(f[i][y].l - y); n           xx = i; n           yy = f[i][y].l; n         } n         if (f[i][y].r < m + 1 && ans > u + abs(f[i][y].r - y)) { n           ans = u + abs(f[i][y].r - y); n           xx = i; n           yy = f[i][y].r; n         } n       } n       if (ans == 0) break; n     } n     printf( %d %d n , xx, yy); n     del(xx, yy); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100007], b[100007], c[100007]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i < n + 1; i++) cin >> a[i]; n   for (int i = 1; i < n + 1; i++) { n     if (a[i] > a[i - 1]) n       b[i] = b[i - 1] + 1; n     else n       b[i] = 1; n   } n   c[n] = 1; n   a[n + 1] = INT_MAX; n   for (int i = n - 1; i > 0; i--) { n     if (a[i + 1] > a[i]) n       c[i] = c[i + 1] + 1; n     else n       c[i] = 1; n   } n   int mx = 0; n   for (int i = 1; i < n + 1; i++) { n     if (b[i] + 1 <= n) n       mx = max(mx, b[i] + 1); n     else n       mx = max(mx, b[i]); n     if (a[i - 1] + 1 < a[i + 1]) mx = max(mx, b[i - 1] + c[i + 1] + 1); n   } n   cout << mx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int N = 132007; n int n, a, b; n int rev[N], fac[N], finv[N], f[N], g[N], h[N]; n inline int qpow(int x, int k, int r = 1) { n   for (; k; k >>= 1, x = 1ll * x * x % mod) n     if (k & 1) r = 1ll * r * x % mod; n   return r; n } n inline void ntt(int *a, int len, int opt) { n   for (int i = 0; i < len; ++i) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int i = 1; i < len; i <<= 1) { n     int wn = qpow(3, (mod - 1) / (i << 1)); n     for (int j = 0; j < len; j += i << 1) { n       int w = 1; n       for (int k = 0; k < i; ++k, w = 1ll * w * wn % mod) { n         int x = a[j + k], y = 1ll * a[j + k + i] * w % mod; n         a[j + k] = (x + y) % mod; n         a[j + k + i] = (x - y + mod) % mod; n       } n     } n   } n   if (~opt) return; n   reverse(a + 1, a + len); n   const int inv = qpow(len, mod - 2); n   for (int i = 0; i < len; ++i) a[i] = 1ll * a[i] * inv % mod; n } n void solve(int n) { n   if (!n) return g[1] = 1, void(); n   int t = n - 1 >> 1, mid = n + 1 >> 1, len = 1, bit = 0; n   for (; len <= n + 2; len <<= 1, ++bit) n     ; n   solve(t); n   for (int i = 0; i < len; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1) << bit - 1; n   for (int i = f[0] = 1; i <= mid; ++i) f[i] = 1ll * f[i - 1] * mid % mod; n   for (int i = 0; i <= mid; ++i) n     f[i] = 1ll * f[i] * finv[i] % mod, h[i] = 1ll * g[i] * fac[i] % mod; n   for (int i = mid + 1; i < len; ++i) f[i] = h[i] = 0; n   reverse(f, f + mid + 2); n   ntt(f, len, 1); n   ntt(h, len, 1); n   for (int i = 0; i < len; ++i) h[i] = 1ll * f[i] * h[i] % mod; n   ntt(h, len, -1); n   for (int i = 0; i <= mid; ++i) h[i] = 1ll * h[i + mid + 1] * finv[i] % mod; n   for (int i = mid + 1; i < len; ++i) h[i] = 0; n   ntt(g, len, 1); n   ntt(h, len, 1); n   for (int i = 0; i < len; ++i) g[i] = 1ll * g[i] * h[i] % mod; n   ntt(g, len, -1); n   if (n & 1) return; n   for (int i = mid + mid + 1; i < len; ++i) g[i] = 0; n   for (int i = len - 1; i; --i) g[i] = (g[i - 1] + 1ll * g[i] * n) % mod; n } n inline int C(int n, int m) { n   return 1ll * fac[n] * finv[m] % mod * finv[n - m] % mod; n } n int main() { n   scanf( %d%d%d , &n, &a, &b); n   long long ans = 0; n   if (a == b && a == 1) return printf( %d n , n == 1) & 0; n   if (a + b > n + 1 || !a || !b) return printf( %d n , 0) & 0; n   for (int i = fac[0] = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; n   finv[n] = qpow(fac[n], mod - 2); n   for (int i = n; i; --i) finv[i - 1] = 1ll * finv[i] * i % mod; n   solve(n - 2); n   return printf( %lld n , 1ll * g[a + b - 2] * C(a + b - 2, a - 1) % mod) & 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x; n vector<int> v; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x); n     int a = lower_bound(v.begin(), v.end(), x) - v.begin(); n     if (a == v.size()) n       v.push_back(x); n     else n       v[a] = x; n   } n   printf( %d n , v.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int b; n   cin >> n >> b; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int mini[n]; n   mini[0] = a[0]; n   for (int i = 1; i < n; i++) { n     mini[i] = min(mini[i - 1], a[i]); n   } n   int maxi = b; n   for (int i = 1; i < n; i++) { n     maxi = max(maxi, b % mini[i] + (b / mini[i]) * a[i]); n   } n   cout << maxi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n int n, m, x, y, w, cnt, col[maxn], dis[maxn], vis[maxn], d[maxn], head[maxn]; n struct node { n   int nxt, to, val; n } e[maxn << 1]; n void add(int x, int y, int w) { n   e[++cnt].to = y; n   e[cnt].nxt = head[x]; n   head[x] = cnt; n   e[cnt].val = w; n } n queue<int> q; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &x, &y, &w); n     add(y, x, w); n   } n   if (n == 1) { n     cout <<  0  << endl <<  0 ; n     return 0; n   } n   memset(col, -1, sizeof(col)); n   q.push(n); n   vis[n] = 0; n   while (q.size()) { n     int x = q.front(); n     q.pop(); n     if (x == 1) { n       break; n     } n     for (int i = head[x]; i; i = e[i].nxt) { n       int v = e[i].to; n       if (vis[v]) continue; n       if (col[v] == -1 || col[v] == 1 - e[i].val) { n         col[v] = 1 - e[i].val; n       } else { n         q.push(v); n         d[v] = d[x] + 1; n         vis[v] = 1; n       } n     } n   } n   if (d[1]) { n     cout << d[1] << endl; n   } else { n     cout <<  -1  << endl; n   } n   for (int i = 1; i <= n; ++i) { n     if (col[i] == -1) { n       cout <<  0 ; n     } else { n       cout << col[i]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fpow(long long int n, long long int k, n                    long long int p = 1000000007) { n   long long int r = 1; n   n = n % p; n   while (k > 0) { n     if (k & 1) { n       r = (r * n) % p; n     } n     k = k >> 1; n     n = (n * n) % p; n   } n   return r; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   if (n <= 9) { n     cout << 1 <<   n ; n     cout << n; n   } else { n     long long int ans = -1; n     for (long long int i = 1; i <= 9; i++) { n       if (n % i == 0) ans = i; n     } n     cout << n / ans <<   n ; n     for (long long int i = 1; i <= n / ans; i++) cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 51, MOD = 1e9 + 7; n struct Matrix { n   int num[6][6]; n   Matrix() { memset(num, 0, sizeof(num)); } n   inline int* operator[](const int& x) { return num[x]; } n   inline const int* operator[](const int& x) const { return num[x]; } n }; n Matrix mat, mat2, x, x2; n int f1, f2, f3, c, res; n long long int n; n inline long long int read() { n   register long long int num = 0, neg = 1; n   register char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) { n     ch = getchar(); n   } n   if (ch ==  - ) { n     neg = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = (num << 3) + (num << 1) + (ch -  0 ); n     ch = getchar(); n   } n   return num * neg; n } n inline int qpow(int base, int exponent) { n   int res = 1; n   while (exponent) { n     if (exponent & 1) { n       res = (long long int)res * base % MOD; n     } n     base = (long long int)base * base % MOD, exponent >>= 1; n   } n   return res; n } n inline Matrix operator*(Matrix x, Matrix y) { n   Matrix res; n   for (register int i = 1; i <= 5; i++) { n     for (register int j = 1; j <= 5; j++) { n       for (register int k = 1; k <= 5; k++) { n         res[i][j] = (res[i][j] + (long long int)x[i][k] * y[k][j] % (MOD - 1)) % n                     (MOD - 1); n       } n     } n   } n   return res; n } n inline Matrix qpow(Matrix base, long long int exponent) { n   Matrix res; n   for (register int i = 1; i <= 5; i++) { n     res[i][i] = 1; n   } n   while (exponent) { n     if (exponent & 1) { n       res = res * base; n     } n     base = base * base, exponent >>= 1; n   } n   return res; n } n int main() { n   n = read(), f1 = read(), f2 = read(), f3 = read(), c = read(), res = 1; n   mat[1][1] = mat[2][1] = mat[3][1] = mat[1][2] = mat[2][3] = mat[4][4] = n       mat[5][4] = 1; n   mat[5][5] = x[1][5] = mat2[1][1] = 1, mat[4][1] = 2, mat[5][1] = MOD - 5, n   x[1][4] = 3; n   mat2[2][1] = mat2[3][1] = mat2[1][2] = mat2[2][3] = x2[1][3] = 1; n   res = qpow(c, (x * qpow(mat, n - 3))[1][1]); n   res = (long long int)res * qpow(f1, (x2 * qpow(mat2, n - 3))[1][1]) % MOD, n   x2[1][3] = 0, x2[1][2] = 1; n   res = (long long int)res * qpow(f2, (x2 * qpow(mat2, n - 3))[1][1]) % MOD, n   x2[1][2] = 0, x2[1][1] = 1; n   res = (long long int)res * qpow(f3, (x2 * qpow(mat2, n - 3))[1][1]) % MOD, n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, i, n, ans = 0, k = 0, mx; n   cin >> n; n   long long int a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   ans = a[0] / 3; n   k = a[0] % 3; n   for (i = 1; i < n; i++) { n     mx = min(k, a[i] / 2); n     ans += mx; n     k -= mx; n     a[i] -= 2 * mx; n     ans += a[i] / 3; n     k += a[i] % 3; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void data() {} n const int N = 1e6 + 100; n const long long mod = 998244353; n const long long mod2 = 1e9 + 7; n int main() { n   data(); n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long k; n   cin >> k; n   string second; n   cin >> second; n   vector<long long> pos; n   long long ans = 0; n   if (k == 0) { n     long long ans = 0; n     long long cur = 0; n     for (long long i = 0; i < second.size(); i++) { n       if (second[i] ==  0 ) { n         cur++; n       } else { n         ans += cur * (cur + 1) / 2; n         cur = 0; n       } n     } n     ans += cur * (cur + 1) / 2; n     cout << ans <<   n ; n     return 0; n   } n   pos.push_back(0); n   for (long long i = 0; i < second.size(); i++) { n     if (second[i] ==  0 ) { n       continue; n     } n     pos.push_back(i + 1); n   } n   pos.push_back(second.size() + 1); n   long long cur = 0; n   for (long long i = 0; i < second.size(); i++) { n     if (second[i] ==  0 ) { n       continue; n     } n     cur++; n     if (cur >= k) { n       ans += (pos[cur - k + 1] - pos[cur - k]) * (pos[cur + 1] - pos[cur]); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int INF = (1 << 30) - 1; n long long Gcd(long long a, long long b) { n   while (a > 0 && b > 0) { n     if (a > b) n       a %= b; n     else n       b %= a; n   } n   return a + b; n } n long long x, y; n int main() { n   cin >> x >> y; n   if (Gcd(x, y) != 1) { n     printf( Impossible n ); n     return 0; n   } n   vector<char> ans; n   while (x > 0 && y > 0) { n     if (x > y) { n       long long k = x / y; n       x %= y; n       if (x == 0) k--; n       vector<char> g; n       while (k) { n         g.push_back(k % 10 +  0 ); n         k /= 10; n       } n       for (int i = g.size() - 1; i >= 0; --i) ans.push_back(g[i]); n       ans.push_back( A ); n     } else { n       long long k = y / x; n       y %= x; n       if (y == 0) k--; n       vector<char> g; n       while (k) { n         g.push_back(k % 10 +  0 ); n         k /= 10; n       } n       for (int i = g.size() - 1; i >= 0; --i) ans.push_back(g[i]); n       ans.push_back( B ); n     } n   } n   for (int i = 0; i < ans.size(); ++i) printf( %c , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   char ch; n   for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar()) n     ; n   long long x = ch -  0 ; n   for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar()) n     x = x * 10 + ch -  0 ; n   return x; n } n const int N = 2 * 1e5 + 5; n struct node { n   long long j, k; n   node(long long _j = 0, long long _k = 0) { n     j = _j; n     k = _k; n   } n   friend bool operator<(node x, node y) { n     return x.j > y.j || x.j == y.j && x.k > y.k; n   } n }; n set<node> f; n long long a[N]; n int main() { n   int n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   f.insert(node(a[1] - 1, 0)); n   for (int i = 1; i <= n - 1; i++) { n     long long m = a[i + 1], Mx = 0; n     bool pty = 0; n     while (1) { n       set<node>::iterator pos = f.begin(); n       long long j = (*pos).j, k = (*pos).k; n       if (j < m) break; n       pty = 1; n       Mx = max(Mx, k + (long long)i * ((j + 1) / m * m - m)); n       f.insert(node(j % m, k + (long long)i * (j - j % m))); n       f.erase(pos); n     } n     if (pty) f.insert(node(m - 1, Mx)); n   } n   long long ans = 0; n   for (set<node>::iterator pos = f.begin(); pos != f.end(); pos++) { n     long long j = (*pos).j, k = (*pos).k; n     ans = max(ans, k + (long long)j * n); n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10, inf = 1e9 + 10, P = 53, MOD = 1e9 + 7; n vector<int> a, b; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, x, t = 0; n   long long ans = 1; n   cin >> n >> x; n   for (int i = 0; i < 42; i++) n     if ((1ll << i) & x) { n       ans *= 2; n       n -= (1ll << i); n       t++; n     } n   if (n < 0 || (n == 0 && t == 1)) { n     cout << 0; n     return 0; n   } n   if (n == 0) { n     cout << ans - 2; n     return 0; n   } n   for (int i = 0; i < 42; i++) n     if (!((1ll << i) & x)) n       if ((1ll << (i + 1)) & n) n -= (1ll << (i + 1)); n   if (n == 0) n     cout << ans; n   else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n const int M = 1100000; n const long long mod = 1e9 + 7; n const int inf = (int)1e9; n const double eps = 1e-9; n long long n, k, a[N]; n long long f[N], inv[N], fac[N], sinv[N], g[N]; n long long pw[N]; n long long pow_mod(long long a, long long e) { n   long long res = 1; n   for (; e; a = a * a % mod, e >>= 1) n     if (e & 1) res = res * a % mod; n   return res; n } n long long num; n long long C(int n, int m) { n   long long res = fac[n] * sinv[m] % mod; n   res = res * sinv[n - m] % mod; n   return res; n } n map<int, int> S; n long long cal(int i) { n   if (g[i] != -1) return g[i]; n   long long fm = pow_mod(pow_mod(k, num), mod - 2); n   long long res = 0; n   for (int j = 0; j <= num; j++) { n     long long tmp = C(num, j) * f[i + j] % mod; n     res = (res + tmp * pw[num - j]) % mod; n   } n   res = res * fm % mod; n   g[i] = res; n   return g[i]; n } n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] == -1) n       num++; n     else n       S[a[i]]++; n   } n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= n; i++) inv[i] = pow_mod(i, mod - 2); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * (long long)i) % mod; n   sinv[0] = 1; n   for (int i = 1; i <= n; i++) sinv[i] = (sinv[i - 1] * inv[i]) % mod; n   f[0] = 0, f[1] = 0; n   for (int i = 2; i <= n; i++) { n     long long a = i - 1; n     long long tmp1 = (k - 1) * a + n - a; n     tmp1 %= mod; n     tmp1 = tmp1 * f[i - 1] % mod; n     long long tmp2 = (k - 1) * a % mod; n     tmp2 %= mod; n     tmp2 = tmp2 * f[i - 2] % mod; n     long long tmp3 = a * k % mod; n     long long tmp = (tmp1 - tmp2 - tmp3 + mod + mod) % mod; n     f[i] = tmp * inv[n - a] % mod; n   } n   pw[0] = 1; n   for (int i = 1; i <= n; i++) pw[i] = (pw[i - 1] * (k - 1)) % mod; n   memset(g, -1, sizeof(g)); n   long long ans = 0; n   for (auto p : S) { n     int val = p.second; n     ans = (ans + cal(val)) % mod; n   } n   ans = (ans + (k - S.size()) * cal(0)) % mod; n   cout << (ans - f[n] + mod) % mod <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int ara[5001], i, n, mx = 0, j, ans[5001] = {0}, col[5001] = {0}, eq, k; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &ara[i]); n   for (i = 1; i <= n; i++) { n     for (k = 1; k <= n; k++) col[k] = 0; n     mx = 0, eq = 0; n     for (j = i; j <= n; j++) { n       col[ara[j]]++; n       if (col[ara[j]] > mx || (col[ara[j]] == mx && ara[j] < eq)) { n         mx = col[ara[j]]; n         eq = ara[j]; n       } n       ans[eq]++; n     } n   } n   for (i = 1; i <= n; i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> pr(n); n   vector<vector<int>> nx(n); n   vector<int> a(m); n   long long tot = 0; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     a[i]--; n     if (i) { n       tot += labs(a[i] - a[i - 1]); n       pr[a[i]].push_back(a[i - 1]); n       nx[a[i - 1]].push_back(a[i]); n     } else { n       pr[a[i]].push_back(a[i]); n     } n   } n   nx[a[m - 1]].push_back(a[m - 1]); n   long long res = tot; n   for (int x = 0; x < n; x++) { n     if (!pr[x].size()) continue; n     long long was = 0; n     for (int i = 0; i < pr[x].size(); i++) n       was += labs(x - pr[x][i]) + labs(x - nx[x][i]); n     vector<int> v; n     for (int i = 0; i < pr[x].size(); i++) { n       if (pr[x][i] != x) v.push_back(pr[x][i]); n       if (nx[x][i] != x) v.push_back(nx[x][i]); n     } n     sort(v.rbegin(), v.rend()); n     long long now = 0; n     for (int i = 0; i < v.size(); i++) now += v[0] - v[i]; n     long long best = now; n     for (int i = 1; i < v.size(); i++) { n       now += i * (v[i - 1] - v[i]); n       now -= ((int)v.size() - i) * (v[i - 1] - v[i]); n       best = min(best, now); n     } n     res = min(res, tot - was + best); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class X, class Y> n bool minimize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x > y + eps) { n     x = y; n     return true; n   } else n     return false; n } n template <class X, class Y> n bool maximize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x + eps < y) { n     x = y; n     return true; n   } else n     return false; n } n template <class T> n T Abs(const T &x) { n   return (x < 0 ? -x : x); n } n const int MOD = (int)1e9 + 7; n int numRow, numCol; n char board[2020][2020]; n int range[2020][2020][2], limit[2020][2020][2]; n int numWay[2020][2020][2], sumRow[2020][2020][2], sumCol[2020][2020][2]; n void init(void) { n   scanf( %d%d , &numRow, &numCol); n   for (int i = (1), _b = (numRow); i <= _b; i++) scanf( %s , board[i] + 1); n   if (board[1][1] ==  R  || board[numRow][numCol] ==  R ) { n     cout << 0 << endl; n     exit(EXIT_SUCCESS); n   } n   if (numRow == 1 && numCol == 1) { n     cout << 1 << endl; n     exit(EXIT_SUCCESS); n   } n } n void add(int &x, const int &y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n int sum(int x, int y) { return x + y >= MOD ? x + y - MOD : x + y; } n int diff(int x, int y) { return x < y ? x - y + MOD : x - y; } n void optimize(void) { n   for (int i = (1), _b = (numRow); i <= _b; i++) { n     int cntRock = 0; n     for (int j = (numCol), _a = (1); j >= _a; j--) { n       range[i][j][1] = numCol - cntRock; n       if (board[i][j] ==  R ) cntRock++; n     } n     int k = numCol; n     for (int j = (numCol), _a = (1); j >= _a; j--) { n       while (k >= 1 && range[i][k][1] >= j) k--; n       k++; n       limit[i][j][1] = k; n     } n   } n   for (int j = (1), _b = (numCol); j <= _b; j++) { n     int cntRock = 0; n     for (int i = (numRow), _a = (1); i >= _a; i--) { n       range[i][j][0] = numRow - cntRock; n       if (board[i][j] ==  R ) cntRock++; n     } n     int k = numRow; n     for (int i = (numRow), _a = (1); i >= _a; i--) { n       while (k >= 1 && range[k][j][0] >= i) k--; n       k++; n       limit[i][j][0] = k; n     } n   } n   for (int i = (2), _b = (numRow); i <= _b; i++) { n     if (i <= range[1][1][0]) numWay[i][1][0] = 1; n     sumCol[i][1][0] = sum(sumCol[i - 1][1][0], numWay[i][1][0]); n     sumRow[i][1][0] = numWay[i][1][0]; n   } n   for (int j = (2), _b = (numCol); j <= _b; j++) { n     if (j <= range[1][1][1]) numWay[1][j][1] = 1; n     sumRow[1][j][1] = sum(sumRow[1][j - 1][1], numWay[1][j][1]); n     sumCol[1][j][1] = numWay[1][j][1]; n   } n   for (int i = (2), _b = (numRow); i <= _b; i++) n     for (int j = (2), _b = (numCol); j <= _b; j++) { n       int k = limit[i][j][1]; n       numWay[i][j][1] = diff(sumRow[i][j - 1][0], sumRow[i][k - 1][0]); n       k = limit[i][j][0]; n       numWay[i][j][0] = diff(sumCol[i - 1][j][1], sumCol[k - 1][j][1]); n       for (int k = 0, _n = (2); k < _n; k++) { n         sumRow[i][j][k] = sum(sumRow[i][j - 1][k], numWay[i][j][k]); n         sumCol[i][j][k] = sum(sumCol[i - 1][j][k], numWay[i][j][k]); n       } n     } n   printf( %d n , sum(numWay[numRow][numCol][1], numWay[numRow][numCol][0])); n } n int main(void) { n   init(); n   optimize(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   cout << (a - 1) * (b + c - 1) + b * c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ll t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     int ar[n]; n     int su = 0; n     for (int i = 0; i < n; i++) { n       cin >> ar[i]; n       su += ar[i]; n     } n     int ans = min(su, m); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long long int INF = 1e12; n int n, k; n int memo[101][101][101]; n int solve(string &str, int i, int N, int _max) { n   _max = max(N, _max); n   int &a = memo[i][N][_max]; n   if (a != -1) return a; n   if (i == n) { n     if (_max == k) return a = 1; n     return a = 0; n   } n   if (str[i] ==  Y ) n     return a = solve(str, i + 1, 0, _max); n   else if (str[i] ==  N ) n     return a = solve(str, i + 1, N + 1, _max); n   else { n     if (solve(str, i + 1, 0, _max)) return a = 1; n     return a = solve(str, i + 1, N + 1, _max); n   } n   return a = 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   memset(memo, -1, sizeof(memo)); n   cin >> n >> k; n   string str; n   cin >> str; n   if (solve(str, 0, 0, 0)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int type, pos, len, start[100020], dest[100020], a[100020], b[100020], q, n, n     seg[4 * 100020]; n bool mark[4 * 100020]; n void setVal(int v, int vl, int vr, int val, int l, int r) { n   if (l <= vl && vr <= r) { n     seg[v] = max(seg[v], val); n     mark[v] = true; n     return; n   } n   if (r < vl || l > vr || vl == vr) return; n   if (mark[v]) { n     mark[v] = false; n     mark[2 * v] = mark[2 * v + 1] = true; n     seg[2 * v] = max(seg[2 * v], seg[v]); n     seg[2 * v + 1] = max(seg[2 * v + 1], seg[v]); n   } n   int mid = (vl + vr) / 2; n   setVal(2 * v, vl, mid, val, l, r); n   setVal(2 * v + 1, mid + 1, vr, val, l, r); n   seg[v] = max(seg[2 * v], seg[2 * v + 1]); n } n int getVal(int v, int vl, int vr, int p) { n   int mid = (vl + vr) / 2; n   if (vl == vr) return seg[v]; n   if (mark[v]) { n     mark[v] = false; n     mark[2 * v] = mark[2 * v + 1] = true; n     seg[2 * v] = max(seg[2 * v], seg[v]); n     seg[2 * v + 1] = max(seg[2 * v + 1], seg[v]); n   } n   if (p <= mid) n     return getVal(2 * v, vl, mid, p); n   else n     return getVal(2 * v + 1, mid + 1, vr, p); n } n int main() { n   scanf( %d %d , &n, &q); n   for (__typeof(n) i = (1); i <= (n); i++) scanf( %d , &b[i]); n   for (__typeof(n) i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (__typeof(q) i = (1); i <= (q); i++) { n     scanf( %d , &type); n     if (type == 2) { n       scanf( %d , &pos); n       int idx = getVal(1, 1, n, pos); n       if (idx == 0) n         printf( %d n , a[pos]); n       else n         printf( %d n , b[start[idx] + pos - dest[idx]]); n     } else { n       scanf( %d %d %d , &start[i], &dest[i], &len); n       setVal(1, 1, n, i, dest[i], dest[i] + len - 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int y, b, r, ans = 0; n   cin >> y >> b >> r; n   if (y + 1 <= b && y + 2 <= r) { n     ans = max(ans, 3 * y + 3); n   } n   if (b + 1 <= r && b - 1 <= y) { n     ans = max(ans, 3 * b); n   } n   if (r - 1 <= b && r - 2 <= y) { n     ans = max(ans, 3 * r - 3); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void umin(T &a, T b) { n   a = min(a, b); n } n template <class T> n inline void umax(T &a, T b) { n   a = max(a, b); n } n const int INF = 0x3f3f3f3f; n long long mod = 1e9 + 7; n const int N = 100005; n char s[N]; n struct Trie { n   static const int maxnode = 100005; n   static const int sigma_size = 26; n   int ch[maxnode][sigma_size]; n   int val[maxnode]; n   int p[maxnode]; n   int d[maxnode], ms[maxnode]; n   priority_queue<int> q[maxnode]; n   int sz; n   void clear() { n     sz = 1; n     memset(ch[0], 0, sizeof(ch[0])); n   } n   int idx(char c) { return c -  a ; } n   void insert(const char *s, int v) { n     int u = 0, n = strlen(s); n     for (int i = 0; i < n; i++) { n       int c = idx(s[i]); n       if (!ch[u][c]) { n         memset(ch[sz], 0, sizeof(ch[sz])); n         val[sz] = 0; n         ch[u][c] = sz++; n         d[ch[u][c]] = d[u] + 1; n       } n       p[ch[u][c]] = u; n       u = ch[u][c]; n     } n     val[u] = v; n   } n   void dfs(int u) { n     for (int i = 0; i < 26; i++) { n       int v = ch[u][i]; n       if (v) { n         dfs(v); n         while (!q[v].empty()) { n           q[u].push(q[v].top()); n           q[v].pop(); n         } n       } n     } n     if (u == 0) return; n     if (val[u] == 0) { n       q[u].pop(); n       q[u].push(d[u]); n     } else { n       q[u].push(d[u]); n     } n   } n   int solve() { n     dfs(0); n     int ans = 0; n     while (!q[0].empty()) { n       ans += q[0].top(); n       q[0].pop(); n     } n     return ans; n   } n } wc; n int main() { n   int n; n   scanf( %d , &n); n   wc.clear(); n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     wc.insert(s, 1); n   } n   printf( %d n , wc.solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int idata = 2e5 + 7; n int i, j, k; n int judge, flag; n long long step[idata]; n long long temp; n int n, m, t; n double maxx = 0, minn = inf; n long long cnt, len, sum, ans; n long long high, wigh; n map<long long, int> pre[2]; n signed main() { n   while (cin >> n >> k) { n     memset(step, 0, sizeof step); n     for (i = 1; i <= n; i++) { n       cin >> step[i]; n       pre[0][step[i]]++; n     } n     for (i = 1; i <= n; i++) { n       pre[0][step[i]]--; n       if (step[i] % k == 0) { n         ans += (long long)pre[1][step[i] / k] * pre[0][step[i] * k]; n       } n       pre[1][step[i]]++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n int n; n int a[MAXN]; n int x[MAXN]; n int y[MAXN]; n int match[MAXN]; n bool vis[MAXN]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   if (~n & 1) { n     cout <<  First  << endl; n     for (int i = 1; i <= n; i++) cout << i <<    ; n     for (int i = 1; i <= n; i++) cout << i <<    ; n     cout << endl; n     cout.flush(); n     return 0; n   } else { n     cout <<  Second  << endl; n     for (int i = 1; i <= 2 * n; i++) { n       cin >> a[i]; n       if (x[a[i]]) n         y[a[i]] = i; n       else n         x[a[i]] = i; n     } n     for (int i = 1; i <= n; i++) { n       match[x[i]] = y[i]; n       match[y[i]] = x[i]; n     } n     for (int i = 1; i <= n; i++) { n       int cur = i; n       if (vis[i] || vis[i + n]) continue; n       while (!vis[cur]) { n         vis[cur] = 1; n         cur = (match[cur] > n ? match[cur] - n : match[cur] + n); n       } n     } n     int sum = 0; n     for (int i = 1; i <= 2 * n; i++) n       if (vis[i]) (sum += i) %= (2 * n); n     if (sum) { n       for (int i = 1; i <= 2 * n; i++) vis[i] ^= 1; n     } n     for (int i = 1; i <= 2 * n; i++) n       if (vis[i]) cout << i <<    ; n     cout << endl; n     cout.flush(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, x, y, i, j, k, l; n   cin >> n >> m >> x >> y; n   cout << x <<     << y <<   n ; n   for (i = 1; i <= m; i++) n     if (i != y) cout << x <<     << i <<   n ; n   int c; n   for (i = 1, j = m; i <= n; i++) n     if (i != x) n       for (k = 1; k <= m; k++) { n         cout << i <<     << j <<   n ; n         if (j == m) n           c = -1; n         else if (j == 1) n           c = 1; n         if (k != m) j += c; n       } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n const int MAX = 150000; n int t[MAX]; n int main() { n   int n, q, k; n   cin >> n >> k >> q; n   for (int i = 0; i < n; i++) scanf( %d , t + i); n   for (int i = 0; i < q; i++) { n     int query; n     scanf( %d , &query); n     int x; n     scanf( %d , &x); n     x = t[x - 1]; n     if (query == 1) { n       if (s.size() == k) { n         set<int>::iterator it = s.begin(); n         if (x > *it) { n           s.erase(*it); n           s.insert(x); n         } n       } else { n         s.insert(x); n       } n     } else { n       if (s.count(x)) n         printf( YES n ); n       else n         printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> v[110]; n int cur[110]; n int main() { n   int n, w; n   scanf( %d %d , &n, &w); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &v[i].first); n     v[i].second = i; n   } n   sort(v, v + n); n   reverse(v, v + n); n   for (int i = 0; i < n; ++i) { n     w -= (v[i].first + 1) / 2; n     if (w < 0) return puts( -1 ), 0; n     cur[i] = (v[i].first + 1) / 2; n   } n   for (int i = 0; i < n - 1; ++i) { n     if (cur[i] < cur[i + 1]) { n       int dif = cur[i + 1] - cur[i]; n       if (cur[i] == cur[i + 1]) dif++; n       if (w >= dif) { n         cur[i] += dif; n         w -= dif; n       } else if (cur[i + 1] >= dif) { n         cur[i + 1] -= dif; n         cur[i] += dif; n       } else n         return puts( -1 ); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (w) { n       int dif = v[i].first - cur[i]; n       if (w >= dif) { n         cur[i] += dif; n         w -= dif; n       } else if (dif >= w) { n         cur[i] += w; n         w = 0; n       } n     } else n       break; n   } n   for (int i = 0; i < n; ++i) v[v[i].second].first = cur[i]; n   for (int i = 0; i < n; ++i) printf( %d  , v[i].first); n   puts(  ); n   return 0; n } n


//#pragma GCC optimize( Ofast ) n  n #include <iostream> n #include <vector> n #include <map> n #include <algorithm> n #include <random> n #include <chrono> n #include <complex> n #include <cstdio> n #include <deque> n #include <cmath> n #include <bitset> n #include <set> n #include <cassert> n  n using namespace std; n  n #define ll long long n #define ull unsigned long long n #define pb push_back n #define se second n #define fi first n #define forn(i,n) for(int i = 0; i < (n); i++) n #define read(a) for(int i = 0; i < a.size(); i++){cin >> a[i];} n #define all(a) a.begin(),a.end() n #define make_unique(a) sort(all(a)); a.resize(unique(a.begin(),a.end())-a.begin()); n const int mod = 1e9 + 7; n //#define int long long n  n const int N = 10000000; n int lp[N+1]; n vector<int> pr; n  n void solve(){ n     int t; n     cin >> t; n  n     for (int i=2; i<=N; ++i) { n         if (lp[i] == 0) { n             lp[i] = i; n             pr.push_back (i); n         } n         for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) n             lp[i * pr[j]] = pr[j]; n     } n  n     set<vector<int>> se; n  n     while(t--){ n         int n,k; n         cin >> n >> k; n         vector<int> a(n); n         read(a); n         ll ans = 1; n         se.clear(); n         for(int i = 0; i < n; ++i){ n             vector<int> qw; n             int nw = a[i]; n             while(nw != 1){ n                 if(!qw.empty() && qw.back() == lp[nw]){ n                     qw.pop_back(); n                     nw /= lp[nw]; n                     continue; n                 } n                 if(qw.empty() || qw.back() != lp[nw]){ n                     qw.push_back(lp[nw]); n                 } n                 nw /= lp[nw]; n                  n             } n             if(se.count(qw)){ n                 ans++; n                 se.clear(); n             } n             se.insert(qw); n         } n         cout << ans <<   n ; n     } n  n } n  n  n  n signed main() { n     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n #ifdef Debugg n     freopen( ain.txt , r ,stdin); n     freopen( aout.txt , w ,stdout); n #endif n     solve(); n #ifdef Debugg n     cout <<   n  << (double)clock()/CLOCKS_PER_SEC; n #endif n }


#include <bits/stdc++.h> n using namespace std; n inline void pisz(int n) { printf( %d n , n); } n int dl[1000][1000]; n int dr[1000][1000]; n int ul[1000][1000]; n int ur[1000][1000]; n int g[1000][1000]; n int n, m; n int recdl(int i, int j) { n   if (i < 0 || i == n || j < 0 || j == m) return -1001001001; n   if (i == n - 1 && j == 0) return g[i][j]; n   if (dl[i][j] != -1) return dl[i][j]; n   return dl[i][j] = n              g[i][j] + (recdl(i + 1, j) < recdl(i, j - 1) ? recdl(i, j - 1) n                                                           : recdl(i + 1, j)); n } n int recdr(int i, int j) { n   if (i < 0 || i == n || j < 0 || j == m) return -1001001001; n   if (i == n - 1 && j == m - 1) return g[i][j]; n   if (dr[i][j] != -1) return dr[i][j]; n   return dr[i][j] = n              g[i][j] + (recdr(i + 1, j) < recdr(i, j + 1) ? recdr(i, j + 1) n                                                           : recdr(i + 1, j)); n } n int recul(int i, int j) { n   if (i < 0 || i == n || j < 0 || j == m) return -1001001001; n   if (i == 0 && j == 0) return g[i][j]; n   if (ul[i][j] != -1) return ul[i][j]; n   return ul[i][j] = n              g[i][j] + (recul(i - 1, j) < recul(i, j - 1) ? recul(i, j - 1) n                                                           : recul(i - 1, j)); n } n int recur(int i, int j) { n   if (i < 0 || i == n || j < 0 || j == m) return -1001001001; n   if (i == 0 && j == m - 1) return g[i][j]; n   if (ur[i][j] != -1) return ur[i][j]; n   return ur[i][j] = n              g[i][j] + (recur(i - 1, j) < recur(i, j + 1) ? recur(i, j + 1) n                                                           : recur(i - 1, j)); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> g[i][j]; n   memset(dl, -1, sizeof(dl)); n   memset(dr, -1, sizeof(dr)); n   memset(ul, -1, sizeof(ul)); n   memset(ur, -1, sizeof(ur)); n   int res = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int b1 = recul(i - 1, j) + recdl(i, j - 1); n       int a1 = recdr(i + 1, j) + recur(i, j + 1); n       int b2 = recul(i, j - 1) + recdl(i + 1, j); n       int a2 = recdr(i, j + 1) + recur(i - 1, j); n       res = (res < (b1 + a1 < b2 + a2 ? b2 + a2 : b1 + a1) n                  ? (b1 + a1 < b2 + a2 ? b2 + a2 : b1 + a1) n                  : res); n     } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 0.0000000001; n const int N = 100000 + 100; n int head[N]; n int tot; n struct node { n   int to, next; n } edge[N << 1]; n int color[N]; n int vis[N]; n int a[N]; n int b[N]; n int num[N]; n void init() { n   memset(head, -1, sizeof(head)); n   tot = 0; n } n void add(int u, int v) { n   edge[tot].to = v; n   edge[tot].next = head[u]; n   head[u] = tot++; n } n int DFS(int u, int t) { n   if (vis[u] == 0) { n     if (color[u] == 0) a[t]++; n     if (color[u] == 1) b[t]++; n     num[t]++; n   } n   vis[u] = 1; n   for (int i = head[u]; i != -1; i = edge[i].next) { n     int v = edge[i].to; n     if (color[v] == 0) { n       color[v] = color[u] ^ 1; n       if (DFS(v, t) == 0) return 0; n     } else if (color[u] == color[v]) { n       return 0; n     } n   } n   return 1; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   init(); n   int u, v; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n   } n   if (m == 0) { n     cout << 3 <<     << (long long)n * (n - 1) * (n - 2) / 6 << endl; n     return 0; n   } n   memset(color, 0, sizeof(color)); n   memset(vis, 0, sizeof(vis)); n   int flag = 0; n   int t = 0; n   for (int i = 1; i <= n; i++) { n     if (color[i] == 0 && vis[i] == 0) { n       if (DFS(i, ++t) == 0) { n         color[i] = 0; n         flag = 1; n         break; n       } n     } n   } n   if (flag == 1) { n     cout << 0 <<     << 1 << endl; n     return 0; n   } n   long long ans = 0; n   flag = 0; n   for (int i = 1; i <= t; i++) { n     if (num[i] <= 2) { n       flag++; n       continue; n     } n     ans = ans + (long long)a[i] * (a[i] - 1) / 2 + n           (long long)b[i] * (b[i] - 1) / 2; n   } n   if (flag != t) n     cout << 1 <<     << ans << endl; n   else { n     cout << 2 <<     << (long long)m * (n - 2) << endl; n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, pur, foo = 0, quz = 1, bar = 0, baz = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &pur); n     foo += pur, bar++; n     if (foo * quz > bar * baz) quz = bar, baz = foo; n   } n   printf( %.6lf n , (double)baz / quz); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fun() {} n long long int md = 998244353; n long long int __gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return __gcd(b, a % b); n } n long long int poww(long long int a, long long int b, long long int md) { n   if (b < 0) return 0; n   if (a == 0) return 0; n   long long int res = 1; n   while (b) { n     if (b & 1) { n       res = (1ll * res * a) % md; n     } n     a = (1ll * a * a) % md; n     b >>= 1; n   } n   return res; n } n long long int poww(long long int a, long long int b) { n   if (b < 0) return 0; n   if (a == 0) return 0; n   long long int res = 1; n   while (b) { n     if (b & 1) { n       res = (1ll * res * a); n     } n     a = (1ll * a * a); n     b >>= 1; n   } n   return res; n } n void ainp(long long int arr[], long long int n) { n   for (long long int i = 1; i <= n; i++) cin >> arr[i]; n } n void disp(long long int arr[], long long int n) { n   for (long long int i = 1; i <= n; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n void dispv(vector<long long int> &v) { n   for (long long int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   cout <<   n ; n } n long long int divide(long long int a, long long int b, long long int md) { n   long long int rr = a * (poww(b, md - 2, md)); n   rr %= md; n   return rr; n } n const long long int size = 2e5 + 5; n long long int n, m, k, u, v; n vector<long long int> g[size]; n pair<long long int, long long int> dis[size], prr[size]; n queue<long long int> q; n long long int arr[size], suffmax[size]; n bool fnn(pair<long long int, long long int> &p1, n          pair<long long int, long long int> &p2) { n   return (p1.first - p1.second < p2.first - p2.second); n } n void bfs() { n   dis[1].first = 0; n   q.push(1); n   while (!q.empty()) { n     long long int node = q.front(); n     q.pop(); n     for (long long int child : g[node]) { n       if (dis[child].first > dis[node].first + 1 || n           (child != 1 && dis[child].first == 0)) { n         dis[child].first = dis[node].first + 1; n         q.push(child); n       } n     } n   } n   q.push(n); n   dis[n].second = 0; n   while (!q.empty()) { n     long long int node = q.front(); n     q.pop(); n     for (long long int child : g[node]) { n       if (dis[child].second > dis[node].second + 1 || n           (child != n && dis[child].second == 0)) { n         dis[child].second = dis[node].second + 1; n         q.push(child); n       } n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   fun(); n   cin >> n >> m >> k; n   ainp(arr, k); n   for (long long int i = 1; i <= m; i++) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   bfs(); n   for (long long int i = 1; i <= k; i++) prr[i] = dis[arr[i]]; n   sort(prr + 1, prr + 1 + k, fnn); n   for (long long int i = k; i > 0; i--) n     suffmax[i] = max(suffmax[i + 1], prr[i].second); n   long long int cost = dis[n].first; n   long long int cst = 0; n   for (long long int i = 1; i < k; i++) { n     cst = max(suffmax[i + 1] + 1 + prr[i].first, cst); n   } n   cout << min(cst, cost) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, T; n int A[20000]; n int rans; n vector<int> ans; n set<pair<int, int>> online; n multiset<pair<int, int>> events; n bool solve() { n   bool good = false; n   for (int i = 0; i < N; i++) events.insert({A[i], -1}); n   while (!events.empty()) { n     int t = events.begin()->first; n     int v = events.begin()->second; n     events.erase(events.begin()); n     if (v == -1) { n       if ((int)online.size() < M) { n         rans++; n         online.insert({t + T - 1, rans}); n         events.insert({t + T - 1, rans}); n         ans.push_back(rans); n       } else { n         pair<int, int> guy = *online.rbegin(); n         online.erase(guy); n         events.erase(events.find(guy)); n         guy.first = t + T - 1; n         online.insert(guy); n         events.insert(guy); n         ans.push_back(guy.second); n       } n       good |= (int)online.size() == M; n     } else n       online.erase({t, v}); n   } n   return good; n } n int main() { n   scanf( %d%d%d , &N, &M, &T); n   int hh, mm, ss; n   for (int i = 0; i < N; i++) { n     scanf( %d:%d:%d , &hh, &mm, &ss); n     A[i] = hh * 3600 + mm * 60 + ss; n   } n   if (!solve()) n     printf( No solution n ); n   else { n     printf( %d n , rans); n     for (auto& it : ans) printf( %d n , it); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init_code() {} n int main() { n   init_code(); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long num = 1, res = 0; n     while (k) { n       if (k & 1) { n         res += num; n         res %= 1000000000 + 7; n       } n       k >>= 1; n       num *= n; n       num %= 1000000000 + 7; n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   for (; b; a %= b, swap(a, b)) n     ; n   return a; n } n int dx[4] = {1, -1, 0, 0}; n int dy[4] = {0, 0, 1, -1}; n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     int ones = 0; n     vector<int> one(k); n     string st; n     cin >> st; n     for (int i = 0; i < n; i++) { n       if (st[i] ==  1 ) { n         one[i % k]++; n         ones++; n       } n     } n     int ans = ones; n     for (int i = 0; i < k; i++) { n       int tmp = ones - one[i % k]; n       string t =   ; n       vector<int> cnt; n       cnt.push_back(0); n       for (int j = i; j < n; j += k) { n         t += st[j]; n         if (cnt.size() == 0) n           cnt.push_back(st[j] -  0 ); n         else n           cnt.push_back(cnt.back() + st[j] -  0 ); n       } n       int len = t.size(); n       vector<int> dp(len + 1); n       int res = 1e9; n       for (int j = 1; j <= len; j++) { n         int p = (t[j - 1] ==  0  ? 1 : 0); n         dp[j] = min(dp[j - 1] + p, cnt[j - 1] + p); n       } n       for (int j = 1; j <= len; j++) res = min(res, dp[j] + cnt[len] - cnt[j]); n       ans = min(ans, res + tmp); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[15], y, k2, k3, r1, z, k4, r, max1, k5, k1, min1, b[1005], n1, n, m, n     k, i, j; n vector<char> f; n int main() { n   string s, s1; n   cin >> k >> n; n   cin >> s; n   for (i = 0; i < n; i++) { n     for (j = 0; j < 10; j++) a[j] = 0; n     cin >> k1 >> k2; n     s1.clear(); n     for (j = k1 - 1; j <= k2 - 1; j++) s1.push_back(s[j]); n     j = 0; n     k3 = 1; n     z = 0; n     while (j >= 0 && j < s1.size()) { n       if ((int)s1[j] >= (int) 0  && (int)s1[j] <= (int) 9 ) { n         a[(int)s1[j] - (int) 0 ]++; n         if (s1[j] ==  0 ) n           s1[j] =  - ; n         else n           s1[j] = (char)((int)s1[j] - 1); n         j += k3; n         z = 0; n       } else if (s1[j] ==  > ) { n         if (z) s1[k4] =  - ; n         k4 = j; n         z = 1; n         k3 = 1; n         j += k3; n       } else if (s1[j] ==  < ) { n         if (z) s1[k4] =  - ; n         k4 = j; n         z = 1; n         k3 = -1; n         j += k3; n       } else n         j += k3; n     } n     for (j = 0; j < 10; j++) cout << a[j] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 501; n int a[N][N], b[N][N]; n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> b[i][j]; n     } n   } n   for (int i = 0; i < m; i++) { n     vector<int> comp1, comp2; n     int x = 0, y = i; n     while (x < n && y >= 0) { n       comp1.push_back(a[x][y]); n       comp2.push_back(b[x][y]); n       x++, y--; n     } n     sort(comp1.begin(), comp1.end()); n     sort(comp2.begin(), comp2.end()); n     if (comp1 != comp2) { n       cout <<  NO n ; n       return; n     } n   } n   for (int i = 1; i < n; i++) { n     vector<int> comp1, comp2; n     int x = i, y = m - 1; n     while (x < n && y >= 0) { n       comp1.push_back(a[x][y]); n       comp2.push_back(b[x][y]); n       x++, y--; n     } n     sort(comp1.begin(), comp1.end()); n     sort(comp2.begin(), comp2.end()); n     if (comp1 != comp2) { n       cout <<  NO n ; n       return; n     } n   } n   cout <<  YES n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   for (int tt = 1; tt <= t; tt++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int mirror(int num) { n   int res = 0; n   for (int i = 0; i < 4; ++i) { n     res |= (num & (1 << i)) << (7 - i - i); n     res |= (num & (1 << (7 - i))) >> (7 - i - i); n   } n   return res; n } n int main() { n   int num; n   string s; n   vector<int> res; n   getline(cin, s); n   num = (0 + 256 - mirror((int)s[0])) % 256; n   res.push_back(num); n   for (int i = 1; i < s.size(); ++i) { n     num = (-mirror(s[i]) + 256 + mirror(s[i - 1])) % 256; n     res.push_back(num); n   } n   for (int i = 0; i < res.size(); ++i) { n     cout << res[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n struct edge { n   int to, nxt, w; n } e[N << 1]; n int head[N], tot = 0; n int n, m; n int vis[N], mxd, id; n int sum[N], dr[N], cnt = 0, pa[N], used[N]; n int dp[N], a[N], b[N], siz[N], anc[N], mp[N]; n void adde(int u, int v, int w) { n   e[++tot] = edge{v, head[u], w}, head[u] = tot; n } n void dfs(int u, int fa, int dep) { n   pa[u] = fa; n   if (vis[u] && dep > mxd) id = u, mxd = dep; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, u, dep + e[i].w); n   } n   return; n } n void gtag(int u, int fa, int *a, int dep) { n   a[u] = dep; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     gtag(v, u, a, dep + e[i].w); n   } n   return; n } n void Dfs(int u, int fa, int rt) { n   siz[u] = vis[u]; n   anc[u] = rt; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     if (used[v]) continue; n     Dfs(v, u, rt); n     siz[u] += siz[v]; n     dp[u] = max(dp[u], dp[v] + e[i].w); n   } n   return; n } n int main() { n   scanf( %d%d , &n, &m); n   int x; n   for (int i = 1; i <= m; i++) scanf( %d , &x), vis[x] = 1; n   int u, v, w; n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &u, &v, &w); n     adde(u, v, w), adde(v, u, w); n   } n   id = mxd = -1; n   dfs(1, 0, 0); n   int s = id; n   id = mxd = -1; n   dfs(s, 0, 0); n   int t = id; n   swap(s, t); n   for (int now = s; now; now = pa[now]) n     dr[++cnt] = now, used[now] = 1, mp[now] = cnt; n   dr[cnt + 1] = n + 1; n   mp[n + 1] = cnt + 1; n   gtag(s, 0, a, 0); n   gtag(t, 0, b, 0); n   for (int i = 1; i <= cnt; i++) Dfs(dr[i], 0, dr[i]); n   int ans = 0; n   for (int i = 1; i <= n; i++) n     if (!used[i] && !vis[i]) ans = max(ans, siz[i]); n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) continue; n     int u = anc[i]; n     int d = max(a[u], b[u]); n     int k = mp[u]; n     if (dp[u] == d || (a[u] == d && b[u] == d)) n       sum[k]++, sum[k + 1]--; n     else { n       if (a[u] == d) n         sum[0]++, sum[k + 1]--; n       else n         sum[k]++; n     } n   } n   for (int i = 1; i <= cnt; i++) sum[i] += sum[i - 1]; n   for (int i = 1; i <= cnt; i++) n     if (!vis[dr[i]]) ans = max(ans, sum[i]); n   int amou = 0; n   for (int i = 1; i <= n; i++) n     if (!used[i] && !vis[i] && siz[i] == ans) amou++; n   for (int i = 1; i <= cnt; i++) n     if (!vis[dr[i]] && sum[i] == ans) amou++; n   printf( %d %d n , ans, amou); n   return 0; n } n