#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n struct node { n   int flag, a, b; n   long long sum; n } t[maxn * 8]; n int a[maxn], cnt[maxn * 2], js[maxn * 2]; n void pushdown(int x, int l, int r) { n   if (t[x].flag) { n     int num = t[x].flag; n     t[2 * x].sum += num * l; n     t[2 * x + 1].sum += num * r; n     t[2 * x].flag += num; n     t[2 * x + 1].flag += num; n     t[x].flag = 0; n   } n } n int ask(int x, int l, int r, int pos) { n   int mid; n   if (l == r) { n     return t[x].sum; n   } n   mid = (l + r) / 2; n   pushdown(x, mid - l + 1, r - mid); n   if (pos <= mid) n     return ask(2 * x, l, mid, pos); n   else n     return ask(2 * x + 1, mid + 1, r, pos); n } n void add(int x, int l, int r, int ql, int qr, int val) { n   int mid; n   if (ql == l && qr == r) { n     t[x].flag += val; n     t[x].sum += (qr - ql + 1); n     return; n   } n   mid = (l + r) / 2; n   pushdown(x, mid - l + 1, r - mid); n   if (qr <= mid) { n     add(2 * x, l, mid, ql, qr, val); n   } else { n     if (ql > mid) n       add(2 * x + 1, mid + 1, r, ql, qr, val); n     else { n       add(2 * x, l, mid, ql, mid, val); n       add(2 * x + 1, mid + 1, r, mid + 1, qr, val); n     } n   } n   t[x].sum = t[2 * x].sum + t[2 * x + 1].sum; n } n void build(int x, int l, int r) { n   int mid; n   t[x].flag = 0; n   t[x].sum = 0; n   if (l == r) { n     return; n   } n   mid = (l + r) / 2; n   build(2 * x, l, mid); n   build(2 * x + 1, mid + 1, r); n } n int top; n node s[maxn]; n int cmp(node a, node b) { return a.sum < b.sum; } n int main() { n   node e; n   int T, n, flag, ans, k, up; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= 2 * k; i++) { n       cnt[i] = 0; n       js[i] = 0; n     } n     build(1, 1, k * 2); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     if (n == 2) { n       printf( 0 n ); n       continue; n     } n     top = 0; n     if (n % 2) n       up = n / 2 + 1; n     else n       up = n / 2; n     for (int i = 1; i <= up; i++) { n       e.a = a[i]; n       e.b = a[n - i + 1]; n       if (e.a > e.b) swap(e.a, e.b); n       e.sum = e.a + e.b; n       s[++top] = e; n       js[e.sum]++; n     } n     sort(s + 1, s + top + 1, cmp); n     int last = 0; n     ans = n; n     for (int i = 1; i <= top; i++) { n       e = s[i]; n       if (e.sum == last) { n         add(1, 1, 2 * k, e.a + 1, e.b + k, 1); n         continue; n       } n       cnt[e.sum] += n / 2 - js[e.sum] + i - 1 - ask(1, 1, 2 * k, e.sum); n       add(1, 1, 2 * k, e.a + 1, e.b + k, 1); n       last = e.sum; n     } n     for (int i = 1; i <= top / 2; i++) { n       swap(s[i], s[top - i + 1]); n     } n     build(1, 1, k * 2); n     last = 0; n     ans = n; n     for (int i = 1; i <= top; i++) { n       e = s[i]; n       if (e.sum == last) { n         add(1, 1, 2 * k, e.a + 1, e.b + k, 1); n         continue; n       } n       cnt[e.sum] += i - 1 - ask(1, 1, 2 * k, e.sum); n       ans = min(ans, cnt[e.sum]); n       add(1, 1, 2 * k, e.a + 1, e.b + k, 1); n       last = e.sum; n     } n     for (int i = 1; i <= 2 * k; i++) { n       if (js[i]) continue; n       ans = min(ans, n - ask(1, 1, 2 * k, i)); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100001], b[100001]; n vector<long long> v[100001]; n signed main() { n   cin >> n; n   long long i; n   for (i = 1; i < n + 1; i++) { n     cin >> a[i]; n     v[a[i]].push_back(i); n   } n   bool ok = 1; n   long long clr = 1; n   for (i = 0; i < 100001; i++) { n     if (!v[i].empty()) { n       long long k = v[i].size(); n       long long j; n       if (k % (n - i) != 0) { n         ok = 0; n         break; n       } n       long long t = k / (n - i); n       long long temp = 1; n       j = 0; n       while (temp <= t) { n         while (j < temp * (n - i)) { n           b[v[i][j]] = clr; n           j++; n         } n         clr++; n         temp++; n       } n     } n   } n   if (ok) { n     cout <<  Possible n ; n     for (i = 1; i < n + 1; i++) cout << b[i] <<    ; n   } else n     cout <<  Impossible ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n const long long maxn = 1000001, mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int ar[n]; n   map<int, int> mapp; n   for (long long i = 0; i < (long long)(n); ++i) { n     cin >> ar[i]; n     mapp[ar[i]] = i; n   } n   set<int> s; n   s.insert(-1); n   s.insert(n); n   int ans = 0; n   for (int i = n; i >= 1; i--) { n     int idx = mapp[i]; n     auto it = s.lower_bound(idx); n     auto iit = s.upper_bound(idx); n     if (it != s.begin()) it--; n     int a = *it + 1; n     int b = *iit - 1; n     if (idx - a < b - idx) { n       for (long long j = (long long)(a); j <= (long long)(idx - 1); ++j) { n         int acha = mapp[i - ar[j]]; n         if (acha > idx && acha <= b) { n           ans++; n         } n       } n     } else { n       for (long long j = (long long)(idx + 1); j <= (long long)(b); ++j) { n         int acha = mapp[i - ar[j]]; n         if (acha < idx && acha >= a) { n           ans++; n         } n       } n     } n     s.insert(mapp[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Trie { n   Trie *nx[2]; n   int cnt; n   Trie() { n     nx[0] = nx[1] = nullptr; n     cnt = 0; n   } n }; n Trie *root; n void ins(long long x) { n   Trie *now = root; n   for (int bit = 45; bit >= 0; --bit) { n     int p = (x >> bit) & 1LL; n     if (now->nx[p] == nullptr) now->nx[p] = new Trie(); n     now = now->nx[p]; n     now->cnt++; n   } n } n long long query(long long x) { n   Trie *now = root; n   long long ret = 0LL; n   for (int bit = 45; bit >= 0; --bit) { n     int p = (x >> bit) & 1LL; n     p ^= 1; n     if (now->nx[p] != nullptr && now->nx[p]->cnt > 0) { n       ret += 1LL << bit; n       now = now->nx[p]; n     } else { n       now = now->nx[p ^ 1]; n     } n     if (now == nullptr || now->cnt == 0) break; n   } n   return ret; n } n const int MXSZ = 1e5; n int n; n long long a[MXSZ + 5]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   root = new Trie(); n   cin >> n; n   long long curr = 0LL; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     curr ^= a[i]; n     ins(curr); n   } n   long long mx = 0LL; n   mx = max(mx, query(0)); n   curr = 0LL; n   for (int i = n; i >= 1; --i) { n     curr ^= a[i]; n     mx = max(mx, query(curr)); n   } n   cout << mx <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int b[5000]; n int f[5000]; n int main() { n   scanf(  %d , &n); n   for (int i = 0; i < (int)(n); i++) scanf(  %d , b + i); n   for (int i = 0; i < (int)(n); i++) b[i]--; n   for (int i = 0; i < n; i++) { n     int c[5000] = {}; n     pair<int, int> best = make_pair(0, 0); n     for (int j = i; j < n; j++) { n       c[b[j]]++; n       best = max(best, make_pair(c[b[j]], -b[j])); n       f[-best.second]++; n     } n   } n   for (int i = 0; i < (int)(n); i++) printf( %d  , f[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int maxn = 3000010; n int n, b; n int pre[maxn], vis[maxn]; n vector<int> prime; n struct init { n   init() { n     memset(pre, 0, sizeof pre); n     prime.clear(); n     for (int i = 2; i < maxn; i++) { n       if (pre[i] == 0) { n         pre[i] = i; n         prime.emplace_back(i); n       } n       for (int j = 0; j < prime.size() && i * prime[j] < maxn; j++) { n         if (prime[j] > pre[i]) break; n         pre[i * prime[j]] = prime[j]; n       } n     } n   } n } init_; n bool ok(int x) { n   if (x == 1) return 0; n   return pre[x] == 0; n } n signed main() { n   while (cin >> n) { n     memset(vis, 0, sizeof vis); n     for (int i = 1; i <= 2 * n; i++) { n       scanf( %d , &b); n       vis[b]++; n     } n     for (int i = maxn - 1; i > 1; i--) { n       if (pre[i] == i) continue; n       while (vis[i] > 0) { n         printf( %d  , i); n         vis[i]--; n         vis[i / pre[i]]--; n       } n     } n     for (int i = 2; i < maxn; i++) { n       while (vis[i] > 0) { n         printf( %d  , i); n         vis[i]--; n         vis[prime[i - 1]]--; n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   reverse(s.begin(), s.end()); n   cout << s[0]; n   for (long long int(i) = (1); (i) < (s.length()); (i)++) { n     if (s[i] == s[i - 1]) n       cout << s[i]; n     else n       break; n   } n   cout <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool KT(long long k, long long n) { n   long long take = 0, cur = n; n   while (cur > 0) { n     long long temp = min(k, cur); n     take += temp; n     cur -= temp; n     cur -= cur / 10; n   } n   if (take * 2 >= n) return true; n   return false; n } n int main() { n   long long n; n   cin >> n; n   long long left = 1, right = n, res = 0; n   while (left <= right) { n     long long mid = (left + right) / 2; n     if (KT(mid, n)) { n       res = mid; n       right = mid - 1; n     } else { n       left = mid + 1; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct in { n   int x, y; n } a[300000]; n int get(int prex, int prey, int x, int y, int k) { n   int num, ans = 100000000; n   if (prex <= k) { n     num = (prex + x + k - 1) / k - 1; n     if (y == num) n       ans = prex + x - num * k; n     else if (y > num && y <= (long long)x * k) n       ans = 1; n   } n   if (prey <= k) { n     num = (x + k - 1) / k - 1; n     if (y == num) n       ans = min(ans, x - num * k); n     else if (y > num && y - k + prey <= (long long)(x - 1) * k) n       ans = 1; n   } n   return ans; n } n int main() { n   int N, K, pre0 = 0, pre1 = 0, next0, next1; n   scanf( %d%d , &N, &K); n   for (int i = 1; i <= N; i++) scanf( %d , &a[i].x); n   for (int i = 1; i <= N; i++) scanf( %d , &a[i].y); n   for (int i = 1; i <= N; i++) { n     next0 = get(pre0, pre1, a[i].x, a[i].y, K); n     next1 = get(pre1, pre0, a[i].y, a[i].x, K); n     pre0 = next0; n     pre1 = next1; n   } n   if (pre0 <= K || pre1 <= K) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n float a1(float x, float y, float z) { n   if (x >= 1.0) n     return z * log(y) + log(log(x)); n   else if (x < 1.0 && y < 1.0 && z < 1.0) n     return z * log(y) + log(log(1 / x)); n   else n     return 0.0 / 0.0; n } n float a2(float x, float y, float z) { n   if (x >= 1.0) n     return log(z) + log(y) + log(log(x)); n   else if (x < 1.0 && y < 1.0 && z < 1.0) n     return log(z) + log(y) + log(log(1 / x)); n   else n     return 0.0 / 0.0; n } n int main() { n   float x, y, z; n   scanf( %f %f %f , &x, &y, &z); n   float a[12]; n   char pat[12][10] = { x^y^z 0 ,  x^z^y 0 ,  (x^y)^z 0 ,  (x^z)^y 0 , n                        y^x^z 0 ,  y^z^x 0 ,  (y^x)^z 0 ,  (y^z)^x 0 , n                        z^x^y 0 ,  z^y^x 0 ,  (z^x)^y 0 ,  (z^y)^x 0 }; n   a[0] = a1(x, y, z); n   a[1] = a1(x, z, y); n   a[2] = a2(x, y, z); n   a[3] = a2(x, z, y); n   a[4] = a1(y, x, z); n   a[5] = a1(y, z, x); n   a[6] = a2(y, x, z); n   a[7] = a2(y, z, x); n   a[8] = a1(z, x, y); n   a[9] = a1(z, y, x); n   a[10] = a2(z, x, y); n   a[11] = a2(z, y, x); n   float ans; n   int pos, i; n   if (x >= 1.0) { n     ans = a[0]; n     pos = 0; n   } else if (y >= 1.0) { n     ans = a[4]; n     pos = 4; n   } else { n     ans = a[8]; n     pos = 8; n   } n   if (x < 1 && y < 1 && z < 1) { n     for (i = 11; i >= 0; --i) { n       if (a[i] <= ans) { n         ans = a[i]; n         pos = i; n       } n     } n   } else { n     for (i = 11; i >= 0; --i) { n       if (a[i] >= ans) { n         ans = a[i]; n         pos = i; n       } n     } n   } n   printf( %s , pat[pos]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vi; n void primefact(int n) { n   while (n % 2 == 0) { n     vi.push_back(2); n     n /= 2; n   } n   int i = 3; n   while (i <= n) { n     while (n % i == 0) { n       vi.push_back(i); n       n /= i; n     } n     i += 2; n   } n   if (n > 2) vi.push_back(n); n } n int main() { n   int n, k, last = 1, i; n   cin >> n >> k; n   primefact(n); n   if (k > vi.size()) n     cout <<  -1  << endl; n   else { n     for (i = 0; i < k - 1; i++) cout << vi[i] <<    ; n     for (i = k - 1; i < vi.size(); i++) last *= vi[i]; n     cout << last; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const long long INF = 1e9; n const long long MOD = 1000000007; n int prevs[20]; n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int ar[n][20]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 20; j++) ar[i][j] = n + 3; n   for (int i = n - 1; i >= 0; i--) { n     int x = 1; n     for (int j = 0; j < 20; j++) { n       if ((x << j) & arr[i]) { n         ar[i][j] = i; n         if (prevs[j] != 0) n           for (int k = 0; k < 20; k++) n             ar[i][k] = min(ar[i][k], ar[prevs[j]][k]); n         prevs[j] = i; n       } n     } n   } n   while (q--) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     if (!arr[x] || !arr[y]) { n       cout <<  Fou  << endl; n       continue; n     } n     int p = 1; n     int l = 0; n     for (int j = 0; j < 20; j++) n       if (p << j & arr[y]) n         if (ar[x][j] <= y) { n           l = 1; n           continue; n         } n     if (l == 1) n       cout <<  Shi  << endl; n     else n       cout <<  Fou  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[1000005]; n int main() { n   int i, j, l, r, p = 0; n   scanf( %s , s); n   n = strlen(s); n   for (j = 0; j < n && s[j] !=  . ; j++) n     ; n   if (j == n) { n     s[n] =  . ; n     n++; n   } n   for (i = 0; i < n && s[i] ==  0 ; i++) n     ; n   if (i == j) { n     for (; i < n - 1 && s[i + 1] ==  0 ; i++) swap(s[i], s[i + 1]), p--; n     if (i == n - 1) { n       printf( 0 n ); n       return 0; n     } n     swap(s[i], s[i + 1]), p--; n     for (r = n - 1; s[r] ==  0 ; r--) n       ; n     if (s[r] ==  . ) r--; n     for (; i <= r; i++) printf( %c , s[i]); n     if (p != 0) printf( E%d n , p); n   } else { n     for (; j > i + 1; j--) swap(s[j], s[j - 1]), p++; n     for (r = n - 1; s[r] ==  0 ; r--) n       ; n     if (s[r] ==  . ) r--; n     for (; i <= r; i++) printf( %c , s[i]); n     if (p != 0) printf( E%d n , p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1000 + 10; n const int INF = 0x7f7f7f7f; n const double eps = 1e-10; n const double pi = acos(-1.0); n inline int realcmp(double a, double b) { n   return (a > b + eps) ? 1 : ((a + eps < b) ? -1 : 0); n } n int main() { n   double hh, mm; n   ios::sync_with_stdio(0); n   scanf( %lf:%lf , &hh, &mm); n   hh += mm / 60; n   while (realcmp(hh, 12) >= 0) hh -= 12; n   printf( %.9lf  , 30.0 * hh); n   printf( %.9lf n , 6.0 * mm); n   return 0; n } n


#include <bits/stdc++.h> n struct point { n   long x; n   long y; n   long z; n }; n const long maxn = 5001; n const double inf = 2000000000; n const double eps = 1.0e-8; n point a[maxn]; n double nnew, min; n long n, q, choice1, choice2; n double d[maxn]; n double sqr(double x) { return (x * x); } n double dist(point a, point b) { n   return (pow(sqr(a.x - b.x) + sqr(a.y - b.y) + sqr(a.z - b.z), 0.5)); n } n int main() { n   scanf( %ld , &n); n   for (q = 1; q <= n; q++) scanf( %ld%ld%ld , &a[q].x, &a[q].y, &a[q].z); n   min = inf; n   for (long h = 2; h <= n; h++) d[h] = dist(a[1], a[h]); n   for (choice1 = 2; choice1 <= n; choice1++) n     for (choice2 = choice1 + 1; choice2 <= n; choice2++) { n       nnew = (d[choice1] + d[choice2] + dist(a[choice1], a[choice2])) / 2; n       if (min - nnew > eps) min = nnew; n     } n   printf( %0.8lf , min); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, i, y, ans = 0, q; n   scanf( %d%d , &n, &x); n   y = int(sqrt(x)); n   if (n < y) y = n; n   for (i = 1; i <= y; i++) { n     if (x % i == 0) { n       q = x / i; n       if (q <= n) { n         if (i == q) n           ans++; n         else n           ans += 2; n       } n     } n   } n   if (x == 1) n     printf( %d n , 1); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e9; n struct Data { n   int p1, p2, p3, p4; n   Data(int a = mx, int b = mx, int c = mx, int d = mx) { n     p1 = a, p2 = b, p3 = c, p4 = d; n   } n }; n inline Data UN(Data a, Data b) { n   Data c; n   c.p1 = min(a.p1 + b.p1, a.p2 + b.p3), c.p2 = min(a.p1 + b.p2, a.p2 + b.p4); n   c.p3 = min(a.p3 + b.p1, a.p4 + b.p3), c.p4 = min(a.p3 + b.p2, a.p4 + b.p4); n   c.p1 = min(c.p1, mx), c.p2 = min(c.p2, mx), c.p3 = min(c.p3, mx), n   c.p4 = min(c.p4, mx); n   return c; n } n struct node { n   int l, r; n   Data s; n } T[200010 << 2]; n char G[3][200010]; n void build(int rt, int l, int r) { n   T[rt].l = l, T[rt].r = r; n   int mid = (l + r) >> 1; n   if (l == r) { n     if (G[1][l] ==  . ) T[rt].s.p1 = 1; n     if (G[2][l] ==  . ) T[rt].s.p4 = 1; n     if (G[1][l] ==  .  && G[2][l] ==  . ) T[rt].s.p2 = T[rt].s.p3 = 2; n     return; n   } n   build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r); n   T[rt].s = UN(T[rt << 1].s, T[rt << 1 | 1].s); n } n Data query(int rt, int start, int end) { n   int l = T[rt].l, r = T[rt].r, mid = (l + r) >> 1; n   if (start <= l && r <= end) return T[rt].s; n   if (end <= mid) return query(rt << 1, start, end); n   if (start > mid) return query(rt << 1 | 1, start, end); n   return UN(query(rt << 1, start, end), query(rt << 1 | 1, start, end)); n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   scanf( %s%s , G[1] + 1, G[2] + 1); n   build(1, 1, n); n   while (q--) { n     int a, b; n     scanf( %d%d , &a, &b); n     int l = a > n ? a - n : a, r = b > n ? b - n : b, res; n     if (l > r) swap(l, r), swap(a, b); n     Data tmp = query(1, l, r); n     if (a <= n && b <= n) res = tmp.p1; n     if (a <= n && b > n) res = tmp.p2; n     if (a > n && b <= n) res = tmp.p3; n     if (a > n && b > n) res = tmp.p4; n     if (res < 1e8) n       printf( %d n , res - 1); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, x, y, a, b; n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld %lld %lld %lld , &x, &y, &a, &b); n     bool yes = false; n     int i = 0; n     int tens = 1000000; n     while (x <= y) { n       if (x == y) { n         yes = true; n         break; n       } n       while (x + a * tens > y - b * tens && tens > 1) tens /= 10; n       x += tens * a; n       y -= tens * b; n       i += tens; n     } n     printf( %d n , yes ? i : -1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(228); n const int N = 404; n int mod; n int fact[N], d0[N], d[N], d2[N][N]; n int main() { n   int qq; n   cin >> qq >> mod; n   fact[0] = 1; n   for (int i = 1; i < N; ++i) fact[i] = (1LL * fact[i - 1] * i) % mod; n   d[1] = 1; n   d0[1] = 1; n   for (int n = 2; n < N; ++n) { n     d0[n] = fact[n]; n     for (int k = 1; k < n; ++k) n       d0[n] = (d0[n] - 1LL * d0[k] * fact[n - k]) % mod; n     if (d0[n] < 0) d0[n] += mod; n   } n   d2[0][0] = 1; n   for (int n = 1; n < N; ++n) { n     d2[n][1] = fact[n]; n     for (int k = 2; k <= n; ++k) n       for (int x = 1; x < n; ++x) n         d2[n][k] = (d2[n][k] + 1LL * fact[x] * d2[n - x][k - 1]) % mod; n   } n   for (int n = 2; n < N; ++n) { n     int dz = 0; n     for (int x = 1; x < n - 1; ++x) n       for (int y = x; y < n - 1; ++y) { n         int c = fact[y - x + 1]; n         c = (1LL * c * d0[x]) % mod; n         c = (1LL * c * d0[n - y - 1]) % mod; n         dz += c; n         if (dz >= mod) dz -= mod; n       } n     dz = (dz * 2) % mod; n     int cd = fact[n]; n     cd = (cd - dz + mod) % mod; n     for (int k = 3; k < n; ++k) { n       cd = (cd - 1LL * d2[n][k] * d[k]) % mod; n     } n     if (n != 2) { n       for (int x = 1; x < n; ++x) cd = (cd - 1LL * d0[x] * d0[n - x]) % mod; n       for (int x = 1; x < n; ++x) cd = (cd - 1LL * d0[x] * d0[n - x]) % mod; n     } n     cd = (cd + mod) % mod; n     d[n] = cd; n   } n   while (qq--) { n     int x; n     cin >> x; n     cout << d[x] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2020; n vector<tuple<int, int, int> > edges; n vector<int> g[N]; n vector<int> back_edge[N]; n bool global; n int num[N], low[N], low2[N], cnt, pai[N], pw[N], aux[N], prev_pai[N], n     prev_pw[N], sobe[N], prev_sobe[N]; n int edg_used, edg_u, edg_v, edg_w; n void dfs(int u) { n   num[u] = low[u] = ++cnt; n   bool flag = false; n   for (auto w : g[u]) { n     int x, v, p; n     tie(x, v, p) = edges[w]; n     if (v == u) swap(v, x); n     if (edg_used == w) continue; n     if (!num[v]) { n       pai[v] = u; n       pw[v] = p; n       sobe[v] = w; n       dfs(v); n       low[u] = min(low[u], low[v]); n     } else if (flag or pai[u] != v) { n       low[u] = min(low[u], num[v]); n       if (!global and num[u] > num[v]) back_edge[u].push_back(w); n     } else if (pai[u] == v) n       flag = true; n   } n } n int main(void) { n   int n, m; n   scanf( %d %d , &n, &m); n   int s, t; n   scanf( %d %d , &s, &t); n   for (int i = 0; i < m; i++) { n     int u, v, w; n     scanf( %d %d %d , &u, &v, &w); n     g[u].push_back(i); n     g[v].push_back(i); n     edges.emplace_back(u, v, w); n   } n   edg_used = -1; n   dfs(s); n   global = true; n   int S = s; n   if (!low[t]) { n     printf( 0 n0 n ); n     return 0; n   } n   int ans = 0x3f3f3f3f; n   int edg; n   int v = t; n   while (v != s) { n     int u = pai[v]; n     if (low[v] > num[u]) { n       if (pw[v] < ans) edg = sobe[v]; n       ans = min(ans, pw[v]); n     } n     v = u; n   } n   int dupla = INT_MAX; n   int x, y; n   for (int i = 1; i <= n; i++) { n     if (back_edge[i].empty()) continue; n     int k = 0, ans = 0x3f3f3f3f; n     for (int j = 0; j < back_edge[i].size(); j++) { n       int e = back_edge[i][j]; n       int x, v, p; n       tie(x, v, p) = edges[e]; n       if (v == i) swap(v, x); n       if (num[v] < ans) k = j; n     } n     vector<int> c[5]; n     cnt = 0; n     for (int j = 1; j <= n; j++) { n       c[0].push_back(num[j]); n       c[1].push_back(low[j]); n       c[2].push_back(pai[j]); n       c[3].push_back(pw[j]); n       c[4].push_back(sobe[j]); n       num[j] = low[j] = 0; n     } n     tie(edg_u, edg_v, edg_w) = edges[back_edge[i][k]]; n     edg_used = back_edge[i][k]; n     dfs(s); n     ans = 0x3f3f3f3f; n     int v = t, edg; n     while (v != s) { n       int u = pai[v]; n       if (low[v] > num[u]) { n         if (pw[v] < ans) edg = sobe[v]; n         ans = min(ans, pw[v]); n       } n       v = u; n     } n     if (ans != 0x3f3f3f3f and ans + edg_w < dupla) { n       dupla = ans + edg_w; n       x = back_edge[i][k]; n       y = edg; n     } n     for (int j = 1; j <= n; j++) { n       num[j] = c[0][j - 1]; n       low[j] = c[1][j - 1]; n       pai[j] = c[2][j - 1]; n       pw[j] = c[3][j - 1]; n       sobe[j] = c[4][j - 1]; n     } n   } n   for (int i = 1; i <= n; i++) { n     prev_pai[i] = pai[i]; n     prev_pw[i] = pw[i]; n     prev_sobe[i] = sobe[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (i == s) continue; n     cnt = 0; n     for (int j = 1; j <= n; j++) num[j] = low[j] = 0; n     tie(edg_u, edg_v, edg_w) = edges[prev_sobe[i]]; n     edg_used = prev_sobe[i]; n     dfs(s); n     if (!low[t]) continue; n     int ans = 0x3f3f3f3f, v = t, edg; n     while (v != s) { n       int u = pai[v]; n       if (low[v] > num[u]) { n         if (pw[v] < ans) edg = sobe[v]; n         ans = min(ans, pw[v]); n       } n       v = u; n     } n     if (ans != 0x3f3f3f3f and ans + edg_w < dupla) { n       dupla = ans + edg_w; n       x = prev_sobe[i]; n       y = edg; n     } n   } n   if (ans == 0x3f3f3f3f and dupla == INT_MAX) { n     printf( -1 n ); n   } else if (ans < dupla and ans != 0x3f3f3f3f) n     printf( %d n1 n%d n , ans, edg + 1); n   else n     printf( %d n2 n%d %d n , dupla, x + 1, y + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int i, j, k; n   j = (n * 2) - 1; n   cout << j <<     n        <<  2 ; n   cout << endl <<  1  2 ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n vector<pair<int, int> > adj[N]; n int ans[N]; n int off[N]; n int lvl[N], anc[N][25]; n int n, a, b, k; n void dfs(int u = 0, int par = 0, int l = 0) { n   lvl[u] = l; n   anc[u][0] = par; n   for (auto nxt : adj[u]) n     if (nxt.first != par) dfs(nxt.first, u, l + 1); n } n void buildLCA() { n   int lg = ceil(log2(n)); n   for (int j = 1; j < lg; j++) n     for (int i = 0; i < n; i++) anc[i][j] = anc[anc[i][j - 1]][j - 1]; n } n int LCA(int i, int j) { n   int lg = ceil(log2(n)); n   int st = lg; n   if (lvl[i] > lvl[j]) swap(i, j); n   int cur = lvl[j]; n   for (; st >= 0; st--) n     if (cur - (1 << st) >= lvl[i]) cur -= (1 << st), j = anc[j][st]; n   if (i == j) return 2 * i - j; n   cur = lvl[i]; n   for (st = lg; st >= 0; st--) n     if (anc[i][st] != anc[j][st]) n       cur -= (1 << st), i = anc[i][st], j = anc[j][st]; n   return anc[i][0]; n } n int solve(int u = 0, int par = 0) { n   int cnt = off[u]; n   for (auto nxt : adj[u]) { n     if (par == nxt.first) continue; n     int x = solve(nxt.first, u); n     ans[nxt.second] = x; n     cnt += x; n   } n   return cnt; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &a, &b); n     a--, b--; n     adj[a].push_back({b, i}); n     adj[b].push_back({a, i}); n   } n   dfs(); n   buildLCA(); n   scanf( %d , &k); n   for (int i = 0; i < k; i++) { n     scanf( %d%d , &a, &b); n     a--, b--; n     off[a]++, off[b]++; n     off[LCA(a, b)] -= 2; n   } n   solve(); n   for (int i = 0; i < n - 1; i++) printf( %d%c , ans[i],    n [i == n - 2]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n struct Point { n   int x, y, type, index; n } num[20005]; n int ans[10005]; n Point c; n bool cmp(Point a, Point b) { n   return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x) > 0; n } n bool cmp2(Point a, Point b) { return (a.x < b.x) || (a.x == b.x && a.y < b.y); } n void f(int l, int r) { n   if (l >= r) return; n   int i; n   swap(num[l], num[min_element(num + l, num + r + 1, cmp2) - num]); n   for (i = l + 1; i <= r; i++) { n     num[i].x -= num[l].x; n     num[i].y -= num[l].y; n   } n   num[l].x = num[l].y = 0; n   c = num[l]; n   sort(num + l + 1, num + r + 1, cmp); n   int pre = num[l].type, minv = 1000000000, id = -1; n   for (i = l + 1; i <= r; i++) { n     pre += num[i].type; n     if (!pre && num[i].type != num[l].type && max(i - l - 1, r - i) < minv) { n       minv = max(i - l - 1, r - i); n       id = i; n     } n   } n   if (num[l].type == 1) n     ans[num[id].index] = num[l].index; n   else n     ans[num[l].index] = num[id].index; n   f(l + 1, id - 1); n   f(id + 1, r); n } n int main() { n   int i, n; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d%d , &num[i].x, &num[i].y); n     num[i].type = -1; n     num[i].index = i + 1; n   } n   for (i = 0; i < n; i++) { n     scanf( %d%d , &num[n + i].x, &num[n + i].y); n     num[n + i].type = 1; n     num[n + i].index = i + 1; n   } n   f(0, 2 * n - 1); n   for (i = 1; i <= n; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 201000, inf = 1 << 30; n struct node { n   int x, y; n } pt[mn], sub[mn]; n struct point { n   int k, x, y, r; n } e[mn * 2]; n int s1[mn * 2], s2[mn * 2]; n int xs[mn * 2], dis[mn], sa[mn], L; n int n, m, i, j, step, lo, hi, mid; n bool cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } n bool cmp1(point a, point b) { return a.x < b.x || (a.x == b.x && a.k < b.k); } n bool subway(int i, int j) { return dis[i] > dis[j]; } n struct tree { n   int tt, rt[mn], s[mn * 20], le[mn * 20], ri[mn * 20]; n   int get(const int &p, int l, int r, int x, int y) { n     if (x <= l && y >= r) return s[p]; n     int m = (l + r) / 2, res = 0; n     if (x <= m) res += get(le[p], l, m, x, y); n     if (y > m) res += get(ri[p], m + 1, r, x, y); n     return res; n   } n   void add(const int &t, int &p, int l, int r, int k) { n     s[p = ++tt] = s[t] + 1; n     if (l == r) return; n     int m = (l + r) / 2; n     if (k <= m) n       add(le[t], le[p], l, m, k), ri[p] = ri[t]; n     else n       add(ri[t], ri[p], m + 1, r, k), le[p] = le[t]; n   } n   void ins(int i, int v) { add(rt[i - 1], rt[i], 1, L, v); } n   int query(int x, int y, int l, int r) { n     if (x > y || l > r || x > m) return 0; n     return get(rt[y], 1, L, l, r) - get(rt[x - 1], 1, L, l, r); n   } n } T; n point eva(int k, int x, int y, int r) { n   point res; n   res.k = k, res.x = x, res.y = y, res.r = r; n   return res; n } n void cross(int &l, int &r, int &x, int &y, int i, int t) { n   l = max(l, pt[i].x - t), x = max(x, pt[i].y - t); n   r = min(r, pt[i].x + t), y = min(y, pt[i].y + t); n } n void add(int *s, int p, int v) { n   for (; p <= L; p += p & (-p)) s[p] = max(s[p], v); n } n int query(int *s, int p) { n   int res = -inf; n   for (; p; p -= p & (-p)) res = max(res, s[p]); n   return res; n } n void get(point p) { n   int y = lower_bound(xs + 1, xs + 1 + L, p.y) - xs; n   if (p.k == 0) n     add(s1, y, p.x + p.y), add(s2, L - y + 1, p.x - p.y); n   else { n     int t1 = p.x + p.y - query(s1, y), t2 = p.x - p.y - query(s2, L - y + 1); n     dis[p.r] = min(dis[p.r], min(t1, t2)); n   } n } n void ugly(node &p) { n   int X = p.x - p.y, Y = p.x + p.y; n   p.x = X, p.y = Y; n } n void nearest() { n   int i, tt = 0; n   for (i = 1; i <= n; ++i) xs[++L] = pt[i].y; n   for (i = 1; i <= m; ++i) xs[++L] = sub[i].y; n   sort(xs + 1, xs + 1 + L); n   L = unique(xs + 1, xs + 1 + L) - xs - 1; n   for (i = 1; i <= n; ++i) e[++tt] = eva(1, pt[i].x, pt[i].y, i); n   for (i = 1; i <= m; ++i) e[++tt] = eva(0, sub[i].x, sub[i].y, i); n   sort(e + 1, e + 1 + tt, cmp1); n   fill(dis + 1, dis + 1 + n, inf); n   fill(s1 + 1, s1 + L + 1, -inf), fill(s2 + 1, s2 + 1 + L, -inf); n   for (i = 1; i <= tt; ++i) get(e[i]); n   for (i = 1; i <= tt; ++i) e[i].x = -e[i].x; n   sort(e + 1, e + 1 + tt, cmp1); n   fill(s1 + 1, s1 + L + 1, -inf), fill(s2 + 1, s2 + 1 + L, -inf); n   for (i = 1; i <= tt; ++i) get(e[i]); n } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; ++i) scanf( %d%d , &pt[i].x, &pt[i].y); n   for (i = 1; i <= m; ++i) scanf( %d%d , &sub[i].x, &sub[i].y); n   nearest(); n   for (i = 1; i <= n; ++i) ugly(pt[i]); n   for (i = 1; i <= m; ++i) ugly(sub[i]); n   sort(sub + 1, sub + 1 + m, cmp); n   L = 0; n   for (i = 1; i <= n; ++i) xs[++L] = pt[i].y; n   for (i = 1; i <= m; ++i) xs[++L] = sub[i].y; n   sort(xs + 1, xs + 1 + L), L = unique(xs + 1, xs + 1 + L) - xs - 1; n   for (i = 1; i <= m; ++i) n     T.ins(i, lower_bound(xs + 1, xs + 1 + L, sub[i].y) - xs); n   for (i = 1; i <= n; ++i) sa[i] = i; n   sort(sa + 1, sa + 1 + n, subway); n   lo = 0, hi = dis[sa[1]]; n   int mix, mxx, miy, mxy, ct, l, r, x, y; n   node su; n   while (lo < hi) { n     int mid = (lo + hi) / 2; n     mix = miy = -inf, mxx = mxy = inf; n     for (i = 1; i <= n; ++i) { n       if (dis[sa[i]] > mid) n         cross(mix, mxx, miy, mxy, sa[i], mid); n       else { n         ct = mid - dis[sa[i]]; n         su.y = -inf, su.x = mix - ct; n         l = lower_bound(sub + 1, sub + 1 + m, su, cmp) - sub; n         su.y = inf, su.x = mxx + ct; n         r = lower_bound(sub + 1, sub + 1 + m, su, cmp) - sub - 1; n         x = lower_bound(xs + 1, xs + 1 + L, miy - ct) - xs, n         y = lower_bound(xs + 1, xs + 1 + L, mxy + ct + 1) - xs - 1; n         if (T.query(l, r, x, y)) n           break; n         else n           cross(mix, mxx, miy, mxy, sa[i], mid); n       } n       if (mix > mxx || miy > mxy) break; n     } n     if (mix <= mxx && miy <= mxy) n       hi = mid; n     else n       lo = mid + 1; n   } n   printf( %d n , lo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n inline void gn(long long &first) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   c ==  -  ? (sg = -1, first = 0) : (first = c -  0 ); n   while ((c = getchar()) >=  0  && c <=  9 ) first = first * 10 + c -  0 ; n   first *= sg; n } n inline void gn(int &first) { n   long long t; n   gn(t); n   first = t; n } n inline void gn(unsigned long long &first) { n   long long t; n   gn(t); n   first = t; n } n inline void gn(double &first) { n   double t; n   scanf( %lf , &t); n   first = t; n } n inline void gn(long double &first) { n   double t; n   scanf( %lf , &t); n   first = t; n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int N, M, E, W; n int dis[101000]; n queue<int> Q; n int pre[101000], edg[101000][3], cnt[101000], val[101000]; n map<pair<int, int>, int> state, flag; n vector<int> adj[101000]; n int len = 0x3f3f3f3f, tot; n void Dijk(int s) { n   memset(dis, 0x3f, sizeof(dis)); n   Q.push(s); n   dis[s] = 0; n   cnt[s] = 0; n   val[s] = W; n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int i = 0; i < adj[u].size(); i++) { n       int v = adj[u][i], k; n       if (!state[pair<int, int>(u, v)]) n         k = val[u] + 1; n       else n         k = val[u] - 1; n       if (dis[v] > dis[u] + 1) { n         dis[v] = dis[u] + 1; n         pre[v] = u; n         val[v] = k; n         Q.push(v); n       } else if (dis[v] == dis[u] + 1 && val[v] > k) { n         val[v] = k; n         pre[v] = u; n         Q.push(v); n       } n     } n   } n } n int main() { n   int u, v, w; n   scanf( %d%d , &N, &M); n   for (int i = 0; i < M; i++) { n     scanf( %d%d%d , &u, &v, &w); n     adj[u].push_back(v); n     adj[v].push_back(u); n     state[pair<int, int>(u, v)] = w; n     state[pair<int, int>(v, u)] = w; n     edg[i][0] = u; n     edg[i][1] = v; n     edg[i][2] = w; n     W += w; n   } n   Dijk(1); n   u = N; n   while (u != 1) { n     flag[pair<int, int>(u, pre[u])] = 1; n     flag[pair<int, int>(pre[u], u)] = 1; n     u = pre[u]; n   } n   tot = 0; n   for (int i = 0; i < M; i++) { n     u = edg[i][0]; n     v = edg[i][1]; n     w = edg[i][2]; n     if (!flag[pair<int, int>(u, v)] && w == 1) n       tot++; n     else if (flag[pair<int, int>(u, v)] && w == 0) n       tot++; n   } n   printf( %d n , tot); n   for (int i = 0; i < M; i++) { n     u = edg[i][0]; n     v = edg[i][1]; n     w = edg[i][2]; n     if (!flag[pair<int, int>(u, v)] && w == 1) { n       printf( %d %d 0 n , u, v); n     } else if (flag[pair<int, int>(u, v)] && w == 0) { n       printf( %d %d 1 n , u, v); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 35005; n struct Node { n   int l, r; n   int lazy; n   int Max; n } node[maxn << 2]; n int n, k; n int a[maxn]; n int pre[maxn]; n int dp[maxn]; n void push_down(int root) { n   node[root << 1].lazy += node[root].lazy; n   node[root << 1 | 1].lazy += node[root].lazy; n   node[root << 1].Max += node[root].lazy; n   node[root << 1 | 1].Max += node[root].lazy; n   node[root].lazy = false; n } n void push_up(int root) { n   node[root].Max = max(node[root << 1].Max, node[root << 1 | 1].Max); n } n void build(int l, int r, int root) { n   node[root].l = l, node[root].r = r; n   node[root].lazy = 0; n   if (l == r) { n     node[root].Max = dp[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, root << 1); n   build(mid + 1, r, root << 1 | 1); n   push_up(root); n } n void update(int l, int r, int root) { n   if (node[root].l >= l && node[root].r <= r) { n     node[root].lazy += 1; n     node[root].Max += 1; n     return; n   } n   if (node[root].lazy) push_down(root); n   int mid = (node[root].l + node[root].r) >> 1; n   if (l <= mid) update(l, r, root << 1); n   if (r > mid) update(l, r, root << 1 | 1); n   push_up(root); n } n int query(int l, int r, int root) { n   if (node[root].l >= l && node[root].r <= r) { n     return node[root].Max; n   } n   if (node[root].lazy) push_down(root); n   int t1 = 0, t2 = 0; n   int mid = (node[root].l + node[root].r) >> 1; n   if (l <= mid) t1 = query(l, r, root << 1); n   if (r > mid) t2 = query(l, r, root << 1 | 1); n   push_up(root); n   if (t1 > t2) return t1; n   return t2; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= k; i++) { n     build(0, n, 1); n     for (int j = 0; j < maxn; j++) pre[j] = 0; n     for (int j = 1; j <= n; j++) { n       update(pre[a[j]], j - 1, 1); n       pre[a[j]] = j; n       dp[j] = query(0, j - 1, 1); n     } n   } n   printf( %d n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void small(long long m, long long s) { n   vector<long long> v; n   long long p = (s % 9 == 0) ? s / 9 - 1 : s / 9; n   for (long long i = 0; i < p; i++) { n     v.push_back(9); n     s -= 9; n   } n   if (m - p == 1) { n     v.push_back(s); n   } n   if (m - p >= 2) { n     s--; n     v.push_back(s); n     for (long long i = 0; i < m - p - 2; i++) { n       v.push_back(0); n     } n     v.push_back(1); n   } n   for (long long i = v.size() - 1; i >= 0; i--) { n     cout << v[i]; n   } n   cout <<    ; n } n void great(long long m, long long s) { n   vector<long long> v; n   long long k = s / 9; n   for (long long i = 0; i < k; i++) { n     v.push_back(9); n     s -= 9; n   } n   if (s > 0) { n     v.push_back(s); n     k++; n   } n   for (long long i = 0; i < m - k; i++) { n     v.push_back(0); n   } n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i]; n   } n } n int main() { n   long long m, s, h; n   string k; n   vector<int> v; n   cin >> m >> s; n   if (s % 9 == 0) { n     long long t = 0; n     if (m == 1 && s == 0) { n       cout <<  0   n            <<  0 ; n       t = 1; n     } n     if (t == 0) { n       if (m < s / 9 || s == 0) { n         cout <<  -1   n              <<  -1 ; n       } else { n         small(m, s); n         great(m, s); n       } n     } n   } else if (s % 9 != 0) { n     long long t = 0; n     if (m == 1 && s == 0) { n       cout <<  0   n            <<  0 ; n       t = 1; n     } n     if (t == 0) { n       if (m < s / 9 + 1 || s == 0) { n         cout <<  -1   n              <<  -1 ; n       } else { n         small(m, s); n         great(m, s); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n long long int inf = 1e9 + 7; n void solve() { n   long long int n; n   cin >> n; n   if (n <= 2) { n     cout <<  -1  << endl; n   } else { n     long long int i; n     for (i = n; i >= 1; i--) { n       cout << i <<    ; n     } n     cout << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   int cost = 0; n   for (int i = 0; i < n;) { n     if (s[i] != t[i]) { n       if (i + 1 < n && s[i] != s[i + 1] && s[i + 1] != t[i + 1]) { n         i += 2; n       } else { n         i++; n       } n       cost++; n     } else { n       i++; n     } n   } n   cout << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n const long long N = 100010; n struct edge { n   long long v, nxt; n } e[N << 1]; n long long head[N], cnt, n, x, y, a[N], dp[N][2], f[N], g[N], h[N]; n inline void add(long long u, long long v) { n   e[++cnt].v = v; n   e[cnt].nxt = head[u]; n   head[u] = cnt; n } n inline void dfs(long long u, long long fa) { n   dp[u][0] = dp[u][1] = f[u] = g[u] = a[u]; n   h[u] = 0; n   for (long long i = head[u]; i; i = e[i].nxt) { n     long long v = e[i].v; n     if (v == fa) continue; n     dfs(v, u); n     dp[u][0] = max(dp[u][0], max(dp[v][0], dp[u][1] + dp[v][1])); n     dp[u][0] = max(dp[u][0], max(f[u] + g[v], f[v] + g[u])); n     dp[u][1] = max(dp[u][1], max(dp[v][1], f[u] + f[v])); n     g[u] = max(g[u], a[u] + g[v]); n     g[u] = max(g[u], max(f[u] + dp[v][1], f[v] + a[u] + h[u])); n     h[u] = max(h[u], dp[v][1]); n     f[u] = max(f[u], f[v] + a[u]); n   } n } n inline void mian() { n   n = read(); n   for (long long i = 1; i <= n; ++i) a[i] = read(); n   for (long long i = 1; i <= n - 1; ++i) { n     x = read(); n     y = read(); n     add(x, y); n     add(y, x); n   } n   dfs(1, 0); n   printf( %lld n , dp[1][0]); n } n signed main() { n   long long qwq = 1; n   while (qwq--) mian(); 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     string s; n     string t; n     cin >> s >> t; n     int ans = 1; n     int last = INT_MIN; n     vector<int> v[26]; n     int dp[2][26]; n     for (int i = 0; i < s.length(); i++) { n       v[s[i] -  a ].push_back(i); n     } n     for (int i = 0; i < 26; i++) { n       dp[0][i] = v[i].size(); n       dp[1][i] = 0; n     } n     for (int i = 0; i < t.length(); i++) { n       if (v[t[i] -  a ].empty()) { n         ans = -1; n         break; n       } else { n         int temp = dp[1][t[i] -  a ]; n         vector<int>::iterator lower, l; n         l = v[t[i] -  a ].begin() + temp; n         lower = lower_bound(l, v[t[i] -  a ].end(), last); n         if (lower - v[t[i] -  a ].begin() == dp[0][t[i] -  a ]) { n           ans++; n           last = v[t[i] -  a ][0]; n           for (int j = 0; j < 26; j++) dp[1][j] = 0; n           dp[1][t[i] -  a ] = 1; n         } else { n           last = v[t[i] -  a ][lower - v[t[i] -  a ].begin()]; n           dp[1][t[i] -  a ] = lower - v[t[i] -  a ].begin() + 1; n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n stack<long long> num; n stack<char> fuhao; n vector<int> pos; n void calc() { n   char cc = fuhao.top(); n   fuhao.pop(); n   long long a = num.top(); n   num.pop(); n   long long b = num.top(); n   num.pop(); n   long long ans; n   if (cc ==  * ) n     ans = a * b; n   else n     ans = a + b; n   num.push(ans); n } n long long solve(string p) { n   int len = p.length(); n   for (int i = 0; i < len; i++) { n     if (p[i] >=  0  && p[i] <=  9 ) { n       num.push(p[i] -  0 ); n     } else if (p[i] ==  ( ) { n       fuhao.push(p[i]); n     } else if (p[i] ==  ) ) { n       while (fuhao.top() !=  ( ) calc(); n       fuhao.pop(); n     } else if (p[i] ==  * ) { n       fuhao.push(p[i]); n     } else if (p[i] ==  + ) { n       while (!fuhao.empty() && fuhao.top() ==  * ) { n         calc(); n       } n       fuhao.push(p[i]); n     } n   } n   while (!fuhao.empty()) calc(); n   long long ans = num.top(); n   return ans; n } n int main() { n   cin >> s; n   int len = s.length(); n   pos.push_back(-1); n   for (int i = 1; i < len; i += 2) n     if (s[i] ==  * ) pos.push_back(i); n   pos.push_back(len); n   long long ans = 0; n   for (int i = 0; i < pos.size() - 1; i++) { n     for (int j = i + 1; j < pos.size(); j++) { n       string temp = s; n       temp.insert(pos[i] + 1, 1,  ( ); n       temp.insert(pos[j] + 1, 1,  ) ); n       ans = max(ans, solve(temp)); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n long long n, m, k; n long long arr[300001]; n long long arr2[300001]; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   cin.ignore(); n   for (int test_case = 0; test_case < t; ++test_case) { n     int flg = 0; n     long long ans1 = 0, ans2 = 0; n     string ans[3] = { NO ,  YES ,  R }; n     string s1, s2 =   ; n     cin >> n >> k; n     for (int i = 0; i < n; ++i) { n       cin >> arr[i]; n     } n     int i; n     long long mn = 10000000000; n     long long mx = 0; n     for (i = 0; i < n; ++i) { n       if (mn < arr[i]) { n         if (mn != 10000000000) { n           ans1 += mx - mn; n           mx = 0; n           mn = 10000000000; n         } n       } n       mx = max(mx, arr[i]); n       mn = min(mn, arr[i]); n     } n     ans1 += mx; n     cout << ans1; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = int(1e6 + 3); n vector<vector<int> > v; n char second[600000]; n int n, m; n int main() { n   scanf( %d%d n , &n, &m); n   v.resize(n); n   for (int i = 0; i < n; ++i) { n     gets(second); n     v[i].resize(m); n     for (int j = 0; j < m; ++j) { n       v[i][j] = (second[j] ==  . ) ? -1 : second[j] -  0 ; n     } n   } n   int res = 1; n   for (int i = 0; i < n; ++i) { n     int dir = -1; n     for (int j = 0; j < m; ++j) n       if (v[i][j] != -1) { n         dir = (v[i][j] > 2) ? 1 : 0; n         if (j & 1) dir = 1 - dir; n         break; n       } n     if (dir != -1) { n       for (int j = 0; j < m; ++j) n         if (v[i][j] != -1) { n           int d2 = (v[i][j] > 2) ? 1 : 0; n           if (j & 1) d2 = 1 - d2; n           if (dir != d2) res = 0; n         } n     } else if (dir == -1) n       res = (res * 2) % mod; n   } n   for (int j = 0; j < m; ++j) { n     int dir = -1; n     for (int i = 0; i < n; ++i) n       if (v[i][j] != -1) { n         dir = (v[i][j] > 1 && v[i][j] < 4) ? 1 : 0; n         if (i & 1) dir = 1 - dir; n         break; n       } n     if (dir != -1) { n       for (int i = 0; i < n; ++i) n         if (v[i][j] != -1) { n           int d2 = (v[i][j] > 1 && v[i][j] < 4) ? 1 : 0; n           if (i & 1) d2 = 1 - d2; n           if (dir != d2) res = 0; n         } n     } else if (dir == -1) n       res = (res * 2) % mod; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 5; n using namespace std; n int a[maxn]; n int vis[maxn]; n int step[maxn * 2]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     int temp = a[i]; n     int step1 = 0, step2 = 0, step3 = 0; n     while (temp < maxn) { n       step1++; n       vis[temp]++; n       temp = temp << 1; n       step[temp] += step1; n     } n     while (a[i] >= 1) { n       if (a[i] & 1 && a[i] != 1) { n         int temp1 = a[i] = a[i] >> 1; n         step2++; n         step3 = step2; n         vis[temp1]++; n         step[temp1] += step3; n         while (temp1 < maxn) { n           step3++; n           temp1 = temp1 << 1; n           vis[temp1]++; n           step[temp1] += step3; n         } n       } else { n         step2++; n         a[i] = a[i] >> 1; n         vis[a[i]]++; n         step[a[i]] += step2; n       } n     } n   } n   int ans = 1e9; n   for (int i = 1; i < maxn; i++) { n     if (vis[i] == n) { n       ans = min(step[i], ans); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int mod = int(1e9) + 7; n const int N = int(1e6) + 7; n long long int two[N]; n void pre() { n   two[0] = 1; n   for (int i = (int)1; i < (int)N; i++) { n     two[i] = (two[i - 1] * 2) % mod; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   pre(); n   string s; n   cin >> s; n   int cnt = 0; n   long long int ans = 0; n   for (int i = (int)0; i < (int)(int)(s.size()); i++) { n     if (s[i] ==  b ) { n       ans = (ans + (two[cnt] - 1)) % mod; n       continue; n     } n     cnt++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = n     3.14159265358979323846264338327950288419716939937510582097494459230; n void swaps(char *x, char *y) { n   char temp; n   temp = *x; n   *x = *y; n   *y = temp; n } n void swapi(int *a, int *b) { n   int temp; n   temp = *a; n   *a = *b; n   *b = temp; n } n unsigned long long gcd(unsigned long long a, unsigned long long b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == 1 || b == 1) return 1; n   if (a == b) return a; n   if (a > b) n     return gcd(b, a % b); n   else n     return gcd(a, b % a); n } n long long Tree[4000010]; n int V[4000010]; n int N; n int Time; n int Outtime[4000010]; n int Intime[4000010]; n int Lvl[4000010]; n int maxval; n bool vis[4000010]; n std::vector<std::vector<int> > Edge(2000010); n void Add(int idx, int val) { n   for (int i = idx; i <= maxval; i += (i & -i)) Tree[i] += val; n } n long long Query(int idx) { n   long long S = 0; n   for (int i = idx; i > 0; i -= (i & -i)) S += Tree[i]; n   return S; n } n void dfs(int idx) { n   vis[idx] = true; n   Intime[idx] = ++Time; n   vis[idx] = true; n   for (int i = 0; i < Edge[idx].size(); i++) { n     if (vis[Edge[idx][i]]) continue; n     Lvl[Edge[idx][i]] = Lvl[idx] + 1; n     dfs(Edge[idx][i]); n   } n   Outtime[idx] = ++Time; n } n int main() { n   int Cases = 1; n   for (int Case = 0; Case < Cases; Case++) { n     int u, v, opt, node, Q; n     cin >> N >> Q; n     for (int i = 1; i <= N; i++) cin >> V[i]; n     for (int i = 0; i < N - 1; i++) { n       cin >> u >> v; n       Edge[u].push_back(v); n       Edge[v].push_back(u); n     } n     dfs(1); n     maxval = Outtime[1]; n     for (int i = 0; i < Q; i++) { n       cin >> opt; n       opt -= 1; n       if (opt == 0) { n         cin >> node >> v; n         if (Lvl[node] % 2) { n           Add(Intime[node], -v); n           Add(Outtime[node], v); n         } else { n           Add(Intime[node], v); n           Add(Outtime[node], -v); n         } n       } else { n         cin >> node; n         if (Lvl[node] % 2) n           cout << V[node] - Query(Intime[node]) << endl; n         else n           cout << V[node] + Query(Intime[node]) << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n long long int binpow(long long int a, long long int b) { n   if (b == 0) return 1; n   long long int res = binpow(a, b / 2); n   if (b % 2) n     return res * res * a; n   else n     return res * res; n } n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> v(n, 0); n     for (int i = 0; i < v.size(); i++) cin >> v[i]; n     sort(v.begin(), v.end(), greater<int>()); n     for (int i = 0; i < n; i++) cout << v[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LLINF = 1e18; n const int INF = 1e9; n const int MOD = 20101009; n const int HMOD = 999959; n const int VMOD = 5000000; n const int MAXN = 1e5 + 10; n const int MAXM = 5e5 + 10; n const int INV = 10050505; n const long long LLINV = 1e18; n const double eps = 1e-3; n int dirx[5] = {0, -1, 1, 0, 0}, diry[5] = {0, 0, 0, -1, 1}; n int n, d, m; n int cnt1, cnt2; n long long a[MAXN], b[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   scanf( %d%d%d , &n, &d, &m); n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x); n     if (x <= m) n       a[++cnt1] = x; n     else n       b[++cnt2] = x; n   } n   sort(a + 1, a + 1 + cnt1, greater<int>()); n   sort(b + 1, b + 1 + cnt2, greater<int>()); n   if (cnt2 == 0) { n     long long sum = 0; n     for (int i = 1; i <= n; ++i) sum += a[i]; n     printf( %lld n , sum); n     return 0; n   } n   for (int i = 1; i <= cnt1; ++i) a[i] = a[i - 1] + a[i]; n   for (int i = 1; i <= cnt2; ++i) b[i] = b[i - 1] + b[i]; n   long long ans = 0; n   for (int i = 1; i <= cnt2; ++i) { n     long long tmp = (long long)(i - 1) * d; n     long long oth = n - i; n     long long ned = cnt1 - (max(0LL, tmp - (cnt2 - i))); n     if (oth >= tmp) ans = max(ans, b[i] + a[ned]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, m; n   char c; n   cin >> h >> c >> m; n   while (1 > 0) { n     m++; n     h += (m / 60); n     m %= 60; n     h %= 24; n     if (h / 10 == m % 10 && h % 10 == m / 10) break; n   } n   cout << h / 10 << h % 10 <<  :  << m / 10 << m % 10; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int tt; n int ss = n + m; n int fa[400005]; n bool vis[500005]; n int ans; n int find(int x) { n   if (fa[x] == x) return x; n   return fa[x] = find(fa[x]); n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n + m; i++) { n     fa[i] = i; n   } n   for (int i = 1; i <= q; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     fa[find(x)] = find(n + y); n   } n   for (int i = 1; i <= n + m; i++) { n     if (!vis[find(i)]) { n       vis[fa[i]] = 1; n       ans++; n     } n   } n   cout << ans - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modulo(long long base, long long exponent, long long modulus); n long long choose(long long n, long long k); n long long inverse(long long a, long long m); n void build(); n void fileio(); n long long ncr(long long n, long long r); n const int nax = 1e6 + 10; n const int LG = log2(nax) + 1; n vector<long long> fact(nax); n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string s; n     cin >> s; n     long long ans = n; n     for (long long i = 0; i < n; i++) { n       if (s[i] ==  1 ) { n         ans = max(ans, 2 * (i + 1)); n         ans = max(ans, 2 * (n - i)); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n long long ncr(long long n, long long r) { n   if (r > n || r < 0 || n < 0) return 0; n   long long ans = fact[n]; n   long long temp = (fact[n - r] * fact[r]) % 1000000007; n   ans = (ans * inverse(temp, 1000000007)) % 1000000007; n   return ans; n } n void fileio() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n void build() { n   fact[0] = 1; n   for (long long i = 1; i < nax; i++) fact[i] = (fact[i - 1] * i) % 1000000007; n } n long long modulo(long long base, long long exponent, long long modulus) { n   if (modulus == 1) return 0; n   long long result = 1; n   base = base % modulus; n   while (exponent > 0) { n     if (exponent % 2 == 1) { n       result = (result * base) % modulus; n     } n     exponent = exponent >> 1; n     base = (base * base) % modulus; n   } n   return result; n } n long long choose(long long n, long long k) { n   if (k == 0) return 1; n   return (n * choose(n - 1, k - 1)) / k; n } n void EE(long long a, long long b, long long &co1, long long &co2) { n   if (a % b == 0) { n     co1 = 0; n     co2 = 1; n     return; n   } n   EE(b, a % b, co1, co2); n   long long temp = co1; n   co1 = co2; n   co2 = temp - co2 * (a / b); n } n long long inverse(long long a, long long m) { n   long long x, y; n   EE(a, m, x, y); n   if (x < 0) x += m; n   return x; n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   cin.tie(nullptr); n   cin.sync_with_stdio(false); n } n using LL = long long; n using LD = long double; n const LL MOD = 1e9 + 7; n const LL INF = LLONG_MAX; n const LL N = 2e5 + 1; n int main() { n   fastio(); n   LL n; n   cin >> n; n   vector<LL> a(n); n   for (auto &e : a) { n     cin >> e; n   } n   vector<LL> ans(n, 0); n   sort(a.begin(), a.end(), greater<LL>()); n   LL p = 0; n   for (LL i = 0; i < n; i += 2) { n     ans[i] = a[p++]; n   } n   for (LL i = 0; i < n; ++i) { n     if (ans[i] == 0) { n       ans[i] = a[p++]; n     } n   } n   cout << (n - 1) / 2 <<   n ; n   for (auto &e : ans) { n     cout << e <<    ; n   } n   cout <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 5000 + 20; n const int maxn = 1e5 + 20; n const int mod = 998244353; n int cnt, head[maxn], pos[maxn]; n long long ksm(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     b >>= 1, a = a * a % mod; n   } n   return res % mod; n } n void read(int &v) { n   int k = 1; n   v = 0; n   int c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) k = 0; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     v = (v << 3) + (v << 1) + (c - 48), c = getchar(); n   if (k == 0) v = -v; n } n int d[maxn], dp[maxn], a[maxn], same[maxn]; n int main() { n   int n, q; n   read(n), read(q); n   for (int i = 1; i <= n; i++) { n     read(a[i]); n     if (a[i] == a[i - 1]) same[i] = same[i - 1] + 1; n     if (a[i] >= a[i - 1]) n       d[i] = 0; n     else n       d[i] = 1; n   } n   dp[0] = 0; n   for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] + d[i]; n   while (q--) { n     int l, r; n     read(l), read(r); n     if (dp[r] - dp[l] == 0) n       puts( Yes ); n     else if (dp[r] - dp[l] == r - l) n       puts( Yes ); n     else { n       int x = dp[r] - dp[l]; n       r = r - same[r]; n       if (dp[r] - dp[r - x] == x) n         puts( Yes ); n       else n         puts( No ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int query(int x, int y) { n   if (x == -1) return 0; n   cout << 1 <<     << x <<     << y << endl; n   string ret; n   cin >> ret; n   return ( TAK  == ret); n } n int get(int l, int r) { n   if (l > r) return -1; n   while (l < r) { n     int m = (l + r) >> 1; n     if (query(m, m + 1)) { n       r = m; n     } else { n       l = m + 1; n     } n   } n   return l; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   int x = get(1, n); n   int y = get(1, x - 1); n   if (!query(y, x)) y = get(x + 1, n); n   cout << 2 <<     << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, d, done[300005] = {0}; n vector<int> graph[300005]; n map<pair<int, int>, int> edge; n set<int> p; n void go() { n   queue<pair<int, int> > q; n   for (auto it : p) q.push({it, 0}); n   while (!q.empty()) { n     int x = q.front().first, P = q.front().second; n     q.pop(); n     if (done[x]) continue; n     done[x] = 1; n     for (auto it : graph[x]) { n       if (done[it] && it != P) n         printf( %d  , edge[{min(x, it), max(x, it)}]); n       else if (it != P) n         q.push({it, x}); n     } n   } n } n int main() { n   scanf( %d , &n); n   scanf( %d , &k); n   scanf( %d , &d); n   for (__typeof(k) i = 0 - (0 > k); i != k - (0 > k); i += 1 - 2 * (0 > k)) { n     int x; n     scanf( %d , &x); n     p.insert(x); n   } n   for (__typeof(n - 1) i = 0 - (0 > n - 1); i != n - 1 - (0 > n - 1); n        i += 1 - 2 * (0 > n - 1)) { n     int u, v; n     scanf( %d , &u); n     scanf( %d , &v); n     graph[u].push_back(v); n     graph[v].push_back(u); n     edge[{min(u, v), max(u, v)}] = i + 1; n   } n   printf( %d n , (int)p.size() - 1); n   go(); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 88; n const int M = 100100; n int dp[N][M][2]; n pair<int, int> a[N]; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d%d , &a[i].first, &a[i].second); n   sort(a, a + n); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) n       for (int f = 0; f < 2; f++) dp[i][j][f] = M; n   dp[0][0][1] = 0; n   for (int i = 0; i < n; i++) { n     for (int x = 0; x <= m; x++) n       dp[i + 1][x][0] = min(dp[i + 1][x][0], min(dp[i][x][0], dp[i][x][1])); n     for (int x = 0; x <= m; x++) { n       int r = a[i].first - x - 1; n       r = max(r, x - a[i].first); n       r = max(r, a[i].second); n       int y = min(m, a[i].first + r); n       dp[i + 1][y][1] = n           min(dp[i + 1][y][1], min(dp[i][x][0], dp[i][x][1]) + r - a[i].second); n     } n     for (int x = 1; x <= m; x++) n       dp[i + 1][x][1] = min(dp[i + 1][x][1], dp[i + 1][x - 1][1] + 1); n   } n   printf( %d n , min(dp[n][m][0], dp[n][m][1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   int p = 2e9 + 7, ans = 0; n   for (int i = 1; i < n; i++) p = min(p, a[i + 1] - a[i]); n   for (int i = 1; i < n; i++) ans += (a[i + 1] - a[i] == p); n   return printf( %d %d , p, ans), 0; n } n


#include <bits/stdc++.h> n const long long mod = 1000000007; n const long long Inf = 1e9; n using namespace std; n bool valid(string s) { n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] ==     || s[i] ==  # ) { n       if (s[i] ==  # ) return true; n     } else n       return false; n   } n   return false; n } n string wtSpace(string s) { n   string res =   ; n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] !=    ) res += s[i]; n   } n   return res; n } n int main() { n   int n, a, d; n   scanf( %d %d %d , &n, &a, &d); n   vector<double> ans(n); n   for (int i = 0; i < n; i++) { n     int t, v; n     scanf( %d %d , &t, &v); n     double time = pow(2 * d / (double)a, 0.5); n     double time2 = v / (double)a; n     if (time <= time2) n       ans[i] = t + time; n     else { n       double x = a * pow(time2, 2) / 2.0; n       ans[i] = t + time2 + (d - x) / (double)v; n     } n   } n   for (int i = 0; i < n - 1; i++) { n     if (ans[i + 1] < ans[i]) ans[i + 1] = ans[i]; n     printf( %.6f n , ans[i]); n   } n   printf( %.6f n , ans[n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T, class U> n inline void print(T first, U second) { n   printsp(first); n   println(second); n } n template <class T, class U, class V> n inline void print(T first, U second, V z) { n   printsp(first); n   printsp(second); n   println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n const int B = 2222222; n int n, u, cnt[B << 1], flag[B]; n long long s[B << 1], first, second; n long long calc(int p) { n   long long ans = 0; n   int r = max(1, p - (int)(first / second)); n   for (int t = 0; t + p < B; t += p) { n     ans += second * ((long long)(cnt[t + p - 1] - cnt[t + r - 1]) * (t + p) - n                      s[t + p - 1] + s[t + r - 1]); n     ans += first * (cnt[t + r - 1] - cnt[t]); n   } n   return ans; n } n void solve_B() { n   gn(n, first, second); n   for (int i = 0; i < n; i++) { n     gn(u); n     s[u] += u; n     cnt[u]++; n   } n   for (int i = 1; i < B; i++) s[i] += s[i - 1], cnt[i] += cnt[i - 1]; n   long long ans = n * first; n   for (int i = 2; i < B; i++) n     if (!flag[i]) { n       smin(ans, calc(i)); n       if ((long long)i * i >= B) continue; n       for (int j = i * i; j < B; j += i) flag[j] = 1; n     } n   println(ans); n   exit(0); n } n int main() { n   solve_B(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int ii = 0; ii < t; ii++) { n     int n; n     cin >> n; n     bool A[52]; n     char Aa[52][52]; n     for (int i = 0; i < n; i++) { n       char t; n       cin >> t; n       if (t ==  1 ) n         A[i] = 0; n       else n         A[i] = 1; n     } n     int k = 0; n     int t = -1; n     for (int i = n - 1; i >= 0; i--) { n       if (A[i] == 0) { n         k++; n       } else { n         t = i; n       } n     } n     if (n - k > 2 || n - k == 0) n       cout <<  YES  << endl; n     else { n       cout <<  NO  << endl; n       continue; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         Aa[i][j] =  = ; n       } n     } n     for (int i = 0; i < n; i++) Aa[i][i] =  X ; n     int c1 = t; n     if (n - k > 2) { n       for (int i = t + 1; i < n; i++) { n         if (A[i] == 1) { n           Aa[c1][i] =  + ; n           Aa[i][c1] =  - ; n           c1 = i; n         } n       } n       Aa[c1][t] =  + ; n       Aa[t][c1] =  - ; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         cout << Aa[i][j]; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long T; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> T; n   int a, b, c, d; n   while (T--) { n     cin >> a >> b >> c >> d; n     bool ok = 0; n     if (a == c) { n       if (d + b == a) ok = 1; n     } n     if (a == d) { n       if (c + b == a) ok = 1; n     } n     if (b == c) { n       if (a + d == b) ok = 1; n     } n     if (b == d) { n       if (a + c == b) ok = 1; n     } n     if (ok) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adjc[100005]; n vector<int> cadj[100005]; n struct edge { n   int u, v, c; n }; n class dsu { n  public: n   dsu(vector<int> vert) : vert(vert) { n     n = vert.size(); n     par.resize(n); n     iota(par.begin(), par.end(), 0); n   } n   int root(int u) { n     if (par[u] == u) { n       return u; n     } n     return (par[u] = root(par[u])); n   } n   void merge(int u, int v) { n     int ui = lower_bound(vert.begin(), vert.end(), u) - vert.begin(); n     int vi = lower_bound(vert.begin(), vert.end(), v) - vert.begin(); n     int ru = root(ui), rv = root(vi); n     if (ru != rv) { n       if (rand() % 2) { n         swap(ru, rv); n       } n       par[ru] = rv; n     } n   } n   bool ced(int u, int v) { n     int ui = lower_bound(vert.begin(), vert.end(), u) - vert.begin(); n     int vi = lower_bound(vert.begin(), vert.end(), v) - vert.begin(); n     if ((ui == n || (vert[ui] != u)) || (vi == n || (vert[vi] != v))) return 0; n     return (root(ui) == root(vi)); n   } n   int n; n   vector<int> vert, par; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<edge> e; n   for (int i = 0; i < m; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     e.push_back({u, v, c}); n     cadj[c].push_back(u); n     cadj[c].push_back(v); n     adjc[u].push_back(c); n     adjc[v].push_back(c); n   } n   for (int i = 1; i <= n; i++) { n     sort(adjc[i].begin(), adjc[i].end()); n     adjc[i].resize(unique(adjc[i].begin(), adjc[i].end()) - adjc[i].begin()); n   } n   vector<dsu> cdsu; n   for (int i = 1; i <= m; i++) { n     sort(cadj[i].begin(), cadj[i].end()); n     cadj[i].resize(unique(cadj[i].begin(), cadj[i].end()) - cadj[i].begin()); n     cdsu.emplace_back(cadj[i]); n   } n   for (edge ed : e) { n     cdsu[ed.c - 1].merge(ed.u, ed.v); n   } n   map<pair<int, int>, int> dp; n   int q; n   cin >> q; n   while (q--) { n     int u, v; n     cin >> u >> v; n     if (adjc[u].size() > adjc[v].size()) { n       swap(u, v); n     } n     if (dp.find({u, v}) != dp.end()) { n       cout << dp[{u, v}] << endl; n       continue; n     } n     int ans = 0; n     for (auto c : adjc[u]) { n       ans += cdsu[c - 1].ced(u, v); n     } n     cout << ans << endl; n     dp[{u, v}] = ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 20 + 5; n const long long INF = 3e9; n const char *cc =  0123456789abcdef ; n int c[maxn][maxn], used[maxn]; n long long dp[maxn][maxn]; n char s[maxn]; n long long solve(int t, int m, bool flag) { n   memset(dp[0], 0, sizeof(dp[0])); n   if (flag) { n     for (int i = 0; i < m; ++i) { n       for (int j = 0; j + used[0] <= t && j + i + 1 <= m; ++j) { n         dp[0][i + j] = min(INF, dp[0][i + j] + c[m - i - 1][j]); n       } n     } n   } else n     for (int i = 0; i + used[0] <= t && i <= m; ++i) dp[0][i] = c[m][i]; n   for (int i = 1; i < 16; ++i) { n     memset(dp[i], 0, sizeof(dp[i])); n     for (int j = 0; j <= m; ++j) { n       if (!dp[i - 1][j]) continue; n       for (int k = 0; k + used[i] <= t && k + j <= m; ++k) { n         dp[i][j + k] = min(INF, dp[i][j + k] + dp[i - 1][j] * c[m - j][k]); n       } n     } n   } n   return dp[15][m]; n } n int main() { n   for (int i = 0; i < maxn; ++i) { n     c[i][0] = c[i][i] = 1; n     for (int j = 1; j < i; ++j) c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; n   } n   int k, t; n   cin >> k >> t; n   bool flag = true; n   for (int i = 0; i < 20; ++i) { n     long long sum = 0; n     for (int j = 0; j < 16; ++j) { n       if (used[j] >= t) continue; n       if (!flag || j != 0) ++used[j]; n       long long tmp = solve(t, 20 - i - 1, flag && j == 0); n       sum += tmp; n       if (sum >= k) { n         s[i] = cc[j]; n         k -= sum - tmp; n         if (j > 0) flag = false; n         break; n       } n       if (!flag || j != 0) --used[j]; n     } n   } n   s[20] = 0; n   int p = 0; n   while (s[p] ==  0 ) ++p; n   cout << s + p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > inc[100010]; n priority_queue<pair<double, int> > Q; n long long A[100010]; n int N, M, K; n vector<pair<int, int> > mul; n int sel[100010]; n int typ[100010]; n void compute() { n   int k; n   while (M-- && !(Q.empty() && mul.empty())) { n     k = Q.empty() ? -1 : Q.top().second; n     if (mul.empty() || (k >= 0 && Q.top().first >= mul.back().first)) { n       A[k] += inc[k].back().first; n       sel[inc[k].back().second] = 1; n       inc[k].pop_back(); n       Q.pop(); n       if (!inc[k].empty()) n         Q.push(make_pair((A[k] + inc[k].back().first) / (1.0 * A[k]), k)); n       continue; n     } n     sel[mul.back().second] = 1; n     mul.pop_back(); n   } n } n pair<int, int> typ1[100010]; n int main() { n   int i, b, t; n   cin >> K >> N >> M; n   for (i = 0; i < K; ++i) cin >> A[i]; n   for (int idx = 0; idx < N; ++idx) { n     cin >> t >> i >> b; n     typ[idx] = t; n     if (t == 3) { n       mul.push_back(make_pair(b, idx)); n       continue; n     } n     --i; n     if (t == 1) { n       typ1[i] = max(typ1[i], make_pair(b - (int)A[i], idx)); n       continue; n     } n     inc[i].push_back(make_pair(b, idx)); n   } n   for (i = 0; i < K; ++i) { n     if (!typ1[i].first) continue; n     inc[i].push_back(typ1[i]); n   } n   sort(mul.begin(), mul.end()); n   for (i = 0; i < K; ++i) { n     if (inc[i].empty()) continue; n     sort(inc[i].begin(), inc[i].end()); n     Q.push(make_pair((A[i] + inc[i].back().first) / (1.0 * A[i]), i)); n   } n   compute(); n   cout << accumulate(sel, sel + N, 0) <<   n ; n   for (i = 0; i < N; ++i) { n     if (!sel[i] || typ[i] > 1) continue; n     cout << i + 1 <<    ; n   } n   for (i = 0; i < N; ++i) { n     if (!sel[i] || typ[i] != 2) continue; n     cout << i + 1 <<    ; n   } n   for (i = 0; i < N; ++i) { n     if (!sel[i] || typ[i] < 3) continue; n     cout << i + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[102]; n char b[102]; n char c[102]; n int main() { n   gets(a); n   gets(b); n   int l = strlen(a); n   int i; n   for (i = l - 1; i >= 0; i--) { n     if (a[i] ==  z ) { n       a[i] =  a ; n     } else { n       a[i] = a[i] + 1; n       break; n     } n   } n   if (!strcmp(a, b)) n     cout <<  No such string ; n   else n     cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int a[N]; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     memset(a, 0, sizeof(a)); n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     sort(a, a + n); n     int mid = (n - 1) / 2; n     int big = n - 1; n     int small = mid; n     int ok = 0; n     while (big > mid && small >= 0) { n       if (a[small] * 2 <= a[big]) { n         ok++; n         small--; n         big--; n       } else { n         small--; n       } n     } n     printf( %d n , n - ok); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, sol; n multiset<int> S; n multiset<int>::iterator p; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int val; n     scanf( %d , &val); n     S.insert(val); n   } n   long long s = 0; n   for (int i = 1; i <= n; i++) { n     p = S.lower_bound(s); n     if (p == S.end()) break; n     if (*p < s) p++; n     sol++; n     s += *p; n     S.erase(p); n   } n   printf( %d n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bz = -10000000000; n long long t[50001], ll[50001], rr[50001], dd[50001]; n long long tu[50001]; n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long a = 1; a <= n; a++) tu[a] = bz; n   for (long long a = 1; a <= m; a++) cin >> t[a] >> ll[a] >> rr[a] >> dd[a]; n   for (long long a = 1; a <= m; a++) { n     if (t[a] == 1) { n       for (long long b = ll[a]; b <= rr[a]; b++) { n         if (tu[b] == bz) n           continue; n         else n           tu[b] += dd[a]; n       } n     } else { n       long long jc = 1; n       for (long long b = ll[a]; b <= rr[a]; b++) { n         if (tu[b] == bz) { n           tu[b] = dd[a]; n           jc = 0; n         } else { n           if (tu[b] >= dd[a]) { n             tu[b] = dd[a]; n             jc = 0; n           } n         } n       } n       if (jc) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   for (long long a = 1; a <= n; a++) n     if (tu[a] == bz) tu[a] = 0; n   for (long long a = m; a >= 1; a--) { n     if (t[a] == 2) { n       long long jc = 1; n       for (long long b = ll[a]; b <= rr[a]; b++) { n         if (tu[b] == dd[a]) jc = 0; n       } n       if (jc) { n         cout <<  NO ; n         return 0; n       } n       continue; n     } n     for (long long b = ll[a]; b <= rr[a]; b++) n       if (tu[a] != bz) tu[b] -= dd[a]; n   } n   cout <<  YES  << endl; n   long long jc = 0; n   for (long long a = 1; a <= n; a++) { n     if (tu[a] == bz) tu[a] = 0; n     if (!jc) { n       cout << tu[a]; n       jc = 1; n     } else { n       cout <<     << tu[a]; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n const int N = 4040; n using namespace std; n int n, m; n int l[N], s[N]; n int cost[N]; n int inf = -1e9; n int d[N][N]; n int f[N][N]; n vector<int> v[N]; n vector<int> pos[N]; n vector<int> fut[N][14]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> l[i]; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   reverse(l + 1, l + n + 1); n   reverse(s + 1, s + n + 1); n   for (int i = 1; i <= n + m; i++) cin >> cost[i]; n   for (int i = 1; i <= n; i++) v[l[i]].push_back(i); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) d[i][j] = inf; n   for (int i = 1; i <= n + m; i++) { n     for (int j = 0; j < 13; j++) { n       fut[i][j].resize((1 << 13 - j)); n       while (fut[i][j].size() >= N - 4) fut[i][j].pop_back(); n       for (auto &x : fut[i][j]) x = inf; n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = 1; j <= n + m; j++) { n       f[i][j] = cost[j]; n       for (int h = 0; h < 13; h++) { n         if (i & (1 << h)) n           f[i][j] += cost[j + h + 1]; n         else n           break; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= l[i]; j++) { n       int dif = min(12, l[i] - j); n       for (int p = 0; p < fut[j][dif].size(); p++) { n         d[p + 1][i] = max(d[p + 1][i], fut[j][dif][p] + f[p][l[i]] - s[i]); n       } n     } n     d[1][i] = max(d[1][i], cost[l[i]] - s[i]); n     for (int j = 0; j < N; j++) { n       int nj = j, x = l[i]; n       for (int h = 0; h < 13; h++) { n         fut[x][h][nj] = max(fut[x][h][nj], d[j][i]); n         nj >>= 1; n       } n     } n   } n   int res = 0; n   for (int i = 0; i < N; i++) { n     for (int j = 1; j <= n; j++) { n       res = max(res, d[i][j]); n     } n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int XpowerY(long long int x, long long int y, long long int m) { n   long long int ans = 1; n   x = x % m; n   while (y > 0) { n     if (y % 2 == 1) ans = (ans * x) % m; n     x = ((x % m) * (x % m)) % m; n     y = y >> 1; n   } n   return ans % m; n } n vector<long long int> gr[100023]; n long long int s[100023], val[100023]; n long long int fl, minP; n long long int dfs(long long int node, long long int prev, long long int lev) { n   long long int mx = 1e12; n   if (lev % 2 == 0) { n     for (int i = 0; i < gr[node].size(); i++) { n       if (s[gr[node][i]] < prev) fl = 1; n       mx = min(mx, s[gr[node][i]] - prev); n     }; n     val[node] = mx; n     if (gr[node].size() == 0) val[node] = 0; n   } n   for (int i = 0; i < gr[node].size(); i++) { n     val[gr[node][i]] = s[gr[node][i]] - mx - prev; n     long long int temp; n     if (lev % 2 == 0) n       temp = prev + val[node]; n     else n       temp = s[node]; n     dfs(gr[node][i], temp, lev + 1); n   } n   return 0; n } n int main() { n   long long int n, x; n   cin >> n; n   for (long long int i = 0; i < n - 1; i++) { n     cin >> x; n     gr[x].push_back(i + 2); n   } n   for (long long int i = 1; i <= n; i++) cin >> s[i]; n   dfs(1, -1, 1); n   val[1] = s[1]; n   if (fl) return cout << -1 << endl, 0; n   for (long long int i = 0; i < n; i++) { n     ; n     minP += val[i + 1]; n   } n   cout << minP << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int ALPHA = 26 + 1; n struct Trie { n   Trie() { nodes.resize(2); } n   struct Node { n     Node() { memset(next, 0, sizeof(next)); } n     int next[ALPHA]; n     int finish = -1; n   }; n   int alloc() { n     nodes.push_back(Node()); n     return nodes.size() - 1; n   } n   void addWord(const string& s, int id) { n     int v = root; n     for (char c : s) { n       c = tolower(c) -  a ; n       if (!nodes[v].next[c]) { n         int x = alloc(); n         nodes[v].next[c] = x; n       } n       v = nodes[v].next[c]; n     } n     nodes[v].finish = id; n   } n   vector<Node> nodes; n   const int root = 1; n }; n int n; n string ciph; n int m; n Trie trie; n vector<int> solved; n vector<int> toTake; n bool solve(int pos) { n   if (pos == n) { n     return true; n   } n   if (solved[pos] == -1) { n     solved[pos] = false; n     int v = trie.root; n     int i = pos; n     while (i < ciph.length() && trie.nodes[v].next[ciph[i] -  a ]) { n       v = trie.nodes[v].next[ciph[i] -  a ]; n       ++i; n       if (trie.nodes[v].finish != -1) { n         if (solve(i)) { n           solved[pos] = true; n           toTake[pos] = trie.nodes[v].finish; n         } n       } n     } n   } n   return solved[pos]; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   solved.resize(n, -1); n   toTake.resize(n, -1); n   cin >> ciph; n   cin >> m; n   vector<string> words(m); n   for (int i = 0; i < m; ++i) { n     cin >> words[i]; n     trie.addWord(words[i], i); n   } n   reverse(ciph.begin(), ciph.end()); n   solve(0); n   vector<int> ans; n   if (solved[0] == 1) { n     int pos = 0; n     while (pos != n) { n       int id = toTake[pos]; n       ans.push_back(id); n       pos += words[id].length(); n     } n   } else { n     assert(false); n   } n   reverse(ans.begin(), ans.end()); n   for (int i = 0; i < ans.size(); ++i) { n     if (i != 0) { n       cout <<    ; n     } n     cout << words[ans[i]]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, m, cnt = 0; n int main() { n   scanf( %lld%lld%lld , &x, &y, &m); n   if (x >= m || y >= m) { n     printf( 0 ); n     return 0; n   } n   if (x <= 0 && y <= 0) { n     printf( -1 ); n     return 0; n   } n   while (x < m && y < m) { n     if (x > y) swap(x, y); n     if (x + y <= 0) { n       cnt += abs(x) / y; n       x = x + (abs(x) - abs(x) % y); n     } else { n       x += y; n       cnt++; n       ; n     } n   } n   printf( %lld , cnt); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int maxN = 2000; n const int S_CNT = 9; n const int M_CNT = 25; n const int DIST = 60 * 60; n int n, m; n int a[maxN][maxN]; n int t[maxN][maxN]; n int used[maxN][maxN]; n int rc[maxN][maxN]; n int is_in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n int get(int x, int y) { n   int cnt = 0; n   for (int i = x - 2; i <= x + 2; ++i) { n     for (int j = y - 2; j <= y + 2; ++j) { n       if (is_in(i, j)) { n         cnt += a[i][j]; n       } n     } n   } n   return cnt; n } n pair<int, int> choose_max(int x, int y) { n   pair<int, int> res = make_pair(-1, -1); n   int rs = -1; n   for (int i = x - 2; i <= x + 2; ++i) { n     for (int j = y - 2; j <= y + 2; ++j) { n       if (is_in(i, j)) { n         if (t[i][j] > rs) { n           rs = t[i][j]; n           res = make_pair(i, j); n         } n       } n     } n   } n   return res; n } n void change_used(int x, int y, int num) { n   for (int i = x - 2; i <= x + 2; ++i) { n     for (int j = y - 2; j <= y + 2; ++j) { n       if (is_in(i, j)) { n         used[i][j] = num; n       } n     } n   } n } n int dist(pair<int, int> a, pair<int, int> b) { n   return (a.first - b.first) * (a.first - b.first) + n          (a.second - b.second) * (a.second - b.second); n } n pair<int, int> choose_long(int cx, int cy, int x, int y, int step) { n   pair<int, int> res = make_pair(cx, cy); n   int rs = -1; n   for (int i = cx - 2; i <= cx + 2; ++i) { n     for (int j = cy - 2; j <= cy + 2; ++j) { n       if (is_in(i, j) && a[i][j] && used[i][j] != step) { n         if (dist(make_pair(i, j), make_pair(x, y)) > rs) { n           rs = dist(make_pair(i, j), make_pair(x, y)); n           res = make_pair(i, j); n         } n       } n     } n   } n   return res; n } n void dfs(int x, int y, int color) { n   if (rc[x][y] != 0) return; n   rc[x][y] = color; n   if (is_in(x + 1, y) && a[x + 1][y]) dfs(x + 1, y, color); n   if (is_in(x - 1, y) && a[x - 1][y]) dfs(x - 1, y, color); n   if (is_in(x, y + 1) && a[x][y + 1]) dfs(x, y + 1, color); n   if (is_in(x, y - 1) && a[x][y - 1]) dfs(x, y - 1, color); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       scanf( %d , &a[i][j]); n     } n   } n   int c_col = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i][j] && !rc[i][j]) { n         ++c_col; n         dfs(i, j, c_col); n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i][j]) { n         t[i][j] = get(i, j); n       } n     } n   } n   vector<pair<int, int> > cur; n   int step = 1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i][j] && !used[i][j] && t[i][j] == S_CNT) { n         ++step; n         int cx = i, cy = j; n         change_used(cx, cy, step); n         cur.push_back(make_pair(cx, cy)); n       } n     } n   } n   vector<int> col(cur.size(), 0); n   for (int i = 0; i < col.size(); ++i) { n     col[i] = rc[cur[i].first][cur[i].second]; n   } n   map<int, int> res; n   for (int i = 0; i < col.size(); ++i) { n     ++res[col[i]]; n   } n   cout << res.size() << endl; n   vector<int> ans; n   for (map<int, int>::iterator it = res.begin(); it != res.end(); ++it) { n     ans.push_back(it->second); n   } n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n char Src[1000001], Dst[1000001]; n int main() { n   int i, j, Cnts, Cntd, Lens, Lend; n   scanf( %s %s , Src, Dst); n   if (strlen(Src) != strlen(Dst)) { n     puts( NO ); n     return 0; n   } n   if (strcmp(Src, Dst) == 0) { n     puts( YES ); n     return 0; n   } n   if (strlen(Src) == 1) { n     puts(Src[0] == Dst[0] ?  YES  :  NO ); n     return 0; n   } n   for (i = 0; Src[i] !=   0 ; ++i) { n     if (Src[i] ==  1 ) { n       break; n     } n   } n   for (j = 0; Dst[j] !=   0 ; ++j) { n     if (Dst[j] ==  1 ) { n       break; n     } n   } n   puts(Src[i] ==  1  && Dst[j] ==  1  ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n bool smax(T &x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n bool smin(T &x, L y) { n   return y < x ? (x = y, 1) : 0; n } n const int maxn = 5e5 + 17; n list<int> a; n list<int>::iterator per[maxn]; n int n; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (int i = 0, x; i < n; i++) cin >> x, a.push_back(x), per[i] = --a.end(); n   sort(per, per + n, n        [](list<int>::iterator &i, list<int>::iterator &j) { return *i < *j; }); n   long long ans = 0, dam = 0; n   for_each(per, per + max(0, n - 2), [&](list<int>::iterator &i) { n     if (i == a.begin() || i == --a.end()) { n       ans += (a.size() - 2) * (*i - dam); n       dam = *i; n     } else { n       auto prv = i, nxt = i; n       prv--, nxt++; n       ans += min(*prv, *nxt) - dam; n     } n     a.erase(i); n   }); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     std::vector<int> a(n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     int minadd = 0; n     int cur = a[0]; n     for (int i = 1; i < n; i++) { n       if (cur > a[i]) { n         minadd = std::max(minadd, cur - a[i]); n       } n       cur = std::max(cur, a[i]); n     } n     int nBits = 0; n     while (minadd) { n       nBits++; n       minadd >>= 1; n     } n     printf( %d n , nBits); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Ans { n   long long maxm, maxp, lmr, rml; n   Ans() { maxm = maxp = lmr = rml = -(1ll << 60); } n } node[100010 << 2]; n void Union(Ans &o, Ans a, Ans b) { n   o.maxm = max(a.maxm, b.maxm); n   o.maxp = max(a.maxp, b.maxp); n   o.lmr = max(max(a.lmr, b.lmr), a.maxp + b.maxm); n   o.rml = max(max(a.rml, b.rml), a.maxm + b.maxp); n } n long long h[100010], d[100010], n; n void build(int o, int l, int r) { n   if (l == r) { n     node[o].maxm = 2 * h[l] - (long long)d[l]; n     node[o].maxp = 2 * h[l] + (long long)d[l]; n     node[o].rml = -(1ll << 60); n     node[o].lmr = -(1ll << 60); n     return; n   } n   build((o << 1), l, ((l + r) >> 1)); n   build((o << 1 | 1), ((l + r) >> 1) + 1, r); n   Union(node[o], node[(o << 1)], node[(o << 1 | 1)]); n } n Ans query(int L, int R, int o = 1, int l = 1, int r = n) { n   Ans ret; n   if (L > R) return ret; n   if (L <= l && r <= R) return node[o]; n   if (R <= ((l + r) >> 1)) return query(L, R, (o << 1), l, ((l + r) >> 1)); n   if (L > ((l + r) >> 1)) n     return query(L, R, (o << 1 | 1), ((l + r) >> 1) + 1, r); n   Union(ret, query(L, R, (o << 1), l, ((l + r) >> 1)), n         query(L, R, (o << 1 | 1), ((l + r) >> 1) + 1, r)); n   return ret; n } n int Q; n Ans ansl, ansr; n int main() { n   scanf( %d%d , &n, &Q); n   for (int i = 1; i <= n; i++) { n     cin >> d[i + 1]; n     d[i + 1] += d[i]; n   } n   for (int i = 1; i <= n; i++) cin >> h[i]; n   build(1, 1, n); n   while (Q--) { n     int l, r; n     scanf( %d %d , &l, &r); n     if (l <= r) { n       ansl = query(r + 1, n); n       ansr = query(1, l - 1); n       cout << max(max(ansr.rml, ansl.rml), ansr.maxp + d[n + 1] + ansl.maxm) n            << endl; n     } else { n       cout << query(r + 1, l - 1).rml << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tot; n long long ans; n int main(int argc, char **argv) { n   scanf( %d%d , &n, &m); n   ++n; n   for (int i = m + 1; (i << 1) <= n && i < n - m; ++i) n     for (int j = m + 1; j < n - m; ++j) { n       tot = (-i * i * 2 + i * j + i * n * 2 - j * n - 1) / (i + j); n       ans += max(min(tot, n - m - 1) - m, 0) * ((i << 1) == n ? 1 : 2); n     } n   printf( %I64d n , ans * 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A, B, C, N; n int main() { n   scanf( %d%d%d%d , &A, &B, &C, &N); n   if (A < C || B < C || A + B >= N + C) n     puts( -1 ); n   else n     printf( %d n , N - (A + B - C)); n } n


#include <bits/stdc++.h> n using namespace std; n template <int mod> n struct modulo { n   int w; n   operator int() const { return w; } n   modulo() {} n   modulo(int w) { n     this->w = w; n     fix(); n   } n   template <class type> n   modulo(type w) { n     this->w = w % mod; n     fix(); n   } n   void inline fix() { n     if (w >= mod || w < -mod) w %= mod; n     if (w < 0) w += mod; n   } n   inline modulo operator-() const { return mod - w; } n   inline modulo &operator+=(const modulo &b) { n     if ((w += b.w) >= mod) w -= mod; n     return *this; n   } n   inline modulo &operator-=(const modulo &b) { n     if ((w -= b.w) < 0) w += mod; n     return *this; n   } n   inline modulo &operator*=(const modulo &b) { n     w = (unsigned long long)w * b.w % mod; n     return *this; n   } n   inline modulo &operator/=(const modulo &b) { return *this *= inv(b); } n   friend modulo operator+(const modulo &a, const modulo &b) { n     return modulo(a) += b; n   } n   template <class type> n   friend modulo operator+(const modulo &a, const type &b) { n     return modulo(a) += b; n   } n   template <class type> n   friend modulo operator+(const type &a, const modulo &b) { n     return modulo(a) += b; n   } n   friend modulo operator-(const modulo &a, const modulo &b) { n     return modulo(a) -= b; n   } n   template <class type> n   friend modulo operator-(const modulo &a, const type &b) { n     return modulo(a) -= b; n   } n   template <class type> n   friend modulo operator-(const type &a, const modulo &b) { n     return modulo(a) -= b; n   } n   friend modulo operator*(const modulo &a, const modulo &b) { n     return modulo(a) *= b; n   } n   template <class type> n   friend modulo operator*(const modulo &a, const type &b) { n     return modulo(a) *= b; n   } n   template <class type> n   friend modulo operator*(const type &a, const modulo &b) { n     return modulo(a) *= b; n   } n   friend modulo operator/(const modulo &a, const modulo &b) { n     return modulo(a) /= b; n   } n   template <class type> n   friend modulo operator/(const modulo &a, const type &b) { n     return modulo(a) /= b; n   } n   template <class type> n   friend modulo operator/(const type &a, const modulo &b) { n     return modulo(a) /= b; n   } n   friend inline modulo inv(const modulo &w) { n     int a = w, m = mod, u = 0, v = 1, t; n     for (; a != 0; t = m / a, m -= t * a, swap(a, m), u -= t * v, swap(u, v)) n       ; n     return u; n   } n   friend inline modulo pow(const modulo &w, int p) { n     if (!w) return 0; n     modulo a = w, r = 1; n     for (; p > 0; a *= a, p >>= 1) n       if (p & 1) r *= a; n     return r; n   } n }; n const int mod = 998244353; n const int nsz = 5e3; n int n; n modulo<mod> f[2][nsz + 5], g[nsz + 5], S[nsz + 5][nsz + 5], ans[nsz + 5]; n modulo<mod> inline C(int n, int m) { return f[0][n] * f[1][m] * f[1][n - m]; } n void inline init() { n   f[0][0] = 1; n   for (int i = 1; i <= n; ++i) f[0][i] = f[0][i - 1] * i; n   f[1][n] = 1 / f[0][n]; n   for (int i = n; i >= 1; --i) f[1][i - 1] = f[1][i] * i; n   S[0][0] = 1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= i; ++j) S[i][j] = S[i - 1][j] * j + S[i - 1][j - 1]; n   } n   for (int i = 0; i <= n; ++i) { n     for (int len = i + 1; len <= n; ++len) n       g[i] += f[0][len - i] * f[1][len] * S[len][len - i]; n   } n } n void inline solve() { n   for (int k = 0; k < n; ++k) { n     for (int i = k; i <= n; ++i) { n       modulo<mod> res = g[i] * C(i, k) * f[0][n]; n       ans[k + 1] += (i - k) & 1 ? -res : res; n     } n   } n } n int main() { n   scanf( %d , &n); n   init(); n   solve(); n   for (int k = 1; k <= n; ++k) printf( %d%c , ans[k].w,   n  [k < n]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> prices[1010]; n int allp[1010]; n int all; n int margin_price; n int above_cnt[1010], margin_cnt[1010], cnt[1010]; n double f[1010][1010], g[1010][1010]; n double comb[1010][1010]; n int main() { n   comb[1][0] = comb[1][1] = 1; n   for (int i = 2; i <= 1000; i++) { n     comb[i][0] = comb[i][i] = 1; n     for (int j = 1; j < i; j++) n       comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; n   } n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int k; n     scanf( %d , &k); n     while (k--) { n       int v; n       scanf( %d , &v); n       prices[i].push_back(v); n       allp[++all] = v; n     } n   } n   sort(allp + 1, allp + all + 1); n   reverse(allp + 1, allp + all + 1); n   margin_price = allp[n]; n   int need_margin = 0; n   for (int i = 1; i <= n; i++) n     if (allp[i] == margin_price) need_margin++; n   for (int i = 1; i <= m; i++) { n     for (int p : prices[i]) { n       if (p > margin_price) n         above_cnt[i]++; n       else if (p == margin_price) n         margin_cnt[i]++; n       cnt[i]++; n     } n   } n   f[0][0] = 1; n   g[0][0] = 1; n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j <= need_margin; j++) { n       if (margin_cnt[i] == 0) { n         f[i][j] += f[i - 1][j] / comb[cnt[i]][above_cnt[i]]; n         g[i][j] += g[i - 1][j]; n       } else if (margin_cnt[i] == 1) { n         f[i][j] += f[i - 1][j] / comb[cnt[i]][above_cnt[i]]; n         g[i][j] += g[i - 1][j]; n         f[i][j + 1] += f[i - 1][j] / comb[cnt[i]][above_cnt[i] + 1]; n         g[i][j + 1] += g[i - 1][j]; n       } else { n         assert(false); n       } n     } n   } n   printf( %.15f n , f[m][need_margin] / g[m][need_margin]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INF = 1000000005; n long long INFF = 1000000000000000005LL; n int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; n int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; n double pi = 3.1415926536; n int MOD = 1e9 + 7; n long long power(long long x, long long n) { n   long long res = 1; n   while (n > 0) { n     if (n & 1) res = res * x; n     x = (x * x); n     n /= 2; n   } n   return res; n } n bool ok(int n, long long d) { n   long long sum = (n * (n + 1) / 2); n   if (sum < d) return false; n   return (d % 2 == sum % 2); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     int i = 0; n     while (!ok(i, abs(a - b))) i++; n     cout << i <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void input() { n   int n; n   cin >> n; n   vector<int> v; n   int i = 1; n   int k = n - 1; n   while (k--) { n     cout <<  ?   << i <<     << i + 1 <<   n ; n     fflush(stdout); n     int x; n     cin >> x; n     v.push_back(x); n     i++; n   } n   cout <<  ?   << 1 <<     << 3 <<   n ; n   fflush(stdout); n   int x; n   cin >> x; n   int a[n + 1]; n   a[1] = x - v[1]; n   for (int i = 2; i <= n; i++) { n     a[i] = v[i - 2] - a[i - 1]; n   } n   cout <<  !  ; n   for (int i = 1; i <= n; i++) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n int main() { n   input(); n   return 0; n } n