#include <bits/stdc++.h> n using namespace std; n bool isPrime(long long n) { n   if (n <= 1) return false; n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return false; n   return true; n } n long long mod = 1000000007; n int main() { n   long long i, j, k = 0, n, m = 0, a[300001], b[300001], t = 0, ans = 0, i1 = 0, n                   i2, h, p1, p2 = 0, q, l, f, c[2], mi; n   char c1, c2, d[1001][1001]; n   string s; n   double j1; n   string e, dz; n   std::vector<pair<long long, long long>> vect; n   vector<long long> v; n   map<string, long long> m1; n   map<string, long long>::iterator itr; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   j = 1; n   for (i = 0; i < n; i++) { n     if (a[i] >= j) { n       t++; n       j++; n     } n   } n   cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n int lim = 1e6; n int inf = 1e9 + 7; n bool ispal(string s) { n   int n = s.size(); n   bool ans = true; n   for (int i = 0; i < n / 2; i++) { n     if (s[i] != s[n - i - 1]) { n       ans = false; n       break; n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     if (!ispal(s)) { n       cout << s <<   n ; n       ; n     } else { n       char x = s[0]; n       bool check = false; n       string ans = s; n       for (int i = 1; i < s.size(); i++) { n         if (x != s[i]) { n           check = true; n           ans[0] = ans[i]; n           ans[i] = x; n           break; n         } n       } n       if (check) { n         cout << ans <<   n ; n         ; n       } else { n         cout << -1 <<   n ; n         ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   int ans = 1; n   for (int i = 1; i <= min(a, b); i++) ans *= i; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n int main() { n   scanf( %lld , &n); n   if (n == 1 || n == 3 || n == 4 || n == 6 || n == 9 || n == 12) { n     cout << 0 << endl; n     return 0; n   } n   int mod = n % 3, mx; n   if (mod == 2) { n     for (long long i = 1e7; i >= 1; i -= 3) { n       if (i * i * 3 + i <= n * 2) { n         mx = i; n         break; n       } n     } n   } else if (mod == 1) { n     for (long long i = 1e7 + 1; i >= 1; i -= 3) { n       if (i * i * 3 + i <= n * 2) { n         mx = i; n         break; n       } n     } n   } else if (mod == 0) { n     for (long long i = 1e7 + 5; i >= 1; i -= 3) { n       if (i * i * 3 + i <= n * 2) { n         mx = i; n         break; n       } n     } n   } n   cout << (int)ceil((double)mx / 3) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> P[2]; n int n; n int mark[100011]; n int cnt[100011]; n int cor(int p, int n) { return 2 * n + p; } n int main() { n   int k, x; n   cin >> n; n   for (int i = 0; i < 2; i++) { n     cin >> k; n     for (int j = 0; j < k; j++) { n       cin >> x; n       P[i].push_back(x); n     } n   } n   mark[cor(0, 0)] = mark[cor(1, 0)] = 2; n   queue<int> Q; n   Q.push(cor(0, 0)); n   Q.push(cor(1, 0)); n   while (Q.size()) { n     int v = Q.front(); n     Q.pop(); n     if (mark[v] == 2) { n       int p = v % 2; n       v /= 2; n       int val; n       for (auto x : P[1 - p]) { n         val = (v - x + 2 * n) % n; n         x = cor(1 - p, val); n         if (!mark[x]) { n           mark[x] = 1; n           Q.push(x); n         } n       } n       continue; n     } n     int p = v % 2; n     v /= 2; n     int val; n     for (auto x : P[1 - p]) { n       val = (v - x + 2 * n) % n; n       x = cor(1 - p, val); n       cnt[x]++; n       if (mark[x] == 0 and cnt[x] == P[x % 2].size()) { n         Q.push(x); n         mark[x] = 2; n       } n     } n   } n   for (int i = 1; i < n; i++) { n     x = mark[cor(0, i)]; n     if (x == 1) { n       cout <<  Win  ; n     } else if (x == 2) { n       cout <<  Lose  ; n     } else { n       cout <<  Loop  ; n     } n   } n   cout <<   n ; n   for (int i = 1; i < n; i++) { n     x = mark[cor(1, i)]; n     if (x == 1) { n       cout <<  Win  ; n     } else if (x == 2) { n       cout <<  Lose  ; n     } else { n       cout <<  Loop  ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; 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 template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n long long mystoi(string str) { n   stringstream ss(str); n   long long ans = 0; n   ss >> ans; n   return ans; n } n long long exp(long long x, long long ex) { n   long long ans = 1ll; n   while (ex > 0) { n     if (ex & 1ll) ans = (ans * x); n     ex >>= 1ll; n     x = (x * x); n   } n   return ans; n } n bool sort_cond(const pair<long long, long long>& a, n                const pair<long long, long long>& b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first < b.first; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   long long lo; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> a(2 * n); n     cin >> a; n     vector<long long> o, e; n     for (long long i = (0); i < (long long)(2 * n); i++) { n       if (a[i] % 2) n         o.push_back(i + 1); n       else n         e.push_back(i + 1); n     } n     long long cnt = 0; n     if (o.size() % 2) o.pop_back(); n     if (e.size() % 2) e.pop_back(); n     if (o.size() > e.size()) { n       for (long long i = (0); i < (long long)(o.size()); i++) { n         if (cnt == n - 1) break; n         cout << o[i] <<     << o[i + 1] << endl; n         cnt++; n         i++; n       } n       for (long long i = (0); i < (long long)(e.size()); i++) { n         if (cnt == n - 1) break; n         cout << e[i] <<     << e[i + 1] << endl; n         cnt++; n         i++; n       } n     } else { n       for (long long i = (0); i < (long long)(e.size()); i++) { n         if (cnt == n - 1) break; n         cout << e[i] <<     << e[i + 1] << endl; n         cnt++; n         i++; n       } n       for (long long i = (0); i < (long long)(o.size()); i++) { n         if (cnt == n - 1) break; n         cout << o[i] <<     << o[i + 1] << endl; n         cnt++; n         i++; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   char last, f; n   cin >> last; n   int ans = 0; n   f = last; n   for (int i = 1; i < n; i++) { n     char a; n     cin >> a; n     if (a != last) ans++; n     last = a; n   } n   if (f ==  F ) n     cout <<  NO n ; n   else n     cout << ((ans & 1) ?  YES n  :  NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n deque<int> a[355]; n int n, m, S, Q, z, L, R, k, l, r, x, c[355][100005]; n int ans; n void deal(int &x) { x = (x + ans - 1) % n + 1; } n int main() { n   scanf( %d , &n); n   S = int(sqrt(n)), m = n / S + 1; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     a[i / S].push_back(x); n     c[i / S][x]++; n   } n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d%d%d , &z, &L, &R); n     deal(L), deal(R); n     if (L > R) swap(L, R); n     L--, l = L / S, r = R / S; n     if (z == 2) { n       scanf( %d , &k); n       deal(k); n       ans = 0; n       if (l == r) { n         for (int i = L % S, _ = R % S; i < _; i++) ans += a[l][i] == k; n       } else { n         for (int i = l + 1; i < r; i++) ans += c[i][k]; n         for (int i = L % S, _ = a[l].size(); i < _; i++) ans += a[l][i] == k; n         for (int i = 0, _ = R % S; i < _; i++) ans += a[r][i] == k; n       } n       printf( %d n , ans); n     } else { n       if (l == r) { n         R = R % S - 1, x = a[r][R], a[r].erase(a[r].begin() + R); n         a[l].insert(a[l].begin() + L % S, x); n       } else { n         for (int i = l; i < r;) { n           x = a[i].back(), a[i].pop_back(), c[i][x]--, i++; n           a[i].push_front(x), c[i][x]++; n         } n         R %= S, x = a[r][R], a[r].erase(a[r].begin() + R), c[r][x]--; n         a[l].insert(a[l].begin() + L % S, x), c[l][x]++; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[500001], pref[500001], valv[500001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int t, i, j, k, l, m = 0, n, o, p, q, r, s, x; n   cin >> n >> x; n   pref[0] = 0; n   valv[0] = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     valv[i + 1] = valv[i] + (a[i] * (a[i] + 1)) / 2; n     a[i + n] = a[i]; n     pref[i + 1] = pref[i] + a[i]; n     m = max(m, a[i]); n   } n   for (i = n; i < 2 * n; i++) { n     pref[i + 1] = pref[i] + a[i]; n     valv[i + 1] = valv[i] + (a[i] * (a[i] + 1)) / 2; n   } n   long long int ans = 0; n   if (m >= x) { n     while (x > 0) { n       ans += m; n       m--; n       x--; n     } n     cout << ans <<   n ; n     return 0; n   } n   for (i = 0; i < 2 * n; i++) { n     long long int start = 0; n     long long int end = i, index = 0; n     while (start <= end) { n       long long int mid = (start + end) / 2; n       if (pref[i + 1] - pref[mid] >= x) { n         index = mid; n         start = mid + 1; n       } else { n         end = mid - 1; n       } n     } n     long long int ss = pref[i + 1] - pref[index + 1], ans1, s1, s2; n     ans1 = valv[i + 1] - valv[index + 1]; n     long long int sum = x - ss; n     s1 = a[index]; n     ans1 += (s1 * (s1 + 1)) / 2; n     long long int z = max(0LL, s1 - sum); n     ans1 -= (z * (z + 1)) / 2; n     ans = max(ans1, ans); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const long long mod = 998244353; n long long qpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b /= 2; n   } n   return ans; n } n long long b[maxn], inv[maxn]; n long long C(long long n, long long m) { n   if (m > n) return 0; n   return b[n] * inv[m] % mod * inv[n - m] % mod; n } n long long Lucas(long long n, long long m) { n   if (m == 0) return 1; n   return C(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod; n } n long long S(long long n, long long m) { n   long long ans = 0; n   for (int k = 0; k <= m; k++) { n     if (k & 1) n       ans = (ans - C(m, k) * qpow(m - k, n) % mod + mod) % mod; n     else n       ans = (ans + C(m, k) * qpow(m - k, n) % mod) % mod; n   } n   return ans; n } n int main() { n   int n, m; n   cin >> n >> m; n   if (m >= n) n     cout << 0 << endl; n   else { n     b[0] = 1; n     inv[0] = 1; n     for (int i = 1; i <= n; i++) n       b[i] = b[i - 1] * i % mod, inv[i] = qpow(b[i], mod - 2) % mod; n     long long ans = S(n, n - m) * C(n, n - m) % mod; n     if (m == 0) n       cout << ans << endl; n     else n       cout << ans * 2 % mod << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int q, x_1, y_1, x_2, y_2, k, len, a[35], b[35], c[35]; n pair<int, int> dp[35][2][2][2]; n int Add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } n int Minus(int a, int b) { return a < b ? a - b + mod : a - b; } n pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { n   return make_pair(Add(a.first, b.first), Add(a.second, b.second)); n } n pair<int, int> dfs(int now, int f1, int f2, int f3) { n   if (now == -1) return make_pair(1, 0); n   if (~dp[now][f1][f2][f3].first) return dp[now][f1][f2][f3]; n   pair<int, int> ret = make_pair(0, 0); n   for (int i = 0; i <= (f1 ? a[now] : 1); i++) n     for (int j = 0; j <= (f2 ? b[now] : 1); j++) n       if ((i ^ j) <= (f3 ? c[now] : 1)) { n         pair<int, int> tmp = dfs(now - 1, f1 && i == a[now], f2 && j == b[now], n                                  f3 && (i ^ j) == c[now]); n         ret = ret + tmp; n         if (i ^ j) ret.second = (ret.second + (1ll << now) * tmp.first) % mod; n       } n   dp[now][f1][f2][f3] = ret; n   return ret; n } n int calc(int x, int y, int k) { n   if (x < 0 || y < 0) return 0; n   memset(dp, -1, sizeof(dp)); n   len = 0; n   while (x || y || k) { n     a[len] = (x & 1); n     b[len] = (y & 1); n     c[len++] = (k & 1); n     x >>= 1; n     y >>= 1; n     k >>= 1; n   } n   pair<int, int> ret = dfs(len - 1, 1, 1, 1); n   return Add(ret.first, ret.second); n } n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d%d%d%d , &x_1, &y_1, &x_2, &y_2, &k); n     printf( n          %d n , n         Minus( n             Add(calc(x_2 - 1, y_2 - 1, k - 1), calc(x_1 - 2, y_1 - 2, k - 1)), n             Add(calc(x_1 - 2, y_2 - 1, k - 1), calc(x_2 - 1, y_1 - 2, k - 1)))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool visited[100005]; n vector<long long> arr[100005]; n int main() { n   std::ios::sync_with_stdio(false); n   long long n, x; n   cin >> n; n   vector<pair<long long, long long> > ans; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     arr[x].push_back(i); n   } n   for (long long i = 0; i < 100005; i++) { n     if (arr[i].size() == 1) { n       ans.push_back(make_pair(i, 0)); n     } else if (arr[i].size() >= 2) { n       bool good = 1; n       long long dif = arr[i][1] - arr[i][0]; n       for (long long j = 1; j < arr[i].size(); j++) { n         if (arr[i][j] != arr[i][j - 1] + dif) good = 0; n       } n       if (good) { n         ans.push_back(make_pair(i, dif)); n       } n     } n   } n   cout << ans.size() << endl; n   for (long long i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modI(long long a, long long m); n long long gcd(long long a, long long b); n long long powM(long long x, long long y, long long m); n long long swap(long long a, long long b); n void swap(long long& a, long long& b) { n   long long tp = a; n   a = b; n   b = tp; n } n long long gcd(long long x, long long y) { n   if (x == 0) return y; n   return gcd(y % x, x); n } n long long powM(long long x, long long y, long long m) { n   long long ans = 1, r = 1; n   x %= m; n   while (r > 0 && r <= y) { n     if (r & y) { n       ans *= x; n       ans %= m; n     } n     r <<= 1; n     x *= x; n     x %= m; n   } n   return ans; n } n long long modI(long long a, long long m) { n   long long m0 = m, y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m; n     a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   long long tt = 1; n   while (tt--) { n     long long n; n     cin >> n; n     long long a[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     long long ans = 0; n     for (long long mx = 1; mx < 31; mx++) { n       long long dp = 0; n       long long tmp = 0; n       for (long long i = 0; i < n; i++) { n         if (a[i] <= mx) { n           dp = max((long long)0, dp + a[i]); n         } n         tmp = max(dp, tmp); n       } n       ans = max(ans, tmp - mx); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<long long> a(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int m; n   cin >> m; n   long long ans = 0; n   long long wt = 0, ht = 0; n   while (m--) { n     long long w, h; n     cin >> w >> h; n     ans = max(ans + ht, a[w]); n     wt = w; n     ht = h; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   string x, y; n   cin >> x >> y; n   char ans[200]; n   for (int i = 0; i < x.size(); i++) { n     if (y[i] == x[i]) { n       ans[i] = x[i]; n     } else if (y[i] < x[i]) { n       ans[i] = y[i]; n     } else { n       cout << -1 <<   n ; n       return 0; n     } n   } n   for (int i = 0; i < x.size(); i++) { n     cout << ans[i]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[4] = {}; n   cin >> n; n   for (int i = 0; i < n; i++) { n     char c; n     cin >> c; n     a[0] += c ==  U ; n     a[1] += c ==  D ; n     a[2] += c ==  L ; n     a[3] += c ==  R ; n   } n   long res = min(a[0], a[1]) * 2 + min(a[2], a[3]) * 2; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = LLONG_MAX; n bool check(char a, char b) { return a ==  _  || a == b; } n long long foo(string s, string tail) { n   if (!check(s[s.size() - 2], tail[0]) || !check(s.back(), tail[1])) return 0; n   s[s.size() - 2] = tail[0]; n   s[s.size() - 1] = tail[1]; n   if (s[0] ==  0 ) return 0; n   long long res = 1; n   for (long long i = 0; i + 2 < s.size(); i++) { n     if (s[i] ==  _ ) { n       res *= i > 0 ? 10 : 9; n     } n   } n   return res; n } n string x(string s, long long val) { n   for (auto &i : s) { n     if (i ==  X ) { n       i =  0  + val; n     } n   } n   return s; n } n int main() { n   ios_base::sync_with_stdio(0); n   string s; n   cin >> s; n   long long res = 0; n   if (s.size() == 1) { n     res = (long long)(s[0] ==  _  || s[0] ==  X  || s[0] ==  0 ); n   } else { n     if (count(s.begin(), s.end(),  X )) { n       for (long long i = 0; i < 10; i++) { n         string temp = x(s, i); n         res += foo(temp,  25 ) + foo(temp,  50 ) + foo(temp,  75 ) + n                foo(temp,  00 ); n       } n     } else { n       res += foo(s,  25 ) + foo(s,  50 ) + foo(s,  00 ) + foo(s,  75 ); n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200; n int n, a[N], m; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     if (x - 1 >= 0) a[x - 1] += y - 1; n     if (x + 1 < n) a[x + 1] += a[x] - y; n     a[x] = 0; n   } n   for (int i = 0; i < n; i++) printf( %d n , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n bool sev(int h, int m); n using namespace std; n int main() { n   int snooze; n   cin >> snooze; n   int h; n   int m; n   int n = 0; n   cin >> h >> m; n   if (sev(h, m)) { n     cout <<  0  << endl; n   } else { n     while (sev(h, m) == false) { n       if (m >= snooze) { n         m = m - snooze; n       } else { n         if (h == 0) h = 24; n         int x = snooze; n         x = x - m; n         h = h - 1; n         m = 60; n         m = m - (x); n       } n       n++; n     } n     cout << n; n   } n   return 0; n } n bool sev(int h, int m) { n   if (h % 10 == 7 || h / 10 % 10 == 7 || m % 10 == 7 || m / 10 % 10 == 7) n     return true; n   else n     return false; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, ans, sum = 0; n   cin >> x; n   x = abs(x); n   for (int i = 0;; i++) { n     sum += i; n     if (sum >= x && (sum - x) % 2 == 0) { n       ans = i; n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> g[200001]; n vector<long long int> g1[200001]; n vector<long long int> vec; n bool vis[200001]; n void fun(long long int u) { n   vis[u] = 1; n   for (long long int i = 0; i < g[u].size(); i++) { n     long long int v = g[u][i]; n     if (!vis[v]) { n       fun(v); n     } n   } n   vec.push_back(u); n } n bool st[200001]; n bool foo = 1; n bool check(long long int u) { n   if (!foo) return false; n   st[u] = 1; n   vis[u] = 1; n   for (long long int i = 0; i < g[u].size(); i++) { n     long long int v = g[u][i]; n     if (st[v]) { n       foo = 0; n       return false; n     } n     if (!vis[v]) check(v); n   } n   st[u] = 0; n   return true; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, m; n     cin >> n >> m; n     for (long long int i = 1; i <= n; i++) { n       g[i].clear(); n       g1[i].clear(); n     } n     vec.clear(); n     memset(vis, false, sizeof(vis)); n     memset(st, 0, sizeof(st)); n     for (long long int i = 1; i <= m; i++) { n       long long int k, u, v; n       cin >> k >> u >> v; n       if (k == 1) { n         g[u].push_back(v); n       } else { n         g1[u].push_back(v); n         g1[v].push_back(u); n       } n     } n     bool ans = 1; n     foo = 1; n     for (long long int i = 1; i <= n; i++) { n       if (!vis[i]) { n         check(i); n         if (!foo) { n           ans = 0; n           break; n         } n       } n     } n     if (!ans) { n       cout <<  NO n ; n       continue; n     } n     memset(vis, 0, sizeof(vis)); n     for (long long int i = 1; i <= n; i++) { n       if (!vis[i]) fun(i); n     } n     reverse(vec.begin(), vec.end()); n     map<pair<long long int, long long int>, bool> done; n     for (long long int i = 0; i < vec.size(); i++) { n       long long int u = vec[i]; n       for (long long int j = 0; j < g1[u].size(); j++) { n         long long int v = g1[u][j]; n         long long int u1 = min(u, v), v1 = max(u, v); n         if (!done[{u1, v1}]) { n           done[{u1, v1}] = 1; n           g[u].push_back(v); n         } n       } n     } n     cout <<  YES n ; n     for (long long int i = 1; i <= n; i++) { n       for (long long int j = 0; j < g[i].size(); j++) { n         cout << i <<     << g[i][j] <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int ans[maxn]; n vector<int> xs[maxn], ys[maxn]; n struct query { n   int l, r, d, u, id; n   query(int aa, int bb, int cc, int dd, int ee) { n     l = aa; n     r = bb; n     d = cc; n     u = dd; n     id = ee; n   } n   query() {} n }; n query qry[maxn]; n vector<int> xq[maxn], yq[maxn]; n struct SegmentTree { n   SegmentTree *lc, *rc; n   static SegmentTree mem[2000000]; n   static SegmentTree *ptr; n   int pos; n   SegmentTree() {} n   ~SegmentTree() {} n   SegmentTree(int L, int R) { n     lc = rc = nullptr; n     pos = 0; n     if (L == R) return; n     lc = new (ptr++) SegmentTree(L, ((L + R) >> 1)); n     rc = new SegmentTree(((L + R) >> 1) + 1, R); n   } n   void pull() { pos = min(lc->pos, rc->pos); } n   void modify(int L, int R, int x, int v) { n     if (L == R) { n       pos = v; n       return; n     } n     if (x <= ((L + R) >> 1)) n       lc->modify(L, ((L + R) >> 1), x, v); n     else n       rc->modify(((L + R) >> 1) + 1, R, x, v); n     pull(); n   } n   int ask(int L, int R, int l, int r) { n     if (L > r || l > R) return maxn; n     if (L >= l && R <= r) return pos; n     return min(lc->ask(L, ((L + R) >> 1), l, r), n                rc->ask(((L + R) >> 1) + 1, R, l, r)); n   } n } * stx, *sty; n SegmentTree SegmentTree::mem[2000000]; n SegmentTree *SegmentTree::ptr = mem; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k, q; n   cin >> n >> m >> k >> q; n   for (int i = 0; i < k; ++i) { n     int x, y; n     cin >> x >> y; n     xs[x].push_back(y); n     ys[y].push_back(x); n   } n   for (int i = 0; i < q; ++i) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     qry[i] = query(x1, x2, y1, y2, i); n     xq[x2].push_back(i); n     yq[y2].push_back(i); n   } n   stx = new (SegmentTree::ptr++) SegmentTree(0, maxn - 1); n   sty = new (SegmentTree::ptr++) SegmentTree(0, maxn - 1); n   for (int i = 0; i < maxn; ++i) n     if (xs[i].size() || xq[i].size()) { n       for (int j : xs[i]) stx->modify(0, maxn - 1, j, i); n       for (int j : xq[i]) { n         if (stx->ask(0, maxn - 1, qry[j].d, qry[j].u) < qry[j].l) ++ans[j]; n       } n     } n   for (int i = 0; i < maxn; ++i) n     if (ys[i].size() || yq[i].size()) { n       for (int j : ys[i]) sty->modify(0, maxn - 1, j, i); n       for (int j : yq[i]) { n         if (sty->ask(0, maxn - 1, qry[j].l, qry[j].r) < qry[j].d) ++ans[j]; n       } n     } n   for (int i = 0; i < q; ++i) { n     if (ans[i] == 2) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   if (n == 5 && k == 2) { n     puts( YES ); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (i & 1) n           printf( L ); n         else n           printf( S ); n       } n       puts(  ); n     } n     return 0; n   } n   vector<string> ans(n,   ); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int sum = (i + j) & 1; n       if (sum == 0 && k > 0) { n         ans[i] +=  L ; n         k--; n       } else { n         ans[i] +=  S ; n       } n     } n   } n   if (k == 0) { n     puts( YES ); n     for (int i = 0; i < n; i++) puts(ans[i].c_str()); n   } else { n     puts( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t; n   cin >> t; n   while (t--) { n     long long n, p, k; n     cin >> n >> p >> k; n     long long ans = 0, pre = 0; n     vector<long long> a(n, -1); n     for (long long i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     sort(a.begin(), a.end()); n     for (long long i = 0; i <= k; ++i) { n       long long sum = pre; n       if (sum > p) break; n       long long ct = i; n       for (long long j = i + k - 1; j < n; j += k) { n         if (sum + a[j] <= p) { n           ct += k; n           sum += a[j]; n         } else n           break; n       } n       pre += a[i]; n       ans = max(ans, ct); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   fast(); n   long long i, j, n, t, su = 0, k; n   cin >> t; n   vector<long long> v[t], s[t]; n   vector<pair<long long, pair<long long, long long>>> p; n   for (i = 0; i < t; i++) { n     cin >> n; n     su = 0; n     for (j = 0; j < n; j++) { n       cin >> k; n       v[i].push_back(k); n       su += k; n     } n     for (j = 0; j < n; j++) { n       p.push_back(make_pair(su - v[i][j], make_pair(i + 1, j + 1))); n     } n   } n   sort(p.begin(), p.end()); n   for (i = 0; i < p.size() - 1; i++) { n     if ((p[i].first == p[i + 1].first) && n         p[i].second.first != p[i + 1].second.first) { n       cout <<  YES  << endl; n       cout << p[i].second.first <<     << p[i].second.second << endl; n       cout << p[i + 1].second.first <<     << p[i + 1].second.second << endl; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned int dp[(int)1e3 + 7], mn[(int)1e3 + 7], n; n int a[(int)1e3 + 7], b[26], ml = 0; n char aa[(int)1e3 + 7]; n void dpi() { n   dp[0] = 1; n   mn[0] = 1; n   ml = 1; n   for (int i = 1; i < n; i++) { n     int mm = a[i]; n     dp[i] = dp[i - 1]; n     mn[i] = mn[i - 1] + 1; n     for (int j = i - 1; j > 0; j--) { n       mm = min(a[j], mm); n       if (mm >= i - j + 1) { n         dp[i] += dp[j - 1]; n         dp[i] %= (int)1e9 + 7; n         mn[i] = min(mn[j - 1] + 1, mn[i]); n         ml = max(ml, i - j + 1); n       } n     } n     mm = min(a[0], mm); n     if (mm >= i + 1) { n       dp[i]++; n       mn[i] = 1; n       ml = max(ml, i + 1); n     } n   } n } n int main() { n   scanf( %d , &n); n   scanf( %s , aa); n   for (int i = 0; i < 26; i++) scanf( %d , &b[i]); n   for (int i = 0; i < n; i++) a[i] = b[aa[i] -  a ]; n   dpi(); n   printf( %d n%d n%d , dp[n - 1], ml, mn[n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double l[10], b[10]; n vector<int> kk(10, -1); n vector<vector<int> > v; n int k, n; n void abc(int i, int sum) { n   if (i == 0 && sum == 0) { n     v.push_back(kk); n     return; n   } n   if (i == 0) { n     return; n   } n   for (int j = 0; j <= k; j++) { n     if (sum - j < 0) break; n     kk[i - 1] = j; n     abc(i - 1, sum - j); n   } n } n double A; n double ans = 0.0; n void solve(vector<int>& v) { n   double sum = 0.0; n   for (int i = 0; i < (1 << n); i++) { n     double cur = 1.0; n     int ed = 0; n     double B = 0.0; n     for (int j = 0; j < n; j++) { n       double z = (l[j] + v[j] * 10) * 0.01; n       z = min(z, 1.0); n       if (i & (1 << j)) { n         cur *= z; n         ed++; n       } else { n         cur *= 1.0 - z; n         B += b[j]; n       } n     } n     if (ed <= n / 2) cur *= A / (A + B); n     sum += cur; n   } n   ans = max(ans, sum); n } n int main() { n   cin >> n >> k >> A; n   abc(n, k); n   for (int i = 0; i < n; i++) { n     cin >> b[i] >> l[i]; n   } n   for (int i = 0; i < v.size(); i++) { n     solve(v[i]); n   } n   printf( %.9f , ans); n } n


#include <bits/stdc++.h> n template <class T> n T abs(T x) { n   return (x > 0) ? x : (-x); n } n template <class T> n T ceil(T a, T b) { n   return a / b + (a % b != 0); n } n int n, t[125][5], cnt[5], l, r, mid; n int a[6] = {1, 2, 4, 8, 16, 32}; n std::set<int> s; n int score(int x, int y) { n   int s = y / x, i; n   for (i = 0; i < 6 && s >= a[i]; ++i) n     ; n   return 500 * i; n } n bool cal(int m) { n   int x = 0, y = 0; n   for (int i = 0, j; i < 5; ++i) { n     if (t[0][i] >= 0 && t[1][i] < 0) n       x += score(cnt[i], n + m) / 250 * (250 - t[0][i]); n     else if (t[0][i] >= 0 && t[1][i] >= 0) { n       if (t[0][i] < t[1][i]) n         j = score(cnt[i], n + m); n       else n         j = score(cnt[i] + m, n + m); n       x += j / 250 * (250 - t[0][i]), y += j / 250 * (250 - t[1][i]); n     } else if (t[0][i] < 0 && t[1][i] >= 0) n       y += score(cnt[i], n + m) / 250 * (250 - t[1][i]); n   } n   return x > y; n } n int main() { n   scanf( %d n , &n); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 5; ++j) { n       scanf( %d , &t[i][j]); n       if (t[i][j] >= 0) ++cnt[j]; n     } n   } n   s.insert(0); n   s.insert(1000000007 + 1); n   for (int i = 0; i < 5; ++i) n     if (t[0][i] < 0 && t[1][i] >= 0) n       for (int j = 0; j < 6; ++j) s.insert(std::max(0, cnt[i] * a[j] - n)); n   for (std::set<int>::iterator it = s.begin(); it != s.end();) { n     for (l = *it, r = *(++it); l < r;) { n       mid = (l + r) >> 1; n       if (cal(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     if (l < *it) break; n   } n   if (l <= 1000000007) n     printf( %d n , l); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   long long n, k; n   cin >> n >> k; n   long long r = max(n - 2 * k, 0LL); n   cout << n * (n - 1) / 2 - r * (r - 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int r, b, y; n   cin >> y >> b >> r; n   int ans = min(y, b - 1); n   ans = min(ans, r - 2); n   ans = ans * 3 + 3; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, mod = 998244353, M = 3e3 + 10; n int ksm(int x, int y) { n   int ans = 1; n   while (y) { n     if (y & 1) ans = (long long)ans * x % mod; n     x = (long long)x * x % mod; n     y >>= 1; n   } n   return ans; n } n int a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) { n     scanf( %d , w + i); n     sum[a[i]] += w[i]; n     sum[2] += w[i]; n   } n   for (int i = 0; i <= 2 * m; i++) inv[i] = ksm(sum[2] + i - m, mod - 2); n   for (int i = m; i >= 0; i--) { n     f[i][m - i] = g[i][m - i] = 1; n     for (int j = m - i - 1; j >= 0; j--) { n       f[i][j] = (1LL * (sum[1] + i + 1) * f[i + 1][j] + n                  1LL * (sum[0] - j) * f[i][j + 1]) % n                 mod * inv[i - j + m] % mod; n       g[i][j] = (1LL * (sum[1] + i) * g[i + 1][j] + n                  1LL * (sum[0] - j - 1) * g[i][j + 1]) % n                 mod * inv[i - j + m] % mod; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i]) n       printf( %I64d n , 1LL * w[i] * f[0][0] % mod); n     else n       printf( %I64d n , 1LL * w[i] * g[0][0] % mod); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n; n   cin >> n; n   if (n[0] !=  - ) { n     cout << n; n   } else { n     char max =  0 ; n     int mem; n     for (int i = n.length() - 2; i < n.length(); ++i) { n       if (n[i] >= max) { n         max = n[i]; n         mem = i; n       } n     } n     string merge = n.substr(0, mem) + n.substr(mem + 1, n.length()); n     int result = atoi(merge.c_str()); n     cout << result; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long inv[100010], fac[100010], p[100010], t[100010][20]; n int n, l, r, P, tot, Phi; n int phi(int k) { n   int i, s; n   s = k; n   for (i = 2; i * i <= k; i++) { n     if (k % i == 0) s = s / i * (i - 1); n     while (k % i == 0) k /= i; n   } n   if (k > 1) s = s / k * (k - 1); n   return s; n } n long long qpow(long long a, long long b, long long mod) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return res; n } n void init() { n   long long x = P; n   Phi = phi(P); n   for (int i = 2; (long long)i * i <= x; i++) { n     if (x % i) continue; n     while (x % i == 0) x /= i; n     p[++tot] = i; n   } n   if (x > 1) p[++tot] = x; n   inv[0] = inv[1] = 1; n   fac[0] = fac[1] = 1; n   for (int i = 2; i < 100010; i++) { n     x = i; n     for (int j = 1; j <= tot; j++) { n       t[i][j] = t[i - 1][j]; n       if (x % p[j]) continue; n       while (x % p[j] == 0) x /= p[j], t[i][j]++; n     } n     fac[i] = fac[i - 1] * x % P; n     inv[i] = qpow(fac[i], Phi - 1, P); n   } n } n long long C(int n, int m) { n   long long res = 0; n   if (n < m || n < 0) return 0; n   res = fac[n] * inv[m] % P * inv[n - m] % P; n   for (int i = 1; i <= tot; i++) n     res = res * qpow(p[i], t[n][i] - t[m][i] - t[n - m][i], P) % P; n   return res; n } n int main() { n   scanf( %d%d%d%d , &n, &P, &l, &r); n   init(); n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     int m = n - i; n     ans = (ans + C(n, i) * n                      (-C(m, (m + min(r, m) >> 1) + 1) + C(m, m + l + 1 >> 1)) % n                      P) % n           P; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n int n; n long long a[maxn]; n map<long long, long long> mp; n long long mul(long long x, long long n) { n   long long ret = 1; n   while (n) { n     if (n & 1) ret *= x; n     x *= x; n     n >>= 1; n   } n   return ret; n } n int main() { n   while (~scanf( %d , &n)) { n     mp.clear(); n     for (int i = (1); i < (n + 1); i++) { n       cin >> a[i]; n       mp[a[i]]++; n     } n     long long ret = 0; n     for (int i = (1); i < (n + 1); i++) { n       for (int j = 0; j < (31); j++) { n         long long cur = mul(2, j); n         if (mp.find(cur - a[i]) != mp.end()) { n           if (mp[cur - a[i]] == 1 && cur - a[i] == a[i]) n             continue; n           else if (mp[cur - a[i]] > 1 && cur - a[i] == a[i]) n             ret += mp[cur - a[i]] - 1; n           else n             ret += mp[cur - a[i]]; n         } n       } n     } n     cout << ret / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   multiset<long long> heros; n   for (int i = 0; i < n; i++) { n     long long temp; n     cin >> temp; n     heros.insert(temp); n   } n   long long sum = accumulate(heros.begin(), heros.end(), 0LL); n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     long long def, atk; n     cin >> def >> atk; n     long long min_gold = numeric_limits<long long>::max(); n     auto it = heros.lower_bound(def); n     if (it != heros.end()) { n       min_gold = min(min_gold, max(0LL, atk - (sum - *it))); n     } n     if (it != heros.begin()) { n       it--; n       min_gold = min(min_gold, max(0LL, atk - (sum - *it)) + (def - *it)); n     } n     cout << min_gold <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int base(int a, int b) { n   int ret = 0; n   vector<int> v; n   while (a > 0) { n     v.push_back(a % b); n     a /= b; n   } n   for (int i = 0; i < v.size(); i++) { n     ret += v[i]; n   } n   return ret; n } n int main() { n   long long int a; n   cin >> a; n   long long int ans = 0; n   long long int k = 0; n   for (long long int i = 2; i < a; i++) { n     ans += base(a, i); n   } n   k = a - 2; n   for (int i = 2; i < k; i++) { n     while (ans % i == 0 && k % i == 0) { n       ans /= i; n       k /= i; n     } n   } n   cout << ans <<  /  << k; n } n


#include <bits/stdc++.h> n using namespace std; n long long lmin(long long a, long long b) { n   if (a < b) return a; n   return b; n } n long long lmax(long long a, long long b) { n   if (a > b) return a; n   return b; n } n int main() { n   long long t1, t2, x1, x2, t0; n   scanf(  %lld %lld %lld %lld %lld , &t1, &t2, &x1, &x2, &t0); n   long long mint = 100000000000LL, mint2 = 10000000000000000LL; n   double min = 10000000000000.000; n   if (t2 == t0 && t1 != t0) { n     printf( 0 %lld n , x2); n     return 0; n   } else if (t2 == t0 && t1 == t0) { n     printf( %lld %lld n , x1, x2); n     return 0; n   } n   for (long long i = 1; i <= x1; i++) { n     double temp = (double)((t0 - t1) * i); n     temp /= (double)(t2 - t0); n     long long temp2 = (long long)temp; n     double t = ((double)t1 * (double)i + (double)t2 * (double)temp2) / n                (double)(i + temp2); n     if (temp > x2) continue; n     long long temp3 = temp2 * (t2 - t0) - i * (t0 - t1); n     if (temp3 >= 0) { n       if (t <= min) { n         min = t; n         mint = temp2; n         mint2 = i; n       } n     } else { n       t = ((double)t1 * (double)i + (double)t2 * (double)(temp2 + 1)) / n           (double)(i + temp2 + 1); n       if (temp2 + 1 > x2) continue; n       if (t <= min) { n         min = t; n         mint = temp2 + 1; n         mint2 = i; n       } n     } n   } n   long long i = 1; n   for (i = 1; mint2 * i <= x1 && mint * i <= x2; i++) n     ; n   if (mint2 * i > x1 || mint * i > x2) i--; n   if (i == 0) { n     mint2 = 0; n     mint = x2; n     i = 1; n   } n   printf( %lld %lld n , mint2 * i, mint * i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z; n   cin >> x >> y >> z; n   if (abs(x - y) > z) { n     if (x > y) { n       cout <<  + n ; n     } else { n       cout <<  - n ; n     } n   } else { n     if (abs(x - y) == 0 && z == 0) { n       cout <<  0 n ; n     } else { n       cout <<  ? n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 1e4 + 5; n int N, K, i, x, y, sz[MN], mx[MN], ct, c[MN], dp[MN]; n vector<int> adj[MN], elem[2]; n vector<pair<int, int> > ans; n int dfs(int n, int p) { n   sz[n] = 1; n   mx[n] = 0; n   for (auto v : adj[n]) { n     if (v == p || c[v]) continue; n     sz[n] += dfs(v, n); n     mx[n] = max(mx[n], sz[v]); n   } n   return sz[n]; n } n void dfs2(int n, int p, int tot) { n   if (2 * mx[n] <= tot && 2 * (tot - sz[n]) <= tot) ct = n; n   for (auto v : adj[n]) { n     if (v == p || c[v]) continue; n     dfs2(v, n, tot); n   } n } n void op(int n, int p, int r) { n   if (r ^ p) ans.push_back({n, r}); n   for (auto v : adj[n]) { n     if (v == p || c[v]) continue; n     op(v, n, r); n   } n } n void op2(int n, int p, int d) { n   elem[d].push_back(n); n   for (auto v : adj[n]) { n     if (v == p || c[v]) continue; n     op2(v, n, d ^ 1); n   } n } n void solve(int n) { n   dfs2(n, 0, dfs(n, 0)); n   int cur = ct; n   c[cur] = 1; n   dfs(cur, 0); n   pair<int, int> big(-1, -1); n   for (auto v : adj[cur]) { n     if (c[v]) continue; n     if (sz[v] > big.first) big = {sz[v], v}; n   } n   for (int i = 0; i < 2; i++) elem[i].clear(); n   for (auto v : adj[cur]) { n     if (c[v]) continue; n     if (v == big.second) n       op2(v, cur, 0); n     else n       op(v, cur, cur); n   } n   int idx = 0; n   if (elem[1].size() < elem[0].size()) idx = 1; n   for (auto v : elem[idx]) n     if (v != big.second) ans.push_back({cur, v}); n   for (auto v : adj[cur]) { n     if (c[v]) continue; n     solve(v); n   } n } n int main() { n   scanf( %d%d , &N, &K); n   for (i = 1; i < N; i++) { n     scanf( %d%d , &x, &y); n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   solve(1); n   assert(10 * N >= ans.size()); n   printf( %d n , (int)ans.size()); n   for (auto v : ans) printf( %d %d n , v.first, v.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n; n   cin >> n; n   long long int ans = 0, ans1, k, a1, a2, x, y, m, s = 0; n   vector<pair<long, pair<long, long>>> r; n   for (int i = 0; i < n; ++i) { n     cin >> k; n     s += k; n     cin >> a1 >> x >> y >> m; n     ans1 = 0; n     r.push_back(make_pair(0, make_pair(a1, i + 1))); n     for (int j = 1; j < k; ++j) { n       a2 = ((a1 * x) % m + y % m) % m; n       if (a2 < a1) ans1++; n       a1 = a2; n       if (r.size() <= 200000) n         r.push_back(make_pair(ans1, (make_pair(a2, i + 1)))); n     } n     ans = max(ans, ans1); n   } n   cout << ans <<   n ; n   sort(r.begin(), r.end()); n   if (s <= 200000) n     for (int i = 0; i < s; ++i) n       cout << r[i].second.first <<     << r[i].second.second <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n typedef long long ll; n const int N=2e5+10,mod=1e9+7; n ll a[N]; n #define PII pair<int,int> n #define x first n #define y second n #define PB push_back n void solve() n { n  int n; n     cin>>n; n     int sum=0; n     map<ll,int> mp; n     for(int i=1;i<=n;i++){ n         cin>>a[i]; n     } n     bool flag=1; n     if(n==2){ n         if(a[1]==a[2]){ n             cout<< YES <<endl; n         } n         else cout<< NO <<endl; n         return; n     } n     ll ans=0; n     for(int i=1;i<=n;i++){ n         ans=(ans^a[i]); n     } n     if(ans==0){ n         cout<< YES <<endl;return; n     } n     int cnt=0; n     ll ans1=0; n  n     for(int i=1;i<=n;i++){ n         ans1=(ans1^a[i]); n         if(ans1==ans){cnt++;ans1=0;} n         if(cnt==2){ n             cout<< YES <<endl;return; n         }     n     } n     cout<< NO <<endl; n } n int main() n { n  ios::sync_with_stdio(false); n  cin.tie(0); n  cout.tie(0); n  int t; n  cin >> t; n  while (t--) n   solve(); n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a; n   scanf( %d%d , &n, &m); n   if (n == 1) n     a = 1; n   else if (m == 1) n     a = 2; n   else if (m == n) n     a = n - 1; n   else if (m - 1 < n - m) n     a = m + 1; n   else n     a = m - 1; n   printf( %d , a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n set<pair<int, int> > topo[2]; n vector<int> G[maxn], R[maxn]; n int n, m, code[maxn], in[maxn], out[maxn]; n bool seen[maxn]; n void dfs(int node) { n   seen[node] = 1; n   for (int son : G[node]) n     if (!seen[son]) dfs(son); n   topo[code[node]].insert({out[node], node}); n } n int process() { n   int answer = 0; n   while (!topo[1].empty()) { n     answer++; n     for (int i = 0; i < 2; i++) { n       while (!topo[i].empty() && topo[i].begin()->first == 0) { n         auto aux = *topo[i].begin(); n         topo[i].erase(topo[i].begin()); n         for (int son : R[aux.second]) { n           topo[code[son]].erase({out[son], son}); n           out[son]--; n           topo[code[son]].insert({out[son], son}); n         } n       } n     } n   } n   return answer; n } n int main() { n   ios::sync_with_stdio(false); n   int x, y; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> code[i]; n   for (int i = 1; i <= m; i++) { n     cin >> x >> y; n     x++; n     y++; n     G[x].push_back(y); n     R[y].push_back(x); n     in[y]++; n     out[x]++; n   } n   for (int i = 1; i <= n; i++) n     if (in[i] == 0) dfs(i); n   cout << process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int a, b, c, n; n   cin >> a >> b >> c >> n; n   if ((min(min(a, b), c) >= n) || (n - (a + b - c) < 1) || (min(a, b) < c)) n     return cout << -1, 0; n   cout << n - (a + b - c); n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> ma; n int main() { n   string s; n   int n; n   cin >> n; n   ma.clear(); n   while (n--) { n     cin >> s; n     if (ma[s] == 0) n       printf( NO n ); n     else n       printf( YES n ); n     ma[s]++; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, w, cnt1, cnt2; n   scanf( %d , &n); n   cnt1 = cnt2 = 0; n   while (n-- > 0) { n     scanf( %d , &w); n     if (w == 100) n       cnt1++; n     else n       cnt2++; n   } n   printf( %s n , cnt1 % 2 == 0 && (cnt2 % 2 == 0 || cnt1 >= 2) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20005; n const int INF = -1; n int n, m, p; n int a[N]; n int b[N]; n int F[2][N]; n int Ra[N]; n int Rb[N]; n string Path; n inline void checkmax(int &t, const int x) { n   if (x > t) t = x; n } n inline int w(int i, int j) { n   int ret = a[i] + b[j]; n   if (ret >= p) ret -= p; n   return ret; n } n long long Gao(int sx, int sy, int ex, int ey) { n   if (sx == ex && sy == ey) return w(sx, sy); n   int step = ex - sx + ey - sy; n   if (step == 1) { n     if (sx < ex) n       Path +=  C ; n     else n       Path +=  S ; n     return w(sx, sy) + w(ex, ey); n   } n   int s1 = step / 2; n   int s2 = step - s1; n   for (int j = 0; j + sy <= ey; j++) F[0][j] = F[1][j] = INF; n   for (int j = sy; j <= ey; j++) Ra[j] = Rb[j] = INF; n   F[0][0] = w(sx, sy); n   for (int i = 1; i <= s1; i++) { n     int p = i & 1; n     for (int j = max(0, i + sx - ex); j <= i && j <= ey - sy; j++) { n       F[p][j] = INF; n       int nx = i - j + sx; n       int ny = sy + j; n       if (F[p ^ 1][j] != INF) checkmax(F[p][j], F[p ^ 1][j] + w(nx, ny)); n       if (j && F[p ^ 1][j - 1] != INF) n         checkmax(F[p][j], F[p ^ 1][j - 1] + w(nx, ny)); n       if (i == s1) Ra[ny] = F[p][j]; n     } n   } n   for (int j = 0; j + sy <= ey; j++) F[0][j] = F[1][j] = INF; n   F[0][0] = w(ex, ey); n   for (int i = 1; i <= s2; i++) { n     int p = i & 1; n     for (int j = max(0, sx + i - ex); j <= i && j <= ey - sy; j++) { n       F[p][j] = INF; n       int nx = ex - (i - j); n       int ny = ey - j; n       if (F[p ^ 1][j] != INF) checkmax(F[p][j], F[p ^ 1][j] + w(nx, ny)); n       if (j && F[p ^ 1][j - 1] != INF) n         checkmax(F[p][j], F[p ^ 1][j - 1] + w(nx, ny)); n       if (i == s2) Rb[ny] = F[p][j] - w(nx, ny); n     } n   } n   int bx = -1; n   int by = -1; n   int ret = -1; n   for (int j = sy; j <= ey; j++) { n     if (Ra[j] != INF && Rb[j] != INF && Ra[j] + Rb[j] > ret) { n       bx = sx + s1 - (j - sy); n       by = j; n       ret = Ra[j] + Rb[j]; n     } n   } n   Gao(sx, sy, bx, by); n   Gao(bx, by, ex, ey); n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     a[i] %= p; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , &b[i]); n     b[i] %= p; n   } n   cout << Gao(0, 0, n - 1, m - 1) << endl; n   cout << Path << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INFint = 2e9; n const long long INF = 1e18; n const long long MOD = 1000000007ll; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<long long> a(n), b; n   for (auto &x : a) cin >> x; n   b = a; n   sort(b.begin(), b.end()); n   b.resize(unique(b.begin(), b.end()) - b.begin()); n   vector<long long> dp[2]; n   dp[0].resize(b.size(), INF); n   dp[1].resize(b.size(), INF); n   dp[0][0] = abs(a[0] - b[0]); n   for (int i = 1; i < b.size(); i++) { n     dp[0][i] = min(dp[0][i - 1], abs(b[i] - a[0])); n   } n   int e = 1; n   for (int i = 1; i < n; i++) { n     dp[e][0] = dp[e ^ 1][0] + abs(a[i] - b[0]); n     for (int j = 1; j < b.size(); j++) { n       dp[e][j] = min(dp[e][j - 1], dp[e ^ 1][j] + abs(a[i] - b[j])); n     } n     e ^= 1; n   } n   cout << dp[e ^ 1][b.size() - 1]; n   fprintf(stderr,   nTIME = %lf n , 1.0 * clock() / CLOCKS_PER_SEC); n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10, INF = 1e9; n int l[N], r[N], ps[N]; n map<int, int> mp; n int cnt, seg[4 * N], lazy[4 * N]; n inline void shift(int id) { n   seg[2 * id] += lazy[id]; n   lazy[2 * id] += lazy[id]; n   seg[2 * id + 1] += lazy[id]; n   lazy[2 * id + 1] += lazy[id]; n   lazy[id] = 0; n   return; n } n void add(int id, int L, int R, int s = 0, int e = cnt) { n   if (L <= s && e <= R) { n     seg[id]++; n     lazy[id]++; n     return; n   } n   if (L >= e || s >= R) return; n   int mid = (s + e) / 2; n   shift(id); n   add(2 * id, L, R, s, mid); n   add(2 * id + 1, L, R, mid, e); n   seg[id] = min(seg[2 * id], seg[2 * id + 1]); n   return; n } n int get(int id, int L, int R, int s = 0, int e = cnt) { n   if (L <= s && e <= R) return seg[id]; n   if (L >= e || s >= R) return INF; n   int mid = (s + e) / 2; n   shift(id); n   return min(get(2 * id, L, R, s, mid), get(2 * id + 1, L, R, mid, e)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n     r[i]++; n     mp[l[i]] = mp[r[i]] = 0; n   } n   for (auto X : mp) mp[X.first] = cnt++; n   for (int i = 0; i < n; i++) { n     l[i] = mp[l[i]]; n     r[i] = mp[r[i]]; n   } n   for (int i = 0; i < n; i++) add(1, l[i], r[i]); n   for (int i = 0; i < n; i++) { n     int mn = get(1, l[i], r[i]); n     if (mn > 1) return cout << i + 1 << endl, 0; n   } n   return cout << -1 << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1500 + 10; n const int INF = 1e9; n int ans[MAX_N][26]; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int it = 0; it < 26; ++it) { n     char c = char(it +  a ); n     for (int i = 0; i < n; ++i) { n       int cnt = 0; n       for (int j = i; j < n; ++j) { n         if (s[j] != c) cnt++; n         ans[cnt][it] = max(ans[cnt][it], j - i + 1); n       } n     } n     for (int i = 1; i <= n; ++i) { n       if (ans[i][it] == 0) { n         ans[i][it] = ans[i - 1][it]; n       } n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int k; n     char c; n     cin >> k >> c; n     cout << ans[k][(int)(c -  a )] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long w, m; n   cin >> w >> m; n   vector<int> a; n   long long z = m; n   while (z) { n     a.push_back(z % w); n     z /= w; n   } n   a.push_back(0); n   int flag = 1; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] == w || a[i] == w - 1) { n       a[i] -= w; n       a[i + 1] += 1; n     } n   } n   for (int i = 0; i < a.size(); i++) n     if (a[i] != 0 && a[i] != 1 && a[i] != -1) { n       flag = 0; n       break; n     } n   if (flag) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n int n, a[N], f[N][21], pos[N], nxt[N], tmp[N], ans; n int query(int l, int r) { n   int k = log2(r - l + 1); n   return max(f[l][k], f[r - (1 << k) + 1][k]); n } n int main() { n   scanf( %d , &n); n   memset(pos, 0x3f, sizeof(pos)); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]), f[i][0] = a[i]; n     pos[tmp[a[i]]] = i; n     tmp[a[i]] = i; n   } n   nxt[n] = n; n   for (int i = n - 1; i >= 1; --i) nxt[i] = min(nxt[i + 1], pos[i] - 1); n   for (int j = 1; j <= 20; ++j) n     for (int i = 1; i + (1 << j) - 1 <= n; ++i) n       f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); n   for (int i = 1; i <= n; ++i) { n     for (int j = i; j <= nxt[i];) { n       int A = query(i, j); n       if (A <= j - i + 1) n         ans++, j++; n       else n         j = i + A - 1; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const double PI = 3.141592653589793238463; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int q; n   cin >> q; n   while (q--) { n     long long sum = 0, r = 0; n     long long x, y; n     cin >> x; n     for (int i = 1; i <= x; i++) { n       cin >> y; n       sum += y; n       r ^= y; n     } n     cout <<  2 n  << r <<     << sum + r <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b, c, s; n map<string, long long> m[3]; n long long f, q; n bool sach(string aa) { n   if (s.size() >= aa.size() && s.substr(s.size() - aa.size(), aa.size()) == aa) n     return true; n   return false; n } n void solve(long long i) { n   if (sach(a)) { n     if (m[i].count(b) || m[i].count(c)) { n       cout <<  NO ; n       exit(0); n     } n     f = 1; n     m[i][a]++; n   } n   if (sach(b)) { n     if (m[i].count(b) || m[i].count(c)) { n       cout <<  NO ; n       exit(0); n     } n     f = 1; n     q = 1; n     m[i][b]++; n   } n   if (sach(c)) { n     if (m[i].size() && m[i].count(b) == 0) { n       cout <<  NO ; n       exit(0); n     } n     m[i][c]++; n     f = 1; n   } n   if (m[1].size() && m[2].size()) { n     cout <<  NO ; n     exit(0); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   vector<string> ss; n   while (cin >> s) { n     f = 0; n     ss.push_back(s); n     a =  lios , b =  etr , c =  initis ; n     solve(1); n     a =  liala , b =  etra , c =  inites ; n     solve(2); n     if (f == 0) { n       cout <<  NO ; n       exit(0); n     } n   } n   if (ss.size() == 1 || q) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300; n const long long mod = 1e9 + 7; n long long dp[N][N], mi[N], mi_1[N], C[N][N], n, k; n int main() { n   cin >> n >> k; n   for (int i = (0); i <= (n); i++) { n     C[i][0] = 1; n     for (int j = (1); j <= (i); j++) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n   } n   mi[0] = mi_1[0] = 1; n   for (int i = (1); i <= (n); i++) n     mi[i] = mi[i - 1] * k % mod, mi_1[i] = mi_1[i - 1] * (k - 1) % mod; n   for (int i = (1); i <= (n); i++) dp[1][i] = C[n][i] * mi_1[n - i] % mod; n   for (int i = (2); i <= (n); i++) n     for (int j = (0); j <= (n); j++) n       for (int s = (0); s <= (j); s++) { n         dp[i][j] = (dp[i][j] + dp[i - 1][s] * C[n - s][j - s] % mod * mi[s] % n                                    mod * mi_1[n - j] % mod) % n                    mod; n         if (j == s) n           dp[i][j] = (dp[i][j] - mi_1[n] * dp[i - 1][s] % mod + mod) % mod; n       } n   cout << dp[n][n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[100]; n int n; n int countNonZero = 0; n int sumElements(int size) { n   int sum = 0; n   for (int i = 0; i < size; i++) sum = sum + A[i]; n   return sum; n } n int findNonZero(int size) { n   int ind = -1; n   for (int i = 0; i < size; i++) { n     if (A[i] != 0) { n       ind = i; n       break; n     } n   } n   return ind; n } n void countZeros() { n   for (int i = 0; i < n; i++) { n     if (A[i] != 0) countNonZero++; n   } n } n int main() { n   int w; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> A[i]; n   w = sumElements(n); n   if (w != 0) { n     cout <<  YES  << endl; n     cout <<  1  << endl; n     cout <<  1   << n << endl; n     return 0; n   } else { n     int k = findNonZero(n); n     if (k == -1 || countNonZero == 1) { n       cout <<  NO  << endl; n       return 0; n     } else { n       cout <<  YES  << endl; n       cout <<  2  << endl; n       cout <<  1   << k + 1 << endl; n       cout << k + 2 <<     << n << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int n, k, a[((int)(1e5) + 5)], x, y, bol[((int)(1e6 + 5))], n     dp[((int)(1e6 + 5))], t, snr; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   cin >> x >> y; n   sort(a + 1, a + n + 1); n   n = unique(a + 1, a + n + 1) - a - 1; n   for (int i = y; i <= x; i++) bol[i - y + 1] = 2; n   for (int i = 1; i <= n; i++) { n     t = a[i]; n     if (t < y) n       if (!(y % t)) n         t = y; n       else n         t = ((y / t) + 1) * t; n     if (t > x) continue; n     for (; t <= x; t += a[i]) bol[t - y + 1] = a[i]; n   } n   for (int i = y + 1; i <= x; i++) dp[i - y + 1] = ((int)(1e9 + 5)); n   for (int i = y; i <= x - 1; i++) { n     t = -1; n     snr = min(x, i + bol[i - y + 1] - 1); n     for (int j = i + 1; j <= snr; j++) { n       if (t == -1 or j + bol[j - y + 1] >= t + bol[t - y + 1]) t = j; n       dp[j - y + 1] = min(dp[j - y + 1], dp[i - y + 1] + 1); n     } n     if (snr == x) break; n     i = t - 1; n   } n   cout << dp[x - y + 1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[31][31][51]; n int solve(int i, int j, int k) { n   if (i <= 0 || j <= 0) return 1e9; n   if (i * j == k || k <= 0) return 0; n   if (dp[i][j][k] != -1) return dp[i][j][k]; n   int op = 1e9; n   for (int x = 1; x <= i; ++x) n     for (int y = 1; y <= k; ++y) n       op = min(op, j * j + solve(i - x, j, y) + solve(x, j, k - y)); n   for (int x = 1; x <= j; ++x) n     for (int y = 1; y <= k; ++y) n       op = min(op, i * i + solve(i, j - x, y) + solve(i, x, k - y)); n   return dp[i][j][k] = op; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   memset(dp, -1, sizeof(dp)); n   int t, n, m, k; n   cin >> t; n   while (t--) { n     cin >> n >> m >> k; n     cout << solve(n, m, k) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e4 + 14; n const int INF = INT_MAX; n const int MAXC = 30; n const long long LLINF = 1e16 + 14; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int d; n     scanf( %d , &d); n     int res; n     scanf( %d , &res); n     for (int i = 1; i < n; ++i) { n       int x; n       scanf( %d , &x); n       int need = i; n       int add = min(d / need, x); n       d -= add * need; n       res += add; n     } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n vector<vector<int>> g(1000); n bool removed[1000]; n int cost[1000]; n int n, d; n void input() { n   cin >> n >> d; n   for (int i = 0; i < n; i++) { n     cin >> cost[i]; n   } n } n void solution() { n   int sum = 0; n   for (int i = 0; i < n; i++) sum += cost[i]; n   sum += 10 * (n - 1); n   if (sum > d) { n     cout << -1; n   } else { n     cout << (n - 1) * 2 + (d - sum) / 5; n   } n } n int main() { n   input(); n   solution(); n   return 0; n } n


#include <algorithm> n #include <bitset> n #include <cmath> n #include <complex> n #include <cstdio> n #include <iostream> n #include <map> n #include <numeric> n #include <queue> n #include <random> n #include <set> n #include <stack> n #include <string> n #include <sys/types.h> n #include <unistd.h> n #include <unordered_map> n #include <unordered_set> n #include <vector> n  n #pragma region macros n #define _overload(_1, _2, _3, name, ...) name n #define _rep(i, n) _range(i, 0, n) n #define _range(i, a, b) for (int i = int(a); i < int(b); ++i) n #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) n #define _rrep(i, n) _rrange(i, n, 0) n #define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i) n #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) n #pragma endregion macros n  n using namespace std; n  n template <class T> bool chmax(T &a, const T &b) { n     return (a < b) ? (a = b, 1) : 0; n } n template <class T> bool chmin(T &a, const T &b) { n     return (b < a) ? (a = b, 1) : 0; n } n  n using ll    = long long; n using R     = long double; n const R EPS = 1e-9L;  // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7 n inline int sgn(const R &r) { n     return (r > EPS) - (r < -EPS); n } n inline R sq(R x) { n     return sqrt(max(x, 0.0L)); n } n  n const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n  n const pid_t pid = getpid(); n // Problem Specific Parameter: n  n ll p[1010][1010]; n ll psum[1010]; n ll p2sum[1010]; n  n int main(void) { n     std::cin.tie(0); n     std::ios::sync_with_stdio(false); n  n     int m, k; n     cin >> m >> k; n  n     rep(i, k) { n         rep(j, m) { n             cin >> p[i][j]; n             psum[i] += p[i][j]; n             p2sum[i] += 1LL * p[i][j] * p[i][j]; n         } n     } n  n  n     const ll vsum = (psum[k - 1] - psum[0]) / (k - 1); n     ll y          = -1; n  n     rep(i, 1, k - 1) { n         const ll v = (psum[i] - psum[i - 1]); n         if (v != vsum) { n             y = i; n             break; n         } n     } n  n     ll x; n     rep(j, m) { n         const ll v      = (psum[y] - psum[y - 1]); n         const ll modify = vsum - v; n  n         p2sum[y] -= p[y][j] * p[y][j]; n         p[y][j] += modify; n         p2sum[y] += p[y][j] * p[y][j]; n  n         set<ll> s; n         rep(i, 1, k - 1) { n             const ll d2 = (p2sum[i - 1] + p2sum[i + 1]) - 2LL * p2sum[i]; n             s.insert(d2); n         } n  n         if (s.size() == 1) { n             x = p[y][j]; n             break; n         } n  n         p2sum[y] -= p[y][j] * p[y][j]; n         p[y][j] -= modify; n         p2sum[y] += p[y][j] * p[y][j]; n     } n  n     cout << y <<     << x << endl; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long poww(long long a, long long b, long long md) { n   return (!b ? 1 n              : (b & 1 ? a * poww(a * a % md, b / 2, md) % md n                       : poww(a * a % md, b / 2, md) % md)); n } n const long long MAXN = 1e6 + 10; n const long long INF = 8e18; n const long long MOD = 1e9 + 7; n pair<long long, long long> A[MAXN]; n long long n; n set<pair<long long, long long> > st; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> A[i].first; n     A[i].second = i; n     st.insert({i, A[i].first}); n   } n   sort(A, A + n); n   long long ans = 0, val = 0; n   long long rem = n; n   for (int i = 0; i < n - 2; i++) { n     auto it = st.find({A[i].second, A[i].first}); n     auto ite = st.end(); n     ite--; n     if (it == st.begin() || it == ite) { n       ans += (A[i].first - val) * (rem - 2); n       val += (A[i].first - val); n       st.erase(it); n       rem--; n     } else { n       auto nxt = it, prv = it; n       nxt++; n       prv--; n       ans += min(nxt->second, prv->second) - val; n       st.erase(it); n       rem--; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct comp { n   bool operator()(const vector<long long>& lhs, n                   const vector<long long>& rhs) const { n     return lhs[0] > rhs[0] || (lhs[0] == rhs[0] && lhs[2] > rhs[2]) || n            (lhs[0] == rhs[0] && lhs[2] == rhs[2] && lhs[1] > rhs[1]); n   } n }; n vector<long long> t; n vector<long long> ans; n priority_queue<vector<long long>, vector<vector<long long> >, comp> events; n queue<long long> q; n set<long long> inq; n priority_queue<long long, vector<long long>, greater<long long> > ready; n bool clear(long long index) { n   auto it = inq.lower_bound(index); n   if (it == inq.end()) return inq.empty(); n   return it == inq.begin(); n } n int32_t main() { n   ios::sync_with_stdio(false); n   long long n, p; n   cin >> n >> p; n   t.resize(n); n   ans.resize(n); n   for (long long i = 0; i < n; i++) cin >> t[i]; n   for (long long i = 0; i < n; i++) events.push({t[i], i, 1}); n   while (!events.empty()) { n     vector<long long> a = events.top(); n     events.pop(); n     long long status = a[2], index = a[1], time = a[0]; n     if (status == 1) { n       if (clear(index)) { n         if (q.empty()) events.push({time + p, index, 2}); n         q.push(index); n         inq.insert(index); n       } else n         ready.push(index); n     } else { n       ans[index] = time; n       q.pop(); n       inq.erase(index); n       if (!ready.empty() && clear(ready.top())) { n         long long nextIndex = ready.top(); n         ready.pop(); n         q.push(nextIndex); n         inq.insert(nextIndex); n       } n       if (!q.empty()) { n         long long nextIndex = q.front(); n         events.push({time + p, nextIndex, 2}); n       } n     } n   } n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000000], g[1000000], h[1000000]; n int d[1000000]; n int dp[1000000]; n int up[1000000]; n int h1[1000000]; n int h2[1000000]; n int t1[1000000]; n int t2[1000000]; n vector<int> v[1000000]; n int x, y, n, m; n int maxy, one_kek, two_kek; n void dfs(int x, int pr = -1) { n   h[x] = 1; n   d[x] = g[x]; n   h1[x] = 0; n   h2[x] = 0; n   t1[x] = 0; n   t2[x] = 0; n   for (int i = 0; i < v[x].size(); i++) { n     int to = v[x][i]; n     if (to == pr) continue; n     dfs(to, x); n     if (d[to] == 0) continue; n     d[x] += d[to]; n     h[x] += h[to]; n     if (1 + h1[to] > h2[x]) { n       h2[x] = 1 + h1[to]; n       t2[x] = to; n       if (h2[x] > h1[x]) { n         swap(h1[x], h2[x]); n         swap(t1[x], t2[x]); n       } n     } n   } n   if (d[x] == 0) return; n } n void dfs1(int x, int pr = -1) { n   if (d[x] == 0) return; n   if (pr == -1) n     dp[x] = h1[x]; n   else { n     if (t1[pr] == x) n       up[x] = 1 + max(up[pr], h2[pr]); n     else n       up[x] = 1 + max(up[pr], h1[pr]); n     dp[x] = max(h1[x], up[x]); n   } n   for (int i = 0; i < v[x].size(); i++) { n     int to = v[x][i]; n     if (to == pr) continue; n     dfs1(to, x); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i < n; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= m; i++) { n     cin >> a[i]; n     g[a[i]]++; n   } n   if (m == 1) { n     cout << a[1] <<   n  << 0; n     return 0; n   } n   dfs(a[1]); n   dfs1(a[1]); n   for (int i = 1; i <= m; i++) { n     if (dp[a[i]] > maxy) { n       maxy = dp[a[i]]; n       one_kek = a[i]; n     } else if (dp[a[i]] == maxy) n       one_kek = min(one_kek, a[i]); n   } n   cout << one_kek <<   n  << h[a[1]] * 2 - 2 - maxy <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1003][1003]; n int r[1003]; n int c[1003]; n char ch[5]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     r[i] = i; n   } n   for (int i = 1; i <= m; i++) { n     c[i] = i; n   } n   for (int i = 1; i <= k; i++) { n     scanf( %s , ch); n     int x, y; n     scanf( %d %d , &x, &y); n     if (ch[0] ==  r ) { n       swap(r[x], r[y]); n     } else if (ch[0] ==  c ) { n       swap(c[x], c[y]); n     } else { n       printf( %d n , a[r[x]][c[y]]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000 * 1000 * 1000 + 7; n const double PI = acos(-1.0); n long long gcd(long long a, long long b) { n   long long m; n   while (b) { n     m = a % b; n     a = b; n     b = m; n   } n   return a; n } n int solve() { n   string s; n   cin >> s; n   long long total = 0; n   int n = s.size(); n   for (int i = 0; i < n; ++i) { n     if (s[i] >=  0  && s[i] <=  9 ) { n       int j = i; n       long long u = 0; n       int dot = -1; n       while (s[j] >=  0  && s[j] <=  9  || s[j] ==  . ) { n         if (s[j] ==  . ) n           dot = j; n         else n           u = u * 10 + (s[j] -  0 ); n         ++j; n       } n       if (dot == -1 || j - dot != 3) u = u * 100; n       total += u; n       i = j - 1; n     } n   } n   s.clear(); n   if (total % 100 != 0) { n     s += total % 10 +  0 ; n     total /= 10; n     s += total % 10 +  0 ; n     total /= 10; n     s +=  . ; n   } else { n     total /= 100; n   } n   int j = 0; n   do { n     ++j; n     s += total % 10 +  0 ; n     if (j == 3) s +=  . , j = 0; n   } while (total /= 10); n   if (s.back() ==  . ) s.pop_back(); n   reverse(s.begin(), s.end()); n   cout << s << endl; n   return 0; n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   int x = 0, y = 0; n   for (int i = 0; i < s.size(); i++) s[i] ==  x  ? x++ : y++; n   if (x > y) n     for (int i = 0; i < x - y; i++) cout <<  x ; n   else n     for (int i = 0; i < y - x; i++) cout <<  y ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, req = 1; n   scanf( %d , &n); n   map<int, int> index; n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     if (x == req) { n       index[x] = i + 1 + 2000; n       req++; n     } n   } n   int cnt = 0; n   for (int i = 1; index[i] != 0; i++) { n     cnt++; n   } n   printf( %d n , cnt); n   if (cnt) printf( %d , index[1]); n   for (int i = 2; index[i] != 0; i++) { n     printf(  %d , index[i]); n   } n   if (cnt) printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int n, m; n char a[2000000], b[2000000]; n int main() { n   int i, j, k; n   scanf( %s%s , a, b); n   n = strlen(a), m = strlen(b); n   int now = 0; n   for (i = 0; i < m; i++) { n     if (b[i] == a[now]) { n       now++; n       if (now == n) break; n     } n   } n   int c1, c2; n   c1 = i; n   now = n - 1; n   for (i = m - 1; i >= 0; i--) { n     if (b[i] == a[now]) { n       now--; n       if (now == -1) break; n     } n   } n   c2 = i; n   printf( %d , max(0, c2 - c1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int u[n + 1]; n   int a[n + 1][n + 1]; n   int maxx[n + 1]; n   int dom[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> u[i]; n     for (int j = 1; j <= n; j++) { n       a[i][j] = 0; n     } n     maxx[i] = u[i]; n     dom[i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i; j++) { n       a[u[i]][j]++; n       if (maxx[j] != u[i]) { n         if (a[u[i]][j] > a[maxx[j]][j]) { n           maxx[j] = u[i]; n           dom[u[i]]++; n         } else if (a[u[i]][j] == a[maxx[j]][j]) { n           if (u[i] < maxx[j]) { n             maxx[j] = u[i]; n             dom[u[i]]++; n           } else { n             dom[maxx[j]]++; n           } n         } else if (a[u[i]][j] < a[maxx[j]][j]) { n           dom[maxx[j]]++; n         } n       } else { n         dom[u[i]]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << dom[i] <<    ; n   } 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,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n long long MOD = 998244353; n double eps = 1e-12; n void solve() { n   long long n; n   cin >> n; n   for (long long i = 0; i <= n; i++) { n     for (long long j = 0; j <= n; j++) { n       for (long long k = 0; k <= n; k++) { n         if ((i * 3 + j * 5 + k * 7) == n) { n           cout << i <<     << j <<     << k <<   n ; n           return; n         } n       } n     } n   } n   cout << -1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   for (int it = 1; it <= t; it++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     long long n, m; n     cin >> n >> m; n     long long d = n / m; n     long long w = m % 10; n     long long sm = 0; n     for (int i = 0; i < 10; i++) { n       sm += (i * w) % 10; n     } n     long long fsm = 0; n     for (int i = 1; i <= d % 10; i++) { n       fsm += (i * w) % 10; n     } n     cout << sm * (d / 10) + fsm <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5000 + 10; n int n, T; n double p[MAXN]; n int t[MAXN]; n double dp[MAXN][MAXN]; n int main() { n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= n; ++i) { n     scanf( %lf%d , &p[i], &t[i]); n     p[i] *= 0.01; n   } n   double ans = 0.0; n   dp[0][0] = 1.0; n   for (int i = 1; i <= n; ++i) { n     double sum = 0.0; n     double npn = pow(1.0 - p[i], t[i]); n     for (int j = 1; j <= T; ++j) { n       sum += dp[i - 1][j - 1]; n       if (j > t[i]) { n         sum -= dp[i - 1][j - t[i] - 1] * npn; n       } n       dp[i][j] += sum * p[i]; n       if (j >= t[i]) { n         dp[i][j] += dp[i - 1][j - t[i]] * npn; n       } n       sum *= 1.0 - p[i]; n       ans += dp[i][j]; n     } n   } n   printf( %.8f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, max = -1; n   cin >> n; n   long long int a[n], b[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (max < a[i]) { n       max = a[i]; n       b[n - i] = 0; n     } else { n       b[n - i] = max + 1 - a[i]; n     } n   } n   for (int i = n; i > 0; i--) { n     cout << b[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXBUF = 10000; n char buf[MAXBUF], *ps = buf, *pe = buf + 1; n inline bool isdigit(const char& n) { return (n >=  0  && n <=  9 ); } n inline void rnext() { n   if (++ps == pe) n     pe = (ps = buf) + n          fread(buf, sizeof(char), sizeof(buf) / sizeof(char), stdin); n } n template <class T> n inline bool in(T& ans) { n   ans = 0; n   T f = 1; n   if (ps == pe) return false; n   do { n     rnext(); n     if ( -  == *ps) f = -1; n   } while (!isdigit(*ps) && ps != pe); n   if (ps == pe) return false; n   do { n     ans = (ans << 1) + (ans << 3) + *ps - 48; n     rnext(); n   } while (isdigit(*ps) && ps != pe); n   ans *= f; n   return true; n } n const int MAXOUT = 10000; n char bufout[MAXOUT], outtmp[50], *pout = bufout, *pend = bufout + MAXOUT; n inline void write() { n   fwrite(bufout, sizeof(char), pout - bufout, stdout); n   pout = bufout; n } n inline void out_char(char c) { n   *(pout++) = c; n   if (pout == pend) write(); n } n inline void out_str(char* s) { n   while (*s) { n     *(pout++) = *(s++); n     if (pout == pend) write(); n   } n } n template <class T> n inline void out_int(T x) { n   if (!x) { n     out_char( 0 ); n     return; n   } n   if (x < 0) x = -x, out_char( - ); n   int len = 0; n   while (x) { n     outtmp[len++] = x % 10 + 48; n     x /= 10; n   } n   outtmp[len] = 0; n   for (int i = 0, j = len - 1; i < j; i++, j--) swap(outtmp[i], outtmp[j]); n   out_str(outtmp); n } n template <typename T, typename... T2> n inline int in(T& value, T2&... value2) { n   in(value); n   return in(value2...); n } n const double eps = 1e-9; n const int maxn = 5e5 + 10; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int N, M, tmp, K, Q; n struct Edge { n   int u, v, w, id; n } edge[maxn]; n struct node { n   int w, id, pointer; n   friend bool operator<(node a, node b) { return a.w > b.w; } n }; n vector<int> query[maxn]; n priority_queue<node> P; n int Back[maxn][2]; n int cnt; n int tree[maxn]; n int Size[maxn]; n int ans[maxn], ID[maxn]; n void init() { n   for (register int i = 1; i <= N; i++) tree[i] = i, Size[i] = 1; n } n int find(int p) { n   if (p == tree[p]) return p; n   return find(tree[p]); n } n bool Union(int u, int v) { n   u = find(u); n   v = find(v); n   if (u == v) return false; n   if (Size[u] > Size[v]) swap(u, v); n   tree[u] = v; n   Size[v] += Size[u]; n   Back[++cnt][0] = u; n   Back[cnt][1] = v; n   return true; n } n void Cancel() { n   while (cnt) { n     int u = Back[cnt][0]; n     int v = Back[cnt--][1]; n     tree[u] = u; n     Size[v] -= Size[u]; n   } n } n bool cmp(Edge a, Edge b) { return a.w < b.w; } n int main() { n   in(N, M); n   init(); n   for (register int i = 1; i <= M; i++) n     in(edge[i].u, edge[i].v, edge[i].w), edge[i].id = i; n   sort(edge + 1, edge + M + 1, cmp); n   for (register int i = 1; i <= M; i++) ID[edge[i].id] = i; n   in(Q); n   for (register int i = 1; i <= Q; i++) { n     in(tmp); n     while (tmp--) { n       int t; n       in(t); n       t = ID[t]; n       query[i].push_back(t); n     } n     sort(query[i].begin(), query[i].end()); n     node h; n     h.id = i; n     h.pointer = 0; n     h.w = query[i][0]; n     P.push(h); n   } n   for (int i = 1; i <= M;) { n     int W = edge[i].w; n     int j = i + 1; n     for (; j <= M && edge[j].w == edge[i].w; j++) n       ; n     j--; n     while (!P.empty()) { n       node x = P.top(); n       if (x.w > j) break; n       P.pop(); n       int v = x.id; n       while (x.pointer < query[v].size() && query[v][x.pointer] <= j) { n         if (!Union(edge[query[v][x.pointer]].u, edge[query[v][x.pointer]].v)) n           break; n         x.pointer++; n       } n       Cancel(); n       if (x.pointer == query[v].size()) { n         ans[x.id] = 1; n         continue; n       } n       x.w = query[v][x.pointer]; n       if (x.w > j) P.push(x); n     } n     for (; i <= j; i++) Union(edge[i].u, edge[i].v); n     cnt = 0; n   } n   for (register int i = 1; i <= Q; i++) { n     if (ans[i] == 1) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long lin() { n   long long x; n   scanf( %I64d , &x); n   return x; n } n vector<int> G[N + N]; n pair<int, int> edg[N + N]; n int n, k, start; n int inp[N + N]; n int st[N + N], T = 1; n long long res, f[N + N]; n void dfs(int u, int par) { n   st[u] = T++; n   if (inp[u] == 1) f[u]++; n   for (int i = 0; i < (int)G[u].size(); i++) { n     int v = G[u][i]; n     if (v != par) { n       dfs(v, u); n       f[u] += f[v]; n       res += min(f[v], 2LL * k - f[v]); n     } n   } n } n int main() { n   n = in(); n   k = in(); n   for (int i = 1; i <= k + k; i++) { n     int x = in(); n     inp[x] = 1; n   } n   for (int i = 1; i <= n - 1; i++) { n     int u = in(), v = in(); n     G[u].push_back(v); n     G[v].push_back(u); n     edg[i] = make_pair(u, v); n   } n   res = 0; n   dfs(1, -1); n   printf( %I64d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n bool test(long long, long long, long long); n int main(void) { n   long long n, m; n   cin >> n >> m; n   long long first, last; n   first = 0; n   last = sqrt(2 * n) + 1; n   if (m >= n) { n     cout << n; n     return 0; n   } n   while (test(first, n, m) == 0) { n     long long med; n     med = (first + last) / 2; n     if (test(med, n, m) == 1) n       last = med; n     else n       first = med; n     if (last - first == 1 && test(first, n, m) == 0) first = last; n   } n   cout << first + m; n   return 0; n } n bool test(long long day, long long n, long long m) { n   if (day % 2 == 0) { n     if ((day / 2) * (day + 1) >= (n - m)) return 1; n   } else { n     if (day * ((day + 1) / 2) >= (n - m)) return 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[50005]; n map<string, int> st; n int sq = 300; n vector<int> p[500100]; n vector<int> h; n string s1[500100]; n int res[1010][1010]; n void pro(int x1, int y1) { n   int x = h[x1]; n   int y = h[y1]; n   int pri = -1, prj = -1; n   int mn = 1e9; n   int r; n   for (int i = 0, j = 0; i < p[x].size() || j < p[y].size();) { n     if (i < p[x].size() && j < p[y].size()) { n       r = max(p[x][i] + s1[x].size(), p[y][j] + s1[y].size()) - n           min(p[x][i], p[y][j]); n       mn = min(mn, r); n     } n     if (i >= p[x].size()) n       prj = p[y][j], j++; n     else if (j >= p[y].size()) n       pri = p[x][i], i++; n     else if (p[x][i] < p[y][j]) n       pri = p[x][i], i++; n     else n       prj = p[x][j], j++; n   } n   res[x1][y1] = mn; n   res[y1][x1] = mn; n } n int main() { n   scanf( %s , s); n   int n = strlen(s); n   int k = 0; n   for (int i = 0; i < n; i++) { n     string t =   ; n     for (int j = 0; j < 4 && j + i < n; j++) { n       t.push_back(s[j + i]); n       if (st.count(t) == 0) { n         s1[k] = t; n         st[t] = k++; n       } n       p[st[t]].push_back(i); n     } n   } n   for (int i = 0; i < k; i++) { n     if (p[i].size() > sq) { n       h.push_back(i); n     } n   } n   for (int i = 0; i < h.size(); i++) { n     res[i][i] = s1[h[i]].size(); n     for (int j = i + 1; j < h.size(); j++) { n       pro(i, j); n     } n   } n   int q; n   cin >> q; n   char s2[10]; n   char s3[10]; n   vector<int>::iterator it, it1; n   while (q--) { n     scanf( %s , s2); n     scanf( %s , s3); n     if (st.count(s2) == 0 || st.count(s3) == 0) { n       printf( -1 n ); n       continue; n     } n     int x = st[s2]; n     int y = st[s3]; n     it = lower_bound(h.begin(), h.end(), x); n     it1 = lower_bound(h.begin(), h.end(), y); n     int mn = 1e9, r; n     if (it != h.end() && *it == x && it1 != h.end() && *it1 == y) { n       printf( %d n , res[it - h.begin()][it1 - h.begin()]); n       continue; n     } else if (it == h.end() || *it != x) { n       for (int i = 0; i < p[x].size(); i++) { n         it = lower_bound(p[y].begin(), p[y].end(), p[x][i]); n         if (it != p[y].end()) { n           r = max(p[x][i] + s1[x].size(), *it + s1[y].size()); n           r -= min(p[x][i], *it); n           mn = min(mn, r); n         } n         if (it != p[y].begin()) { n           it--; n           r = max(p[x][i] + s1[x].size(), *it + s1[y].size()); n           r -= min(p[x][i], *it); n           mn = min(mn, r); n         } n       } n     } else { n       for (int i = 0; i < p[y].size(); i++) { n         it = lower_bound(p[x].begin(), p[x].end(), p[y][i]); n         if (it != p[x].end()) { n           r = max(p[y][i] + s1[y].size(), *it + s1[x].size()); n           r -= min(p[y][i], *it); n           mn = min(mn, r); n         } n         if (it != p[x].begin()) { n           it--; n           r = max(p[y][i] + s1[y].size(), *it + s1[x].size()); n           r -= min(p[y][i], *it); n           mn = min(mn, r); n         } n       } n     } n     printf( %d n , mn); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[1000500]; n map<pair<int, long long>, bool> dp; n bool found = false; n void solve(int idx, long long sum, bool take) { n   if (found) return; n   if (take && sum % m == 0) { n     found = true; n     return; n   } n   if (idx >= n) return; n   bool &ret = dp[{idx, sum}]; n   if (ret) return; n   ret = true; n   solve(idx + 1, sum + a[idx], take + 1); n   if (found) return; n   solve(idx + 1, sum, take); n   if (found) return; n } n int main() { n   scanf( %lld%lld , &n, &m); n   bool z = false; n   for (int i = 0; i < n; ++i) scanf( %lld , &a[i]), z |= (a[i] == 0); n   solve(0, 0, 0); n   if (z || found) return puts( YES ); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; 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 = 8e3 + 5; n int nxt[MAXN], p[MAXN][MAXN], dp[MAXN]; n int N, len; n char s[MAXN], S[MAXN]; n inline int calc(int x) { n   int ret = 0; n   while (x) x /= 10, ret++; n   return ret; n } n inline void KMP(int st) { n   len = N - st + 1; n   for (int i = 1; i <= len; i++) s[i] = S[st + i - 1]; n   int j = 0; n   for (int i = 2; i <= len; i++) { n     while (j && s[i] != s[j + 1]) j = nxt[j]; n     if (s[i] == s[j + 1]) j++; n     nxt[i] = j; n   } n   for (int i = 1; i <= len; i++) { n     int tmp = i - nxt[i]; n     if (i % tmp) tmp = i; n     p[st][st + i - 1] = tmp + calc(i / tmp); n   } n } n int main() { n   scanf( %s , S + 1); n   N = strlen(S + 1); n   for (int i = 1; i <= N; i++) KMP(i); n   memset(dp, 0x3f, sizeof(dp)); n   dp[0] = 0; n   for (int i = 1; i <= N; i++) n     for (int j = 0; j < i; j++) dp[i] = min(dp[i], dp[j] + p[j + 1][i]); n   printf( %d n , dp[N]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxtt(T& t1, T t2) { n   t1 = max(t1, t2); n } n template <typename T> n void mintt(T& t1, T t2) { n   t1 = min(t1, t2); n } n bool debug = 0; n int n, m, k; n string direc =  URDL ; n const long long MOD2 = (long long)1000000007 * (long long)1000000007; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int& x, int y, int mod = 1000000007) { n   x += y; n   if (x >= mod) x -= mod; n   if (x < 0) x += mod; n   assert(x >= 0 && x < mod); n } n void et(int x = -1) { n   printf( %d n , x); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } n int p[1000035], q[1000035]; n void fmain(int tid) { n   scanf( %d%lld , &n, &lk); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     p[i] = q[i] = i; n   } n   long long mi = (long long)(n + 1) * n / 2; n   int hf = n / 2; n   long long mx = (long long)(n - hf + 1 + n) * hf; n   if (n & 1) mx += n / 2 + 1; n   if (lk < mi) et(); n   mintt(lk, mx); n   long long tar = lk - mi; n   for (int(i) = 1; (i) <= (int)(hf); (i)++) { n     int bg = n + 1 - i; n     if (bg - i <= tar) { n       swap(q[i], q[bg]); n       tar -= bg - i; n     } else { n       swap(q[i], q[i + tar]); n       break; n     } n   } n   printf( %lld n , lk); n   for (int(i) = 1; (i) <= (int)(n); (i)++) printf( %d  , p[i]); n   puts(  ); n   for (int(i) = 1; (i) <= (int)(n); (i)++) printf( %d  , q[i]); n   puts(  ); n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct abc { n   int p, q, idx; n }; n bool compareTwoStudents(abc a, abc b) { n   if (a.p != b.p) return a.p < b.p; n   return (a.q < b.q); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int i, j, k, m, n, t, u, v, w, p = 0, q, flag = 0; n   long long int x, y, z; n   string s, s1, s2; n   vector<int> abc; n   vector<int>::iterator it; n   priority_queue<int> pq; n   map<int, int> make_pair; n   cin >> t; n   while (t--) { n     std::cin >> n; n     std::cout << n <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n const int maxn = 2000 + 7; n int n; n long double p; n int t; n long double dp[maxn][maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> p >> t; n   dp[0][0] = 1.0; n   for (int i = 0; i < t; i++) { n     for (int j = 0; j < n; j++) { n       dp[i + 1][j] += (1.0 - p) * dp[i][j]; n       dp[i + 1][j + 1] += p * dp[i][j]; n     } n     dp[i + 1][n] += dp[i][n]; n   } n   long double ans = 0.0; n   for (int i = 1; i <= n; i++) { n     ans += 1.0 * i * dp[t][i]; n   } n   cout << fixed << setprecision(12) << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n #pragma GCC target( avx ) n using namespace std; n template <class t> n inline t read(t &x) { n   char c = getchar(); n   bool f = 0; n   x = 0; n   while (!isdigit(c)) f |= c ==  - , c = getchar(); n   while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   if (f) x = -x; n   return x; n } n template <class t> n inline void write(t x) { n   if (x < 0) n     putchar( - ), write(-x); n   else { n     if (x > 9) write(x / 10); n     putchar( 0  + x % 10); n   } n } n const int mod = 1e9 + 7, N = 705; n int ans, n, dig[N], f[N][N][10][2]; n char s[N]; n signed main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; i++) dig[i] = s[i] ^ 48; n   for (int i = 0; i <= 9; i++) f[0][0][i][0] = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j <= i; j++) n       for (int k = 1; k <= 9; k++) n         for (int lim = 0; lim <= 1; lim++) n           for (int x = 0; x <= (lim ? 9 : dig[i + 1]); x++) n             (f[i + 1][j + (x >= k)][k][lim | (x < dig[i + 1])] += n              f[i][j][k][lim]) %= mod; n   for (int k = 1; k <= 9; k++) n     for (int i = 1, mul = 1; i <= n; i++) n       ans = (ans + 1ll * mul * (f[n][i][k][0] + f[n][i][k][1]) % mod) % mod, n       mul = (1ll * mul * 10 + 1) % mod; n   write(ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n int a[N]; n int per[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d , &a[i]); n   } n   int ls = a[0]; n   set<int> st; n   for (int i = 1; i <= n; i++) { n     st.insert(i); n   } n   for (int i = 0; i < n - 1; i++) { n     if (per[a[i]]) { n       ls = *(--st.end()); n       st.erase(--st.end()); n       per[ls] = a[i]; n     } else { n       st.erase(a[i]); n       per[a[i]] = per[ls]; n       per[ls] = a[i]; n       if (i == 0) i--; n     } n   } n   printf( %d n , a[0]); n   for (int i = 1; i <= n; i++) { n     if (per[i] == i) continue; n     printf( %d %d n , i, per[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int a, b, c; n long long int MOD; n long long int Comb[110][110]; n long long int comb(int a, int b) { n   if (a == b) return 1; n   if (b == 0) return 1; n   if (Comb[a][b] != -1) return Comb[a][b]; n   return Comb[a][b] = (comb(a - 1, b - 1) + comb(a - 1, b)) % MOD; n } n int check[110][110]; n std::vector<long long int> ans[110][110]; n void func(int k, int h) { n   if (check[k][h]) return; n   if (k == 0) { n     ans[k][h].push_back(1 % MOD); n     check[k][h] = 1; n     return; n   } n   int start = 0; n   if (h == b) start++; n   for (int i = 0; i <= k - 1; i++) { n     func(i, h + 1); n     func(k - i - 1, h + 1); n     while (ans[k][h].size() <= n            start + ans[i][h + 1].size() + ans[k - i - 1][h + 1].size() - 2) n       ans[k][h].push_back(0); n     for (int j1 = 0; j1 < ans[i][h + 1].size(); j1++) { n       for (int j2 = 0; j2 < ans[k - i - 1][h + 1].size(); j2++) { n         long long int t = ans[i][h + 1][j1] * ans[k - i - 1][h + 1][j2]; n         t %= MOD; n         t *= comb(k - 1, i), t %= MOD; n         ans[k][h][start + j1 + j2] += t, ans[k][h][start + j1 + j2] %= MOD; n       } n     } n   } n   check[k][h] = 1; n } n int main() { n   scanf( %d%d%d%lld , &a, &b, &c, &MOD); n   for (int i = 0; i <= a; i++) n     for (int j = 0; j <= a; j++) Comb[i][j] = -1; n   func(a, 1); n   if (ans[a][1].size() >= c + 1) n     printf( %lld , ans[a][1][c] % MOD); n   else n     printf( 0 ); n } n


#include <bits/stdc++.h> n const long long INF = 2 * 1e9; n const long long MOD = 1000000007; n using namespace std; n void solve() { n   long long n, k, m; n   cin >> n >> k >> m; n   vector<long long> a(n); n   vector<pair<long long, long long> > c; n   long long x; n   cin >> x; n   c.push_back({x, 1}); n   long long cnt = 0; n   for (long long i = 1; i < n; i++) { n     long long x; n     cin >> x; n     if (c.size() == 0) { n       c.push_back({x, 1}); n       continue; n     } n     if (x == c.back().first) { n       c[(long long)(c).size() - 1].second++; n       cnt += c[(long long)(c).size() - 1].second / k * k; n       c[(long long)(c).size() - 1].second %= k; n       if (c[(long long)(c).size() - 1].second == 0) c.pop_back(); n     } else { n       c.push_back({x, 1}); n     } n   } n   if ((long long)(c).size() == 0) { n     cout << 0 <<   n ; n     return; n   } n   long long i = 0, j = (long long)(c).size() - 1; n   auto tmp = c; n   long long cnt1 = 0; n   while (1) { n     if (tmp[i].first == tmp[j].first) { n       if (i == j) break; n       cnt1 += (tmp[i].second + tmp[j].second) / k * k; n       if ((tmp[i].second + tmp[j].second) % k == 0) { n         i++; n         j--; n       } else n         break; n     } else { n       break; n     } n   } n   long long ans = 0; n   if (i == j && tmp[i].first == tmp[j].first) { n     long long cur = tmp[i].second * m; n     long long cnt2 = (cur / k) * k; n     cur %= k; n     ans += cnt * m + cnt1 * (m - 1) + cnt2; n     if (cur == 0) { n       i--; n       j++; n       while (i >= 0 && j < tmp.size()) { n         if (tmp[i].first == tmp[j].first) { n           ans += (tmp[i].second + tmp[j].second) / k * k; n           if ((tmp[i].second + tmp[j].second) % k == 0) { n             i--; n             j++; n           } else n             break; n         } else n           break; n       } n     } n   } else { n     ans += cnt * m + (cnt1) * (m - 1); n   } n   cout << n * m - ans <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.fixed; n   cout.precision(12); n   solve(); 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   long long int ans = 0; n   if (n == 1 || n == 2) { n     cout << n << endl; n     return 0; n   } n   for (long long int i = 1; i <= (sqrt(n)); i++) { n     if (n % i == 0) ans += 2; n     if (i * i == n) ans = ans - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void read(_T &f) { n   f = 0; n   _T fu = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) { n       fu = -1; n     } n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     f = (f << 3) + (f << 1) + (c & 15); n     c = getchar(); n   } n   f *= fu; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x < 10) n     putchar(x + 48); n   else n     print(x / 10), putchar(x % 10 + 48); n } n template <typename T> n void print(T x, char t) { n   print(x); n   putchar(t); n } n const int md = 998244353; n inline int add(int x, int y) { n   x += y; n   if (x >= md) x -= md; n   return x; n } n inline int sub(int x, int y) { n   x -= y; n   if (x < 0) x += md; n   return x; n } n inline int mul(int x, int y) { return 1ll * x * y % md; } n inline int fpow(int x, int y) { n   int ans = 1; n   while (y) { n     if (y & 1) ans = mul(ans, x); n     y >>= 1; n     x = mul(x, x); n   } n   return ans; n } n namespace Poly { n vector<int> rev, roots, one(1, 1); n vector<int> operator+(vector<int> a, vector<int> b) { n   int n = max((int)a.size(), (int)b.size()); n   a.resize(n); n   b.resize(n); n   for (int i = 0; i < n; i++) { n     a[i] = add(a[i], b[i]); n   } n   return a; n } n vector<int> operator-(vector<int> a, vector<int> b) { n   int n = max((int)a.size(), (int)b.size()); n   a.resize(n); n   b.resize(n); n   for (int i = 0; i < n; i++) { n     a[i] = sub(a[i], b[i]); n   } n   return a; n } n void getRevRoot(int base) { n   int n = 1 << base; n   rev.resize(n); n   roots.resize(n); n   for (int i = 1; i < n; i++) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (base - 1)); n   for (int mid = 1; mid < n; mid <<= 1) { n     int wn = fpow(3, (md - 1) / (mid << 1)); n     roots[mid] = 1; n     for (int i = 1; i < mid; i++) roots[mid + i] = mul(roots[mid + i - 1], wn); n   } n } n void ntt(vector<int> &a, int base) { n   int n = 1 << base; n   for (int i = 0; i < n; i++) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int mid = 1; mid < n; mid <<= 1) { n     for (int i = 0; i < n; i += (mid << 1)) { n       for (int j = 0; j < mid; j++) { n         int x = a[i + j], y = mul(a[i + j + mid], roots[mid + j]); n         a[i + j] = add(x, y); n         a[i + j + mid] = sub(x, y); n       } n     } n   } n } n vector<int> operator*(vector<int> a, vector<int> b) { n   int base = 0, n = (int)a.size() + (int)b.size() - 1; n   while ((1 << base) < n) ++base; n   a.resize(1 << base); n   b.resize(1 << base); n   getRevRoot(base); n   ntt(a, base); n   ntt(b, base); n   for (int i = 0; i < (1 << base); i++) a[i] = mul(a[i], b[i]); n   reverse(a.begin() + 1, a.end()); n   ntt(a, base); n   a.resize(n); n   int inv = fpow(1 << base, md - 2); n   for (int i = 0; i < n; i++) { n     a[i] = mul(a[i], inv); n   } n   return a; n } n }  // namespace Poly n using Poly::operator*; n const int N = 1e5 + 5; n vector<int> f, g, tmp; n int fac[N], inv[N]; n int n; n long long m; n int main() { n   read(n); n   read(m); n   f.resize(n + 1); n   tmp.resize(n + 1); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = mul(fac[i - 1], i); n   inv[n] = fpow(fac[n], md - 2); n   for (int i = n; i >= 1; i--) inv[i - 1] = mul(inv[i], i); n   for (int i = 0; i <= n; i++) read(f[i]), tmp[i] = inv[i]; n   for (int i = 0; i <= n; i++) f[i] = mul(f[i], fac[i]); n   reverse(f.begin(), f.end()); n   g = f * tmp; n   g.resize(n + 1); n   reverse(g.begin(), g.end()); n   for (int i = 0; i <= n; i++) n     g[i] = mul(mul(g[i], inv[i]), fpow(fpow(i + 1, m % (md - 1)), md - 2)); n   for (int i = 1; i <= n; i += 2) tmp[i] = md - tmp[i]; n   for (int i = 0; i <= n; i++) g[i] = mul(g[i], fac[i]); n   reverse(g.begin(), g.end()); n   f = g * tmp; n   f.resize(n + 1); n   reverse(f.begin(), f.end()); n   for (int i = 0; i <= n; i++) n     f[i] = mul(f[i], inv[i]), print(f[i], i == n ?   n  :    ); n   return 0; n } n