#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n constexpr auto INF = 9223372036854775807; n using namespace std; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n inline long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int mod_power(long long int x, long long int y) { n   long long int res = 1; n   x = x % 1000000007; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res; n } n long long modInverse(long long n) { return mod_power(n, 1000000007 - 2); } n long long nCrModPFermat(long long n, long long int r) { n   if (r == 0) return 1; n   vector<long long int> fac(n + 1, 0); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % 1000000007; n   return (fac[n] * modInverse(fac[r]) % 1000000007 * modInverse(fac[n - r]) % n           1000000007) % n          1000000007; n } n bool isprime(long long int n) { n   if (n == 2) return true; n   if (n % 2 == 0 || n == 1) { n     return false; n   } n   for (long long int j = 3; j <= sqrt(n); j += 2) { n     if (n % j == 0) { n       return false; n     } n   } n   return true; n } n vector<vector<long long int>> arr; n vector<bool> visited; n vector<long long int> out_time; n vector<long long int> pre_time_vertex; n vector<long long int> vertex_pre_time; n vector<long long int> in_time; n vector<long long int> depth; n long long int cnt = 1, d = 0, cnt2 = 1; n void dfs(long long int u) { n   pre_time_vertex[cnt] = u; n   vertex_pre_time[u] = cnt; n   in_time[u] = cnt2; n   cnt2++; n   cnt++; n   visited[u] = true; n   for (long long int i : arr[u]) { n     if (!visited[i]) { n       depth[i] = depth[u] + 1; n       dfs(i); n     } n   } n   out_time[u] = cnt2; n   cnt2++; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int n, q; n   cin >> n >> q; n   vector<long long int> parent(n + 1, -1); n   for (long long int i = (long long int)2; i < (long long int)n + 1; i += 1) { n     cin >> parent[i]; n   } n   arr.resize(n + 1, vector<long long int>()); n   visited.resize(n + 1, false); n   pre_time_vertex.resize(n + 1, 0); n   vertex_pre_time.resize(n + 1, 0); n   out_time.resize(n + 1, 0); n   in_time.resize(n + 1, 0); n   depth.resize(n + 1, 0); n   for (long long int i = (long long int)2; i < (long long int)n + 1; i += 1) { n     arr[parent[i]].push_back(i); n     arr[i].push_back(parent[i]); n   } n   for (long long int i = (long long int)1; i < (long long int)n + 1; i += 1) { n     sort(arr[i].begin(), arr[i].end()); n   } n   dfs(1); n   for (long long int i = (long long int)0; i < (long long int)q; i += 1) { n     long long int u, k; n     cin >> u >> k; n     long long int req_pre = vertex_pre_time[u] + k - 1; n     if (k == 1) { n       cout << u <<   n ; n     } else if (req_pre <= n && depth[pre_time_vertex[req_pre]] > depth[u]) { n       long long int vertex = pre_time_vertex[req_pre]; n       if (out_time[u] > out_time[vertex]) n         cout << pre_time_vertex[req_pre] <<   n ; n       else n         cout << -1 <<   n ; n     } else { n       cout << -1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int d[n]; n   d[0] = a[0]; n   map<int, int> even, odd; n   even[d[0]]++; n   odd[0]++; n   long long int c = 0; n   for (int i = 1; i < n; i++) { n     d[i] = a[i] ^ d[i - 1]; n     if (i % 2 == 0) { n       even[d[i]]++; n       if (even.find(d[i]) != even.end()) c += even[d[i]] - 1; n     } else { n       odd[d[i]]++; n       if (odd.find(d[i]) != odd.end()) c += odd[d[i]] - 1; n     } n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n; n   cin >> n; n   double sum = 0, y; n   for (int i = 0; i < n; i++) { n     cin >> y >> y; n     sum += y; n   } n   sum = sum / n; n   cout << sum + 5 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, t, m, n; n   cin >> t; n   int arr[t]; n   for (i = 0; i < t; i++) { n     int sum = 0, k = 0; n     cin >> n >> m; n     int arr1[n]; n     for (int j = 0; j < n; j++) { n       cin >> arr1[j]; n       if (j != 0) sum += arr1[j]; n     } n     k = arr1[0]; n     while (k < m) { n       if (sum != 0) { n         k++; n         sum--; n       } else n         break; n     } n     arr[i] = k; n   } n   for (i = 0; i < t; i++) cout << arr[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> small; n vector<long long int> large; n long long int largest(long long int digits, long long int sum) { n   long long int i; n   if (digits == 0) return sum; n   for (i = 9; i >= 0; i--) { n     if (sum >= i) { n       large.push_back(i); n       sum = sum - i; n       break; n     } n   } n   return largest(digits - 1, sum); n } n long long int smallest(long long int digits, long long int sum) { n   long long int i; n   if (digits == 0) return sum; n   for (i = 0; i <= 9; i++) { n     if (sum >= i && ((sum - i) <= 9 * (digits - 1))) { n       small.push_back(i); n       sum = sum - i; n       break; n     } n   } n   return smallest(digits - 1, sum); n } n int main() { n   long long int i, j, t, n, m, temp, sum1, sum; n   cin >> n >> sum; n   sum1 = sum; n   for (i = 1; i <= 9; i++) { n     if (sum >= i && ((sum - i) <= 9 * (n - 1))) { n       small.push_back(i); n       sum = sum - i; n       break; n     } n   } n   temp = smallest(n - 1, sum); n   if (temp > 0 || sum1 == 0) { n     if (sum1 == 0 && n == 1) n       cout << 0 <<    ; n     else n       cout << -1 <<    ; n   } else { n     for (i = 0; i < small.size(); i++) cout << small[i]; n     cout <<    ; n   } n   temp = largest(n, sum1); n   if (temp > 0 || sum1 == 0) { n     if (sum1 == 0 && n == 1) n       cout << 0 << endl; n     else n       cout << -1 << endl; n   } else { n     for (i = 0; i < large.size(); i++) cout << large[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long n, m, k; n long long mpow(long long b, long long e) { n   if (e == 0) return 1; n   if (e == 1) return b % MOD; n   long long tmp = mpow(b, e / 2) % MOD; n   tmp = tmp * tmp % MOD; n   if (e % 2) tmp = tmp * (b % MOD) % MOD; n   return tmp; n } n int main() { n   cin >> n >> m >> k; n   if (n % 2 != m % 2 && k == -1) n     cout << 0 << endl; n   else { n     long long r = mpow(2, n - 1) % MOD; n     r = mpow(r, m - 1) % MOD; n     cout << r << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int segtree[4 * ((24 * 3600) + 100)], lazy[4 * ((24 * 3600) + 100)]; n void propagate(int root, int l, int r) { n   segtree[root] += lazy[root]; n   if (l != r) { n     lazy[2 * root + 1] += lazy[root]; n     lazy[2 * root + 2] += lazy[root]; n   } n   lazy[root] = 0; n } n int query(int root, int l, int r, int a, int b) { n   if (b < l || r < a) return 0; n   propagate(root, l, r); n   if (a <= l && r <= b) return segtree[root]; n   int mid = (l + r) / 2; n   return max(query(2 * root + 1, l, mid, a, b), n              query(2 * root + 2, mid + 1, r, a, b)); n } n void update(int root, int l, int r, int a, int b, int val) { n   if (b < l || r < a) return; n   if (a <= l && r <= b) { n     lazy[root] += val; n     return; n   } n   int mid = (l + r) / 2; n   update(2 * root + 1, l, mid, a, b, val); n   update(2 * root + 2, mid + 1, r, a, b, val); n   propagate(2 * root + 1, l, mid); n   propagate(2 * root + 2, mid + 1, r); n   segtree[root] = max(segtree[2 * root + 1], segtree[2 * root + 2]); n } n int n, M, T, h, m, s; n int main() { n   scanf( %d %d %d , &n, &M, &T); n   vector<int> resp; n   int user = 0; n   int ult = -1; n   for (int i = 0; i < n; i++) { n     scanf( %d:%d:%d , &h, &m, &s); n     int ini = h * 3600 + m * 60 + s; n     int fim = ini + T - 1; n     int val = query(0, 0, ((24 * 3600) + 100), ini, fim); n     if (val < M) { n       user++; n       resp.push_back(user); n       update(0, 0, ((24 * 3600) + 100), ini, fim, 1); n     } else { n       update(0, 0, ((24 * 3600) + 100), max(ult + 1, ini), fim, 1); n       resp.push_back(user); n     } n     ult = fim; n   } n   if (query(0, 0, ((24 * 3600) + 100), 0, ((24 * 3600) + 100)) != M) n     printf( No solution n ); n   else { n     printf( %d n , user); n     for (int i = 0; i < resp.size(); i++) { n       printf( %d n , resp[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void Read(int &x) { n   char c; n   while (c = getchar(), c != EOF) n     if (c >=  0  && c <=  9 ) { n       x = c -  0 ; n       while (c = getchar(), c >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n       ungetc(c, stdin); n       return; n     } n } n int m, n, a[100000 + 10], b[100000 * 2 + 10], f[100000 + 10], x; n void read() { n   Read(m), Read(n); n   int i; n   for (i = 1; i <= n; i++) Read(a[i]), b[i] = a[i]; n   for (i = 1; i <= n; i++) b[n + i] = b[i] + m; n } n bool check(int x) { n   int i; n   f[0] = -0x7fffffff; n   f[1] = 1; n   for (i = 2; i <= n; i++) { n     f[i] = -0x7fffffff; n     if (f[i - 1] >= a[i] - 1) f[i] = a[i] + x; n     if (f[i - 1] >= a[i] - x - 1) f[i] = max(f[i], a[i]); n     if (f[i - 2] >= a[i] - x - 1) f[i] = max(f[i], a[i - 1] + x); n   } n   if (f[n] + x >= m) return 1; n   f[1] = 1 + x, f[2] = max(1 + x, a[2]); n   if (a[2] - a[1] - 1 > x + x) return 0; n   for (i = 3; i <= n; i++) { n     f[i] = -0x7fffffff; n     if (f[i - 1] >= a[i] - 1) f[i] = a[i] + x; n     if (f[i - 1] >= a[i] - x - 1) f[i] = max(f[i], a[i]); n     if (f[i - 2] >= a[i] - x - 1) f[i] = max(f[i], a[i - 1] + x); n   } n   if (f[n] >= min(m, m + a[2] - x - 1)) return 1; n   return 0; n } n int Divide_Conqure(int l, int r) { n   int mid; n   while (l < r) { n     mid = (l + r) >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   return l; n } n void solve() { n   int i, mx, p; n   mx = b[n + 1] - b[n]; n   p = 1; n   for (i = 2; i <= n; i++) n     if (b[i] - b[i - 1] > mx) { n       mx = b[i] - b[i - 1]; n       p = i; n     } n   a[1] = 1; n   for (i = 2; i <= n; i++) { n     a[i] = a[i - 1] + b[p + 1] - b[p]; n     p++; n   } n   printf( %d n , Divide_Conqure(0, mx - 1)); n } n int main() { n   read(); n   if (n == 1) { n     printf( %d n , m - 1); n     return 0; n   } n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool isp[300005]; n void sv() { n   long long i, j; n   memset(isp, true, sizeof(isp)); n   isp[1] = false; n   for (i = 2; i * i < 300005; i++) { n     if (isp[i]) { n       for (j = i * i; j < 300005; j += i) isp[j] = false; n     } n   } n } n long long powr(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, i, j, a[300005], x, y; n   map<long long, vector<long long> > mp; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]].push_back(i); n   } n   long long ans = 0; n   auto it = mp.begin(); n   while (it != mp.end()) { n     (it->second).push_back(n); n     it++; n   } n   auto itr = mp.begin(); n   while (itr != mp.end()) { n     long long l = (itr->second).size(); n     for (i = l - 1; i > 0; i--) { n       if ((itr->second)[i] != (itr->second)[i - 1] + 1) { n         long long tmp = (itr->second)[i] - (itr->second)[0] - 1; n         if (tmp > ans) { n           ans = tmp; n         } else n           break; n       } n     } n     itr++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int N = 1e6 + 5; n const int K = (1 << 17) + 10; n const long long int INF = 2e18 + 500; n const int inf = 0x7fffffff; n const int SQRT = 500; n const int lgN = 20; n int main(int argc, char const *argv[]) { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   stack<int> S; n   int ans = 0; n   int req = 0; n   for (int i = 0; i < 2 * n; i++) { n     string s; n     cin >> s; n     if (s[0] ==  a ) { n       int x; n       cin >> x; n       S.push(x); n     } else { n       req += 1; n       if (S.size() == 0) { n         continue; n       } else { n         int x = S.top(); n         if (x == req) { n           S.pop(); n         } else { n           ans += 1; n           S = stack<int>(); n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[31]; n int main() { n   long long t, n; n   long long q = 0; n   cin >> t; n   for (int i = 0; i <= 29; i++) { n     a[i] = pow(2, i); n   } n   for (int i = 1; i <= t; i++) { n     cin >> n; n     if (n % 2 == 0) { n       q = (n + 1) * (n / 2); n     } else { n       q = ((n / 2) + 1) * n; n     } n     for (int i = 0; i <= 29; i++) { n       if (a[i] <= n) { n         q = q - (a[i] * 2); n       } else { n         break; n       } n     } n     cout << q << endl; n     q = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mpow(int base, int exp); n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> v(n); n   vector<long long int> cnt(2 * n + 1, 0); n   for (long long int i = 0; i < (n); i++) { n     cin >> v[i]; n     cnt[v[i]]++; n   } n   vector<long long int> out; n   for (long long int i = 0; i < (n); i++) { n     out.push_back(v[i]); n     bool flag = true; n     for (long long int j = v[i] + 1; j <= 2 * n; j++) { n       if (cnt[j] == 0) { n         out.push_back(j); n         cnt[j]++; n         flag = false; n         break; n       } n     } n     if (flag) { n       cout << -1 <<   n ; n       return; n     } n   } n   for (auto i : out) cout << i <<    ; n   cout <<   n ; n   ; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n int mpow(int base, int exp) { n   base %= 1000000007; n   int result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long int)result * base) % 1000000007; n     base = ((long long int)base * base) % 1000000007; n     exp >>= 1; n   } n   return result; n } n


#include <bits/stdc++.h> n const int N = 1 << 20; n int n, m, lim; n char s[22][100005]; n int64_t a[N], b[N]; n void fwt(int64_t* a, int f) { n   for (int i = 1; i < lim; i <<= 1) n     for (int j = 0; j < lim; j += i << 1) n       for (int k = 0; k < i; ++k) { n         int64_t x = a[j + k], y = a[j + k + i]; n         a[j + k] = x + y >> f; n         a[j + k + i] = x - y >> f; n       } n } n int main() { n   scanf( %d%d , &n, &m); n   lim = 1 << n; n   for (int i = 0; i < lim; ++i) n     b[i] = std::min(__builtin_popcount(i), n - __builtin_popcount(i)); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s[i]); n     for (int j = 0; j < m; ++j) s[i][j] ^=  0 ; n   } n   for (int i = 0; i < m; ++i) { n     int p = 0; n     for (int j = 0; j < n; ++j) p |= s[j][i] << j; n     ++a[p]; n   } n   fwt(a, 0), fwt(b, 0); n   for (int i = 0; i < lim; ++i) a[i] *= b[i]; n   fwt(a, 1); n   std::cout << *std::min_element(a, a + lim) << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n int n; n int a[maxn]; n int p; n double dp[maxn][maxn]; n double release(int x) { n   double res = 1; n   for (register double i = x + 1; i <= n; i++) { n     res *= (i - x); n     res /= i; n   } n   return res; n } n signed main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   scanf( %d , &p); n   dp[0][0] = 1; n   for (register int i = 1; i <= n; i++) { n     for (register int j = n - 1; j >= 0; j--) { n       for (register int k = p - a[i]; k >= 0; k--) { n         dp[j + 1][k + a[i]] += dp[j][k]; n       } n     } n   } n   double ans = 0; n   for (register int i = 1; i <= n; i++) { n     for (register int j = 0; j <= p; j++) { n       if (dp[i][j]) { n         ans += dp[i][j] * release(i); n       } n     } n   } n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r, id; n   bool operator<(const node& nd) const { n     return l == nd.l ? r > nd.r : l < nd.l; n   } n } a[300010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) { n     scanf( %d%d , &a[i].l, &a[i].r); n     a[i].id = i; n   } n   sort(a, a + n); n   int idx = 0; n   for (int i = (1); i < (n); ++i) { n     if (a[i].r <= a[idx].r) { n       printf( %d %d n , a[i].id + 1, a[idx].id + 1); n       return 0; n     } else { n       idx = i; n     } n   } n   puts( -1 -1 ); n   return 0; n } n


#include <bits/stdc++.h> n int perm[100010 + 1]; n using namespace std; n int main() { n   int n; n   cin >> n; n   int s = 1; n   int e = n; n   while (e - s > 0) { n     perm[s] = s + 1; n     perm[s + 1] = e; n     perm[e] = e - 1; n     perm[e - 1] = s; n     s += 2; n     e -= 2; n   } n   if (s == e) perm[s] = s; n   if ((n % 4) > 1) n     cout << -1; n   else n     for (int i = 1; i <= n; i++) cout << perm[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, m, cnt, head[N], vis[N], in[N], u, v, ans, op[N]; n int idx, dfn[N], g[N], f[N]; n struct edge { n   int v, nxt; n } e[N]; n void add(int u, int v) { n   e[++cnt] = (edge){v, head[u]}; n   head[u] = cnt; n } n int dfs(int u) { n   vis[u] = in[u] = 1; n   for (int i = head[u], v; v = e[i].v, i; i = e[i].nxt) n     if (in[v] || (!vis[v] && !dfs(v))) return 0; n   return in[dfn[++idx] = u] = 0, 1; n } n int main() { n   cin >> n >> m; n   for (int i = (1); i <= (m); i++) scanf( %d%d , &u, &v), add(u, v); n   for (int i = (1); i <= (n); i++) n     if (!vis[i] && !dfs(i)) return puts( -1 ), 0; n   memset(g, 0x3f, sizeof g), memset(f, 0x3f, sizeof f); n   for (int j = (1); j <= (n); j++) n     for (int i = head[dfn[j]], v; v = e[i].v, i; i = e[i].nxt) n       g[dfn[j]] = min(g[dfn[j]], min(v, g[v])); n   for (int j = (n); j >= (1); j--) n     for (int i = head[dfn[j]], v; v = e[i].v, i; i = e[i].nxt) n       f[v] = min(f[v], min(dfn[j], f[dfn[j]])); n   for (int i = (1); i <= (n); i++) n     if (min(g[i], f[i]) > i) ans++, op[i] = 1; n   cout << ans << endl; n   for (int i = (1); i <= (n); i++) putchar(op[i] ?  A  :  E ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 122; n string s[N]; n bool eq[N]; n vector<int> g[30]; n int used[N]; n string ans; n void dfs(int x) { n   used[x] = 1; n   for (int to : g[x]) { n     if (!used[to]) { n       dfs(to); n     } else if (used[to] == 1) { n       puts( Impossible ); n       exit(0); n     } n   } n   used[x] = 2; n   ans += x +  a ; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n   } n   fill(eq, eq + N, 1); n   for (int j = 0; j < N; ++j) { n     for (int i = 1; i < n; ++i) { n       if (s[i].size() > j && eq[i]) { n         if (s[i - 1].size() > j) { n           if (s[i - 1][j] != s[i][j]) { n             g[s[i - 1][j] -  a ].push_back(s[i][j] -  a ); n             eq[i] = 0; n           } else { n             eq[i] = 1; n           } n         } else { n           eq[i] = 0; n         } n       } else if (eq[i]) { n         puts( Impossible ); n         return 0; n       } else { n         eq[i] = 0; n       } n     } n   } n   for (int i = 0; i < 26; ++i) { n     if (!used[i]) { n       dfs(i); n     } n   } n   reverse(ans.begin(), ans.end()); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p1, p2, p3, p4, a, b; n int fun(int x) { return x % p1 % p2 % p3 % p4; } n int main() { n   while (~scanf( %d%d%d%d%d%d , &p1, &p2, &p3, &p4, &a, &b)) { n     int cnt = 0; n     for (int i = a; i <= b; i++) { n       if (fun(i) == i) cnt++; n     } n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void main2() { n   long long int n, m, sum = 0, t = INT_MAX, temp, r = 0; n   cin >> n >> m; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       cin >> temp; n       if (temp < 0) r++; n       sum += abs(temp); n       t = min(t, abs(temp)); n     } n   } n   if (r % 2 == 0) n     cout << sum <<   n ; n   else n     cout << sum - 2 * t <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     main2(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<long long int> lucky; n long long int l, r; n void gen(long long int a) { n   if (a <= 1e11) lucky.push_back(a); n   if (a * 10 + 4 <= 1e11) gen(a * 10 + 4); n   if (a * 10 + 7 <= 1e11) gen(a * 10 + 7); n } n void solve() { n   cin >> l >> r; n   gen(4); n   gen(7); n   sort(lucky.begin(), lucky.end()); n   if (l == r) { n     cout << *lower_bound(lucky.begin(), lucky.end(), l); n     return; n   } n   long long int lb = lower_bound(lucky.begin(), lucky.end(), l) - lucky.begin(); n   long long int ans = (min(r, lucky[lb]) - l + 1) * lucky[lb], i; n   for (i = lb + 1; lucky[i] <= r; i++) { n     ans += (lucky[i] - lucky[i - 1]) * lucky[i]; n   } n   i--; n   if (lucky[i] < r) { n     ans += (r - lucky[i]) * lucky[i + 1]; n   } n   cout << ans; n } n int32_t main() { n   ios::sync_with_stdio(false); n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, S, M, K; n int A[2020], B[2020]; n int L[2020], R[2020]; n vector<int> V; n int MR[2020]; n int dp[1600][1600]; n int ok(int v) { n   int i, j; n   for (i = 0; i < (N); i++) B[i + 1] = B[i] + (A[i] <= v); n   memset(dp, 0, sizeof(dp)); n   for (i = 0; i < (N); i++) { n     for (j = 0; j < (M); j++) { n       if (MR[i] > i) { n         dp[MR[i]][j + 1] = max(dp[MR[i]][j + 1], dp[i][j] + B[MR[i]] - B[i]); n         if (dp[MR[i]][j + 1] >= K) return 1; n       } n       dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n     } n   } n   return 0; n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N >> S >> M >> K; n   for (i = 0; i < (N); i++) { n     cin >> A[i]; n     V.push_back(A[i]); n   } n   for (i = 0; i < (S); i++) { n     cin >> L[i] >> R[i]; n     for (j = L[i] - 1; j < R[i]; j++) MR[j] = max(MR[j], R[i]); n   } n   sort((V.begin()), (V.end())); n   V.erase(unique((V.begin()), (V.end())), V.end()); n   if (ok(V.back()) == 0) return (void)printf( -1 n ); n   int ret = V.size() - 1; n   for (i = 12; i >= 0; i--) { n     if (ret >= (1 << i) && ok(V[ret - (1 << i)])) ret -= 1 << i; n   } n   cout << V[ret] << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x7fffffff; n const int maxn = 2e5 + 5; n int bit[2][maxn], num[maxn]; n int n, k, a, b, q; n void update(int id, int x, int v) { n   while (x <= n) { n     bit[id][x] += v; n     x += (x & -x); n   } n } n int sum(int id, int x) { n   int ans = 0; n   while (x) { n     ans += bit[id][x]; n     x -= x & -x; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k >> a >> b >> q; n   int x, y, z; n   while (q--) { n     cin >> x; n     if (x == 1) { n       cin >> y >> z; n       if (num[y] + z < b) { n         update(0, y, z); n         update(1, y, z); n       } else if (num[y] + z < a) { n         if (num[y] < b) update(0, y, b - num[y]); n         update(1, y, z); n       } else { n         if (num[y] < b) update(0, y, b - num[y]); n         if (num[y] < a) update(1, y, a - num[y]); n       } n       num[y] += z; n     } else { n       cin >> y; n       int ans = sum(0, y - 1) + sum(1, n) - sum(1, y + k - 1); n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], c[100005]; n int main() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     c[i] = c[i - 1] + 20; n     long long x = upper_bound(a + 1, a + 1 + n, a[i] - 90) - a; n     c[i] = min(c[i], c[x - 1] + 50); n     x = upper_bound(a + 1, a + 1 + n, a[i] - 1440) - a; n     c[i] = min(c[i], c[x - 1] + 120); n   } n   for (int i = 1; i <= n; i++) { n     cout << c[i] - c[i - 1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7; n int n; n char s[N][10]; n int val[N]; n int l[N], r[N]; n set<int> st[N]; n void go(int u, int v) { n   if (st[v].size() > st[u].size()) { n     st[u].swap(st[v]); n   } n   for (int x : st[v]) st[u].insert(x); n   set<int>().swap(st[v]); n } n void dfs(int u) { n   if (*s[u] ==  I ) { n     st[u].insert(u); n     return; n   } n   if (*s[u] ==  N ) { n     int v = l[u]; n     dfs(v); n     val[u] = !val[v]; n     go(u, v); n   } else if (*s[u] ==  A ) { n     int v1 = l[u], v2 = r[u]; n     dfs(v1); n     dfs(v2); n     val[u] = val[v1] & val[v2]; n     if (val[u] != ((val[v1] ^ 1) & val[v2])) { n       go(u, v1); n     } n     if (val[u] != (val[v1] & (val[v2] ^ 1))) { n       go(u, v2); n     } n   } else if (*s[u] ==  O ) { n     int v1 = l[u], v2 = r[u]; n     dfs(v1); n     dfs(v2); n     val[u] = val[v1] | val[v2]; n     if (val[u] != ((val[v1] ^ 1) | val[v2])) { n       go(u, v1); n     } n     if (val[u] != (val[v1] | (val[v2] ^ 1))) { n       go(u, v2); n     } n   } else { n     int v1 = l[u], v2 = r[u]; n     dfs(v1); n     dfs(v2); n     val[u] = val[v1] ^ val[v2]; n     if (val[u] != ((val[v1] ^ 1) ^ val[v2])) { n       go(u, v1); n     } n     if (val[u] != (val[v1] ^ (val[v2] ^ 1))) { n       go(u, v2); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , s[i]); n     if (*s[i] ==  I ) { n       scanf( %d , val + i); n     } else if (*s[i] ==  N ) { n       scanf( %d , l + i); n     } else { n       scanf( %d%d , l + i, r + i); n     } n   } n   dfs(1); n   for (int i = 1; i <= n; ++i) { n     if (*s[i] ==  I ) { n       printf( %d , val[1] ^ (st[1].count(i))); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool smin(T &a, const T &b) { n   return b < a ? a = b, 1 : 0; n } n template <typename T> n inline bool smax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const long long N = 3e5 + 10; n long long n, bad[N], m; n vector<long long> adj[N]; n bool pos[N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   while (m--) { n     long long u, v; n     cin >> u >> v; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   set<pair<long long, long long> > s; n   for (long long i = 1; i <= n; i++) n     bad[i] = (long long)adj[i].size(), s.insert({bad[i], i}); n   while (1) { n     pair<long long, long long> pi = *s.rbegin(); n     s.erase(pi); n     long long v = pi.second; n     if (bad[v] < 2) break; n     bad[v] = (long long)adj[v].size() - bad[v]; n     for (long long u : adj[v]) { n       s.erase({bad[u], u}); n       if (pos[u] == pos[v]) { n         bad[u]--; n       } else { n         bad[u]++; n       } n       s.insert({bad[u], u}); n     } n     pos[v] ^= 1; n     s.insert({bad[v], v}); n   } n   for (long long i = 1; i <= n; i++) cout << pos[i]; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n int arr[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   int pos = n; n   cout << 1 << endl; n   for (int i = 1; i <= n; i++) { n     int tmp; n     scanf( %d , &tmp); n     arr[tmp] = 1; n     while (arr[pos]) pos--; n     printf(  %d , i - (n - pos) + 1); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long pw(long long a, long long b, long long p) { n   if (b == 0) return 1; n   long long t = pw(a, b / 2, p); n   if (b % 2) n     return ((a * t * t) % p); n   else n     return ((t * t) % p); n } n map<long long, pair<long long, long long> > m; n long long b[100005]; n void solve() { n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   for (long long j = 1; j <= n; j++) { n     long long i = a[j]; n     b[j] = i; n     b[j] += b[j - 1]; n     m[i].first++; n     if (m[i].first == 1) m[i].second = j; n   } n   long long mx = 0, mn = -1; n   for (auto z : m) { n     long long lmx = 0; n     lmx += z.second.first; n     long long u = z.second.second, l = 0, f = u - 1; n     while (u >= l) { n       long long mid = (l + u) / 2; n       long long ss = b[z.second.second - 1] - b[mid]; n       ss = z.first * (z.second.second - mid - 1) - ss; n       if (k < ss) { n         l = mid + 1; n       } n       if (k >= ss) { n         f = mid; n         u = mid - 1; n       } n     } n     lmx += z.second.second - f - 1; n     if (lmx > mx) { n       mx = lmx; n       mn = z.first; n     } n   } n   cout << mx <<     << mn; n } n signed main() { n   ios_base::sync_with_stdio(false); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, k; n char a[5][500]; n bool vis[5][500], check; n bool valid(int x, int y) { n   if (x < 0 || x > 2 || vis[x][y]) return 0; n   return 1; n } n void dfs(int x, int y, int tm) { n   if (!valid(x, y) || check) return; n   if (a[x][y + 2 * tm] !=  . ) return; n   vis[x][y] = 1; n   if (y >= n - 1) { n     check = 1; n     return; n   } n   if (a[x][y + 2 * tm + 1] !=  . ) return; n   dfs(x, y + 1, tm + 1); n   if (a[x + 1][y + 1 + 2 * tm] ==  . ) dfs(x + 1, y + 1, tm + 1); n   if (a[x - 1][y + 1 + 2 * tm] ==  . ) dfs(x - 1, y + 1, tm + 1); n } n int main() { n   cin >> t; n   while (t--) { n     check = 0; n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < 4; i++) n       for (int j = 0; j < 500; j++) a[i][j] =  . ; n     cin >> n >> k; n     for (int i = 0; i < 3; i++) n       for (int j = 0; j < n; j++) cin >> a[i][j]; n     int p; n     for (int i = 0; i < 3; i++) n       if (a[i][0] ==  s ) p = i; n     a[p][0] =  . ; n     dfs(p, 0, 0); n     if (check) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma hdrstop n using namespace std; n void rot(int n, int *x, int *y, int rx, int ry); n int xy2d(int n, int x, int y) { n   int rx, ry, s, d = 0; n   for (s = n / 2; s > 0; s /= 2) { n     rx = (x & s) > 0; n     ry = (y & s) > 0; n     d += s * s * ((3 * rx) ^ ry); n     rot(s, &x, &y, rx, ry); n   } n   return d; n } n void d2xy(int n, int d, int *x, int *y) { n   int rx, ry, s, t = d; n   *x = *y = 0; n   for (s = 1; s < n; s *= 2) { n     rx = 1 & (t / 2); n     ry = 1 & (t ^ rx); n     rot(s, x, y, rx, ry); n     *x += s * rx; n     *y += s * ry; n     t /= 4; n   } n } n void rot(int n, int *x, int *y, int rx, int ry) { n   int t; n   if (ry == 0) { n     if (rx == 1) { n       *x = n - 1 - *x; n       *y = n - 1 - *y; n     } n     t = *x; n     *x = *y; n     *y = t; n   } n } n #pragma argsused n int main(int argc, char *argv[]) { n   int a, b; n   cin >> a >> b; n   int x, y; n   d2xy(1 << a, b, &x, &y); n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Read(int &p) { n   p = 0; n   char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; c = getchar()) p = p * 10 + c -  0 ; n } n int n, k; n struct Node { n   long long siz; n   int mxx[12], mnn[12]; n   bool operator<(const Node &b) const { n     for (int i = 1; i <= k; i++) n       if (mxx[i] > b.mnn[i]) return 0; n     return 1; n   } n } S; n set<Node> M; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     S.siz = 1; n     for (int j = 1; j <= k; j++) { n       int x; n       Read(x); n       S.mnn[j] = S.mxx[j] = x; n     } n     set<Node>::iterator it = M.find(S); n     while (it != M.end()) { n       S.siz += it->siz; n       for (int j = 1; j <= k; j++) { n         S.mxx[j] = max(S.mxx[j], it->mxx[j]); n         S.mnn[j] = min(S.mnn[j], it->mnn[j]); n       } n       M.erase(it); n       it = M.find(S); n     } n     M.insert(S); n     printf( %lld n , M.rbegin()->siz); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, ind, mini(1000000000); n int t[11]; n string ans =   ; n char chr(int x) { return char( 0  + x); } n int main() { n   cin >> n; n   for (int i = 1; i < 10; i++) { n     cin >> t[i]; n     if (t[i] <= mini) { n       mini = t[i]; n       ind = i; n     } n   } n   if (mini > n) { n     cout << -1; n     return 0; n   } n   while (n - mini >= 0) { n     ans += chr(ind); n     n -= mini; n   } n   for (int i = 0; i < (int)ans.size(); i++) { n     for (int j = 9; j > ind; j--) { n       if (n - t[j] + mini >= 0) { n         ans[i] = chr(j); n         n -= t[j] - mini; n         break; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e18; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int nax = 500000 + 10; n int arr[nax], color[nax]; n vector<int> graph[nax]; n map<int, set<int> > map1; n int main() { n   ios::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> color[i]; n   while (m--) { n     int u, v; n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     for (auto x : graph[i]) { n       if (color[i] != color[x]) map1[color[i]].insert(color[x]); n     } n   } n   int maxi = map1[color[1]].size(), ans = color[1]; n   for (int i = 2; i <= n; i++) { n     if (map1[color[i]].size() == maxi && color[i] < ans) { n       ans = color[i]; n     } else if (map1[color[i]].size() > maxi) { n       maxi = map1[color[i]].size(); n       ans = color[i]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200001; n const long long int MOD = 1000000007; n long long int N = 1, c[M], i, j, N2, m; n bool flag; n int main() { n   cin >> m; n   fill(c, c + M, 1); n   while (m--) { n     int p; n     cin >> p; n     c[p]++; n     N *= p, N %= MOD; n   } n   for (i = 1; i < M; i++) n     if (c[i] % 2 == 0 && !flag) { n       c[i] /= 2, flag = true; n       break; n     } n   if (!flag) { n     N = 1; n     for (i = 1; i < M; i++) n       for (j = 1; j * 2 < c[i]; j++) N *= i, N %= MOD; n   } n   for (i = 1; i < M; i++) { n     N2 = N; n     for (j = 1; j < c[i]; j++) N *= N2, N %= MOD; n   } n   cout << N << endl; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, int> mp; n int x[2005], y[2005]; n int main() { n   int n, ans = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , x + i, y + i); n     for (int j = 0; j < i; j++) ans += mp[{x[i] + x[j], y[i] + y[j]}]++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, T, fa[500005], top, st[500005 << 2], d[500005], a[500005]; n struct E { n   int x, y, l, r; n } e[500005]; n int Getfa(int x) { n   while (fa[x] != x) x = fa[x]; n   return x; n } n int col(int x) { n   int now = 0; n   while (fa[x] != x) now ^= a[x], x = fa[x]; n   return now; n } n void link(int x, int y, int D) { n   if (d[x] > d[y]) swap(x, y); n   if (d[x] == d[y]) d[y]++, st[++top] = -y; n   fa[x] = y; n   a[x] = D; n   st[++top] = x; n } n void RE(int t) { n   for (; top > t; top--) n     if (st[top] < 0) n       d[-st[top]]--; n     else n       fa[st[top]] = st[top], a[st[top]] = 0; n } n void work(int l, int r, int k) { n   int mid = (l + r) >> 1, now = top; n   for (int i = 1; i <= k; i++) n     if (e[i].l <= l && r <= e[i].r) { n       int u = Getfa(e[i].x), v = Getfa(e[i].y); n       if (u != v) n         link(u, v, col(e[i].x) == col(e[i].y)); n       else if (col(e[i].x) == col(e[i].y)) { n         for (int i = l; i <= r; i++) puts( NO ); n         RE(now); n         return; n       } n       swap(e[k--], e[i--]); n     } n   if (l == r) n     puts( YES ); n   else { n     int i, j; n     for (i = 1, j = 0; i <= k; i++) n       if (e[i].l <= mid) swap(e[i], e[++j]); n     work(l, mid, j); n     for (i = 1, j = 0; i <= k; i++) n       if (e[i].r > mid) swap(e[i], e[++j]); n     work(mid + 1, r, j); n   } n   RE(now); n } n map<int, int> M[500005]; n int main() { n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= T; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x > y) swap(x, y); n     if (M[x][y]) n       e[M[x][y]].r = i - 1, M[x][y] = 0; n     else { n       M[x][y] = ++m; n       e[m].x = x; n       e[m].y = y; n       e[m].l = i - 1; n       e[m].r = T; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (++e[i].l > e[i].r) { n       i--, m--; n       assert(false); n     } n   } n   for (int i = 1; i <= n; i++) fa[i] = i, d[i] = 1; n   work(1, T, m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char s[100010]; n int main() { n   cin >> n >> m; n   scanf( %s , s + 1); n   while (m--) { n     int l, r; n     cin >> l >> r; n     char a[2], b[2]; n     scanf( %s%s , a, b); n     for (int i = l; i <= r; i++) n       if (s[i] == a[0]) s[i] = b[0]; n   } n   printf( %s , s + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n bool vis[N][N]; n int n, ai, aj; n bool can(int i, int j) { n   if (ai == i || aj == j || i + j == ai + aj || i - j == ai - aj) return 0; n   return (i <= n && i >= 1 && j <= n && j >= 1); n } n int bi, bj, ci, cj; n int di[] = {0, 0, 1, 1, 1, -1, -1, -1}; n int dj[] = {1, -1, 0, 1, -1, 0, 1, -1}; n void dfs(int i, int j) { n   if (!can(i, j) || vis[i][j]) return; n   vis[i][j] = 1; n   for (int z = 0; z < 8; ++z) dfs(i + di[z], j + dj[z]); n } n int main() { n   cin >> n >> ai >> aj >> bi >> bj >> ci >> cj; n   dfs(bi, bj); n   if (vis[ci][cj]) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long int> v; n   long long int n, num, max_ind = 0, max_num = -1; n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     cin >> num; n     v.push_back(num); n     if (num > max_num) { n       max_num = num; n       max_ind = i; n     } n   } n   if (max_num != 1) n     v[max_ind] = 1; n   else n     v[max_ind] = 2; n   sort(v.begin(), v.end()); n   for (long long int i = 0; i < n; i++) { n     cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class S> n ostream& operator<<(ostream& os, const pair<T, S>& v) { n   return os <<  (  << v.first <<  ,   << v.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  [ ; n   for (int i = int(0); i <= int((static_cast<int>((v).size())) - 1); ++i) { n     if (i) os <<  ,  ; n     os << v[i]; n   } n   return os <<  ] ; n } n template <class T> n bool setmax(T& _a, T _b) { n   if (_a < _b) { n     _a = _b; n     return true; n   } n   return false; n } n template <class T> n bool setmin(T& _a, T _b) { n   if (_b < _a) { n     _a = _b; n     return true; n   } n   return false; n } n template <class T> n T gcd(T _a, T _b) { n   return _b == 0 ? _a : gcd(_b, _a % _b); n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   iota((a).begin(), (a).end(), 0); n   int cur = 0; n   while (k--) { n     int x; n     cin >> x; n     int next = (cur + x) % static_cast<int>((a).size()); n     cout << a[next] + 1 <<    ; n     a.erase(a.begin() + next); n     cur = next % static_cast<int>((a).size()); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:536870912 ) n #pragma warning(disable : 4996) n using namespace std; n const int N = 305; n int dp[N * 2][N][N], ar[N][N]; n int n; n pair<int, int> getC(int t, int i) { n   return t <= n - 1 ? make_pair(t - i, i) : make_pair(n - 1 - i, t - n + 1 + i); n } n int getS(int t) { return t <= n - 1 ? t + 1 : (2 * n - 1 - t); } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) scanf( %d , &ar[i][j]); n   } n   memset(dp, 128, sizeof(dp)); n   ; n   dp[0][0][0] = ar[0][0]; n   for (int i = 0; i < 2 * n - 2; i++) { n     int t = getS(i + 1), c = getS(i); n     for (int a = 0; a < c; a++) { n       for (int b = 0; b < c; b++) { n         for (int p = (i < n - 1 ? 0 : -1); p < (i < n - 1 ? 2 : 1); p++) { n           for (int q = (i < n - 1 ? 0 : -1); q < (i < n - 1 ? 2 : 1); q++) { n             auto g = getC(i + 1, a + p), h = getC(i + 1, b + q); n             if (g.first >= n || g.second >= n || h.first >= n || n                 h.second >= n || g.first < 0 || g.second < 0 || h.first < 0 || n                 h.second < 0) n               continue; n             int sum = dp[i][a][b] + ar[g.first][g.second]; n             if (g != h) sum += ar[h.first][h.second]; n             dp[i + 1][a + p][b + q] = max(dp[i + 1][a + p][b + q], sum); n           } n         } n       } n     } n   } n   printf( %d , dp[2 * n - 2][0][0]); n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   int* children; n   node() { n     children = new int[2]; n     children[0] = children[1] = -1; n   } n   int& operator[](int i) { return children[i]; } n }; n struct trie { n   std::vector<node> nodes; n   int root; n   trie() { n     nodes.push_back(node()); n     root = 0; n   } n   void insert(long long x) { n     int u = this->root; n     for (int i = 41; i >= 0; --i) { n       int v = ((x & (1LL << i)) ? 1 : 0); n       int next = nodes[u][v]; n       if (next == -1) { n         next = nodes[u][v] = nodes.size(); n         nodes.push_back(node()); n       } n       u = next; n     } n   } n   long long maximize(long long x) { n     int u = this->root; n     long long ans = 0; n     for (int i = 41; i >= 0; --i) { n       int cnt = 0, v; n       if (nodes[u][0] != -1) { n         cnt++; n         v = 0; n       } n       if (nodes[u][1] != -1) { n         cnt++; n         v = 1; n       } n       if (cnt == 0) n         return ans; n       else if (cnt == 1) { n         if (v) ans ^= (1LL << i); n         u = nodes[u][v]; n       } else { n         v = (x & (1LL << i) ? 1 : 0) ^ 1; n         if (v) ans ^= (1LL << i); n         u = nodes[u][v]; n       } n     } n     return ans; n   } n }; n int N; n long long partial_xor[100001]; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   std::cout.tie(NULL); n   std::cin >> N; n   for (int i = 0; i < N; ++i) { n     std::cin >> partial_xor[i]; n     if (i) partial_xor[i] ^= partial_xor[i - 1]; n   } n   long long xor_sum = partial_xor[N - 1]; n   long long ans = xor_sum; n   trie t; n   for (int i = 0; i < N; ++i) { n     ans = std::max(ans, partial_xor[i]); n     if (i) { n       ans = std::max(ans, partial_xor[i - 1] ^ xor_sum); n       ans = std::max(ans, t.maximize(partial_xor[i - 1]) ^ partial_xor[i - 1]); n     } n     t.insert(partial_xor[i] ^ xor_sum); n   } n   std::cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> maxH, maxV; n set<pair<int, int> > vH, vV; n int main() { n   int w, h, n, x; n   std::set<pair<int, int> >::iterator it, itL, itR; n   char ch; n   cin >> w >> h >> n; n   vH.insert(make_pair(0, h)); n   vH.insert(make_pair(h, 0)); n   vV.insert(make_pair(0, w)); n   vV.insert(make_pair(w, 0)); n   maxH.insert(h); n   maxV.insert(w); n   for (int i = 0; i < n; i++) { n     cin >> ch >> x; n     if (ch ==  H ) { n       vH.insert(make_pair(x, 0)); n       it = vH.find(make_pair(x, 0)); n       pair<int, int> lp, p, rp; n       it--; n       lp = *it; n       it++; n       p = *it; n       it++; n       rp = *it; n       vH.insert(make_pair((int)lp.first, x - lp.first)); n       vH.erase(vH.find(lp)); n       vH.insert(make_pair(x, rp.first - x)); n       vH.erase(vH.find(p)); n       lp = make_pair(lp.first, x - lp.first); n       p = make_pair(x, rp.first - x); n       maxH.erase(maxH.find(rp.first - lp.first)); n       maxH.insert(lp.second); n       maxH.insert(p.second); n       cout << 1ll * (*maxV.rbegin()) * (*maxH.rbegin()) << endl; n     } else { n       vV.insert(make_pair(x, 0)); n       it = vV.find(make_pair(x, 0)); n       pair<int, int> lp, p, rp; n       it--; n       lp = (*it); n       it++; n       p = (*it); n       it++; n       rp = (*it); n       vV.insert(make_pair(lp.first, x - lp.first)); n       vV.erase(vV.find(lp)); n       vV.insert(make_pair(x, rp.first - x)); n       vV.erase(vV.find(p)); n       lp = make_pair(lp.first, x - lp.first); n       p = make_pair(x, rp.first - x); n       maxV.erase(maxV.find(rp.first - lp.first)); n       maxV.insert(lp.second); n       maxV.insert(p.second); n       cout << 1ll * (*maxH.rbegin()) * (*maxV.rbegin()) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, t; n   cin >> n >> k >> t; n   if (t <= k) n     cout << t << endl; n   else if (t > k && t <= n) n     cout << k << endl; n   else n     cout << k - (t - n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n const int MAXN = 900; n const int MAXM = 1900000; n int N, edges; n int last[MAXN], preve[MAXM], head[MAXM]; n int Cap[MAXM], Flow[MAXM]; n int dist[MAXN]; n int nextEdge[MAXN]; n queue<int> Q; n void init(int N) { n   edges = 0; n   memset(last, -1, sizeof last); n } n set<int> namex, namey; n vector<int> xx, yy; n void handlex(int f) { n   if (namex.find(f) != namex.end()) return; n   namex.insert(f); n   xx.push_back(f); n } n void handley(int f) { n   if (namey.find(f) != namey.end()) return; n   yy.push_back(f); n   namey.insert(f); n } n inline void add_edge(int u, int v, int cap, int flow = 0) { n   head[edges] = v; n   preve[edges] = last[u]; n   Cap[edges] = cap; n   Flow[edges] = flow; n   last[u] = edges++; n   head[edges] = u; n   preve[edges] = last[v]; n   Cap[edges] = 0; n   Flow[edges] = 0; n   last[v] = edges++; n } n inline bool dinicBfs(int S, int E, int N) { n   int from = S, to, cap, flow; n   memset(dist, 0, sizeof(int) * N); n   dist[from] = 1; n   while (!Q.empty()) Q.pop(); n   Q.push(from); n   while (!Q.empty()) { n     from = Q.front(); n     Q.pop(); n     for (int e = last[from]; e >= 0; e = preve[e]) { n       to = head[e]; n       cap = Cap[e]; n       flow = Flow[e]; n       if (!dist[to] && cap > flow) { n         dist[to] = dist[from] + 1; n         Q.push(to); n         if (to == E) return true; n       } n     } n   } n   return (dist[E] != 0); n } n inline int dfs(int from, int minEdge, int E) { n   if (!minEdge) return 0; n   if (from == E) return minEdge; n   int to, e, cap, flow, ret; n   for (; nextEdge[from] >= 0; nextEdge[from] = preve[e]) { n     e = nextEdge[from]; n     to = head[e]; n     cap = Cap[e]; n     flow = Flow[e]; n     if (dist[to] != dist[from] + 1) continue; n     ret = dfs(to, min(minEdge, cap - flow), E); n     if (ret) { n       Flow[e] += ret; n       Flow[e ^ 1] -= ret; n       return ret; n     } n   } n   return 0; n } n int dinicUpdate(int S, int E) { n   int flow = 0; n   while (int minEdge = dfs(S, INF, E)) { n     if (minEdge == 0) break; n     flow += minEdge; n   } n   return flow; n } n int maxFlow(int S, int E, int N) { n   int totFlow = 0; n   while (dinicBfs(S, E, N)) { n     for (int i = 0; i <= N; i++) nextEdge[i] = last[i]; n     totFlow += dinicUpdate(S, E); n   } n   return totFlow; n } n int x1[60], y11[60], x2[60], y2[60]; n int main() { n   int i, j, k, l, m, n; n   init(32); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d%d , &x1[i], &y11[i], &x2[i], &y2[i]); n     x2[i]++; n     y2[i]++; n     handlex(x1[i]); n     handlex(x2[i]); n     handley(y11[i]); n     handley(y2[i]); n   } n   sort(xx.begin(), xx.end()); n   sort(yy.begin(), yy.end()); n   int so = xx.size() + yy.size() + 1; n   int sink = so + 1; n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j < xx.size(); j++) { n       if (xx[j] <= x1[i]) continue; n       if (xx[j] > x2[i]) break; n       for (int k = 1; k < yy.size(); k++) { n         if (yy[k] <= y11[i]) continue; n         if (yy[k] > y2[i]) break; n         int g = xx.size() + k; n         add_edge(j, g, INF); n       } n     } n   } n   for (int i = 1; i < xx.size(); i++) { n     add_edge(so, i, xx[i] - xx[i - 1]); n   } n   for (int i = 1; i < yy.size(); i++) { n     int g = xx.size() + i; n     add_edge(g, sink, yy[i] - yy[i - 1]); n   } n   int ans = maxFlow(so, sink, sink + 2); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e18; n const long long mod = (long long)1e9 + 7; n const double eps = (double)1e-9; n const double pi = acos(-1.0); n const int dx[] = {0, 0, 1, 0, -1}; n const int dy[] = {0, 1, 0, -1, 0}; n const int N = 100500; n int n, x, y, a, b, T; n long long k; n multiset<long long> st, st1; n long long check(int j) { n   st1 = st; n   long long ans = 0; n   for (int i = 1; i <= j; ++i) n     if (i % a == 0 && i % b == 0) { n       ans += *st1.begin() * (x + y); n       st1.erase(st1.begin()); n     } n   for (int i = 1; i <= j; ++i) n     if (i % a == 0 && i % b != 0) { n       ans += *st1.begin() * x; n       st1.erase(st1.begin()); n     } n   for (int i = 1; i <= j; ++i) n     if (i % b == 0 && i % a != 0) { n       ans += *st1.begin() * y; n       st1.erase(st1.begin()); n     } n   return -ans; n } n void solve() { n   cin >> n; n   st.clear(); n   for (int i = 1; i <= n; ++i) { n     long long x; n     cin >> x; n     st.insert(-x / 100); n   } n   cerr << st.size() << endl; n   cin >> x >> a >> y >> b >> k; n   if (x < y) swap(x, y), swap(a, b); n   int l = 1, r = n; n   while (l <= r) { n     int md = (l + r) >> 1; n     if (check(md) >= k) n       r = md - 1; n     else n       l = md + 1; n   } n   if (r == n) r = -2; n   cout << r + 1 << endl; n } n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> add_pair(pair<long long, long long> a, n                                     pair<long long, long long> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n long long power(long long a, long long b) { n   long long res = 1; n   a = a % ((long long)1e9 + 7); n   while (b > 0) { n     if (b & 1) { n       res = (res * a) % ((long long)1e9 + 7); n       b--; n     } n     a = (a * a) % ((long long)1e9 + 7); n     b >>= 1; n   } n   return res; n } n long long fermat_inv(long long y) { return power(y, ((long long)1e9 + 7) - 2); } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long mul(long long a, long long b) { n   return (a * (b % ((long long)1e9 + 7))) % ((long long)1e9 + 7); n } n long long sub(long long a, long long b) { n   return ((a - b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) % n          ((long long)1e9 + 7); n } n long long add(long long a, long long b) { n   return ((a + b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) % n          ((long long)1e9 + 7); n } n vector<vector<long long> > G; n map<long long, long long> m[100005]; n vector<long long> b; n long long ans; n void dfs(long long rt, long long par) { n   m[rt][b[rt]]++; n   if (rt != par) { n     for (auto it = (m[par]).begin(); it != (m[par]).end(); it++) { n       m[rt][gcd(b[rt], it->first)] += it->second; n     } n   } n   for (auto it = (m[rt]).begin(); it != (m[rt]).end(); it++) { n     ans = add(ans, mul(it->first, it->second)); n   } n   for (auto it = (G[rt]).begin(); it != (G[rt]).end(); it++) { n     if (*it != par) { n       dfs(*it, rt); n     } n   } n } n void solve() { n   long long n, u, v, i; n   cin >> n; n   G = vector<vector<long long> >(n + 1, vector<long long>()); n   b = vector<long long>(n + 1); n   for (i = (0); i < (n); i += (1)) cin >> b[i + 1]; n   for (i = (0); i < (n - 1); i += (1)) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   ans = 0; n   dfs(1, 1); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int add(int a, int b) { n   long long x = a + b; n   if (x >= 1000000009) x -= 1000000009; n   if (x < 0) x += 1000000009; n   return x; n } n long long mul(long long a, long long b) { return (a * b) % 1000000009; } n long long pw(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % 1000000009; n     a = (a * a) % 1000000009; n     b >>= 1; n   } n   return ans; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long rand_seed() { n   long long a = rng(); n   return a; n } n int n; n map<int, pair<int, int> > pos; n map<pair<int, int>, int> id; n vector<set<int> > p, ch; n bool canrem(int i) { n   int ok = 1; n   for (auto j : p[i]) ok &= (ch[j].size() > 1); n   return ok; n } n void rem(int i) { n   for (auto j : p[i]) ch[j].erase(i); n   for (auto j : ch[i]) p[j].erase(i); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   p.resize(n); n   ch.resize(n); n   for (int x, y, i = 0; i < n; i++) { n     cin >> y >> x; n     pos[i] = {x, y}; n     id[{x, y}] = i; n   } n   for (int i = 0; i < n; i++) { n     int x = pos[i].first, y = pos[i].second; n     for (int z = -1; z < 2; z++) { n       if (id.count({x + 1, y + z})) p[i].insert(id[{x + 1, y + z}]); n       if (id.count({x - 1, y + z})) ch[i].insert(id[{x - 1, y + z}]); n     } n   } n   set<int> can; n   for (int i = 0; i < n; i++) { n     int x = pos[i].first, y = pos[i].second; n     if (canrem(i)) can.insert(i); n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int j; n     if (i % 2 == 0) n       j = *can.rbegin(); n     else n       j = *can.begin(); n     ans = mul(ans, n); n     ans = add(ans, j); n     int x = pos[j].first; n     int y = pos[j].second; n     rem(j); n     can.erase(j); n     id.erase(pos[j]); n     for (int z = -2; z < 3; z++) { n       if (id.count({x, y + z})) can.erase(id[{x, y + z}]); n       if (id.count({x - 1, y + z})) can.erase(id[{x - 1, y + z}]); n     } n     for (int z = -2; z < 3; z++) { n       if (id.count({x, y + z}) && canrem(id[{x, y + z}])) n         can.insert(id[{x, y + z}]); n       x--; n       if (id.count({x, y + z}) && canrem(id[{x, y + z}])) n         can.insert(id[{x, y + z}]); n       x++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x; n   cin >> n; n   long long b = n, i(0), c(0); n   for (int i = n + 1;; i++) { n     b = i; n     if (b < 0) b *= -1; n     while (b != 0) { n       x = b % 10; n       if (x == 8) { n         c = 1; n         break; n       } n       b /= 10; n     } n     if (c == 1) { n       cout << i - n << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e10; n long long s[300005]; n int main() { n   int T; n   long long n, k; n   scanf( %d , &T); n   while (T--) { n     scanf( %lld %lld , &n, &k); n     for (long long i = 1; i <= n; i++) scanf( %lld n , &s[i]); n     long long ans = inf, v; n     for (long long i = k + 1; i <= n; i++) { n       if (ans > (s[i] - s[i - k])) ans = s[i] - s[i - k], v = i; n     } n     long long kk = (s[v] + s[v - k]) / 2; n     cout << kk << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long deal(long long x, long long y) { n   int a = x + y; n   return (x + 1) * x * (y + 1) / 2 + y * (y + 1) / 2 * (x + 1); n } n long long max(long long a, long long b) { return a > b ? a : b; } n int main() { n   long long m, b; n   long long ans = -1; n   scanf( %lld%lld , &m, &b); n   for (int i = 0;; i += m) { n     long long ys = b - i / m; n     if (ys < 0) break; n     ans = max(ans, deal(i, ys)); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pil = pair<int, ll>; n using pli = pair<ll, int>; n using pll = pair<ll, ll>; n template <typename T> n bool chmax(T &x, const T &y) { n   return (x < y) ? (x = y, true) : false; n } n template <typename T> n bool chmin(T &x, const T &y) { n   return (x > y) ? (x = y, true) : false; n } n template <typename T> n int flg(T x, int i) { n   return (x >> i) & 1; n } n template <typename T> n void print(const vector<T> &v, T x = 0) { n   int n = v.size(); n   for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ?   n  :    ); n } n template <typename T> n void printn(const vector<T> &v, T x = 0) { n   int n = v.size(); n   for (int i = 0; i < n; i++) cout << v[i] + x <<   n ; n } n template <typename T> n int lb(const vector<T> &v, T x) { n   return lower_bound(begin(v), end(v), x) - begin(v); n } n template <typename T> n int ub(const vector<T> &v, T x) { n   return upper_bound(begin(v), end(v), x) - begin(v); n } n template <typename T> n void rearrange(vector<T> &v) { n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), end(v)); n } n template <typename T> n vector<int> id_sort(const vector<T> &v, bool greater = false) { n   int n = v.size(); n   vector<int> ret(n); n   iota(begin(ret), end(ret), 0); n   sort(begin(ret), end(ret), n        [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; }); n   return ret; n } n struct io_setup { n   io_setup() { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout << fixed << setprecision(15); n   } n } io_setup; n const int inf = (1 << 30) - 1; n const ll INF = (1LL << 60) - 1; n const int MOD = 1000000007; n template <int mod> n struct Mod_Int { n   int x; n   Mod_Int() : x(0) {} n   Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} n   static int get_mod() { return mod; } n   Mod_Int &operator+=(const Mod_Int &p) { n     if ((x += p.x) >= mod) x -= mod; n     return *this; n   } n   Mod_Int &operator-=(const Mod_Int &p) { n     if ((x += mod - p.x) >= mod) x -= mod; n     return *this; n   } n   Mod_Int &operator*=(const Mod_Int &p) { n     x = (int)(1LL * x * p.x % mod); n     return *this; n   } n   Mod_Int &operator/=(const Mod_Int &p) { n     *this *= p.inverse(); n     return *this; n   } n   Mod_Int &operator++() { return *this += Mod_Int(1); } n   Mod_Int operator++(int) { n     Mod_Int tmp = *this; n     ++*this; n     return tmp; n   } n   Mod_Int &operator--() { return *this -= Mod_Int(1); } n   Mod_Int operator--(int) { n     Mod_Int tmp = *this; n     --*this; n     return tmp; n   } n   Mod_Int operator-() const { return Mod_Int(-x); } n   Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; } n   Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; } n   Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; } n   Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; } n   bool operator==(const Mod_Int &p) const { return x == p.x; } n   bool operator!=(const Mod_Int &p) const { return x != p.x; } n   Mod_Int inverse() const { n     assert(*this != Mod_Int(0)); n     return pow(mod - 2); n   } n   Mod_Int pow(long long k) const { n     Mod_Int now = *this, ret = 1; n     for (; k > 0; k >>= 1, now *= now) { n       if (k & 1) ret *= now; n     } n     return ret; n   } n   friend ostream &operator<<(ostream &os, const Mod_Int &p) { n     return os << p.x; n   } n   friend istream &operator>>(istream &is, Mod_Int &p) { n     long long a; n     is >> a; n     p = Mod_Int<mod>(a); n     return is; n   } n }; n using mint = Mod_Int<MOD>; n int main() { n   int N; n   cin >> N; n   vector<int> a(N); n   for (int i = 0; i < N; i++) cin >> a[i]; n   int M = 100000; n   vector<vector<int>> ds(M + 1); n   vector<ll> c(M + 1); n   iota(c.begin(), c.end(), 0); n   for (int i = 1; i <= M; i++) { n     ds[i].emplace_back(i); n     for (int j = 2 * i; j <= M; j += i) { n       c[j] -= c[i]; n       ds[j].emplace_back(i); n     } n   } n   mint ans = 0; n   vector<ll> cnt(M + 1, 0); n   vector<bool> used(M + 1, false); n   for (int i = 1; i <= N; i++) { n     mint tmp = 0; n     for (int j = i - 1; j < N; j += i) { n       for (auto &e : ds[a[j]]) { n         used[e] = true; n         cnt[e]++; n       } n     } n     for (int j = i - 1; j < N; j += i) { n       for (auto &e : ds[a[j]]) { n         if (used[e]) { n           used[e] = false; n           tmp += c[e] * cnt[e] * cnt[e]; n         } n         cnt[e]--; n       } n     } n     ans += tmp * c[i]; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 101; n int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; n const int sz = sizeof(prime) / sizeof(int), V = 58; n int pos[V + 1], dp[N][1 << sz], n, A[N], minPrime[V + 1], trace[N][1 << sz]; n void prepare() { n   for (int i = 2; i * i <= V; ++i) n     if (!minPrime[i]) n       for (int j = i * i; j <= V; j += i) n         if (!minPrime[j]) minPrime[j] = i; n   for (int i = 2; i <= V; ++i) n     if (!minPrime[i]) minPrime[i] = i; n   for (int i = 0; i < sz; ++i) pos[prime[i]] = i; n } n int solve(int idx, int msk) { n   if (idx > n) return 0; n   int &res = dp[idx][msk]; n   if (res != INT_MAX) return res; n   int s, val, d; n   for (int x = 1; x <= 58; ++x) { n     val = x; n     d = -1; n     s = 0; n     while (val > 1) { n       if (minPrime[val] != d) { n         d = minPrime[val]; n         s |= (1 << pos[d]); n       } n       val /= d; n     } n     if (!(s & msk)) { n       val = abs(x - A[idx]) + solve(idx + 1, msk | s); n       if (val < res) { n         res = val; n         trace[idx][msk] = x; n       } n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> A[i]; n   fill(&dp[0][0], &dp[0][0] + sizeof(dp) / sizeof(int), INT_MAX); n   prepare(); n   solve(1, 0); n   int msk = 0, val, d, s; n   for (int i = 1; i <= n; ++i) { n     val = trace[i][msk]; n     cout << val <<    ; n     d = -1; n     s = 0; n     while (val > 1) { n       if (minPrime[val] != d) { n         d = minPrime[val]; n         s |= (1 << pos[d]); n       } n       val /= d; n     } n     msk |= s; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 3; n struct node { n   int nonzero, val; n   void merge(node &n1, node &n2, int range) { n     if (val) n       nonzero = range; n     else n       nonzero = n1.nonzero + n2.nonzero; n   } n   void update(int x) { val += x; } n } tree[7 * MAX]; n void update(int root, int l, int r, int u, int v, int val) { n   if (u <= l and v >= r) { n     tree[root].update(val); n     tree[root].merge(tree[2 * root], tree[2 * root + 1], r - l + 1); n     return; n   } n   int mid = (l + r) / 2; n   if (u <= mid) update(2 * root, l, mid, u, v, val); n   if (v > mid) update(2 * root + 1, mid + 1, r, u, v, val); n   tree[root].merge(tree[2 * root], tree[2 * root + 1], r - l + 1); n } n vector<int> G[MAX], intervals[MAX]; n int st[MAX], en[MAX], Time = 0, ans[MAX], n, m; n void dfs(int x, int par) { n   st[x] = ++Time; n   for (int i = 0; i < G[x].size(); i++) n     if (G[x][i] != par) dfs(G[x][i], x); n   en[x] = Time; n } n void dfs2(int x, int par) { n   if (intervals[x].size()) intervals[x].push_back(x); n   for (int i = 0; i < intervals[x].size(); i++) n     update(1, 1, n, st[intervals[x][i]], en[intervals[x][i]], 1); n   ans[x] = max(0, tree[1].nonzero - 1); n   for (int i = 0; i < G[x].size(); i++) n     if (G[x][i] != par) dfs2(G[x][i], x); n   for (int i = 0; i < intervals[x].size(); i++) n     update(1, 1, n, st[intervals[x][i]], en[intervals[x][i]], -1); n } n int main() { n   scanf( %d , &n); n   ; n   scanf( %d , &m); n   ; n   memset(tree, 0, sizeof tree); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d , &a); n     ; n     scanf( %d , &b); n     ; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   dfs(1, -1); n   while (m--) { n     int a, b; n     scanf( %d , &a); n     ; n     scanf( %d , &b); n     ; n     intervals[a].push_back(b); n     intervals[b].push_back(a); n   } n   dfs2(1, -1); n   for (int i = 1; i < n + 1; i++) printf( %d  , ans[i]); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pri[100003], top, n; n bool ntp[100002]; n struct node { n   int a, b; n   bool operator<(const node &rhs) const { return b > rhs.b; } n }; n void euler() { n   for (int i = 2; i <= 100000; ++i) { n     if (!ntp[i]) pri[top++] = i; n     for (int j = 0; j < top && pri[j] * i <= 100000; ++j) { n       ntp[pri[j] * i] = 1; n       if (i % pri[j] == 0) break; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   euler(); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     int bn = n; n     vector<node> v; n     int sz = 1; n     for (int i = 0; i < top && n != 1; ++i) { n       if (n % pri[i] == 0) { n         node x; n         x.a = pri[i]; n         x.b = 0; n         while (n % pri[i] == 0) ++x.b, n /= pri[i]; n         sz *= (x.b + 1); n         v.push_back(x); n       } n     } n     if (n != 1) { n       node x; n       x.a = n; n       x.b = 1; n       v.push_back(x); n     } n     if (v.size() == 0) { n       cout <<  NO n ; n     } else if (v.size() == 1) { n       if (v[0].b < 6) { n         cout <<  NO n ; n       } else { n         cout <<  YES n ; n         const int &t = v[0].a; n         cout << t <<     << t * t <<     << bn / (t * t * t) <<   n ; n       } n     } else if (v.size() == 2) { n       if (v[0].b + v[1].b < 4) { n         cout <<  NO n ; n       } else { n         cout <<  YES n ; n         cout << v[0].a <<     << v[1].a <<     << bn / v[0].a / v[1].a <<   n ; n       } n     } else { n       cout <<  YES n ; n       cout << v[0].a <<     << v[1].a <<     << bn / v[0].a / v[1].a <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using min_queue = priority_queue<T, vector<T>, greater<T>>; n const long long MOD = 1e9 + 7; n vector<int> g[1010]; n long long edg[1010][1010]; n map<pair<int, int>, long long> res; n int leaf[1010]; n int N; n void dfs(int v, int p) { n   leaf[v] = v; n   for (int w : g[v]) { n     if (w == p) continue; n     dfs(w, v); n     leaf[v] = leaf[w]; n   } n } n bool dfs3(int v, int l2, int p, long long r) { n   if (v == l2) return 1; n   for (int w : g[v]) { n     if (w == p) continue; n     if (dfs3(w, l2, v, r)) { n       edg[v][w] += r; n       edg[w][v] += r; n       return 1; n     } n   } n   return 0; n } n void add(int l1, int l2, long long r) { n   if (r == 0) return; n   if (l1 > l2) swap(l1, l2); n   res[{l1, l2}] += r; n   dfs3(l1, l2, 0, -r); n } n void dfs2(int v, int p, long long r, int l1, int l2) { n   add(leaf[v], l1, r / 2); n   add(leaf[v], l2, r / 2); n   add(l1, l2, -r / 2); n   vector<int> ww; n   for (int w : g[v]) { n     if (w != p) ww.push_back(w); n   } n   for (int w : g[v]) { n     long long r = edg[v][w]; n     if (w == p) continue; n     if (ww.size() > 1) { n       if (ww[0] == w) n         l2 = leaf[ww[1]]; n       else n         l2 = leaf[ww[0]]; n     } n     dfs2(w, v, r, l1, l2); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int a, b; n     cin >> a >> b; n     long long val; n     cin >> val; n     g[a].push_back(b); n     g[b].push_back(a); n     edg[a][b] = edg[b][a] = val; n   } n   bool good = 1; n   int rt = 0; n   for (int i = 1; i <= N; i++) { n     if (g[i].size() != 2) { n       if (rt == 0 || g[i].size() >= 3) rt = i; n       continue; n     } n     if (edg[i][g[i][0]] != edg[i][g[i][1]]) { n       good = 0; n       break; n     } n   } n   if (!good) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   dfs(rt, 0); n   if (g[rt].size() == 1) { n     long long v = edg[rt][g[rt][0]]; n     cout << 1 << endl; n     cout << rt <<     << leaf[rt] <<     << v << endl; n     return 0; n   } n   set<int> ch; n   for (int w : g[rt]) { n     if (ch.size() >= 3) break; n     ch.insert(w); n   } n   for (int w : g[rt]) { n     set<int> ch1 = ch; n     ch1.erase(w); n     int l1 = leaf[*ch1.begin()]; n     int l2 = leaf[*ch1.rbegin()]; n     dfs2(w, rt, edg[rt][w], l1, l2); n   } n   int cnt = 0; n   for (auto pr : res) n     if (pr.second != 0 && pr.first.first != pr.first.second) cnt++; n   cout << cnt <<   n ; n   for (auto pr : res) { n     if (pr.second != 0 && pr.first.first != pr.first.second) n       cout << pr.first.first <<     << pr.first.second <<     << pr.second n            << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const int MAXN = 150010; n const int INF = 2147483600; n int N; n long long A, B, C, D; n inline long long Query(long long t) { n   if (t * D > C || t < 0) return INF; n   return (t * (t + 1) / 2 * D) * B - (t + 1) * A; n } n inline long long Qt(long long l, long long r) { n   long long fans = INF, pos = 0; n   r /= D; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (Query(mid + 1) - Query(mid) < 0) n       l = mid + 1; n     else n       pos = mid, r = mid - 1; n   } n   return min(Query(pos - 1), min(Query(pos), Query(pos + 1))); n } n int main() { n   int T = read(); n   while (T--) { n     A = read(), B = read(), C = read(), D = read(); n     if (A > C * B) { n       puts( -1 ); n       continue; n     } n     if (D >= C) { n       printf( %lld n , A); n     } else { n       long long l = 0, r = C - 1; n       long long ans = Qt(0, C); n       printf( %lld n , max(-ans, 0LL)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int N = 505; n int sign(double x) { return (x > eps) - (x < -eps); } n int n; n struct P { n   double x, y; n   P(double a = 0., double b = 0.) : x(a), y(b) {} n   P operator+(const P& a) const { return P(x + a.x, y + a.y); } n   P operator-(const P& a) const { return P(x - a.x, y - a.y); } n   double operator*(const P& a) const { return x * a.x + y * a.y; } n   double operator^(const P& a) const { return x * a.y - y * a.x; } n   double cross(P a, P b) { return (a - *this) ^ (b - *this); } n   double radio(P p, P b) { n     if (sign(b.x - x)) return (p.x - x) / (b.x - x); n     return (p.y - y) / (b.y - y); n   } n   void in() { scanf( %lf%lf , &x, &y); } n }; n struct PLOY { n   int pnt; n   P ch[6]; n   bool anti() { n     int dr = sign(ch[0].cross(ch[1], ch[2])); n     if (dr == 0) return false; n     if (dr < 0) reverse(ch, ch + pnt); n     ch[pnt] = ch[0]; n     return true; n   } n   double S() { n     double s = 0.; n     for (int i = 0; i < pnt; i++) s += ch[i] ^ ch[i + 1]; n     return fabs(s) * 0.5; n   } n } pl[505]; n pair<double, int> pr[2005]; n double cover[505]; n double get() { n   for (int i = 0; i < N; i++) cover[i] = 0.; n   double s = 0.; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     for (int x = 0; x < pl[i].pnt; x++) { n       cnt = 0; n       pr[cnt++] = make_pair(0., 0); n       pr[cnt++] = make_pair(1., 0); n       for (int j = 0; j < n; j++) { n         for (int y = 0; y < pl[j].pnt; y++) { n           int dr1 = sign(pl[i].ch[x].cross(pl[i].ch[x + 1], pl[j].ch[y])); n           int dr2 = sign(pl[i].ch[x].cross(pl[i].ch[x + 1], pl[j].ch[y + 1])); n           if (dr1 == 0 && dr2 == 0) { n             if (i > j && (pl[i].ch[x + 1] - pl[i].ch[x]) * n                                  (pl[j].ch[y + 1] - pl[j].ch[y]) > n                              0.) { n               pr[cnt++] = n                   make_pair(pl[i].ch[x].radio(pl[j].ch[y], pl[i].ch[x + 1]), 1); n               pr[cnt++] = make_pair( n                   pl[i].ch[x].radio(pl[j].ch[y + 1], pl[i].ch[x + 1]), -1); n             } n           } else if (dr1 >= 0 && dr2 < 0) { n             double s1 = pl[j].ch[y + 1].cross(pl[j].ch[y], pl[i].ch[x]); n             double s2 = pl[j].ch[y + 1].cross(pl[j].ch[y], pl[i].ch[x + 1]); n             pr[cnt++] = make_pair(s1 / (s1 - s2), 1); n           } else if (dr2 >= 0 && dr1 < 0) { n             double s1 = pl[j].ch[y].cross(pl[j].ch[y + 1], pl[i].ch[x]); n             double s2 = pl[j].ch[y].cross(pl[j].ch[y + 1], pl[i].ch[x + 1]); n             pr[cnt++] = make_pair(s1 / (s1 - s2), -1); n           } n         } n       } n       sort(pr, pr + cnt); n       double r = 0., cur, last; n       int sum; n       sum = pr[0].second; n       last = min(max(pr[0].first, 0.), 1.); n       for (int k = 1; k < cnt; k++) { n         cur = min(max(pr[k].first, 0.), 1.); n         cover[sum] += (cur - last) * (pl[i].ch[x] ^ pl[i].ch[x + 1]); n         sum += pr[k].second, last = cur; n       } n     } n   } n   return cover[0] * 0.5; n } n int main() { n   while (~scanf( %d , &n)) { n     double ans = 0.; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 4; j++) pl[i].ch[j].in(); n       pl[i].pnt = 4; n       if (!pl[i].anti()) continue; n       ans += pl[i].S(); n     } n     printf( %.10f n , ans / get()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[10][10]; n bool CheckArr(const string& s) { n   const int n = s.length(); n   for (int a = 0; a < (10); ++a) { n     if (arr[a][0] == 0) continue; n     char c = s[arr[a][0] - 1]; n     for (int i = 0; i < (10); ++i) { n       if (arr[a][i] == 0) break; n       int idx = arr[a][i] - 1; n       if (c != s[idx]) return false; n     } n   } n   for (int i = 0; i < 10; ++i) { n     if (arr[i][0] == 0) continue; n     for (int j = i + 1; j < 10; ++j) { n       if (arr[j][0] == 0) continue; n       int idx1 = arr[i][0] - 1; n       int idx2 = arr[j][0] - 1; n       if (s[idx1] == s[idx2]) return false; n     } n   } n   return true; n } n bool Check(const string& s1, const string& s2) { n   const int n = s1.length(); n   if (n != s2.length()) return false; n   for (int i = 0; i < (n); ++i) { n     if (s1[i] ==  ? ) continue; n     if (s1[i] >=  0  && s1[i] <=  9  && s1[i] == s2[i]) continue; n     if (s1[i] >=  A  && s1[i] <=  J ) continue; n     return false; n   } n   return true; n } n int main() { n   string s; n   cin >> s; n   const int n = s.length(); n   if (n > 5) { n     cout <<  :(  << endl; n     return 0; n   } n   for (int i = 0; i < (n); ++i) n     if (s[i] >=  A  && s[i] <=  J ) { n       int alphaIdx = s[i] -  A ; n       int posIdx = 0; n       while (arr[alphaIdx][posIdx] != 0) ++posIdx; n       arr[alphaIdx][posIdx] = i + 1; n     } n   int ans = 0; n   for (int i = 1; i <= 99999; ++i) { n     char str[6]; n     sprintf(str,  %d , i); n     string s2(str); n     if (Check(s, s2) && CheckArr(s2)) ++ans; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, i = 1, s, p, q; n   cin >> x >> y; n   s = x % 10; n   q = s; n   p = s; n   while (p % 10 != 0 || p != y) { n     if (p % 10 == 0 || p == y) { n       break; n     } n     i++; n     q = q + s; n     p = q % 10; n   } n   cout << i; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long SIZE = 100000; n const int INF = 0x3f3f3f3f; n const long long ll_INF = 0x3f3f3f3f3f3f3f3f; n const long double PI = acos(-1); n const long long MAXN = numeric_limits<long long>::max(); n const long long MAX = 2000000; n void disp(vector<long long> v) { n   for (auto u : v) cout << u <<    ; n   cout <<   n ; n } n void solve() { n   long long l, r, x = 0, y = 1, temp, ans = 0; n   cin >> l >> r; n   y = 1; n   while (y <= r) { n     temp = y; n     if (temp >= l && temp <= r) ans++; n     while (temp <= r) { n       temp *= 2; n       if (temp >= l && temp <= r) ans++; n     } n     y *= 3; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( -ffloat-store ) n using namespace std; n clock_t time_p = clock(); n void aryanc403() {} n const long long int INF = 0xFFFFFFFFFFFFFFFL; n long long int seed; n mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); n inline long long int rnd(long long int l = 0, long long int r = INF) { n   return uniform_int_distribution<long long int>(l, r)(rng); n } n class CMP { n  public: n   bool operator()(pair<long long int, long long int> a, n                   pair<long long int, long long int> b) { n     return !(a.first < b.first || (a.first == b.first && a.second <= b.second)); n   } n }; n void add(map<long long int, long long int> &m, long long int x, n          long long int cnt = 1) { n   auto jt = m.find(x); n   if (jt == m.end()) n     m.insert({x, cnt}); n   else n     jt->second += cnt; n } n void del(map<long long int, long long int> &m, long long int x, n          long long int cnt = 1) { n   auto jt = m.find(x); n   if (jt->second <= cnt) n     m.erase(jt); n   else n     jt->second -= cnt; n } n bool cmp(const pair<long long int, long long int> &a, n          const pair<long long int, long long int> &b) { n   return a.first < b.first || (a.first == b.first && a.second < b.second); n } n const long long int mod = 1000000007L; n long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y; n long long int m; n string s, t, a, b; n bool fl; n vector<vector<long long int> > dp, bb; n long long int solve(long long int n, long long int m) { n   long long int &ans = dp[n + 1][m + 1]; n   if (ans != -1) return ans; n   ans = INF; n   if (n != -1) { n     long long int cur = solve(n - 1, m) + 1; n     if (cur < INF) ans = min(ans, bb[cur][a[n] -  a ]); n   } n   if (m != -1) { n     long long int cur = solve(n, m - 1) + 1; n     if (cur < INF) ans = min(ans, bb[cur][b[m] -  a ]); n   } n   return ans; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> T; n   while (T--) { n     cin >> s >> t; n     s =  #  + s; n     fl = false; n     bb.clear(); n     bb.resize((int)(s).size() + 5, vector<long long int>(26, INF)); n     for (i = ((int)(s).size() - 1); i >= (1); --i) { n       assert(i + 1 < (int)(bb).size()); n       bb[i] = bb[i + 1]; n       bb[i][s[i] -  a ] = i; n     } n     for (long long int l = 1; l <= (int)(t).size(); ++l) { n       a = t.substr(0, l); n       b = t.substr(l); n       n = (int)(a).size(); n       m = (int)(b).size(); n       dp.clear(); n       dp.resize(n + 2, vector<long long int>(m + 2, -1)); n       dp[0][0] = 0; n       if (solve(n - 1, m - 1) < (int)(s).size()) { n         fl = true; n         break; n       } n     } n     cout << (fl ?  YES  :  NO ) <<   n ; n   } n   aryanc403(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char get(void) { n   static char buf[100000], *S = buf, *T = buf; n   if (S == T) { n     T = (S = buf) + fread(buf, 1, 100000, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline void read(int& x) { n   static char c; n   x = 0; n   for (c = get(); c <  0  || c >  9 ; c = get()) n     ; n   for (; c >=  0  && c <=  9 ; c = get()) x = x * 10 + c -  0 ; n } n const int M = 500 * 1000 + 10; n const int up = 300000 + 10; n const int mod = 998244353; n int n; n int cnt[M] = {0}; n bool vs[M] = {0}; n int dp[M] = {0}; n int fac[100]; n int tp; n int num[100]; n int C[8][M] = {0}; n void init() { n   C[0][0] = 1; n   for (int i = 1; i < up; ++i) { n     C[0][i] = 1; n     for (int j = 1; j < 8; ++j) C[j][i] = (C[j - 1][i - 1] + C[j][i - 1]) % mod; n   } n } n void dfs(int c, int al) { n   if (c == tp) { n     ++cnt[al]; n     return; n   } n   for (int i = 0; i < num[c] + 1; ++i) { n     dfs(c + 1, al); n     al *= fac[c]; n   } n } n void add(int a) { n   tp = 0; n   for (int i = 2; i * i <= a; ++i) { n     if (0 == a % i) { n       fac[tp] = i; n       num[tp] = 0; n       while (a % i == 0) ++num[tp], a /= i; n       ++tp; n     } n   } n   if (1 != a) { n     fac[tp] = a; n     num[tp++] = 1; n   } n   dfs(0, 1); n } n bool solve(int x) { n   for (int t = up; t; --t) { n     dp[t] = C[x][cnt[t]]; n     for (int i = 2; i * t < up; ++i) dp[t] = (dp[t] - dp[i * t] + mod) % mod; n   } n   return dp[1] > 0; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   init(); n   cin >> n; n   int a; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     if (vs[a]) continue; n     vs[a] = 1; n     add(a); n   } n   bool fg = 1; n   if (vs[1]) { n     fg = 0; n     cout << 1 << endl; n   } n   for (int i = 2; i < 8; ++i) { n     if (!fg) break; n     if (solve(i)) { n       fg = 0; n       cout << i << endl; n       break; n     } n   } n   if (fg) cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)2e18; n const int N = 1005; n char s[N]; n int lcp[N][N]; n vector<pair<int, int>> segs; n bool cmp(pair<int, int> a, pair<int, int> b) { n   int alen = a.second - a.first; n   int blen = b.second - b.first; n   int clen = min(alen, blen); n   int com = lcp[a.first][b.first]; n   if (com < clen) return s[a.first + com] < s[b.first + com]; n   return alen < blen; n } n int n, m; n int mn[N]; n long long dp[N][N]; n void sadd(long long &a, long long b) { n   a += b; n   if (a >= INF) a = INF; n } n long long getCntGEQ(int id) { n   for (int i = 0; i < n; i++) mn[i] = n + 1; n   for (int i = id; i < (int)segs.size(); i++) n     mn[segs[i].first] = min(mn[segs[i].first], segs[i].second); n   memset(dp, 0, sizeof dp); n   dp[0][0] = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j <= m; j++) { n       long long cur = dp[i][j]; n       if (i != 0) sadd(dp[i + 1][j], cur); n       sadd(dp[mn[i]][j + 1], cur); n     } n   return dp[n][m]; n } n void printStr(pair<int, int> p) { n   int l = p.first, r = p.second; n   for (int i = l; i < r; i++) printf( %c , s[i]); n   printf(  n ); n } n int main(int, char **) { n   long long k; n   scanf( %d%d%lld , &n, &m, &k); n   scanf( %s , s); n   for (int i = n - 1; i >= 0; i--) n     for (int j = n - 1; j >= 0; j--) n       if (s[i] == s[j]) lcp[i][j] = 1 + lcp[i + 1][j + 1]; n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j <= n; j++) segs.emplace_back(i, j); n   sort(segs.begin(), segs.end(), cmp); n   int lb = -1, rb = (int)segs.size(); n   while (rb - lb > 1) { n     int mb = (lb + rb) / 2; n     long long cur = getCntGEQ(mb); n     if (cur >= k) n       lb = mb; n     else n       rb = mb; n   } n   printStr(segs[lb]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using V = vector<T>; n const long long int mod = 1000000007; n struct sb { n   size_t c, ind; n   unsigned long long int a, b; n }; n bool cmp(sb a, sb b) { return a.c < b.c; } n struct s { n   unsigned long long int sub; n   size_t lastsub; n   int before; n   bool can; n } DP[51][101][51]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   size_t N, M; n   unsigned long long int K; n   cin >> N >> M >> K; n   V<sb> subs(M); n   for (__typeof(M) i = (0) - ((0) > (M)); i != (M) - ((0) > (M)); n        i += ((0) > (M) ? -1 : 1)) n     cin >> subs[i].a >> subs[i].b >> subs[i].c, subs[i].ind = i; n   sort((subs).begin(), (subs).end(), cmp); n   pair<size_t, unsigned long long int> ans; n   for (__typeof(M) i = (0) - ((0) > (M)); i != (M) - ((0) > (M)); n        i += ((0) > (M) ? -1 : 1)) { n     for (__typeof(subs[i].b + 1) j = n              (subs[i].a) - ((subs[i].a) > (subs[i].b + 1)); n          j != (subs[i].b + 1) - ((subs[i].a) > (subs[i].b + 1)); n          j += ((subs[i].a) > (subs[i].b + 1) ? -1 : 1)) { n       size_t l = j - subs[i].a; n       DP[i][l][1].sub = j; n       DP[i][l][1].lastsub = 0; n       DP[i][l][1].before = -1; n       DP[i][l][1].can = true; n       for (__typeof(N + 1) k = (2) - ((2) > (N + 1)); n            k != (N + 1) - ((2) > (N + 1)); k += ((2) > (N + 1) ? -1 : 1)) { n         DP[i][l][k].sub = 0; n         DP[i][l][k].lastsub = 0; n         DP[i][l][k].before = -1; n         DP[i][l][k].can = false; n         for (__typeof(0) z = (i) - ((i) > (0)); z != (0) - ((i) > (0)); n              z += ((i) > (0) ? -1 : 1)) { n           if (subs[z].c == subs[i].c) continue; n           if (j % K == 0 && j / K >= subs[z].a && n               (j / K - subs[z].a) <= subs[z].b - subs[z].a) { n             if (DP[z][j / K - subs[z].a][k - 1].can && n                 DP[z][j / K - subs[z].a][k - 1].sub + j >= DP[i][l][k].sub) { n               DP[i][l][k].sub = DP[z][j / K - subs[z].a][k - 1].sub + j; n               DP[i][l][k].lastsub = j / K - subs[z].a; n               DP[i][l][k].can = true; n               DP[i][l][k].before = z; n             } n           } n           if (j >= K + subs[z].a && n               (j - K - subs[z].a) <= subs[z].b - subs[z].a) { n             if (DP[z][j - K - subs[z].a][k - 1].can && n                 DP[z][j - K - subs[z].a][k - 1].sub + j >= DP[i][l][k].sub) { n               DP[i][l][k].sub = DP[z][j - K - subs[z].a][k - 1].sub + j; n               DP[i][l][k].lastsub = j - K - subs[z].a; n               DP[i][l][k].can = true; n               DP[i][l][k].before = z; n             } n           } n         } n       } n       if (DP[i][l][N].can && n           DP[i][l][N].sub >= DP[ans.first][ans.second][N].sub) n         ans = {i, l}; n     } n   } n   if (!DP[ans.first][ans.second][N].can) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   V<pair<size_t, unsigned long long int> > res; n   for (__typeof(1) i = (N + 1) - ((N + 1) > (1)); i != (1) - ((N + 1) > (1)); n        i += ((N + 1) > (1) ? -1 : 1)) { n     res.push_back({subs[ans.first].ind + 1, subs[ans.first].a + ans.second}); n     ans = {DP[ans.first][ans.second][i].before, n            DP[ans.first][ans.second][i].lastsub}; n   } n   for (__typeof(0) i = (N) - ((N) > (0)); i != (0) - ((N) > (0)); n        i += ((N) > (0) ? -1 : 1)) n     cout << res[i].first <<     << res[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int32_t INFint = 1e9; n const long long INFll = 1e18; n int32_t 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     long long n; n     cin >> n; n     long long arr[n]; n     for (long long i = 0; i < n; i++) cin >> arr[i]; n     ; n     sort(arr, arr + n); n     long long ans = arr[0] * arr[n - 1]; n     long long fact[100000]; n     long long index = 0; n     for (long long i = 2; i <= sqrt(ans); i++) { n       if (ans % i == 0) { n         fact[index] = i; n         index++; n         if (i != ans / i) { n           fact[index] = ans / i; n           index++; n         } n       } n     } n     sort(fact, fact + index); n     if (index != n) { n       cout << -1 <<   n ; n     } else { n       long long flag = 0; n       for (long long i = 0; i < n; i++) { n         if (fact[i] != arr[i]) { n           flag = 1; n           break; n         } n       } n       ans = flag ? -1 : ans; n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   map<pair<string, string>, int> map_count; n   vector<int> cost_list; n   int n, a, b, k, f; n   string s, t, prv =   ; n   cin >> n >> a >> b >> k >> f; n   for (int cost, i = 0; i < n; i++) { n     cin >> s >> t; n     if (s == prv) { n       cost = b; n     } else { n       cost = a; n     } n     prv = t; n     if (s > t) { n       swap(s, t); n     } n     map_count[make_pair(s, t)] += cost; n   } n   for (auto& pkv : map_count) { n     cost_list.push_back(pkv.second); n   } n   sort(cost_list.begin(), cost_list.end(), greater<int>()); n   int res = 0; n   for (int i = 0; i < cost_list.size(); i++) { n     if (i < k && cost_list[i] > f) { n       res += f; n     } else { n       res += cost_list[i]; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> compos; n int complement_component(vector<vector<int>> &g) { n   int n = g.size(), ans = 0; n   set<int> S; n   for (int i = 0; i < n; ++i) S.insert(i); n   while (S.size()) { n     ++ans; n     int theCompo = 0; n     queue<int> q; n     q.push(*S.begin()); n     S.erase(S.begin()); n     compos.push_back(vector<int>{}); n     while (q.size()) { n       int u = q.front(); n       q.pop(); n       compos.back().push_back(u); n       if (g[u].size()) { n         while (S.size() and *S.begin() < g[u][0]) { n           q.push(*S.begin()); n           S.erase(S.begin()); n         } n         int it = 0; n         while (S.size() and it < g[u].size()) { n           auto cur = S.upper_bound(g[u][it]); n           while (cur != S.end() and n                  (it + 1 == g[u].size() or *cur < g[u][it + 1])) { n             q.push(*cur), S.erase(cur); n             cur = S.upper_bound(g[u][it]); n           } n           ++it; n         } n       } else { n         compos.back() = vector<int>(n); n         iota(compos.back().begin(), compos.back().end(), 0); n         return 1; n       } n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int N, M, u, v; n   cin >> N >> M; n   vector<vector<int>> g(N); n   while (M--) { n     cin >> u >> v, --u, --v; n     g[u].push_back(v), g[v].push_back(u); n   } n   for (int i = 0; i < N; ++i) sort(g[i].begin(), g[i].end()); n   cout << complement_component(g) <<   n ; n   sort(compos.begin(), compos.end()); n   for (auto &compo : compos) { n     cout << compo.size() <<    ; n     for (int i = 0; i < compo.size(); ++i) n       cout << compo[i] + 1 <<    n [i == compo.size() - 1]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n vector<long long int> a, b; n struct segmentTree { n   vector<long long int> t; n   segmentTree() { t.resize(4 * n); } n   void add(long long int ind, long long int x, long long int v = 0, n            long long int tl = 0, long long int tr = n) { n     if (tr - tl == 1) n       t[v] += x; n     else { n       long long int mid = (tl + tr) / 2; n       if (ind >= mid) n         add(ind, x, 2 * v + 2, mid, tr); n       else n         add(ind, x, 2 * v + 1, tl, mid); n       t[v] = t[2 * v + 1] + t[2 * v + 2]; n     } n   } n   long long int getK(long long int k, long long int v = 0, long long int tl = 0, n                      long long int tr = n) { n     if (tr - tl == 1) n       return tl; n     else { n       long long int mid = (tl + tr) / 2; n       if (t[2 * v + 1] >= k) n         return getK(k, 2 * v + 1, tl, mid); n       else n         return getK(k - t[2 * v + 1], 2 * v + 2, mid, tr); n     } n   } n }; n struct query { n   long long int k, p, ind; n   query(long long int k = 0, long long int p = 0, long long int ind = 0) n       : k(k), p(p), ind(ind) {} n }; n signed main() { n   cin >> n; n   a.resize(n); n   b.resize(n); n   iota(b.begin(), b.end(), 0); n   for (long long int i = 0; i < n; ++i) cin >> a[i]; n   sort(b.begin(), b.end(), [](long long int i, long long int j) { n     return a[i] > a[j] || (a[i] == a[j] && i < j); n   }); n   long long int q; n   cin >> q; n   vector<long long int> ans(q); n   vector<query> queries; n   for (long long int i = 0; i < q; ++i) { n     long long int k, p; n     cin >> k >> p; n     queries.emplace_back(k, p, i); n   } n   sort(queries.begin(), queries.end(), n        [](const query& a, const query& b) { return a.k < b.k; }); n   long long int last = 0; n   segmentTree t; n   for (auto ev : queries) { n     while (last < ev.k) t.add(b[last++], 1); n     long long int ind = t.getK(ev.p); n     ans[ev.ind] = a[ind]; n   } n   for (auto i : ans) cout << i <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e18; n long long k, n, c; n string s; n long long p[26][26]; n long long f[105][105][26]; n char x, y; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s >> k >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> x >> y >> c; n     p[x -  a ][y -  a ] = c; n   } n   s =  *  + s; n   for (long long i = 0; i < s.size(); i++) n     for (long long j = 0; j <= k; j++) n       for (long long v = 0; v < 26; v++) f[i][j][v] = -MAX; n   for (long long i = 0; i < 26; i++) { n     if (i == s[1] -  a ) n       f[1][0][i] = 0; n     else n       f[1][1][i] = 0; n   } n   for (long long i = 2; i < s.size(); i++) { n     for (long long j = 0; j <= min(i - 1, k); j++) { n       for (long long v = 0; v < 26; v++) { n         for (long long u = 0; u < 26; u++) { n           long long ok = (s[i] -  a  != v); n           f[i][j + ok][v] = max(f[i][j + ok][v], f[i - 1][j][u] + p[u][v]); n         } n       } n     } n   } n   long long ans = -MAX; n   for (long long i = 0; i <= k; i++) n     for (long long j = 0; j < 26; j++) ans = max(f[s.size() - 1][i][j], ans); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, mx, a[100][100], dp[100][100], me[100], inf = -1e15; n int sub(int a, int b) { return (((a - b) % k) + k); } n signed main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   memset(dp, -1, sizeof dp); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       for (int u = min(m / 2, j); u >= 1; u--) { n         for (int t = 0; t < k; t++) { n           int x = sub(t, a[i][j]) % k; n           if (dp[u - 1][x] != -1) n             dp[u][t] = max(dp[u][t], dp[u - 1][x] + a[i][j]); n         } n       } n     } n     for (int u = 1; u <= m / 2; u++) { n       for (int t = 0; t < k; t++) { n         dp[0][t] = max(dp[0][t], dp[u][t]); n         if (n != i) dp[u][t] = -1; n       } n     } n   } n   int mx = 0; n   for (int i = 0; i <= m / 2; i++) mx = max(mx, dp[i][0]); n   cout << mx <<   n ; n } n