#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n using pq = priority_queue<T>; n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char* x) { cerr <<     << x <<    ; } n void __print(const string& x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V>& x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  ,  ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T& x) { n   int first = 0; n   cerr <<  { ; n   for (auto& i : x) cerr << (first++ ?  ,   :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n const int MOD = 998244353; n const char nl =   n ; n const int MX = 100001; n struct mi { n   long long v; n   explicit operator long long() const { return v; } n   mi() { v = 0; } n   mi(long long _v) { n     v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; n     if (v < 0) v += MOD; n   } n   friend bool operator==(const mi& a, const mi& b) { return a.v == b.v; } n   friend bool operator!=(const mi& a, const mi& b) { return !(a == b); } n   friend bool operator<(const mi& a, const mi& b) { return a.v < b.v; } n   mi& operator+=(const mi& m) { n     if ((v += m.v) >= MOD) v -= MOD; n     return *this; n   } n   mi& operator-=(const mi& m) { n     if ((v -= m.v) < 0) v += MOD; n     return *this; n   } n   mi& operator*=(const mi& m) { n     v = v * m.v % MOD; n     return *this; n   } n   mi& operator/=(const mi& m) { return (*this) *= inv(m); } n   friend mi pow(mi a, long long p) { n     mi ans = 1; n     assert(p >= 0); n     for (; p; p /= 2, a *= a) n       if (p & 1) ans *= a; n     return ans; n   } n   friend mi inv(const mi& a) { n     assert(a.v != 0); n     return pow(a, MOD - 2); n   } n   mi operator-() const { return mi(-v); } n   mi& operator++() { return *this += 1; } n   mi& operator--() { return *this -= 1; } n   mi operator++(int) { n     mi temp; n     temp.v = v++; n     return temp; n   } n   mi operator--(int) { n     mi temp; n     temp.v = v--; n     return temp; n   } n   friend mi operator+(mi a, const mi& b) { return a += b; } n   friend mi operator-(mi a, const mi& b) { return a -= b; } n   friend mi operator*(mi a, const mi& b) { return a *= b; } n   friend mi operator/(mi a, const mi& b) { return a /= b; } n   friend ostream& operator<<(ostream& os, const mi& m) { n     os << m.v; n     return os; n   } n   friend istream& operator>>(istream& is, mi& m) { n     long long x; n     is >> x; n     m.v = x; n     return is; n   } n }; n void __print(mi X) { cout << X.v; } n int K; n mi A, H; n mi vals[6]; n int places[6]; n map<int, map<mi, vector<int>>> get(vector<int> B) { n   int nm = (1 << (K - 1)) - 1; n   map<int, map<mi, vector<int>>> res; n   for (int m = 0; m < (1 << nm); m++) { n     int p = 0; n     int v = K; n     vector<int> cur; n     for (int i = 0; i < ((int)(B).size()); i++) cur.push_back(i); n     vector<int> place((int)(B).size()); n     mi value = 0; n     while ((int)(cur).size() > 1) { n       vector<int> nxt; n       for (int i = 0; i < ((int)(cur).size() / 2); i++) { n         if (m & (1 << p)) { n           nxt.push_back(cur[i * 2]); n           place[cur[i * 2 + 1]] = places[v]; n           value += B[cur[i * 2 + 1]] * vals[v]; n         } else { n           nxt.push_back(cur[i * 2 + 1]); n           place[cur[i * 2]] = places[v]; n           value += B[cur[i * 2]] * vals[v]; n         } n         p++; n       } n       cur = nxt; n       v--; n     } n     place[cur[0]] = 2; n     value += B[cur[0]] * vals[1]; n     res[B[cur[0]]][value] = (place); n   } n   return res; n } n void solve() { n   cin >> K >> A >> H; n   vals[0] = A; n   for (int i = 1; i < (6); i++) { n     vals[i] = pow(mi(A), (pow(mi(2), i - 1) + 1).v); n   } n   places[0] = 1; n   places[1] = 2; n   places[2] = 3; n   places[3] = 5; n   places[4] = 9; n   places[5] = 17; n   vector<int> A1, A2; n   for (int i = 0; i < (1 << (K - 1)); i++) { n     A1.push_back(i + 1); n     A2.push_back(i + 1 + (1 << (K - 1))); n   } n   map<int, map<mi, vector<int>>> res1 = get(A1), res2 = get(A2); n   for (auto& a : res1) { n     for (auto& b : res2) { n       for (auto& x : a.second) { n         mi cur1 = x.first + a.first * vals[0] - a.first * vals[1]; n         if (b.second.count(H - cur1)) { n           vector<int> ans1 = x.second, ans2 = b.second[H - cur1]; n           ans1[a.first - 1] = 1; n           for (auto& c : ans1) cout << c <<    ; n           for (auto& c : ans2) cout << c <<    ; n           return; n         } n         mi cur2 = x.first + b.first * vals[0] - b.first * vals[1]; n         if (b.second.count(H - cur2)) { n           vector<int> ans1 = x.second, ans2 = b.second[H - cur2]; n           ans2[b.first - (1 << (K - 1)) - 1] = 1; n           for (auto& c : ans1) cout << c <<    ; n           for (auto& c : ans2) cout << c <<    ; n           return; n         } n       } n     } n   } n   cout << -1 << nl; n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin.exceptions(cin.failbit); n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <iostream> n #include <vector> n  n #define int long long n  n const int DEN = 100000; n const int INF = 100000000000000000; n  n int ceild(int x) { n   if(x%DEN != 0) { n     x += DEN-x%DEN; n   } n   return x/DEN; n } n  n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n  n   int n, m; n   std::cin >> n >> m; n  n   std::vector<int> poss[n+1]; n   for(int i = 0; i <= m; i++) n     poss[0].push_back(i==0); n  n   for(int i = 1; i <= n; i++) { n     poss[i] = poss[i-1]; n     int t, x, y; n     std::cin >> t >> x >> y; n  n     if(t == 2) { n       for(int j = 0; j <= m; j++) { n         if(!poss[i-1][j]) n           continue; n         int next = j; n         int count = 0; n         while(next <= m && count <= y) { n           poss[i][next] = 1; n           next *= x; n           next = ceild(next); n           if(next <= m && poss[i-1][next]) n             break; n           count++; n         } n       } n     } n     else { n       x = ceild(x); n       std::vector<int> next(m+1, -INF); n       for(int j = 0; j <= m; j++) { n         if(j-x >= 0) n           next[j] = next[j-x]; n         if(poss[i-1][j]) n           next[j] = j; n         if(j-next[j] <= y*x) n           poss[i][j] = 1; n       } n     } n  n   } n  n   std::vector<int> answer(m+1, -1); n   for(int i = n; i > 0; i--) { n     for(int j = 1; j <= m; j++) n       if(poss[i][j]) n         answer[j] = i; n   } n  n   for(int j = 1; j <= m; j++) n     std::cout << answer[j] <<    ; n   std::cout << std::endl; n  n   return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, r; n   int y1, y2, yw, x, y; n   scanf( %d%d%d%d%d%d , &a, &b, &c, &x, &y, &r); n   y2 = b; n   if (b - a < r) { n     printf( -1 n ); n     return 0; n   } n   y1 = a + r; n   yw = c - r; n   y = 2 * yw - y; n   double k = (1.0) * (y - y1) / x; n   double ans = (yw - y1) / k; n   double dis = (y2 - y1) / sqrt(k * k + 1); n   if (dis < r) n     printf( -1 n ); n   else n     printf( %.10f n , ans); n   return 0; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n long long int n, k, m, d; n int main() { n   std::ios::sync_with_stdio(false); n   long long int best = -1; n   cin >> n >> k >> m >> d; n   int x; n   long long int pre = m; n   long long int ans = 0; n   for (int cycles = 1; cycles <= d; cycles++) { n     long long int peeps = (cycles - 1) * k + 1; n     long long int eachCandy = min(m, n / peeps); n     if (eachCandy == 0) break; n     ans = max(ans, eachCandy * cycles); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using db = double; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n const int N = 2e5 + 10; n const ll inf = 1e15 + 42; n vector<vector<int> > g(100007); n int flg, root, cnt; n vector<int> ans; n void dfs(int u, int par) { n   if (flg) return; n   int val = 0; n   for (int v : g[u]) { n     if (v != par) { n       val++; n       dfs(v, u); n     } n   } n   if (val == 2 && u == 1) { n     return; n   } n   if (val > 1 && cnt == 0) { n     cnt++; n     root = u; n   } else if (val > 1) { n     flg = 1; n   } n } n void dfs1(int u, int par) { n   int ok = 1; n   for (int v : g[u]) { n     if (v != par) { n       ok = 0; n       dfs1(v, u); n     } n   } n   if (ok) ans.push_back(u); n } n void testCase() { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, -1); n   if (flg == 1) { n     cout <<  No  n          <<   n ; n     return; n   } n   if (root == 0) root = 1; n   dfs1(root, -1); n   cout <<  Yes  n        <<   n ; n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) cout << root <<     << ans[i] <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   testCase(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int nofQs; n   cin >> nofQs; n   int minSec = INT_MAX; n   vector<int> custs(nofQs); n   for (int i = 0; i < nofQs; i++) { n     cin >> custs[i]; n   } n   for (int i = 0; i < nofQs; i++) { n     int tAmt = 0; n     for (int j = 0; j < custs[i]; j++) { n       int nofItems; n       cin >> nofItems; n       tAmt += (nofItems * 5); n       tAmt += 15; n     } n     minSec = min(tAmt, minSec); n   } n   cout << minSec; n } n


#include <bits/stdc++.h> n using namespace std; n int f[(1 << 20)], cnt[(1 << 20)], n, m; n string s; n int main() { n   cin >> n >> m >> s; n   fill(f + 1, f + (1 << m), INT_MAX); n   for (int i = 1; i < n; i++) { n     int a = s[i - 1] -  a , b = s[i] -  a ; n     if (a != b) cnt[(1 << a) | (1 << b)]++; n   } n   for (int i = 0; i < (1 << m); i++) { n     int c = m - __builtin_popcount(i); n     for (int j = 0; j < m; j++) { n       if ((i & (1 << j)) == 0) { n         int num = f[i]; n         for (int k = 0; k < m; k++) { n           int flag = 1; n           if (i & (1 << k)) flag = -1; n           num += flag * cnt[(1 << k) | (1 << j)] * c; n         } n         f[i | (1 << j)] = min(f[i | (1 << j)], num); n       } n     } n   } n   cout << f[(1 << m) - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define     F       first n #define     S       second n typedef pair<int, int>  pi; n typedef long long       ll; n const ll big = 1e6 + 9; n ll GCD(ll a,ll b) n { n     if(!b) n         return a; n     return GCD(b, a % b); n } n ll LCM(ll a,ll b) n { n     return a*b/GCD(a,b); n } n  n int main() n {ios_base::sync_with_stdio(0); n     cin.tie(0);cout.tie(0); n  n     ll t; n     cin >> t; n     while(t--) n     { n         string s, t; n         cin >> s >> t; n         ll n1 = s.size(), n2 = t.size(); n         ll lcm = LCM(n1,n2); n         ll k1 = lcm/n1, k2 = lcm/n2; n         k1--,k2--; n  n         string s1 = s, t1 = t; n         while(k1--) n             s += s1; n         while(k2--) n             t += t1; n         if(s == t) n             cout << s <<   n ; n         else n             cout <<  -1 n ; n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) { n   return (a.second < b.second); n } n long long int modpower(long long int a, long long int b, long long int c) { n   long long int res = 1; n   while (b) { n     if (b & 1LL) res = (res * a) % c; n     a = (a * a) % c; n     b >>= 1; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n using namespace std; n const int INF = 987654321; n const int MOD = 1000000007; n const int N = 1e5 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     int a[N], b[N]; n     for (int i = 1; i <= 1000; i++) { n       a[i] = 0; n     } n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       a[x]++; n     } n     int j = -1; n     for (int i = 0; i < m; i++) { n       cin >> b[i]; n       if (a[b[i]]) { n         j = b[i]; n       } n     } n     if (j > 0) n       cout <<  YES  <<   n  <<  1   << j <<   n ; n     else n       cout <<  NO  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y) { n   if (y == 0) n     return 1; n   else { n     long long result = power(x, y / 2) % 1000000007; n     if (y % 2 == 0) n       return (result % 1000000007 * result % 1000000007) % 1000000007; n     else n       return (result % 1000000007 * result % 1000000007 * x % 1000000007) % n              1000000007; n   } n } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) { n     cin >> v[i]; n   } n   long long start = -1; n   long long sum = 0; n   long long div = n / k; n   long long minsum = INT_MAX; n   for (long long i = 0; i < k; i++) { n     sum = 0; n     for (long long j = i; j < n; j = j + k) { n       sum = sum + v[j]; n     } n     if (sum < minsum) { n       minsum = sum; n       start = i + 1; n     } n   } n   cout << start; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, i, count = 0; n   cin >> n; n   double a[n], j, ans = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (i = n - 1; i >= 0; i--) { n     if (count % 2 == 0) { n       ans += 3.1415926536 * a[i] * a[i]; n       count++; n     } else { n       ans -= 3.1415926536 * a[i] * a[i]; n       count++; n     } n   } n   cout << fixed << setprecision(9) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n void read(T& x) { n   x = 0; n   int flag = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) flag = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= flag; n } n using namespace std; n const int maxn = 200010; n int n, q, f[maxn], sz[maxn], tp; n pair<int, int> sta[maxn], a[maxn]; n bool ok[maxn]; n struct info { n   int l, r, u, v; n   info() {} n   info(int a, int b, int c, int d) { l = a, r = b, u = c, v = d; } n }; n vector<info> seg; n int find(int x) { return f[x] == x ? f[x] : find(f[x]); } n bool check(int x) { return find(x) == find(x + n); } n void link(int u, int v) { n   int r1 = find(u), r2 = find(v); n   if (r1 == r2) return; n   if (sz[r1] > sz[r2]) swap(r1, r2); n   f[r1] = r2, sz[r2] += sz[r1], sta[++tp] = make_pair(r1, r2); n } n void cut(pair<int, int> t) { n   int u = t.first, v = t.second; n   if (f[v] == u) swap(u, v); n   f[u] = u; n   while (f[v] != v) sz[v] -= sz[u], v = f[v]; n   sz[v] -= sz[u]; n } n map<pair<int, int>, int> mp; n vector<pair<int, int> > edge[maxn << 2]; n void modify(int p, int l, int r, int ql, int qr, int u, int v) { n   if (ql <= l && r <= qr) { n     edge[p].push_back(make_pair(u, v)); n     return; n   } n   int mid = (l + r) >> 1; n   if (ql <= mid) modify(p << 1, l, mid, ql, qr, u, v); n   if (qr > mid) modify(p << 1 | 1, mid + 1, r, ql, qr, u, v); n } n void dfs(int p, int l, int r, bool flag) { n   int mid = (l + r) >> 1, cur = tp; n   for (int i = 0; i <= (int)edge[p].size() - 1; i++) { n     if (!flag) continue; n     int u = edge[p][i].first, v = edge[p][i].second; n     link(u, v + n), link(v, u + n); n     if (check(u) || check(v)) flag = 0; n   } n   if (l == r) { n     puts(flag ?  YES  :  NO ); n     while (tp > cur) cut(sta[tp--]); n     return; n   } n   dfs(p << 1, l, mid, flag), dfs(p << 1 | 1, mid + 1, r, flag); n   while (tp > cur) cut(sta[tp--]); n } n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= (int)2 * n; i++) f[i] = i, sz[i] = 1; n   for (int i = 1; i <= (int)q; i++) { n     scanf( %d %d , &a[i].first, &a[i].second); n     int tmp = mp[a[i]]; n     if (!tmp) n       mp[a[i]] = i; n     else { n       ok[tmp] = ok[i] = 1, mp[a[i]] = 0; n       seg.push_back(info(tmp, i - 1, a[i].first, a[i].second)); n     } n   } n   for (int i = 1; i <= (int)q; i++) n     if (!ok[i]) seg.push_back(info(mp[a[i]], q, a[i].first, a[i].second)); n   for (int i = 0; i <= (int)seg.size() - 1; i++) n     modify(1, 1, q, seg[i].l, seg[i].r, seg[i].u, seg[i].v); n   dfs(1, 1, q, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int IINF = INT_MAX; n const long long LLINF = LLONG_MAX; n inline bool LT(double a, double b) { n   return !(fabs((a) - (b)) < (1e-8)) && a < b; n } n inline bool LTE(double a, double b) { n   return (fabs((a) - (b)) < (1e-8)) || a < b; n } n void compute() {} n int main() { n   string s; n   cin >> s; n   deque<char> deq; n   int cnt = 0; n   int n = s.size(); n   for (int i = 0; i < n; ++i) { n     if (deq.empty()) { n       deq.push_back(s[i]); n     } else { n       if (deq.back() == s[i]) { n         deq.pop_back(); n         ++cnt; n       } else { n         deq.push_back(s[i]); n       } n     } n   } n   if (cnt % 2 == 0) n     puts( No ); n   else n     puts( Yes ); n   compute(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   scanf( %d , &n); n   printf( %d  , n); n   int idx = 0; n   for (int i = 2; i <= n; i++) { n     while (n % i == 0) { n       n = n / i; n       printf( %d  , n); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 500 + 5; n int board[M][M], dp[M][M]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < m; j++) { n       board[i + 1][j + 1] = s[j] -  0  + 0; n     } n   } n   for (int row = 1; row <= n; row++) { n     for (int col = 1; col <= m; col++) { n       int num = 0; n       num = num + (board[row][col] == 1); n       num = num + (col - 1 >= 1 && col - 1 <= m && board[row][col - 1] == 1); n       num = num + (col + 1 >= 1 && col + 1 <= m && board[row][col + 1] == 1); n       num = num + (row + 1 >= 1 && row + 1 <= n && board[row + 1][col] == 1); n       num = num + (row - 1 >= 1 && row - 1 <= n && board[row - 1][col] == 1); n       dp[row][col] = dp[row][col - 1] + (num == 5); n     } n   } n   long long int ans = 0; n   for (int col1 = 1; col1 <= m; col1++) { n     for (int col2 = col1 + 2; col2 <= m; col2++) { n       int p1 = 1; n       int p2 = 3; n       int numstar = dp[p2 - 1][col2 - 1] - dp[p2 - 1][col1]; n       while (p2 <= n) { n         while (p2 <= n && numstar < k) { n           p2++; n           numstar = numstar + dp[p2 - 1][col2 - 1] - dp[p2 - 1][col1]; n         } n         if (p2 <= n && numstar >= k) { n           ans = ans + n - p2 + 1; n           numstar = numstar - (dp[p1 + 1][col2 - 1] - dp[p1 + 1][col1]); n           p1++; n           while (p1 + 2 <= p2 && numstar >= k) { n             numstar = numstar - (dp[p1 + 1][col2 - 1] - dp[p1 + 1][col1]); n             p1++; n             ans = ans + n - p2 + 1; n             if (p1 + 2 > p2) { n               break; n             } n           } n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T abs(T x) { n   return x > 0 ? x : -x; n } n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n ostream &operator<<(ostream &s, const vector<T> &x) { n   s <<  [ ; n   for (auto it : x) { n     s <<  it  n       <<  ,  ; n   } n   s <<  ] ; n   return s; n } n template <typename T> n ostream &operator<<(ostream &s, const set<T> &x) { n   s <<  { ; n   for (auto it : x) { n     s <<  it  n       <<  ,  ; n   } n   s <<  } ; n   return s; n } n vector<pair<long long, long long> > a[5]; n int main() { n   int n, k, b, c; n   cin >> n >> k >> b >> c; n   if (b > c * 5) b = c * 5; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     for (int i = 0; i < 5; i++) n       a[((x + i) % 5 + 5) % 5].push_back(make_pair(x + i, i * c)); n   } n   long long ans = 1e18; n   for (int i = 0; i < 5; i++) { n     sort(a[i].begin(), a[i].end()); n     auto v = a[i]; n     bool flag = false; n     long long firstpos = -1; n     multiset<long long> setic; n     long long allsum = 0; n     for (auto f : v) { n       long long pos = f.first, w = f.second; n       if (!flag) { n         flag = true; n         firstpos = pos; n       } n       w -= (pos - firstpos) / 5 * b; n       allsum += w; n       setic.insert(w); n       if ((int)setic.size() > k) { n         auto it = setic.end(); n         it--; n         allsum -= *it; n         setic.erase(it); n       } n       if ((int)setic.size() == k) { n         ans = min(ans, allsum + (long long)k * (pos - firstpos) / 5 * b); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   int data = 0, w = 1; n   char ch = getchar(); n   while (ch !=  -  && !std::isdigit(ch)) ch = getchar(); n   if (ch ==  - ) w = -1, ch = getchar(); n   while (std::isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar(); n   return data * w; n } n const int N(1e5 + 10); n std::vector<int> G[N]; n int n, m, val[N], fa[N], size[N], heavy[N]; n int dfn[N], pos[N], bel[N], bot[N], cnt, ans; n void dfs(int x) { n   size[x] = 1; n   for (auto i : G[x]) { n     dfs(i), size[x] += size[i]; n     if (size[heavy[x]] < size[i]) heavy[x] = i; n   } n } n void dfs(int x, int c) { n   pos[dfn[x] = ++cnt] = x, bel[x] = c, bot[bel[x]] = x; n   if (heavy[x]) dfs(heavy[x], c); n   for (auto i : G[x]) n     if (i != heavy[x]) dfs(i, i); n } n struct Chain { n   int L, B, cnt; n   std::vector<int> tag, bel, v, bl, br, vis; n   std::vector<std::vector<int>> t; n   void build(int l, int r) { n     L = r - l + 1, B = std::sqrt(L), cnt = (L - 1) / B + 1; n     tag.resize(cnt + 1), bel.resize(L + 1), vis.resize(L + 1); n     bl.resize(cnt + 1), br.resize(cnt + 1), v.resize(L + 1); n     for (int i = 1; i <= L; i++) bel[i] = (i - 1) / B + 1; n     for (int i = 1; i <= L; i++) br[bel[i]] = i; n     for (int i = L; i >= 1; i--) bl[bel[i]] = i; n     t.assign(cnt + 1, std::vector<int>(B << 1 | 1)); n     for (int i = 1; i <= L; i++) v[i] = val[pos[i + l - 1]]; n     for (int i = 1; i <= L; i++) n       if ((-B <= (v[i]) && (v[i]) <= B)) ++t[bel[i]][v[i] + B]; n   } n   void rebuild(int x) { n     for (int i = bl[x]; i <= br[x]; i++) v[i] -= tag[x]; n     tag[x] = 0, t[x].assign(B << 1 | 1, 0); n     for (int i = bl[x]; i <= br[x]; i++) n       if (!vis[i]) { n         if ((-B <= (v[i]) && (v[i]) <= B)) ++t[x][v[i] + B]; n         if (v[i] <= tag[x]) ++ans; n       } n   } n   void Brute_Modify(int x, int l, int r, int d) { n     for (int i = l; i <= r; i++) n       if (!vis[i]) { n         if ((-B <= (v[i]) && (v[i]) <= B)) --t[x][v[i] + B]; n         if (v[i] <= tag[x]) --ans; n       } n     for (int i = l; i <= r; i++) v[i] += d; n     for (int i = l; i <= r; i++) n       if (!vis[i]) { n         if ((-B <= (v[i]) && (v[i]) <= B)) ++t[x][v[i] + B]; n         if (v[i] <= tag[x]) ++ans; n       } n   } n   void Modify(int l, int r, int d) { n     if (bel[l] == bel[r]) return Brute_Modify(bel[l], l, r, -d); n     for (int i = bel[l] + 1; i < bel[r]; i++) { n       tag[i] += d; n       if (!(-B <= (tag[i]) && (tag[i]) <= B)) { n         for (int j = bl[i]; j <= br[i]; j++) n           if (!vis[j] && v[j] <= tag[i] - d) --ans; n         rebuild(i); n       } else if (d == 1) n         ans += t[i][tag[i] + B]; n       else n         ans -= t[i][tag[i] + B + 1]; n     } n     Brute_Modify(bel[l], l, br[bel[l]], -d); n     Brute_Modify(bel[r], bl[bel[r]], r, -d); n   } n   void recol(int x, int d) { n     int b = bel[x]; n     vis[x] = (d == 1); n     if ((-B <= (v[x]) && (v[x]) <= B)) t[b][v[x] + B] -= d; n     if (v[x] <= tag[b]) ans -= d; n   } n } b[N]; n int main() { n   n = read(), m = read(); n   for (int i = 2; i <= n; i++) G[fa[i] = read()].push_back(i); n   for (int i = 1; i <= n; i++) val[i] = read() + 1; n   dfs(1), dfs(1, 1); n   for (int i = 1; i <= n; i++) n     if (bel[i] == i) b[i].build(dfn[i], dfn[bot[i]]); n   for (int i = 1, x, o; i <= m; printf( %d  , ans), i++) { n     x = read(), o = 1; n     if (x < 0) o = -1, x = -x; n     b[bel[x]].recol(dfn[x] - dfn[bel[x]] + 1, o); n     for (; x; b[bel[x]].Modify(1, dfn[x] - dfn[bel[x]] + 1, o), x = fa[bel[x]]) n       ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxLog = 10; n const int maxN = 1 << maxLog; n const int INF = 0x3c3c3c3c; n int f[333]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> a(n); n   f[ > ] = 1; n   f[ < ] = -1; n   for (auto &x : a) { n     cin >> x; n   } n   int v = 0; n   for (int i = 0; i < n * 2 + 2; i++) { n     int j = f[s[v]] * a[v]; n     v += j; n     if (!(0 <= v && v < n)) { n       cout <<  FINITE  << endl; n       return 0; n     } n   } n   cout <<  INFINITE  << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, q; n   cin >> n >> k >> q; n   vector<int> A(n); n   set<int> S; n   for (int i = 0; i < n; ++i) cin >> A[i]; n   int ty, id; n   for (int i = 0; i < q; ++i) { n     cin >> ty >> id; n     if (ty == 1) { n       S.insert(A[id - 1]); n       if (S.size() > k) { n         S.erase(S.begin()); n       } n     } else { n       if (S.find(A[id - 1]) != S.end()) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using uint = unsigned int; n using pii = pair<int, int>; n using vi = vector<int>; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if (a % b == 0) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, odd = 0, even = 0; n   cin >> n; n   while (n--) { n     cin >> a; n     if (a % 2 == 0) { n       even += 1; n     } else { n       odd += 1; n     } n   } n   cout << (odd % 2 == 1 ? odd : even) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n, m, a[1001], b[1001], litri = 0, ok = 1; n   cin >> n >> m; n   double copie = m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   litri = m / (b[1] - 1); n   for (int i = n; i >= 1; i--) { n     m = m + litri; n     litri = m / (a[i] - 1); n     m = m + litri; n     litri = m / (b[i] - 1); n     if (a[i] == 1 || b[i] == 1) ok = 0; n   } n   double diferenta; n   diferenta = m - copie; n   if (ok == 0) n     cout << -1; n   else n     cout << fixed << setprecision(10) << diferenta; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( test.in ); n double i, j, sens, k, o, ta, tb, pasi, p, d, pl, haur, n; n int a, b, t, f, c; n int main() { n   cin >> a >> b >> t >> f >> c; n   k = 1000; n   ta = a / k; n   tb = b / k; n   p = a * t; n   sens = 1; n   while (p < c) { n     n = 0; n     while (p + a > d + b && p < c) { n       p += a; n       d += b; n       n += a; n     } n     while (p > d && p < c) { n       p += ta; n       d += tb; n       n += ta; n     } n     if (p < c && p != c) ++haur; n     d = 0; n     p += n + a * f; n   } n   cout << haur; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   char opr[40]; n   while ((~scanf( %s , opr)) && opr[0] ==  s ) { n     int x = 0, y = 1; n     while (1) { n       printf( ? %d %d n , x, y); n       fflush(stdout); n       char ans[20]; n       scanf( %s , ans); n       if (ans[0] ==  x ) break; n       x = y, y = y * 2 + 1; n     } n     int mid; n     int l = x, r = y; n     while (l + 1 < r) { n       mid = (l + r) >> 1; n       printf( ? %d %d n , l, mid); n       fflush(stdout); n       char ans[20]; n       scanf( %s , ans); n       if (ans[0] ==  y ) n         l = mid; n       else n         r = mid; n     } n     if (l == 0) { n       printf( ! 1 n ); n       fflush(stdout); n       continue; n     } n     printf( ? %d %d n , r, r + 1); n     fflush(stdout); n     char ans[20]; n     scanf( %s , ans); n     if (ans[0] ==  x ) r--; n     printf( ! %d n , r); n     fflush(stdout); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pil = pair<int, ll>; n using pli = pair<ll, int>; n using vs = vector<string>; n using vvs = vector<vs>; n using vvvs = vector<vvs>; n using vb = vector<bool>; n using vvb = vector<vb>; n using vvvb = vector<vvb>; n using vi = vector<int>; n using vvi = vector<vi>; n using vvvi = vector<vvi>; n using vl = vector<ll>; n using vvl = vector<vl>; n using vvvl = vector<vvl>; n using vd = vector<double>; n using vvd = vector<vd>; n using vvvd = vector<vvd>; n using vpii = vector<pii>; n using vvpii = vector<vpii>; n using vvvpii = vector<vvpii>; n template <class T> n bool amax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool amin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n ll ri() { n   ll l; n   cin >> l; n   return l; n } n string rs() { n   string s; n   cin >> s; n   return s; n } n template <class T> n T read() { n   T t; n   cin >> t; n   return t; n } n template <class T, class U> n ostream &operator<<(ostream &o, const pair<T, U> &p) { n   return o <<  (  << p.first <<  ,   << p.second <<  ) ; n } n ostream &operator<<(ostream &o, const vb &t) { n   for (auto &&e : t) o <<  #. [e]; n   return o; n } n template <class T> n ostream &operator<<(ostream &o, const vector<T> &t) { n   o <<  { ; n   for (auto &&e : t) o << e <<  , ; n   o <<  }  << endl; n   return o; n } n void panic() { n   cout <<  NO  << endl; n   exit(0); n } n string solve() { n   int n = ri(); n   int k = ri(); n   auto s = rs(); n   int w = 0, l = 0, d = 0, u = 0; n   for (auto &&c : s) { n     if (c ==  W ) n       w++; n     else if (c ==  L ) n       l++; n     else if (c ==  D ) n       d++; n     else if (c ==  ? ) n       u++; n   } n   vvi dp(n + 1, vi(2 * k + 1)); n   vvi fp(n + 1, vi(2 * k + 1)); n   if (s[n - 1] ==  D ) { n     panic(); n   } n   if (s[n - 1] ==  W  || s[n - 1] ==  ? ) { n     dp[n][k + k] = 1; n   } n   if (s[n - 1] ==  L  || s[n - 1] ==  ? ) { n     dp[n][0] = 1; n   } n   for (int i = int(n) - 1; i >= int(0); i--) n     for (int j = int(1); j < int(2 * k); ++j) { n       char r = s[i]; n       if (r ==  W  || r ==  ? ) { n         if (amax(dp[i][j], dp[i + 1][j + 1])) { n           fp[i][j] = j + 1; n         } n       } n       if (r ==  L  || r ==  ? ) { n         if (amax(dp[i][j], dp[i + 1][j - 1])) { n           fp[i][j] = j - 1; n         } n       } n       if (r ==  D  || r ==  ? ) { n         if (amax(dp[i][j], dp[i + 1][j])) { n           fp[i][j] = j; n         } n       } n     }; n   ; n   if (dp[0][k] == 0) panic(); n   vi A; n   { n     int x = k; n     for (int i = int(0); i < int(n + 1); ++i) { n       A.emplace_back(x); n       x = fp[i][x]; n     } n   }; n   string ans; n   for (int i = int(0); i < int(n); ++i) { n     if (A[i] == A[i + 1]) n       ans +=  D ; n     else if (A[i] > A[i + 1]) n       ans +=  L ; n     else if (A[i] < A[i + 1]) n       ans +=  W ; n   }; n   ; n   for (int i = int(0); i < int(n); ++i) { n     assert(s[i] ==  ?  || ans[i] == s[i]); n   } n   return ans; n } n void Main() { cout << solve() << endl; } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   Main(); n   return 0; n } n


#include <bits/stdc++.h> n char s[128]; n bool ce(char r, char a, char b, char c) { return r == a || r == b || r == c; } n bool spcmp(char a, char b, char c) { n   return ce( A , a, b, c) && ce( B , a, b, c) && ce( C , a, b, c); n } n int main() { n   int n, i; n   scanf( %s , s); n   for (i = 1; s[i + 1] !=   0 ; i++) { n     if (spcmp(s[i - 1], s[i], s[i + 1])) { n       printf( Yes n ); n       return 0; n     } n   } n   printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k, x, y, res = 0; n     cin >> n >> k; n     vector<vector<int>> gr(n + 1); n     vector<int> deg(n + 1, 0), ch(n + 1, 0), nxt; n     for (int i = 0; i < n - 1; i++) { n       cin >> x >> y; n       gr[x].push_back(y); n       gr[y].push_back(x); n       deg[x]++; n       deg[y]++; n     } n     for (int i = 1; i <= n; i++) { n       if (deg[i] == 1) { n         int par = gr[i].front(); n         ch[par]++; n         if (ch[par] >= k) { n           nxt.push_back(par); n         } n       } n     } n     while (!nxt.empty()) { n       int a = nxt.back(); n       nxt.pop_back(); n       if (ch[a] < k) { n         continue; n       } n       vector<int> del; n       for (int i = 0; i < gr[a].size(); i++) { n         if (deg[gr[a][i]] == 1) { n           del.push_back(i); n         } n       } n       res += del.size() / k; n       while (del.size() % k != 0) { n         del.pop_back(); n       } n       ch[a] -= del.size(); n       for (int i = del.size() - 1; i >= 0; i--) { n         gr[a].erase(gr[a].begin() + del[i]); n       } n       deg[a] -= del.size(); n       if (deg[a] == 1) { n         int par = gr[a].front(); n         ch[par]++; n         if (ch[par] >= k) { n           nxt.push_back(par); n         } n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = (int)1e9; n const double PI = 2 * acos(0.0); n const double eps = 1e-9; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int foo = max(3 * a / 10, a - a / 250 * c); n   int bar = max(3 * b / 10, b - b / 250 * d); n   if (foo == bar) n     cout <<  Tie  << endl; n   else if (foo > bar) n     cout <<  Misha  << endl; n   else n     cout <<  Vasya  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n + 1]; n   for (int i = 1; i < n + 1; i++) { n     cin >> a[i]; n   } n   int s = 0; n   int arr[n + 1], max; n   max = INT_MIN; n   memset(arr, 0, sizeof(arr)); n   for (int i = 1; i < n + 1; i++) { n     if (a[i] == 1) { n       arr[i] = arr[i - 1] + 1; n     } else { n       arr[i] = 0; n     } n     if (arr[i] > max) { n       max = arr[i]; n     } n   } n   if (arr[n] != 0) { n     int j = 1; n     while (arr[j] != 0) { n       arr[n]++; n       j++; n     } n     if (arr[n] > max) { n       max = arr[n]; n     } n   } n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double inf = 1e7; n const double eps = 1e-7; n int main() { n   int n, width, height, begin0, begin1; n   cin >> n >> width >> height >> begin0 >> begin1; n   vector<pair<int, pair<double, double>>> a(n + 1); n   a[0] = make_pair(0, make_pair(1, begin1 - begin0)); n   for (int i = 1; i <= n; ++i) { n     int x, y; n     cin >> a[i].first >> x >> y; n     a[i].second = make_pair(1 - (double)height / y, (double)x * height / y); n   } n   auto check = [&](double v) { n     double l = begin0, r = begin0; n     for (int i = 1; i <= n; ++i) { n       double delta = v * (a[i].first - a[i - 1].first); n       double k0 = a[i - 1].second.first, b0 = a[i - 1].second.second; n       double k1 = a[i].second.first, b1 = a[i].second.second; n       double k = k0 / k1, tl, tr; n       if (k1 > 0) { n         tl = (b0 - delta - b1) / k1; n         tr = (b0 + delta - b1) / k1; n       } else { n         tl = (b0 + delta - b1) / k1; n         tr = (b0 - delta - b1) / k1; n       } n       if (fabs(k0 - k1) < eps) { n         tl = max(tl, -delta); n         tr = min(tr, delta); n         if (tl > tr + eps) { n           return false; n         } n         l += tl; n         r += tr; n       } else { n         double ml = (tl + delta) / (1 - k), mr = (tr - delta) / (1 - k); n         double nl = inf, nr = -inf; n         if (k > 1) { n           { n             double left = max(l, mr), right = min(r, ml); n             if (right >= left - eps) { n               nl = min(nl, left - delta); n               nr = max(nr, right + delta); n             } n           } n           { n             double left = max(l, max(ml, mr)), right = r; n             if (right >= left - eps) { n               right = min(right, (delta - tl) / (k - 1)); n               if (right >= left - eps) { n                 nl = min(nl, k * left + tl); n                 nr = max(nr, right + delta); n               } n             } n           } n           { n             double left = l, right = min(r, min(ml, mr)); n             if (right >= left - eps) { n               left = max(left, -(delta + tr) / (k - 1)); n               if (right >= left - eps) { n                 nl = min(nl, left - delta); n                 nr = max(nr, k * right + tr); n               } n             } n           } n           { n             double left = max(l, ml), right = min(r, mr); n             if (right >= left - eps) { n               nl = min(nl, k * left + tl); n               nr = max(nr, k * right + tr); n             } n           } n         } else { n           { n             double left = max(l, ml), right = min(r, mr); n             if (right >= left - eps) { n               if (k > 0) { n                 nl = min(nl, k * left + tl); n                 nr = max(nr, k * right + tr); n               } else { n                 nl = min(nl, k * right + tl); n                 nr = max(nr, k * left + tr); n               } n             } n           } n           { n             double left = l, right = min(r, min(ml, mr)); n             if (right >= left - eps) { n               left = max(left, (tl - delta) / (1 - k)); n               if (right >= left - eps) { n                 if (k > 0) { n                   nl = min(nl, k * left + tl); n                 } else { n                   nl = min(nl, k * right + tl); n                 } n                 nr = max(nr, right + delta); n               } n             } n           } n           { n             double left = max(l, max(ml, mr)), right = r; n             if (right >= left - eps) { n               right = min(right, (tr + delta) / (1 - k)); n               if (right >= left - eps) { n                 nl = min(nl, left - delta); n                 if (k > 0) { n                   nr = max(nr, k * right + tr); n                 } else { n                   nr = max(nr, k * left + tr); n                 } n               } n             } n           } n           { n             double left = max(l, mr), right = min(r, ml); n             if (right >= left - eps) { n               nl = min(nl, left - delta); n               nr = max(nr, right + delta); n             } n           } n         } n         if (nl > nr + eps) { n           return false; n         } n         l = nl; n         r = nr; n       } n       l = max(l, (double)0); n       r = min(r, (double)width); n       if (k1 > 0) { n         l = max(l, -b1 / k1); n         r = min(r, (width - b1) / k1); n       } else { n         l = max(l, (width - b1) / k1); n         r = min(r, -b1 / k1); n       } n       if (l > r + eps) { n         return false; n       } n     } n     return true; n   }; n   if (!check(width + 1)) { n     cout << -1 << endl; n   } else { n     double l = 0, r = width; n     for (int t = 0; t < 50; ++t) { n       double m = (l + r) / 2; n       if (check(m)) { n         r = m; n       } else { n         l = m; n       } n     } n     cout << fixed << setprecision(6) << r << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[10], n, res; n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   p[1] = 2; n   p[2] = 3; n   p[3] = 5; n   p[4] = 7; n   res = 0; n   for (int a = 1; a <= 4; a++) res += n / p[a]; n   for (int a = 1; a <= 4; a++) n     for (int b = a + 1; b <= 4; b++) res -= n / (p[a] * p[b]); n   for (int a = 1; a <= 4; a++) n     for (int b = a + 1; b <= 4; b++) n       for (int c = b + 1; c <= 4; c++) res += n / (p[a] * p[b] * p[c]); n   for (int a = 1; a <= 4; a++) n     for (int b = a + 1; b <= 4; b++) n       for (int c = b + 1; c <= 4; c++) n         for (int d = c + 1; d <= 4; d++) res -= n / (p[a] * p[b] * p[c] * p[d]); n   res = n - res; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, x, a, b; n     cin >> n >> x >> a >> b; n     int ans = abs(b - a) + x; n     ans = min(ans, n - 1); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool isPal(string s, long long l, long long r) { n   while (l < r) { n     if (s[l] != s[r]) return false; n     l++; n     r--; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   string s; n   cin >> s; n   long long ans = 0; n   for (long long i = 0; i < s.size(); i++) { n     for (long long j = i; j < s.size(); j++) { n       if (!isPal(s, i, j)) { n         if (ans < j - i + 1) ans = j - i + 1; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void test() { cerr <<   n ; } n template <typename T, typename... Args> n void test(T x, Args... args) { n   cerr << x <<    ; n   test(args...); n } n const int MAXN = (int)1e6 + 5; n const int MOD = (int)1e9 + 7; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long sum = 0, ma = a[0], mi = 0; n   for (int i = 0; i < n; i++) { n     sum += a[i]; n     if (i == 0) { n       mi = sum; n     } else { n       ma = max(ma, sum - mi); n       mi = min(mi, sum); n     } n     if (i != n - 1) ma = max(ma, sum); n   } n   cout << (sum > ma ?  YES  :  NO ) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MSIZE = 1e3 + 5; n const int MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     int cnt[26] = {0}; n     for (char c : s) cnt[c -  a ]++; n     int os = 0; n     for (int i = 0; i <= 25; i++) { n       if (cnt[i] >= k - os) { n         cnt[i] -= (k - os); n         cout << (char)( a  + i); n         break; n       } n       os += cnt[i]; n       cnt[i] = 0; n     } n     if (os) { n       cout << endl; n       continue; n     } n     bool pr = 0; n     for (int i = 0; i <= 25; i++) { n       if (cnt[i] and cnt[i] == n - k) { n         int val = cnt[i] / k + (cnt[i] % k != 0); n         while (val--) cout << (char)( a  + i); n         break; n       } n       while (cnt[i]--) cout << (char)( a  + i); n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n const int INF = (int)1e9; n using namespace std; n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n template <class T> n T sqr(T x) { n   return x * x; n } n int n; n int x[1000], y[1000]; n bool used[1000]; n void dfs(int v) { n   used[v] = true; n   for (int i = 0; i < n; ++i) { n     if ((!used[i]) && (x[v] == x[i] || y[v] == y[i])) { n       dfs(i); n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> x[i] >> y[i]; n   memset(used, false, sizeof(used)); n   int ans = -1; n   for (int j = 0; j < n; ++j) { n     if (!used[j]) { n       ans++; n       dfs(j); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int v, c; n   node *opp, *next; n } Map[500]; n node *List[110]; n int d[110], vd[110]; n int e, n; n int a[110], p1[110], p2[110], N, M; n bool b[100000 + 10]; n int prime[100000 + 10], Nprime = 0; n void addedge(int i, int j, int c) { n   Map[++e].v = j; n   Map[e].c = c; n   Map[e].opp = &Map[e + 1]; n   Map[e].next = List[i]; n   List[i] = &Map[e]; n   Map[++e].v = i; n   Map[e].c = 0; n   Map[e].opp = &Map[e - 1]; n   Map[e].next = List[j]; n   List[j] = &Map[e]; n } n int dfs(int i, int flow) { n   int j, temp, ff = 0; n   node *u; n   if (i == n) return (flow); n   u = List[i]; n   while (u != NULL) { n     j = u->v; n     if (u->c > 0 && d[i] == d[j] + 1) { n       temp = dfs(j, min(u->c, flow - ff)); n       u->c = u->c - temp; n       u->opp->c = u->opp->c + temp; n       ff += temp; n       if (ff == flow) return (flow); n     } n     u = u->next; n   } n   if (d[1] >= n) return (ff); n   vd[d[i]]--; n   if (vd[d[i]] == 0) d[1] = n; n   d[i]++; n   vd[d[i]]++; n   return (ff); n } n void getprime() { n   for (int i = 2; i <= 100000; i++) { n     if (!b[i]) { n       prime[++Nprime] = i; n       for (int j = i + i; j <= 100000; j += i) b[j] = true; n     } n   } n } n int solve(int k) { n   e = 0; n   for (int i = 0; i < 110; i++) List[i] = NULL; n   for (int i = (1); i <= (M); ++i) { n     int t1 = a[p1[i]], t2 = a[p2[i]]; n     int count = 0; n     while (t1 % k == 0 && t2 % k == 0) { n       t1 /= k; n       t2 /= k; n       count++; n     } n     if (p1[i] % 2 == 1) n       addedge(p1[i] + 1, p2[i] + 1, count); n     else n       addedge(p2[i] + 1, p1[i] + 1, count); n   } n   for (int i = (1); i <= (N); ++i) { n     int count = 0; n     while (a[i] % k == 0) { n       a[i] /= k; n       count++; n     } n     if (i % 2 == 1) n       addedge(1, i + 1, count); n     else n       addedge(i + 1, N + 2, count); n   } n   n = N + 2; n   memset(d, 0, sizeof(d)); n   memset(vd, 0, sizeof(vd)); n   vd[0] = n; n   int maxflow = 0; n   while (d[1] <= n - 1) maxflow = maxflow + dfs(1, 100000000); n   return maxflow; n } n int main() { n   getprime(); n   scanf( %d%d , &N, &M); n   for (int i = (1); i <= (N); ++i) scanf( %d , a + i); n   for (int i = (1); i <= (M); ++i) scanf( %d%d , p1 + i, p2 + i); n   int ans = 0; n   for (int i = (1); i <= (Nprime); ++i) ans += solve(prime[i]); n   for (int i = (1); i <= (N); ++i) n     if (a[i] > 1) ans += solve(a[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nC2(int n) { n   if (n == 0) return 0; n   return ((long long)n * (n - 1)) / 2; n } n void solve() { n   int n, m; n   scanf( %d %d , &n, &m); n   long long Kmin, Kmax; n   int r = n % m; n   Kmin = nC2((n + m - 1) / m) * r + nC2(n / m) * (m - r); n   Kmax = nC2(n - m + 1); n   printf( %lld %lld n , Kmin, Kmax); n } n int main() { n   int tt; n   tt = 1; n   for (int i = 0; i < tt; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[5001][5001]; n int p[5001]; n bool u[5001]; n int i, j, n, k; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , &a[i]); n     for (j = 0; j < n; j++) a[i][j] -=  0 ; n   } n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) p[i] += a[i][j]; n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) n       if (p[j] == n - i - 1) { n         u[j] = 1; n         break; n       } n   for (i = 0; i < n; i++) n     if (!u[i]) break; n   if (i == n) { n     printf( -1 n ); n     return 0; n   } n   for (j = 0; j < n; j++) n     for (k = 0; k < n; k++) n       if (a[i][j] && a[j][k] && a[k][i]) { n         printf( %d %d %d n , i + 1, j + 1, k + 1); n         return 0; n       } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9; n const int mx = 1e3 + 10; n const int mxd = (1 << 13) + 10; n long long int a[mx], b[mx]; n vector<long long int> divs; n pair<int, long long int> f[mx][mxd]; n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int n; n   long long int k; n   scanf( %d %lld , &n, &k); n   long long int g = k; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , a + i); n     b[i] = gcd(k, a[i]); n     g /= gcd(g, a[i]); n   } n   if (g != 1) { n     puts( -1 ); n     return 0; n   } n   if (k == 1) { n     puts( 1 ); n     int idx = min_element(a + 1, a + 1 + n) - a; n     printf( %d n , idx); n     return 0; n   } n   for (long long int d = 1; d * d <= k; d++) { n     if (k % d == 0) { n       divs.push_back(d); n       if (d != k / d) divs.push_back(k / d); n     } n   } n   sort(divs.begin(), divs.end()); n   divs.resize(unique(divs.begin(), divs.end()) - divs.begin()); n   int nd = divs.size(); n   unordered_map<long long int, int> mp; n   for (int i = 0; i < int(divs.size()); i++) mp[divs[i]] = i; n   for (int d = 1; d < nd; d++) f[0][d] = pair<int, long long int>(oo, 0ll); n   for (int i = 1; i <= n; i++) { n     for (int d = 1; d < nd; d++) { n       f[i][d] = f[i - 1][d]; n       long long int x = divs[d] / gcd(divs[d], b[i]); n       int idx = mp[x]; n       pair<int, long long int> bet(f[i - 1][idx].first + 1, n                                    a[i] + f[i - 1][idx].second); n       f[i][d] = min(f[i][d], bet); n     } n   } n   int pos = n, x = nd - 1; n   printf( %d n , f[n][nd - 1].first); n   int c = 0; n   while (c < f[n][nd - 1].first) { n     if (f[pos][x] == f[pos - 1][x]) n       pos--; n     else { n       printf( %d  , pos); n       long long int nx = divs[x] / gcd(divs[x], b[pos]); n       x = mp[nx]; n       pos--; n       c++; n     } n   } n   puts(  ); 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   long long q; n   long long m; n   cin >> q >> m; n   long long j = 0; n   vector<pair<long long, long long>> spells; n   spells.push_back({0, 0}); n   for (int i = 0; i < q; i++) { n     long long k, a, b; n     cin >> k >> a >> b; n     if (k == 1) { n       long long x = 1 + (a + j) % 1000000; n       long long y = 1 + (b + j) % 1000000; n       long long l = -1; n       long long r = spells.size(); n       while (l + 1 != r) { n         long long mid = (r + l) / 2; n         if (spells[mid].first < x) n           l = mid; n         else n           r = mid; n       } n       if (r == spells.size() || n           (spells[r].second - spells[r - 1].second) * n                   (x - spells[r - 1].first) > n               (y - spells[r - 1].second) * n                   (spells[r].first - spells[r - 1].first)) { n         spells.insert(spells.begin() + r, {x, y}); n         long long shift = 0; n         while (r - shift > 1) { n           if ((spells[r].second - spells[r - shift - 2].second) * n                   (spells[r - shift - 1].first - spells[r - shift - 2].first) > n               (spells[r - shift - 1].second - spells[r - shift - 2].second) * n                   (spells[r].first - spells[r - shift - 2].first)) n             break; n           shift++; n         } n         if (shift > 0) n           spells.erase(spells.begin() + r - shift, spells.begin() + r); n       } n     } else { n       long long t = 1 + (a + j) % 1000000; n       long long h = 1 + (b + j) % 1000000; n       long long l = -1; n       long long r = spells.size(); n       if (h > spells[r - 1].first * t) { n         cout <<  NO n ; n         continue; n       } n       while (l + 1 != r) { n         long long mid = (r + l) / 2; n         if (spells[mid].first * t < h) n           l = mid; n         else n           r = mid; n       } n       if (spells[r - 1].second * (t * spells[r].first - h) + n               spells[r].second * (h - t * spells[r - 1].first) <= n           m * (spells[r].first - spells[r - 1].first)) { n         cout <<  YES n ; n         j = i + 1; n       } else n         cout <<  NO n ; n     } n   } n } n


#include <stdio.h> n #include <iostream> n #include <vector> n #include <unordered_set> n #include <math.h> n #include <time.h> n #include <algorithm> n #include <string.h> n #include <string> n #include <deque> n #include <ctype.h> n #include <limits.h> n #include <map> n #include <set> n #include <unordered_map> n #include <stack> n #include <queue> n #include <functional> n #include <limits.h> n #include <random> n  n using namespace std; n typedef long long ll; n  n void solve(); n  n mt19937 rng(time(0)); n  n ll qpow(ll x, ll p, ll MOD) { n     ll res = 1, v = x; n     while (p) { n         if(p & 1) { n             res *= v; n             res %= MOD; n         } n         v *= v; n         v %= MOD; n         p >>= 1; n     } n     return res; n } n  n ll inv(ll x, ll MOD) { n     return qpow(x, MOD - 2, MOD); n } n  n void init() { n #ifdef _LOCAL_  n     if(!freopen( case.txt ,  r , stdin)) { n         perror( open file failed!: ); n         exit(1); n     } n     if(!freopen( main.ans ,  w , stdout)) { n         perror( open file failed!: ); n         exit(1); n     } n #endif n } n   n #ifdef _LOCAL_ n void dbg_out() { cerr << endl; } n template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr <<     << H; dbg_out(T...); } n #   define dbg(...) cerr <<  (  << #__VA_ARGS__ <<  ): , dbg_out(__VA_ARGS__) n #else n #   define dbg(...) n #endif n  n ll read() { n     int ch = getchar(); n     if(ch == EOF) { n         return INT_MIN; n     } n     while(!isdigit(ch) && ch !=  - ) { n         ch = getchar(); n         if(ch == EOF) { n             return INT_MIN; n         } n     } n     ll res = 0, sign = 1; n     if(ch ==  - ) { n         ch = getchar(); n         sign = -1; n     } n     while(isdigit(ch)) { n         res = res * 10 + ch -  0 ; n         ch = getchar(); n     } n     return res * sign; n } n  n int main() { n     init(); n     solve(); n     return 0; n } n /** 写之前 :  n  *  1. MAXN 是否定义正确？ n  */ n /*********************code start here*********************/ n int a[505][505]; n int sa[1100] = {0}, fa[1100]; n void solve() { n     int n = read(); n     for(int i = 0; i < n; ++i) { n         for(int j = 0; j < n; ++j) { n             a[i][j] = read(); n         } n     } n     memset(fa, -1, sizeof(fa)); n     int sz = n; n     for(int i = 0; i < n; ++i) { n         sa[i] = a[i][i]; n         for(int j = i+1; j < n; ++j) { n             if(fa[j] == -1) { n                 int p = i; n                 while(fa[p] != -1 && sa[fa[p]] < a[i][j]) { n                     p = fa[p]; n                 } n                 if(fa[p] == -1) { n                     fa[p] = sz++; n                     sa[fa[p]] = a[i][j]; n                 } n                 if(sa[fa[p]] == a[i][j]) { n                     fa[j] = fa[p]; n                 } else { n                     int up = fa[p]; n                     fa[j] = fa[p] = sz++; n                     fa[fa[p]] = up; n                     sa[fa[p]] = a[i][j]; n                 } n             } else { n                 int ti = i, tj = j; n                 while(sa[fa[ti]] < a[i][j]) { n                     ti = fa[ti]; n                 } n                 while(sa[fa[tj]] < a[i][j]) { n                     tj = fa[tj]; n                 } n                 if(sa[fa[ti]] == a[i][j]) { n                     fa[tj] = fa[ti]; n                 } else if(sa[fa[tj]] == a[i][j]) { n                     fa[ti] = fa[tj]; n                 } else { n                     int up = fa[ti]; n                     fa[ti] = fa[tj] = sz++; n                     fa[fa[ti]] = up; n                     sa[fa[ti]] = a[i][j]; n                 } n             } n         } n     } n     printf( %d n , sz); n     for(int i = 0; i < sz; ++i) { n         printf( %d  , sa[i]); n     } n     printf(  n ); n     for(int i = 0; i < sz; ++i) { n         if(fa[i] == -1) { n             printf( %d n , i + 1); n             break; n         } n     } n     for(int i = 0; i < sz; ++i) { n         if(fa[i] != -1) { n             printf( %d %d n , i + 1, fa[i] + 1); n         } n     } n } n  n /** 提交前检查 :  n  *  1. 答案和中间结果是否应该是 ll? n  *  2. 是否还有没有读入的数据？ n  *  3. 是否有 n = 1 的情况？ n  */


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int n = 0, m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0, n                   z = 0, cnt = 0, l = 0, r = 0, mid = 0, lo = 0, hi = 0; n     string s; n     bool flag = false; n     cin >> n; n     long long int a[n]; n     for (i = 0; i <= n - 1; i++) cin >> a[i]; n     long long int dpl[n]; n     long long int dpr[n]; n     pair<long long int, long long int> pr; n     stack<pair<long long int, long long int>> st; n     st.push(make_pair(a[0], 0)); n     for (i = 1; i < n; i++) { n       while (!st.empty() && (st.top()).first > a[i]) { n         pr = st.top(); n         st.pop(); n         dpr[pr.second] = i; n       } n       st.push(make_pair(a[i], i)); n     } n     while (!st.empty()) { n       pr = st.top(); n       st.pop(); n       dpr[pr.second] = n; n     } n     st.push(make_pair(a[n - 1], n - 1)); n     for (i = n - 2; i >= 0; i--) { n       while (!st.empty() && (st.top()).first > a[i]) { n         pr = st.top(); n         st.pop(); n         dpl[pr.second] = i; n       } n       st.push(make_pair(a[i], i)); n     } n     while (!st.empty()) { n       pr = st.top(); n       st.pop(); n       dpl[pr.second] = -1; n     } n     long long int ans[n + 1]; n     vector<pair<long long int, long long int>> vec; n     for (i = 0; i < n; i++) vec.push_back(make_pair(a[i], i)); n     sort(vec.rbegin(), vec.rend()); n     j = 1; n     for (i = 0; i < n; i++) { n       pr = vec[i]; n       z = 0; n       z = pr.second - dpl[pr.second] - 1; n       z += dpr[pr.second] - pr.second - 1; n       z++; n       while (j <= z) ans[j] = pr.first, j++; n     } n     for (i = 1; i <= n; i++) cout << ans[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Lower[1000000 + 1], Higher[1000000 + 1]; n char Used[1000000 + 1]; n long long SumNat(long long n) { n   long long p = n, q = n + 1; n   if (p % 2) n     q /= 2; n   else n     p /= 2; n   return (p) * (q); n } n long long PrimePi(long long n) { n   long long v = sqrt(n + 1e-9), p, temp, q, j, end, i, d, t; n   for (int i = 0; i <= 1000000; i++) Used[i] = 0; n   Higher[1] = n - 1; n   for (p = 2; p <= v; p++) { n     Lower[p] = p - 1; n     Higher[p] = (n / p) - 1; n   } n   for (p = 2; p <= v; p++) { n     if (Lower[p] == Lower[p - 1]) continue; n     temp = Lower[p - 1]; n     q = p * p; n     Higher[1] -= Higher[p] - temp; n     j = 1 + (p & 1); n     end = (v <= n / q) ? v : n / q; n     for (i = p + j; i <= 1 + end; i += j) { n       if (Used[i]) continue; n       d = i * p; n       if (d <= v) n         Higher[i] -= Higher[d] - temp; n       else { n         t = n / d; n         Higher[i] -= Lower[t] - temp; n       } n     } n     if (q <= v) n       for (i = q; i <= end; i += p * j) Used[i] = 1; n     for (i = v; i >= q; i--) { n       t = i / p; n       Lower[i] -= Lower[t] - temp; n     } n   } n   return Higher[1]; n } n bool prime[10000000 + 1]; n long long piii[10000000 + 1]; n long long Prim(long long n) { n   if (n <= 10000000) return piii[n]; n   return PrimePi(n); n } n int main() { n   long long n, i, j; n   long long answer; n   for (i = 2; i * i <= 10000000; i++) n     if (!prime[i]) n       for (j = i * i; j <= 10000000; j += i) prime[j] = true; n   for (i = 2; i <= 10000000; i++) { n     piii[i] = piii[i - 1]; n     if (!prime[i]) piii[i]++; n   } n   cin >> n; n   for (i = 1;; i++) { n     if (i * i * i > n) break; n   } n   answer = Prim(i - 1); n   for (i = 2; i < (n / i); i++) n     if (!prime[i]) answer += (Prim(n / i) - Prim(i)); n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans, seq[4], d[3005][3005], MaxT[3005][3005], Max[3005][3005], row; n vector<vector<int>> g; n bool comp(int a, int b) { return d[row][a] > d[row][b]; } n bool compT(int a, int b) { return d[a][row] > d[b][row]; } n void Dijkstra(int s) { n   priority_queue<pair<int, int>> pq; n   pq.push({0, s}); n   while (!pq.empty()) { n     int dist = -pq.top().first, x = pq.top().second; n     pq.pop(); n     if (d[s][x] != -1) continue; n     d[s][x] = dist; n     for (int adj : g[x]) { n       if (d[s][adj] != -1) continue; n       pq.push({-dist - 1, adj}); n     } n   } n } n void sol(int A, int B, int C, int D) { n   if (d[A][B] == -1 || d[B][C] == -1 || d[C][D] == -1) return; n   if (d[A][B] + d[B][C] + d[C][D] > ans) { n     ans = d[A][B] + d[B][C] + d[C][D]; n     seq[0] = A; n     seq[1] = B; n     seq[2] = C; n     seq[3] = D; n   } n } n int main() { n   cin >> n >> m; n   g.resize(n + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       d[i][j] = -1; n     } n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n   } n   for (int i = 1; i <= n; i++) Dijkstra(i); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       MaxT[i][j] = j; n       Max[i][j] = j; n     } n   } n   for (row = 1; row <= n; row++) { n     sort(Max[row] + 1, Max[row] + n + 1, comp); n     sort(MaxT[row] + 1, MaxT[row] + n + 1, compT); n   } n   for (int B = 1; B <= n; B++) { n     for (int C = 1; C <= n; C++) { n       if (B == C) continue; n       for (int i = 1; i < 3; i++) { n         for (int j = 1; j < 3; j++) { n           int A = MaxT[B][i], D = Max[C][j]; n           if (A == B || A == C || A == D || D == B || D == C) continue; n           sol(A, B, C, D); n         } n       } n     } n   } n   for (int i = 0; i < 4; i++) cout << seq[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long int, vector<long long int>, greater<long long int> > q; n int main() { n   int n; n   scanf( %d , &n); n   long long int x, sum = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &x); n     q.push(x); n   } n   while (q.size()) { n     x = q.top(); n     q.pop(); n     if (sum > x) n       ans++; n     else n       sum += x; n   } n   cout << n - ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e5 + 23; n const int INF = 1e9 + 23; n const int MOD = 1e9 + 7; n struct Mada { n   long long x, h, p; n }; n Mada mada[MAX_N]; n stack<Mada> Dane; n int dp[MAX_N]; n int n; n bool cmp(Mada a, Mada b) { return a.x < b.x; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> mada[i].x >> mada[i].h; n     mada[i].p = i; n   } n   sort(mada + 1, mada + 1 + n, cmp); n   dp[mada[n].p] = 1; n   Dane.push(mada[n]); n   for (int i = n - 1; i >= 1; i--) { n     long long res = 0; n     while (!Dane.empty()) { n       if (Dane.top().x > mada[i].h + mada[i].x - 1) { n         break; n       } n       res += dp[Dane.top().p]; n       Dane.pop(); n     } n     dp[mada[i].p] = res + 1; n     Dane.push(mada[i]); n   } n   for (int i = 1; i <= n; i++) { n     cout << dp[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char rep; n void query(long long x, long long y) { n   cout <<  ?   << x <<     << y << endl; n   cin >> rep; n   return; n } n void solve() { n   long long x = 0; n   long long y = 1; n   while (true) { n     query(x, y); n     if (rep ==  x ) { n       break; n     } n     x = y; n     y = y * 2; n   } n   long long l = x + 1; n   long long r = y; n   while (l != r) { n     long long med = (l + r) / 2; n     query(med, y); n     if (rep ==  x ) { n       l = med + 1; n     } else { n       r = med; n     } n   } n   cout <<  !   << l << endl; n   return; n } n int main() { n   string s; n   while (true) { n     cin >> s; n     if (s[0] ==  s ) { n       solve(); n     } else { n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void checkMin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkMax(T& a, T b) { n   if (b > a) a = b; n } n const int N = 22; n const long long INF = 1LL << 60; n int n, m, a[N], b[N]; n long long dp[N][N], tmp[N][N], f[N][N], ans[N][N]; n void DP(int n) { n   for (int t = 0; t <= n; t++) { n     for (int i = 0; i <= n; i++) dp[0][i] = INF; n     dp[0][t] = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 0; j <= n; j++) { n         dp[i][j] = INF; n         if (j > 0) checkMin(dp[i][j], dp[i - 1][j - 1] + a[i]); n         if (j < n) checkMin(dp[i][j], dp[i - 1][j + 1] + b[i]); n       } n     for (int i = 0; i <= n; i++) f[t][i] = dp[n][i]; n   } n } n void fast_mult(int n, int m) { n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) ans[i][j] = INF; n   for (int i = 0; i <= n; i++) ans[i][i] = 0; n   while (m > 0) { n     if (m & 1) { n       for (int i = 0; i <= n; i++) n         for (int j = 0; j <= n; j++) tmp[i][j] = ans[i][j], ans[i][j] = INF; n       for (int k = 0; k <= n; k++) n         for (int i = 0; i <= n; i++) n           for (int j = 0; j <= n; j++) checkMin(ans[i][j], tmp[i][k] + f[k][j]); n     } n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= n; j++) tmp[i][j] = f[i][j], f[i][j] = INF; n     for (int k = 0; k <= n; k++) n       for (int i = 0; i <= n; i++) n         for (int j = 0; j <= n; j++) checkMin(f[i][j], tmp[i][k] + tmp[k][j]); n     m >>= 1; n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   DP(n); n   fast_mult(n, m); n   cout << ans[0][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 69, INF = 1e9 + 85, MOD = 1e9 + 7; n int n, k, a, b, q, arr[MAXN], seg[2][MAXN << 2]; n void update(int indx, int val, int type, int l = 0, int r = n, int id = 1) { n   if (indx < l or indx >= r) return; n   if (r - l == 1) { n     seg[type][id] = val; n     return; n   } n   int mid = (l + r) >> 1; n   update(indx, val, type, l, mid, id << 1 | 0); n   update(indx, val, type, mid, r, id << 1 | 1); n   seg[type][id] = seg[type][id << 1 | 0] + seg[type][id << 1 | 1]; n } n int get(int st, int en, int type, int l = 0, int r = n, int id = 1) { n   if (l >= en or r <= st) return 0; n   if (l >= st and r <= en) return seg[type][id]; n   int mid = (l + r) >> 1; n   return get(st, en, type, l, mid, id << 1 | 0) + n          get(st, en, type, mid, r, id << 1 | 1); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k >> a >> b >> q; n   while (q--) { n     int type, day, order; n     cin >> type; n     if (type == 1) { n       cin >> day >> order; n       arr[day] += order; n       update(day - 1, min(arr[day], a), 0); n       update(day - 1, min(arr[day], b), 1); n     } else { n       cin >> day; n       cout << get(0, day - 1, 1) + get(day + k - 1, n, 0) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t{}; n   cin >> t; n   while (t--) { n     long long int n{}; n     cin >> n; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     bool visit[n]; n     memset(visit, 0, sizeof(visit)); n     vector<pair<long long int, long long int>> vec{}; n     for (long long int i = 0; i < n; i++) { n       for (long long int j = 0; j < n; j++) { n         if (i != j && a[i] != a[j] && (!visit[i] || !visit[j])) { n           visit[i] = 1; n           visit[j] = 1; n           vec.push_back({i + 1, j + 1}); n         } n       } n     } n     if (vec.size() == n - 1) { n       cout <<  YES  <<   n ; n       for (long long int i = 0; i < n - 1; i++) { n         cout << vec[i].first <<     << vec[i].second <<   n ; n       } n     } else { n       cout <<  NO  <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, p, q; n   scanf( %d %d %d , &n, &p, &q); n   char word[n + 1]; n   scanf( %s , word); n   if (n == p + q) { n     printf( 2 n ); n     for (int i = 0; i < p; ++i) { n       printf( %c , word[i]); n     } n     printf(  n ); n     for (int i = p; i < n; ++i) { n       printf( %c , word[i]); n     } n     printf(  n ); n     return 0; n   } else if (n % p != 0 && n % q != 0) { n     int z = 0, i; n     for (i = 0; i < n; ++i) { n       int x = n - (p * i); n       if (x % q == 0 && x > 0) { n         z = i; n         break; n       } n     } n     if (z == 0) { n       printf( -1 n ); n       return 0; n     } n     int y = (n - (z * p)) / q; n     printf( %d n , z + y); n     for (i = 0; i < z; ++i) { n       for (int j = 0; j < p; ++j) { n         int f = p * i + j; n         printf( %c , word[f]); n       } n       printf(  n ); n     } n     for (i = 0; i < y; ++i) { n       int f = z * p; n       for (int j = 0; j < q; ++j) { n         int g = f + q * i + j; n         printf( %c , word[g]); n       } n       printf(  n ); n     } n     return 0; n   } else { n     int s; n     if (n % p == 0) { n       s = p; n     } else { n       s = q; n     } n     printf( %d n , n / s); n     for (int i = 0; i < n / s; ++i) { n       for (int j = 0; j < s; ++j) { n         int x = i * s + j; n         printf( %c , word[x]); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b, c, ans, x[4]; n int main() { n   cin >> a >> b >> c; n   x[0] = a + b + c; n   x[1] = a + c + c + a; n   x[2] = b + c + c + b; n   x[3] = b + b + a + a; n   ans = x[0]; n   for (int i = 1; i < 4; i++) { n     if (ans > x[i]) ans = x[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100 + 5; n using namespace std; n int val[maxn][maxn]; n void doit(int x1, int y1, int x2, int y2) { n   for (int i = x1; i <= x2; i++) { n     for (int j = y1; j <= y2; j++) { n       val[i][j]++; n     } n   } n } n int main() { n   memset(val, 0, sizeof(val)); n   int n; n   cin >> n; n   while (n--) { n     int x, y, u, v; n     cin >> x >> y >> u >> v; n     doit(x, y, u, v); n   } n   int sum = 0; n   for (int i = 1; i <= 100; i++) n     for (int j = 1; j <= 100; j++) sum += val[i][j]; n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n inline bool mina(A &first, B second) { n   return (first > second) ? (first = second, 1) : 0; n } n template <class A, class B> n inline bool maxa(A &first, B second) { n   return (first < second) ? (first = second, 1) : 0; n } n const int MAXN = 1e5 + 5; n int N, K; n int ans[MAXN]; n long long minimum(long long prev, long long num) { n   long long start = prev + 1; n   long long end = start + num - 1; n   return (start + end) * num / 2; n } n int main() { n   cin >> N >> K; n   int ll = 1, rr = N; n   while (ll <= rr) { n     int mid = (ll + rr) / 2; n     if (mid + minimum(mid, K - 1) > N) { n       rr = mid - 1; n     } else { n       ll = mid + 1; n     } n   } n   if (rr == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   ans[0] = rr; n   int left = N - rr; n   int now = 1; n   while (left) { n     if (now >= K) { n       cout <<  NO  << endl; n       return 0; n     } else if (now + 1 == K) { n       ans[now] = left; n       if (left > ans[now - 1] && left <= 2 * ans[now - 1]) { n         break; n       } else { n         cout <<  NO  << endl; n         return 0; n       } n     } else { n       if (2LL * ans[now - 1] + minimum(ans[now - 1] * 2, K - now - 1) > left) { n         int ll = ans[now - 1] + 1; n         int rr = 2LL * ans[now - 1]; n         while (ll <= rr) { n           int mid = (ll + rr) / 2; n           if (mid + minimum(mid, K - now - 1) > left) { n             rr = mid - 1; n           } else { n             ll = mid + 1; n           } n         } n         if (rr < ans[now - 1] + 1) { n           cout <<  NO  << endl; n           return 0; n         } n         ans[now] = rr; n         left -= ans[now]; n         now++; n       } else { n         ans[now] = ans[now - 1] * 2; n         left -= ans[now]; n         now++; n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = 0; (i) < (K); ++(i)) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MX = 10010896; n const int lmt = 3164; n const int N = 10000001; n using namespace std; n long long int extgcd(long long int a, long long int b, long long int& x, n                      long long int& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     int g = extgcd(b, a % b, y, x); n     y -= a / b * x; n     return g; n   } n } n long long int modpow(long long int a, long long int b) { n   long long int res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n inline void solve() { n   long long int n, m; n   cin >> n >> m; n   long long int a[n], b[m]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long int i = 0; i < m; i++) { n     cin >> b[i]; n   } n   long long int ans = 0; n   bool flag2 = false; n   for (ans = 0; ans <= 512; ans++) { n     long long int ci = ans; n     for (long long int i = 0; i < n; i++) { n       flag2 = false; n       for (long long int j = 0; j < m; j++) { n         if (((a[i] & b[j]) | ci) <= ans) { n           flag2 = true; n           ci = (a[i] & b[j]) | ci; n           break; n         } n       } n       if (!flag2) break; n     } n     if (flag2) { n       cout << ans <<   n ; n       return; n     } n   } n } n int main() { n   cout << fixed << setprecision(12); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1, tc = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int max_height(long long n) { return (long long)(sqrt(24 * n + 1) - 1) / 6; } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     int pyramids = 0; n     while (1) { n       long long height = max_height(n); n       if (!height) break; n       long long int cards = (3 * height + 1) * height / 2; n       pyramids += n / cards; n       n -= n / cards * cards; n     } n     cout << pyramids << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-7, PI = 3.1415926; n const int N = 2e5 + 10; n using namespace std; n long long n, q, m, k, x, y, a[N], mx = -1, mem[N][3], vis[N][3], mn = 1e9, sum, n                                   t; n char c[N]; n string s, s1, s2; n map<int, int> mp; n vector<int> vec; n long long dp(long long i, int cur) { n   if (i <= 0 || i > n) return 0; n   if (vis[i][cur] == x) return -1e17; n   if (mem[i][cur] != -1) return mem[i][cur]; n   vis[i][cur] = x; n   long long p1 = 0, p2 = 0; n   if (cur == 0) n     p1 += a[i] + dp(i + a[i], cur ^ 1); n   else n     p1 += a[i] + dp(i - a[i], cur ^ 1); n   return mem[i][cur] = p1; n } n int main() { n   cin >> n; n   memset(mem, -1, sizeof mem); n   for (int i = 2; i <= n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     a[1] = i; n     mem[1][0] = -1; n     x++; n     long long ans = dp(1, 0); n     if (ans >= 0) n       cout << ans << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int ph[N]; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   for (int i = 1; i <= n; i++) n     ph[i] = ph[i - 1] + (s[i - 1] >=  A  && s[i - 1] <=  Z  ? 1 : 0); n   int ans = 1e5; n   for (int i = 0; i <= n; i++) { n     ans = min(ans, i - ph[i] + ph[n] - ph[i]); n   } n   cout << ans << endl; 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 n; n   cin >> n; n   int x; n   cin >> x; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << 0 <<   n ; n     return 0; n   } n   sort(a.begin(), a.end()); n   if (x < a[0]) { n     cout << a[0] - x + a[n - 2] - a[0] <<   n ; n     return 0; n   } n   if (x > a[n - 1]) { n     cout << x - a[n - 1] + a[n - 1] - a[1] <<   n ; n     return 0; n   } n   int ans1 = a[n - 2] - a[0] + min((int)abs(x - a[0]), (int)abs(x - a[n - 2])); n   int ans2 = a[n - 1] - a[1] + min((int)abs(x - a[1]), (int)abs(x - a[n - 1])); n   cout << min(ans1, ans2) <<   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   cout.precision(15); n   long long int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     long long int x; n     cin >> x; n     long long int n = (long long int)s.length(); n     vector<int> v(n, 2); n     for (long long int i = x; i < n - x; i++) { n       if (s[i - x] ==  1  && s[i + x] ==  1 ) n         v[i] = 1; n       else n         v[i] = 0; n     } n     for (long long int i = 0; i < x; i++) { n       if ((i + x) < n && s[i + x] ==  0 ) n         v[i] = 0; n       else n         v[i] = 1; n     } n     for (long long int i = n - x; i < n; i++) { n       if ((i - x) >= 0 && s[i - x] ==  0 ) n         v[i] = 0; n       else n         v[i] = 1; n     } n     string ans; n     for (long long int i = 0; i < n; i++) { n       if (i - x >= 0 && v[i - x] == 1) n         ans +=  1 ; n       else if (i + x < n && v[i + x] == 1) n         ans +=  1 ; n       else n         ans +=  0 ; n     } n     if (ans != s) n       cout <<  -1 n ; n     else { n       string a; n       for (long long int i = 0; i < n; i++) a += ( 0  + v[i]); n       cout << a <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char buf[1 << 21], *p1 = buf, *p2 = buf; n template <class T> n void read(T &x) { n   x = 0; n   int c = getchar(); n   int flag = 0; n   while (c <  0  || c >  9 ) flag |= (c ==  - ), c = getchar(); n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   if (flag) x = -x; n } n template <class T> n T _max(T a, T b) { n   return b < a ? a : b; n } n template <class T> n T _min(T a, T b) { n   return a < b ? a : b; n } n template <class T> n bool checkmax(T &a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n bool checkmin(T &a, T b) { n   return b < a ? a = b, 1 : 0; n } n const int N = 82; n int n; n int l[N], r[N], foo[N]; n int arr[N << 1], top = 0; n int L, R; n double f[N][N], g[N][N]; n double ans[N][N]; n void init() { n   read(n); n   for (int i = 1; i <= n; ++i) { n     read(l[i]); n     read(r[i]); n     arr[++top] = l[i]; n     arr[++top] = r[i]; n     foo[i] = r[i] - l[i]; n   } n   sort(arr + 1, arr + top + 1); n   top = unique(arr + 1, arr + top + 1) - arr - 1; n } n double calcl(int idx) { n   return 1.0 * _min(_max(0, L - l[idx]), foo[idx]) / foo[idx]; n } n double calcr(int idx) { n   return 1.0 * _min(_max(0, r[idx] - R), foo[idx]) / foo[idx]; n } n double calcmid(int idx) { return 1 - calcl(idx) - calcr(idx); } n void run(int idx, int bar) { n   L = arr[bar], R = arr[bar + 1]; n   if (L < l[idx] || R > r[idx]) return; n   memset(f, 0, sizeof f); n   f[0][0] = 1; n   int tot = 0; n   for (int i = 1; i <= n; ++i) { n     if (i == idx) continue; n     ++tot; n     double cl = calcl(i), cr = calcr(i), cm = calcmid(i); n     for (int j = tot; j >= 0; --j) { n       for (int k = tot - j; k >= 0; --k) { n         if (j + k < tot) n           f[j][k] *= cm; n         else n           f[j][k] = 0; n         if (j) f[j][k] += f[j - 1][k] * cl; n         if (k) f[j][k] += f[j][k - 1] * cr; n       } n     } n   } n   double co = 1.0 * (R - L) / foo[idx]; n   for (int i = 0; i <= tot; ++i) { n     for (int j = 0; i + j <= tot; ++j) { n       int num = tot - i - j + 1; n       double res = f[i][j] / num * co; n       ans[idx][i + 1] += res; n       ans[idx][n - j + 1] -= res; n     } n   } n } n void solve() { n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j < top; ++j) { n       run(i, j); n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       ans[i][j] += ans[i][j - 1]; n       printf( %.8lf%c , ans[i][j],    n [j == n]); n     } n   } n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using LL = long long; n const int N = 100000 + 5; n LL D[N], H[N]; n int n, m; n struct Unit { n   int l, r; n   LL Lmax, Rmax, Mmax, sum; n   void show() { n     printf( l = %d,r = %d n , l, r); n     printf( Lmax = %I64d,Rmax = %I64d,Mmax = %I64d,sum = %I64d n , Lmax, Rmax, n            Mmax, sum); n   } n   Unit operator+(const Unit &rhs) const { n     Unit ret; n     ret.l = l; n     ret.r = rhs.r; n     ret.Lmax = std::max(Lmax + D[r] + rhs.sum, rhs.Lmax); n     ret.Rmax = std::max(Rmax, sum + D[r] + rhs.Rmax); n     ret.Mmax = std::max(Mmax, rhs.Mmax); n     ret.Mmax = std::max(ret.Mmax, Lmax + D[r] + rhs.Rmax); n     ret.sum = sum + D[r] + rhs.sum; n     return ret; n   } n }; n struct SegmentTree { n   Unit t[N << 2]; n   void build(int l, int r, int rt) { n     if (l == r) { n       t[rt].l = t[rt].r = l; n       t[rt].Lmax = t[rt].Rmax = 2 * H[l]; n       t[rt].sum = 0; n       t[rt].Mmax = -1; n       return; n     } n     int mid = l + r >> 1; n     build(l, mid, rt << 1); n     build(mid + 1, r, rt << 1 | 1); n     t[rt] = t[rt << 1] + t[rt << 1 | 1]; n   } n   Unit query(int L, int R, int l, int r, int rt) { n     if (L <= l && r <= R) { n       return t[rt]; n     } n     int mid = l + r >> 1; n     if (L <= mid && mid < R) n       return query(L, R, l, mid, rt << 1) + n              query(L, R, mid + 1, r, rt << 1 | 1); n     if (L <= mid) return query(L, R, l, mid, rt << 1); n     return query(L, R, mid + 1, r, rt << 1 | 1); n   } n } sgt; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , D + i); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , H + i); n   } n   sgt.build(1, n, 1); n   while (m--) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (a > b) { n       printf( %I64d n , sgt.query(b + 1, a - 1, 1, n, 1).Mmax); n     } else { n       if (a == 1) { n         printf( %I64d n , sgt.query(b + 1, n, 1, n, 1).Mmax); n       } else if (b == n) { n         printf( %I64d n , sgt.query(1, a - 1, 1, n, 1).Mmax); n       } else { n         printf( n              %I64d n , n             (sgt.query(b + 1, n, 1, n, 1) + sgt.query(1, a - 1, 1, n, 1)).Mmax); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void main0(); n int main() { n   clock_t start, end; n   ios::sync_with_stdio(false); n   cin.tie(0); n   main0(); n   return 0; n } n const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1}; n const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1}; n const int N = 5e5 + 5; n const int M = 1e5; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const int G = 3; n const int Gi = 332748118; n const double eps = 1e-6; n mt19937 rnd( n     (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n bool chkmin(T &x, T y) { n   return y < x ? x = y, 1 : 0; n } n template <typename T> n bool chkmax(T &x, T y) { n   return y > x ? x = y, 1 : 0; n } n long long qpow(long long n, long long m) { n   long long ret = 1; n   while (m) { n     if (m & 1) ret = ret * n % mod; n     n = n * n % mod; n     m >>= 1; n   } n   return ret; n } n long long getinv(long long a) { return qpow(a, mod - 2); } n int n; n vector<int> e[N]; n long long fac[N]; n void init() { n   fac[0] = 1; n   for (int i = 1; i <= N - 1; ++i) { n     fac[i] = fac[i - 1] * i % mod; n   } n } n int r[N], lim; n void ntt(vector<long long> &A, int type) { n   for (int i = 0; i <= lim - 1; ++i) { n     if (i < r[i]) swap(A[i], A[r[i]]); n   } n   for (int mid = 1; mid <= lim - 1; mid <<= 1) { n     long long Wn = qpow(type == 1 ? G : Gi, (mod - 1) / (mid << 1)); n     for (int j = 0; j <= lim - 1; j += (mid << 1)) { n       long long w = 1; n       for (int k = 0; k <= mid - 1; ++k, w = (w * Wn) % mod) { n         int x = A[j + k]; n         int y = w * A[j + mid + k] % mod; n         A[j + k] = (x + y) % mod; n         A[j + mid + k] = (x - y + mod) % mod; n       } n     } n   } n   if (type == -1) { n     long long tmp = getinv(lim); n     for (int i = 0; i <= lim - 1; ++i) { n       A[i] = A[i] * tmp % mod; n     } n   } n } n vector<long long> operator*(vector<long long> A, vector<long long> B) { n   int len = A.size() + B.size() - 1; n   lim = 1; n   while (lim <= len) lim <<= 1; n   for (int i = 0; i <= lim - 1; ++i) { n     r[i] = (r[i >> 1] >> 1) | ((i & 1) * (lim >> 1)); n   } n   A.resize(lim); n   B.resize(lim); n   ntt(A, 1); n   ntt(B, 1); n   for (int i = 0; i <= lim - 1; ++i) { n     A[i] = A[i] * B[i] % mod; n   } n   ntt(A, -1); n   A.resize(len); n   return A; n } n vector<long long> solve(int l, int r) { n   if (l == r) { n     vector<long long> tmp({1, (int)e[l].size() - (l != 1)}); n     return tmp; n   } n   int mid = (l + r) / 2; n   return solve(l, mid) * solve(mid + 1, r); n } n void main0() { n   init(); n   cin >> n; n   for (int i = 1; i <= n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   vector<long long> v = solve(1, n); n   long long ans = 0; n   for (int i = 0; i <= n - 1; ++i) { n     ans += (i & 1 ? -1 : 1) * v[i] * fac[n - i] % mod; n     ans %= mod; n   } n   ans += mod; n   ans %= mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T1, class T2> n inline void print(T1 x1, T2 x2) { n   printsp(x1), println(x2); n } n template <class T1, class T2, class T3> n inline void print(T1 x1, T2 x2, T3 x3) { n   printsp(x1), printsp(x2), println(x3); n } n template <class T1, class T2, class T3, class T4> n inline void print(T1 x1, T2 x2, T3 x3, T4 x4) { n   printsp(x1), printsp(x2), printsp(x3), println(x4); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int a[211][211]; n int dp[211][211][211]; n int dst[211][211]; n int id[211][211]; n int di[] = {1, -1, 0, 0}; n int dj[] = {0, 0, 1, -1}; n char res[211][211]; n int vst[1011]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       gn(a[i][j]); n       vst[a[i][j]]++; n       a[i][j] = a[i][j] * 10000 + vst[a[i][j]]; n     } n   int nn = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) id[i][j] = ++nn; n   memset(dst, 0x3f3f3f3f, sizeof dst); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       for (int k = 0; k < 4; k++) { n         int ii = i + di[k], jj = j + dj[k]; n         if (ii < 1 or jj < 1 or ii > n or jj > m) continue; n         dst[id[i][j]][id[ii][jj]] = a[ii][jj]; n         dst[id[ii][jj]][id[i][j]] = a[i][j]; n       } n     } n   for (int k = 1; k <= nn; k++) { n     for (int i = 1; i <= nn; i++) { n       for (int j = 1; j <= nn; j++) { n         smin(dst[i][j], dst[i][k] + dst[k][j]); n       } n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       for (int ii = 1; ii <= n; ii++) n         for (int jj = 1; jj <= m; jj++) { n           dst[id[i][j]][id[ii][jj]] += a[i][j]; n         } n     } n   memset(dp, 0x3f3f3f3f, sizeof dp); n   for (int i = 0; i < k; i++) { n     int first, second; n     gn(first, second); n     dp[1 << i][first][second] = a[first][second]; n   } n   for (int i = 1; i < 1 << k; i++) { n     for (int p = 1; p <= n; p++) { n       for (int q = 1; q <= m; q++) { n         int b = 0; n         while (b = (b - i) & i) { n           if (b < i) smin(dp[i][p][q], dp[b][p][q] + dp[i ^ b][p][q] - a[p][q]); n         } n       } n     } n     for (int r = 1; r <= n; r++) { n       for (int w = 1; w <= m; w++) { n         for (int p = 1; p <= n; p++) { n           for (int q = 1; q <= m; q++) { n             smin(dp[i][p][q], dp[i][r][w] + dst[id[p][q]][id[r][w]] - a[r][w]); n           } n         } n       } n     } n   } n   int ans = 0x3f3f3f3f; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) smin(ans, dp[(1 << k) - 1][i][j]); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) res[i][j] =  . ; n   cout << ans / 10000 << endl; n   int tmp = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (dp[(1 << k) - 1][i][j] == ans) n         res[i][j] =  X , tmp += a[i][j] / 10000; n     } n   } n   if (ans / 10000 != tmp) n     while (1) n       ; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) printf( %c , res[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 999999999; n int n; n template <typename T> n struct SegTree_t { n   int n; n   vector<int> tree, A; n   SegTree_t(int _n) : n(_n), tree(4 * n, 0), A(4 * n, 0) {} n   T operation(const T &a, const T &b) { return max(a, b); } n   T query(int node, int l, int r, int a, int b) { n     if (a > r || b < l) return 0; n     if (l >= a && r <= b) return tree[node]; n     return operation(query(node << 1, l, (l + r) / 2, a, b), n                      query((node << 1) + 1, (l + r) / 2 + 1, r, a, b)); n   } n   T query(int a, int b) { return query(1, 0, n - 1, a, b); } n   void update(int node, int l, int r, int idx, int val) { n     if (l == r) { n       A[idx] = val; n       tree[node] = val; n     } else { n       int mid = (l + r) / 2; n       if (l <= idx and idx <= mid) n         update(node << 1, l, mid, idx, val); n       else n         update((node << 1) + 1, mid + 1, r, idx, val); n       tree[node] = operation(tree[(node << 1)], tree[(node << 1) + 1]); n     } n   } n   void update(int idx, int val) { return update(1, 0, n - 1, idx, val); } n }; n int main() { n   scanf( %d , &n); n   int ret = 0; n   SegTree_t<int> segtree(n); n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     int y = segtree.query(0, x - 1); n     ret = max(ret, y + 1); n     segtree.update(x, y + 1); n   } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n pair<int, int> ar[3001]; n const long long INF = 1000000000000000; n long long dp[3001][3001]; n int func(int pos, int pre) { n   if (pos > n) return 0; n   if (dp[pos][pre] != -INF) return dp[pos][pre]; n   int x = ar[pos].second + func(pos + 1, pos); n   int y = abs(ar[pos].first - ar[pre].first) + func(pos + 1, pre); n   return dp[pos][pre] = min(x, y); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> ar[i].first >> ar[i].second; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) dp[i][j] = -INF; n   } n   sort(ar + 1, ar + n + 1); n   long long ans = func(2, 1) + ar[1].second; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6, mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, sum = 0; n   cin >> n; n   long long x; n   long long nm = 0; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     nm = max(nm, x); n     sum += x; n   } n   if (sum % (n - 1) == 0) n     sum = sum / (n - 1); n   else n     sum = sum / (n - 1) + 1; n   cout << max(sum, nm) <<   n ; n   return 0; n } n