#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, j, k, m, t, x, y, sum; n   cin >> t; n   while (t > 0) { n     t = t - 1; n     cin >> n >> k; n     long long a[n], c[k]; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (i = 0; i < k; i++) { n       cin >> c[i]; n     } n     sort(a, a + n); n     sort(c, c + k); n     sum = 0; n     m = -1; n     j = n - 1; n     for (i = 0; i < k; i++) { n       if (c[i] == 1) { n         sum = sum + 2 * a[j]; n         m = i; n       } else n         sum = sum + a[j]; n       c[i] = c[i] - 1; n       j = j - 1; n     } n     j = m + 1; n     i = n - k - 1; n     for (i = i; i >= 0; i = i - 1) { n       while (c[j] > 1) { n         c[j] = c[j] - 1; n         i = i - 1; n       } n       sum = sum + a[i]; n       j = j + 1; n     } n     cout << sum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double eps = 1e-9; n const int INF = 1e9 + 7; n const int MAXN = int(2e5 + 7); n int n, a[3], sum, g[10][10], d[10], ans = INF * 2; n string second[7] = { Anka ,    Chapay ,  Cleo ,        Troll , n                      Dracul ,  Snowy ,   Hexadecimal }; n int get(string a) { n   for (int i = 0; i < 7; i++) n     if (second[i] == a) return i; n } n void check() { n   vector<int> v[3]; n   for (int i = 0; i < 7; i++) v[d[i]].push_back(i); n   int maxi = 0, mini = INF * 2; n   for (int i = 0; i < 3; i++) { n     if (v[i].size() == 0) return; n     maxi = max(maxi, a[i] / (int)v[i].size()); n     mini = min(mini, a[i] / (int)v[i].size()); n   } n   int res = 0; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < v[i].size(); j++) n       for (int k = 0; k < v[i].size(); k++) res += g[v[i][j]][v[i][k]]; n   if (ans > maxi - mini) n     ans = maxi - mini, sum = res; n   else if (ans == maxi - mini && res > sum) n     sum = res; n } n void go(int x) { n   if (x == 7) { n     check(); n     return; n   } n   for (int i = 0; i < 3; i++) { n     d[x] = i; n     go(x + 1); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     string b[3]; n     cin >> b[0] >> b[1] >> b[2]; n     g[get(b[0])][get(b[2])] = 1; n   } n   cin >> a[0] >> a[1] >> a[2]; n   go(0); n   cout << ans <<     << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const unsigned long long inf = 1e18; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<unsigned long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   unsigned long long ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) { n       for (int k = j; k < n; k++) { n         ans = max(ans, (a[i] | a[j] | a[k])); n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int N = 2e6 + 10; n int n, m, x, a[N], fi[N]; n long long pos[N]; n void prepare() { n   scanf( %d%d , &n, &m); n   for (auto i = 0; i < n; i++) scanf( %d , a + i), a[n + i] = a[i]; n   for (auto i = 1; i <= m; i++) scanf( %d , &x), fi[x] = i; n } n int solve() { n   int ans = 0; n   for (int r = 0, l = 0; r < 2 * n; r++) { n     if (!fi[a[r]]) { n       l = r + 1; n       continue; n     } n     if (l < r) n       pos[r] = fi[a[r]] + (pos[r - 1] - fi[a[r]] + m - 1) / m * m; n     else n       pos[r] = fi[a[r]]; n     while (r - l >= n || pos[r] - pos[l] >= m) l++; n     ans = max(ans, r - l + 1); n   } n   return ans; n } n int main() { n   prepare(); n   cout << solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d; n int a[(1 << 20) + 5]; n int sum[(1 << 20) + 5]; n int b[22][100005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> d; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x; n     for (int j = 1; j <= x; j++) { n       cin >> y; n       b[i][max(1, y - d + 1)]++; n       b[i][y + 1]--; n     } n   } n   for (int j = 0; j < m; j++) { n     int c = 0; n     for (int i = 1; i <= n; i++) { n       c += b[j][i]; n       if (c) a[i] += (1 << j); n     } n   } n   int ans = 22; n   for (int i = 1; i <= n - d + 1; i++) sum[a[i]]++; n   for (int j = 0; j < m; j++) n     for (int i = 0; i < (1 << m); i++) n       if ((i & (1 << j))) sum[i] += sum[(i ^ (1 << j))]; n   for (int i = 0; i < (1 << m); i++) n     if (sum[i] == 0) ans = min(ans, m - __builtin_popcount(i)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   long long f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f = ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   x = f ? -x : x; n } n const long long N = 15, M = 105; n long long mp[N][N], f[N][1 << N], n, m, q; n struct lca { n   long long x, y, z; n } L[M]; n struct xy { n   long long x, y; n } B[M]; n bool in(long long x, long long y) { return x & (1 << y); } n long long dfs(long long u, long long mask) { n   if (~f[u][mask]) return f[u][mask]; n   long long &now = f[u][mask]; n   mask ^= (1 << u), now = 0; n   long long pos; n   for (long long i = 0; i < n; i++) n     if (mask & (1 << i)) { n       pos = i; n       break; n     } n   for (long long sub = mask; sub; sub = (sub - 1) & mask) { n     if (!(sub & (1 << pos))) continue; n     long long flag = 0, v = -1; n     for (long long i = 1; i <= q; i++) n       if (in(sub, L[i].z)) { n         if (!in(sub, L[i].x) || !in(sub, L[i].y)) { n           flag = 1; n           break; n         } n       } else if (u == L[i].z) { n         if (in(sub, L[i].x) && in(sub, L[i].y)) { n           flag = 1; n           break; n         } n       } n     if (flag) continue; n     for (long long i = 1; i <= m; i++) n       if (B[i].x != u && B[i].y != u && (in(sub, B[i].x) ^ in(sub, B[i].y))) { n         flag = 1; n         break; n       } n     if (flag) continue; n     for (long long i = 0; i < n; i++) n       if (mp[u][i] && in(sub, i)) n         if (!~v) n           v = i; n         else { n           flag = 1; n           break; n         } n     if (flag) continue; n     if (~v) n       now = (now + dfs(u, mask ^ (1 << u) ^ sub) * dfs(v, sub)); n     else n       for (long long i = 0; i < n; i++) n         if (in(sub, i)) n           now = (now + dfs(u, mask ^ (1 << u) ^ sub) * dfs(i, sub)); n   } n   return now; n } n signed main() { n   read(n), read(m), read(q); n   for (long long i = 1, x, y; i <= m; i++) n     read(B[i].x), read(B[i].y), --B[i].x, --B[i].y, n         mp[B[i].x][B[i].y] = mp[B[i].y][B[i].x] = 1; n   for (long long x, y, z, i = 1; i <= q; i++) n     read(L[i].x), read(L[i].y), read(L[i].z), L[i].x--, L[i].y--, L[i].z--; n   memset(f, -1, sizeof(f)); n   for (long long i = 0; i < n; i++) f[i][1 << i] = 1; n   printf( %lld , dfs(0, (1 << n) - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n long long n, k; n int a[15]; n long long rc[15], p[15], ans; n void solve() { n   scanf( %d%d , &n, &k); n   k++; n   scanf( %d , &a[1]); n   rc[1] = 1; n   long long now = 1, tmp; n   int ss = 0; n   for (int i = 2; i <= n; i++) { n     scanf( %d , &a[i]); n     tmp = 1; n     while (ss < a[i]) now *= 10, tmp *= 10, ss++; n     p[i - 1] = tmp - 1; n     rc[i] = now; n   } n   ans = 0; n   for (int i = 1; i < n; i++) { n     if (k >= p[i]) n       ans += p[i] * rc[i], k -= p[i]; n     else { n       ans += rc[i] * k; n       k = 0; n     } n   } n   if (k) ans += rc[n] * k; n   printf( %lld n , ans); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e5; n int n; n string ans; n int kmp(string s) { n   vector<int> pf(s.size()); n   for (int j = 1; j < s.size(); ++j) { n     int k = pf[j - 1]; n     while (s[k] != s[j]) { n       if (!k) { n         k = -1; n         break; n       } n       k = pf[k - 1]; n     } n     pf[j] = k + 1; n   } n   return pf.back(); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     if (!i) { n       cin >> ans; n       continue; n     } n     string s; n     cin >> s; n     string t = s; n     t +=  # ; n     int a = min((int)ans.size(), (int)s.size()); n     t.insert(t.end(), ans.end() - a, ans.end()); n     int x = kmp(t); n     while (x--) ans.pop_back(); n     ans.insert(ans.end(), s.begin(), s.end()); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const long long int MOD = 1e18 + 7; n long long int modPow(long long int a, long long int b) { n   long long int res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long int MODPow(long long int a, long long int b) { n   long long int res = 1; n   a %= MOD; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n   } n   return res; n } n long long int nCr(long long int n, long long int k) { n   long long int res = 1; n   if (k > n - k) k = n - k; n   for (long long int i = 0; i < k; ++i) { n     res = (res * (n - i)) % mod; n     res = (res * modPow(i + 1, mod - 2)) % mod; n   } n   return res; n } n void Sieve(long long int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (long long int p = 2; p * p <= n; p++) n     if (prime[p] == true) n       for (long long int i = p * 2; i <= n; i += p) prime[i] = false; n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   int a[n], b[n]; n   for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i]; n   sort(b, b + n, greater<int>()); n   map<int, int> mp; n   for (int i = 0; i < m * k; i++) mp[b[i]]++; n   long long int total = 0, sum = 0; n   for (int i = 0; i < m * k; i++) total += b[i]; n   cout << total <<   n ; n   int c = 0; n   std::vector<int> v; n   for (int i = 0; i < n; i++) { n     if (mp[a[i]] > 0) c++, mp[a[i]]--; n     if (c == m) v.push_back(i + 1), c = 0; n   } n   for (int i = 0; i < k - 1; i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename tp> n inline void read(tp& x) { n   x = 0; n   char tmp; n   bool key = 0; n   for (tmp = getchar(); !isdigit(tmp); tmp = getchar()) key = (tmp ==  - ); n   for (; isdigit(tmp); tmp = getchar()) x = (x << 3) + (x << 1) + (tmp ^  0 ); n   if (key) x = -x; n } n template <typename tp> n inline void ckmn(tp& x, tp y) { n   x = x < y ? x : y; n } n template <typename tp> n inline void ckmx(tp& x, tp y) { n   x = x < y ? y : x; n } n const int N = 100010; n struct edge { n   int la, b; n } con[N << 1]; n int tot, first[N]; n void add_edge(int from, int to) { n   con[++tot] = (edge){first[from], to}; n   first[from] = tot; n } n int n, m; n vector<int> ans; n void clear() { n   tot = 0; n   for (int i = (1); i <= (n); ++i) first[i] = 0; n } n int key = 0; n int dep[N], vis[N], inp[N], num[N], mnv[N], dp[N]; n void dfs(int pos) { n   vis[pos] = 1; n   inp[pos] = 1; n   mnv[pos] = pos; n   for (int i = first[pos]; i; i = con[i].la) { n     if (vis[con[i].b]) { n       if (inp[con[i].b]) { n         if (dep[con[i].b] < dep[mnv[pos]]) mnv[pos] = con[i].b; n         ++num[pos]; n         --num[con[i].b]; n       } else n         key = 1; n     } else { n       dep[con[i].b] = dep[pos] + 1; n       dfs(con[i].b); n       num[pos] += num[con[i].b]; n       if (dep[mnv[con[i].b]] < dep[mnv[pos]]) mnv[pos] = mnv[con[i].b]; n     } n   } n   inp[pos] = 0; n } n void fsd(int pos) { n   vis[pos] = 1; n   if (dep[pos] > 0) { n     if (num[pos] == 1) { n       dp[pos] = dp[mnv[pos]]; n     } n   } n   for (int i = first[pos]; i; i = con[i].la) n     if (!vis[con[i].b]) fsd(con[i].b); n } n bool doit(int rt) { n   key = 0; n   for (int i = (1); i <= (n); ++i) vis[i] = 0; n   for (int i = (1); i <= (n); ++i) num[i] = 0; n   dfs(rt); n   if (key) return false; n   for (int i = (1); i <= (n); ++i) dp[i] = 0; n   dp[rt] = 1; n   for (int i = (1); i <= (n); ++i) vis[i] = 0; n   fsd(rt); n   for (int i = (1); i <= (n); ++i) n     if (dp[i]) ans.push_back(i); n   return true; n } n void solve() { n   int x, y; n   int sed = 0; n   read(n), read(m); n   for (int i = (1); i <= (m); ++i) { n     read(x), read(y); n     add_edge(x, y); n     sed = (sed + (long long)233 * x) % 998244353; n   } n   srand(sed); n   ans.clear(); n   for (int _ = (1); _ <= (100); ++_) { n     int rt = ((rand() << 16ll) | rand()) % n + 1; n     if (doit(rt)) break; n   } n   if ((int)ans.size() * 5 < n) { n     puts( -1 ); n   } else { n     for (int i = (0); i <= ((int)ans.size() - 1); ++i) printf( %d  , ans[i]); n     puts(  ); n   } n   clear(); n } n int main() { n   int T; n   read(T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int a, b, c, d, i, j, n, flag = 0; n   cin >> a >> b >> c >> d; n   n = max(max(a, b), max(c, d)); n   flag = 1; n   for (i = 0; i <= n; i++) { n     for (j = 0; j <= n; j++) { n       if (b + i * a == d + j * c) { n         cout << b + i * a << endl; n         flag = 0; n         break; n       } n     } n     if (!flag) break; n   } n   if (flag) cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int m = 0, n = 0, c = 0; n   m = a.length(); n   n = b.length(); n   int x = n - 1; n   if (m != n) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < m; i++) { n     if (a[i] == b[x]) { n       c++; n     } n     x--; n   } n   if (c == m) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n const long long int mod = 1e9 + 7; n using namespace std; n bool prime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n bool isPowerOfTwo(long long int n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n void SieveOfEratosthenes(long long int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (long long int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (long long int p = 2; p <= n; p++) n     if (prime[p]) cout << p <<    ; n } n long long int modularinv(long long int x, long long int n) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return modularinv((x * x) % mod, n / 2); n   else n     return (x * modularinv((x * x) % mod, (n - 1) / 2)) % mod; n } n class node { n  public: n   long long int first; n   bool operator<(const node& rhs) const { return first > rhs.first; } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n, i; n   cin >> n; n   vector<long long int> v(n); n   long long int sum = 0; n   for (i = 0; i < n; i++) { n     cin >> v[i]; n     sum += v[i]; n   } n   long long int count = 0; n   long long int d; n   if (sum % 2 != 0) n     sum = sum / 2 + 1; n   else n     sum = sum / 2; n   for (i = 0; i < n; i++) { n     count += v[i]; n     if (count >= sum) { n       d = i + 1; n       break; n     } n   } n   cout << d; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   cin.get(); n   vector<string> v(n); n   for (int i = 0; i < n; i++) getline(cin, v[i]); n   vector<int> p(k); n   for (int i = 0; i < k; i++) p[i] = i; n   int minDiff = INT_MAX; n   do { n     int minNum, maxNum; n     minNum = INT_MAX; n     maxNum = INT_MIN; n     for (int i = 0; i < n; i++) { n       int num = 0; n       for (int j = 0; j < k; j++) num = num * 10 + v[i][p[j]] -  0 ; n       if (num < minNum) minNum = num; n       if (num > maxNum) maxNum = num; n     } n     minDiff = min(minDiff, maxNum - minNum); n   } while (next_permutation(p.begin(), p.end())); n   cout << minDiff; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 200009; n vector<int> a; n vector<int> b; n vector<int> bb; n int main() { n   long long s, x; n   cin >> s >> x; n   int s1 = s, x1 = x; n   int ans = 0; n   for (int i = 0; i < 45; i++) { n     if ((s & ((long long)1 << i)) && (x & ((long long)1 << i))) ans++; n     if (!(s & ((long long)1 << i)) && (x & ((long long)1 << i))) { n       cout << 0; n       return 0; n     } n     if ((s & ((long long)1 << i)) && !(x & ((long long)1 << i))) { n       cout << 0; n       return 0; n     } n     if (!(s & ((long long)1 << i)) && !(x & ((long long)1 << i))) { n       if ((s & ((long long)1 << (i + 1))) && !(x & ((long long)1 << (i + 1)))) { n         s -= ((long long)1 << (i + 1)); n       } n       if (!(s & ((long long)1 << (i + 1))) && (x & ((long long)1 << (i + 1)))) { n         s -= ((long long)1 << (i + 1)); n       } n     } n   } n   if (s1 != x1) n     cout << ((long long)1 << ans); n   else n     cout << ((long long)1 << ans) - 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, q, d[110]; n void nhap() { n   memset(d, 0, sizeof(d)); n   int x; n   cin >> n >> p; n   for (int i = 1; i <= p; i++) { n     cin >> x; n     d[x] = 1; n   } n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> x; n     d[x] = 1; n   } n } n void cbi() { n   for (int i = 1; i <= n; i++) n     if (!d[i]) { n       cout <<  Oh, my keyboard! ; n       exit(0); n     } n   cout <<  I become the guy. ; n } n void xuli() {} n void ghikq() {} n int main() { n   nhap(); n   cbi(); n   xuli(); n   ghikq(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 600010; n struct wl { n   int k, l, pos; n }; n wl w[MAXN]; n wl temp[MAXN]; n int n; n int p[MAXN]; n int cnt, tcnt; n int *t; n int x[MAXN], y[MAXN]; n int find(int p) { n   int l = 0, r = tcnt; n   while (l < r) { n     int mid = (l + r) / 2; n     if (p > t[mid]) n       l = mid + 1; n     else n       r = mid; n   } n   return l; n } n void init() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     p[cnt++] = x[i]; n     p[cnt++] = y[i]; n   } n   sort(p, p + cnt); n   int new_cnt = 0; n   new_cnt = unique(p, p + cnt) - p; n   t = p; n   cnt = new_cnt; n   tcnt = cnt; n   new_cnt = 0; n   w[new_cnt].k = 0; n   w[new_cnt++].l = t[0] - 1; n   for (int i = 0; i < cnt - 1; i++) { n     w[new_cnt].k = t[i]; n     w[new_cnt++].l = 1; n     w[new_cnt].k = t[i] + 1; n     w[new_cnt++].l = t[i + 1] - t[i] - 1; n   } n   w[new_cnt].k = t[cnt - 1]; n   w[new_cnt++].l = 1; n   cnt = new_cnt; n   for (int i = 0; i < n; i++) { n     int xx = find(x[i]) + 1, yy = find(y[i]) + 1; n     swap(w[(xx - 1) * 2 + 1], w[(yy - 1) * 2 + 1]); n   } n } n long long ans = 0; n long long sum[MAXN]; n void solve(int l, int r) { n   if (l == r) return; n   int p1 = l, p2, p = l; n   int mid = (l + r) / 2; n   p2 = mid + 1; n   solve(l, mid); n   solve(mid + 1, r); n   sum[l] = w[l].l; n   for (int i = l + 1; i <= mid; i++) sum[i] = sum[i - 1] + w[i].l; n   while (p1 <= mid && p2 <= r) { n     if (w[p1].k <= w[p2].k) { n       temp[p++] = w[p1++]; n       continue; n     } n     if (w[p1].k > w[p2].k) { n       temp[p] = w[p2]; n       ans += (sum[mid] - sum[p1] + (long long)w[p1].l) * (long long)(w[p2].l); n       p2++; n       p++; n     } n   } n   for (int i = p2; i <= r; i++) temp[p++] = w[i]; n   for (int i = p1; i <= mid; i++) temp[p++] = w[i]; n   for (int i = l; i <= r; i++) w[i] = temp[i]; n   return; n } n int main() { n   init(); n   solve(0, cnt - 1); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> g[200001], rg[200001]; n bool vis[200001]; n long long int par[200001]; n long long int sz[200001], dnom[200001]; n long long int ans = 0, no = 0; n long long int n, m, S; n deque<long long int> top; n void dfs(long long int x) { n   vis[x] = 1; n   for (auto v : g[x]) { n     if (!vis[v]) dfs(v); n   } n   top.push_front(x); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> S; n   S--; n   vector<pair<long long int, long long int> > g1[n]; n   long long int dis[n]; n   for (int i = 0; i < n; i++) { n     dis[i] = LLONG_MAX / 2; n   } n   for (int i = 0; i < m; i++) { n     long long int u, v, w; n     cin >> u >> v >> w; n     u--; n     v--; n     g1[u].push_back({v, w}); n     g1[v].push_back({u, w}); n   } n   set<pair<long long int, long long int> > second; n   second.insert({0, S}); n   dis[S] = 0; n   while (!second.empty()) { n     pair<long long int, long long int> p = *second.begin(); n     second.erase(second.begin()); n     if (vis[p.second]) continue; n     vis[p.second] = 1; n     long long int cd = p.first, u = p.second; n     for (auto v : g1[u]) { n       if (cd == dis[v.first] + v.second) { n         g[v.first].push_back(u); n         rg[u].push_back(v.first); n       } n       if (dis[v.first] > cd + v.second) { n         dis[v.first] = cd + v.second; n         second.insert({dis[v.first], v.first}); n       } n     } n   } n   memset(vis, 0, sizeof(vis)); n   dfs(S); n   long long int ans = 0; n   dnom[top[0]] = -1; n   for (int i = 1; i < top.size(); i++) { n     long long int u = top[i]; n     long long int x = dnom[rg[u][0]]; n     bool flag = 1; n     for (auto par : rg[u]) { n       if (dnom[par] != x) { n         flag = 0; n         break; n       } n     } n     if (!flag || x == -1) n       dnom[u] = u; n     else n       dnom[u] = x; n     sz[dnom[u]]++; n     ans = max(ans, sz[dnom[u]]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, w[26][26]; n   int money, cost, temp; n   char a[100002], b[100002], x, y; n   char result[100002]; n   for (int i = 0; i < 26; ++i) { n     for (int j = 0; j < 26; ++j) { n       w[i][j] = 1073741823; n     } n     w[i][i] = 0; n   } n   cin >> a >> b; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> x >> y >> cost; n     x -=  a ; n     y -=  a ; n     w[x][y] = min(w[x][y], cost); n   } n   for (int k = 0; k < 26; ++k) { n     for (int i = 0; i < 26; ++i) { n       for (int j = 0; j < 26; ++j) { n         w[i][j] = min(w[i][j], w[i][k] + w[k][j]); n       } n     } n   } n   n = strlen(a); n   money = strlen(b) == n ? 0 : 1073741823; n   for (int i = 0; i < n && money < 1073741823; ++i) { n     cost = 1073741823; n     for (int j = 0; j < 26; ++j) { n       temp = w[a[i] -  a ][j] + w[b[i] -  a ][j]; n       if (temp < cost) { n         cost = temp; n         result[i] =  a  + j; n       } n     } n     money += cost; n   } n   if (money < 1073741823) { n     result[n] =   0 ; n     cout << money << endl << result << endl; n   } else { n     cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e4 + 7; n const long long M = 1e3 + 7; n const long long INF = 1e18; n long long n, m, k, a[N], g, r, ans; n bool h[N][M], ok; n queue<pair<long long, long long> > q; n vector<long long> v; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) cin >> a[i]; n   cin >> g >> r; n   sort(a + 1, a + m + 1); n   v.push_back(1); n   while (!v.empty()) { n     for (long long i : v) q.push({i, g}); n     v.clear(); n     ans += g; n     while (!q.empty()) { n       long long node = q.front().first, t = q.front().second; n       q.pop(); n       if (h[node][t]) continue; n       h[node][t] = 1; n       if (node == m) { n         ans -= t; n         ok = 1; n         break; n       } n       if (t == 0) v.push_back(node); n       if (node > 1 && a[node] - a[node - 1] <= t) n         q.push({node - 1, t - a[node] + a[node - 1]}); n       if (a[node + 1] - a[node] <= t) n         q.push({node + 1, t - a[node + 1] + a[node]}); n     } n     if (ok == 1) break; n     ans += r; n   } n   if (ok) n     cout << ans << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int tobin(string s) { n   int ans = 0; n   for (int i = 0; i < s.size(); i++) { n     ans |= 1 << (s[i] -  a ); n   } n   return ans; n } n int main() { n   int n, mask, save = 0; n   cin >> n; n   mask = (1 << 26) - 1; n   while (n--) { n     char c, c2; n     string s; n     cin >> c; n     if (c ==  . ) { n       cin >> s; n       mask &= ~tobin(s); n     } n     if (c ==  ! ) { n       if (__builtin_popcount(mask) == 1) save++; n       cin >> s; n       mask &= tobin(s); n     } n     if (c ==  ? ) { n       if (__builtin_popcount(mask) == 1 && n != 0) save++; n       cin >> c2; n       mask &= ~(1 << (int)(c2 -  a )); n     } n   } n   cout << save <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long max_n = 1000006; n inline long long read() { n   long long x = 0; n   bool w = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) w |= c ==  - , c = getchar(); n   while (c >=  0  && c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   return w ? -x : x; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 ^ 48); n } n long long n, a[max_n], t[max_n], pr[max_n]; n struct unionfind { n   long long fa[max_n], sz[max_n]; n   inline void clear() { n     for (register long long i = 0; i <= n; ++i) fa[i] = i; n   } n   inline long long find(long long x) { n     if (fa[x] == x) return x; n     return fa[x] = find(fa[x]); n   } n   inline void merge(long long x, long long y) { n     x = find(x), y = find(y); n     if (x == y) return; n     fa[x] = y; n   } n } uf; n long long c[max_n], d[max_n]; n inline void getpr() { n   for (register long long i = 2; i < max_n; ++i) n     if (!pr[i]) n       for (register long long j = i, k = 0; j < max_n; j += i) { n         if (t[j]) { n           if (!k) n             c[i] = uf.find(t[j]); n           else n             uf.merge(k, t[j]); n           k = t[j]; n         } n         pr[j] = i; n       } n } n map<pair<long long, long long>, bool> mp; n signed main() { n   n = read(); n   long long T = read(); n   uf.clear(); n   for (register long long i = 1; i <= n; ++i) { n     a[i] = read(); n     t[a[i]] = i; n   } n   getpr(); n   for (register long long i = 2; i < max_n; ++i) n     if (pr[i] == i) c[i] = uf.find(c[i]); n   for (register long long i = 1; i <= n; ++i) { n     long long j = a[i] + 1, cnt = 0; n     d[++cnt] = uf.find(i); n     while (j != 1) { n       d[++cnt] = c[pr[j]]; n       j /= pr[j]; n     } n     sort(d + 1, d + 1 + cnt), cnt = unique(d + 1, d + 1 + cnt) - d - 1; n     for (register long long j = 1; j < cnt; ++j) n       for (register long long k = j + 1; k <= cnt; ++k) n         mp[make_pair(d[j], d[k])] = 1; n   } n   while (T--) { n     long long x = read(), y = read(); n     x = uf.find(x), y = uf.find(y); n     if (x == y) { n       puts( 0 ); n       continue; n     } n     if (x > y) x ^= y ^= x ^= y; n     if (mp[make_pair(x, y)]) n       puts( 1 ); n     else n       puts( 2 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   bool g; n   int n, m[5001] = {0}, x; n   cin >> n; n   if (n % 2 == 1) { n     v.push_back(1); n     m[1] = 1; n   } else { n     v.push_back(2); n     m[2] = 1; n   } n   while (true) { n     g = false; n     for (int i = 1; i <= n; i++) { n       if (!m[i] && abs(v.back() - i) != 1) { n         v.push_back(i); n         m[i] = 1; n         g = true; n       } n     } n     if (!g) break; n   } n   cout << v.size() <<   n ; n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp_pos[1000010], dp_neg[1000010]; n int mark_pos[1000010], mark_neg[1000010]; n int pos[1000010], neg[1000010], cnt_pos = 0, cnt_neg = 0; n int max_pos, max_neg; n int a[1000010]; n void calc(int* dp, int* mark) { n   int cnt = 0, i, j; n   for (i = 0; i <= max_pos * max_neg; i++) dp[i] = 10000000; n   for (i = 0; i <= 1000; i++) n     if (mark[i]) a[cnt++] = i; n   for (i = 0; i < cnt; i++) dp[a[i]] = 1; n   for (i = 1; i <= max_pos * max_neg; i++) n     for (j = 0; j < cnt && a[j] <= i; j++) dp[i] = min(dp[i], 1 + dp[i - a[j]]); n } n int main() { n   int n, k, i; n   scanf( %d , &n); n   scanf( %d , &k); n   memset(mark_pos, 0, sizeof(mark_pos)); n   memset(mark_neg, 0, sizeof(mark_neg)); n   for (i = 0; i < k; i++) { n     int x; n     scanf( %d , &x); n     if (x >= n) n       mark_pos[x - n] = 1; n     else n       mark_neg[n - x] = 1; n   } n   if (mark_pos[0]) { n     printf( 1 n ); n     return 0; n   } n   max_pos = max_neg = -10000000; n   for (i = 0; i <= 1000; i++) n     if (mark_pos[i]) max_pos = i; n   for (i = 0; i <= 1000; i++) n     if (mark_neg[i]) max_neg = i; n   if (max_neg == -10000000 || max_pos == -10000000) { n     printf( -1 n ); n     return 0; n   } n   calc(dp_pos, mark_pos); n   calc(dp_neg, mark_neg); n   int ans = 2 * 10000000; n   for (i = 1; i <= max_neg * max_pos; i++) n     ans = min(ans, dp_pos[i] + dp_neg[i]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, check = 0; n   string x; n   cin >> n >> x; n   sort(x.begin(), x.end()); n   if (n == 1) n     cout <<  YES ; n   else { n     for (int i = 0; i < x.size() - 1; i++) { n       if (x[i] == x[i + 1]) { n         check = 1; n         break; n       } n     } n     if (check == 1) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 10; n using namespace std; n map<int, int> mp; n struct a { n   int x, y, z; n }; n vector<a> res; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     int t; n     cin >> t; n     mp[t]++; n   } n   priority_queue<pair<int, int>> pq; n   for (auto it = mp.begin(); it != mp.end(); ++it) { n     int fi = it->second; n     int se = it->first; n     pq.push({fi, se}); n   } n   while (pq.size() >= 3) { n     auto xx = pq.top(); n     pq.pop(); n     auto yy = pq.top(); n     pq.pop(); n     auto zz = pq.top(); n     pq.pop(); n     a temp; n     temp.x = xx.second; n     temp.y = yy.second; n     temp.z = zz.second; n     res.push_back(temp); n     if (xx.first - 1) { n       pq.push({xx.first - 1, xx.second}); n     } n     if (yy.first - 1) { n       pq.push({yy.first - 1, yy.second}); n     } n     if (zz.first - 1) { n       pq.push({zz.first - 1, zz.second}); n     } n   } n   cout << res.size() <<   n ; n   for (int i = 0; i < res.size(); ++i) { n     int b[3]; n     b[0] = res[i].x; n     b[1] = res[i].y; n     b[2] = res[i].z; n     sort(b, b + 3); n     cout << b[2] <<     << b[1] <<     << b[0] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(int64_t &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const int64_t &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 1 << 20; n vector<int> pos[SIZE]; n int new_id[SIZE]; n int a[SIZE]; n int dp[SIZE]; n void solve() { n   int n; n   R(n); n   vector<int> d; n   for (int i = 0; i < (n); ++i) { n     R(a[i]); n     d.emplace_back(a[i]); n   } n   for (int i = 0; i < (n); ++i) pos[i].clear(); n   (sort(d.begin(), d.end()), n    d.resize(distance(d.begin(), unique(d.begin(), d.end())))); n   for (int i = 0; i < (n); ++i) n     a[i] = (lower_bound(d.begin(), d.end(), a[i]) - d.begin()); n   int an = 0; n   for (int i = 0; i < (n); ++i) { n     pos[a[i]].emplace_back(i); n   } n   int it = 0; n   for (int i = 0; i < (((int)(d).size())); ++i) { n     an = max(an, ((int)(pos[i]).size())); n     for (int v : pos[i]) new_id[it++] = v; n   } n   for (int i = n - 1; i >= 0; i--) { n     dp[i] = 1; n     if (i + 1 < n && new_id[i] < new_id[i + 1]) dp[i] += dp[i + 1]; n   } n   for (int i = 0; i < (n); ++i) { n     int base = dp[i]; n     int added = 0; n     if (i && a[new_id[i]] != a[new_id[i - 1]]) { n       added += lower_bound((pos[a[new_id[i - 1]]]).begin(), n                            (pos[a[new_id[i - 1]]]).end(), new_id[i]) - n                pos[a[new_id[i - 1]]].begin(); n     } n     if (i + base < n && a[new_id[i + base]] != a[new_id[i + base - 1]]) { n       added += n           pos[a[new_id[i + base]]].end() - n           upper_bound((pos[a[new_id[i + base]]]).begin(), n                       (pos[a[new_id[i + base]]]).end(), new_id[i + base - 1]); n     } n     an = max(an, base + added); n   } n   for (int i = (1); i < (((int)(d).size())); ++i) { n     for (int j = 0; j < (((int)(pos[i]).size())); ++j) { n       an = max(an, ((int)(pos[i]).size()) - j + n                        (int)(lower_bound((pos[i - 1]).begin(), n                                          (pos[i - 1]).end(), pos[i][j]) - n                              pos[i - 1].begin())); n     } n   } n   W(n - an); n } n int main() { n   int ___T; n   scanf( %d , &___T); n   for (int cs = 1; cs <= ___T; cs++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10000], b[10000]; n long long res[10000]; n int val[10000]; n map<long long, int> mp; n int main() { n   int n; n   long long ans = 0; n   int len = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n     if (mp[a[i]] == 2) res[++len] = a[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   for (int i = 1; i <= len; i++) { n     for (int j = 1; j <= n; j++) { n       if ((res[i] & a[j]) == a[j]) val[j] = 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     ans += val[i] * b[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void deba(T *a, int n) { n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n const int N = 5e5 + 10, M = 5e5 + 10; n long long n, m, c[N], t[N], num, ans[N]; n vector<pair<long long, long long> > e3[N]; n vector<long long> e2[M]; n bool mark[M]; n vector<long long> e[M], er[M], topSort; n long long topSort_num[M], comp[M], cnt_comp; n void add(long long x, long long y) { n   e2[x ^ 1].push_back(y); n   e2[y ^ 1].push_back(x); n } n void topol(long long x) { n   mark[x] = 1; n   for (auto u : e[x]) n     if (!mark[u]) topol(u); n   topSort.push_back(x); n   topSort_num[x] = topSort.size(); n } n void dfs(long long x) { n   comp[x] = cnt_comp; n   for (auto u : er[x]) n     if (!comp[u]) dfs(u); n } n bool check(long long x, bool exx = 0) { n   topSort.clear(); n   memset(mark, 0, sizeof(mark)); n   memset(comp, 0, sizeof(comp)); n   cnt_comp = 1; n   for (int i = 0; i < 2 * num; i++) e[i] = e2[i]; n   for (int i = 0; i < m; i++) n     if (t[i] > x) e[2 * i + 1].push_back(2 * i); n   for (int i = 0; i < 2 * num; i++) n     if (!mark[i]) topol(i); n   reverse(topSort.begin(), topSort.end()); n   for (int i = 0; i < 2 * num; i++) er[i].clear(); n   for (int i = 0; i < 2 * num; i++) n     for (auto u : e[i]) er[u].push_back(i); n   for (auto u : topSort) n     if (!comp[u]) { n       dfs(u); n       cnt_comp++; n     } n   for (int i = 0; i < m; i++) { n     if (comp[2 * i] == comp[2 * i + 1]) return 0; n     if (comp[2 * i + 1] > comp[2 * i]) n       ans[i] = 1; n     else n       ans[i] = 0; n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   num = m; n   for (int i = 0; i < m; i++) { n     long long v, u; n     cin >> v >> u >> c[i] >> t[i]; n     e3[v].push_back({c[i], i}); n     e3[u].push_back({c[i], i}); n   } n   for (int i = 1; i < n + 1; i++) { n     if (!e3[i].size()) continue; n     bool flg = 0; n     long long id; n     sort(e3[i].begin(), e3[i].end()); n     for (int j = 1; j < e3[i].size(); j++) { n       if (e3[i][j].first == e3[i][j - 1].first && flg) { n         cout <<  No ; n         return 0; n       } n       if (e3[i][j].first == e3[i][j - 1].first) flg = 1, id = j; n     } n     if (flg) add(2 * e3[i][id].second + 1, 2 * e3[i][id - 1].second + 1); n     long long lst = e3[i][0].second; n     for (int j = 1; j < e3[i].size(); j++) { n       long long ex = e3[i][j].second; n       add(2 * lst, 2 * ex); n       add(2 * ex, 2 * num + 1); n       add(2 * lst, 2 * num + 1); n       lst = num++; n     } n   } n   long long l = -1, r = (long long)N * 10000; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   if (r == N * 10000) { n     cout <<  No ; n     return 0; n   } n   check(r, 1); n   long long res = 0; n   for (int i = 0; i < m; i++) res += ans[i]; n   cout <<  Yes n  << r <<     << res <<   n ; n   for (int i = 0; i < m; i++) n     if (ans[i]) cout << i + 1 <<    ; n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n const int maxm = 305; n long long a[maxn]; n long long c[11][maxn]; n long long num[maxn]; n long long n, m, v, k; n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   long long ans = 0; n   ans += (a[n - 1] * k + a[n - 2]) * (m / (k + 1)); n   long long mod = m % (k + 1); n   ans += (a[n - 1]) * mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, i, j, k, l, T, u, v, q, t = 0, t1 = 0, te, c = 0; n long long int a[1010], b[1010]; n long long int gcd(long long int p, long long int q) { n   if (q == 0) n     return p; n   else n     return gcd(q, p % q); n } n int main() { n   std::ios::sync_with_stdio(false); n   long long int t, w, b; n   cin >> t >> w >> b; n   long long int g = gcd(max(w, b), min(w, b)), lcm, ans = 0; n   double l = w; n   l = l * b; n   l = l / g; n   if (l <= 6e18) { n     lcm = w / g; n     lcm = lcm * b; n     w = min(w, b); n     ans = t / lcm; n     ans = ans * w; n     ans += min(t % lcm, w - 1); n   } else { n     w = min(w, b); n     lcm = t + 1; n     ans += min(t % lcm, w - 1); n   } n   g = gcd(t, ans); n   t = t / g; n   ans = ans / g; n   cout << ans <<  /  << t; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, isum = 0; n   cin >> n; n   if (n % 2 == 0) n     cout << ((n + 1) * (n + 1) - 1) / 4; n   else n     cout << ((n + 1) * (n + 1)) / 4; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long head[1100000], a[1100000]; n struct node { n   long long v, nex; n } bian[2100000]; n long long cnt; n void add(long long x, long long y) { n   bian[++cnt].v = y; n   bian[cnt].nex = head[x]; n   head[x] = cnt; n } n long long tot[1100000], d[1100000], f[1100000], zong; n void dfs(long long x, long long fa) { n   tot[x] = 1; n   for (long long i = head[x]; i; i = bian[i].nex) { n     long long v = bian[i].v; n     if (v == fa) continue; n     dfs(v, x); n     tot[x] += tot[v]; n   } n   f[1] += tot[x]; n } n long long ans = 0; n void dp(long long x, long long fa) { n   ans = max(ans, f[x]); n   for (long long i = head[x]; i; i = bian[i].nex) { n     long long v = bian[i].v; n     if (v == fa) continue; n     f[v] = f[x] - tot[v] + n - tot[v]; n     dp(v, x); n   } n } n signed main() { n   cin >> n; n   for (long long i = 1; i < n; i++) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     add(x, y), add(y, x); n   } n   dfs(1, 0); n   dp(1, 0); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, K, L, R; n long long G, A; n long long ans; n void run(int use, long long ntot) { n   if (N <= 500000) { n     for (int nplus = 0; nplus <= N - 1; nplus++) { n       long long rr = (N + nplus + use); n       long long rv = G + (1 + use); n       long long nbefore = ((ntot - rv) % rr + rr) % rr; n       long long nafter = nplus - nbefore; n       if (nbefore >= 0 && nbefore <= G && nafter >= 0 && nafter <= A) n         ans = max(ans, (long long)nplus + use); n     } n   } else { n     if (G + 1 + use <= ntot) { n       long long rv = ntot - G - 1 - use; n       if (rv <= G) ans = max(ans, rv + use + A); n     } n     for (int nrun = 1; nrun * N <= ntot; nrun++) { n       long long nstart = (nrun * (N + use)) + (G + 1 + use); n       long long nleft = ntot - nstart; n       if (nleft >= 0) { n         long long nbefore = nleft % nrun; n         long long nafter = (nleft - ((nrun + 1) * (nbefore))) / nrun; n         if (nafter > A) { n           long long ndrop = (nafter - A + nrun) / (nrun + 1); n           nafter -= ndrop * (nrun + 1); n           nbefore += ndrop * nrun; n         } n         if (nafter >= 0 && nafter <= A && nbefore >= 0 && nbefore <= G) n           ans = max(ans, nbefore + nafter + use); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> N >> L >> R >> K; n   G = (R - L + N) % N; n   A = (N - 1 - G); n   ans = -1; n   for (int i = 0; i <= 1; i++) { n     for (int j = 0; j <= i; j++) { n       run(i, K + j); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T abs(T t) { n   return t < 0 ? -t : t; n } n const long long modn = 1000000007; n inline long long mod(long long x) { return x % modn; } n const int MAXN = 312345; n int n, m; n vector<long long> s[4]; n struct pd { n   long long x; n   int i[3]; n   pd(long long xx, int ii, int iii) { n     x = xx; n     i[1] = ii; n     i[2] = iii; n   } n   pd() {} n   bool operator<(const struct pd &o) const { n     return (x < o.x || (x == o.x && i[1] < o.i[1])); n   } n } dp[MAXN]; n bool cmp(int a, int b) { return a > b; } n int main() { n   scanf( %d %d , &n, &m); n   long long res = 0; n   for (int a = 0; a < n; a++) { n     long long w, val; n     scanf( %lld %lld , &w, &val); n     s[w].push_back(val); n     if (w == 3) res += val; n   } n   for (int i = 1; i <= 3; i++) sort(s[i].begin(), s[i].end(), cmp); n   dp[0] = (pd){0, 0, 0}; n   for (int i = 0; i < m; i++) { n     dp[i + 1] = max(dp[i], dp[i + 1]); n     if (dp[i].i[1] != s[1].size()) n       dp[i + 1] = max(dp[i + 1], pd(dp[i].x + s[1][dp[i].i[1]], dp[i].i[1] + 1, n                                     dp[i].i[2])); n     if (dp[i].i[2] != s[2].size()) n       dp[i + 2] = max(dp[i + 2], pd(dp[i].x + s[2][dp[i].i[2]], dp[i].i[1], n                                     dp[i].i[2] + 1)); n     ; n   } n   int i = 0, j = 0; n   long long ans = 0; n   for (int k = s[3].size(); k >= 0; k--) { n     if (m < 3 * k) { n       if (k > 0) { n         res -= s[3][k - 1]; n       } n       continue; n     } n     int sob = m - 3 * k; n     ans = max(ans, res + dp[sob].x); n     if (k > 0) res -= s[3][k - 1]; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, MOD = 1e9 + 7; n int d, n, a[N], dp[N], rt; n vector<int> g[N]; n void dfs(int v, int pr = -1) { n   dp[v] = 1; n   for (int to : g[v]) { n     if (to == pr) { n       continue; n     } n     if (a[to] < a[rt] || a[to] > a[rt] + d) { n       continue; n     } n     if (a[to] == a[rt] && to > rt) { n       continue; n     } n     dfs(to, v); n     dp[v] = (1ll * dp[v] * (dp[to] + 1)) % MOD; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> d >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     rt = i; n     dfs(rt, -1); n     ans = (ans + dp[i]) % MOD; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, d, i, j; n   cin >> k >> d; n   if (d != 0) { n     for (i = 1; i <= k; i++) { n       if (i == 1) n         cout << d; n       else n         cout << 0; n     } n   } n   if (d == 0) { n     if (k == 1) n       cout << 0; n     else n       cout <<  No solution ; n   } n } n


#include<bits/stdc++.h> n  n #define ll long long n using namespace std; n const int N = 2e5 + 10; n  n struct Seg_Tree { n     struct { n         int l, r, num; n         ll sum, sL, sR; n     } t[N << 3]; n  n #define update(p) {                                                                     n         t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum;                                   n         t[p].num = t[p << 1].num + t[p << 1 | 1].num;                                   n         t[p].sL = t[p << 1].sL + t[p << 1].sum * t[p << 1 | 1].num + t[p << 1 | 1].sL;  n         t[p].sR = t[p << 1].sR + t[p << 1 | 1].sum * t[p << 1].num + t[p << 1 | 1].sR;  n     } n  n     void build(int p, int l, int r) { n         t[p].l = l, t[p].r = r; n         if (l == r) n             return t[p].sum = t[p].sL = t[p].sR = t[p].num = 0, void(); n         int mid = (l + r) >> 1; n         build(p << 1, l, mid); n         build(p << 1 | 1, mid + 1, r); n         update(p); n     } n  n     void change(int p, int x, ll v) { n         if (t[p].l == t[p].r) { n             t[p].sum += v, t[p].sL += v, t[p].sR += v; n             t[p].num += v < 0 ? -1 : 1; n             return; n         } n         int mid = (t[p].l + t[p].r) >> 1; n         change(x <= mid ? p << 1 : p << 1 | 1, x, v); n         update(p); n     } n  n     ll ask_L(int p, int x) { n         if (t[p].num <= x)return t[p].sum; n         ll val = ask_L(p << 1, x); n         if (x > t[p << 1].num) n             val += ask_L(p << 1 | 1, x - t[p << 1].num); n         return val; n     } n  n     ll ask_R(int p, int x) { n         if (t[p].num <= x)return t[p].sum; n         ll val = ask_R(p << 1 | 1, x); n         if (x > t[p << 1 | 1].num) n             val += ask_R(p << 1, x - t[p << 1 | 1].num); n         return val; n     } n  n     ll ask_sL(int p, int x) { n         if (t[p].num <= x)return t[p].sL; n         ll val = ask_sL(p << 1, x); n         if (x > t[p << 1].num) n             val += t[p << 1].sum * (x - t[p << 1].num) + ask_sL(p << 1 | 1, x - t[p << 1].num); n         return val; n     } n  n     ll ask_sR(int p, int x) { n         if (t[p].num <= x)return t[p].sR; n         ll val = ask_sR(p << 1 | 1, x); n         if (x > t[p << 1 | 1].num) n             val += t[p << 1 | 1].sum * (x - t[p << 1 | 1].num) + ask_sR(p << 1, x - t[p << 1 | 1].num); n         return val; n     } n } S; n  n int n, q; n ll a[N]; n vector<ll> v; n pair<int, ll> p[N]; n #define f(x) (S.ask_R(1, x)-S.ask_L(1,x+1)) n  n inline ll solve() { n     if (n <= 1)return 0ll; n     int l, r, pmx = (n) >> 1; n     if (f(pmx) <= 0)return S.t[1].sR - S.t[1].sL; n     int pl = 1, pr = n - 1; n     if (f(pl) < 0) { n         l = 1, r = pmx; n         while (l < r) { n             int mid = (l + r) >> 1; n             f(mid) >= 0 ? (r = mid) : (l = mid + 1); n         } n         pl = l; n     } n     if (f(pr - 1) < 0) { n         l = pmx, r = n - 2; n         while (l < r) { n             int mid = (l + r + 1) >> 1; n             f(mid) >= 0 ? (l = mid) : (r = mid - 1); n         } n         pr = l + 1; n     } n     ll ans = S.ask_R(1, pr) - S.ask_L(1, pl); n     if (pl > 1)ans += S.ask_sR(1, pl - 1) - S.ask_sL(1, pl - 1); n     if (pr < n - 1)ans += (S.t[1].sR - S.ask_sR(1, pr)) - (S.t[1].sL - S.ask_sL(1, pr)); n     return ans; n } n  n int main() { n     scanf( %d%d , &n, &q); n     for (int i = 1; i <= n; i++) n         scanf( %lld , &a[i]), v.push_back(a[i]); n     for (int i = 1; i <= q; i++) { n         scanf( %d%lld , &p[i].first, &p[i].second); n         v.push_back(p[i].second); n     } n     v.push_back(0); n     sort(v.begin(), v.end()); n     v.erase(unique(v.begin(), v.end()), v.end()); n     for (int i = 1; i <= n; i++) n         a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin(); n     for (int i = 1; i <= q; i++) n         p[i].second = lower_bound(v.begin(), v.end(), p[i].second) - v.begin(); n     S.build(1, 1, (int) v.size() - 1); n     for (int i = 1; i <= n; i++)S.change(1, a[i], v[a[i]]); n     printf( %lld n , solve()); n     for (int i = 1; i <= q; i++) { n         if (p[i].first == 1) { n             n++; n             S.change(1, p[i].second, v[p[i].second]); n         } else { n             n--; n             S.change(1, p[i].second, -v[p[i].second]); n         } n         printf( %lld n , solve()); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long tot, n, w, b, x; n deque<pair<long long, long long> > maxq; n void add(long long val, long long pos) { n   while (maxq.size() > 0 && maxq.back().first <= val) maxq.pop_back(); n   maxq.push_back({val, pos}); n } n long long query() { return maxq.front().first; } n void erase(long long pos) { n   while (maxq.size() > 0 && maxq.front().second < pos) maxq.pop_front(); n } n int main() { n   cin >> n >> w >> b >> x; n   long long c[n], cost[n]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   for (int j = 0; j < n; j++) cin >> cost[j]; n   long long dp[n][10005], temp; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 10005; j++) dp[i][j] = -1000000000000ll * n; n   for (long long j = 0; j < 10001; j++) { n     if (j <= c[0]) { n       if (w - cost[0] * j < 0) n         dp[0][j] = -1000000000000ll * n; n       else n         dp[0][j] = min(w + b * j, w - cost[0] * j + x); n     } else n       dp[0][j] = -1000000000000ll * n; n   } n   for (int i = 1; i < n; i++) { n     maxq.clear(); n     tot = 0; n     for (long long j = 0; j < 10001; j++) { n       add(dp[i - 1][j] - tot, j); n       temp = query() + tot; n       if (temp < 0) n         dp[i][j] = -1000000000000ll * n; n       else n         dp[i][j] = min(w + b * j, temp + x); n       tot -= cost[i]; n       erase(j - c[i] + 1); n     } n   } n   for (int i = 10000; i >= 0; i--) { n     if (dp[n - 1][i] >= 0) { n       cout << i << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   long long int n = s.size(); n   map<char, long long int> r; n   for (long long int i = 0; i < n; i++) { n     r[s[i]]++; n   } n   long long int k = 0, cnt = 0; n   for (auto it = r.begin(); it != r.end(); it++) { n     if (it->second >= 2) n       k++; n     else if (it->second == 1) n       cnt++; n   } n   cout << k + cnt / 2 <<   n ; n   return; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long int t = 1; n   cin >> t; n   for (long long int i = 1; i < t + 1; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isVowel(char c) { n   return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ); n } n int main() { n   vector<string> v; n   int cnt[] = {5, 7, 5}; n   for (int i = 0; i < 3; i++) { n     string s; n     char c; n     cin >> c; n     getline(cin, s); n     s = c + s; n     v.push_back(s); n   } n   for (int i = 0; i < 3; i++) { n     int c = 0; n     for (auto u : v[i]) { n       if (isVowel(u)) c++; n     } n     if (c != cnt[i]) return cout <<  NO n , 0; n   } n   cout <<  YES n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n int n, k, p[N], gk[256], gl[256]; n int main() { n   cin >> n >> k; n   memset(gk, -1, sizeof(gk)); n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   for (int i = 0; i < n; i++) { n     int cc = p[i]; n     if (gk[cc] == -1) { n       int leftmost, len = 0; n       for (int j = cc; j >= 0 && gk[j] == -1 && len < k; j--) { n         leftmost = j; n         len++; n       } n       if (leftmost > 0 && len < k && gl[leftmost - 1] + len <= k) { n         len += gl[leftmost - 1]; n         leftmost = gk[leftmost - 1]; n       } n       for (int j = cc; j >= leftmost; j--) { n         gk[j] = leftmost; n         gl[j] = len; n       } n     } n     p[i] = gk[cc]; n   } n   for (int i = 0; i < n; i++) { n     cout << p[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(228); n const int M = 1791791791; n const int B = 239; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<string> a(n), b(m); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n   } n   map<int, int> ans; n   for (int i = 0; i + m - 1 < n; i++) { n     int kek = 0; n     for (int x = i; x <= i + m - 1; x++) { n       for (int y = 0; y < m; y++) { n         kek = (kek * (long long)B + a[x][y]) % M; n       } n     } n     ans[kek] = i; n   } n   for (int j = 0; j + m - 1 < n; j++) { n     int kek = 0; n     for (int x = 0; x < m; x++) { n       for (int y = j; y <= j + m - 1; y++) { n         kek = (kek * (long long)B + b[x][y]) % M; n       } n     } n     if (ans.count(kek)) { n       cout << ans[kek] + 1 <<     << j + 1 <<   n ; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 1e1; n long long n; n long long r, avg; n long long must, ans = 0, sum = 0; n pair<long long, long long> d[MAXN]; n int main() { n   cin >> n >> r >> avg; n   long long x, y; n   for (long long i = 1; i <= n; i++) { n     scanf( %I64d%I64d , &x, &y); n     sum += x; n     d[i] = make_pair(y, x); n   } n   must = max(n * avg - sum, 1LL * 0); n   sort(d + 1, d + n + 1); n   long long i = 1; n   while (must) { n     ans += min(must, r - d[i].second) * d[i].first; n     must -= min(must, r - d[i].second); n     i++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001]; n stack<int> st; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   long long int sum = 0; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sum += a[0]; n   for (int i = 1; i < n; i++) sum += max(a[i] - a[i - 1], 0); n   cout << sum <<   n ; n   for (int i = 0; i < a[0]; i++) st.push(0); n   for (int i = 1; i < n; i++) { n     if (a[i] - a[i - 1] > 0) { n       for (int j = 0; j < a[i] - a[i - 1]; j++) st.push(i); n     } else if (st.size() != 0) { n       int num = abs(a[i] - a[i - 1]); n       for (int j = 0; j < num; j++) { n         int inde = st.top(); n         inde++; n         st.pop(); n         cout << inde <<     << i <<   n ; n       } n     } n   } n   for (int i = 0; i < a[n - 1]; i++) { n     if (st.size() != 0) { n       cout << st.top() + 1 <<     << n <<   n ; n       st.pop(); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long xx, yy, n, m, i, g, j, k, x, y; n   std::vector<long long> v; n   vector<pair<long long, long long>> vp; n   cin >> n >> m; n   map<pair<long long, long long>, long long> mp; n   for (i = 0; i < m; i++) { n     cin >> x >> y; n     vp.push_back({x, y}); n     mp[{x, y}]++; n   } n   for (i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       v.push_back(i); n       v.push_back(n / i); n     } n     if ((n % i == 0 && i == n / i) || i == 1) { n       v.pop_back(); n     } n   } n   for (auto itr : v) { n     g = 1; n     k = itr; n     for (i = 0; i < m; i++) { n       xx = vp[i].first + k; n       yy = vp[i].second + k; n       if (xx >= n + 1) { n         xx = xx % (n + 1) + 1; n       } n       if (yy >= n + 1) { n         yy = yy % (n + 1) + 1; n       } n       if (mp.find({xx, yy}) == mp.end() && mp.find({yy, xx}) == mp.end()) { n         g = 0; n         break; n       } n     } n     if (g) { n       cout <<  Yes  n            <<   n ; n       return 0; n     } n   } n   cout <<  No  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sum = 0, n, m; n const long long mod = 1e9 + 7; n long long f[25]; n long long qpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n long long getc(long long a, long long b) { n   if (a < b) return 0; n   if (b > a - b) b = a - b; n   long long s1 = 1, s2 = 1; n   for (long long i = 0; i < b; i++) { n     s1 = s1 * (a - i) % mod; n     s2 = s2 * (i + 1) % mod; n   } n   return s1 * qpow(s2, mod - 2) % mod; n } n long long lucas(long long n, long long k) { n   if (k == 0) return 1; n   return getc(n % mod, k % mod) * lucas(n / mod, k / mod) % mod; n } n long long solve() { n   long long ans = 0; n   for (int i = 0; i < (1 << n); i++) { n     long long cnt = 0, sum = m; n     for (int j = 0; j < n; j++) { n       if (i & (1 << j)) { n         sum -= f[j] + 1; n         cnt++; n       } n     } n     if (sum < 0) continue; n     if (cnt & 1) n       ans -= lucas(sum + n - 1, n - 1); n     else n       ans += lucas(sum + n - 1, n - 1); n     ans %= mod; n   } n   return (ans + mod) % mod; n } n int main() { n   scanf( %lld%lld , &n, &m); n   for (int i = 0; i < n; i++) scanf( %lld , &f[i]); n   printf( %lld n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1e5 + 10; n const long long mod = 998244353; n const int LARGEST = 0x3f3f3f3f; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; n int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int bx[] = {0, 1, 0, -1}; n int by[] = {-1, 0, 1, 0}; n using namespace std; n int a[inf], stmin[inf][20], stmax[inf][20]; n pair<int, int> q[inf]; n int dp[inf]; n int n, s, len, head, tail; n int ask(int x, int y) { n   int tlen = log(y - x + 1) / log(2); n   int p = max(stmax[x][tlen], stmax[y - (1 << tlen) + 1][tlen]); n   int q = min(stmin[x][tlen], stmin[y - (1 << tlen) + 1][tlen]); n   return p - q; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> s >> len; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     stmax[i][0] = stmin[i][0] = a[i]; n   } n   for (int j = 1; j < 20; j++) n     for (int i = 1; i <= n; i++) n       if (i + (1 << (j - 1)) <= n) { n         stmax[i][j] = max(stmax[i][j - 1], stmax[i + (1 << (j - 1))][j - 1]); n         stmin[i][j] = min(stmin[i][j - 1], stmin[i + (1 << (j - 1))][j - 1]); n       } n   if (len > n || ask(1, len) > s) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) dp[i] = LARGEST; n   dp[0] = 0; n   dp[len] = 1; n   head = tail = 1; n   q[tail++] = make_pair(dp[0], 0); n   for (int i = len + 1; i <= n; i++) { n     int l = 1, r = i - len + 1, ans = -1; n     while (l < r) { n       int mid = (l + r) >> 1; n       if (ask(mid, i) <= s) n         r = ans = mid; n       else n         l = mid + 1; n     } n     if ((ask(l, i) <= s) && ans == -1) n       ans = l; n     else if (ans != -1 && (ask(l, i) <= s)) n       ans = min(ans, l); n     while (head < tail && q[tail - 1].first >= dp[i - len]) tail--; n     q[tail++] = make_pair(dp[i - len], i - len); n     if (ans != -1) { n       while (head < tail && q[head].second < ans - 1) head++; n       if (head < tail) dp[i] = min(dp[i], q[head].first + 1); n     } n   } n   if (dp[n] != LARGEST) n     cout << dp[n] << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n const double PI = acos(-1.0); n using namespace std; n const int MAXN = 200117, MAXM = 1200117, C = 5, MAXX = 1000000120; n int n, k, a[MAXN], b, c, m, p[MAXM]; n long long ans, s[C]; n priority_queue<long long> q[C]; n inline void add(int x) { n   int rx = ((x % C) + C) % C; n   for (int i = 0; i < C; i++) { n     int d = c * ((i < rx) ? (C + i - rx) : (i - rx)); n     long long xx = n         d + 1LL * b * n                 (((MAXX + i) - (x + ((i < rx) ? (C + i - rx) : (i - rx)))) / C); n     if (((int)(q[i]).size()) < k) n       s[i] += xx, q[i].push(xx); n     else if (q[i].top() > xx) n       s[i] -= q[i].top() - xx, q[i].pop(), q[i].push(xx); n   } n } n int main() { n   scanf( %d %d %d %d , &n, &k, &b, &c); n   b = min(b, 5 * c); n   for (int i = 0; i < n; scanf( %d , &a[i++])) n     ; n   for (int i = 0; i < n; i++) n     for (int j = 0; j <= 5; j++) p[m++] = a[i] + j; n   sort(p, p + m); n   m = unique(p, p + m) - p; n   sort(a, a + n); n   ans = (2117117117117117117LL); n   for (int i = 0, j = 0; i < m; i++) { n     for (; j < n && p[i] >= a[j]; add(a[j++])) n       ; n     int t = ((p[i] % C) + C) % C; n     if (((int)(q[t]).size()) == k) n       ans = min(ans, s[t] - 1LL * k * b * (((MAXX + t) - p[i]) / C)); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int po(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y >>= 1; n     x = (x * x) % p; n   } n   return res % p; n } n long long int modInverse(long long int n, long long int p) { n   return po(n, p - 2, p); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int n; n   cin >> n; n   long long int cnt = 0; n   for (long long int i = 1; i < n + 1; i++) { n     long long int c = 0; n     long long int x = i; n     for (long long int j = 2; j * j <= i; j++) { n       if (x % j == 0) { n         c++; n         while (x % j == 0) x /= j; n       } n     } n     if (x != 1) c++; n     if (c == 2) cnt++; n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n struct listNode { n   long long int n; n   listNode *next; n   listNode(long long int n = 0LL) : n(n), next(NULL) {} n }; n long long int num; n listNode *edge[100100], *tail[100100]; n long long int color[100100]; n void insertEdge(long long int a, long long int b) { n   if (!edge[a]) { n     edge[a] = new listNode(b); n     tail[a] = edge[a]; n   } else { n     tail[a]->next = new listNode(b); n     tail[a] = tail[a]->next; n   } n } n void dfs(long long int node, long long int parent, long long int &res0, n          long long int &res1) { n   long long int a, b, temp; n   if (color[node]) { n     res0 = 0LL; n     res1 = 1LL; n   } else { n     res0 = 1LL; n     res1 = 0LL; n   } n   for (listNode *ptr = edge[node]; ptr; ptr = ptr->next) { n     if (ptr->n == parent) continue; n     dfs(ptr->n, node, a, b); n     if (color[node]) { n       res1 *= (a + b); n       if (res1 >= 1000000007) res1 %= 1000000007; n     } else { n       res1 *= (a + b); n       if (res1 >= 1000000007) res1 %= 1000000007; n       temp = res0 * b; n       if (temp >= 1000000007) temp %= 1000000007; n       res1 += temp; n       if (res1 >= 1000000007) res1 %= 1000000007; n       res0 *= (a + b); n       if (res0 >= 1000000007) res0 %= 1000000007; n     } n   } n } n int main() { n   long long int i, a, b; n   scanf( %I64d , &num); n   for (i = 1; i <= num - 1; i++) { n     scanf( %I64d , &a); n     insertEdge(i, a); n     insertEdge(a, i); n   } n   for (i = 0; i < num; i++) scanf( %I64d , color + i); n   dfs(0, 0, a, b); n   printf( %I64d n , b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       (i + j) % 2 ? cout <<  B  : cout <<  W ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1; n   int tc; n   cin >> tc; n   getchar(); n   while (tc--) { n     cin >> s1; n     for (int i = 0; i < s1.length(); i += 2) { n       cout << s1[i]; n     } n     cout << s1[s1.size() - 1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long row1[n], row2[n]; n   for (int i = 0; i < n; i++) cin >> row1[i]; n   for (int i = 0; i < n; i++) cin >> row2[i]; n   long long first, second, pref = 0, pres = 0; n   for (int i = 0; i < n; i++) { n     first = max(pref, pres + row1[i]); n     second = max(pres, pref + row2[i]); n     pref = first; n     pres = second; n   } n   cout << max(pref, pres); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n], i, j, ans = 1; n     int c[201][n + 1]; n     vector<int> v[201]; n     memset(c, 0, sizeof(c)); n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       v[a[i]].push_back(i); n       c[a[i]][i + 1]++; n     } n     for (i = 1; i <= 200; i++) { n       for (j = 1; j <= n; j++) c[i][j] += c[i][j - 1]; n       ans = max(ans, c[i][n]); n     } n     for (i = 1; i <= n / 2; i++) { n       for (j = 1; j <= 200; j++) { n         int l = v[j].size(); n         if (2 * i <= l) { n           for (int k = 1; k <= 200; k++) n             ans = max(ans, 2 * i + c[k][v[j][l - i]] - c[k][v[j][i - 1] + 1]); n         } n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n const int maxn = 100010; n char s[maxn]; n int main() { n   int a = 0, b = 0, c = 0, i; n   scanf( %s , s); n   int len = strlen(s); n   if (s[0] ==  )  || s[len - 1] ==  (  || s[0] ==  # ) { n     printf( -1 n ); n     return 0; n   } n   for (i = len - 1; i >= 0; i--) { n     if (s[i] ==  ( ) a++; n     if (s[i] ==  ) ) b++; n     if (a > b) { n       printf( -1 n ); n       return 0; n     } n     if (s[i] ==  # ) break; n   } n   a = 0, b = 0; n   for (i = 0; i < len; i++) { n     if (s[i] ==  ( ) a++; n     if (s[i] ==  ) ) { n       if (s[i - 1] ==  ( ) { n         s[i - 1] =  . ; n         s[i] =  . ; n         b--; n         a--; n       } n       b++; n     } n     if (s[i] ==  # ) { n       b++; n       c++; n     } n     if (b > a) { n       printf( -1 n ); n       return 0; n     } n   } n   b -= c; n   if (a - b < c) { n     printf( -1 n ); n     return 0; n   } n   a = 0; n   for (i = 0; i < len; i++) { n     if (s[i] ==  ( ) a++; n     if (s[i] ==  ) ) { n       b--; n       a--; n     } n     if (s[i] ==  # ) { n       if (c > 1) { n         printf( 1 n ); n         a--; n         c--; n       } else n         printf( %d n , a - b); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int a, int b) { n   if (a > b) n     return b; n   else n     return a; n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     long long int n; n     cin >> n; n     vector<int> a(n + 1, 0); n     vector<int> ans(n + 1, 0); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     vector<int> ones; n     vector<int> zeros; n     ; n     string s; n     cin >> s; n     int one = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  1 ) { n         ones.push_back(a[i + 1]); n       } else n         zeros.push_back(a[i + 1]); n     } n     int k = ones.size(); n     int max = n - k; n     sort(ones.begin(), ones.end()); n     sort(zeros.begin(), zeros.end()); n     map<int, int> m; n     map<int, int> p; n     int j = n; n     for (int i = k - 1; i >= 0; i--) { n       m[ones[i]] = j; n       j--; n     } n     for (int i = 0; i < max; i++) { n       p[zeros[i]] = i + 1; n     } n     for (int i = 1; i <= n; i++) { n       if (s[i - 1] ==  0 ) { n         ans[i] = p[a[i]]; n       } else { n         ans[i] = m[a[i]]; n       } n     } n     for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<vector<int> > D; n int main(int argc, char **argv) { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     string t; n     cin >> t; n     D.push_back(vector<int>()); n     for (int j = 0; j < m; j++) { n       D[D.size() - 1].push_back(t[j] ==  1 ); n     } n   } n   int c0[400][400], c1[400][400]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int k; n       for (k = 0;; k++) { n         if (i + k >= n || j + k >= m || D[i + k][j + k]) break; n       } n       c0[i][j] = k; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int k; n       for (k = 0;; k++) { n         if (i + k >= n || j - k < 0 || D[i + k][j - k]) break; n       } n       c1[i][j] = k; n     } n   int s = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (D[i][j]) continue; n       for (int k = 1;; k++) { n         if (j + k >= m || i + k >= n || D[i][j + k] || D[i + k][j]) break; n         if (c1[i][j + k] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (j - k < 0 || i + k >= n || D[i][j - k] || D[i + k][j]) break; n         if (c0[i][j - k] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (j + k >= m || i - k < 0 || D[i][j + k] || D[i - k][j]) break; n         if (c0[i - k][j] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (j - k < 0 || i - k < 0 || D[i][j - k] || D[i - k][j]) break; n         if (c1[i - k][j] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (i + k >= n || j + k >= m || j - k < 0 || D[i][j + k] || D[i][j - k]) n           break; n         if (c0[i][j - k] > k && c1[i][j + k] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (i - k < 0 || j + k >= m || j - k < 0 || D[i][j + k] || D[i][j - k]) n           break; n         if (c0[i - k][j] > k && c1[i - k][j] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (i - k < 0 || i + k >= n || j + k >= m || D[i - k][j] || D[i + k][j]) n           break; n         if (c0[i - k][j] > k && c1[i][j + k] > k) s += 1; n       } n       for (int k = 1;; k++) { n         if (i - k < 0 || i + k >= n || j - k >= m || D[i - k][j] || D[i + k][j]) n           break; n         if (c0[i][j - k] > k && c1[i - k][j] > k) s += 1; n       } n     } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111; n const int INF = (int)1e9; n const int mod = (int)1e9 + 7; n int n, m; n vector<int> v[N]; n int get(int i, int j) { n   if (i > j) swap(i, j); n   int s = j - i; n   s = min(s, m - s); n   return s; n } n int get_res(int x, int y) { n   auto left = v[y].begin(), right = v[y].end(); n   --right; n   int s = get(x, *left); n   s = min(s, get(x, *right)); n   auto it = upper_bound(v[y].begin(), v[y].end(), x); n   if (it != v[y].end()) { n     s = min(s, get(x, *it)); n   } n   if (it != v[y].begin()) { n     --it; n     s = min(s, get(x, *it)); n   } n   return s; n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       char x; n       x = getchar(); n       if (x ==  1 ) v[i].push_back(j); n     } n     scanf(  n ); n   } n   int ans = (int)1e9; n   for (int j = 0; j < m; ++j) { n     int res = 0; n     for (int i = 0; i < n; ++i) { n       if (!v[i].size()) { n         puts( -1 ); n         return 0; n       } n       res += get_res(j, i); n     } n     ans = min(ans, res); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n int read() { n   register int x = 0; n   register char f = 1, ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = !f; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^  0 ); n   return f ? x : -x; n } n int n, fac[200005], dp[200005], sz[200005]; n int edge, to[200005 << 1], pr[200005 << 1], hd[200005]; n void addedge(int u, int v) { to[++edge] = v, pr[edge] = hd[u], hd[u] = edge; } n void dfs(int u, int fa = 0) { n   sz[u] = 1, dp[u] = 1; n   int d = 0; n   for (register int i = hd[u], v; i; i = pr[i]) n     if ((v = to[i]) != fa) n       dfs(v, u), sz[u] += sz[v], ++d, dp[u] = 1ll * dp[u] * dp[v] % 998244353; n   if (!fa) n     dp[u] = 1ll * n * dp[u] % 998244353 * fac[d] % 998244353; n   else n     dp[u] = 1ll * (d + 1) * dp[u] % 998244353 * fac[d] % 998244353; n } n int main() { n   n = read(); n   for (register int i = 1, u, v; i < n; ++i) n     u = read(), v = read(), addedge(u, v), addedge(v, u); n   fac[0] = 1; n   for (register int i = 1; i <= n; ++i) n     fac[i] = 1ll * fac[i - 1] * i % 998244353; n   dfs(1); n   printf( %d n , dp[1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 4; n int n, k, a[N]; n bool used[N], inStack[N]; n set<int> s; n set<int> q; n void add(int& it) { n   if (s.size() && *s.begin() < a[it]) { n     cout << -1; n     exit(0); n   } n   inStack[a[it]] = 1; n   used[a[it]] = 1; n   s.insert(a[it]); n   ++it; n } n void sovle() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     q.insert(i); n   } n   for (int i = 0; i < k; ++i) { n     cin >> a[i]; n     used[a[i]] = 1; n     q.erase(a[i]); n   } n   int it = 0; n   for (int i = 1; i <= n;) { n     if (used[i]) { n       if (inStack[i]) { n         int top = *s.begin(); n         if (top != i) { n           cout << -1; n           exit(0); n         } else { n           s.erase(top); n           ++i; n         } n       } else { n         add(it); n       } n     } else { n       if (it < k) { n         add(it); n       } else { n         if (s.empty()) { n           a[it] = *q.rbegin(); n           add(it); n           q.erase(*q.rbegin()); n         } else { n           auto findIt = q.lower_bound(*s.begin()); n           if (findIt == q.begin()) { n             cout << -1; n             exit(0); n           } else { n             --findIt; n             a[it] = *findIt; n             add(it); n             q.erase(findIt); n           } n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     cout << a[i] <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   sovle(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MAXN = 100; n int cnt[MAXN]; n int pre[MAXN]; n ll fact[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   fact[0] = 1; n   for (int i = 1; i < MAXN; i++) { n     fact[i] = fact[i - 1] * i; n   } n   int kase; n   cin >> kase; n   while (kase--) { n     memset(cnt, 0, sizeof(cnt)); n     memset(pre, 0, sizeof(pre)); n     ll n; n     cin >> n; n     int idx = 2; n     while (n) { n       cnt[n % idx]++; n       n /= idx; n       idx++; n     } n     pre[0] = cnt[0]; n     for (int i = 1; i <= idx; i++) { n       pre[i] = pre[i - 1] + cnt[i]; n     } n     ll same = 1; n     for (int i = 0; i < idx; i++) { n       same *= fact[cnt[i]]; n     } n     ll ans = 1; n     idx--; n     for (int i = 1; i < idx; i++) { n       ans *= (pre[i] - i + 1); n     } n     ll cost = pre[0]; n     for (int i = 0; i < idx; i++) { n       pre[i]--; n     } n     for (int i = 1; i < idx - 1; i++) { n       cost *= (pre[i] - i + 1); n     } n     cout << (ans - cost) / same - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int d[5001]; n int t[5001]; n int l[5001]; n int r[5001]; n int x[5001]; n vector<int> cl, cr, num, cx; n int eksi[5001]; n int ek[5001][5001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) d[i] = 1000000000; n   for (int ad = 1; ad <= m; ad++) { n     cin >> t[ad] >> l[ad] >> r[ad] >> x[ad]; n     if (t[ad] == 1) n       for (int i = l[ad]; i <= r[ad]; i++) eksi[i] += x[ad]; n     else { n       int ma = -1000000000; n       for (int i = l[ad]; i <= r[ad]; i++) ma = max(ma, d[i] + eksi[i]); n       if (ma < x[ad]) { n         cout <<  NO  << endl; n         return 0; n       } else n         for (int i = l[ad]; i <= r[ad]; i++) n           if (d[i] > x[ad] - eksi[i]) d[i] = x[ad] - eksi[i]; n     } n   } n   for (int i = 1; i <= n; i++) eksi[i] = d[i]; n   for (int i = 1; i <= m; i++) { n     if (t[i] == 1) { n       for (int j = l[i]; j <= r[i]; j++) eksi[j] += x[i]; n     } else if (t[i] == 2) { n       int ma = -1000000000; n       for (int j = l[i]; j <= r[i]; j++) ma = max(ma, eksi[j]); n       if (x[i] != ma) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) cout << d[i] <<    ; n   cout << 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 -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void wrote(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) wrote(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   wrote(x); n   puts(  ); n } n const long long maxn = 111; n double p[maxn], win, tim; n long long n, a[maxn]; n int main() { n   n = read(); n   win = 1; n   for (long long i = 1; i <= n; ++i) { n     p[i] = (a[i] = read()) * 0.01; n     win *= p[i]; n   } n   tim = n; n   for (long long it = 0; ((1 - win) > 1e-10) && (it <= 500000); it++) { n     tim += 1 - win; n     long long cho = 0; n     double cho_v = 0; n     for (long long i = 1; i <= n; ++i) { n       double now_v = (1 - p[i]) * a[i] * 0.01 / p[i]; n       if (now_v > cho_v) cho_v = now_v, cho = i; n     } n     win = 1; n     p[cho] += (1 - p[cho]) * a[cho] * 0.01; n     for (long long i = 1; i <= n; ++i) win = win * p[i]; n   } n   printf( %lf n , tim); n } n


#include <bits/stdc++.h> n using namespace std; n char g[8][8]; n int calc(int x, int y, int a, int d) { n   int res = 0; n   while (x != d) { n     x += a; n     ++res; n     if (g[x][y] !=  . ) return 2e9; n   } n   return res; n } n int main() { n   for (int i = 0; i < 8; ++i) scanf( %s , g[i]); n   int a = 2e9, b = 2e9; n   for (int i = 0; i < 8; ++i) n     for (int j = 0; j < 8; ++j) { n       if (g[i][j] ==  W ) a = min(a, calc(i, j, -1, 0)); n       if (g[i][j] ==  B ) b = min(b, calc(i, j, 1, 7)); n     } n   if (a <= b) n     puts( A ); n   else n     puts( B ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   long long n, d; n   cin >> n >> d; n   long long a[105] = {0}; n   long long pref[105] = {0}; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] % 2 != 0) pref[i]++; n     if (i != 1) pref[i] += pref[i - 1]; n   } n   long long z = 0; n   long long b[105] = {0}; n   for (long long i = 2; i < n; i++) { n     if (i - pref[i] == pref[i]) { n       z++; n       b[z] = abs(a[i] - a[i + 1]); n     } n   } n   sort(b + 1, b + 1 + z); n   long long v = 1; n   while (d >= 0) { n     if (v > z) break; n     if (d - b[v] >= 0) { n       d -= b[v]; n       v++; n     } else n       break; n   } n   cout << v - 1; n } n


#include<bits/stdc++.h> n #define ll long long int n #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); n  n using namespace std; n int main(){ n             ll t; n             cin>>t; n             while(t--){ n                         ll n,k1,k2,w,b; n                         cin>>n>>k1>>k2; n                         cin>>w>>b; n                          n                         int m=(k1+k2)/2; n                         int c=((n*2)-(k1+k2))/2; n                         n                         if(m>=w && c>=b){ n                                     cout<< YES <<  n ; n                         }else{ n                                     cout<< NO <<  n ; n                         } 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 <typename 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 <typename 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 <typename T> n inline void println(T first) { n   print(first), putchar(  n ); n } n template <typename T> n inline void printsp(T first) { n   print(first), putchar(   ); n } n template <class T, class T1> n inline void print(T first, T1 second) { n   printsp(first), println(second); n } n template <class T, class T1, class T2> n inline void print(T first, T1 second, T2 z) { n   printsp(first), printsp(second), 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 int first[1555], second[1555]; n int ans[1555], sz[1555]; n vector<int> adj[1555]; n void dfs(int u, int fa = -1) { n   sz[u] = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == fa) continue; n     dfs(v, u); n     sz[u] += sz[v]; n   } n } n long double alp[1555][1555]; n int id0; n bool cmp(int u, int v) { return alp[u][id0] + 1e-14 < alp[v][id0]; } n int id[1555]; n void solve(int u, int fa, int *id, int n) { n   if (n == 1) { n     ans[id[0]] = u; n     return; n   } n   for (int i = 1; i < n; i++) { n     if (second[id[i]] < second[id[0]]) swap(id[0], id[i]); n   } n   ans[id[0]] = u; n   id0 = id[0]; n   sort(id + 1, id + n, cmp); n   int st = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == fa) continue; n     solve(v, u, id + st, sz[v]); n     st += sz[v]; n   } n } n int main() { n   srand(time(NULL)); n   int n; n   gn(n); n   for (int i = 1; i < n; i++) { n     int u, v; n     gn(u, v); n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   int u = rand() % n; n   dfs(u); n   for (int i = 0; i < n; i++) gn(first[i], second[i]); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) n       if (i != j) n         alp[i][j] = atan2l(second[j] - second[i], first[j] - first[i]); n   } n   for (int i = 0; i < n; i++) id[i] = i; n   solve(u, -1, id, n); n   for (int i = 0; i < n; i++) printsp(ans[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[3005][3005]; n string s, t; n int n, m; n inline long long solve(int l, int r) { n   int id = r - l + 1; n   if (r >= n) return 0ll; n   if (l == 0) { n     for (int i = r + 1; i < m; i++) { n       if (t[i] != s[id]) return 0ll; n       id++; n     } n     return 1ll * (n - id + 1ll); n   } n   long long &ret = dp[l][r]; n   if (ret != -1ll) return ret; n   ret = 0ll; n   if (r + 1 < m) { n     if (s[id] == t[r + 1]) ret = (ret + solve(l, r + 1)) % 998244353; n   } else n     ret = (ret + solve(l, r + 1)) % 998244353; n   if (l - 1 < m) { n     if (s[id] == t[l - 1]) ret = (ret + solve(l - 1, r)) % 998244353; n   } else n     ret = (ret + solve(l - 1, r)) % 998244353; n   return ret; n } n int main() { n   memset((dp), (-1), sizeof((dp))); n   ; n   cin >> s >> t; n   n = s.length(); n   m = t.length(); n   long long ans = 0ll; n   for (int i = 0; i < n; i++) { n     if (i < m && t[i] == s[0]) { n       ans = (ans + 2 * solve(i, i)) % 998244353; n     } else if (i >= m) { n       ans = (ans + 2 * solve(i, i)) % 998244353; n     } n   } n   printf( %lld n , ans); n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long sign = 1, x = 0; n   char s = getchar(); n   while (s >  9  || s <  0 ) { n     if (s ==  - ) sign = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = (x << 3) + (x << 1) + s -  0 ; n     s = getchar(); n   } n   return x * sign; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x / 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n int n, k; n int a[(100 + 5)]; n int main() { n   int kase; n   cin >> kase; n   while (kase--) { n     cin >> n; n     if (n == 1 || n == 2) { n       cout << 1 << endl; n       continue; n     } n     int ans = (n + 1) / 2; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000]; n int s[200000]; n int n, p, k; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int tc; n   cin >> tc; n   while (tc--) { n     cin >> n >> p >> k; n     for (int i = 0; i < n; ++i) cin >> a[i]; n     sort(a, a + n); n     int mx; n     if (a[0] <= p) { n       mx = 1; n     } else { n       cout <<  0 n ; n       continue; n     } n     s[0] = a[0]; n     for (int i = 1; i <= k - 2; ++i) { n       s[i] = s[i - 1] + a[i]; n       if (s[i] <= p) mx = i + 1; n     } n     for (int i = 0; i <= k - 1; i++) { n       int z = 0; n       int y = 0; n       for (int j = i + k - 1; j < n; j += k) { n         if (z + a[j] > p) break; n         z += a[j]; n         y++; n       } n       if (i > 0) { n         int add = upper_bound(s, s + i, p - z) - s - 1; n         if (add >= 0 && p - z >= s[add]) mx = max(mx, y * k + add + 1); n       } else n         mx = max(mx, y * k); n     } n     cout << mx <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[101], nxt, l, h; n int main(int argc, const char* argv[]) { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int mn = 10001; n   for (int i = 0; i < n; i++) { n     nxt = i + 1; n     nxt %= n; n     if (mn > abs(a[i] - a[nxt])) { n       mn = abs(a[i] - a[nxt]); n       l = i; n       h = nxt; n     } n   } n   printf( %d %d n , l + 1, h + 1); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, t; n int main() { n   cin >> n >> t; n   if (n == 8 && t == 10) { n     cout << 11111110; n     return 0; n   } n   if (n == 18 & t == 10) { n     cout << 111111111111111110; n     return 0; n   } n   if (n == 1 && t == 10) { n     cout << -1; n     return 0; n   } n   if (t == 10) { n     for (int i = 0; i < n - 1; i++) { n       cout << 1; n     } n     cout << 0; n     return 0; n   } n   cout << t; n   for (int i = 0; i < n - 1; i++) { n     cout << 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int l, r; n   Node(int l, int r) : l(l), r(r) {} n   bool operator<(const Node &o) const { n     return r - l == o.r - o.l ? r > o.r : r - l > o.r - o.l; n   } n }; n vector<int> event; n int arr[200000]; n map<int, int> matching; n map<int, int> cloak_pos; n set<Node> heap; n int query[100000 + 10][3]; n int query_pos[100000 + 10]; n void modify(int pos, int delta) { n   while (pos < event.size()) { n     arr[pos] += delta; n     pos += pos & -pos; n   } n } n int sum(int pos) { n   int ret = 0; n   while (pos > 0) { n     ret += arr[pos]; n     pos -= pos & -pos; n   } n   return ret; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < q; ++i) { n     scanf( %d , &query[i][0]); n     if (query[i][0] == 0) { n       scanf( %d%d , &query[i][1], &query[i][2]); n     } n   } n   matching[1] = n; n   matching[n] = 1; n   heap.insert(Node(1, n)); n   event.push_back(0); n   for (int i = 0; i < q; ++i) { n     if (query[i][0] > 0) { n       int x = query[i][0]; n       if (cloak_pos.count(x)) { n         int y = cloak_pos[x]; n         int l1, r1, l2, r2; n         if (matching.count(y - 1)) { n           l1 = matching[y - 1]; n           r1 = y - 1; n           matching.erase(y - 1); n           heap.erase(Node(l1, r1)); n         } else { n           l1 = r1 = y; n         } n         if (matching.count(y + 1)) { n           l2 = y + 1; n           r2 = matching[y + 1]; n           matching.erase(y + 1); n           heap.erase(Node(l2, r2)); n         } else { n           l2 = r2 = y; n         } n         matching[l1] = r2; n         matching[r2] = l1; n         heap.insert(Node(l1, r2)); n         query_pos[i] = -y; n         cloak_pos.erase(x); n       } else { n         Node cur = *heap.begin(); n         heap.erase(heap.begin()); n         int y = (cur.l + cur.r) / 2; n         if ((cur.r - cur.l + 1) % 2 == 0) { n           ++y; n         } n         matching.erase(cur.r); n         if (cur.r != cur.l) { n           matching.erase(cur.l); n         } n         event.push_back(y); n         query_pos[i] = y; n         if (y + 1 <= cur.r) { n           matching[y + 1] = cur.r; n           matching[cur.r] = y + 1; n           heap.insert(Node(y + 1, cur.r)); n         } n         if (cur.l <= y - 1) { n           matching[y - 1] = cur.l; n           matching[cur.l] = y - 1; n           heap.insert(Node(cur.l, y - 1)); n         } n         cloak_pos[x] = y; n       } n     } n   } n   sort(event.begin(), event.end()); n   event.erase(unique(event.begin(), event.end()), event.end()); n   for (int i = 0; i < q; ++i) { n     if (query[i][0] > 0) { n       if (query_pos[i] < 0) { n         int y = -query_pos[i]; n         int t = lower_bound(event.begin(), event.end(), y) - event.begin(); n         modify(t, -1); n       } else { n         int y = query_pos[i]; n         int t = lower_bound(event.begin(), event.end(), y) - event.begin(); n         modify(t, +1); n       } n     } else { n       int x = n           lower_bound(event.begin(), event.end(), query[i][1]) - event.begin(); n       int y = upper_bound(event.begin(), event.end(), query[i][2]) - n               event.begin() - 1; n       int ans = 0; n       if (x <= y) { n         ans = sum(y) - sum(x - 1); n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, n, s, p, b, c; n   cin >> k >> n >> s >> p; n   float a = (n * 1.0) / s; n   b = ceil(a); n   c = b * k; n   float d = (c * 1.0) / p; n   int t = ceil(d); n   cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, cur_div; n vector<long long> cnt; n bool check(long long x) { n   long long tot = 0; n   for (long long i = 0; i < 26; i++) { n     tot += cnt[i] / x; n     if (tot >= cur_div) { n       return true; n     } n   } n   return false; n } n void solve() { n   string second; n   cin >> n >> k; n   cin >> second; n   if (n == k) { n     cout << n <<   n ; n     return; n   } n   vector<long long> divs; n   for (long long i = 1; i * i <= k; i++) { n     if (k % i) continue; n     long long div0 = i, div1 = k / i; n     divs.push_back(div0); n     if (div0 != div1) { n       divs.push_back(div1); n     } n   } n   cnt.assign(26, 0); n   for (char &c : second) { n     cnt[c -  a ]++; n   } n   long long ans = 1; n   for (long long i = 0; i < divs.size(); i++) { n     cur_div = divs[i]; n     long long l = 1, r = n / cur_div; n     while (l <= r) { n       long long mid = (l + r) >> 1; n       if (check(mid)) { n         ans = max(ans, mid * cur_div); n         l = mid + 1; n       } else { n         r = mid - 1; n       } n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cout <<  { ; n   __print(x.first); n   cout <<  , ; n   __print(x.second); n   cout <<  } ; n } n template <typename T> n void __print(const T &x) { n   int first = 0; n   cout <<  { ; n   for (auto &i : x) cout << (first++ ?  ,  :   ), __print(i); n   cout <<  } ; n } n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n template <class T> n void remdup(vector<T> &v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), end(v)); n } n template <typename T> n struct cmp { n   bool operator()(const T &p1, const T &p2) {} n }; n const long long N = 1e5 + 100; n const long long mod = 1e9 + 7; n const long long X = 2e5 + 100; n long long dp[205][205][205], n, kk; n string s, t; n long long solve(long long nn, long long k, long long a) { n   if (nn == n) return 0; n   if (dp[nn][k][a] != -1) return dp[nn][k][a]; n   long long a1 = 0; n   long long a2 = 0; n   if (t[0] == s[nn]) a1++; n   if (t[1] == s[nn]) a2 = a; n   long long ans = a2 + solve(nn + 1, k, a + a1); n   for (long long i = (0); i < (2); ++i) { n     if (k == 0) break; n     a1 = 0, a2 = 0; n     if (t[i] == t[0]) a1++; n     if (t[i] == t[1]) a2 = a; n     ans = max(ans, a2 + solve(nn + 1, k - 1, a + a1)); n   } n   return dp[nn][k][a] = ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   memset(dp, -1, sizeof(dp)); n   cin >> n >> kk; n   cin >> s >> t; n   cout << solve(0, kk, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   char b, c; n   int n, m; n   int l, r; n   cin >> n >> m; n   cin >> s; n   for (int j = 1; j <= m; j++) { n     cin >> l >> r >> b >> c; n     for (int i = l - 1; i < r; i++) { n       if (s[i] == b) { n         s[i] = c; n       } n     } n   } n   for (int i = 0; i < n; i++) cout << s[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long r; n   vector<long long> a; n   cin >> r; n   for (int i = 0; i < r; i++) { n     long long aux; n     cin >> aux; n     a.push_back(aux); n   } n   a.push_back(a[0]); n   long long n = 0, m = 1; n   long long min = abs(a[0] - a[1]); n   for (int i = 1; i < r; i++) { n     if (abs(a[i] - a[i + 1]) < min) { n       n = i % r; n       m = (i + 1) % r; n       min = abs(a[i] - a[i + 1]); n     } n   } n   cout << n + 1 <<     << m + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n uint64_t l, r, s; n set<uint64_t> S; n void f(uint64_t x) { n   S.insert(x); n   if (x < r) f(x * 10 + 4), f(x * 10 + 7); n } n int main() { n   cin >> l >> r; n   for (f(0); l <= r;) { n     auto it = S.lower_bound(l); n     s += (min(r, *it) - l + 1) * *it; n     l = *it + 1; n   } n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long ab(long long x) { return x > 0LL ? x : -x; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k, sum = 0, x; n   cin >> n >> k; n   vector<long long> v; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     sum += x; n     v.push_back(x); n   } n   long long ans = -1; n   for (long long i = 0; i < k; i++) { n     long long sum2 = 0; n     for (long long j = i; j < n; j += k) sum2 += v[j]; n     ans = max(ans, ab(sum - sum2)); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[10010]; n char b[10010], c[1010], d[1010]; n int main() { n   while (scanf( %s%s , a, b) != EOF) { n     int len = strlen(a); n     sort(a, a + len); n     int flag = 0; n     for (int i = 0; i < len; i++) { n       if (a[i] !=  0 ) { n         flag = i; n         char t = a[i]; n         a[i] = a[0]; n         a[0] = t; n         break; n       } n     } n     if (strcmp(a, b) == 0) { n       printf( OK n ); n     } else { n       printf( WRONG_ANSWER n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = LLONG_MAX; n void solve() { n   long long k; n   cin >> k; n   string s; n   cin >> s; n   long long test[10] = {0}; n   long long sum = 0; n   for (long long i = 0; i < s.size(); i++) { n     int digit = s[i] -  0 ; n     test[digit]++; n     sum = sum + digit; n   } n   long long ans = 0; n   for (int i = 0; i < 9; i++) { n     for (long long j = 0; j < test[i]; j++) { n       if (sum < k) { n         ans++; n         sum = sum + 9 - i; n       } n     } n   } n   cout << ans << endl; n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n