#include <bits/stdc++.h> n using namespace std; n const int N = 3050; n int n, p, s; n int sump[N], sum[N]; n struct Student { n   int a, b, id; n   void input() { scanf( %d%d , &a, &b); } n   bool operator<(const Student &student) const { n     return b - a < student.b - student.a; n   } n } data[N]; n void del() { n   sort(data + 1, data + n + 1); n   sum[0] = 0; n   sump[0] = 0; n   for (int i = 1; i <= n; i++) { n     sump[i] = sump[i - 1] + data[i].a; n     sum[i] = sum[i - 1] + data[i].b - data[i].a; n   } n   int id = -1, ans = 0; n   for (int i = 1; i <= n; i++) { n     int cur; n     if (i <= p) n       cur = sump[n] + sum[n] - sum[p + 1] - data[i].a; n     else n       cur = sump[n] + sum[n] - sum[p] - data[i].b; n     if (cur > ans) { n       ans = cur; n       id = i; n     } n   } n   swap(data[id], data[n]); n   n--; n } n void solve() { n   for (int i = 1; i <= n; i++) scanf( %d , &data[i].a); n   for (int i = 1; i <= n; i++) scanf( %d , &data[i].b); n   for (int i = 1; i <= n; i++) data[i].id = i; n   int k = n - p - s; n   for (int i = 0; i < k; i++) del(); n   sort(data + 1, data + n + 1); n   int ans = 0; n   for (int i = 1; i <= p; i++) ans += data[i].a; n   for (int i = p + 1; i <= n; i++) ans += data[i].b; n   printf( %d n , ans); n   for (int i = 1; i <= p; i++) printf( %d  , data[i].id); n   puts(  ); n   for (int i = p + 1; i <= n; i++) printf( %d  , data[i].id); n   puts(  ); n } n int main() { n   while (scanf( %d%d%d , &n, &p, &s) != EOF) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int MOD = 1e6 + 3; n const int N = 1e2 + 5; n int n, wid, hei; n long long f[N], phei[N]; n struct matrix { n   long long a[N][N]; n   matrix() { memset(a, 0, sizeof(a)); } n } base, cur; n matrix operator*(matrix a, matrix b) { n   matrix ans; n   for (auto i = (0); i <= (wid); i++) n     for (auto j = (0); j <= (wid); j++) n       for (auto k = (0); k <= (wid); k++) n         ans.a[i][j] = (ans.a[i][j] + a.a[i][k] * b.a[k][j]) % MOD; n   return ans; n } n void prepare() { n   cin >> n >> wid >> hei; n   phei[0] = 1; n   for (auto i = (1); i <= (wid); i++) phei[i] = phei[i - 1] * hei % MOD; n   f[0] = 1; n   for (auto i = (1); i <= (wid); i++) n     for (int j = 1; j <= min(i, wid); j++) n       f[i] = (f[i] + f[i - j] * phei[j - 1]) % MOD; n   for (auto i = (1); i <= (wid); i++) base.a[i][i - 1] = 1; n   for (auto i = (0); i <= (wid); i++) base.a[i][wid] = phei[wid - i]; n } n long long solve() { n   n++; n   if (n <= wid) return f[n]; n   n -= wid; n   for (auto i = (0); i <= (wid); i++) cur.a[i][i] = 1; n   while (n) { n     if (n & 1) cur = cur * base; n     base = base * base; n     n >>= 1; n   } n   long long ans = 0; n   for (auto i = (0); i <= (wid); i++) ans = (ans + f[i] * cur.a[i][wid]) % MOD; n   return ans; n } n int main() { n   prepare(); n   cout << solve(); n } n


#include <bits/stdc++.h> n const int MN = 100005, MS = 1 << 18 | 7; n namespace HASH { n const int mod = 1e9 + 7; n const int bs = 29; n const int iv = 758620695; n int _w[MN << 1], *w = _w + MN; n void init(int N) { n   w[0] = 1; n   for (int i = 1; i < N; ++i) { n     w[+i] = 1ll * w[i - 1] * bs % mod; n     w[-i] = 1ll * w[1 - i] * iv % mod; n   } n } n struct str { n   int x, m; n   str(int _v = 0, int _len = 0) : x(_v), m(_len) {} n   inline bool operator==(const str& rhs) const { n     return x == rhs.x && m == rhs.m; n   } n   inline str operator+(const str& rhs) const { n     return str((x + 1ll * rhs.x * w[m]) % mod, m + rhs.m); n   } n   inline str operator-(const str& rhs) const { n     return str(1ll * (x - rhs.x + mod) * w[-rhs.m] % mod, m - rhs.m); n   } n }; n }  // namespace HASH n using HASH::str; n int N, Q; n struct node { n   bool err; n   str vl, vr; n   node(int _v = 0) { n     err = 0; n     if (_v != 0) _v > 0 ? vr = str(_v, 1) : vl = str(-_v, 1); n   } n } tr[MN << 3]; n str gValL(int i, int k) { n   if (!k) return str(); n   if (k == tr[i].vl.m) return tr[i].vl; n   if (k <= tr[(i << 1)].vl.m) return gValL((i << 1), k); n   return tr[(i << 1)].vl + n          (gValL(((i << 1) | 1), k - tr[(i << 1)].vl.m + tr[(i << 1)].vr.m) - n           tr[(i << 1)].vr); n } n str gValR(int i, int k) { n   if (!k) return str(); n   if (k == tr[i].vr.m) return tr[i].vr; n   if (k <= tr[((i << 1) | 1)].vr.m) return gValR(((i << 1) | 1), k); n   return tr[((i << 1) | 1)].vr + (gValR((i << 1), k - tr[((i << 1) | 1)].vr.m + n                                                       tr[((i << 1) | 1)].vl.m) - n                                   tr[((i << 1) | 1)].vl); n } n void Upd(int i) { n   if (tr[(i << 1)].err || tr[((i << 1) | 1)].err) return tr[i].err = 1, void(); n   tr[i].err = 0; n   tr[i].vl = tr[(i << 1)].vl; n   tr[i].vr = tr[((i << 1) | 1)].vr; n   if (tr[(i << 1)].vr.m <= tr[((i << 1) | 1)].vl.m) { n     if (tr[(i << 1)].vr == gValL(((i << 1) | 1), tr[(i << 1)].vr.m)) n       tr[i].vl = tr[i].vl + (tr[((i << 1) | 1)].vl - tr[(i << 1)].vr); n     else n       tr[i].err = 1; n   } else { n     if (tr[((i << 1) | 1)].vl == gValR((i << 1), tr[((i << 1) | 1)].vl.m)) n       tr[i].vr = tr[i].vr + (tr[(i << 1)].vr - tr[((i << 1) | 1)].vl); n     else n       tr[i].err = 1; n   } n } n void Build(int i, int l, int r) { n   if (l == r) { n     int x; n     scanf( %d , &x); n     tr[i] = x; n     return; n   } n   Build((i << 1), l, ((l + r) >> 1)), n       Build(((i << 1) | 1), ((l + r) >> 1) + 1, r); n   Upd(i); n } n void Mdf(int i, int l, int r, int p, int x) { n   if (l == r) return tr[i] = x, void(); n   p <= ((l + r) >> 1) ? Mdf((i << 1), l, ((l + r) >> 1), p, x) n                       : Mdf(((i << 1) | 1), ((l + r) >> 1) + 1, r, p, x); n   Upd(i); n } n int stk[45], tp; n void Extract(int i, int l, int r, int a, int b) { n   if (r < a || b < l) return; n   if (a <= l && r <= b) return stk[++tp] = i, void(); n   Extract((i << 1), l, ((l + r) >> 1), a, b), n       Extract(((i << 1) | 1), ((l + r) >> 1) + 1, r, a, b); n } n str seq[45]; n str gVal(int i, int k) { n   if (!k) return str(); n   if (k == seq[i].m) return seq[i]; n   if (k <= tr[stk[i]].vr.m) return gValR(stk[i], k); n   return tr[stk[i]].vr + n          (gVal(i - 1, k - tr[stk[i]].vr.m + tr[stk[i]].vl.m) - tr[stk[i]].vl); n } n bool Qur(int l, int r) { n   tp = 0, Extract(1, 1, N, l, r); n   for (int i = 1; i <= tp; ++i) { n     if (tr[stk[i]].err) return 0; n     if (seq[i - 1].m < tr[stk[i]].vl.m) return 0; n     if (tr[stk[i]].vl == gVal(i - 1, tr[stk[i]].vl.m)) n       seq[i] = tr[stk[i]].vr + (seq[i - 1] - tr[stk[i]].vl); n     else n       return 0; n   } n   return !seq[tp].m; n } n int main() { n   scanf( %d%*d , &N); n   HASH::init(N); n   Build(1, 1, N); n   scanf( %d , &Q); n   while (Q--) { n     int t, x, y; n     scanf( %d%d%d , &t, &x, &y); n     if (t == 1) n       Mdf(1, 1, N, x, y); n     else n       puts(Qur(x, y) ?  Yes  :  No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int cnt = 0; n   int len = s.length(); n   for (int i = 0; i < len; i++) { n     if (s.at(i) ==  a ) cnt++; n   } n   int ltr = len - cnt; n   while (ltr >= cnt) { n     ltr--; n     len--; n   } n   cout << len; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int P = 1e9 + 7; n const int inf = 1e9; n const long long Inf = 1e15; n inline int IN() { n   char ch = getchar(); n   int x = 0, f = 0; n   while (ch <  0  || ch >  9 ) ch = getchar(), f = (ch ==  - ); n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch - 48; n     ch = getchar(); n   } n   return f ? (-x) : x; n } n inline int Pow(int x, int y, int p) { n   int an = 1; n   for (; y; y >>= 1, x = (long long)x * x % p) n     if (y & 1) an = (long long)an * x % p; n   return an; n } n void renew(int &x, int y) { n   x += y; n   if (x < 0) n     x += P; n   else if (x >= P) n     x -= P; n } n void setIO(string a) { n   freopen((a +  .in ).c_str(),  r , stdin); n   freopen((a +  .out ).c_str(),  w , stdout); n } n template <typename T> n inline void chkmin(T &a, const T &b) { n   if (a > b) a = b; n } n template <typename T> n inline void chkmax(T &a, const T &b) { n   if (a < b) a = b; n } n int n, k; n int dp[1000005], g[1000005], fac[1000005], inv[1000005]; n int main() { n   scanf( %d%d , &n, &k); n   fac[0] = 1; n   for (int i = (int)1; i <= (int)n; i++) fac[i] = (long long)fac[i - 1] * i % P; n   inv[0] = inv[1] = 1; n   for (int i = (int)2; i <= (int)n; i++) n     inv[i] = (long long)inv[P % i] * (P - P / i) % P; n   for (int i = (int)2; i <= (int)n; i++) n     inv[i] = (long long)inv[i - 1] * inv[i] % P; n   dp[1] = dp[0] = 1; n   for (int i = (int)2; i <= (int)k; i++) dp[i] = (long long)dp[i - 1] * i % P; n   for (int i = (int)1; i <= (int)k; i++) n     g[i] = (g[i - 1] + (long long)inv[i] * dp[i]) % P; n   for (int i = (int)k + 1; i <= (int)n; i++) { n     dp[i] = (g[i - 1] + P - g[i - k - 1]) % P; n     dp[i] = (long long)dp[i] * fac[i - 1] % P; n     g[i] = (g[i - 1] + (long long)inv[i] * dp[i]) % P; n   } n   int ans = 0; n   for (int i = (int)0; i <= (int)n - 1; i++) n     ans = (ans + (long long)dp[i] * fac[n - 1] % P * inv[i]) % P; n   ans = (0LL + fac[n] - ans) % P; n   ans = (ans + P) % P; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 200000; n int f[N + 5]; n namespace segt { n const int S = (N << 2); n struct type { n   int mn, lm, rm, m, s; n   friend type operator+(const type &a, const type &b) { n     type c; n     c.mn = std::min(a.mn, b.mn), c.m = std::max(a.m, b.m); n     if (a.mn < b.mn) { n       c.lm = a.lm, c.rm = std::max(a.rm, b.m), c.s = a.s; n     } else if (a.mn > b.mn) { n       c.lm = std::max(a.m, b.lm), c.rm = b.rm, c.s = b.s; n     } else { n       c.lm = a.lm, c.rm = b.rm, c.s = a.s + b.s + std::max(a.rm, b.lm); n     } n     return c; n   } n } t[S + 5]; n int le[S + 5], ri[S + 5], tag[S + 5]; n void addtag(int x, int k) { tag[x] += k, t[x].mn += k; } n void pushup(int x) { t[x] = t[(x << 1)] + t[(x << 1 | 1)]; } n void pushdown(int x) { n   if (tag[x]) n     addtag((x << 1), tag[x]), addtag((x << 1 | 1), tag[x]), tag[x] = 0; n } n void build(int x, int l, int r) { n   le[x] = l, ri[x] = r, t[x] = (type){0, 0, 0, 0, 0}; n   if (l == r) return; n   int m = (l + r) >> 1; n   build((x << 1), l, m), build((x << 1 | 1), m + 1, r); n } n void add(int x, int l, int r, int d) { n   if (r < le[x] || l > ri[x]) return; n   if (l <= le[x] && ri[x] <= r) { n     addtag(x, d); n     return; n   } n   pushdown(x), add((x << 1), l, r, d), add((x << 1 | 1), l, r, d), pushup(x); n } n void update(int x, int p) { n   if (le[x] == ri[x]) { n     t[x].lm = f[p], t[x].rm = f[p + 1], t[x].m = std::max(f[p], f[p + 1]); n     return; n   } n   int m = (le[x] + ri[x]) >> 1; n   pushdown(x), update(p <= m ? (x << 1) : (x << 1 | 1), p), pushup(x); n } n }  // namespace segt n std::set<int> st[N + 5]; n int a[N + 5], n, q; n void clear(int x) { n   if (st[x].empty()) return; n   int lc = *st[x].begin(); n   segt::add(1, lc, (*st[x].rbegin()) - 1, -1); n   f[lc] = 0, segt::update(1, lc - 1), segt::update(1, lc); n } n void calc(int x) { n   if (st[x].empty()) return; n   int lc = *st[x].begin(); n   segt::add(1, lc, (*st[x].rbegin()) - 1, 1); n   f[lc] = (int)st[x].size(), segt::update(1, lc - 1), segt::update(1, lc); n } n void erase(int i) { n   int x = a[i]; n   clear(x), st[x].erase(i), calc(x); n } n void insert(int i) { n   int x = a[i]; n   clear(x), st[x].insert(i), calc(x); n } n int main() { n   scanf( %d%d , &n, &q); n   segt::build(1, 0, n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), insert(i); n   printf( %d n , n - segt::t[1].s); n   for (int t = 1, i, x; t <= q; t++) { n     scanf( %d%d , &i, &x); n     erase(i), a[i] = x, insert(i); n     printf( %d n , n - segt::t[1].s); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n inline long long llread() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n int n; n char s[25][400010]; n int l[25], rest[25]; n vector<int> v[25][400010]; n int dp[(1 << 20) + 10][2], g[(1 << 20) + 10]; n signed main() { n   n = read(); n   for (int i = (1); i <= (n); i++) scanf( %s , s[i] + 1); n   for (int i = (1); i <= (n); i++) l[i] = strlen(s[i] + 1); n   for (int i = (1); i <= (n); i++) { n     int top = 0; n     for (int j = (1); j <= (l[i]); j++) { n       if (s[i][j] ==  ( ) n         top++; n       else n         top--; n       if (top <= 0) v[i][-top].push_back(j); n     } n     rest[i] = top; n   } n   for (int i = (1); i <= ((1 << n) - 1); i++) dp[i][0] = dp[i][1] = -0x3f3f3f3f; n   dp[0][0] = dp[0][1] = g[0] = 0; n   for (int mask = (1); mask <= ((1 << n) - 1); mask++) { n     for (int i = (1); i <= (n); i++) { n       if (mask & (1 << (i - 1))) { n         int S = mask ^ (1 << (i - 1)); n         if (dp[mask][0] < dp[S][0]) { n           dp[mask][0] = dp[S][0]; n         }; n         if (g[S] >= 0) { n           int x; n           if (v[i][g[S] + 1].size() == 0) n             x = l[i] + 1; n           else n             x = v[i][g[S] + 1][0]; n           int y = lower_bound(v[i][g[S]].begin(), v[i][g[S]].end(), x) - n                   v[i][g[S]].begin(); n           if (x == l[i] + 1) { n             if (dp[mask][1] < dp[S][1] + y) { n               dp[mask][1] = dp[S][1] + y; n             }; n           } n           if (dp[mask][0] < dp[S][1] + y) { n             dp[mask][0] = dp[S][1] + y; n           }; n         } n         g[mask] = g[S] + rest[i]; n       } n     } n   } n   printf( %d n , max(dp[(1 << n) - 1][0], dp[(1 << n) - 1][1])); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   int b[200]; n   int t[200]; n   int tmp; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) t[i] = 1; n   for (int i = 0; i < m; ++i) { n     scanf( %d , &tmp); n     for (int j = tmp; j <= n; ++j) { n       if (t[j] == 1) { n         b[j] = tmp; n         t[j] = 0; n       } n     } n   } n   for (int i = 1; i <= n; ++i) printf( %d  , b[i]); n } n


#include <bits/stdc++.h> n using namespace std; n void read (int &x) { n     char ch = getchar(); int f = 0; x = 0; n     while (!isdigit(ch)) { if (ch ==  - ) f = 1; ch = getchar(); } n     while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n     if (f) x = -x; n } const int N = 3e5 + 5; n int n, a[N], f[N], fr[N], d[N], k[N], lg[N]; n #define cmax(x, y) (x = x > y ? x : y) n struct ans { int v, w; } ; n struct st { n     int c[N][19]; n     void pre () { n         for (int i = 0; i <= n; ++i) c[i][0] = a[i] + i; n         for (int i = 1; (1 << i) <= n; ++i) n             for (int j = 1; j + (1 << i) - 1 <= n; ++j) n                 c[j][i] = max (c[j][i - 1], c[j + (1 << i - 1)][i - 1]); n     } n     int query (int l, int r) { n         if (l > r) return 0; int t = lg[r - l + 1]; n         // printf ( %d %d %d n , l, r, max (c[l][t], c[r - (1 << t) + 1][t])); n         return max (c[l][t], c[r - (1 << t) + 1][t]); n     } n } A; n signed main() { n     int T; read (T); lg[0] = -1; n     for (int i = 1; i <= 300002; ++i) lg[i] = lg[i >> 1] + 1; n     while (T--) { n         read (n); a[++n] = 0; n         for (int i = 1; i < n; ++i) read (a[i]); A.pre (); n         for (int i = 0; i <= n; ++i) f[i] = k[i] = fr[i] = 0; n         for (int i = 1, l, r, m, j, mx = -1, w = 0; i <= n; ++i) { n             l = 0, r = i - 1, j = -1; n             while (l <= r) { n                 m = l + r >> 1; n                 if (f[m] + 1 >= i - a[i]) j = m, r = m - 1; n                 else l = m + 1; n             } n             if (j >= 0) f[i] = max (A.query (j + 1, i - 1), i - 1), fr[i] = j; n             if (f[i - 1] >= i && f[i] < i + a[i]) f[i] = i + a[i], fr[i] = i - 1, k[i] = 1; n             if (mx > f[i]) f[i] = mx, fr[i] = w; else mx = f[i], w = i; n             // printf ( 666 %d %d %d %d n , j, i, f[i], fr[i]); n         } n         if (f[n] >= n - 1) { n             puts ( YES ); n             for (int i = n; i >= 1; i = fr[i]) { n                 for (int j = fr[i] + 1; j < i; ++j) d[j] = 1; d[i] = k[i]; n             } n             for (int i = 1; i < n; ++i) putchar (d[i] ?  R  :  L ); puts (  ); n         } else puts ( NO ); n     } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   bool ch = 0; n   bool arr[n + 1]; n   vector<int> vec, vec1; n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (b > a) swap(a, b); n     vec.push_back(a); n     vec1.push_back(b); n   } n   if (!m) n     cout << n - 1; n   else { n     sort(vec.begin(), vec.end()); n     sort(vec1.begin(), vec1.end()); n     cout << max(0, vec[0] - vec1[vec1.size() - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   bool p = 1; n   cin >> a; n   int A[a]; n   for (int i = 0; i < a; i++) { n     cin >> A[i]; n     if (A[i] == 1) { n       p = 0; n     } n   } n   if (p) { n     cout <<  EASY ; n   } else { n     cout <<  HARD ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { return (a > b) ? a : b; } n long long min(long long a, long long b) { return (a < b) ? a : b; } n vector<long long> ar[100010]; n long long vis[100010], dist[10001]; n long long solve() { return 0; } n long long cc; n double pi = 3.141592653589793238; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     n = 2 * n; n     double ans = 1 / tan(pi / n); n     cout << fixed << setprecision(11) << ans <<   n ; 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   cout.tie(0); n   int n, c, a; n   cin >> n >> c; n   vector<int> arr; n   for (int i = 0; i < n; i++) { n     cin >> a; n     arr.push_back(a); n   } n   int maxx = 0; n   for (int i = 0; i < n - 1; i++) { n     maxx = max(maxx, arr[i] - arr[i + 1] - c); n   } n   cout << maxx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100001; n long long h, c, temp; n long double f(long long m) { n   long double r = 1.0 * (m * h + (m)*c) / (2 * m); n   return (abs(r - temp)); n } n long double f12(long long m) { n   long double r = 1.0 * (m * h + (m - 1) * c) / (2 * m - 1); n   return (abs(r - temp)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   t = 1; n   cin >> t; n   while (t--) { n     long long i, j, k; n     cin >> h >> c >> temp; n     long long l = 1; n     long long r = 1000000000; n     while (r - l > 2) { n       long long m1 = (l * 2 + r) / 3; n       long long m2 = (l + 2 * r) / 3; n       if (f(m1) > f(m2)) n         l = m1; n       else n         r = m2; n     } n     long double ans = f(2); n     j = 2; n     for (i = l; i <= r; i++) { n       if (f(i) < ans) { n         j = 2 * i; n         ans = f(i); n       } n     } n     l = 1; n     r = 1000000000; n     while (r - l > 2) { n       long long m1 = (l * 2 + r) / 3; n       long long m2 = (l + 2 * r) / 3; n       if (f12(m1) > f12(m2)) n         l = m1; n       else n         r = m2; n     } n     for (i = l; i <= r; i++) { n       if (f12(i) < ans) { n         j = 2 * i - 1; n         ans = f12(i); n       } n     } n     cout << j <<   n ; n   } n } n


#include <bits/stdc++.h> n int n, p[4005], f[4005][4005], w[4005], tl; n void solve() { n   scanf( %d , &n); n   n <<= 1; n   for (int i = 1; i <= n; ++i) scanf( %d , &p[i]); n   tl = 0; n   int pre = 1; n   w[++tl] = 1; n   for (int i = 2; i <= n; ++i) { n     if (p[i] < p[pre]) n       w[tl]++; n     else { n       pre = i; n       w[++tl] = 1; n     } n   } n   for (int i = 0; i <= n; ++i) n     for (int j = 0; j <= n; ++j) f[i][j] = 0; n   f[0][0] = 1; n   for (int i = 1; i <= tl; ++i) n     for (int j = 0; j <= n; ++j) n       f[i][j] = f[i - 1][j] | (j - w[i] >= 0 ? f[i - 1][j - w[i]] : 0); n   if (f[tl][n / 2]) n     puts( YES ); n   else n     puts( NO ); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long m, d; n long long a[1000][1000]; n long long me[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> m >> d; n   long long cnt = 1, cnt1 = 0; n   long long pr = 0; n   long long ok = 0; n   for (long long i = 1; i <= 1000; i++) { n     if (ok == 0) { n       for (long long j = d; j <= 7; j++) { n         a[i][j] = cnt; n         cnt++; n         if (cnt - 1 == me[m - 1]) { n           cout << i; n           return 0; n         } n       } n       ok = 1; n     } else { n       for (long long j = 1; j <= 7; j++) { n         a[i][j] = cnt; n         cnt++; n         if (cnt - 1 == me[m - 1]) { n           cout << i; n           return 0; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 4e18; n const int inf = 2e9; n const double eps = 1e-13; n const int N = 1e5 + 5; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n int mod; n int a[N]; n int b[N]; n void norm(int &x) { n   x %= mod; n   if (x < 0) x += mod; n } n int main(void) { n   int i, j, n, m; n   scanf( %d %d %d , &n, &m, &mod); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (i = 1; i <= m; ++i) { n     scanf( %d , &b[i]); n     b[i] += b[i - 1]; n   } n   for (i = 1; i <= n; ++i) { n     int l = max(1, m - n + i); n     int r = min(m, i); n     a[i] += b[r] - b[l - 1]; n     norm(a[i]); n     printf( %d  , a[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n template <class _T> n bool maximize(_T &a, _T b) { n   return a < b ? a = b, 1 : 0; n } n template <class _T> n bool minimize(_T &a, _T b) { n   return a > b ? a = b, 1 : 0; n } n const int ooit = 2e9; n const long long ooll = 1e15; n using namespace std; n int n, q, a[200005]; n void enter() { n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     char c; n     cin >> c; n     a[i] = (int)c; n   } n } n set<int> pos[256]; n struct BIT { n   int val[200005]; n   void update(int i, int x, int n) { n     for (; i <= n; i += i & -i) val[i] += x; n   } n   int get(int i) { n     int res = 0; n     for (; i > 0; i -= i & -i) res += val[i]; n     return res; n   } n } tree; n void prepare() { n   for (int i = 1; i <= n; i++) pos[a[i]].insert(i), tree.update(i, 1, n); n } n int tfind(int x) { n   int l = 1, r = n, res = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (tree.get(mid) >= x) n       r = mid - 1, res = mid; n     else n       l = mid + 1; n   } n   return res; n } n bool is_remove[200005]; n void update(int l, int r, int x) { n   auto it_down = pos[x].lower_bound(l); n   if (it_down == pos[x].end()) return; n   auto it_up = pos[x].upper_bound(r); n   vector<int> must_remove; n   for (; it_down != it_up; it_down++) must_remove.push_back(*it_down); n   for (auto i : must_remove) { n     pos[x].erase(i); n     tree.update(i, -1, n); n     is_remove[i] = true; n   } n } n void process() { n   prepare(); n   for (int i = 1; i <= q; i++) { n     int l, r, cx; n     char c; n     cin >> l >> r >> c; n     cx = (int)c; n     int tl = tfind(l), tr = tfind(r); n     update(tl, tr, cx); n   } n   for (int i = 1; i <= n; i++) n     if (!is_remove[i]) cout << (char)a[i]; n   cout << endl; n } n int main() { n   enter(); n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INFint = 2147483647; n const long long INF = 9223372036854775807ll; n const long long MOD = 1000000007ll; n const long double EPS = 1e-9; n vector<int> p; n vector<int> used; n vector<vector<int> > g; n long long ans = 0; n void dfs(int v) { n   used[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (used[to]) { n       if (p[to] == p[v]) { n         ans++; n       } n     } else { n       p[to] = !p[v]; n       dfs(to); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   p.resize(n); n   g.resize(n); n   used.resize(n); n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       p[i] = 0; n       dfs(i); n     } n   } n   ans /= 2; n   if ((n - ans) & 1) ans++; n   cout << ans << endl; n   fprintf(stderr,   nTIME = %lf n , 1.0 * clock() / CLOCKS_PER_SEC); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 1000 * 100 + 10, Z = 26, P = 2, mod = 1e9 + 9; n int pw[N], wp[N], n, m, tar[Z], hsh[N][Z]; n string s, t; n long long Pow(long long a, long long b) { n   return b ? (b & 1 ? a : 1) % mod * Pow(a * a % mod, b >> 1) % mod : 1; n } n int add(int a, int b) { n   a += b; n   a += (a < 0) * mod; n   a -= (a >= mod) * mod; n   return a; n } n int mul(int a, int b) { return 1LL * a * b % mod; } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   pw[0] = 1; n   for (int i = 1; i < N; i++) pw[i] = mul(pw[i - 1], P); n   wp[N - 1] = Pow(pw[N - 1], mod - 2); n   for (int i = N - 2; i >= 0; i--) wp[i] = mul(wp[i + 1], P); n   cin >> n >> m >> s >> t; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < Z; j++) n       tar[j] = add(tar[j], mul(pw[i], t[i] -  a  == j)); n   vector<pair<int, int>> targ; n   for (int i = 0; i < Z; i++) targ.push_back({tar[i], i}); n   sort(targ.begin(), targ.end()); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < Z; j++) n       hsh[i][j] = add(hsh[i - 1][j], mul(pw[i - 1], s[i - 1] -  a  == j)); n   vector<int> o; n   for (int i = 0; i + m <= n; i++) { n     bool res = true; n     vector<pair<int, int>> me; n     for (int j = 0; j < Z; j++) { n       int val = mul(add(hsh[i + m][j], -hsh[i][j]), wp[i]); n       me.push_back({val, j}); n     } n     sort(me.begin(), me.end()); n     for (int i = 0; i < Z; i++) res &= me[i].first == targ[i].first; n     set<pair<int, int>> st; n     set<int> NO; n     for (int i = 0; i < Z; i++) { n       if (me[i].first == 0) continue; n       if (me[i].second != targ[i].second) n         if (me[i].second < targ[i].second) n           st.insert({me[i].second, targ[i].second}); n         else n           st.insert({targ[i].second, me[i].second}); n       else n         NO.insert(me[i].second); n     } n     set<int> F, S; n     for (auto it : st) F.insert(it.first), S.insert(it.second); n     res &= (int)(F.size()) == (int)(st.size()); n     res &= (int)(S.size()) == (int)(st.size()); n     for (auto it : st) S.insert(it.first); n     res &= (int)(S.size()) == ((int)(st.size()) * 2); n     for (auto it : S) res &= !NO.count(it); n     if (res) o.push_back(i + 1); n   } n   cout << (int)(o.size()) <<   n ; n   for (auto i : o) cout << i <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1001; n bool used[N][N]; n bool best[N][N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   vector<pair<int, int>> points(3); n   for (auto& [x, y] : points) { n     cin >> x >> y; n   } n   int x, y, cnt; n   auto update_x = [&](int a) { n     while (x != a) { n       if (x > a) { n         --x; n       } else { n         ++x; n       } n       if (!used[x][y]) { n         used[x][y] = true; n         ++cnt; n       } n     } n   }; n   auto update_y = [&](int a) { n     while (y != a) { n       if (y > a) { n         --y; n       } else { n         ++y; n       } n       if (!used[x][y]) { n         used[x][y] = true; n         ++cnt; n       } n     } n   }; n   int ans = 1e9; n   vector<int> perm = {0, 1, 2}; n   do { n     for (int dir1 = 0; dir1 < 2; ++dir1) { n       for (int dir2 = 0; dir2 < 2; ++dir2) { n         memset(used, false, sizeof used); n         x = points[perm[0]].first; n         y = points[perm[0]].second; n         used[x][y] = true; n         cnt = 1; n         if (dir1 == 0) { n           update_x(points[perm[1]].first); n           update_y(points[perm[1]].second); n         } else { n           update_y(points[perm[1]].second); n           update_x(points[perm[1]].first); n         } n         if (dir2 == 0) { n           update_x(points[perm[2]].first); n           update_y(points[perm[2]].second); n         } else { n           update_y(points[perm[2]].second); n           update_x(points[perm[2]].first); n         } n         if (cnt < ans) { n           ans = cnt; n           memcpy(best, used, sizeof used); n         } n       } n     } n   } while (next_permutation(perm.begin(), perm.end())); n   cout << ans <<   n ; n   for (int i = 0; i <= 1000; ++i) { n     for (int j = 0; j <= 1000; ++j) { n       if (best[i][j]) { n         cout << i <<     << j <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   long int a, i, s; n   scanf( %ld , &a); n   s = a; n   if (a != 1) { n     for (i = 2;; i++) { n       s = s + 1 + i * (a - i); n       if ((a - i) == 0) { n         break; n       } n     } n     printf( %ld , s); n   } else { n     printf( 1 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int A[] = {2, 3, 5, 7, 11, 13, 17, 19}, n, B[5005], vmax; n priority_queue<int> Q; n set<int> S; n inline int sol(int i) { n   int j, k, t, first, second; n   B[0] = 1; n   for (j = 1; j <= n; j++) { n     first = B[j - 1] * A[0]; n     for (t = 0; t < j; t++) n       for (k = 0; k < i; k++) n         if (B[t] * A[k] > B[j - 1]) first = min(first, B[t] * A[k]); n     B[j] = first; n     if (first > vmax) return 0; n   } n   for (j = 0; j < i; j++) { n     first = 0; n     for (k = 1; k <= n; k++) n       if (B[k] % A[j] == 0) first++; n     if (first < (n + 1) / 2) { n       S.clear(); n       for (k = 1; k <= n; k++) S.insert(B[k]); n       first = (n + 1) / 2 - first; n       for (k = 1; k <= n && first; k++) n         if (B[k] % A[j] != 0) { n           second = B[k] * A[j]; n           while (S.find(second) != S.end()) { n             second *= A[j]; n             if (second > vmax) return 0; n           } n           if (second > vmax) return 0; n           S.erase(B[k]); n           B[k] = second; n           S.insert(B[k]); n           first--; n         } n       sort(B + 1, B + n + 1); n     } n   } n   for (j = 0; j < i; j++) { n     first = 0; n     for (k = 1; k <= n; k++) n       if (B[k] % A[j] == 0) first++; n     if (first < (n + 1) / 2) return 0; n   } n   return 1; n } n int main() { n   cin >> n; n   vmax = 2 * n * n; n   int i; n   for (i = 1; i <= 8; i++) n     if (sol(i)) break; n   printf( %d , B[1]); n   for (i = 2; i <= n; i++) printf(  %d , B[i]); n   printf(  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   cout.tie(0); n   int teamA, teamB, y1, y2, y; n   cin >> teamA >> teamB >> y1 >> y2 >> y; n   int min = y - (teamA * (y1 - 1) + teamB * (y2 - 1)); n   if (min < 0) { n     cout << 0 <<    ; n   } else { n     cout << min <<    ; n   } n   int max = y - teamA * y1 - teamB * y2; n   if (max < 0) { n     if (y2 < y1) { n       max = y - teamB * y2; n       if (max < 0) { n         cout << y / y2 << endl; n         return 0; n       } else if (max == 0) { n         cout << teamB << endl; n         return 0; n       } n       cout << teamB + max / y1 << endl; n       return 0; n     } else { n       max = y - teamA * y1; n       if (max < 0) { n         cout << y / y1 << endl; n         return 0; n       } else if (max == 0) { n         cout << teamA << endl; n         return 0; n       } n       cout << teamA + max / y2 << endl; n       return 0; n     } n   } else { n     cout << teamA + teamB << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000010; n long long a[maxn], s1, s2; n double ans[maxn]; n int l[maxn], r[maxn]; n vector<int> gao[6][maxn]; n int n, m, pos; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   l[1] = 1; n   for (int i = 2; i <= n; i++) { n     l[i] = i; n     while (l[i] != 1 && a[l[i] - 1] > a[i]) l[i] = l[l[i] - 1]; n   } n   r[n] = n; n   for (int i = n - 1; i >= 1; i--) { n     r[i] = i; n     while (r[i] != n && a[r[i] + 1] >= a[i]) r[i] = r[r[i] + 1]; n   } n   for (int i = 1; i <= n; i++) { n     int a1 = r[i] + 1 - i; n     int a2 = i + 1 - l[i]; n     if (a1 <= a2) { n       gao[0][a1].push_back(i); n       gao[1][a2 + 1].push_back(i); n       gao[2][r[i] - l[i] + 3].push_back(i); n     } else { n       gao[3][a2].push_back(i); n       gao[4][a1 + 1].push_back(i); n       gao[5][r[i] - l[i] + 3].push_back(i); n     } n   } n   for (int i = 1; i <= n; i++) s1 += a[i]; n   for (int L = 1; L <= n; L++) { n     for (int i = 0, sz = gao[0][L].size(); i < sz; i++) { n       pos = gao[0][L][i]; n       s1 -= a[pos]; n       s2 += (r[pos] - pos + 1LL) * a[pos]; n     } n     for (int i = 0, sz = gao[1][L].size(); i < sz; i++) { n       pos = gao[1][L][i]; n       s2 += (pos - l[pos] + 1LL) * a[pos]; n       s1 -= a[pos]; n     } n     for (int i = 0, sz = gao[2][L].size(); i < sz; i++) { n       pos = gao[2][L][i]; n       s2 -= (r[pos] - l[pos] + 2LL) * a[pos]; n       s1 += a[pos]; n     } n     for (int i = 0, sz = gao[3][L].size(); i < sz; i++) { n       pos = gao[3][L][i]; n       s1 -= a[pos]; n       s2 += (pos - l[pos] + 1LL) * a[pos]; n     } n     for (int i = 0, sz = gao[4][L].size(); i < sz; i++) { n       pos = gao[4][L][i]; n       s2 += (r[pos] - pos + 1LL) * a[pos]; n       s1 -= a[pos]; n     } n     for (int i = 0, sz = gao[5][L].size(); i < sz; i++) { n       pos = gao[5][L][i]; n       s2 -= (r[pos] - l[pos] + 2LL) * a[pos]; n       s1 += a[pos]; n     } n     long long temp = s1 * L + s2; n     ans[L] = 1.0 * temp / (n - L + 1); n   } n   scanf( %d , &m); n   while (m--) { n     int k; n     scanf( %d , &k); n     printf( %.10f n , ans[k]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   char arr[105]; n   scanf( %d%*c , &t); n   while (t--) { n     gets(arr); n     int len = strlen(arr); n     char temp[20] = {arr[len - 5], arr[len - 4], arr[len - 3], arr[len - 2], n                      arr[len - 1]}; n     char tem[20] = {arr[0], arr[1], arr[2], arr[3], arr[4]}; n     if (strcmp(temp,  lala. ) == 0 && strcmp(tem,  miao. ) == 0) n       printf( OMG>.< I don t know! n ); n     else if (strcmp(temp,  lala. ) == 0) n       printf( Freda s n ); n     else if (strcmp(tem,  miao. ) == 0) n       printf( Rainbow s n ); n     else n       printf( OMG>.< I don t know! n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a, pertama, kedua, sum; n vector<pair<long long, int> > v; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                greater<pair<long long, int> > > n     pq; n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.second == b.second) return a.first < b.first; n   return a.second < b.second; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     pq.push(make_pair(a, i)); n   } n   while (!pq.empty()) { n     pertama = pq.top().first; n     kedua = pq.top().second; n     pq.pop(); n     if (pertama == pq.top().first) { n       pq.push(make_pair(2 * pertama, pq.top().second)); n       pq.pop(); n     } else { n       v.push_back(make_pair(pertama, kedua)); n     } n     if (pq.empty()) v.push_back(make_pair(pertama, kedua)); n   } n   sort(v.begin(), v.end(), cmp); n   sum = v.size(); n   cout << sum << endl; n   for (int i = 0; i < sum; i++) { n     if (i != 0) cout <<    ; n     cout << v[i].first; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prime_pair; n int least[1001]; n void sieve(int n) { n   vector<int> pr, prime; n   for (int i = 2; i <= n; i++) { n     if (!least[i]) { n       least[i] = i; n       pr.push_back(i); n     } n     for (int j = 0; j < pr.size() && pr[j] <= least[i] && i * pr[j] <= n; j++) { n       least[i * pr[j]] = pr[j]; n     } n   } n   for (int i = 2; i <= n; i++) n     if (least[i] == i) prime.push_back(i); n   for (int i = 1; i < prime.size(); i++) { n     int temp = prime[i] + prime[i - 1]; n     prime_pair.push_back(temp); n   } n } n bool bi_search(int val) { n   int left = 0, right = prime_pair.size() - 1; n   while (right >= left) { n     int mid = (left + right) / 2; n     if (prime_pair[mid] == val) return true; n     if (prime_pair[mid] > val) n       right = mid - 1; n     else n       left = mid + 1; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   sieve(n); n   int res = 0; n   for (int i = 13; i <= n; i++) { n     if (least[i] == i && bi_search(i - 1)) { n       res++; n     } n   } n   if (res >= k) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q, ar[200000], ans[200000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   for (int i = 0; i < n - 1; i++) { n     q = 1; n     while (i + q * 2 < n) q *= 2; n     ar[i + q] += ar[i]; n     if (i) ans[i] = ans[i - 1]; n     ans[i] += ar[i]; n     ar[i] = 0; n   } n   for (int i = 0; i < n - 1; i++) cout << ans[i] << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   static int aa[200000], pp[200000], qq[200000]; n   int n, k, i, p, q; n   long long x, xk, ans, a; n   scanf( %d%d%lld , &n, &k, &x); n   for (i = 0; i < n; i++) scanf( %d , &aa[i]); n   for (i = 0, p = 0; i < n; i++) { n     pp[i] = p; n     p |= aa[i]; n   } n   for (i = n - 1, q = 0; i >= 0; i--) { n     qq[i] = q; n     q |= aa[i]; n   } n   xk = 1; n   while (k--) xk *= x; n   ans = 0; n   for (i = 0; i < n; i++) n     if (ans < (a = pp[i] | aa[i] * xk | qq[i])) ans = a; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n double ans; n int cnt[200]; n vector<int> v[200]; n int main() { n   cin >> s; n   double n = s.size(); n   for (int i = 0; i < n; i++) v[s[i]].push_back(i); n   for (char i =  a ; i <=  z ; i++) { n     if (!v[i].empty()) { n       int x = v[i][0]; n       int y = 0; n       for (int k = 0; k < n; k++) { n         memset(cnt, 0, sizeof cnt); n         for (int j = 0; j < v[i].size(); j++) { n           v[i][j]++; n           v[i][j] %= (int)n; n           cnt[s[v[i][j]]]++; n         } n         int cur = 0; n         for (int j =  a ; j <=  z ; j++) cur += (cnt[j] == 1); n         y = max(y, cur); n       } n       ans += y; n     } n   } n   cout << fixed << setprecision(8) << (ans / n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 2e5; n int n, k; n long long add[sz], cost[sz], pre[sz], suf[sz], mnpre[sz], mnsuf[sz], n     bestSufTake[sz], bestSufTakeFromPre[sz]; n void input() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> add[i]; n   for (int i = 0; i < n; i++) cin >> cost[i]; n } n void initPreSuf() { n   for (int i = 0; i < n; i++) pre[i] = add[i] + (i ? pre[i - 1] : 0); n   for (int i = n - 1; i >= 0; i--) n     suf[i] = add[i] + (i != n - 1 ? suf[i + 1] : 0); n   for (int i = 0; i < n; i++) n     mnpre[i] = (i ? min(mnpre[i - 1], cost[i]) : cost[i]); n   for (int i = n - 1; i >= 0; i--) n     mnsuf[i] = (i != n - 1 ? min(mnsuf[i + 1], cost[i]) : cost[i]); n   for (int i = n - 1; i >= 0; i--) n     bestSufTake[i] = max(bestSufTake[i + 1], suf[i] - cost[i]); n   for (int i = 0; i < n; i++) { n     if (!i) n       bestSufTakeFromPre[i] = suf[i] - cost[i]; n     else n       bestSufTakeFromPre[i] = max(bestSufTakeFromPre[i - 1], suf[i] - cost[i]); n   } n } n void solveZero() { cout << max(0LL, bestSufTake[0]) << endl; } n void solveEven() { n   long long bst = max(0LL, bestSufTake[0]); n   for (int i = 0; i < n - 1; i++) bst = max(bst, pre[n - 1] - cost[i]); n   cout << bst << endl; n } n void solveOne() { n   long long bst = max(0LL, bestSufTake[1]); n   for (int i = 1; i < n - 1; i++) n     bst = max(bst, pre[i] - mnpre[i] + bestSufTake[i + 1]); n   for (int i = 1; i < n - 1; i++) n     bst = max(bst, max(bestSufTakeFromPre[i - 1] - add[i], bestSufTake[i + 1]) + n                        max(0LL, add[i] - cost[i])); n   cout << bst << endl; n } n void solveOdd() { n   long long bst = max(0LL, bestSufTake[1]); n   long long best = add[0] - cost[0]; n   for (int i = n - 2; i >= 0; i--) { n     if (i == 1) continue; n     bst = max(bst, pre[n - 1] - cost[i]); n   } n   if (n == 4) { n     bst = max(bst, pre[n - 1] - cost[1]); n     bst = max(bst, pre[n - 1] - cost[0]); n   } else { n     bst = max(bst, pre[n - 1] - add[n - 1] - cost[1]); n     for (int i = 0; i < n; i++) { n       if (i == 1 || i == 2) continue; n       bst = max(bst, pre[n - 1] - add[i] - cost[1]); n     } n   } n   cout << bst << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   input(); n   initPreSuf(); n   if (k == 0) n     solveZero(); n   else if (k == 1) n     solveOne(); n   else if (k % 2 == 0) n     solveEven(); n   else n     solveOdd(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, x, y, lsum = 1, root, tot, Ans1, Ans2; n int head[400010], f[400010], size[400010], d[400010], p[400010], l[400010], n     g[400010], o[400010]; n vector<int> son[400010]; n map<int, int> M; n struct Edge { n   int t, next, l; n } e[400010 * 8]; n inline int Find(int x) { return (x == p[x]) ? x : p[x] = Find(p[x]); } n inline void Add(int s, int t) { n   e[lsum].t = t; n   e[lsum].next = head[s]; n   head[s] = lsum++; n } n inline int Abs(int x) { return (x < 0) ? -x : x; } n inline void Swap(int &a, int &b) { a ^= b ^= a ^= b; } 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 read() { n   int p = 0, f = 1; n   char c = getchar(); n   while (c < 48 || c > 57) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >= 48 && c <= 57) p = (p << 1) + (p << 3) + c - 48, c = getchar(); n   return p * f; n } n inline void Maketree(int x, int fa) { n   int i = 0; n   size[x] = 1; n   for (i = head[x]; i; i = e[i].next) { n     if (e[i].t == fa) continue; n     f[e[i].t] = x; n     son[x].push_back(e[i].t); n     Maketree(e[i].t, x); n     size[x] += size[e[i].t]; n   } n } n inline void Dfs(int x) { n   if (!son[x].size()) { n     o[p[x]] = 1; n     return; n   } n   for (auto i : son[x]) { n     Dfs(i); n     g[p[x]] += o[p[i]]; n     o[p[x]] += g[p[i]]; n   } n   if (g[p[x]] && o[p[x]]) Ans1 = 3; n } n int main() { n   n = read(); n   for (i = 1; i < n; i++) { n     x = read(); n     y = read(); n     Add(x, y); n     Add(y, x); n     d[x]++; n     d[y]++; n   } n   for (i = 1; i <= n; i++) n     if (d[i] > 1) { n       root = i; n       break; n     } n   Maketree(root, 0); n   for (i = 1; i <= n; i++) p[i] = n + i; n   for (i = 1; i <= n; i++) n     if (!son[i].size()) p[i] = p[f[i]]; n   for (i = 1; i <= n; i++) n     if (p[i] == n + i) p[i] = i; n   for (i = 1; i <= n; i++) n     if (!M[p[i]]) Ans2++, M[p[i]]++; n   Ans1 = 1; n   Dfs(root); n   printf( %d %d n , Ans1, Ans2 - 1); 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(NULL); n   cout.tie(NULL); n   long long n, l = 1, r; n   cin >> n; n   r = n; n   if (n == 1) { n     cout << 1 << endl << 1; n     return 0; n   } n   if (n == 2) { n     cout << 1 << endl << 1; n     return 0; n   } n   if (n == 3) { n     cout << 2 << endl << 1 <<     << 3; n     return 0; n   } n   if (n == 4) { n     cout << 4 << endl << 2 <<     << 4 <<     << 1 <<     << 3; n     return 0; n   } n   if (n == 5) { n     cout << 5 << endl << 1 <<     << 3 <<     << 5 <<     << 2 <<     << 4; n     return 0; n   } n   vector<long long> a(n + 1); n   long long f = 0; n   for (long long i = 1; i <= n; i++) { n     if (f) { n       a[l] = i; n       l++; n     } else { n       a[r] = i; n       r--; n     } n     f += 1; n     f %= 2; n   } n   for (long long i = 1; i < n; i++) { n     if (abs(a[i] - a[i + 1]) == 1) { n       swap(a[i + 1], a[n]); n     } n   } n   cout << n <<   n ; n   ; n   for (long long i = 1; i <= n; i++) { n     cout << a[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n template <class T1, class T2, class T3> n inline void gn(T1 &x1, T2 &x2, T3 &x3) { n   gn(x1, x2), gn(x3); n } n template <class T1, class T2, class T3, class T4> n inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   gn(x1, x2, x3), gn(x4); n } n template <class T1, class T2, class T3, class T4, class T5> n inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   gn(x1, x2, x3, x4), gn(x5); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T1, class T2> n inline void print(T1 x1, T2 x2) { n   printsp(x1), println(x2); n } n template <class T1, class T2, class T3> n inline void print(T1 x1, T2 x2, T3 x3) { n   printsp(x1), printsp(x2), println(x3); n } n template <class T1, class T2, class T3, class T4> n inline void print(T1 x1, T2 x2, T3 x3, T4 x4) { n   printsp(x1), printsp(x2), printsp(x3), println(x4); n } n template <class T1, class T2, class T3, class T4, class T5> n inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) { n   printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int a[300300], id[300300]; n set<int> s; n bool cmp(int u, int v) { n   if (a[u] > a[v]) n     return 1; n   else if (a[u] == a[v]) { n     if (u > v) n       return 1; n     else n       return 0; n   } else n     return 0; n } n double calc(int p) { n   s.insert(p); n   double A = 0; n   double B = 0; n   set<int>::iterator pre, nxt; n   pre = nxt = s.find(p); n   for (int i = 0; i < 50 and pre != s.begin(); i++) { n     pre--; n     A += (double)(*nxt - *pre) / (1ll << i); n     nxt--; n   } n   pre = nxt = s.find(p); n   nxt++; n   for (int i = 0; i < 50 and nxt != s.end(); i++) { n     B += (double)(*nxt - *pre) / (1ll << i); n     pre++; n     nxt++; n   } n   return A * B / 2; n } n int main() { n   int n; n   gn(n); n   for (int i = 0; i < n; i++) { n     gn(a[i]); n     id[i] = i; n   } n   sort(id, id + n, cmp); n   double ans = 0; n   s.insert(-1); n   s.insert(n); n   for (int i = 0; i < n; i++) { n     ans += calc(id[i]) * a[id[i]]; n   } n   ans /= ((long long)n * n); n   printf( %.6lf n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& os, vector<T> v) { n   os <<  [ ; n   string sep; n   for (const T& el : v) os << sep << el, sep =  ,  ; n   return os <<  ] ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n int dsu_find(vector<int>& parents, int a) { n   return (parents[a] == a) ? a : (parents[a] = dsu_find(parents, parents[a])); n } n bool dsu_union(vector<int>& parents, int a, int b) { n   int pa = dsu_find(parents, a); n   int pb = dsu_find(parents, b); n   if (pa != pb) { n     parents[pa] = pb; n     return true; n   } n   return false; n } n void run() { n   int m, n; n   cin >> m >> n; n   vector<long long> a(m + 1), b(n + 1); n   for (int i = 1; i < m + 1; i++) cin >> a[i]; n   for (int j = 1; j < n + 1; j++) cin >> b[j]; n   auto cost = [&](int x, int y) -> long long { n     int u = min(x, y), v = max(x, y); n     return a[u] + b[v - m]; n   }; n   long long tot_cost = 0; n   vector<pair<long long, pair<int, int> > > edgelist; n   for (int i = 1; i < m + 1; i++) { n     int s; n     cin >> s; n     for (int x = 0; x < s; x++) { n       int j; n       cin >> j; n       edgelist.push_back(make_pair(a[i] + b[j], make_pair(i, m + j))); n       tot_cost += (a[i] + b[j]); n     } n   } n   sort(edgelist.begin(), edgelist.end(), n        greater<pair<long long, pair<int, int> > >()); n   vector<int> parents(m + n + 1); n   for (int i = 1; i < m + n + 1; i++) parents[i] = i; n   long long mst_cost = 0; n   for (pair<long long, pair<int, int> > e : edgelist) { n     ; n     if (dsu_find(parents, e.second.first) != n         dsu_find(parents, e.second.second)) { n       mst_cost += e.first; n       dsu_union(parents, e.second.first, e.second.second); n       ; n     } n   } n   cout << tot_cost - mst_cost <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   run(); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int mod = 998244353; n int sum(int a, int b) { n   int s = a + b; n   if (s >= mod) s -= mod; n   return s; n } n int mult(int a, int b) { return (1LL * a * b) % mod; } n int pw(int a, int b) { n   if (b == 0) return 1; n   if (b & 1) return mult(a, pw(a, b - 1)); n   int res = pw(a, b / 2); n   return mult(res, res); n } n int sub(int a, int b) { n   int s = a - b; n   if (s < 0) s += mod; n   return s; n } n int n; n const int maxN = (int)1e6 + 100; n string s; n int invfact[maxN]; n int fact[maxN]; n int inv[maxN]; n int cnk(int a, int b) { n   if (a < b || b < 0) return 0; n   return mult(fact[a], mult(invfact[b], invfact[a - b])); n } n int tp[maxN]; n int cnt0[maxN], cnt1[maxN]; n int c_pref[maxN]; n int c_2[maxN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   inv[1] = fact[0] = fact[1] = invfact[0] = invfact[1] = 1; n   for (int i = 2; i < maxN; i++) { n     fact[i] = mult(fact[i - 1], i); n     inv[i] = mult(inv[mod % i], mod - mod / i); n     invfact[i] = mult(invfact[i - 1], inv[i]); n   } n   cin >> s; n   n = s.size(); n   int que = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  ( ) n       tp[i + 1] = 1; n     else if (s[i] ==  ) ) n       tp[i + 1] = 0; n     else n       tp[i + 1] = 2; n     que += tp[i + 1] == 2; n   } n   cnt1[0] = 0; n   for (int i = 1; i <= n; i++) { n     cnt1[i] = cnt1[i - 1] + (tp[i] == 1); n   } n   cnt0[n + 1] = 0; n   for (int i = n; i >= 1; i--) { n     cnt0[i] = cnt0[i + 1] + (tp[i] == 0); n   } n   c_pref[que + 1] = 0; n   for (int i = que; i >= 0; i--) { n     c_pref[i] = sum(c_pref[i + 1], cnk(que, i)); n   } n   c_2[que] = 0; n   for (int i = que - 1; i >= 0; i--) { n     c_2[i] = sum(c_2[i + 1], cnk(que - 1, i)); n   } n   int val = 0; n   int que1 = 0; n   for (int pref = 1; pref < n; pref++) { n     if (tp[pref] == 0) continue; n     if (tp[pref] == 1) { n       int at_most = min(que + 1, que1 + 1 + cnt1[pref - 1] - cnt0[pref + 1]); n       at_most = max(at_most, 0); n       val = sum(val, c_pref[at_most]); n       que1 += (tp[pref] == 2); n     } else { n       int at_most = min(que + 1, que1 + 1 + cnt1[pref - 1] - cnt0[pref + 1]); n       at_most = max(at_most, 0); n       val = sum(val, c_2[at_most]); n       que1 += (tp[pref] == 2); n     } n   } n   cout << val; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const static int inf = 10000000; n const static int mod = 1000000007; n int check(const vector<string>& s, const vector<string>& t) { n   int n = s.size(); n   vector<int> idx(n); n   for (int i = 0; i < (int)(n); i++) { n     idx[i] = i; n   } n   int ret = 0; n   do { n     vector<string> ss(n); n     for (int i = 0; i < (int)(n); i++) { n       ss[i] = s[idx[i]]; n     } n     int pos = 0; n     for (int i = 0; i < t.size() && pos < s.size(); ++i) { n       if (ss[pos] == t[i]) { n         ++pos; n       } n     } n     if (pos != s.size()) { n       continue; n     } n     int inv = 0; n     for (int i = 0; i < (int)(n); i++) { n       for (int j = i + 1; j < n; ++j) { n         if (idx[i] > idx[j]) { n           ++inv; n         } n       } n     } n     int buf = n * (n - 1) / 2 - inv + 1; n     ret = max(ret, buf); n   } while (next_permutation((idx).begin(), (idx).end())); n   return ret; n } n int main() { n   int n; n   while (cin >> n) { n     vector<string> s(n); n     for (int i = 0; i < (int)(n); i++) { n       cin >> s[i]; n     } n     int mxp = 0; n     int idx = -1; n     int k; n     cin >> k; n     for (int i = 0; i < (int)(k); i++) { n       int m; n       cin >> m; n       vector<string> t(m); n       for (int j = 0; j < (int)(m); j++) { n         cin >> t[j]; n       } n       int p = check(s, t); n       if (p > mxp) { n         mxp = p; n         idx = i; n       } n     } n     if (idx == -1) { n       cout <<  Brand new problem!  << endl; n     } else { n       cout << idx + 1 << endl; n       cout <<  [: ; n       for (int i = 0; i < (int)(mxp); i++) { n         cout <<  | ; n       } n       cout <<  :]  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n bool ask(int x, int y) { n   if (x < 0 || y > n) return 0; n   string s; n   cout << 1 <<     << x <<     << y << endl; n   cin >> s; n   return s ==  TAK ; n } n int solve(int l, int r) { n   if (l > r) return -1; n   int mid; n   while (l < r) { n     mid = (l + r) >> 1; n     if (ask(mid, mid + 1)) n       r = mid; n     else n       l = mid + 1; n   } n   return l; n } n int main() { n   int i, j; n   cin >> n >> k; n   int ans1 = solve(1, n); n   int ans2 = solve(1, ans1 - 1); n   if (!ask(ans2, ans1)) ans2 = solve(ans1 + 1, n); n   cout << 2 <<     << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200200; n const int INF = 1000000007; n struct Ramp { n   int i, x, d, t, p; n   bool operator<(int o) const { return x < o; } n   bool operator<(const Ramp& o) const { return x < o.x; } n } r[MAXN], rr; n struct Edge { n   int a, b, c, t; n   Edge(int a, int b, int c, int t = -1) : a(a), b(b), c(c), t(t) {} n }; n vector<Edge> e[MAXN]; n int d[MAXN]; n const Edge* pre[MAXN]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     r[i].i = i + 1; n     scanf( %d%d%d%d , &r[i].x, &r[i].d, &r[i].t, &r[i].p); n     r[n + i].i = -1; n     r[n + i].x = r[i].x + r[i].d; n   } n   n += n; n   r[n].x = 0; n   r[n + 1].x = m; n   r[n].i = r[n + 1].i = -1; n   n += 2; n   sort(r, r + n); n   for (int i = 1; i < n; ++i) { n     e[i - 1].push_back(Edge(i - 1, i, r[i].x - r[i - 1].x)); n     e[i].push_back(Edge(i, i - 1, r[i].x - r[i - 1].x)); n   } n   for (int i = 0; i < n; ++i) { n     if (r[i].i == -1 || r[i].x < r[i].p) { n       continue; n     } n     rr.x = r[i].x - r[i].p; n     int a = lower_bound(r, r + n, rr) - r; n     rr.x = r[i].x + r[i].d; n     int b = lower_bound(r, r + n, rr) - r; n     for (int aa = a - 1; aa <= a; ++aa) { n       for (int bb = b; bb <= b; ++bb) { n         if (0 <= aa && aa < bb && bb < n) { n           int cc = r[i].p + r[i].t; n           cc += abs(r[i].x - r[i].p - r[aa].x); n           cc += abs(r[i].x + r[i].d - r[bb].x); n           e[aa].push_back(Edge(aa, bb, cc, r[i].i)); n         } n       } n     } n   } n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       q; n   fill(d, d + n, INF); n   fill(pre, pre + n, (Edge*)NULL); n   d[0] = 0; n   q.push({d[0], 0}); n   while (!q.empty()) { n     int t = q.top().first; n     int v = q.top().second; n     q.pop(); n     if (d[v] != t) { n       continue; n     } n     for (const Edge& w : e[v]) { n       if (d[w.b] > t + w.c) { n         d[w.b] = t + w.c; n         q.push({d[w.b], w.b}); n         pre[w.b] = &w; n       } n     } n   } n   printf( %d n , d[n - 1]); n   vector<int> ans; n   for (int i = n - 1; i > 0; i = pre[i]->a) { n     if (pre[i]->t != -1) { n       ans.push_back(pre[i]->t); n     } n   } n   reverse(ans.begin(), ans.end()); n   printf( %d n , (int)ans.size()); n   for (int i : ans) { n     printf( %d  , i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> st; n set<string>::iterator it; n vector<long long> v, vv; n int main() { n   int n, a[100000], cnt = 0, cnt2 = 0, k; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > 0) n       cnt++; n     else if (a[i] < 0) n       cnt2++; n   } n   if (n % 2 == 0) n     k = n / 2; n   else n     k = (n / 2) + 1; n   if (cnt > cnt2) { n     if (cnt >= k) n       cout << 2 << endl; n     else n       cout << 0 << endl; n   } else { n     if (cnt2 >= k) n       cout << -2 << endl; n     else n       cout << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[12][12]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]); n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         a[i][j] = min(a[i][j], a[i][k] + a[k][j]); n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) ans = max(ans, a[i][j]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int64_t ans; n int n, m, ve, ts[1000001], nn[1000001], t, cut[1000001], p[1000001], n     vis[1000001], low[1000001]; n vector<int> e[1000001]; n void dfs(int i) { n   low[i] = ts[i] = ++t; n   vis[i] = 1; n   for (auto&& x : e[i]) n     if (!vis[x]) { n       p[x] = i; n       dfs(x); n       low[i] = min(low[i], low[x]); n       if (low[x] > ts[i]) cut[x] = 1; n     } else if (x != p[i]) n       low[i] = min(low[i], low[x]); n } n int main() { n   scanf( %d%d , &n, &m); n   if (m < 2) return puts( 0 ), 0; n   for (int _ = 0; _ < m; ++_) { n     int s, t; n     scanf( %d%d , &s, &t); n     if (s == t) n       ++nn[s]; n     else n       e[s].push_back(t), e[t].push_back(s); n   } n   for (int i = 1; i <= n; ++i) n     if (e[i].size()) { n       dfs(i); n       break; n     } n   for (int i = 1; i <= n; ++i) n     if ((nn[i] || e[i].size()) && !vis[i]) return puts( 0 ), 0; n   int64_t slc = accumulate(nn, nn + n + 1, 0); n   ans = slc * (slc - 1); n   for (int i = 1; i <= n; ++i) { n     int no = nn[i]; n     nn[i] = 0; n     nn[i] = e[i].size(); n     ans += nn[i] * int64_t(nn[i] - 1); n     ans += slc * nn[i]; n   } n   cout << ans / 2; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, l; n   cin >> n >> l; n   vector<int> flags(n + 2); n   flags[0] = 0, flags[n + 1] = l; n   for (int i = 0; i < n; ++i) { n     cin >> flags[i + 1]; n   } n   vector<double> s(n + 2); n   vector<double> e(n + 2); n   s[0] = 0; n   e[n + 1] = 0; n   for (int i = 1; i < n + 2; ++i) { n     s[i] = s[i - 1] + (flags[i] - flags[i - 1]) * 1.0 / i; n     e[n - i + 1] = n         e[n - i + 2] + (flags[n - i + 2] - flags[n - i + 1]) * 1.0 / (i); n   } n   for (int i = 0; i < n + 2; ++i) { n     if (abs(s[i] - e[i]) < 1e-6) { n       cout << s[i] <<   n ; n       return; n     } else if (s[i] > e[i]) { n       double sspd = i; n       double espd = n - i + 2; n       double dist = flags[i] - flags[i - 1]; n       double result = espd * e[i] + sspd * s[i - 1] + dist; n       result = result / (sspd + espd); n       cout << result <<   n ; n       return; n     } n   } n } n int main() { n   cout << setprecision(9); n   cout << fixed; n   int tests; n   cin >> tests; n   while (tests) { n     solve(); n     tests--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string str; n int ss; n int par[10001]; n int ans[2][10001]; n int root(int x) { n   if (par[x] != x) par[x] = root(par[x]); n   return par[x]; n } n map<string, int> m; n int main() { n   while (getline(cin, str)) { n     char ty = str[0]; n     int k = 2; n     int z = 0; n     int p = 0; n     string tt =   ; n     tt += ty; n     tt +=    ; n     while (k < str.size()) { n       tt +=     ; n       string t =   ; n       k++; n       while (k < str.size() && str[k] !=     ) t += str[k], k++; n       tt += t; n       z++; n       if (k < str.size()) { n         if (!m.count(tt)) { n           m[tt] = ++ss; n           par[m[tt]] = m[tt]; n         } n         if (z > 1) { n           int xx = root(m[tt]); n           int yy = root(p); n           if (xx != yy) { n             par[xx] = yy; n             ans[0][yy]++; n           } n         } n         p = m[tt]; n       } else { n         ans[1][root(p)]++; n       } n     } n   } n   int mx1 = 0, mx2 = 0; n   for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) { n     mx1 = max(mx1, ans[0][it->second]); n     mx2 = max(mx2, ans[1][it->second]); n   } n   cout << mx1 <<     << mx2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long maxn = 2e6 + 10; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string v; n   cin >> v; n   for (long i = 0; i < v.size(); i++) { n     if (v[i] !=  1  && v[i] !=  4 ) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   if (v[0] !=  1 ) { n     cout <<  NO  << endl; n     return 0; n   } n   unsigned long long p = 0; n   for (long i = 1; i < v.size(); i++) { n     if (v[i] ==  4  && v[i - 1] ==  4 ) { n       if (v[i + 1] ==  4 ) { n         cout <<  NO  << endl; n         p = 1; n         return 0; n       } n     } n   } n   if (p == 0) { n     cout <<  YES  << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100; n int n, m; n int was[MAXN]; n vector<int> g[MAXN]; n bool was2[3][MAXN]; n bool dfs(int v, int col = 1) { n   was[v] = col; n   for (int i = 0; i < (int)g[v].size(); ++i) { n     if (!was[g[v][i]]) { n       if (dfs(g[v][i], 3 - col)) { n         return true; n       } n     } else if (was[v] == was[g[v][i]]) n       return true; n   } n   return false; n } n int dfs2(int v, int col) { n   was2[col][v] = true; n   int res = was[v] == col; n   for (int i = 0; i < (int)g[v].size(); ++i) { n     if (!was2[col][g[v][i]]) { n       res += dfs2(g[v][i], col); n     } n   } n   return res; n } n unsigned long long C(int n, int k) { n   if (k > n) return 0; n   unsigned long long res = 1; n   for (int i = 0; i < k; ++i) res *= n - i; n   for (int i = 2; i <= k; ++i) res /= i; n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0, a, b; i < m; ++i) { n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 1; i <= n; ++i) { n     if (!was[i] && dfs(i)) { n       puts( 0 1 ); n       return 0; n     } n   } n   unsigned long long one = 0; n   for (int i = 1; i <= n; ++i) { n     if (!was2[1][i]) { n       one += C(dfs2(i, 1), 2); n       one += C(dfs2(i, 2), 2); n     } n   } n   if (one) { n     cout << 1 <<     << one << endl; n     return 0; n   } n   if (m) { n     cout << 2 <<     << m * 1ll * (n - 2) <<   n ; n     return 0; n   } n   cout << 3 <<     << C(n, 3) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int longpre(string y) { n   string s = y +  ? ; n   reverse(y.begin(), y.end()); n   s += y; n   int n = s.length(); n   int lps[n]; n   for (int i = 0; i < n; i++) lps[i] = 0; n   int i = 0; n   int j = 1; n   while (j < n) { n     if (s[i] == s[j]) { n       lps[j] = i + 1; n       i++; n       j++; n     } else { n       if (i == 0) { n         lps[j] = 0; n         j++; n       } else { n         i = lps[i - 1]; n       } n     } n   } n   return lps[n - 1]; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int k = 0; n     int i = 0; n     int n = s.length(); n     int j = n - 1; n     while (i < j) { n       if (s[i] == s[j]) n         k++; n       else n         break; n       i++; n       j--; n     } n     string t =   ; n     if (k > 0) { n       t += s.substr(0, k); n     } n     string mid = s.substr(k, n - 2 * k); n     if (mid.length() > 0) { n       int prefix = longpre(mid); n       reverse(mid.begin(), mid.end()); n       int suffix = longpre(mid); n       if (prefix > suffix) { n         reverse(mid.begin(), mid.end()); n         t += mid.substr(0, prefix); n       } else { n         t += mid.substr(0, suffix); n       } n     } n     t += s.substr(n - k, k); n     cout << t << endl; n   } n } n


#include<bits/stdc++.h> n #define fort(i,a,b) for(long long i=a;i<=b;i++) n #define forn(i,a,b) for(long long i=a;i>=b;i--) n #define rep(i,a,b) for(long long i=a;i<b;i++) n #define x first n #define y second n #define pb push_back n #define MP make_pair n using namespace std; n const long long  N=2e5+5; n long long  s1,s2,d,c,test,n,l[N][20],f[N],kq[N],h[N],cnt[N],kt[N],timer,tin[N],tout[N]; n vector<long long > a[N]; n void dfs(long long  u,long long  pa) n { n     tin[u]=++timer; n     h[u]=h[pa]+1; n     l[u][0]=pa; n     cnt[u]++; n     for(long long  v:a[u]) n         if (v!=pa) n     { n         dfs(v,u); n         f[u]+=cnt[u]*cnt[v]; n         cnt[u]+=cnt[v]; n     } n     f[u]+=(n-cnt[u])*cnt[u]; n     tout[u]=++timer; n } n void build_lca() n { n     fort(i,1,log2(n)) n      fort(j,1,n) n       l[j][i]=l[l[j][i-1]][i-1]; n } n long long  lca(long long  u,long long  v) n { n     if (h[u]<h[v]) swap(u,v); n     forn(i,log2(h[u]),0) n      if (h[l[u][i]]>=h[v]) u=l[u][i]; n     if (u==v) return u; n     forn(i,log2(h[u]),0) n      if (l[u][i]!=l[v][i]) n      { n          u=l[u][i]; n          v=l[v][i]; n      } n     return l[u][0]; n } n void chinh(long long  x) n { n     if (kt[x]) return; n     kt[x]=1; n     chinh(l[x][0]); n } n bool con(long v,long u) n { n     return tin[u]<tin[v]&&tout[v]<tout[u]; n } n int main() n { n     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n     cin>>test; n     while (test--) n     { n         cin>>n; n         fort(i,1,n-1) n         { n             long long  u,v; n             cin>>u>>v; n             u++;v++; n             a[u].pb(v); n             a[v].pb(u); n         } n         dfs(1,0); n         build_lca(); n         kq[1]=n*(n-1)/2-f[1]; n         kq[n+1]=1; n         fort(i,1,n) n          if (a[i].size()>2) kq[n+1]=0; n         d=1;c=1;s1=0;s2=0; n         kt[1]=1; n         fort(i,2,n) n          if (!kt[i]) n          { n              if (d!=c) n              { n                 if (c==1) n                 { n                     if (lca(i,d)!=1&&lca(i,d)!=d&&kt[lca(i,d)]) n                     { n                         kq[i]=s2*cnt[d]; n                         break; n                     } n                 } else n                 { n                     if (lca(i,d)!=d&&lca(i,c)!=c) n                     { n                         kq[i]=cnt[d]*cnt[c]; n                         break; n                     } n                 } n                 if (con(i,d)) n                     { n  n                         chinh(i); n                         s1=cnt[d]-cnt[i]; n                         kq[i]=s1*s2; n                         d=i; n                         s1=cnt[d]; n                     } else n                 if (con(i,c)) n                     { n                         chinh(i); n                         if (c==1) s2-=cnt[i];else n                             s2=cnt[c]-cnt[i]; n                         kq[i]=s1*s2; n                         c=i; n                         s2=cnt[c]; n                     } n             } else n             { n                 chinh(i); n                 for(long long  v:a[1]) n                     if (!kt[v]) s2+=cnt[v]; n                 s2++;s1=cnt[i]; n                 kq[i]=f[1]-cnt[i]*s2; n                 d=i; n             } n         } n         fort(i,1,n+1) cout<<kq[i]<<   ;cout<<  n ; n         fort(i,1,n) a[i].clear(); n         fort(i,1,n+1) kq[i]=0; n         fort(i,1,n) cnt[i]=0; n         fort(i,1,n) n          fort(j,0,log2(n)) l[i][j]=0; n         fort(i,1,n) kt[i]=0; n         fort(i,1,n) f[i]=0; n         timer=0; n     } n } n


#include <bits/stdc++.h> n template <typename T> n void MACRO_VAR_Scan(T& t) { n   std::cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   std::cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n template <class T> n inline T CHMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <class T> n inline T CHMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n template <class T> n std::vector<std::vector<T>> VV(int n, int m, T init = T()) { n   return std::vector<std::vector<T>>(n, std::vector<T>(m, init)); n } n template <typename S, typename T> n std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using PAIR = std::pair<int, int>; n using PAIRLL = std::pair<ll, ll>; n constexpr int INFINT = 1 << 30; n constexpr int INFINT_LIM = (1LL << 31) - 1; n constexpr ll INFLL = 1LL << 60; n constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); n constexpr double EPS = 1e-10; n constexpr int MOD = 998244353; n constexpr double PI = 3.141592653589793238462643383279; n template <class T, size_t N> n void FILL(T (&a)[N], const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, size_t N, size_t M, class T> n void FILL(ARY (&a)[N][M], const T& val) { n   for (auto& b : a) FILL(b, val); n } n template <class T> n void FILL(std::vector<T>& a, const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, class T> n void FILL(std::vector<std::vector<ARY>>& a, const T& val) { n   for (auto& b : a) FILL(b, val); n } n bool g[20][20]; n bool h[20][20]; n bool dp[16][1 << 16][16]; n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   int n, m; n   MACRO_VAR_Scan(n, m); n   ; n   std::vector<std::vector<int>> a(n, std::vector<int>(m)); n   for (auto& R : a) n     for (auto& w : R) std::cin >> w; n   ; n   auto check = [&](int d) { n     FILL(g, true); n     FILL(h, true); n     FILL(dp, false); n     for (int i = 0; i < int(n); ++i) n       for (int j = 0; j < int(n); ++j) { n         for (int k = 0; k < int(m); ++k) { n           if (std::abs(a[i][k] - a[j][k]) < d) { n             g[i][j] = false; n             break; n           } n         } n         for (int k = 0; k < int(m - 1); ++k) { n           if (std::abs(a[i][k] - a[j][k + 1]) < d) { n             h[i][j] = false; n             break; n           } n         } n       } n     for (int root = 0; root < int(n); ++root) { n       dp[root][1 << root][root] = true; n       for (int mask = 0; mask < int(1 << n); ++mask) n         for (int i = 0; i < int(n); ++i) { n           if (!dp[root][mask][i]) continue; n           if (!(mask & 1 << i)) continue; n           for (int j = 0; j < int(n); ++j) { n             if (!g[i][j]) continue; n             if (mask & 1 << j) continue; n             dp[root][mask | 1 << j][j] = true; n           } n         } n       for (int i = 0; i < int(n); ++i) n         if (n == 1 || i != root) { n           if (dp[root][(1 << n) - 1][i] && h[i][root]) return true; n         } n     } n     return false; n   }; n   int ng = 1000000009, ok = 0; n   while (ng - ok > 1) { n     int me = (ok + ng) / 2; n     if (check(me)) n       ok = me; n     else n       ng = me; n   } n   std::cout << (ok); n   std::cout <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long int r = power(a, b / 2) % 1000000007; n   if (b % 2 == 0) n     return (r * r) % 1000000007; n   else n     return (((r * r) % 1000000007) * a) % 1000000007; n } n int prime[1000009]; n void sieve(int n) { n   prime[0] = prime[1] = 1; n   for (int i = 2; i <= sqrt(n); i++) { n     if (!prime[i]) { n       for (int j = 2 * i; j <= n; j++) { n         prime[j]++; n       } n     } n   } n } n bool comp(const pair<long long int, long long int> &a, n           const pair<long long int, long long int> &b) { n   return (a.second < b.second); n } n const int N = 5 * 1e5 + 9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   long long int a[n]; n   vector<pair<long long int, int> > v; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     v.push_back({a[i], i + 1}); n   } n   sort(v.begin(), v.end()); n   if (n == 2 || n == 3) { n     cout <<  1 n ; n     return 0; n   } n   map<long long int, long long int> mp; n   for (int i = 0; i < n - 1; i++) { n     mp[v[i + 1].first - v[i].first]++; n   } n   if (mp.size() > 3) { n     cout <<  -1 n ; n     return 0; n   } n   if (mp.size() == 1) { n     cout << v[0].second <<   n ; n     return 0; n   } n   if (n == 4) { n     if (v[1].first + v[3].first == 2 * v[2].first) { n       cout << v[0].second <<   n ; n       return 0; n     } n     if (v[0].first + v[3].first == 2 * v[2].first) { n       cout << v[1].second <<   n ; n       return 0; n     } n     if (v[0].first + v[3].first == 2 * v[1].first) { n       cout << v[2].second <<   n ; n       return 0; n     } n     if (v[0].first + v[2].first == 2 * v[1].first) { n       cout << v[3].second <<   n ; n       return 0; n     } n     cout <<  -1 n ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       mp[v[i + 1].first - v[i].first]--; n       if (mp[v[i + 1].first - v[i].first] == 0) { n         mp.erase(v[i + 1].first - v[i].first); n       } n       if (mp.size() == 1) { n         cout << v[0].second <<   n ; n         return 0; n       } n       mp[v[i + 1].first - v[i].first]++; n     } else if (i == n - 1) { n       mp[v[i].first - v[i - 1].first]--; n       if (mp[v[i].first - v[i - 1].first] == 0) { n         mp.erase(v[i].first - v[i - 1].first); n       } n       if (mp.size() == 1) { n         cout << v[n - 1].second <<   n ; n         return 0; n       } n       mp[v[i].first - v[i - 1].first]++; n     } else { n       mp[v[i + 1].first - v[i].first]--; n       mp[v[i].first - v[i - 1].first]--; n       if (mp[v[i + 1].first - v[i].first] == 0) { n         mp.erase(v[i + 1].first - v[i].first); n       } n       if (mp[v[i].first - v[i - 1].first] == 0) { n         mp.erase(v[i].first - v[i - 1].first); n       } n       mp[v[i + 1].first - v[i - 1].first]++; n       if (mp.size() == 1) { n         cout << v[i].second <<   n ; n         return 0; n       } n       mp[v[i + 1].first - v[i - 1].first]--; n       if (mp[v[i + 1].first - v[i - 1].first] == 0) { n         mp.erase(v[i + 1].first - v[i - 1].first); n       } n       mp[v[i + 1].first - v[i].first]++; n       mp[v[i].first - v[i - 1].first]++; n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int Q; n   cin >> Q; n   while (Q-- > 0) { n     int n; n     cin >> n; n     vector<int> dp(2001, inf); n     dp[0] = 0; n     for (int i = 0; i < n; ++i) { n       int a; n       cin >> a; n       vector<int> aux(dp); n       fill(dp.begin(), dp.end(), inf); n       for (int j = 0; j < 2001; ++j) { n         if (aux[j] != inf) { n           dp[max(0, j - a)] = min(dp[max(0, j - a)], aux[j] + a); n           if (j + a < 2001) { n             dp[j + a] = min(dp[j + a], max(0, aux[j] - a)); n           } n         } n       } n     } n     int res = inf; n     for (int i = 0; i <= 2000; ++i) { n       if (dp[i] != inf) { n         res = min(res, i + dp[i]); n       } n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long x[100007], y[100007], xx, yy, m, p, q; n long long ans; n int get(int p, int q) { n   while ((x[q] - x[p]) * (yy - y[p]) - (y[q] - y[p]) * (xx - x[p]) <= 0) n     q = q % n + 1; n   q = (q + n - 2) % n + 1; n   return q; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %I64d%I64d , &x[i], &y[i]); n   for (cin >> m; m; m--) { n     cin >> xx >> yy; n     int now = 0; n     ans = 0; n     for (int i = 1; i <= n; i++) n       if ((xx - x[i]) * (y[i % n + 1] - y[i]) - n               (yy - y[i]) * (x[i % n + 1] - x[i]) < n           0) n         now++; n     if (now != 0 && now != n) { n       cout << 0 << endl; n       continue; n     } n     for (p = 1, q = get(1, 1); p <= n; p++, q = get(p, q)) { n       int r = ((q - p) % n + n) % n; n       ans += ((long long)r) * (r - 1) / 2; n     } n     ans = ((long long)n * (n - 1) * (n - 2) / 6 - ans); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100010, Block = 500, TB = 300; n struct query { n   int l, r, id; n   bool operator<(const query &a) const { n     if (l / TB != a.l / TB) return l / TB < a.l / TB; n     return r < a.r; n   } n } q[MaxN]; n long long ans[MaxN]; n int cnt[MaxN], num[MaxN], a[MaxN]; n vector<int> large; n bool vis[MaxN]; n int n, m; n int tmp[MaxN]; n long long get_ans() { n   for (int i = 1; i <= Block * 2; ++i) tmp[i] = num[i]; n   long long ret = 0; n   int x = 1; n   for (int i = 1; i <= Block; ++i) { n     if (!tmp[i]) continue; n     if (tmp[i] >= 2) { n       ret += (tmp[i] / 2) * i * 2; n       tmp[i * 2] += tmp[i] / 2; n       tmp[i] &= 1; n     } n     if (tmp[i] & 1) { n       x = max(i + 1, x); n       while (tmp[x] == 0 && i + x <= Block * 2) ++x; n       if (i + x > Block * 2) break; n       ret += i + x; n       ++tmp[i + x]; n       --tmp[x]; n     } n     tmp[i] = 0; n   } n   priority_queue<int, vector<int>, greater<int> > Q; n   for (int i = 1; i <= Block * 2; ++i) n     for (int j = 1; j <= tmp[i]; ++j) Q.push(i); n   for (auto i : large) n     if (cnt[i] > Block * 2) Q.push(cnt[i]); n   while (Q.size() >= 2) { n     int x = Q.top(); n     Q.pop(); n     int y = Q.top(); n     Q.pop(); n     ret += x + y; n     Q.push(x + y); n   } n   return ret; n } n void reverse(int x) { n   int u = a[x]; n   if (vis[x]) { n     --num[cnt[u]]; n     --cnt[u]; n     ++num[cnt[u]]; n   } else { n     --num[cnt[u]]; n     ++cnt[u]; n     ++num[cnt[u]]; n   } n   vis[x] ^= 1; n } n void move(int &x, int p) { n   while (x > p) { n     reverse(x); n     --x; n   } n   while (x < p) { n     ++x; n     reverse(x); n   } n } n int main() { n   scanf( %d , &n); n   int mx = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     ++cnt[a[i]]; n     mx = max(mx, a[i]); n   } n   for (int i = 1; i <= mx; ++i) n     if (cnt[i] > Block * 2) large.push_back(i); n   memset(cnt, 0, sizeof(cnt)); n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].id = i; n   } n   sort(q + 1, q + m + 1); n   int l = 0, r = 0; n   for (int i = 1; i <= m; ++i) { n     move(l, q[i].l - 1); n     move(r, q[i].r); n     if (q[i].l == q[i - 1].l && q[i].r == q[i - 1].r) n       ans[q[i].id] = ans[q[i - 1].id]; n     else n       ans[q[i].id] = get_ans(); n   } n   for (int i = 1; i <= m; ++i) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, size[55]; n double f[55][55], fac[55]; n int cnt, head[55], to[110], nxt[110]; n void add(int x, int y) { n   cnt++; n   to[cnt] = y; n   nxt[cnt] = head[x]; n   head[x] = cnt; n } n double c(int n, int m) { n   if (m < 0 || m > n) { n     return 0.0; n   } else { n     return fac[n] / fac[m] / fac[n - m]; n   } n } n void dfs(int x, int fa) { n   size[x] = 1; n   f[x][0] = 1.0; n   double tmp[55]; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == fa) { n       continue; n     } n     dfs(y, x); n     for (int j = 0; j < size[x] + size[y]; j++) { n       tmp[j] = 0.0; n     } n     for (int j = 0; j < size[y]; j++) { n       for (int k = 0; k < size[x]; k++) { n         for (int h = j + k + 1; h <= size[y] + k; h++) { n           tmp[h] += c(h, k) * c(size[x] + size[y] - 1 - h, size[x] - 1 - k) * n                     f[x][k] * f[y][j] / 2.0; n         } n         tmp[j + k] += 1.0 * (size[y] - j) * c(j + k, j) * n                       c(size[x] + size[y] - 1 - j - k, size[x] - 1 - k) * n                       f[x][k] * f[y][j]; n       } n     } n     size[x] += size[y]; n     for (int j = 0; j < size[x]; j++) { n       f[x][j] = tmp[j]; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   fac[0] = 1.0; n   for (int i = 1; i <= n; i++) { n     fac[i] = fac[i - 1] * i; n   } n   for (int i = 1; i <= n; i++) { n     dfs(i, 0); n     printf( %.10lf n , f[i][n - 1] / fac[n - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int SIZE = 1e6 + 5; n const long long INF = 1LL << 58; n const double eps = 1e-10; n char d[2009][2009]; n char a[2009][2009]; n int dy[4] = {-1, 0, 1, 0}; n int dx[4] = {0, 1, 0, -1}; n int dy2[8] = {-2, -1, 0, 1, 2, 1, 0, -1}; n int dx2[8] = {0, 1, 2, 1, 0, -1, -2, -1}; n char v2[5] =  ^>v< ; n char v[5] =  v<^> ; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (n); ++i) { n     scanf( %s , (d[i])); n     for (int j = 0; j < (m); ++j) { n       if (d[i][j] ==  * ) n         a[i][j] =  * ; n       else n         a[i][j] =  . ; n     } n   } n   queue<pair<int, int> > q; n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) { n       if (d[i][j] ==  * ) continue; n       q.push(make_pair(i, j)); n     } n   } n   while (((int)(q).size())) { n     pair<int, int> cur = q.front(); n     q.pop(); n     int y = cur.first, x = cur.second; n     if (a[y][x] !=  . ) continue; n     int ck = -1; n     for (int k = 0; k < (4); ++k) { n       int ny = y + dy[k], nx = x + dx[k]; n       if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue; n       if (a[ny][nx] ==  . ) { n         if (ck != -1) { n           ck = -1; n           break; n         } else n           ck = k; n       } n     } n     if (ck == -1) continue; n     int ny = y + dy[ck], nx = x + dx[ck]; n     a[y][x] = v[ck]; n     a[ny][nx] = v2[ck]; n     for (int k = 0; k < (8); ++k) { n       int ny = y + dy2[k], nx = x + dx2[k]; n       if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue; n       q.push(make_pair(ny, nx)); n     } n   } n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) { n       if (a[i][j] ==  . ) { n         puts( Not unique ); n         return 0; n       } n     } n   } n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) { n       printf( %c , a[i][j]); n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > hop, top; n int h, t, R; n struct State { n   int h, t; n   int c; n   State() {} n   State(int h, int t, int c) : h(h), t(t), c(c) {} n   bool operator<(const State o) const { n     if (h != o.h) return h < o.h; n     return t < o.t; n   } n }; n vector<vector<int> > G; n pair<int, int> BFS(State init) { n   queue<State> q; n   q.push(init); n   map<State, int> vis; n   int ii = 0; n   vis[init] = 0; n   G.resize(1); n   while (q.size()) { n     State& s = q.front(); n     State ns; n     for (int i = 0; i < min((int)hop.size(), s.h); i++) { n       ns = State(s.h - i - 1 + hop[i].first, s.t + hop[i].second, s.c + 1); n       if (ns.h == ns.t && ns.h == 0) { n         return make_pair(ns.c, 1); n       } n       if (ns.h + ns.t > R) { n         if (!vis.count(ns)) { n           vis[ns] = ++ii; n           G.resize(ii + 1); n           G[vis[s]].push_back(ii); n         } n         continue; n       } n       if (!vis.count(ns)) { n         q.push(ns); n         vis[ns] = ++ii; n         G.resize(ii + 1); n         G[vis[s]].push_back(ii); n       } else { n         G[vis[s]].push_back(vis[ns]); n       } n     } n     for (int i = 0; i < min((int)top.size(), s.t); i++) { n       ns = State(s.h + top[i].first, s.t + top[i].second - i - 1, s.c + 1); n       if (ns.h == ns.t && ns.h == 0) { n         return make_pair(ns.c, 1); n       } n       if (ns.h + ns.t > R) { n         if (!vis.count(ns)) { n           vis[ns] = ++ii; n           G.resize(ii + 1); n           G[vis[s]].push_back(ii); n         } n         continue; n       } n       if (!vis.count(ns)) { n         q.push(ns); n         vis[ns] = ++ii; n         G.resize(ii + 1); n         G[vis[s]].push_back(ii); n       } else { n         G[vis[s]].push_back(vis[ns]); n       } n     } n     q.pop(); n   } n   return make_pair(0, -1); n } n enum colors { WHITE, GREY, BLACK }; n vector<int> color; n bool hasLoop; n void DFS(int node) { n   if (hasLoop) return; n   color[node] = GREY; n   for (int i = 0; i < G[node].size(); i++) { n     if (hasLoop) return; n     if (color[G[node][i]] == WHITE) { n       DFS(G[node][i]); n     } else if (color[G[node][i]] == GREY) { n       hasLoop = 1; n       return; n     } n   } n   color[node] = BLACK; n } n void Input() { n   cin >> h >> t >> R; n   int hs, ts; n   cin >> hs; n   hop.resize(hs); n   for (int i = 0; i < hs; i++) { n     cin >> hop[i].first >> hop[i].second; n   } n   cin >> ts; n   top.resize(ts); n   for (int i = 0; i < ts; i++) { n     cin >> top[i].first >> top[i].second; n   } n } n vector<int> dp; n int recurse(int node) { n   if (dp[node] != -1) return dp[node]; n   int mx = 0; n   for (int i = 0; i < G[node].size(); i++) { n     mx = max(mx, recurse(G[node][i])); n   } n   return dp[node] = mx + 1; n } n void Solve() { n   Input(); n   State init(h, t, 0); n   auto ret = BFS(init); n   if (ret.second == 1) { n     cout <<  Ivan  << endl; n     cout << ret.first; n     return; n   } n   color.resize(G.size()); n   DFS(0); n   if (hasLoop) { n     cout <<  Draw ; n     return; n   } n   dp.assign(G.size(), -1); n   cout <<  Zmey  << endl << recurse(0) - 1; n } n int main() { Solve(); } n


#include <bits/stdc++.h> n using namespace std; n int N; n int K[1010]; n pair<int, int> nex[1010][2520]; n int P, Q; n int num[1010][2520]; n int vis[1010][2520]; n class SCC { n  public: n   static const int MV = 1010 * 2520; n   vector<vector<int> > SC; n   int NV, GR[MV]; n  n  private: n   vector<int> E[MV], RE[MV], NUM; n   int vis[MV]; n  n  public: n   void init(int NV) { n     this->NV = NV; n     for (int i = 0; i < MV; i++) { n       E[i].clear(); n       RE[i].clear(); n     } n   } n   void add_edge(int x, int y) { n     E[x].push_back(y); n     RE[y].push_back(x); n   } n   void dfs(int cu) { n     vis[cu] = 1; n     for (int i = 0; i < E[cu].size(); i++) n       if (!vis[E[cu][i]]) dfs(E[cu][i]); n     NUM.push_back(cu); n   } n   void revdfs(int cu, int ind) { n     int i; n     vis[cu] = 1; n     GR[cu] = ind; n     SC[ind].push_back(cu); n     for (i = 0; i < (RE[cu].size()); i++) n       if (!vis[RE[cu][i]]) revdfs(RE[cu][i], ind); n   } n   void scc() { n     int c = 0; n     SC.clear(); n     SC.resize(MV); n     NUM.clear(); n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < NV; i++) n       if (!vis[i]) dfs(i); n     memset(vis, 0, sizeof(vis)); n     for (int i = NUM.size() - 1; i >= 0; i--) n       if (!vis[NUM[i]]) { n         SC[c].clear(); n         revdfs(NUM[i], c); n         sort(SC[c].begin(), SC[c].end()); n         c++; n       } n     SC.resize(c); n   } n }; n int dfs(int v, int val, vector<int>& V) { n   if (vis[v][val] == 2) { n     set<int> S; n     int i; n     for (i = V.size() - 1; i >= 0; i--) { n       S.insert(V[i] / 2520); n       if (V[i] == v * 2520 + val) break; n     } n     num[v][val] = S.size(); n   } else if (vis[v][val] == 0) { n     vis[v][val] = 2; n     V.push_back(v * 2520 + val); n     num[v][val] = dfs(nex[v][val].first, nex[v][val].second, V); n     V.pop_back(); n   } n   vis[v][val] = 1; n   return num[v][val]; n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N; n   for (i = 0; i < (N); i++) { n     cin >> K[i]; n     K[i] = (K[i] % 2520 + 2520) % 2520; n   } n   for (i = 0; i < (N); i++) { n     vector<int> E; n     cin >> x; n     E.resize(x); n     for (j = 0; j < (x); j++) cin >> E[j]; n     for (j = 0; j < (2520); j++) { n       nex[i][j].first = E[(j + K[i]) % x] - 1; n       nex[i][j].second = (j + K[i]) % 2520; n     } n   } n   for (i = 0; i < (N); i++) n     for (j = 0; j < (2520); j++) n       if (vis[i][j] == 0) { n         vector<int> V; n         dfs(i, j, V); n       } n   cin >> Q; n   while (Q--) { n     cin >> x >> y; n     x--; n     y = (y % 2520 + 2520) % 2520; n     cout << num[x][y] << endl; n   } n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n void ingraph(vector<long long> graph[], long long m) { n   long long x, y; n   for (long long i = 0; i < m; i++) { n     cin >> x >> y; n     x--, y--; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long modmulti(long long a, long long b) { n   return ((a % mod) * 1ll * (b % mod)) % mod; n } n long long modadd(long long a, long long b) { n   long long asdfgh = ((a % mod) + (b % mod) + mod + mod) % mod; n   asdfgh = (asdfgh + mod) % mod; n   return asdfgh; n } n long long modpower(long long a, long long n) { n   if (n == 0) return 1; n   if (n == 1) return a % mod; n   long long b = modpower(a, n / 2); n   b = modmulti(b, b); n   if (n % 2 == 0) return b; n   return modmulti(a, b); n } n long long modinv(long long a) { return modpower(a, mod - 2); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long sum = 0; n     vector<long long> vec; n     for (long long i = 1; i <= n; i = i * 2) { n       vec.push_back(i); n       n = n - i; n     } n     if (n > 0) { n       vec.push_back(n); n     } n     sort(vec.begin(), vec.end()); n     cout << (long long)(vec).size() - 1 <<   n ; n     for (long long i = 1; i < (long long)(vec).size(); i++) n       cout << vec[i] - vec[i - 1] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int n = s.length(); n     if (n < 3) { n       cout << 0 << endl; n       continue; n     } n     if (n == 3) { n       if (s ==  101  || s ==  010 ) n         cout << 1 << endl; n       else n         cout << 0 << endl; n       continue; n     } n     long long int zero = 0, one = 0; n     for (long long int i = 0; i < n; i++) { n       if (s[i] ==  1 ) n         one++; n       else n         zero++; n     } n     if (one == n || zero == n) { n       cout << 0 << endl; n       continue; n     } n     long long int done = 0, dzero = 0; n     long long int min1 = INT_MAX, min2 = INT_MAX; n     for (long long int i = 0; i < n; i++) { n       if (s[i] ==  1 ) n         done++; n       else n         dzero++; n       min1 = min(min1, (dzero + one - done)); n       min2 = min(min2, (done + zero - dzero)); n     } n     cout << min(min1, min2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, neg = 0; n char s[1009]; n int main() { n   vector<int> ans; n   scanf( %[^=]=%d , s, &n); n   ans.push_back(n); n   for (int i = 1; s[i]; i++) { n     if (s[i] ==    ) continue; n     if (s[i] ==  - ) n       neg = 1; n     else if (s[i] ==  + ) n       neg = 0; n     else if (s[i] ==  ?  && !neg) { n       ans[0] -= 1; n       ans.push_back(1); n     } else if (s[i] ==  ? ) { n       int c = 0; n       ans[0] += 1; n       ans.push_back(-1); n     } n   } n   int c = 1; n   while (ans[0] > n && c < ans.size()) { n     if (ans[c] > 0 && ans[c] < n) { n       ans[0]--; n       ans[c]++; n     } else n       c++; n   } n   c = 1; n   while (ans[0] <= 0 && c < ans.size()) { n     if (ans[c] < 0 && abs(ans[c]) < n) { n       ans[0]++; n       ans[c]--; n     } else n       c++; n   } n   if (ans[0] <= 0 || ans[0] > n) { n     printf( Impossible n ); n     return 0; n   } n   printf( Possible n ); n   printf( %d , ans[0]); n   for (int i = 1; i < ans.size(); i++) { n     if (ans[i] > 0) n       printf(  + %d , ans[i]); n     else n       printf(  - %d , abs(ans[i])); n   } n   printf(  = %d n , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n int main() { n   int n; n   cin >> n; n   int ansx = 0, ansy = 0; n   for (int i = 1; i < n; i++) { n     int j = n - i; n     if (j <= i) break; n     if (gcd(i, j) != 1) continue; n     ansx = i; n     ansy = j; n   } n   cout << ansx <<     << ansy << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, N = 1005, MOD = 100000007; n const double eps = 1e-6; n int a[15], m, ans[N], ct; n char b; n int main() { n   ct = 0; n   for (int i = 1; i <= 10; i++) { n     scanf( %c , &b); n     if (b ==  1 ) { n       a[ct++] = i; n     } n   } n   scanf( %d , &m); n   bool flag = 0; n   int s[2], tmp; n   int i, j, tail; n   for (int k = 0; k < ct; k++) { n     s[0] = s[1] = 0; n     s[0] = ans[0] = a[k]; n     tail = k; n     tmp = 1; n     for (i = 1; i < m; i++) { n       for (j = 0; j < ct; j++) { n         if (j == tail) continue; n         if (s[tmp] + a[j] > s[tmp ^ 1]) { n           s[tmp] += a[j]; n           ans[i] = a[j]; n           tail = j; n           tmp ^= 1; n           break; n         } n       } n       if (j == ct) { n         flag = 0; n         break; n       } n     } n     if (i == m) { n       flag = 1; n       break; n     } n   } n   if (flag) { n     printf( YES n ); n     for (int k = 0; k < m; k++) { n       printf( %d  , ans[k]); n     } n     puts(  ); n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e17; n long long n, k, niv[200005]; n vector<long long> muchii[200005]; n bool isleaf[200005], used[200005]; n long long red, blue, par[200005]; n vector<long long> line; n long long timp, in[200005], out[200005]; n struct date { n   long long minim, best; n } arb[1400005]; n long long lazy[1400005]; n void dfs(int nod) { n   if (nod == 1) n     niv[nod] = 1; n   else if (muchii[nod].size() == 1) n     isleaf[nod] = 1; n   timp++; n   line.push_back(nod); n   in[nod] = timp; n   for (auto i : muchii[nod]) n     if (i != par[nod]) { n       niv[i] = niv[nod] + 1; n       par[i] = nod; n       dfs(i); n     } n   out[nod] = timp; n } n void prop(int nod) { n   long long val = lazy[nod]; n   arb[nod * 2].minim += val; n   arb[nod * 2 + 1].minim += val; n   lazy[nod * 2] += val; n   lazy[nod * 2 + 1] += val; n } n void update(int nod, int st, int dr, int a, int b, long long val) { n   if (lazy[nod] != 0 && st != dr) prop(nod); n   lazy[nod] = 0; n   if (st >= a && dr <= b) { n     arb[nod].minim += val; n     if (st == dr && isleaf[line[st]]) n       arb[nod].best = line[st]; n     else n       lazy[nod] += val; n     return; n   } n   int mij = (st + dr) / 2; n   if (a <= mij) update(nod * 2, st, mij, a, b, val); n   if (b > mij) update(nod * 2 + 1, mij + 1, dr, a, b, val); n   arb[nod].minim = arb[nod].best = 0; n   if (arb[nod * 2].best > 0 && arb[nod * 2].minim > arb[nod].minim) n     arb[nod] = arb[nod * 2]; n   if (arb[nod * 2 + 1].best > 0 && arb[nod * 2 + 1].minim > arb[nod].minim) n     arb[nod] = arb[nod * 2 + 1]; n } n int main() { n   cin >> n >> k; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     muchii[a].push_back(b); n     muchii[b].push_back(a); n   } n   line.push_back(0); n   dfs(1); n   for (int i = 1; i <= n; i++) update(1, 1, n, in[i], in[i], niv[i]); n   long long ans = -INF; n   blue = n; n   red = 0; n   while (red <= k) { n     long long val = red * (n - red); n     long long x = min(n / 2, blue); n     val -= x * (n - x); n     ans = max(ans, val); n     long long nod = arb[1].best; n     red++; n     while (nod > 0 && !used[nod]) { n       used[nod] = 1; n       update(1, 1, n, in[nod], out[nod], -1); n       blue--; n       nod = par[nod]; n     } n   } n   cout << ans; n   return 0; n } n