#include <bits/stdc++.h> n using namespace std; n int cc[100002]; n pair<int, int> in[100002]; n vector<pair<int, int> > adj[100002]; n int pos = 1; n int l[100002], r[100002], label[100002]; n long long d[100002]; n void dfs(int node, int par, long long depth = 0) { n   l[node] = pos; n   d[node] = depth; n   int ch, w; n   for (pair<int, int> edge : adj[node]) { n     if (edge.first == par) { n       continue; n     } n     dfs(edge.first, node, depth + edge.second); n   } n   label[pos] = node; n   r[node] = pos++; n } n long long pen[100002]; n void update(int ind, long long add) { n   while (ind < 100002) { n     pen[ind] += add; n     ind += ind & (-ind); n   } n } n long long query(int ind) { n   long long ret = 0; n   while (ind > 0) { n     ret = ret + pen[ind]; n     ind = ind & (ind - 1); n   } n   return ret; n } n pair<long long, int> seg[100002 * 4]; n long long lazy[100002 * 4]; n const long long inf = -4e18; n void init(int pos, int l, int r) { n   lazy[pos] = 0; n   if (l == r) { n     seg[pos] = {d[label[l]], l}; n     return; n   } n   int mid = (l + r) / 2; n   init(pos * 2, l, mid); n   init(pos * 2 + 1, mid + 1, r); n   seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]); n } n void push(int pos, int l, int r) { n   if (lazy[pos] != 0) { n     seg[pos].first += lazy[pos]; n     if (l != r) { n       lazy[pos * 2] += lazy[pos]; n       lazy[pos * 2 + 1] += lazy[pos]; n     } n     lazy[pos] = 0; n   } n } n void update(int pos, int l, int r, int ql, int qr, long long val) { n   push(pos, l, r); n   if (r < ql || qr < l) { n     return; n   } n   if (ql <= l && r <= qr) { n     lazy[pos] = val; n     push(pos, l, r); n     return; n   } n   int mid = (l + r) / 2; n   update(pos * 2, l, mid, ql, qr, val); n   update(pos * 2 + 1, mid + 1, r, ql, qr, val); n   seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]); n } n pair<long long, int> query(int pos, int l, int r, int ql, int qr) { n   push(pos, l, r); n   if (r < ql || qr < l) { n     return {inf, 0}; n   } n   if (ql <= l && r <= qr) { n     return seg[pos]; n   } n   int mid = (l + r) / 2; n   return max(query(pos * 2, l, mid, ql, qr), n              query(pos * 2 + 1, mid + 1, r, ql, qr)); n } n long long w[100002]; n long long dfs2(int node, int par) { n   priority_queue<long long> pq; n   for (pair<int, int> edge : adj[node]) { n     if (edge.first == par) { n       continue; n     } n     pq.push(dfs2(edge.first, node)); n   } n   long long ret = w[node]; n   if (node != 1) { n     if (pq.size() > 0) pq.pop(); n   } n   if (pq.size() > 0) { n     ret = max(ret, pq.top()); n   } n   return ret; n } n void solve() { n   int n; n   long long T; n   scanf( %d %lld  , &n, &T); n   long long rb = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d   , &cc[i]); n     rb += cc[i]; n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d  , &in[i].first); n     in[i].second = i; n   } n   sort(in + 1, in + n + 1); n   for (int i = 2; i <= n; ++i) { n     int p, l; n     scanf( %d %d  , &p, &l); n     l *= 2; n     adj[p].push_back({i, l}); n   } n   dfs(1, 0); n   init(1, 1, n); n   for (int i = 1; i <= n; ++i) { n     int node, co, p, x, y; n     node = in[i].second; n     co = cc[node]; n     p = r[node]; n     x = l[node], y = p; n     long long val = (long long)co * in[i].first; n     while (true) { n       pair<long long, int> q = query(1, 1, n, x, y); n       if (q.first + val >= T) { n         long long remCC = min((T - q.first) / in[i].first, (long long)co); n         w[label[q.second]] = query(q.second) + remCC; n         update(1, 1, n, q.second, q.second, inf); n       } else { n         break; n       } n     } n     update(1, 1, n, x, y, val); n     update(x, co); n     update(y + 1, -co); n   } n   while (true) { n     pair<long long, int> q = query(1, 1, n, 1, n); n     if (q.first < 0) { n       break; n     } n     w[label[q.second]] = query(q.second); n     update(1, 1, n, q.second, q.second, inf); n   } n   printf( %lld n , dfs2(1, 0)); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int pre[N], n, m; n int f(int x) { return pre[x] == x ? x : pre[x] = f(pre[x]); } n set<int> s[N]; n map<int, vector<int> > v[N]; n void join(int x, int y) { n   x = f(x); n   y = f(y); n   if (x == y) return; n   if (s[x].size() < s[y].size()) swap(x, y); n   set<int>::iterator it = s[y].begin(); n   for (; it != s[y].end(); ++it) s[x].insert(*it); n   pre[y] = x; n } n void add() { n   int x, y, z; n   scanf( %d %d %d , &x, &y, &z); n   s[f(x)].insert(y); n   s[f(y)].insert(x); n   v[x][z].push_back(y); n   join(y, v[x][z][0]); n   v[y][z].push_back(x); n   join(x, v[y][z][0]); n } n void query() { n   int x, y; n   scanf( %d %d , &x, &y); n   if (f(x) == f(y) || s[f(x)].count(y)) n     printf( Yes n ); n   else n     printf( No n ); n } n signed main() { n   char opt[10]; n   int c, q; n   scanf( %d %d %d %d , &n, &m, &c, &q); n   for (register int i = (1); i <= (n); ++i) pre[i] = i; n   for (register int i = (1); i <= (m); ++i) add(); n   for (register int i = (1); i <= (q); ++i) { n     scanf( %s , opt); n     if (opt[0] ==  + ) n       add(); n     else n       query(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long n; n   cin >> n; n   long long i; n   if (n == 1) n     cout << 1; n   else { n     long long sum = 1; n     for (i = 1;; i++) { n       sum += (1ll << i); n       if (sum >= n) break; n     } n     cout << i + 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long R = 1e3 + 69, INF = 2e9 + 6969, mod = 998244353; n long long n, a[R], dp[R], fact[R], rev[R]; n long long choose(long long N, long long K) { n   long long cur = (fact[N] * rev[K]) % mod; n   cur = (cur * rev[N - K]) % mod; n   return cur; n } n long long Pow(long long a, long long p) { n   long long w = 1; n   while (p) { n     if (p & 1) w = (w * a) % mod; n     a = (a * a) % mod; n     p >>= 1; n   } n   return w; n } n int main() { n   scanf( %lld , &n); n   for (int i = 0; i < n; ++i) scanf( %lld , &a[i]); n   fact[0] = 1; n   for (int i = 1; i <= n; ++i) fact[i] = (i * fact[i - 1]) % mod; n   rev[n] = Pow(fact[n], mod - 2); n   for (int i = n - 1; i >= 0; --i) rev[i] = (rev[i + 1] * (i + 1)) % mod; n   dp[n] = 1; n   for (int i = n - 1; i >= 0; --i) { n     if (a[i] <= 0 || a[i] + i >= n) continue; n     for (int j = i + a[i] + 1; j <= n; ++j) { n       dp[i] += (dp[j] * choose(j - i - 1, a[i])) % mod; n       dp[i] %= mod; n     } n   } n   long long sum = 0; n   for (int i = 0; i < n; ++i) sum = (sum + dp[i]) % mod; n   printf( %lld n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, h, a, b, k; n   cin >> n >> h >> a >> b >> k; n   for (long long int i = 0; i < k; i++) { n     long long int t1, f1, t2, f2; n     cin >> t1 >> f1 >> t2 >> f2; n     long long int ans; n     if (t1 == t2) { n       cout << abs(f1 - f2) << endl; n       continue; n     } n     if (a <= f1 && f1 <= b) { n       cout << abs(t1 - t2) + abs(f1 - f2) << endl; n       continue; n     } n     long long int temp; n     if (abs(f1 - a) <= abs(f1 - b)) { n       temp = a; n       ans = abs(f1 - a); n     } else { n       temp = b; n       ans = abs(f1 - b); n     } n     ans = ans + abs(t1 - t2) + abs(temp - f2); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using pii = pair<int, int>; n constexpr auto MOD = 1000000007LL; n constexpr auto LINF = (1LL << 60); n constexpr auto INF = 2147483647LL; n constexpr auto PI = 3.1415926535897932384626433; n constexpr auto EPS = 1E-9; n template <typename T1, typename T2> n ostream &operator<<(ostream &out, const pair<T1, T2> p) { n   out << p.first <<     << p.second; n   return out; n } n template <typename T1, typename T2> n istream &operator>>(istream &in, pair<T1, T2> &p) { n   in >> p.first >> p.second; n   return in; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (auto &x : v) in >> x; n   return in; n } n template <typename T> n ostream &operator<<(ostream &out, vector<T> v) { n   for (int i = 0; i < v.size(); i++) n     out << v[i] << (i + 1 == v.size() ?   n  :    ); n   out.flush(); n   return out; n } n struct Node { n   int l, r; n   int emp; n   ll end; n   Node() {} n } T[1000010 << 2]; n void maintain(int id) { n   ll ext = max(0LL, T[id << 1].end - T[id << 1 | 1].l); n   T[id].emp = T[id << 1].emp + max(0LL, T[id << 1 | 1].emp - ext) + n               max(0LL, T[id << 1 | 1].l - T[id << 1].end); n   T[id].end = max(0LL, ext - T[id << 1 | 1].emp) + T[id << 1 | 1].end; n   return; n } n void build(int id = 1, int l = 0, int r = 1e6) { n   T[id].l = l; n   T[id].r = r; n   T[id].emp = 0; n   T[id].end = l; n   if (l + 1 == r) { n     return; n   } n   build(id << 1, l, l + r >> 1); n   build(id << 1 | 1, l + r >> 1, r); n   return; n } n void add(int t, int d, int id = 1, int l = 0, int r = 1e6) { n   if (t >= r || l > t) return; n   if (l + 1 == r) { n     T[id].emp = 0; n     T[id].end = t + d; n     return; n   } n   add(t, d, id << 1, l, l + r >> 1); n   add(t, d, id << 1 | 1, l + r >> 1, r); n   maintain(id); n   return; n } n void rem(int t, int id = 1, int l = 0, int r = 1e6) { n   if (t >= r || l > t) return; n   if (l + 1 == r) { n     T[id].emp = 0; n     T[id].end = l; n     return; n   } n   rem(t, id << 1, l, l + r >> 1); n   rem(t, id << 1 | 1, l + r >> 1, r); n   maintain(id); n   return; n } n void ask(int t, ll &res, int id = 1, int l = 0, int r = 1e6) { n   if (l > t) return; n   if (t + 1 >= r) { n     ll ext = max(0LL, res - T[id].l); n     ext = max(0LL, ext - T[id].emp); n     res = T[id].end + ext; n     return; n   } n   ask(t, res, id << 1, l, l + r >> 1); n   ask(t, res, id << 1 | 1, l + r >> 1, r); n   return; n } n int records[300010]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   build(); n   string op; n   for (int i = 1; i <= n; i++) { n     cin >> op; n     if (op ==  ? ) { n       int t; n       ll res = -1; n       cin >> t; n       --t; n       ask(t, res); n       cout << max(0LL, res - t) << endl; n     } else if (op ==  + ) { n       int d; n       cin >> records[i] >> d; n       --records[i]; n       add(records[i], d); n     } else if (op ==  - ) { n       int id; n       cin >> id; n       rem(records[id]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000010, mod = 1e9 + 7; n int i, j, k, n, m, y, e; n long long ans = 1, a[maxn]; n int main() { n   a[0] = 1; n   a[1] = 1; n   cin >> n; n   for (i = 2; i <= n; i++) a[i] = (a[i - 2] * (i - 1) + a[i - 1]) % mod; n   for (i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (x == 1) n       y++; n     else n       e++; n   } n   for (i = n; e; i--, e--) { n     ans *= i; n     ans %= mod; n   } n   cout << ans * a[y] % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int quick_pow(int a, int b, int MOD) { n   a %= MOD; n   int res = 1; n   while (b) { n     if (b & 1) res = (res * a) % MOD; n     b /= 2; n     a = (a * a) % MOD; n   } n   return res; n } n const int maxn = 200000 + 10; n const int MOD = 1e9 + 7; n const int INF = 1e9; n int a[maxn], dp[maxn]; n priority_queue<int, vector<int>, greater<int> > que1, que2[maxn]; n typedef struct Segment { n   int sum[maxn * 4]; n   void pushUp(int k) { sum[k] = min(sum[k * 2], sum[k * 2 + 1]); } n   void build(int k, int l, int r) { n     sum[k] = INF; n     if (l == r) return; n     int m = (l + r) / 2; n     build(k * 2, l, m); n     build(k * 2 + 1, m + 1, r); n   } n   void update(int p, int v, int k, int l, int r) { n     if (l == r) { n       sum[k] = min(sum[k], v); n       return; n     } n     int m = (l + r) / 2; n     if (p <= m) n       update(p, v, k * 2, l, m); n     else n       update(p, v, k * 2 + 1, m + 1, r); n     pushUp(k); n   } n   int ask(int a, int b, int k, int l, int r) { n     if (a <= l && r <= b) return sum[k]; n     int m = (l + r) / 2, res = INF; n     if (a <= m) res = min(res, ask(a, b, k * 2, l, m)); n     if (b > m) res = min(res, ask(a, b, k * 2 + 1, m + 1, r)); n     return res; n   } n } Tree; n Tree tree; n int main() { n   int i, j, n, m; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   tree.build(1, 1, n); n   printf( 0 ); n   int now = 0; n   tree.update(a[1], a[1] + 1, 1, 1, n); n   now = -1; n   for (i = 2; i <= n; i++) { n     int res = now + i; n     res = min(res, tree.ask(i, n, 1, 1, n) - i); n     printf(  %d , res); n     tree.update(a[i], res + 1 + a[i], 1, 1, n); n     now = min(now, res - i); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pai = acos(-1); n const double eps = 1e-8; n const long long mod = 1e9 + 7; n const int MXN = 1e6 + 5; n vector<int> v[MXN]; n int c[MXN]; n int suf[MXN]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int le; n     scanf( %d , &le); n     while (le--) { n       int aa; n       scanf( %d , &aa); n       v[i].push_back(aa); n     } n     int mi = INT_MAX; n     for (auto j : v[i]) { n       if (j > mi) c[i] = 1; n       mi = min(mi, j); n     } n     sort(v[i].begin(), v[i].end()); n   } n   long long gg = 0; n   for (int i = 1; i <= n; i++) { n     if (c[i]) { n       gg++; n       continue; n     } n     suf[v[i].back()]++; n   } n   for (int i = 1e6 - 1; i >= 0; i--) suf[i] = suf[i] + suf[i + 1]; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (c[i]) n       ans = ans + (long long)n; n     else n       ans = ans + (long long)suf[v[i][0] + 1] + gg; n   } n   cout << ans <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n  n int main() n { n    ll int n,q; n    string s; n    cin>>n>>q; n    ll int cum[n+2]; n    cin>>s; n    cum[0]=s[0]- a +1; n    for(int i=1;i<n;i++) n    { n  n        cum[i]=cum[i-1]+s[i]- a +1; n    } n  n    for(ll int i=0;i<q;i++) n    {   ll int a,b,ans=0; n        cin>>a>>b; n  n        if (a==1) n        {cout<<cum[b-1]<<endl;} n        else n        {cout<<cum[b-1]-cum[a-2]<<endl;} n  n    } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n void __never(int a) { printf(  nOPS %d , a); } n const int INF = 1000000000; n bool was[6][26][6][11][16][21]; n int dp[6][26][6][11][16][21]; n struct Edge { n   int a, l, h; n }; n int n; n Edge graf[7][7]; n int mask[7]; n void dfs(int a, int b, int rest, int cost, vector<int>& v, n          vector<pair<vector<int>, int> >& res) { n   if (b == n + 1) { n     if (rest != 0) return; n     vector<int> v2(5); n     for (int(i) = (a); (i) <= (n - 2); ++(i)) v2[i] = v[i] + mask[i + 1]; n     res.push_back(make_pair(v2, cost)); n     return; n   } n   for (int(i) = (graf[a][b].l); (i) <= (graf[a][b].h); ++(i)) { n     if (i > rest) break; n     mask[b] = i; n     dfs(a, b + 1, rest - i, cost + ((i > 0) ? graf[a][b].a : 0) + i * i, v, n         res); n   } n } n int get(vector<int> v, int st) { n   if (was[st][v[0]][v[1]][v[2]][v[3]][v[4]]) n     return dp[st][v[0]][v[1]][v[2]][v[3]][v[4]]; n   was[st][v[0]][v[1]][v[2]][v[3]][v[4]] = true; n   int& res = dp[st][v[0]][v[1]][v[2]][v[3]][v[4]]; n   if (v[0] + v[1] + v[2] + v[3] + v[4] == 0 && st == n - 1) return res = 0; n   vector<pair<vector<int>, int> > vv; n   dfs(st + 1, st + 2, v[st], 0, v, vv); n   res = -INF; n   for (int(i) = (0); (i) <= ((int)((vv).size()) - 1); ++(i)) { n     int h = get(vv[i].first, st + 1); n     if (h > -INF) res = max(res, h + vv[i].second); n   } n   return res; n } n void init() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n void sol() { n   cin >> n; n   int a, b; n   for (int(i) = (1); (i) <= ((n * (n - 1)) / 2); ++(i)) { n     cin >> a >> b; n     cin >> graf[a][b].l >> graf[a][b].h >> graf[a][b].a; n   } n   bool zero = true; n   for (int(i) = (1); (i) <= (n); ++(i)) n     for (int(j) = (i + 1); (j) <= (n); ++(j)) zero &= graf[i][j].l == 0; n   if (zero) { n     printf( 0 0 ); n     return; n   } n   int hi = 0; n   for (int(i) = (2); (i) <= (n); ++(i)) hi += graf[1][i].h; n   vector<int> v(5); n   for (int(I) = (1); (I) <= (hi); ++(I)) { n     v[0] = I; n     if (get(v, 0) > 0) { n       printf( %d %d , I, get(v, 0)); n       return; n     } n   } n   printf( -1 -1 ); n } n void gen() { n   int n = 6; n   printf( %d , n); n   for (int(i) = (1); (i) <= (n); ++(i)) n     for (int(j) = (i + 1); (j) <= (n); ++(j)) n       printf(  n%d %d %d %d %d , i, j, rand() & 1, 5, rand() & 15); n } n int main() { n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n const int MOD = 1000000007; n const int MAX = 10000005; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   long long n; n   cin >> n; n   long long b, d; n   long long max = -1; n   long long y = 0; n   vector<pair<long long, long long> > vecx, vecy; n   for (long long i = 0; i < n; i++) { n     cin >> b >> d; n     vecx.push_back(make_pair(b, d)); n     vecy.push_back(make_pair(d, b)); n   } n   sort(vecx.begin(), vecx.end()); n   sort(vecy.begin(), vecy.end()); n   long long pointer1 = 0; n   long long pointer2 = 0; n   long long sum = 0; n   for (long long i = 0; i < 2 * vecx.size(); i++) { n     if (pointer1 == vecx.size() || pointer2 == vecx.size()) { n       goto ab; n     } n     if (vecx[pointer1].first < vecy[pointer2].first) { n       sum++; n       if (sum > max) { n         max = sum; n         y = vecx[pointer1].first; n       } n       pointer1++; n     } else if (vecx[pointer1].first > vecy[pointer2].first) { n       sum--; n       pointer2++; n     } else { n       sum--; n       pointer2++; n     } n   } n ab:; n   cout << y <<     << max <<    ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long long N = 2e5 + 10, alp = 26; n const long double pi = 3.14159265358979; n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<vector<long long> > g1(n), rg1(n), g2(n), rg2(n); n   for (long long i = 0; i < m; ++i) { n     long long u, v, t; n     cin >> u >> v >> t; n     --u; n     --v; n     if (t) n       g1[u].push_back(v), rg1[v].push_back(u); n     else n       g2[u].push_back(v), rg2[v].push_back(u); n   } n   vector<long long> ans(n, -1), len(n, -1); n   ans[n - 1] = 0; n   len[n - 1] = 0; n   queue<long long> q; n   q.push(n - 1); n   while (q.size()) { n     long long u = q.front(); n     q.pop(); n     set<long long> s1, s2; n     for (long long to : rg1[u]) n       if (ans[to] == 1 && len[to] == -1) len[to] = len[u] + 1, q.push(to); n     for (long long to : rg2[u]) n       if (ans[to] == 2 && len[to] == -1) len[to] = len[u] + 1, q.push(to); n     for (long long to : rg1[u]) n       if (ans[to] == -1) s1.insert(to); n     for (long long to : rg2[u]) n       if (ans[to] == -1) s2.insert(to); n     for (long long x : s1) n       if (!s2.count(x)) ans[x] = 2; n     for (long long x : s2) n       if (!s1.count(x)) ans[x] = 1; n     for (long long x : s1) n       if (s2.count(x) && len[x] == -1) n         ans[x] = 1, len[x] = len[u] + 1, q.push(x); n   } n   cout << len[0] <<   n ; n   for (long long i = 0; i < n; ++i) cout << (ans[i] == 1 ? 1 : 0); n } n int32_t main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int maxn = 212345; n int n, a[maxn], f[maxn], pre[maxn]; n vector<int> b; n int F(int x) { return x == f[x] ? x : f[x] = F(f[x]); } n inline void U(int a, int b) { f[min(F(a), F(b))] = max(F(a), F(b)); } n inline void go(int l, int r) { n   for (int i = l, j;; i = j) { n     j = F(i) + 1; n     if (j > r) return; n     U(i, j); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) b.push_back(a[i]); n   for (int i = 1; i <= n; ++i) f[i] = i; n   sort(b.begin(), b.end()); n   b.erase(unique(b.begin(), b.end()), b.end()); n   for (int i = 1; i <= n; ++i) n     a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin(); n   for (int i = 1; i <= n; ++i) { n     if (pre[a[i]]) go(pre[a[i]], i); n     pre[a[i]] = i; n   } n   long long res = 1, cnt = 0; n   for (int i = 1; i <= n; ++i) n     if (F(i) == i) { n       if (cnt) res = (res + res) % mod; n       ++cnt; n     } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, y, a[(int)1e5 + 4]; n unsigned long long x, sum; n void error() { n   puts( -1 ); n   exit(0); n } n int main() { n   scanf( %d%I64d%d , &n, &x, &y); n   sum = (unsigned long long)n; n   if (n > y || (n == y && sum < x)) error(); n   for (int i = 0; i < n; i++) a[i] = 1; n   a[n - 1] += (y - n); n   if (sum - 1LL + (unsigned long long)a[n - 1] * (unsigned long long)a[n - 1] < n       x) n     error(); n   for (int i = 0; i < n; i++) printf( %d n , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long counter; n long long n, r, l, x; n vector<long long> a; n long long sum, rr; n bool cheake(long long mask) { n   long long nmax = -1; n   long long nmin = 10000000000; n   long long i = 0; n   rr = 0; n   sum = 0; n   while ((mask >> i) != 0) { n     if ((mask >> i) % 2 == 1) { n       rr++; n       sum = sum + a[i]; n       nmax = max(a[i], nmax); n       nmin = min(a[i], nmin); n     } n     i++; n   } n   if ((rr >= 2) && (sum >= l) && (sum <= r) && (nmax - nmin) >= x) n     return true; n   else n     return false; n } n void solv(long long i, long long mask) { n   if (i == n) return; n   solv(i + 1, mask); n   long long nmask = mask | (1 << i); n   solv(i + 1, nmask); n   if (cheake(nmask)) counter++; n } n int main() { n   long long k; n   cin >> n >> l >> r >> x; n   for (long long i = 0; i < n; i++) { n     cin >> k; n     a.push_back(k); n   } n   solv(0, 0); n   cout << counter; n } n


#include<iostream> n #include<cstdio> n #include<cstring> n #include<cmath> n #include<algorithm> n #include<vector> n #include<stack> n #include<queue> n #include<set> n #include<map> n //#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n const int maxn = 1e5+5; n const int inf = 0x3f3f3f3f; n const ll INF = 1e18; n const ll mod = 998244353; n  n #define for1(i,n) for(int i=1;i<=n;i++) n #define forn(i,n) for(int i=0;i<n;i++) n #define m_p(x,y) make_pair(x,y) n  n inline int read(){//ll?? n     int x=0,f=1;char ch=getchar(); n     while(ch< 0 ||ch> 9 ){if(ch== - )f=-1;ch=getchar();} n     while(ch>= 0 &&ch<= 9 ){x=x*10+ch- 0 ;ch=getchar();} n     return x*f; n } n  n int main(){ n  int t=read(); while(t--){ n   int x=read(), y=read(); n   if(abs(x-y)<=1) cout << x+y << endl; n   else cout << x+y+abs(x-y)-1 << endl; n  } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, s; n vector<int> v1, v2; n pair<int, pair<int, int>> arr[3005]; n bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) { n   if (a.second.first == b.second.first) n     return a.second.second < b.second.second; n   return a.second.first > b.second.first; n } n int dp[3005][3005]; n int solve(int x, int taken) { n   if (x == n) { n     if (taken == p) return 0; n     return -1e9; n   } n   int &ret = dp[x][taken]; n   if (ret != -1) return ret; n   ret = 0; n   if (taken < p) { n     ret = max(ret, arr[x].second.second + solve(x + 1, taken + 1)); n   } n   int lef = min(s, x - taken); n   if (lef < s) { n     ret = max(ret, arr[x].second.first + solve(x + 1, taken)); n   } n   ret = max(ret, solve(x + 1, taken)); n   return ret; n } n void trace(int x, int taken) { n   if (x == n) return; n   int &ret = dp[x][taken]; n   if (taken < p && ret == arr[x].second.second + solve(x + 1, taken + 1)) { n     v2.push_back(arr[x].first); n     trace(x + 1, taken + 1); n     return; n   } n   int lef = min(s, x - taken); n   if (lef < s) { n     if (ret == arr[x].second.first + solve(x + 1, taken)) { n       v1.push_back(arr[x].first); n       trace(x + 1, taken); n       return; n     } n   } n   trace(x + 1, taken); n } n int main() { n   cin >> n >> p >> s; n   swap(p, s); n   for (int i = 0; i < n; i++) { n     cin >> arr[i].second.first; n     arr[i].first = i + 1; n   } n   for (int i = 0; i < n; i++) { n     cin >> arr[i].second.second; n   } n   sort(arr, arr + n, cmp); n   memset(dp, -1, sizeof dp); n   cout << solve(0, 0) <<   n ; n   trace(0, 0); n   for (auto i : v1) cout << i <<    ; n   cout <<   n ; n   for (auto i : v2) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = (int)1e5 + 100; n const int MAX = (int)1e5 + 1; n int l[maxN], d[maxN], cnt[maxN], q[maxN]; n vector<int> pool[maxN]; n multiset<int> p; n void solveTask() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> l[i]; n   for (int i = 1; i <= n; ++i) cin >> d[i]; n   for (int i = 1; i <= n; ++i) n     q[l[i]] += d[i], cnt[l[i]] += 1, pool[l[i]].push_back(d[i]); n   int answer = 0; n   int all_sum = 0; n   for (int i = 1; i <= MAX; ++i) { n     if (cnt[i] == 0) continue; n     int possibleCnt = cnt[i] - 1; n     int cur_sum = 0; n     for (set<int>::iterator it = p.begin(); it != p.end(); ++it) { n       if (possibleCnt == 0) break; n       cur_sum -= *it; n       --possibleCnt; n     } n     answer = max(answer, cur_sum + q[i]); n     all_sum += q[i]; n     for (int j = 0; j < pool[i].size(); ++j) { n       p.insert(-pool[i][j]); n     } n   } n   cout << all_sum - answer << endl; n } n int main() { n   solveTask(); n   ios_base::sync_with_stdio(false); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long get(long long n) { n   for (long long i = 2; i * i <= n; ++i) n     if (n % i == 0) return i; n   return n; n } n int main() { n   long long n; n   scanf( %lld , &n); n   long long cnt = 0; n   if (n % 2 != 0) { n     n -= get(n); n     ++cnt; n   } n   printf( %lld n , cnt + n / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, x, m = 0; n   char a[1000]; n   int s[26]; n   cin >> n; n   while (n--) { n     cin >> a; n     m = 0; n     int mi = 1000, mx = -1; n     for (i = 0; i < 26; i++) { n       s[i] = 0; n     } n     x = strlen(a); n     for (i = 0; i < x; i++) { n       s[a[i] -  a ]++; n       mi = min(mi, (a[i] -  a )); n       mx = max(mx, (a[i] -  a )); n     } n     for (i = 0; i < 26; i++) { n       if (s[i] > 1) { n         cout <<  No  << endl; n         m = 1; n         break; n       } n     } n     for (i = mi; i <= mx; i++) { n       if (s[i] != 1 && m != 1) { n         cout <<  No  << endl; n         m = 1; n         break; n       } n     } n     if (m == 0) cout <<  Yes  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int ar[500009], br[500009]; n int main() { n   long long int a, b, c, i, j = 0, k = 9999999999999; n   string s; n   cin >> a; n   getchar(); n   cin >> s; n   for (i = 0; i < a; i++) { n     cin >> ar[i]; n   } n   for (i = 0; i < a - 1; i++) { n     if (s[i] ==  R  && s[i + 1] ==  L ) { n       k = min(k, (ar[i + 1] - ar[i])); n     } n   } n   if (k == 9999999999999) { n     cout <<  -1 ; n   } else n     cout << (k / 2) << endl; n } n


#include <bits/stdc++.h> n #pragma diag_suppress 177 n using namespace std; n const int MM = 220000; n int m, n, k, p1, p2, p3, f, a[MM], pr[MM], af[MM], tree[4 * MM]; n string s, s1, s2; n int MINN = 0; n int func(int aa, int bb) { return (aa | bb); } n void build(int node, int start, int end) { n   if (start == end) n     tree[node] = a[start]; n   else { n     int mid = (start + end) / 2; n     build(2 * node, start, mid); n     build(2 * node + 1, mid + 1, end); n     tree[node] = func(tree[2 * node], tree[2 * node + 1]); n   } n } n int query(int node, int start, int end, int l, int r) { n   if (r < start or end < l) return MINN; n   if (l <= start and end <= r) return tree[node]; n   int mid = (start + end) / 2; n   int px1 = query(2 * node, start, mid, l, r); n   int px2 = query(2 * node + 1, mid + 1, end, l, r); n   return func(px1, px2); n } n bool chk(int hi, int mid, int val) { n   return (query(1, 0, n - 1, mid, hi) > val); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   int cur; n   priority_queue<pair<int, int> > pq; n   vector<int> ind; n   for (int i = 0; i < n; i++) { n     cur = i; n     while (!ind.empty() && a[ind.back()] <= a[i]) { n       cur = pr[ind.back()]; n       ind.pop_back(); n     } n     pr[i] = cur; n     ind.push_back(i); n   } n   ind.clear(); n   for (int i = n - 1; i >= 0; i--) { n     cur = i; n     while (!ind.empty() && a[ind.back()] < a[i]) { n       cur = af[ind.back()]; n       ind.pop_back(); n     } n     af[i] = cur; n     ind.push_back(i); n   } n   long long ans = 0; n   build(1, 0, n - 1); n   int ls, rs, hi, lo, mid, xx, yy; n   for (int i = 0; i < n; i++) { n     ls = i - pr[i] + 1; n     rs = af[i] - i + 1; n     hi = i; n     lo = pr[i]; n     while (hi > lo) { n       mid = (hi + lo) / 2; n       if (chk(i, mid, a[i])) n         lo = mid + 1; n       else n         hi = mid; n     } n     xx = i - lo + 1; n     ans += (long long)ls * (long long)rs; n     hi = af[i]; n     lo = i; n     while (hi > lo) { n       mid = (hi + lo) / 2; n       if (chk(mid, i, a[i])) n         hi = mid; n       else n         lo = mid + 1; n     } n     if (chk(lo, i, a[i])) lo--; n     yy = lo - i + 1; n     ans -= (long long)xx * (long long)yy; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5; n const int INF = 1e9 + 9; n const int B = 1e9 + 7; n long long n; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   long long pwr = 1; n   for (;;) { n     long long x = (n + (pwr - 1)) / pwr; n     if (n % pwr != 0) { n       cout << x <<   n ; n       return 0; n     } n     if (x == 1) { n       cout << x <<   n ; n       return 0; n     } n     if (x == 2 && pwr * 3 < n) { n       cout << x <<   n ; n       return 0; n     } n     pwr *= 3; n   } n   assert(0); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == b) return a; n   if (a > b) return gcd(a - b, b); n   return gcd(a, b - a); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     int a, h, sum = 0; n     cin >> a; n     for (int i = 2; i < a; i++) { n       int n = a; n       while (n >= i) { n         sum += n % i; n         n = n / i; n       } n       sum += n; n     } n     h = gcd(sum, a - 2); n     cout << sum / h <<  /  << (a - 2) / h << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int i, arr[n]; n     for (i = 0; i < n; i++) cin >> arr[i]; n     int ap{0}, bp{n}, asize{arr[0]}, bsize{}, moves{1}, left = n - 1; n     bool a = 1; n     while (left != 0) { n       if (a) { n         while (bsize <= asize && left != 0) { n           bsize += arr[bp - 1]; n           bp--; n           left--; n         } n         moves++; n         asize = 0; n         a = 0; n       } else { n         while (asize <= bsize && left != 0) { n           asize += arr[ap + 1]; n           ap++; n           left--; n         } n         moves++; n         bsize = 0; n         a = 1; n       } n     } n     cout << moves <<    ; n     asize = 0; n     for (i = 0; i <= ap; i++) asize += arr[i]; n     cout << asize <<    ; n     bsize = 0; n     for (; i < n; i++) bsize += arr[i]; n     cout << bsize << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n ll hell = 998244353; n int main() { n   ll n; n   cin >> n; n   vector<ll> v[n]; n   for (ll i = 0; i < n; ++i) { n     ll x; n     cin >> x; n     for (ll j = 0; j < x; ++j) { n       ll y; n       cin >> y; n       v[i].push_back(y); n     } n     v[i].push_back(100009); n     sort(v[i].begin(), v[i].end()); n   } n   ll ans = 0; n   for (ll i = 0; i < n; ++i) { n     ll e = (i != 0) ? i - 1 : n - 1; n     ll f = (i != n - 1) ? i + 1 : 0; n     for (ll j = 1; j < v[i].size() - 1; ++j) { n       ll v1 = v[i][j]; n       ll v2 = v[i][j - 1]; n       ll c1 = lower_bound(v[e].begin(), v[e].end(), v1) - n               lower_bound(v[e].begin(), v[e].end(), v2); n       ll c2 = lower_bound(v[f].begin(), v[f].end(), v1) - n               lower_bound(v[f].begin(), v[f].end(), v2); n       if (c1 != c2) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (f) x = -x; n } n template <typename F> n inline void write(F x) { n   static short st[30]; n   short tp = 0; n   if (x < 0) putchar( - ), x = -x; n   do st[++tp] = x % 10, x /= 10; n   while (x); n   while (tp) putchar( 0  | st[tp--]); n   putchar(  n ); n } n template <typename T> n inline void Mx(T &x, T y) { n   x < y && (x = y, 0); n } n template <typename T> n inline void Mn(T &x, T y) { n   x > y && (x = y); n } n const int N = 2005; n int m, n; n double f[N][N]; n int main() { n   read(n), read(m); n   int mx = max(n, m); n   for (int i = 0; i <= mx; i++) f[i][0] = 1; n   for (int i = 1; i <= mx; i++) f[0][i] = 1.0 / (i + 1); n   for (int i = 2; i <= n + m; i++) { n     for (int j = 1; j < i; j++) { n       if (i - j < 1) continue; n       int x = j, y = i - j; n       double inv = 1.0 / (y + 1); n       double c = inv * y * (1 - f[y - 1][x]), b = (1 - f[y][x - 1]); n       double d = c + inv; n       double p = (1.0 - b) / (1 - c + d - b); n       f[x][y] = p * c + (1 - p); n     } n   } n   printf( %.9lf %.9lf n , f[n][m], 1 - f[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, ns[10005] = {}; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     ns[a]++; n   } n   int ans = 0; n   for (int i = 0; i < 10000; i++) { n     int j = i + 1; n     while (ns[i] > 1) { n       for (; j < 10000 && ns[j] != 0; j++) n         ; n       ns[j]++; n       ns[i]--; n       ans += j - i; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string c =   ; n   string t =   ; n   cin >> t >> c; n   char lt = t[0]; n   int nt = int(t[1]) - 48; n   char lc = c[0]; n   int nc = int(c[1]) - 48; n   int cnt = 16; n   vector<pair<char, int> > torre; n   vector<pair<char, int> > caballo; n   for (int i = -2; i <= 2; i += 4) { n     for (int j = -1; j <= 1; j += 2) { n       char x = lt + i; n       int y = nt + j; n       char xx = lt + j; n       int yy = nt + i; n       if (int(x) < 105 && int(x) > 96 && y < 9 && y > 0) { n         cnt++; n         torre.push_back(make_pair(x, y)); n       } n       if (int(xx) < 105 && int(xx) > 96 && yy < 9 && yy > 0) { n         cnt++; n         torre.push_back(make_pair(xx, yy)); n       } n     } n   } n   for (int i = -2; i <= 2; i += 4) { n     for (int j = -1; j <= 1; j += 2) { n       char x = lc + i; n       int y = nc + j; n       char xx = lc + j; n       int yy = nc + i; n       if (int(x) < 105 && int(x) > 96 && y < 9 && y > 0 && x != lt && y != nt) { n         cnt++; n         caballo.push_back(make_pair(x, y)); n       } n       if (int(xx) < 105 && int(xx) > 96 && yy < 9 && yy > 0 && xx != lt && n           yy != nt) { n         cnt++; n         caballo.push_back(make_pair(xx, yy)); n       } n     } n   } n   for (int i = 0; i < torre.size(); i++) { n     for (int j = 0; j < caballo.size(); j++) { n       if (torre[i].first == caballo[j].first && n           torre[i].second == caballo[j].second) n         cnt--; n     } n   } n   cout << 64 - cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read(int f = 1, int x = 0, char ch =    ) { n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n const int N = 1e5 + 5, oo = 1e9 + 7; n int n, m, a[N], ans[N], b[N], f[N], g[N], l[N], p[N], vis[N]; n int get(int i, int x) { n   int j = lower_bound(b + 1, b + 1 + m, x) - b - 1; n   return vis[j] = 1, ans[i] = b[j]; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) a[i] = read(), f[i] = oo; n   ++n, a[n] = f[n] = oo; n   m = read(); n   for (int i = 1; i <= m; ++i) b[i] = read(); n   sort(b + 1, b + 1 + m); n   for (int i = 1; i <= n; ++i) n     if (~a[i]) { n       int j = lower_bound(f + 1, f + 1 + n, a[i]) - f - 1; n       l[i] = j + 1, f[j + 1] = a[i], p[i] = g[j], g[j + 1] = i; n     } else n       for (int j = n, k = m; k; f[j + 1] = b[k], g[j + 1] = i, --k) n         for (; f[j] >= b[k]; --j) n           ; n   for (int i = l[n], j = n, x = a[n]; i; --i) n     if (~a[j]) n       ~a[p[j]] ? x = a[p[j]] : x = get(p[j], x), j = p[j]; n     else { n       bool flg = false; n       for (int k = j - 1; k && !flg; --k) n         if (~a[k] && l[k] == i - 1 && a[k] < x) x = a[j = k], flg = true; n       for (int k = j - 1; k && !flg; --k) n         if (!~a[k]) x = get(j = k, x), flg = true; n     } n   for (int i = 1; i < n; ++i) n     if (ans[i]) a[i] = ans[i]; n   for (int i = 1, j = 1; i < n; ++i) n     if (!~a[i]) { n       for (; vis[j]; ++j) n         ; n       vis[j] = 1, a[i] = b[j]; n     } n   for (int i = 1; i < n; ++i) printf( %d  , a[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, k; n   cin >> n >> k; n   string second; n   cin >> second; n   if (k > 2) { n     int c = 0; n     for (int i = 1; i < n; i++) { n       if (second[i] == second[i - 1]) { n         if (i + 1 < n) { n           int j = 0; n           while (j < k && n                  ((j +  A ) == second[i - 1] || (j +  A ) == second[i + 1])) n             j++; n           second[i] = j +  A ; n         } else { n           second[i] = ((second[i - 1] -  A  + 1) % k) +  A ; n         } n         c++; n       } n     } n     cout << c <<   n ; n     cout << second <<   n ; n     return; n   } n   string t =  A , h =  B ; n   for (int i = 1; i < n; i++) { n     if (t[i - 1] ==  A ) n       t +=  B ; n     else n       t +=  A ; n     if (h[i - 1] ==  A ) n       h +=  B ; n     else n       h +=  A ; n   } n   int c1 = 0, c2 = 0; n   for (int i = 0; i < n; i++) { n     if (second[i] != t[i]) c1++; n     if (h[i] != second[i]) c2++; n   } n   cout << min(c1, c2) <<   n ; n   if (c1 <= c2) n     cout << t <<   n ; n   else n     cout << h <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Event { n   int type; n   int p; n   int x; n }; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<int> v(n); n   for (auto& el : v) { n     cin >> el; n   } n   int q; n   cin >> q; n   vector<Event> evs(q); n   for (auto& ev : evs) { n     cin >> ev.type; n     if (ev.type == 2) { n       cin >> ev.x; n     } else { n       cin >> ev.p >> ev.x; n       ev.p--; n     } n   } n   int mx = 0; n   vector<bool> has_ans(n, false); n   for (int i = q - 1; i >= 0; i--) { n     if (evs[i].type == 1) { n       if (!has_ans[evs[i].p]) { n         v[evs[i].p] = max(mx, evs[i].x); n         has_ans[evs[i].p] = true; n       } n     } else { n       mx = max(evs[i].x, mx); n     } n   } n   for (int i = 0; i < n; i++) { n     if (!has_ans[i]) { n       v[i] = max(v[i], mx); n     } n   } n   for (auto& el : v) { n     cout << el <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef int ll; n const ll MAX = 5e5 + 100; n const ll inf = 1e9; n const ll mod = 1e9 + 7; n bool go[MAX]; n ll p[MAX], dsu[MAX], n, m; n pair<ll, ll> e[MAX]; n vector<pair<ll, ll> > Querie; n ll ans[MAX]; n ll vertex; n vector<ll> adj[MAX]; n vector<ll> querys_on_graph[MAX]; n ll find(ll u) { return (dsu[u] == u ? u : dsu[u] = find(dsu[u])); } n void uni(ll u, ll v) { n   ll x = find(u), y = find(v); n   if (x == y) return; n   vertex++; n   adj[vertex].push_back(x); n   adj[vertex].push_back(y); n   dsu[y] = vertex; n   dsu[x] = vertex; n } n ll L[MAX], R[MAX], pos[MAX], leaf = 1, val[MAX], val_to_u[MAX]; n ll tree[MAX * 2]; n void dfs(ll u) { n   L[u] = inf; n   for (ll v : adj[u]) { n     dfs(v); n     L[u] = min(L[u], L[v]); n     R[u] = max(R[u], R[v]); n   } n   if (adj[u].size() == 0) { n     pos[u] = leaf++; n     L[u] = R[u] = pos[u]; n     val[pos[u]] = p[u]; n   } n } n void build(ll node, ll l, ll r) { n   if (l == r) { n     tree[node] = val[l]; n     return; n   } n   ll mid = (l + r) >> 1; n   build(node << 1, l, mid); n   build(node << 1 | 1, mid + 1, r); n   tree[node] = max(tree[node << 1], tree[node << 1 | 1]); n } n void update(ll node, ll l, ll r, ll x) { n   if (l == r) { n     tree[node] = 0; n     return; n   } n   ll mid = (l + r) >> 1; n   if (x <= mid) n     update(node << 1, l, mid, x); n   else n     update(node << 1 | 1, mid + 1, r, x); n   tree[node] = max(tree[node << 1], tree[node << 1 | 1]); n } n ll query(ll node, ll l, ll r, ll i, ll j) { n   if (j < l || r < i) return 0; n   if (i <= l && r <= j) { n     return tree[node]; n   } n   ll mid = (l + r) >> 1; n   return max(query(node << 1, l, mid, i, j), n              query(node << 1 | 1, mid + 1, r, i, j)); n } n void dfs_go(ll u) { n   for (ll v : adj[u]) { n     dfs_go(v); n   } n   sort(querys_on_graph[u].begin(), querys_on_graph[u].end()); n   for (ll it : querys_on_graph[u]) { n     ll answer = query(1, 1, n, L[u], R[u]); n     ans[it] = answer; n     if (answer != 0) { n       update(1, 1, n, pos[val_to_u[answer]]); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ll q, t, u; n   cin >> n >> m >> q; n   for (ll i = 1; i < (ll)n + 1; i++) cin >> p[i], val_to_u[p[i]] = i; n   for (ll i = 1; i < (ll)m + 1; i++) cin >> e[i].first >> e[i].second; n   for (ll i = 0; i < (ll)q; i++) { n     cin >> t >> u; n     if (t == 2) go[u] = 1; n     Querie.push_back({t, u}); n   } n   for (ll i = 1; i < (ll)2 * n + 5; i++) dsu[i] = i; n   for (ll i = 1; i < (ll)m + 1; i++) n     if (!go[i]) Querie.push_back({2, i}); n   vertex = n; n   vector<ll> ask; n   reverse(Querie.begin(), Querie.end()); n   for (pair<ll, ll> it : Querie) { n     if (it.first == 2) { n       uni(e[it.second].first, e[it.second].second); n     } else { n       ask.push_back(find(it.second)); n     } n   } n   vertex++; n   for (ll i = 1; i < (ll)vertex; i++) n     if (find(i) == i) adj[vertex].push_back(find(i)); n   dfs(vertex); n   build(1, 1, n); n   reverse(ask.begin(), ask.end()); n   for (ll i = 0; i < (ll)ask.size(); i++) { n     ll u = ask[i]; n     ll answer = query(1, 1, n, L[u], R[u]); n     cout << answer << endl; n     if (answer != 0) { n       update(1, 1, n, pos[val_to_u[answer]]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   vector<int> one; n   vector<int> two; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int t, w; n     cin >> t >> w; n     if (t == 1) n       one.push_back(w); n     else n       two.push_back(w); n   } n   sort(one.begin(), one.end()); n   sort(two.begin(), two.end()); n   vector<int> pref_one = {0}; n   vector<int> pref_two = {0}; n   for (int i = 1; i <= one.size(); i++) { n     int val = one[i - 1] + pref_one[i - 1]; n     pref_one.push_back(val); n   } n   for (int i = 1; i <= two.size(); i++) { n     int val = two[i - 1] + pref_two[i - 1]; n     pref_two.push_back(val); n   } n   int res = 1000000; n   for (int i = 0; i <= one.size(); i++) { n     for (int j = 0; j <= two.size(); j++) { n       bool possible = false; n       int ans = i + 2 * j; n       int w_one = pref_one[one.size() - i]; n       int w_two = pref_two[two.size() - j]; n       if (ans >= (w_one + w_two)) possible = true; n       if (possible) res = min(res, ans); n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long Mypow(long long X, long long Y) { n   if (Y < 0) return 0; n   if (Y == 0) return 1; n   if (Y == 1) { n     return X; n   } else { n     long long x = Mypow(X, Y / 2); n     x *= x; n     if (Y % 2) n       return (x * X); n     else n       return x; n   } n } n long long n, m, l, r, x; n long long p[100100]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n   } n   while (m--) { n     scanf( %I64d %I64d %I64d , &l, &r, &x); n     if (x < l || x > r) { n       printf( Yes n ); n       continue; n     } n     int sum = 0; n     for (int i = l; i <= r; i++) { n       if (p[i] < p[x]) { n         sum++; n       } n     } n     if (x == l + sum) { n       printf( Yes n ); n     } else { n       printf( No n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; n const double eps = 1e-8; n void read(int &k) { n   k = 0; n   char x = getchar(); n   while (x <  0  || x >  9 ) x = getchar(); n   while (x >=  0  && x <=  9 ) { n     k = k * 10 - 48 + x; n     x = getchar(); n   } n } n int n, tra[128], lx, ly, Lx[10010], Ly[10010], a[2100][2100], ll, rr, tx, ty, n     nx, ny, k; n pair<pair<int, int>, pair<int, int> > ope[1010]; n pair<int, int> dd[5000000]; n char s[10]; n void update(int type, int &x) { n   if (!type) n     x = lower_bound(Lx + 1, Lx + lx + 1, x) - Lx; n   else n     x = lower_bound(Ly + 1, Ly + ly + 1, x) - Ly; n } n int main() { n   tra[ R ] = 0; n   tra[ D ] = 1; n   tra[ L ] = 2; n   tra[ U ] = 3; n   scanf( %d , &n); n   nx = 0; n   ny = 0; n   Lx[++lx] = 0; n   Ly[++ly] = 0; n   Ly[++ly] = Lx[++lx] = 1000000003; n   Ly[++ly] = Lx[++lx] = -1000000003; n   Lx[++lx] = 1; n   Ly[++ly] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %s%d , s, &k); n     ope[i].first = make_pair(nx, ny); n     nx += dx[tra[s[0]]] * k; n     ny += dy[tra[s[0]]] * k; n     ope[i].second = make_pair(nx, ny); n     Lx[++lx] = nx; n     Ly[++ly] = ny; n     Lx[++lx] = nx + 1; n     Ly[++ly] = ny + 1; n   } n   sort(Lx + 1, Lx + lx + 1); n   lx = unique(Lx + 1, Lx + lx + 1) - Lx - 1; n   sort(Ly + 1, Ly + ly + 1); n   ly = unique(Ly + 1, Ly + ly + 1) - Ly - 1; n   for (int i = 1; i <= n; i++) { n     update(0, ope[i].first.first); n     update(1, ope[i].first.second); n     update(0, ope[i].second.first); n     update(1, ope[i].second.second); n     if (ope[i].first.first == ope[i].second.first) { n       if (ope[i].first.second > ope[i].second.second) n         swap(ope[i].first.second, ope[i].second.second); n       for (int j = ope[i].first.second; j <= ope[i].second.second; j++) n         a[ope[i].first.first][j] = 1; n     } else { n       if (ope[i].first.first > ope[i].second.first) n         swap(ope[i].first.first, ope[i].second.first); n       for (int j = ope[i].first.first; j <= ope[i].second.first; j++) n         a[j][ope[i].first.second] = 1; n     } n   } n   a[1][1] = 2; n   for (dd[ll = rr = 0] = make_pair(1, 1); ll <= rr; ll++) { n     for (int dir = 0; dir < 4; dir++) { n       tx = dd[ll].first + dx[dir]; n       ty = dd[ll].second + dy[dir]; n       if (tx > 0 && tx <= lx && ty > 0 && ty <= ly && !a[tx][ty]) n         a[tx][ty] = 2, dd[++rr] = make_pair(tx, ty); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= lx; i++) n     for (int j = 1; j <= ly; j++) n       if (a[i][j] <= 1) ans += 1ll * (Lx[i + 1] - Lx[i]) * (Ly[j + 1] - Ly[j]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, dist1, dist2, i, blank; n   string s, t; n   for (i = 0; i < 300; i++) cout <<  a ; n   cout << endl; n   cin >> dist1; n   if (dist1 == 0) return 0; n   for (i = 0; i < 300; i++) cout <<  b ; n   cout << endl; n   cin >> dist2; n   if (dist2 == 0) return 0; n   blank = dist1 + dist2 - 300; n   dist1 -= blank; n   dist2 -= blank; n   n = dist1 + dist2; n   s =   ; n   for (i = 0; i < n; i++) s +=  a ; n   if (dist2 == n) { n     cout << s << endl; n     cin >> dist2; n     return 0; n   } n   for (i = 0; i < n - 1; i++) { n     t = s; n     t[i] =  b ; n     cout << t << endl; n     cin >> dist2; n     if (dist2 == 0) return 0; n     if (dist2 < dist1) s[i] =  b ; n     dist1 = min(dist2, dist1); n   } n   s[n - 1] =  b ; n   cout << s << endl; n   cin >> dist2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct tp { n   int l, r; n }; n int n; n vector<tp> t; n bool comp(tp a, tp b) { return a.l < b.l or a.l == b.l and a.r < b.r; } n int main() { n   cout.sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int l, r; n     cin >> l >> r; n     t.push_back({l, r}); n   } n   sort(t.begin(), t.end(), comp); n   int cl = -1; n   int cr = -1; n   int ans = 0; n   for (int i = 0; i < t.size(); i++) { n     if (t[i].l > cr) { n       ans += 1; n       cl = t[i].l; n       cr = t[i].r; n     } else if (t[i].l <= cr and t[i].r > cr) { n       continue; n     } else if (t[i].l <= cr and t[i].r <= cr) { n       cl = t[i].l; n       cr = t[i].r; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 140000; n int sx[MAX + 10], ex[MAX], sy[MAX + 10], ey[MAX]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     scanf( %d %d %d %d , &sx[i], &sy[i], &ex[i], &ey[i]); n   vector<pair<int, int> > px, py; n   for (int i = 0; i < n; i++) { n     px.push_back(make_pair(sx[i], -i - 1)); n     px.push_back(make_pair(ex[i], i + 1)); n   } n   sort(px.begin(), px.end()); n   set<int> rec; n   int rx = -1, ry = -1; n   for (int i = 0; i < (int)px.size(); i++) { n     if (px[i].second < 0) { n       rec.insert(-1 * px[i].second); n     } else { n       rec.erase(px[i].second); n     } n     if ((int)rec.size() >= n - 1) { n       py.clear(); n       for (set<int>::iterator it = rec.begin(); it != rec.end(); it++) { n         py.push_back(make_pair(sy[*it - 1], -1 * (*it))); n         py.push_back(make_pair(ey[*it - 1], *it)); n       } n       sort(py.begin(), py.end()); n       int inter = 0; n       for (int j = 0; j < (int)py.size(); j++) { n         if (py[j].second < 0) n           inter++; n         else n           inter--; n         if (inter >= n - 1) rx = px[i].first, ry = py[j].first; n       } n     } n   } n   printf( %d %d n , rx, ry); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 13 * 16, M = 1e9 + 7; n int n, m, k; n struct zz { n   int a[N][N]; n } A; n inline zz cf(zz x, zz y) { n   zz z; n   memset(z.a, 0, sizeof z.a); n   for (int i = 0; i < (1 << m) * (k + 1); i++) n     for (int j = 0; j < (1 << m) * (k + 1); j++) n       for (int l = 0; l < (1 << m) * (k + 1); l++) n         z.a[i][j] = (z.a[i][j] + (long long)x.a[i][l] * y.a[l][j]) % M; n   return z; n } n inline zz ksm(zz x, int y) { n   zz ans; n   memset(ans.a, 0, sizeof ans.a); n   for (int i = 0; i < (1 << m) * (k + 1); i++) ans.a[i][i] = 1; n   for (; y; y >>= 1, x = cf(x, x)) n     if (y & 1) ans = cf(ans, x); n   return ans; n } n int main() { n   scanf( %d%d%d , &n, &k, &m); n   for (int i = 0; i < 1 << m; i++) n     for (int j = 0; j <= k; j++) { n       int num = 1; n       for (int l = 0; l < m; l++) n         if ((1 << l) & i) num++; n       A.a[(i >> 1) * (k + 1) + j][i * (k + 1) + j] = 1; n       if (j < k) n         A.a[(i >> 1 | (1 << (m - 1))) * (k + 1) + j + 1][i * (k + 1) + j] = num; n     } n   A = ksm(A, n); n   int ans = 0; n   for (int i = 0; i < 1 << m; i++) (ans += A.a[i * (k + 1) + k][0]) %= M; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n set<int> b[127]; n set<int>::iterator it, itt; n int n, m, l, r, c[200005]; n char s, ch[2], a[200005]; n void upd(int v, int d) { n   while (v <= n) { n     c[v] += d; n     v += v & -v; n   } n } n int find(int x) { n   int cur = 0, ans = 0; n   for (int i = 20; i >= 0; i--) { n     ans += (1 << i); n     if (ans >= n || cur + c[ans] >= x) n       ans -= (1 << i); n     else n       cur += c[ans]; n   } n   return ans + 1; n } n int gsum(int v) { n   int sum = 0; n   while (v > 0) { n     sum += c[v]; n     v -= v & -v; n   } n   return sum; n } n int main() { n   scanf( %d%d , &n, &m); n   scanf( %s , a + 1); n   for (int i = 1; i <= n; i++) b[a[i]].insert(i); n   for (int i = 1; i <= n; i++) upd(i, 1); n   for (int i = 0; i < m; i++) { n     scanf( %d%d%s , &l, &r, ch); n     s = ch[0]; n     l = find(l); n     r = find(r); n     it = b[s].lower_bound(l); n     while (it != b[s].end() && *it <= r) n       upd(*it, -1), a[*it] =  # , itt = it, it++, b[s].erase(itt); n   } n   for (int i = 1; i <= n; i++) n     if (gsum(i) - gsum(i - 1) == 1) printf( %c , a[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1 << 17; n const int INF = 7 << 28; n struct T { n   int maxa, minr; n   T() : maxa(0), minr(INF) {} n   T(int a, int b) : maxa(a), minr(b) {} n   T operator+(T t) const { return T(max(maxa, t.maxa), min(minr, t.minr)); } n }; n T a[MAXN * 2]; n int del[MAXN * 2]; n int l[MAXN], r[MAXN], t[MAXN]; n void setDel(int i, int d) { n   del[i] += d; n   a[i].minr -= d; n } n void push(int i) { n   setDel(i * 2, del[i]); n   setDel(i * 2 + 1, del[i]); n   del[i] = 0; n } n void com(int i) { n   push(i); n   a[i] = a[i * 2] + a[i * 2 + 1]; n } n void clear(int i) { n   int j; n   for (j = 0; (1 << j) < MAXN; j++) n     ; n   for (; j; j--) push(i >> j); n } n T get(int i) { n   clear(i); n   T ret = T(); n   while (i) { n     if (~i & 1) ret = a[i + 1] + ret; n     i >>= 1; n   } n   return ret; n } n void modify(int i, T t) { n   i += MAXN; n   clear(i); n   a[i] = t; n   del[i] = 0; n   i >>= 1; n   while (i) { n     com(i); n     i >>= 1; n   } n } n void init() { fill(a, a + MAXN * 2, T()); } n void put(int t) { printf( %d  , t); } n int main() { n   int n; n   int i, j, k; n   int ans = 0; n   init(); n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d , &l[i], &r[i], &t[i]); n     r[i]++; n   } n   for (i = 1; i <= n; i++) { n     if (max(ans, l[i]) + t[i] <= r[i]) { n       put(0); n       modify(i, T(t[i], max(ans, l[i]) - l[i])); n       ans = max(ans, l[i]) + t[i]; n     } else { n       int ra, rr; n       j = 1; n       ra = 0; n       rr = INF; n       while (j < MAXN) { n         push(j); n         j = j * 2 + 1; n         if (max(a[j].maxa, ra) < min(rr, a[j].minr)) { n           ra = max(a[j].maxa, ra); n           rr = min(a[j].minr, rr); n           j--; n         } n       } n       T temp = get(j); n       int move = max(temp.maxa, min(temp.minr, a[j].maxa)); n       if (max(ans - move, l[i]) + t[i] > r[i] || n           max(ans - move, l[i]) + t[i] >= ans) { n         put(-1); n         continue; n       } n       move = min(move, ans - l[i]); n       j = 1; n       while (j < MAXN) { n         j = j * 2 + 1; n         if (a[j].maxa < move) j--; n       } n       put(j - MAXN); n       temp = get(j); n       move = min(temp.minr, a[j].maxa); n       ans -= move; n       modify(j - MAXN, T(0, INF)); n       while (j != 1) { n         if (~j & 1) setDel(j + 1, move); n         j >>= 1; n         com(j); n       } n       modify(i, T(t[i], max(ans, l[i]) - l[i])); n       ans = max(ans, l[i]) + t[i]; n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     long long n; n     cin >> n; n     int a = 0; n     int b = 0; n     int c = 0; n     while (n % 2 == 0) { n       n /= 2; n       ++a; n     } n     while (n % 3 == 0) { n       n /= 3; n       ++b; n     } n     while (n % 5 == 0) { n       n /= 5; n       ++c; n     } n     if (n != 1) { n       cout <<   -1   << endl; n     } else { n       cout << a + b * 2 + c * 3 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, cnt, Max, head[N]; n struct Edge { n   int to, nxt, val; n } e[N << 1]; n int read() { n   int x = 0, flg = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) flg = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * flg; n } n void add(int x, int y, int w) { n   e[++cnt].to = y; n   e[cnt].val = w; n   e[cnt].nxt = head[x]; n   head[x] = cnt; n } n void dfs(int x, int fa, int dis) { n   Max = max(Max, dis); n   for (int i = head[x]; i; i = e[i].nxt) { n     int u = e[i].to; n     if (u == fa) continue; n     dfs(u, x, dis + e[i].val); n   } n } n int main() { n   n = read(); n   int x, y, w; n   long long sum = 0; n   for (int i = 2; i <= n; ++i) { n     x = read(), y = read(), w = read(); n     add(x, y, w); n     add(y, x, w); n     sum += 2 * w; n   } n   dfs(1, 0, 0); n   printf( %lld n , sum - Max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   std::cin >> n; n   std::vector<int> v(101); n   for (int i = 0; i < n; ++i) { n     int temp; n     std::cin >> temp; n     v[temp]++; n   } n   int mex_A = -1; n   int mex_B = -1; n   bool flag1 = 1; n   for (int i = 0; i < n; ++i) { n     if (v[i] > 1 && flag1) { n       mex_A = mex_B = i; n     } else if (v[i] >= 1) { n       mex_A = i; n       flag1 = 0; n     } else if (v[i] == 0) { n       break; n     } n   } n   std::cout << ++mex_A + ++mex_B <<   n ; n } n int main() { n   int x; n   cin >> x; n   while (x--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct compare { n   bool operator()(const pair<int, int>& a, const pair<int, int>& b) { n     return true; n   } n }; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x, y; n     cin >> n >> x >> y; n     vector<int> ans; n     for (int i = 1; i <= 50; i++) { n       ans.clear(); n       if ((x - y) % i != 0) continue; n       for (int j = x; j <= y; j += i) ans.push_back(j); n       if (ans.back() != y || ans.size() > n) continue; n       int j = x - i; n       while (ans.size() < n && j >= 1) { n         ans.push_back(j); n         j -= i; n       } n       j = y + i; n       while (ans.size() < n) { n         ans.push_back(j); n         j += i; n       } n       break; n     } n     for (auto ai : ans) cout << ai <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n const int p = 257; n const long long mods = 1610612741; n const int maxn = 200010; n long long hash1[maxn][2]; n int cntzero[maxn]; n char s[maxn]; n int N; n long long pp[maxn]; n void gethash(char s[]) { n   hash1[0][0] = 0; n   hash1[0][1] = 0; n   for (int i = 1; i <= N; i++) { n     cntzero[i] = cntzero[i - 1]; n     if (s[i] ==  0 ) { n       cntzero[i]++; n       hash1[i][0] = (hash1[i - 1][0] * p + (i & 1) + 1) % mods; n       hash1[i][1] = (hash1[i - 1][1] * p + ((i + 1) & 1) + 1) % mods; n     } else { n       hash1[i][0] = hash1[i - 1][0]; n       hash1[i][1] = hash1[i - 1][1]; n     } n   } n   return; n } n void init() { n   pp[0] = 1; n   for (int i = 1; i <= N; i++) { n     pp[i] = (pp[i - 1] * p) % mods; n   } n } n long long getsubhash(int L, int R) { n   return (hash1[R][L & 1] - n           (hash1[L - 1][L & 1] * pp[cntzero[R] - cntzero[L - 1]]) % mods + n           mods) % n          mods; n } n int main() { n   scanf( %d , &N); n   scanf( %s , s + 1); n   init(); n   gethash(s); n   int q; n   scanf( %d , &q); n   while (q--) { n     int l1, l2, lens; n     scanf( %d%d%d , &l1, &l2, &lens); n     if (getsubhash(l1, l1 + lens - 1) == getsubhash(l2, l2 + lens - 1)) n       printf( Yes n ); n     else n       printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int binary_search(vector<int> a, int korol) { n   if (a[a.size() - 1] < korol) return a.size() - 1; n   if (a[0] > korol) return -1; n   int max = a.size() - 1, min = 0, mid; n   if (min + max >= 0) n     mid = (int)((min + max) / 2); n   else n     mid = (int)((min + max - 1) / 2); n   while (1) { n     while (a[mid] > korol) { n       max = mid; n       if (min + max >= 0) n         mid = (int)((min + max) / 2); n       else n         mid = (int)((min + max - 1) / 2); n     } n     while (a[mid] < korol && mid != min) { n       min = mid; n       if (min + max >= 0) n         mid = (int)((min + max) / 2); n       else n         mid = (int)((min + max - 1) / 2); n     } n     if (min == mid) return min; n   } n } n int result(vector<int> a, vector<int> b, int korol) { n   sort(a.begin(), a.begin() + a.size()); n   sort(b.begin(), b.begin() + b.size()); n   int min_a = binary_search(a, korol); n   int min_b = binary_search(b, korol); n   if (min_a != -1 && min_b == -1) { n     cout <<  YES ; n     return 1; n   } n   if (min_b != -1 && min_a != -1 && a[min_a] > b[min_b]) { n     cout <<  YES ; n     return 1; n   } n   if (min_a + 1 != a.size() && min_b + 1 == b.size()) { n     cout <<  YES ; n     return 1; n   } n   if (min_a + 1 != a.size() && min_b + 1 != b.size() && n       a[min_a + 1] < b[min_b + 1]) { n     cout <<  YES ; n     return 1; n   } n   return 0; n } n int main() { n   int n; n   cin >> n; n   int korol_x, korol_y; n   cin >> korol_x >> korol_y; n   vector<int> slon_na_glav(0); n   vector<int> slon_na_dobav(0); n   vector<int> pomecha_gor(0); n   vector<int> pomecha_ver(0); n   vector<int> ladia_na_gor(0); n   vector<int> ladia_na_ver(0); n   vector<int> pomecha_glav(0); n   vector<int> pomecha_dobav(0); n   for (int i = 0; i < n; i++) { n     char fig; n     cin >> fig; n     int x, y; n     cin >> x >> y; n     if (fig ==  B ) { n       if (y - x == korol_y - korol_x) slon_na_glav.push_back(x); n       if (y + x == korol_x + korol_y) slon_na_dobav.push_back(x); n       if (x == korol_x) pomecha_ver.push_back(y); n       if (y == korol_y) pomecha_gor.push_back(x); n     } n     if (fig ==  R ) { n       if (y - x == korol_y - korol_x) pomecha_glav.push_back(x); n       if (y + x == korol_x + korol_y) pomecha_dobav.push_back(x); n       if (x == korol_x) ladia_na_ver.push_back(y); n       if (y == korol_y) ladia_na_gor.push_back(x); n     } n     if (fig ==  Q ) { n       if (y - x == korol_y - korol_x) slon_na_glav.push_back(x); n       if (y + x == korol_x + korol_y) slon_na_dobav.push_back(x); n       if (x == korol_x) ladia_na_ver.push_back(y); n       if (y == korol_y) ladia_na_gor.push_back(x); n     } n   } n   int progress = 0; n   if (!slon_na_glav.empty()) { n     if (pomecha_glav.empty()) { n       cout <<  YES ; n       return 0; n     } n     progress = result(slon_na_glav, pomecha_glav, korol_x); n     if (progress) return 0; n   } n   if (!slon_na_dobav.empty()) { n     if (pomecha_dobav.empty()) { n       cout <<  YES ; n       return 0; n     } n     progress = result(slon_na_dobav, pomecha_dobav, korol_x); n     if (progress) return 0; n   } n   if (!ladia_na_gor.empty()) { n     if (pomecha_gor.empty()) { n       cout <<  YES ; n       return 0; n     } n     progress = result(ladia_na_gor, pomecha_gor, korol_x); n     if (progress) return 0; n   } n   if (!ladia_na_ver.empty()) { n     if (pomecha_ver.empty()) { n       cout <<  YES ; n       return 0; n     } n     progress = result(ladia_na_ver, pomecha_ver, korol_y); n     if (progress) return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void boost() { n   ios_base::sync_with_stdio(); n   cin.tie(0); n   cout.tie(0); n } n const long long maxn = 1e5 + 123; n const long long inf = 1e9 + 123; n const long long mod = 1e9 + 7; n const double infm = 1e-9; n const double pi = acos(-1); n map<char, int> m; n int main() { n   boost(); n   int n; n   cin >> n; n   string s; n   cin >> s; n   int ans = 0; n   for (int i = 1; i < s.size(); i++) { n     if (s[i] == s[i - 1]) { n       m[s[i - 1]]++; n       m[s[i + 1]]++; n       if (m[ B ] == 0) { n         s[i] =  B ; n       } else if (m[ G ] == 0) { n         s[i] =  G ; n       } else if (m[ R ] == 0) { n         s[i] =  R ; n       } n       m[s[i - 1]]--; n       m[s[i + 1]]--; n       ans++; n     } n   } n   cout << ans <<   n  << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void readInt(int &num) { n   num = 0; n   int c = getchar(); n   int s = 1; n   if (c ==  - ) { n     s = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     num = num * 10 + (c -  0 ); n     c = getchar(); n   } n   num *= s; n } n inline void printInt(int num) { n   string s =   ; n   if (num < 0) { n     putchar( - ); n     num = -num; n   } n   if (num == 0) putchar( 0 ); n   while (num) { n     s +=  0  + num % 10; n     num /= 10; n   } n   for (int i = s.size() - 1; i >= 0; i--) { n     putchar(s[i]); n   } n } n inline void readLL(long long &num) { n   num = 0; n   int c = getchar(); n   int s = 1; n   if (c ==  - ) { n     s = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     num = num * 10 + (c -  0 ); n     c = getchar(); n   } n   num *= s; n } n inline void printLL(long long num) { n   string s =   ; n   if (num < 0) { n     putchar( - ); n     num = -num; n   } n   if (num == 0) putchar( 0 ); n   while (num) { n     s +=  0  + num % 10; n     num /= 10; n   } n   for (int i = s.size() - 1; i >= 0; i--) { n     putchar(s[i]); n   } n } n inline void readString(string &s) { n   s =   ; n   char c = getchar(); n   while (c !=     && c !=   n  && c != EOF) { n     s += c; n     c = getchar(); n   } n } n inline void readLine(string &s) { n   s =   ; n   char c = getchar(); n   while (c !=   n  && c != EOF) { n     s += c; n     c = getchar(); n   } n } n inline void printString(const string &s) { n   for (int i = 0; i < (int)s.size(); i++) putchar(s[i]); n } n int main() { n   int n; n   readInt(n); n   string str; n   string zeroes =   ; n   string ugly =   ; n   bool is_find = false; n   for (int i = 0; i < (int)n; i++) { n     string temp; n     readString(temp); n     if (temp.size() == 1 && temp[0] ==  0 ) { n       putchar( 0 ); n       return 0; n     } else if (!is_find) { n       if (temp[0] !=  1 ) { n         ugly = temp; n         is_find = true; n       } else if (temp.size() > 1) { n         string tz =   ; n         for (int i = 1; i < temp.size(); i++) n           if (temp[i] !=  0 ) { n             ugly = temp; n             is_find = true; n             break; n           } else n             tz +=  0 ; n         if (!is_find) zeroes += tz; n       } n     } else if (temp.size() > 1) { n       for (int j = 1; j < temp.size(); j++) zeroes +=  0 ; n     } n   } n   string ans = (is_find) ? ugly + zeroes :  1  + zeroes; n   printString(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fa[100020], sz[100020]; n vector<int> g[100020]; n int n, m, q; n int find(int x) { n   if (fa[x] != x) fa[x] = find(fa[x]); n   return fa[x]; n } n int dp[100020][3], G[100020], mx[100020]; n vector<int> d[100020]; n vector<long long> sum[100020]; n bool cmp(int i, int j) { return i > j; } n void dfs(int u, int p) { n   for (auto v : g[u]) { n     if (v == p) continue; n     dfs(v, u); n     dp[u][2] = dp[v][0] + 1; n     sort(dp[u], dp[u] + 3, cmp); n   } n } n void dfs2(int u, int p) { n   for (auto v : g[u]) { n     if (v == p) continue; n     int t; n     if (dp[u][0] == dp[v][0] + 1) n       t = dp[u][1]; n     else n       t = dp[u][0]; n     G[v] = max(G[u] + 1, t + 1); n     dfs2(v, u); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; ++i) { n     fa[i] = i; n     sz[i] = 1; n   } n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n     sz[find(v)] += sz[find(u)]; n     fa[find(u)] = find(v); n   } n   for (int i = 1; i <= n; ++i) { n     if (fa[i] == i) { n       dfs(i, 0); n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (fa[i] == i) dfs2(i, 0); n   } n   for (int i = 1; i <= n; ++i) { n     d[i].push_back(0); n     sum[i].push_back(0); n   } n   for (int i = 1; i <= n; ++i) { n     int x = find(i); n     mx[x] = max(mx[x], max(dp[i][0], G[i])); n     int t = max(dp[i][0], G[i]); n     d[x].push_back(t); n     sum[x].push_back(0); n   } n   for (int i = 1; i <= n; ++i) { n     sort(d[i].begin(), d[i].end()); n     if (fa[i] == i) { n       for (int j = 1; j < sum[i].size(); ++j) { n         sum[i][j] = sum[i][j - 1] + d[i][j]; n       } n     } n   } n   map<pair<int, int>, double> ans; n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     x = find(x), y = find(y); n     if (x == y) { n       puts( -1 ); n       continue; n     } n     if (sz[x] > sz[y]) swap(x, y); n     if (ans.count(make_pair(x, y))) { n       printf( %.10lf n , ans[make_pair(x, y)]); n       continue; n     } n     long long res = 0; n     int k = max(mx[x], mx[y]); n     bool fst = 1; n     for (auto v : d[x]) { n       if (fst) { n         fst = 0; n         continue; n       } n       int p = lower_bound(d[y].begin(), d[y].end(), k - 1 - v) - d[y].begin(); n       p = max(p, 1); n       res += sum[y][sum[y].size() - 1] - sum[y][p - 1] + n              1LL * (v + 1) * (sum[y].size() - p); n       res += 1LL * (p - 1) * k; n     } n     double res2 = res * 1.0 / sz[x] / sz[y]; n     ans[make_pair(x, y)] = res2; n     printf( %.12lf n , res2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[1000000]; n int one_line[5001][5001] = {0}; n int summ[5001][5001] = {0}; n int fac[5001]; n int main() { n   int n, m, p; n   scanf( %d %d %d , &n, &m, &p); n   for (int i = 0; i < n; i++) scanf( %d , &l[i]); n   fac[0] = 1; n   for (int i = 1; i <= 5000; i++) fac[i] = ((fac[i - 1]) * (long long)i) % p; n   one_line[1][1] = 1; n   for (int i = 2; i <= 5000; i++) { n     for (int j = 1; j <= min(m, 5000); j++) { n       one_line[i][j] = n           (one_line[i - 1][j] * (long long)(j - 1) + one_line[i - 1][j - 1]) % n           p; n     } n   } n   summ[1][1] = m; n   for (int i = 2; i <= 5000; i++) { n     for (int j = 1; j <= min(m, 5000); j++) { n       summ[i][j] = (summ[i - 1][j] * (long long)(j - 1) + n                     summ[i - 1][j - 1] * (long long)(m - j + 1)) % n                    p; n     } n   } n   int prev_progress[5001] = {0}; n   int prev_l = 0; n   prev_progress[0] = 1; n   for (int i = 0; i < n; i++) { n     int next_progress[5001]; n     for (int j = 0; j <= l[i]; j++) next_progress[j] = 0; n     int tot1 = 0; n     for (int j = 0; j <= prev_l; j++) tot1 = (tot1 + prev_progress[j]) % p; n     int tot2 = 1; n     tot2 = (tot2 * (long long)m) % p; n     for (int j = 1; j < l[i]; j++) tot2 = (tot2 * (long long)(m - 1)) % p; n     for (int j = 1; j <= l[i]; j++) { n       next_progress[j] = (tot1 * (long long)summ[l[i]][j]) % p; n       int prev = 0; n       if (j <= prev_l) prev = prev_progress[j]; n       next_progress[j] -= n           (((prev * (long long)one_line[l[i]][j]) % p) * fac[j]) % p; n       if (next_progress[j] < 0) next_progress[j] += p; n     } n     for (int j = 0; j <= l[i]; j++) prev_progress[j] = next_progress[j]; n     prev_l = l[i]; n   } n   int res = 0; n   for (int i = 0; i <= prev_l; i++) res = (res + prev_progress[i]) % p; n   printf( %d n , res); n } n


#include <bits/stdc++.h> n const int N = (1 << 19) - 1; n using namespace std; n int n; n double dp[N + 1], a[20][20]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) scanf( %lf , &a[i][j]); n   } n   dp[(1 << n) - 1] = 1; n   for (int s = ((1 << n) - 1); s > 0; s--) { n     int c = 0; n     for (int i = 0; i < n; i++) { n       if ((1 << i) & s) c++; n     } n     if (c < 2) continue; n     for (int i = 0; i < n; i++) n       if ((1 << i) & (s)) n         for (int j = 0; j < n; j++) { n           if (i == j) continue; n           if ((1 << j) & s) { n             dp[s ^ (1 << i)] += dp[s] * a[j][i] / c / (c - 1); n             dp[s ^ (1 << j)] += dp[s] * a[i][j] / c / (c - 1); n           } n         } n   } n   for (int i = 1; i < (1 << n); i <<= 1) { n     printf( %.10f  , dp[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int pwr(long long int base, long long int p) { n   long long int ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % 1000000007; n     base = (base * base) % 1000000007; n     p /= 2; n   } n   return ans; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int n; n int dyn[5005][5005][2]; n char ch[5001][2]; n int dp(int neww, int val, int cont) { n   dyn[neww][val][cont] %= 1000000007; n   if (neww == n && !val) return 1; n   if (dyn[neww][val][cont] != -1) return dyn[neww][val][cont]; n   if (val < 0 || neww > n) return 0; n   int final = 0; n   if (ch[neww][0] !=  f ) { n     final += dp(neww + 1, val, 0); n     final = final % 1000000007; n   } else { n     final += dp(neww + 1, val + 1, 1); n     final = final % 1000000007; n   } n   if (cont == 0) { n     final += dp(neww, val - 1, 0); n     final = final % 1000000007; n   } n   final = final % 1000000007; n   return dyn[neww][val][cont] = final; n } n int main() { n   int i, j, k, m; n   memset(dyn, -1, sizeof dyn); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , &ch[i]); n   } n   int ans = dp(0, 0, 0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<pair<int, int> > v; n vector<int> query; n int ans; n struct SEG { n   SEG(int l, int r, int sum) : l(l), r(r), sum(sum) {} n   int l, r; n   int sum; n }; n vector<SEG> seg; n void init(int idx, int s, int e) { n   if (s == e) return; n   seg[idx].l = seg.size(); n   seg.push_back({-1, -1, 0}); n   seg[idx].r = seg.size(); n   seg.push_back({-1, -1, 0}); n   init(seg[idx].l, s, (s + e) / 2); n   init(seg[idx].r, (s + e) / 2 + 1, e); n } n void update(int idx, int s, int e, int x, int y) { n   seg[idx].sum += y; n   if (s == e) return; n   if (x <= (s + e) / 2) { n     update(seg[idx].l, s, (s + e) / 2, x, y); n   } else { n     update(seg[idx].r, (s + e) / 2 + 1, e, x, y); n   } n } n int calc(int idx, int s, int e, int x, int y) { n   if (x <= s && e <= y) return seg[idx].sum; n   if (x > e || y < s) return 0; n   return calc(seg[idx].l, s, (s + e) / 2, x, y) + n          calc(seg[idx].r, (s + e) / 2 + 1, e, x, y); n } n int main() { n   seg.push_back({-1, -1, 0}); n   scanf( %d , &N); n   init(0, 0, N - 1); n   for (int i = 0; i < N; i++) { n     int x; n     scanf( %d , &x); n     v.push_back({(x > 0) ? x : -x, i}); n     update(0, 0, N - 1, i, 1); n   } n   sort(v.begin(), v.end()); n   while (!v.empty()) { n     pair<int, int> now = v.back(); n     v.pop_back(); n     update(0, 0, N - 1, now.second, -1); n     query.push_back(now.second); n     while (!v.empty() && v.back().first == now.first) { n       query.push_back(v.back().second); n       update(0, 0, N - 1, v.back().second, -1); n       v.pop_back(); n     } n     while (!query.empty()) { n       int x = query.back(); n       query.pop_back(); n       ans += min(calc(0, 0, N - 1, 0, x - 1), calc(0, 0, N - 1, x + 1, N - 1)); n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_LEN = 1010; n using namespace std; n template <typename U, typename V> n string to_string(pair<U, V>); n string to_string(const string& e_) { return     + e_ +    ; } n string to_string(char e_) { return      + string(1, e_) +     ; } n string to_string(bool e_) { return e_ ?  true  :  false ; } n template <typename T> n string to_string(T e_) { n   string s_ =  [  ; n   for (const auto& x_ : e_) s_ += to_string(x_) +    ; n   return s_ +  ] ; n } n template <typename U, typename V> n string to_string(pair<U, V> e_) { n   return  (  + to_string(e_.first) +  ,   + to_string(e_.second) +  ) ; n } n void dbg_str() { ; } n template <typename U, typename... V> n void dbg_str(U u, V... v) { n   ; n   dbg_str(v...); n } n int n, m, x; n map<int, vector<int> > a; n map<vector<int>, vector<int> > b; n int main() { n   scanf( %d , &n); n   if (n == 2) { n     vector<int> l; n     scanf( %d %d , &m, &x); n     printf( 1 %d n , x); n     printf( %d  , --m); n     while (m--) { n       scanf( %d , &x); n       printf( %d  , x); n     } n     exit(0); n   } n   for (int i = 0; i < (n * (n - 1)) / 2; i++) { n     scanf( %d , &m); n     while (m--) { n       scanf( %d , &x); n       a[x].push_back(i); n     } n   }; n   for (auto& it : a) { n     b[it.second].push_back(it.first); n   } n   for (auto& it : b) { n     printf( %d  , (int)it.second.size()); n     for (int i = 0; i < (int)it.second.size(); i++) printf( %d  , it.second[i]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c; n   char s[15][15]; n   int cake = 0; n   int row = 0; n   scanf( %d %d , &r, &c); n   for (int i = 1; i <= r; i++) { n     scanf( %s , s[i]); n   } n   for (int i = 1; i <= r; i++) { n     for (int j = 0; j < c; j++) { n       if (s[i][j] ==  S ) n         break; n       else if (s[i][j] ==  .  && j == c - 1) { n         cake += c; n         row++; n       } n     } n   } n   for (int j = 0; j < c; j++) { n     for (int i = 1; i <= r; i++) { n       if (s[i][j] ==  S ) n         break; n       else if (s[i][j] ==  .  && i == r) n         cake += (r - row); n     } n   } n   printf( %d , cake); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n char str[105]; n ll best[105]; n ll memo[105][105][105]; n ll dp(int l, int r, int p) { n   if (l >= r) return 0; n   if (memo[l][r][p] != -1) return memo[l][r][p]; n   int ptr = l; n   while (ptr < r && str[ptr] == str[l]) ptr++; n   ll ret = best[p + ptr - l] + dp(ptr, r, 0); n   for (int i = ptr; i < r; ++i) { n     if (str[i] == str[l]) { n       ret = max(ret, dp(ptr, i, 0) + dp(i, r, p + ptr - l)); n     } n   } n   return memo[l][r][p] = ret; n } n int main() { n   int n; n   cin >> n >> str; n   for (int i = 1; i <= n; ++i) { n     cin >> best[i]; n     for (int j = 1; j < i; ++j) { n       best[i] = max(best[i], best[j] + best[i - j]); n     } n   } n   memset(memo, -1, sizeof memo); n   cout << dp(0, n, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n double eps = 1e-9; n const int INF = 1e9 + 7; n const int MAXN = int(3e5 + 7); n vector<pair<int, int> > v[MAXN]; n int n, p, a[MAXN]; n long long ans[MAXN], dp[MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   scanf( %d , &p); n   for (int i = 0; i < p; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[y].push_back(make_pair(x, i)); n   } n   for (int i = 1; i <= 500; i++) { n     for (int j = n; j > 0; j--) { n       dp[j] += a[j]; n       if (j + i <= n) dp[j] += dp[j + i]; n     } n     for (int j = 0; j < v[i].size(); j++) n       ans[v[i][j].second] = dp[v[i][j].first]; n     memset(dp, 0, sizeof(dp)); n   } n   for (int i = 501; i <= 300000; i++) { n     for (int j = 0; j < v[i].size(); j++) { n       long long cur = 0; n       for (int k = v[i][j].first; k <= n; k += i) cur += a[k]; n       ans[v[i][j].second] = cur; n     } n   } n   for (int i = 0; i < p; i++) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 16; n string s; n long long string_int(string s) { n   long long num = 0; n   for (int i = 0; i < s.size(); i++) { n     num += s[i] -  0 ; n     num *= 10; n   } n   return num / 10; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s; n   bool flag = false; n   if (s[0] ==  0 ) flag = true; n   int epos = s.find( e ); n   int d = string_int(s.substr(epos + 1)); n   s = s.substr(0, epos); n   for (int i = 0; i < 125; i++) s.push_back( 0 ); n   s.erase(1, 1); n   int exp = 1 + d; n   s.insert(exp,  . ); n   epos = s.find( e ); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  . ) { n       s = s.substr(i - 1); n       break; n     } else if (s[i] !=  0 ) { n       s = s.substr(i); n       break; n     } n   } n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  0 ) n       s.pop_back(); n     else n       break; n   } n   if (s[s.size() - 1] ==  . ) s.pop_back(); n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   while (cin >> n) { n     vector<int> v; n     int x; n     for (int i = 0; i < n; i++) { n       cin >> x; n       v.push_back(x); n     } n     bool in = false; n     int cnt = 0; n     bool f = true; n     bool s = false, l = false; n     int ind1 = 1, ind2 = 1; n     for (int i = 1; i < n; i++) { n       bool d = false; n       while (v[i - 1] > v[i] && i < n) { n         d = true; n         if (f) { n           ind1 = i; n           ind2 = i + 1; n           f = false; n         } else { n           ind2 = i + 1; n         } n         if (i - 1 == 0) s = true; n         if (i == n - 1) l = true; n         i++; n       } n       if (d) { n         d = false; n         cnt++; n       } n     } n     bool val = false; n     if (cnt == 0) n       val = 1; n     else if (cnt == 1) { n       if (s && l) n         val = true; n       else if (s && v[ind1 - 1] < v[ind2]) n         val = true; n       else if (l && v[ind2 - 1] > v[ind1 - 2]) n         val = true; n       else if (v[ind1 - 1] < v[ind2] && v[ind2 - 1] > v[ind1 - 2]) n         val = true; n     } n     if (val) { n       cout <<  yes  << endl; n       cout << ind1 <<     << ind2 << endl; n     } else { n       cout <<  no  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[300005]; n int rev[300005]; n int b[300005]; n int main() { n   int n, m; n   long long ans = 0; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     rev[a[i]] = i; n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x = rev[x]; n     y = rev[y]; n     b[max(x, y)] = max(b[max(x, y)], min(x, y)); n   } n   int t = 0; n   for (int i = 1; i <= n; i++) { n     t = max(t, b[i]); n     ans += i - t; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505, mod = 1e9 + 7; n int n, m, e[N][N], f[N][N], g[N][N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = (1); i <= (n); i++) n     for (int j = (1); j <= (n); j++) cin >> e[i][j]; n   for (int i = (1); i <= (n); i++) g[i][i] = f[i][i] = 1; n   for (int len = (1); len <= (n); len++) n     for (int l = (1); l <= (n - len + 1); l++) { n       int r = l + len - 1; n       for (int k = (l); k <= (r - 1); k++) n         if (e[l][r]) (g[l][r] += (long long)f[l][k] * f[k + 1][r] % mod) %= mod; n       for (int k = (l + 1); k <= (r); k++) n         (f[l][r] += (long long)g[l][k] * f[k][r] % mod) %= mod; n     } n   cout << f[1][n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n const int MAXM = 100010; n const int INF = 1000000000; n int dist[MAXN], dp[MAXN]; n pair<int, int> par[MAXN]; n bool marked[MAXM]; n vector<pair<int, int> > edges[MAXN]; n queue<int> q; n vector<pair<int, int> > v; n int x[MAXM], y[MAXM], z[MAXM]; n int n, m; n void bfs(int s) { n   for (int i = 0; i < n; i++) dist[i] = INF; n   dist[s] = 0; n   q.push(s); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int i = 0; i < edges[v].size(); i++) { n       int u = edges[v][i].first; n       if (dist[u] > dist[v] + 1) { n         dist[u] = dist[v] + 1; n         q.push(u); n       } n     } n   } n } n int main() { n   cin >> n >> m; n   int working = 0; n   for (int i = 0; i < m; i++) { n     cin >> x[i] >> y[i] >> z[i]; n     x[i]--, y[i]--; n     working += z[i]; n     edges[x[i]].push_back(pair<int, int>(y[i], i)); n     edges[y[i]].push_back(pair<int, int>(x[i], i)); n   } n   bfs(0); n   for (int i = 0; i < n; i++) { n     v.push_back(pair<int, int>(dist[i], i)); n     dp[i] = INF; n   } n   sort(v.begin(), v.end()); n   dp[0] = 0; n   for (int i = 0; i < n; i++) { n     int u = v[i].second; n     for (int j = 0; j < edges[u].size(); j++) { n       int q = edges[u][j].first; n       int w = 1 - z[edges[u][j].second]; n       if (dist[q] == dist[u] + 1 && dp[q] > dp[u] + w) { n         par[q] = pair<int, int>(u, edges[u][j].second); n         dp[q] = dp[u] + w; n       } n     } n   } n   int now = n - 1; n   while (now != 0) { n     marked[par[now].second] = true; n     now = par[now].first; n   } n   cout << working - dist[n - 1] + dp[n - 1] * 2 << endl; n   for (int i = 0; i < m; i++) n     if (marked[i] ^ z[i]) n       cout << x[i] + 1 <<     << y[i] + 1 <<     << 1 - z[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long inf = 0x3f3f3f3f3f3f3f3f; n const long long mod = 998244353; n const int N = 2e5 + 10; n mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); n int n; n int z[100][2]; n int qry(int i, int j) { n   cout <<  ?   << i <<     << j << endl; n   int ans; n   cin >> ans; n   return ans; n } n int p[N]; n int f(int i) { n   if (p[i] != -1) return p[i]; n   if (i != z[0][0]) n     p[i] = qry(i, z[0][0]); n   else n     p[i] = qry(i, z[0][1]); n   for (int j = 1; j < 11; j++) { n     if (i != z[j][0]) n       p[i] &= qry(i, z[j][0]); n     else n       p[i] &= qry(i, z[j][1]); n   } n   return p[i]; n } n int main() { n   scanf( %d , &n); n   memset(p, -1, sizeof(p)); n   while (1) { n     bool ok = 1; n     for (int i = 0; i < 11; i++) n       if (!z[i][0]) ok = 0; n     if (ok) break; n     int i = uniform_int_distribution<int>((1), (n))(mt), n         j = uniform_int_distribution<int>((1), (n))(mt); n     while (i == j) n       i = uniform_int_distribution<int>((1), (n))(mt), n       uniform_int_distribution<int>((1), (n))(mt); n     int tmp = qry(i, j); n     for (int u = 0; u < 11; u++) n       if (!(tmp >> u & 1)) z[u][0] = i, z[u][1] = j; n   } n   int idx = 1, val = f(idx); n   for (int i = 2; i <= n && val != 0; i++) { n     if (qry(i, idx) != val) continue; n     idx = i, val = f(i); n   } n   for (int i = 1; i <= n; i++) { n     if (p[i] == -1) p[i] = qry(idx, i); n   } n   cout <<  ! ; n   for (int i = 1; i <= n; i++) cout <<     << p[i]; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, T; n double dp[5001][5001]; n int p[5001], t[5001]; n double quickpow(double x, int y) { n   double res = 1; n   while (y) { n     if (y & 1) res *= x; n     x *= x; n     y >>= 1; n   } n   return res; n } n int main() { n   cin >> n >> T; n   dp[0][0] = 1; n   for (int i = 0; i < n; i++) scanf( %d%d , &p[i], &t[i]); n   double ans = 0; n   for (int i = 0; i < n; i++) { n     double pp = p[i] * 0.01; n     double now = 0; n     double r = quickpow(1 - pp, t[i] - 1) * pp; n     double r2 = quickpow(1 - pp, t[i] - 1); n     for (int j = 0; j < T; j++) { n       now *= (1 - pp); n       now += dp[i][j] * pp; n       if (j + 1 >= t[i]) { n         now -= dp[i][j + 1 - t[i]] * r; n         dp[i + 1][j + 1] = now + dp[i][j + 1 - t[i]] * r2; n       } else { n         dp[i + 1][j + 1] = now; n       } n     } n     for (int j = 0; j <= T; j++) ans += dp[i + 1][j]; n   } n   printf( %.12f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 2; n int n, l; n long long a[N], f[N]; n int main() { n   scanf( %d %d , &n, &l); n   for (int i = 1; i < n; ++i) scanf( %lld , &a[i]), a[i] += a[i - 1]; n   long long ans = LLONG_MAX; n   for (int i = l; i < n; ++i) ans = min(ans, a[i] - a[i - l]); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, j, ans = 0; n long long a, b; n const long long M = (long long)1E9 + 7; n long long power(long long x, long long y) { n   long long res = 1; n   x %= M; n   while (y) { n     if (y % 2) res = (res * x) % M; n     y = y >> 1; n     x = (x * x) % M; n   } n   return res % M; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   b = power(2LL, (long long)s.length() - 1); n   long long x = 1; n   for (i = (long long)s.length() - 1; i >= 0; i--) { n     if (s[i] ==  1 ) a += x; n     x *= 2; n     if (x > M) x %= M; n     a %= M; n   } n   cout << (a * b) % M; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n map<long long, int> ma; n int main() { n   long long b1, q, m, l; n   cin >> b1 >> q >> l >> m; n   while (m--) { n     long long x; n     cin >> x; n     ma[x] = 1; n   } n   int ans = 0, i; n   for (i = 1; i < 100; i++) { n     if (abs(b1) > l) break; n     if (!ma[b1]) ans++; n     b1 *= q; n   } n   if (ans < 49) n     cout << ans << endl; n   else n     cout <<  inf  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int nuts, divis, maxsec, capacity, boxees = 0; n   cin >> maxsec >> nuts >> divis >> capacity; n   while (nuts > 0) { n     boxees++; n     nuts -= ((min(maxsec, divis + 1)) * capacity); n     divis -= ((min(maxsec, divis + 1)) - 1); n   } n   cout << boxees << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int vis[100010], h[100100]; n vector<int> res; n set<int> q, off; n int a[100100]; n char s[100010][2]; n int main() { n   int i, j, z, T, d; n   while (cin >> n >> m) { n     q.clear(); n     res.clear(); n     off.clear(); n     memset(vis, 0, sizeof(vis)); n     for (i = 1; i <= m; i++) { n       scanf( %s%d , s[i], &a[i]); n       if (s[i][0] ==  -  && !vis[a[i]]) q.insert(a[i]); n       if (s[i][0] ==  +  && !vis[a[i]]) off.insert(a[i]); n       vis[a[i]]++; n     } n     memset(vis, 0, sizeof(vis)); n     if (q.size() != 0) { n       set<int>::iterator it; n       for (it = off.begin(); it != off.end(); it++) vis[*it]++; n       off.clear(); n     } n     for (i = 1; i <= m; i++) { n       if (s[i][0] ==  +  && q.size() != 0) { n         set<int>::iterator it; n         for (it = off.begin(); it != off.end(); it++) vis[*it]++; n         off.clear(); n       } n       if (s[i][0] ==  - ) { n         q.erase(a[i]); n         off.insert(a[i]); n       } else { n         if (off.count(a[i]) != 0) off.erase(a[i]); n         q.insert(a[i]); n       } n       if (q.size() != 0) { n         set<int>::iterator it; n         for (it = off.begin(); it != off.end(); it++) vis[*it]++; n         off.clear(); n       } n     } n     for (i = 1; i <= n; i++) { n       if (!vis[i]) res.push_back(i); n     } n     printf( %d n , res.size()); n     for (i = 0; i < res.size(); i++) { n       if (i) printf(   ); n       printf( %d , res[i]); n     } n     if (res.size() != 0) puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n long long fac[N], infac[N]; n int n, m; n long long poww(long long b, long long p) { n   if (p == 0) return 1; n   if (p & 1) return (b * poww(b, p - 1)) % m; n   return poww((b * b) % m, p / 2); n } n long long npk(int n, int k) { return (fac[n] * infac[n - k]) % m; } n long long nck(int n, int k) { n   return (((fac[n] * infac[n - k]) % m) * infac[n - k]) % m; n } n int main() { n   cin >> n >> m; n   fac[0] = infac[0] = 1; n   for (int i = 1; i <= n; i++) { n     fac[i] = (i * fac[i - 1]) % m; n     infac[i] = poww(fac[i], m - 2); n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long a = n - i + 1; n     a = (a * fac[i]) % m; n     ans = (ans + a * fac[n - i + 1]) % m; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int result; n   string input; n   cin >> input; n   set<string> sinput; n   sinput.insert(input); n   for (int i = input.size() - 1; i >= 0; --i) { n     input = input[input.size() - 1] + input; n     input.pop_back(); n     sinput.insert(input); n   } n   result = sinput.size(); n   printf( %d n , result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n long long n, m, A, cf, cm, maxForce = -1, minSkill, minIdx, i, a[N], b[N]; n pair<long long, int> sum[N]; n inline long long query(long long money, int limit) { n   long long l = sum[1].first, r = A, mid; n   if (limit <= 1) { n     return A; n   } n   while (l < r) { n     mid = (l + r + 1) / 2; n     int x = upper_bound(b + 1, b + limit, mid) - b - 1; n     if (mid * x - sum[x].first <= money) n       l = mid; n     else n       r = mid - 1; n   } n   return r; n } n int main() { n   cin >> n >> A >> cf >> cm >> m; n   for (i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     sum[i].first = a[i]; n     sum[i].second = i; n   } n   sort(sum + 1, sum + n + 1); n   for (i = 1; i <= n; ++i) { n     b[i] = sum[i].first; n     sum[i].first += sum[i - 1].first; n   } n   b[n + 1] = A; n   for (i = n + 1; i >= 1; --i) { n     m -= A - b[i]; n     if (m < 0) { n       break; n     } n     long long x = query(m, i); n     long long force = cf * (n - i + 1) + cm * x; n     if (maxForce < force) { n       maxForce = force; n       minIdx = i; n       minSkill = x; n     } n   } n   for (i = n; i >= minIdx; --i) { n     a[sum[i].second] = A; n   } n   cout << maxForce << endl; n   for (i = 1; i <= n; ++i) { n     printf( %d  , max(a[i], minSkill)); n   } n   return 0; n } n


#include <bits/stdc++.h> n   n  #define dumb ios_base::sync_with_stdio(false);cin.tie(NULL); n      n  typedef  long long ll; n  n   using namespace std; n  n #define modulo 1000000007; n  n bool isprime(long long n) n { n     for(long long int i=2;i*i<=n;i++) n     { n         if(n%i==0) n             return 0; n     } n     return 1; n } n long long gcd(long long int a, long long int b) n { n   if (b == 0) n     return a; n   return gcd(b, a % b); n } n  n // Function to return LCM of two numbers n long long lcm(int a, int b) n { n     return (a / gcd(a, b)) * b; n } n  n  n ll ncr(ll n,ll k) n { n     ll res = 1; n     if (k > n - k) n         k = n - k; n     for (ll i = 0; i < k; ++i) { n         res *= (n - i); n         res /= (i + 1); n     } n     return res; n } n  n map<int,int> m; n  n void solve() n { n   int n; n   cin>>n; n  n   auto it=m.lower_bound(n); n   if(it->first==n) n   { n     cout<<it->second<<endl; n     return; n   } n   else n   { n     int diff=it->first-n; n     if(diff==1) n       cout<<it->second+1<<endl; n     else n       cout<<it->second<<endl; n      n     return; n   } n  n return;  n } n  n  n  n  n  n  n int main() n { n   dumb; n     int t=1; n     cin>>t; n     int i=1; n     m[0]=0; n     int sum=0; n     while(1) n     { n       m[sum+i]=i; n       if(sum+i>1000000) n         break; n       sum+=i; n       i++; n     } n     while(t--) n     { n       //cout<< in ; n        solve();  n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename T, size_t N> n void PRINT_ARRAY(const T (&a)[N], int n = -1) { n   if (!n) n = N; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n template <typename T, size_t N, size_t M> n void PRINT_ARRAY(const T (&a)[N][M], int n, int m) { n   for (int i = 0; i < n; i++) PRINT_ARRAY(a[i], m); n   cout <<   n ; n } n long long arr[3005], cost[3005], lessc, more, ans; n int n; n void solve() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < n; i++) cin >> cost[i]; n   ans = (int)(1e9 + 7); n   for (int i = 1; i < n - 1; i++) { n     lessc = (int)(1e9 + 7); n     more = (int)(1e9 + 7); n     for (int j = 0; j < i; j++) n       if (arr[i] > arr[j]) lessc = min(lessc, cost[j]); n     for (int j = i + 1; j < n; j++) n       if (arr[j] > arr[i]) more = min(more, cost[j]); n     ans = min(ans, cost[i] + lessc + more); n   } n   cout << (ans == (int)(1e9 + 7) ? -1 : ans); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n bitset<2020> mat[2020]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &N, &M); n     vector<pair<int, int> > E; n     int deg[2020] = {}; n     for (int i = 1; i <= M; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       deg[x]++; n       deg[y]++; n       E.push_back(pair<int, int>(x, y)); n     } n     int ok = 1; n     for (int i = 1; i <= N; i++) n       if (deg[i] & 1) ok = 0; n     if (ok) { n       puts( 1 ); n       for (int i = 1; i <= N; i++) printf( 1  ); n       puts(  ); n     } else { n       for (int i = 1; i <= N; i++) mat[i].reset(); n       for (int i = 1; i <= N; i++) n         if (deg[i] & 1) mat[i][i] = 1; n       for (pair<int, int> e : E) { n         int x = e.first, y = e.second; n         mat[x][y] = !mat[x][y]; n         mat[y][x] = !mat[y][x]; n       } n       int V[2020] = {}; n       for (int i = 1; i <= N; i++) V[i] = (deg[i] & 1); n       int now = 1; n       int base[2020]; n       for (int i = 1; i <= N; i++) { n         int f = -1; n         for (int j = now; j <= N; j++) n           if (mat[j][i]) { n             f = j; n             break; n           } n         if (f == -1) continue; n         swap(mat[f], mat[now]); n         swap(V[f], V[now]); n         for (int j = now + 1; j <= N; j++) { n           if (mat[j][i]) { n             mat[j] ^= mat[now]; n             V[j] ^= V[now]; n           } n         } n         base[now] = i; n         ++now; n       } n       --now; n       int res[2020] = {}; n       for (int i = now; i; i--) { n         int p = base[i]; n         res[p] = V[i]; n         if (res[p]) { n           for (int j = 1; j < i; j++) n             if (mat[j][p]) V[j] ^= 1; n         } n       } n       puts( 2 ); n       for (int i = 1; i <= N; i++) printf( %d  , res[i] + 1); n       puts(  ); n     } n   } n } n