#include <bits/stdc++.h> n using namespace std; n template <class T> n using v2d = vector<vector<T> >; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); n const int maxN = 2e5 + 10; n const int maxM = 5e5 + 10; n int n, m, a, b; n vector<int> adj[maxN]; n int fa[maxN], fb[maxN]; n long long ca = 0, cb = 0; n void DFSa(int u) { n   if (u == b) { n     return; n   } n   fa[u] = 1; n   for (auto &v : adj[u]) { n     if (!fa[v]) { n       DFSa(v); n     } n   } n } n void DFSb(int u) { n   if (u == a) { n     return; n   } n   fb[u] = 1; n   for (auto &v : adj[u]) { n     if (!fb[v]) { n       DFSb(v); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> m >> a >> b; n     for (int i = 1; i <= (int)(n); ++i) { n       adj[i].clear(); n       fa[i] = fb[i] = 0; n     } n     while (m--) { n       int u, v; n       cin >> u >> v; n       adj[u].emplace_back(v); n       adj[v].emplace_back(u); n     } n     DFSa(a); n     DFSb(b); n     ca = cb = 0; n     for (int i = 1; i <= (int)(n); ++i) { n       if (fa[i]) { n         ca += !fb[i]; n       } n       if (fb[i]) { n         cb += !fa[i]; n       } n     } n     cout << (ca - 1) * (cb - 1) <<   n ; 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(NULL); n   int t; n   cin >> t; n   while (t--) { n     unsigned int n; n     cin >> n; n     unsigned long k; n     cin >> k; n     if (n & 1) { n       for (int i = 2; i <= (n); i++) { n         if (n % i == 0) { n           n += i; n           break; n         } n       } n       n += 2 * (k - 1); n     } else n       n += 2 * k; n     cout << n <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                           greater<pair<long long, int> > > &P, n            priority_queue<pair<long long, int> > &N, vector<long long> &ans, n            int k, int x) { n   while (k > 0) { n     int ps = P.size(), pn = N.size(); n     if (ps && pn) { n       pair<long long, int> pt = P.top(); n       P.pop(); n       pair<long long, int> nt = N.top(); n       N.pop(); n       int sign = (pn & 1) ? -1 : 1; n       if (nt.first * -1 <= pt.first) { n         nt.first += sign * x; n         if (nt.first >= 0) { n           P.push(nt); n           P.push(pt); n         } else { n           N.push(nt); n           P.push(pt); n         } n       } else { n         pt.first -= sign * x; n         if (pt.first < 0) { n           N.push(nt); n           N.push(pt); n         } else { n           N.push(nt); n           P.push(pt); n         } n       } n     } else if (ps) { n       pair<long long, int> pt = P.top(); n       P.pop(); n       pt.first -= x; n       if (pt.first < 0) { n         N.push(pt); n       } else { n         P.push(pt); n       } n     } else { n       pair<long long, int> nt = N.top(); n       N.pop(); n       int sign = (pn & 1) ? -1 : 1; n       nt.first += sign * x; n       if (nt.first >= 0) { n         P.push(nt); n       } else { n         N.push(nt); n       } n     } n     k--; n   } n   while (!P.empty()) { n     pair<long long, int> it = P.top(); n     P.pop(); n     ans[it.second] = it.first; n   } n   while (!N.empty()) { n     pair<long long, int> it = N.top(); n     N.pop(); n     ans[it.second] = it.first; n   } n } n int main() { n   int n, k, x, t; n   while (cin >> n >> k >> x) { n     priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                    greater<pair<long long, int> > > n         Qp; n     priority_queue<pair<long long, int> > Qn; n     for (int i = 0; i < n; i++) { n       cin >> t; n       if (t >= 0) n         Qp.push(make_pair(t, i)); n       else n         Qn.push(make_pair(t, i)); n     } n     vector<long long> ans(n); n     solve(Qp, Qn, ans, k, x); n     for (int i = 0; i < n; i++) cout << ans[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char ch[200000]; n int main() { n   scanf( %s , ch + 1); n   int n = strlen(ch + 1); n   int flag = 0; n   for (int i = 1; i <= n; i++) { n     if (ch[i] != ch[n - i + 1]) flag++; n   } n   if (flag == 2 || n % 2 == 1 && flag == 0) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> globmap; n int optwin[32000]; n int optptsc[32000]; n int optptso[32000]; n int pts[32000]; n map<string, int> hashid; n string hashinv[32000]; n void findit(int s) { n   if (optwin[s] != -1) { n     return; n   } n   string S = hashinv[s]; n   string append =  z ; n   optwin[s] = 0; n   optptsc[s] = 0; n   optptso[s] = 1000000000; n   bool can = 0; n   for (int c =  a ; c <=  z ; c++) { n     append[0] = c; n     string Snext; n     for (int b = 0; b < 2; b++) { n       if (b) n         Snext = S + append; n       else n         Snext = append + S; n       if (hashid.find(Snext) == hashid.end()) continue; n       can = 1; n       int snext = hashid[Snext]; n       findit(snext); n       int candwin = !optwin[snext]; n       int candptsc = pts[snext] + optptso[snext]; n       int candptso = optptsc[snext]; n       if (candwin < optwin[s]) continue; n       if (candwin > optwin[s]) { n         optwin[s] = candwin; n         optptsc[s] = candptsc; n         optptso[s] = candptso; n         continue; n       } n       if (candptsc < optptsc[s]) continue; n       if (candptsc > optptsc[s]) { n         optwin[s] = candwin; n         optptsc[s] = candptsc; n         optptso[s] = candptso; n         continue; n       } n       if (candptso > optptso[s]) continue; n       if (candptso < optptso[s]) { n         optwin[s] = candwin; n         optptsc[s] = candptsc; n         optptso[s] = candptso; n         continue; n       } n     } n   } n   if (!can) { n     optwin[s] = 0; n     optptsc[s] = 0; n     optptso[s] = 0; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   map<string, int> globmap; n   for (int i = 0; i < n; i++) { n     string word; n     cin >> word; n     map<string, bool> allsubs; n     int L = word.size(); n     for (int i = 0; i < L; i++) n       for (int j = i; j < L; j++) allsubs[word.substr(i, j - i + 1)] = 1; n     for (map<string, bool>::iterator it = allsubs.begin(); it != allsubs.end(); n          it++) { n       string S = (*it).first; n       if (globmap.find(S) == globmap.end()) n         globmap[S] = 1; n       else n         globmap[S]++; n     } n   } n   int ct = 0; n   for (map<string, int>::iterator it = globmap.begin(); it != globmap.end(); n        it++) { n     string S = (*it).first; n     int L = S.size(); n     int maxx = 0; n     int summ = 0; n     for (int i = 0; i < L; i++) { n       summ += S[i] -  a  + 1; n       if (S[i] -  a  + 1 > maxx) maxx = S[i] -  a  + 1; n     } n     (*it).second += summ * maxx; n     hashid[S] = ct + 1; n     hashinv[ct + 1] = S; n     pts[ct + 1] = (*it).second; n     ct++; n   } n   hashid[  ] = 0; n   hashinv[0] =   ; n   pts[0] = 0; n   ct++; n   for (int i = 0; i < ct; i++) { n     optwin[i] = -1; n     optptsc[i] = -1; n     optptso[i] = -1; n   } n   findit(0); n   if (optwin[0]) n     printf( First n ); n   else n     printf( Second n ); n   printf( %d %d n , optptsc[0], optptso[0]); n } n


#include <bits/stdc++.h> n using namespace std; n int longestIncreasingSubsequenceDP(int a[], int n) { n   int dp[n], ans = 0; n   for (int k = 0; k < n; k++) { n     dp[k] = 1; n     for (int i = 0; i < k; i++) { n       if (a[i] <= a[k]) { n         dp[k] = max(dp[k], dp[i] + 1); n         ans = max(dp[k], ans); n       } n     } n   } n   return ans; n } n int gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == b) return a; n   if (a > b) return gcd(a - b, b); n   return gcd(a, b - a); n } n void scanArray(int arr[], int n) { n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n } n int highestFromThree(int n1, int n2, int n3) { return max(max(n1, n2), n3); } n int highestFromFour(int n1, int n2, int n3, int n4) { n   return max(max(n1, n2), max(n3, n4)); n } n int result[1000] = {0}; n int factDP(int n) { n   if (n >= 0) { n     result[0] = 1; n     for (int i = 1; i <= n; ++i) { n       result[i] = i * result[i - 1]; n     } n     return result[n]; n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   int l[n], r[n]; n   vector<pair<int, int>> v; n   int points[m]; n   for (int i = 0; i < m; i++) { n     points[i] = i + 1; n   } n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n     v.push_back(make_pair(l[i], r[i])); n   } n   int count = m; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < v.size(); j++) { n       if (points[i] >= v[j].first && points[i] <= v[j].second) { n         count--; n         points[i] = 0; n         break; n       } n     } n   } n   cout << count <<   n ; n   for (int i = 0; i < m; i++) { n     if (points[i] != 0) { n       cout << points[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, a[4]; n char s[N]; n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  A ) n       a[0]++; n     else if (s[i] ==  C ) n       a[1]++; n     else if (s[i] ==  G ) n       a[2]++; n     else if (s[i] ==  T ) n       a[3]++; n   } n   sort(a, a + 4); n   int mx = a[3]; n   long long m = 0; n   for (int i = 0; i < 4; ++i) n     if (a[i] == mx) m++; n   long long ans = 1; n   for (int i = 0; i < n; ++i) ans = (ans * m) % (1000000007); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using std::abs; n using std::bitset; n using std::cerr; n using std::cin; n using std::copy; n using std::cout; n using std::deque; n using std::endl; n using std::fill; n using std::fixed; n using std::greater; n using std::iota; n using std::istream; n using std::lower_bound; n using std::make_pair; n using std::make_tuple; n using std::map; n using std::max; n using std::max_element; n using std::min; n using std::min_element; n using std::multiset; n using std::next_permutation; n using std::nth_element; n using std::ostream; n using std::pair; n using std::priority_queue; n using std::queue; n using std::reverse; n using std::rotate; n using std::set; n using std::sort; n using std::string; n using std::swap; n using std::tie; n using std::tuple; n using std::unique; n using std::unordered_map; n using std::unordered_set; n using std::upper_bound; n using std::vector; n long long rnd(long long x, long long y) { n   static auto gen = std::bind(std::uniform_int_distribution<long long>(), n                               std::mt19937(960172)); n   return gen() % (y - x + 1) + x; n } n long long gcd(long long a, long long b) { n   while (b > 0) { n     long long t = a % b; n     a = b; n     b = t; n   } n   return a; n } n template <typename T> n T sqr(T const& a) { n   return a * a; n } n int const INF = 100 + (int)1e9; n long long const INFL = 100 + (long long)1e18; n long double const PI = 3.1415926535897932384626433832795028; n void solve() { n   vector<string> const hs = { Anka ,    Chapay ,  Cleo ,        Troll , n                               Dracul ,  Snowy ,   Hexadecimal }; n   int qq; n   cin >> qq; n   int n = hs.size(); n   vector<vector<int>> z(n, vector<int>(n)); n   while (qq--) { n     string a, b, c; n     cin >> a >> b >> c; n     int i = 0; n     while (hs[i] != a) ++i; n     int j = 0; n     while (hs[j] != c) ++j; n     ++z[i][j]; n   } n   int A, B, C; n   cin >> A >> B >> C; n   int three = 1; n   for (int i = 0; i < n; ++i) three *= 3; n   int diff = 2 * INF; n   int sym = 0; n   for (int mask = 0; mask < three; ++mask) { n     int val = mask; n     vector<int> who(n), cnt(3); n     for (int i = 0; i < n; ++i) { n       int x = val % 3; n       val /= 3; n       who[i] = x; n       ++cnt[x]; n     } n     if (!cnt[0] || !cnt[1] || !cnt[2]) continue; n     int a = A / cnt[0], b = B / cnt[1], c = C / cnt[2]; n     int cdiff = max({a, b, c}) - min({a, b, c}); n     int csym = 0; n     for (int x = 0; x < n; ++x) n       for (int y = 0; y < n; ++y) csym += z[x][y] * (who[x] == who[y]); n     if (cdiff < diff || (cdiff == diff && csym > sym)) { n       diff = cdiff; n       sym = csym; n     } n   } n   cout << diff <<     << sym << endl; n } n int main() { n   cout.precision(15); n   cout << fixed; n   cerr.precision(6); n   cerr << fixed; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 * 1000 + 5; n int xs, ys, xt, yt, n, a[N], b[N], r[N]; n map<pair<int, int>, int> mark; n map<pair<int, int>, int> dist; n int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; n int dy[] = {0, 0, 1, -1, 1, -1, 1, -1}; n void bfs(int x, int y) { n   queue<pair<int, int> > q; n   dist[{x, y}] = 0; n   q.push({x, y}); n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     x = p.first, y = p.second; n     for (int i = 0; i < 8; i++) { n       int nx = x + dx[i], ny = y + dy[i]; n       if (!mark[{nx, ny}]) n         continue; n       else if (dist[{nx, ny}] > dist[{x, y}] + 1) { n         dist[{nx, ny}] = dist[{x, y}] + 1; n         q.push({nx, ny}); n       } n     } n   } n } n void input() { n   cin >> xs >> ys >> xt >> yt >> n; n   for (int i = 0; i < n; i++) cin >> r[i] >> a[i] >> b[i]; n } n void solve() { n   for (int i = 0; i < n; i++) { n     for (int j = a[i]; j <= b[i]; j++) { n       mark[{r[i], j}] = true; n       dist[{r[i], j}] = 1e9; n     } n   } n   bfs(xs, ys); n } n void output() { n   int ans = dist[{xt, yt}]; n   if (ans == 1e9) n     cout << -1 <<   n ; n   else n     cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   input(); n   solve(); n   output(); n } n


#include<bits/stdc++.h> n #define ll long long n using namespace std; n ll T,n,k; n void work(){ n  scanf( %lld%lld ,&n,&k); n  if(n<=k){printf( %lld n ,(long long)ceil((double)k*1.0/(double)n));return;} n  else{ n   if(n%k==0){ n    printf( %lld n ,1LL); n    return; n   } n   else printf( %lld n ,2LL); n  } n  return; n } n int main(){ n  scanf( %d ,&T); n  while(T--)work();  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1000 * 1000 * 1000 + 7; n const long long LINF = INF * (long long)INF; n const int MAX = 3000 + 47; n vector<int> g[MAX]; n vector<int> S; n int L[MAX]; n int U[MAX]; n queue<int> q; n int D[MAX]; n int n; n void dfs(int v, int p) { n   if (U[v] == 1) { n     int sz = (int)S.size() - 1; n     L[v] = 1; n     while (S[sz] != v) { n       L[S[sz]] = 1; n       sz--; n     } n     return; n   } n   U[v] = 1; n   S.push_back(v); n   for (int i = (0); i < ((int)g[v].size()); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     dfs(to, v); n   } n   S.pop_back(); n   U[v] = 2; n } n void bfs() { n   for (int i = (0); i < (n); i++) D[i] = INF; n   for (int i = (0); i < (n); i++) { n     if (!L[i]) continue; n     q.push(i); n     D[i] = 0; n   } n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int i = (0); i < ((int)g[v].size()); i++) { n       int to = g[v][i]; n       if (D[to] != INF) continue; n       D[to] = D[v] + 1; n       q.push(to); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = (0); i < (n); i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(0, 0); n   bfs(); n   for (int i = (0); i < (n); i++) { n     cout << D[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l, r, cnt; n int a[105]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> l >> r; n     for (int i = l; i <= r; ++i) { n       a[i] = 1; n     } n   } n   for (int i = 1; i <= m; ++i) { n     if (!a[i]) cnt++; n   } n   cout << cnt << endl; n   for (int i = 1; i <= m; ++i) { n     if (!a[i]) cout << i <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, ser = 0, dim = 0; n   vector<int> v; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n   } n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       ser += max(v.front(), v.back()); n       if (v.front() > v.back()) n         v.erase(v.begin()); n       else n         v.pop_back(); n     } else { n       dim += max(v.front(), v.back()); n       if (v.front() > v.back()) n         v.erase(v.begin()); n       else n         v.pop_back(); n     } n   } n   cout << ser <<     << dim << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long long MOD = 1e9 + 7; n long long dp[4000][4000]; n long long mn[4000][4000]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   set<long long> uss; n   long long a[n]; n   long long b[n]; n   long long sm = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] = a[i] - i; n     uss.insert(a[i]); n   } n   vector<long long> us; n   for (auto i : uss) { n     us.push_back(i); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < us.size(); j++) { n       dp[i + 1][j] = mn[i][j] + abs(us[j] - a[i]); n       mn[i + 1][j] = dp[i + 1][j]; n       if (j) mn[i + 1][j] = min(mn[i + 1][j], mn[i + 1][j - 1]); n     } n   } n   cout << mn[n][us.size() - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   int n; n   cin >> n; n   int t; n   int speed = 0; n   int ans = 0; n   int c = 0; n   int bs; n   stack<int> s; n   cin >> t >> speed; n   for (int i = 1; i < n; i++) { n     cin >> t; n     if (t == 1) { n       cin >> speed; n       while (!s.empty() && speed > s.top()) { n         s.pop(); n         ans++; n       } n     } else if (t == 2) { n       ans += c; n       c = 0; n     } else if (t == 3) { n       cin >> bs; n       s.push(bs); n       if (bs < speed) { n         s.pop(); n         ans++; n       } n     } else if (t == 4) { n       c = 0; n     } else if (t == 5) { n       while (!s.empty()) s.pop(); n     } else { n       c++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps(1e-8); n const long long inf = -99999999999999999; n long long row, col, k, p; n long long sumr[1001]; n long long sumc[1001]; n long long answerR[1000001]; n long long answerC[1000001]; n priority_queue<long long> Rtmp, Ctmp; n int main() { n   scanf( %I64d %I64d %I64d %I64d , &row, &col, &k, &p); n   memset(sumr, 0, sizeof(sumr)); n   memset(sumc, 0, sizeof(sumc)); n   long long tmp; n   for (int i = 1; i <= row; i++) { n     for (int j = 1; j <= col; j++) { n       scanf( %I64d , &tmp); n       sumr[i] += tmp; n       sumc[j] += tmp; n     } n   } n   for (int i = 1; i <= row; i++) { n     Rtmp.push(sumr[i]); n   } n   for (int i = 1; i <= col; i++) { n     Ctmp.push(sumc[i]); n   } n   memset(answerR, 0, sizeof(answerR)); n   memset(answerC, 0, sizeof(answerC)); n   long long ans = 0; n   for (int j = 1; j <= k; j++) { n     ans += Rtmp.top(); n     tmp = Rtmp.top(); n     tmp -= col * p; n     Rtmp.pop(); n     Rtmp.push(tmp); n     answerR[j] = ans; n   } n   ans = 0; n   for (int j = 1; j <= k; j++) { n     ans += Ctmp.top(); n     tmp = Ctmp.top(); n     tmp -= row * p; n     Ctmp.pop(); n     Ctmp.push(tmp); n     answerC[j] = ans; n   } n   long long answer = inf; n   for (int i = 0; i <= k; i++) { n     answer = max(answer, answerR[i] + answerC[k - i] - i * (k - i) * p); n   } n   printf( %I64d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000000 + 7; n const long long int N = 10000000 + 6; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   char matrix[4][n]; n   for (int i = 0; i < 4; i++) n     for (int j = 0; j < n; j++) matrix[i][j] =  . ; n   bool bo = true; n   if (k & 1) { n     if (k > n - 2) { n       int g = n - 2; n       for (int i = 1; i <= g; i++) matrix[1][i] =  # ; n       int l = k - n; n       matrix[2][1] =  # ; n       matrix[2][n - 2] =  # ; n       for (int i = 2; i <= l + 1; i++) matrix[2][i] =  # ; n     } else { n       int i = n / 2 - k / 2; n       int c = 1; n       for (int j = i; c <= k; c++, j++) matrix[1][j] =  # ; n     } n   } else { n     int g = k / 2; n     for (int i = 1; i <= g; i++) matrix[1][i] =  # ; n     for (int i = 1; i <= g; i++) matrix[2][i] =  # ; n   } n   if (bo) { n     cout <<  YES n ; n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < n; j++) cout << matrix[i][j]; n       cout <<   n ; n     } n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n void read(T& val) { n   std::string tmp = typeid(val).name(); n   if (strcmp(tmp.c_str(),  i ) == 0) n     scanf( %d , &val); n   else if (strcmp(tmp.c_str(),  x ) == 0) n     scanf( %I64d , &val); n   else if (strcmp(tmp.c_str(),  d ) == 0) n     scanf( %lf , &val); n   else if (tmp.length() > 2 && tmp.back() ==  c ) n     scanf( %s , val); n   else if (strcmp(tmp.c_str(),  c ) == 0) n     scanf( %c , &val); n } n template <typename T, typename... Targs> n void read(T& val, Targs&... Fargs) { n   read(val); n   read(Fargs...); n } n template <typename T> n void print(T val) { n   std::string tmp = typeid(val).name(); n   if (strcmp(tmp.c_str(),  i ) == 0) n     printf( %d , val); n   else if (strcmp(tmp.c_str(),  x ) == 0) n     printf( %I64d , val); n   else if (strcmp(tmp.c_str(),  d ) == 0) n     printf( %.15f , val); n   else if (strcmp(tmp.c_str(),  c ) == 0) n     printf( %c , val); n   else if (tmp.length() > 2 && tmp.back() ==  c ) n     printf( %s , val); n } n template <typename T, typename... Targs> n void print(T val, Targs... Fargs) { n   print(val); n   print(Fargs...); n } n const double pi = acos(-1); n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n const int MAXN = 1e6 + 5; n const double eps = 1e-9; n using namespace std; n int a = 1, b, c, n, x, y, z; n int main() { n   read(n, x, y, c); n   while (a < c) { n     b++; n     z = min(x - 1, b); n     a += max(z - max((y + b - n), 0) + 1, 0) + n          max(z - max((b - y + 1), 0) + 1, 0) - (z == b); n     z = min(n - x, b); n     a += max(z - max((y + b - n), 1) + 1, 0) + n          max(z - max((b - y + 1), 1) + 1, 0) - (z == b); n   } n   print(b); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18; n const long double Pi = 3.141592653589793116, EPS = 1e-9, n                   Gold = ((1 + sqrt(5)) / 2); n long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL}; n long long keyCount = sizeof(keymod) / sizeof(long long); n mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); n mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count()); n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcountll(s); n } n auto TimeStart = chrono::steady_clock::now(); n auto TimeEnd = chrono::steady_clock::now(); n void ControlIO(int argc, char* argv[]); n void TimerStart(); n void TimerStop(); n void Exit(); n string cppstr_infile =  FILE.IN ; n string cppstr_outfile =  FILE.OUT ; n int n; n vector<int> c; n vector<long long> dp; n vector<vector<int>> adj; n vector<vector<int>> DependencyList; n set<int> FinalList; n void DFS_GetList(int z, int last, vector<int>& MaxLeaf, vector<int>& MaxID, n                  vector<int>& MaxCnt) { n   dp[z] = 0; n   bool isLeaf = true; n   for (auto t : adj[z]) { n     if (t == last) continue; n     isLeaf = false; n     DFS_GetList(t, z, MaxLeaf, MaxID, MaxCnt); n     dp[z] += dp[t]; n     if (MaxLeaf[t] >= MaxLeaf[z]) MaxID[z] = MaxID[t]; n     if (MaxLeaf[t] > MaxLeaf[z]) n       MaxCnt[z] = MaxCnt[t]; n     else if (MaxLeaf[t] == MaxLeaf[z]) n       MaxCnt[z] += MaxCnt[t]; n     MaxLeaf[z] = max(MaxLeaf[z], MaxLeaf[t]); n   } n   if (isLeaf) { n     MaxLeaf[z] = dp[z] = c[z]; n     MaxID[z] = z; n     MaxCnt[z] = 1; n     DependencyList[z].push_back(z); n     return; n   } n   int MaxRemovable = -MaxLeaf[z]; n   if (MaxRemovable + c[z] > 0) return; n   if (MaxRemovable + c[z] < 0 && MaxCnt[z] == 1) { n     DependencyList[MaxID[z]].clear(); n   } n   if (MaxCnt[z] == 1) n     DependencyList[MaxID[z]].push_back(z); n   else { n     DependencyList[z].push_back(z); n     MaxID[z] = z; n   } n   dp[z] += (MaxRemovable + c[z]); n   MaxLeaf[z] = c[z]; n   MaxCnt[z] = 1; n } n void Input() { n   cin >> n; n   c.resize(n); n   dp.resize(n); n   adj.resize(n); n   DependencyList.resize(n); n   for (auto& z : c) cin >> z; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n } n void Solve() { n   vector<int> MaxLeaf(n, -1), MaxID(n, -1), MaxCnt(n, 0); n   DFS_GetList(0, -1, MaxLeaf, MaxID, MaxCnt); n   for (int id = 0; id < n; id++) { n     for (auto z : DependencyList[id]) FinalList.insert(z + 1); n   } n   cout << dp[0] <<     << FinalList.size() <<   n ; n   for (auto it = FinalList.begin(); it != FinalList.end(); it++) n     cout << (*it) <<    ; n   cout <<   n ; n } n int main(int argc, char* argv[]) { n   ControlIO(argc, argv); n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   Input(); n   TimerStart(); n   Solve(); n   TimerStop(); n   return 0; n } n void ControlIO(int argc, char* argv[]) { n   char* infile = new char[cppstr_infile.size() + 1]; n   char* outfile = new char[cppstr_outfile.size() + 1]; n   strcpy(infile, cppstr_infile.c_str()); n   strcpy(outfile, cppstr_outfile.c_str()); n } n void TimerStart() {} n void TimerStop() {} n void Exit() { n   TimerStop(); n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pll = pair<ll, ll>; n using pii = pair<int, int>; n struct Node { n   vector<ll> con; n   ll h1, h2; n } g[100010]; n ll dfs(ll n, ll p) { n   g[n].h1 = g[n].h2 = 0; n   for (auto &i : g[n].con) { n     if (i == p) continue; n     ll k = dfs(i, n) + 1; n     if (k >= g[n].h1) { n       g[n].h2 = g[n].h1; n       g[n].h1 = k; n     } else if (k >= g[n].h2) { n       g[n].h2 = k; n     } n   } n   return g[n].h1; n } n ll dist(ll n, ll p, ll t) { n   if (n == t) return 0; n   ll ans = (ll)(1e17); n   for (auto &i : g[n].con) { n     if (i == p) continue; n     ans = min(ans, dist(i, n, t) + 1); n   } n   return ans; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ll N; n   cin >> N; n   while (N--) { n     ll n, a, b, da, db; n     cin >> n >> a >> b >> da >> db; n     for (ll i = 0; i <= n - 1; i++) g[i].con.clear(); n     a--; n     b--; n     ll s, t; n     for (ll i = 0; i <= n - 2; i++) { n       cin >> s >> t; n       s--; n       t--; n       g[s].con.emplace_back(t); n       g[t].con.emplace_back(s); n     } n     dfs(0, 0); n     ll height = 0; n     for (ll i = 0; i <= n - 1; i++) height = max(height, g[i].h1 + g[i].h2); n     ll dis = dist(a, a, b); n     if (da * 2 < db && da * 2 < height && dis > da) n       cout <<  Bob n ; n     else n       cout <<  Alice n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int n, m; n     cin >> n; n     vector<int> v(2 * n), v2; n     for (int i = 0; i < 2 * n; i++) { n       cin >> v[i]; n     } n     for (int i = 0; i < 2 * n; i++) { n       int d = 1; n       for (int j = 0; j < i; j++) { n         if (v[j] == v[i]) { n           d = 0; n           break; n         } n       } n       if (d) { n         v2.push_back(v[i]); n       } n     } n     for (int i = 0; i < v2.size(); i++) { n       cout << v2[i] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[1100000], v[1100000], w[1100000], n, K, s1[1100000], s2[1100000], len1, n     len2, pre1, pre2; n void insert(int k) { n   while (len1 >= pre1 && v[s1[len1]] <= v[k]) len1--; n   s1[++len1] = k; n   while (len2 >= pre2 && c[s2[len2]] >= c[k]) len2--; n   s2[++len2] = k; n } n int main() { n   scanf( %d%d , &n, &K); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   pre1 = 1; n   pre2 = 1; n   len1 = 1; n   len2 = 1; n   int now = 1; n   s1[1] = 1; n   s2[1] = 1; n   for (int i = 1; i <= n; i++) { n     if (now < i) insert(i), now++; n     while (s1[pre1] < i) pre1++; n     while (s2[pre2] < i) pre2++; n     while (now < n) { n       int k1 = v[s1[pre1]], k2 = c[s2[pre2]]; n       if (k1 * 100 >= k2) break; n       k1 = max(k1, v[now + 1]); n       k2 = min(k2, c[now + 1]); n       if (k1 * 100 < k2) { n         insert(now + 1); n         now++; n       } else n         break; n     } n     int k1 = v[s1[pre1]], k2 = c[s2[pre2]]; n     w[i] = min(k1 * 100, k2); n     if (now != n) { n       k1 = max(k1, v[now + 1]); n       k2 = min(k2, c[now + 1]); n       w[i] = max(w[i], min(k1 * 100, k2)); n     } n   } n   sort(w + 1, w + n + 1); n   double ans = 0, noww = (double)K / (double)n; n   for (int i = 1; i <= n - K + 1; i++) { n     ans = ans + w[i] * noww; n     int k1 = n - K + 1 - i, k2 = n - i; n     noww = noww * k1 / k2; n   } n   printf( %.11lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int a[305], ans[305], b[305][305], n, mx = 0; n bool visit[305]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> s; n     for (int j = 0; j < n; j++) { n       b[i][j + 1] = s[j] - 48; n     } n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (b[i][k] && b[k][j]) b[i][j] = 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     mx = a[i]; n     for (int j = 1; j <= n; j++) { n       if (b[i][j]) { n         if (a[j] < mx && !visit[a[j]]) { n           mx = a[j]; n           swap(a[i], a[j]); n         } n       } n     } n     visit[mx] = 1; n     ans[i] = mx; n   } n   for (int i = 0; i < n; i++) { n     if (i) n       cout <<     << ans[i + 1]; n     else n       cout << ans[i + 1]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int isp[100005]; n void pp() { n   for (long long int i = 2; i <= 100000; i++) { n     if (!isp[i]) { n       for (long long int j = i * i; j <= 100000; j += i) { n         isp[j] = 1; n       } n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   pp(); n   while (t--) { n     long long int n, i, j, x, y; n     cin >> n; n     if (!isp[n]) { n       for (long long int i = 1; i <= n; i++) { n         for (long long int j = 1; j <= n; j++) { n           cout << 1 <<    ; n         } n         cout <<   n ; n       } n       continue; n     } n     x = -1; n     for (long long int i = n + 1; i <= 100000; i++) { n       if (!isp[i] && isp[i - n + 1]) { n         x = i - n + 1; n         break; n       } n     } n     for (long long int i = 1; i < n; i++) { n       for (long long int j = 1; j < n; j++) { n         cout << 1 <<    ; n       } n       cout << x <<   n ; n     } n     y = 0; n     for (long long int i = 1; i < n; i++) { n       y += x; n       cout << x <<    ; n     } n     for (long long int i = y; i <= 100000; i++) { n       if (!isp[i] && isp[i - y]) { n         x = i - y; n         break; n       } n     } n     cout << x <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> p; n vector<pair<long long, long long>> adj[101]; n long long visited[101]; n long long parent[101]; n long long maxi = 0; n set<long long, greater<long long>> set1; n vector<long long> arr; n long long ans[101]; n void dfs(long long v, long long p) { n   for (auto q : adj[v]) { n     long long u = q.first; n     if (u == p) continue; n     dfs(u, v); n     ans[v] = max(ans[v], ans[u] + q.second); n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, a, b, c; n   cin >> n; n   long long z = n - 1; n   while (z--) { n     cin >> a >> b >> c; n     adj[a].push_back(make_pair(b, c)); n     adj[b].push_back(make_pair(a, c)); n   } n   memset(visited, 0, sizeof(visited)); n   dfs(0, -1); n   cout << ans[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n vector<int> x[1003]; n int a[1003], v[1003]; n int n; n void pp() { n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < x[i].size(); j++) cout << i <<     << x[i][j] <<   n ; n   } n } n int main() { n   int m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= k; i++) { n     cin >> a[i]; n     v[a[i]] = 1; n   } n   if (k == n) { n     cout <<  -1  <<   n ; n     return 0; n   } n   int l; n   for (int i = 1; i <= n; i++) { n     if (v[i] == 0) l = i; n   } n   for (int i = 1; i <= n; i++) { n     if (i != l) x[l].push_back(i); n   } n   int w = n - 1; n   if (w == m) { n     pp(); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (i != l && i != a[1] && i != a[2] && j != l && j != a[1] && n           j != a[2]) { n         x[i].push_back(j); n         w++; n         if (w == m) { n           pp(); n           return 0; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (i != l && i != a[1] && i != a[2]) { n       if (v[i] == 1) { n         x[a[1]].push_back(i); n         w++; n         if (w == m) { n           pp(); n           return 0; n         } n       } else { n         x[a[1]].push_back(i); n         w++; n         if (w == m) { n           pp(); n           return 0; n         } n         x[a[2]].push_back(i); n         w++; n         if (w == m) { n           pp(); n           return 0; n         } n       } n     } n   } n   cout <<  -1  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int INF = 0x3f3f3f3f; n long long n, l, r; n pair<long long, long long> dfs(long long x) { n   if (x == 0) n     return make_pair(1, 0); n   else if (x == 1) n     return make_pair(1, 1); n   long long tmp = x & 1; n   pair<long long, long long> t = dfs(x / 2); n   t.first = t.first * 2 + 1; n   t.second = t.second * 2 + tmp; n   return t; n } n long long get(long long x, long long k) { n   if (x == 0) n     return 0; n   else if (x == 1) n     return 1; n   pair<long long, long long> t = dfs(x / 2); n   if (k <= t.first) n     return get(x / 2, k); n   else if (k == t.first + 1) n     return t.second + (x & 1); n   else { n     return t.second + (x & 1) + get(x / 2, k - t.first - 1); n   } n } n int main() { n   scanf( %I64d%I64d%I64d , &n, &l, &r); n   printf( %I64d n , get(n, r) - (l - 1 == 0 ? 0 : get(n, l - 1))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200; n int s[maxn][6], n; n int cnt[maxn]; n int get_score(int id, int f, int a) { n   if (s[f][id] == -1) return 0; n   int sum = 0; n   if (s[1][id] == -1 || s[2][id] == -1 || s[1][id] < s[2][id]) n     sum = cnt[id]; n   else n     sum = cnt[id] + a; n   int score = 500; n   for (int i = 2; i <= 32; i *= 2) { n     if (sum * i > n + a) { n       break; n     } else { n       score += 500; n     } n   } n   return score * (250.0 - s[f][id]) * 250.0; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= 5; j++) scanf( %d , &s[i][j]); n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= 5; j++) n       if (s[i][j] != -1) cnt[j]++; n   int sum1 = 0, sum2 = 0; n   int ss = -1; n   for (int v = 0; v <= 120 * 32; v++) { n     sum1 = 0, sum2 = 0; n     for (int j = 1; j <= 5; j++) { n       sum1 += get_score(j, 1, v); n       sum2 += get_score(j, 2, v); n     } n     if (sum1 > sum2) { n       ss = v; n       break; n     } n   } n   printf( %d n , ss); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long a, b, sum = 0; n   cin >> a >> b; n   while (true) { n     if (a >= b) { n       sum += a / b; n       a %= b; n     } n     if (a == 0) { n       break; n     } n     swap(a, b); n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p = 1000000007; n long long fact[100000]; n long long dp[100000]; n long long factdp[100000]; n long long ar[200000]; n int main() { n   fact[0] = 1; n   fact[1] = 1; n   dp[0] = 1; n   dp[1] = 1; n   factdp[0] = 1; n   factdp[1] = 1; n   for (int i = 2; i < 10000; i++) { n     dp[i] = dp[p % i] * (p - (p / i)) % p; n     fact[i] = i * fact[i - 1] % p; n     factdp[i] = factdp[i - 1] * (dp[i]) % p; n   } n   long long n, m; n   cin >> n >> m; n   long long ans = n       (fact[2 * m + n - 1] * factdp[2 * m] % p * factdp[n - 1] % p + p) % p; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n long long cal(long long x) { return (x + 1) * x / 2; } n int main() { n   std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   long long l = 0, r = 1e8, ans = -1; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (cal(mid) < n) n       l = mid + 1, ans = mid; n     else n       r = mid - 1; n   } n   cout << n - cal(ans) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int def = 0; n   int a[N], d[N] = {}; n   for (int i = 0; i <= n; i++) { n     int a[N]; n     char c[10]; n     if (scanf( %d , &a[i])) n       d[i] = 1, def++; n     else n       scanf( %s , c); n   } n   if (k == 0) { n     if (d[0]) n       printf( %s n , a[0] == 0 ?  Yes  :  No ); n     else n       printf( %s n , def % 2 == 0 ?  No  :  Yes ); n   } else { n     if (def != n + 1) { n       printf( %s n , n % 2 ?  Yes  :  No ); n     } else { n       if (k == 1) { n         int resp = 0; n         for (int i = n; i >= 0; i--) resp += a[i]; n         printf( %s n , resp == 0 ?  Yes  :  No ); n       } else { n         long long r = 0; n         for (int i = 0; i <= n; i++) { n           r = r / k + a[i]; n           if (r % k) break; n         } n         printf( %s n , r == 0 ?  Yes  :  No ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = (1ll << 60) - 1; n long long int nCrModpDP(long long int n, long long int r, long long int p) { n   long long int i, j, C[r + 1]; n   memset(C, 0, sizeof(C)); n   C[0] = 1; n   for (i = 1; i <= n; i++) { n     for (j = min(i, r); j > 0; j--) { n       C[j] = (C[j] + C[j - 1]) % p; n     } n   } n   return C[r]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int tc = 1; n   while (tc--) { n     long long int fact[1001]; n     fact[0] = fact[1] = 1; n     fact[2] = 2; n     fact[3] = 6; n     for (long long int i = 4; i < 1001; i++) n       fact[i] = ((fact[i - 1] % 1000000007) * (i % 1000000007)) % 1000000007; n     long long int n, m; n     cin >> n >> m; n     long long int ans = nCrModpDP(n + 2 * m - 1, n - 1, 1000000007); n     cout << ans % 1000000007; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long ans; n int a[100002]; n int b[100002]; n bool jobs[100002]; n priority_queue<int> pq[100002]; n priority_queue<int> pq2; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     pq[a[i]].push(-b[i]); n     jobs[a[i]] = true; n   } n   for (int i = 1; i <= n; i++) { n     while (pq[i].size() > 1) { n       pq2.push(pq[i].top()); n       pq[i].pop(); n     } n   } n   for (int i = 1; i <= k; i++) { n     if (!jobs[i]) { n       ans -= pq2.top(); n       pq2.pop(); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   char c = getchar(); n   bool f = 0; n   while (c < 48) f |= c ==  - , c = getchar(); n   while (c > 47) x = x * 10 + (c ^ 48), c = getchar(); n   return f ? -x : x; n } n int n, k; n struct node { n   int sz, mn[10], mx[10]; n   bool operator<(const node& o) const { n     for (int i = (0), iend = (k - 1); i <= iend; ++i) n       if (mx[i] > o.mn[i]) return 0; n     return 1; n   } n }; n node merge(node A, node B) { n   node res; n   res.sz = A.sz + B.sz; n   for (int i = (0), iend = (k - 1); i <= iend; ++i) n     res.mn[i] = min(A.mn[i], B.mn[i]), res.mx[i] = max(A.mx[i], B.mx[i]); n   return res; n } n multiset<node> S; n int main() { n   cin >> n >> k; n   for (int _ = (1), _end = (n); _ <= _end; ++_) { n     node cur; n     cur.sz = 1; n     for (int i = (0), iend = (k - 1); i <= iend; ++i) n       cur.mn[i] = cur.mx[i] = read(); n     while (1) { n       auto it1 = S.lower_bound(cur), it2 = S.upper_bound(cur); n       if (it1 != it2) { n         cur = merge(*it1, cur), S.erase(it1); n       } else n         break; n     } n     S.insert(cur); n     printf( %d n , S.rbegin()->sz); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10, H = 2e5 + 10, MOD = 1e9 + 7; n struct node { n   int x, y; n } a[N]; n long long fac[H], inv[H], f[N]; n int h, w, n; n long long popow(long long x, int y) { n   long long t = x, re = 1; n   while (y) { n     if (y & 1) re = re * t % MOD; n     t = t * t % MOD; n     y >>= 1; n   } n   return re; n } n void prepare() { n   fac[0] = inv[0] = 1; n   for (int i = 1; i <= 2e5; ++i) { n     fac[i] = fac[i - 1] * i % MOD; n     inv[i] = popow(fac[i], MOD - 2); n   } n } n bool cmp(const node &aa, const node &bb) { n   return aa.x < bb.x || (aa.x == bb.x && aa.y < bb.y); n } n long long calc(int aa, int bb) { n   if (aa < bb) return 0; n   return fac[aa] * inv[bb] % MOD * inv[aa - bb] % MOD; n } n int main() { n   prepare(); n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].y); n   ++n; n   a[n].x = h; n   a[n].y = w; n   sort(&a[1], &a[1 + n], cmp); n   for (int i = 1; i <= n; ++i) { n     f[i] = calc(a[i].x + a[i].y - 2, a[i].x - 1); n     for (int j = 1; j < i; ++j) n       if (a[j].y <= a[i].y) { n         f[i] -= f[j] * calc(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x - a[j].x); n         f[i] %= MOD; n       } n   } n   printf( %lld n , (f[n] + MOD) % MOD); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const int MXN = 2e5 + 10; n const int MXS = MXN * 4; n const int INF = 1e9; n const int Mod = 998244353; n int n, q, num; n int Seg[MXS], MUL[MXS], ADD[MXS]; n set<pair<pair<int, int>, int> > st[MXN]; n void Shift(int id, int s, int e) { n   if (MUL[id] == 1 && ADD[id] == 0) return; n   Seg[id] = n       ((1ll * Seg[id] * MUL[id] % Mod) + (1ll * ADD[id] * (e - s + 1) % Mod)) % n       Mod; n   if ((e - s + 1) > 1) { n     ADD[(id * 2)] = ((1ll * ADD[(id * 2)] * MUL[id] % Mod) + ADD[id]) % Mod; n     ADD[(id * 2 + 1)] = n         ((1ll * ADD[(id * 2 + 1)] * MUL[id] % Mod) + ADD[id]) % Mod; n     MUL[(id * 2)] = 1ll * MUL[(id * 2)] * MUL[id] % Mod; n     MUL[(id * 2 + 1)] = 1ll * MUL[(id * 2 + 1)] * MUL[id] % Mod; n   } n   ADD[id] = 0, MUL[id] = 1; n } n void Upd(int l, int r, int add, int mul, int id = 1, int s = 1, int e = n) { n   Shift(id, s, e); n   if (e < l || s > r) return; n   if (l <= s && e <= r) { n     ADD[id] = add, MUL[id] = mul; n     Shift(id, s, e); n     return; n   } n   Upd(l, r, add, mul, (id * 2), s, ((s + e) / 2)), n       Upd(l, r, add, mul, (id * 2 + 1), ((s + e) / 2 + 1), e); n   Seg[id] = (Seg[(id * 2)] + Seg[(id * 2 + 1)]) % Mod; n } n int Get(int l, int r, int id = 1, int s = 1, int e = n) { n   Shift(id, s, e); n   if (e < l || s > r) return 0; n   if (l <= s && e <= r) return Seg[id]; n   return (Get(l, r, (id * 2), s, ((s + e) / 2)) + n           Get(l, r, (id * 2 + 1), ((s + e) / 2 + 1), e)) % n          Mod; n } n void Split(int tp, int p, bool f) { n   auto itr = st[tp].upper_bound({{p, INF}, 0}); n   itr--; n   int l, r, sit = itr->second; n   tie(l, r) = itr->first; n   if (!f && l == p) return; n   if (f && r == p) return; n   st[tp].erase(itr); n   st[tp].insert({{l, p - 1 + f}, sit}); n   st[tp].insert({{p + f, r}, sit}); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (int i = 0; i < MXS; i++) MUL[i] = 1; n   for (int i = 1; i <= n; i++) st[i].insert({{1, n}, 0}); n   for (int i = 0; i < q; i++) { n     int t, l, r, x; n     cin >> t >> l >> r; n     if (t != 2) cin >> x; n     if (t == 2) { n       cout << Get(l, r) <<   n ; n       continue; n     } n     Split(x, l, 0), Split(x, r, 1); n     auto itr = st[x].lower_bound({{l, l}, 0}); n     while (itr != st[x].end() && itr->first.second <= r) { n       int nl, nr, ns = itr->second; n       tie(nl, nr) = itr->first; n       if (ns) Upd(nl, nr, 0, 2); n       if (!ns) Upd(nl, nr, 1, 1); n       st[x].erase(itr); n       itr = st[x].lower_bound({{nr + 1, nr + 1}, 0}); n     } n     st[x].insert({{l, r}, 1}); n   } 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   ; n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   int ok = 0; n   for (int i = 0; i < n - 1; i++) { n     if (((long long int)arr[i] * 2) > arr[i + 1] and arr[i + 1] > arr[i]) n       ok = 1; n   } n   if (ok) n     cout <<  YES  <<   n ; n   else n     cout <<  NO  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[1000005], t = 0; n int main() { n   int n, cnt; n   cin >> n; n   map<long long int, int> m; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   cnt = 1; n   long long int t = arr[0]; n   m[t]++; n   for (int i = 1; i < n; i++) { n     t = t + arr[i]; n     m[t]++; n     cnt = max(cnt, m[t]); n   } n   cout << n - cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Dango { n const int MAXN = 200005, INF = 0x3f3f3f3f; n struct Edge { n   int from, to, dis, id; n   bool operator<(const Edge b) const { return dis < b.dis; } n } e[MAXN]; n struct tree { n   int l, r, data; n   bool tag; n } t[MAXN << 2]; n int n, m; n int head[MAXN], to[MAXN * 2], dis[MAXN * 2], nxt[MAXN * 2]; n int fa[MAXN]; n int ans[MAXN]; n int size[MAXN], maxson[MAXN], belong[MAXN], f[MAXN]; n int id[MAXN], link[MAXN], tmp[MAXN], d[MAXN]; n bool flag[MAXN]; n int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     f |= (ch ==  - ); n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f ? -x : x; n } n int ffind(int a) { return f[a] == a ? a : f[a] = ffind(f[a]); } n void add(int u, int v, int w) { n   static int cnt; n   cnt++; n   to[cnt] = v; n   dis[cnt] = w; n   nxt[cnt] = head[u]; n   head[u] = cnt; n } n void dfs1(int u, int f) { n   size[u] = 1; n   fa[u] = f; n   d[u] = d[f] + 1; n   for (int i = head[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (v == f) continue; n     tmp[v] = dis[i]; n     dfs1(v, u); n     size[u] += size[v]; n     if (size[v] > size[maxson[u]]) maxson[u] = v; n   } n } n void dfs2(int u, int f, int anc) { n   static int cnt; n   id[u] = ++cnt; n   link[cnt] = u; n   belong[u] = anc; n   if (maxson[u]) dfs2(maxson[u], u, anc); n   for (int i = head[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (v == f || v == maxson[u]) continue; n     dfs2(v, u, v); n   } n } n void build(int a, int l, int r) { n   t[a].l = l; n   t[a].r = r; n   t[a].tag = false; n   if (l == r) { n     t[a].data = tmp[link[l]]; n     return; n   } n   int mid = (l + r) >> 1; n   build(a << 1, l, mid); n   build(a << 1 | 1, mid + 1, r); n   t[a].data = max(t[a << 1].data, t[a << 1 | 1].data); n } n void pushdown(int a) { n   int l = a << 1, r = a << 1 | 1; n   t[a].tag = false; n   t[l].data = min(t[l].data, t[a].data); n   t[r].data = min(t[r].data, t[a].data); n   t[l].tag = t[r].tag = true; n } n void update(int a, int l, int r, int data) { n   if (t[a].l >= l && t[a].r <= r) { n     t[a].data = min(t[a].data, data); n     t[a].tag = true; n     return; n   } n   if (t[a].tag) pushdown(a); n   int mid = (t[a].l + t[a].r) >> 1; n   if (l <= mid) update(a << 1, l, r, data); n   if (r > mid) update(a << 1 | 1, l, r, data); n } n int querymax(int a, int l, int r) { n   if (t[a].l >= l && t[a].r <= r) return t[a].data; n   if (t[a].tag) pushdown(a); n   int mid = (t[a].l + t[a].r) >> 1, res = 0; n   if (l <= mid) res = max(res, querymax(a << 1, l, r)); n   if (r > mid) res = max(res, querymax(a << 1 | 1, l, r)); n   return res; n } n int querymin(int a, int l, int r) { n   if (t[a].l >= l && t[a].r <= r) return t[a].data; n   if (t[a].tag) pushdown(a); n   int mid = (t[a].l + t[a].r) >> 1, res = INF; n   if (l <= mid) res = min(res, querymin(a << 1, l, r)); n   if (r > mid) res = min(res, querymin(a << 1 | 1, l, r)); n   return res; n } n int solve(int u, int v) { n   int res = 0; n   while (belong[u] != belong[v]) { n     if (d[belong[u]] < d[belong[v]]) swap(u, v); n     res = max(res, querymax(1, id[belong[u]], id[u])); n     u = fa[belong[u]]; n   } n   if (u != v) { n     if (d[u] > d[v]) swap(u, v); n     res = max(res, querymax(1, id[u] + 1, id[v])); n   } n   return res - 1; n } n void fix(int u, int v, int w) { n   while (belong[u] != belong[v]) { n     if (d[belong[u]] < d[belong[v]]) swap(u, v); n     update(1, id[belong[u]], id[u], w); n     u = fa[belong[u]]; n   } n   if (u != v) { n     if (d[u] > d[v]) swap(u, v); n     update(1, id[u] + 1, id[v], w); n   } n } n int solve2(int u, int v) { n   int res = INF; n   while (belong[u] != belong[v]) { n     if (d[belong[u]] < d[belong[v]]) swap(u, v); n     res = min(res, querymin(1, id[belong[u]], id[u])); n     u = fa[belong[u]]; n   } n   if (u != v) { n     if (d[u] > d[v]) swap(u, v); n     res = min(res, querymin(1, id[u] + 1, id[v])); n   } n   if (res == INF) res = 0; n   return res - 1; n } n int work() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) f[i] = i; n   for (int i = 1; i <= m; i++) { n     e[i].from = read(); n     e[i].to = read(); n     e[i].dis = read(); n     e[i].id = i; n   } n   sort(e + 1, e + m + 1); n   for (int i = 1; i <= m; i++) { n     int u = e[i].from, v = e[i].to, w = e[i].dis; n     int a = ffind(u), b = ffind(v); n     if (a != b) { n       f[a] = b; n       add(u, v, w); n       add(v, u, w); n     } else n       flag[i] = true; n   } n   dfs1(1, 0); n   dfs2(1, 0, 1); n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     if (!flag[i]) continue; n     ans[e[i].id] = solve(e[i].from, e[i].to); n   } n   memset(tmp, INF, sizeof(tmp)); n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     if (!flag[i]) continue; n     fix(e[i].from, e[i].to, e[i].dis); n   } n   for (int i = 1; i <= m; i++) { n     if (flag[i]) continue; n     ans[e[i].id] = solve2(e[i].from, e[i].to); n   } n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n   return 0; n } n }  // namespace Dango n int main() { return Dango::work(); } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v[200010]; n int mark[200010]; n vector<pair<int, int> > sorted; n int Query[200010]; n int main() { n   int n, q, i, j; n   memset(mark, 0, sizeof(mark)); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     int x, y; n     scanf( %d , &x); n     scanf( %d , &y); n     v[x].push_back(make_pair(y, i)); n   } n   for (i = 1; i <= n; i++) { n     if ((int)v[i].size() == 0) continue; n     sorted.push_back(make_pair(v[i][v[i].size() - 1].first, i)); n   } n   sort(sorted.begin(), sorted.end()); n   scanf( %d , &q); n   while (q--) { n     int k, x; n     scanf( %d , &k); n     for (i = 0; i < k; i++) { n       scanf( %d , &Query[i]); n       mark[Query[i]] = 1; n     } n     for (i = (int)sorted.size() - 1; i >= 0; --i) n       if (!mark[sorted[i].second]) break; n     if (i < 0) { n       printf( 0 0 n ); n       for (i = 0; i < k; i++) mark[Query[i]] = 0; n       continue; n     } n     int p = sorted[i].second; n     int lo = 0, hi = v[p].size() - 1, mid; n     while (lo < hi) { n       mid = (lo + hi) / 2; n       int pos = v[p][mid].second, val = v[p][mid].first; n       int cnt = (int)v[p].size() - 1 - mid; n       for (i = 0; i < k; i++) { n         if ((int)v[Query[i]].size() == 0) continue; n         int pos = upper_bound(v[Query[i]].begin(), v[Query[i]].end(), n                               make_pair(val, 0)) - n                   v[Query[i]].begin(); n         cnt += (int)v[Query[i]].size() - pos; n       } n       if (cnt == n - 1 - pos) n         hi = mid; n       else n         lo = mid + 1; n     } n     printf( %d %d n , p, v[p][lo].first); n     for (i = 0; i < k; i++) mark[Query[i]] = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e18; n const long long maxn = 2e5, maxm = 1e6, M = 20; n struct girl { n   long long w, id; n   bool operator<(const girl a) const { return w > a.w; } n }; n pair<long long, long long> operator+(pair<long long, long long> a, n                                      pair<long long, long long> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n vector<girl> gf[maxn]; n vector<int> g[maxn]; n priority_queue<girl> Q[maxn]; n pair<long long, long long> Min[maxm]; n long long c[maxn], d[maxn], X[maxn][2], Y[maxn]; n long long f[maxn][22]; n long long nex[maxn], par[maxn], size[maxn], per[maxn]; n long long Z[maxm], pos[maxm]; n girl sta[maxn]; n long long tot, L, R; n long long n, m, q, i, j, x, y, z, la; n void partition(long long o, long long fa) { n   f[o][0] = fa; n   size[o] = 1; n   d[o] = d[fa] + 1; n   long long len = g[o].size(), i; n   for (i = 0; i < len; i++) { n     long long v = g[o][i]; n     if (v == fa) continue; n     partition(v, o); n     size[o] += size[v]; n     if (size[v] > size[nex[o]]) nex[o] = v; n   } n } n void dfs(long long o, long long fa) { n   X[o][0] = ++tot; n   Y[tot] = o; n   if (nex[o]) dfs(nex[o], o); n   long long len = g[o].size(), i; n   for (i = 0; i < len; i++) { n     long long v = g[o][i]; n     if (v == fa || v == nex[o]) continue; n     dfs(v, o); n   } n   X[o][1] = tot; n } n void getPar(long long o, long long fa) { n   if (nex[o]) getPar(nex[o], fa); n   par[o] = fa; n } n void update(long long o) { n   if (Min[2 * o] + make_pair(Z[2 * o], 0) < n       Min[2 * o + 1] + make_pair(Z[2 * o + 1], 0)) { n     Min[o] = Min[2 * o] + make_pair(Z[2 * o], 0); n     pos[o] = pos[2 * o]; n   } else { n     Min[o] = Min[2 * o + 1] + make_pair(Z[2 * o + 1], 0); n     pos[o] = pos[2 * o + 1]; n   } n } n void pushdown(long long o) { n   Z[2 * o] += Z[o]; n   Z[2 * o + 1] += Z[o]; n   Z[o] = 0; n } n void build(long long o, long long l, long long r) { n   if (l + 1 == r) { n     Q[l].push((girl){oo, 0}); n     long long len = gf[Y[l]].size(), i; n     for (i = 0; i < len; i++) Q[l].push(gf[Y[l]][i]); n     Min[o] = make_pair(Q[l].top().w, Y[l]); n     pos[o] = l; n     return; n   } n   long long m = (l + r) >> 1; n   build(2 * o, l, m); n   build(2 * o + 1, m, r); n   update(o); n } n void change(long long o, long long l, long long r, long long x, long long y, n             long long z) { n   if (x <= l && r <= y) { n     Z[o] += z; n     return; n   } n   pushdown(o); n   long long m = (l + r) >> 1; n   if (x < m) change(2 * o, l, m, x, y, z); n   if (m < y) change(2 * o + 1, m, r, x, y, z); n   update(o); n } n void find(long long o, long long l, long long r, long long x, long long y, n           pair<long long, long long> &MIN, long long &POS) { n   if (x <= l && r <= y) { n     if (Min[o] + make_pair(Z[o], 0) < MIN) { n       MIN = Min[o] + make_pair(Z[o], 0); n       POS = pos[o]; n     } n     return; n   } n   pushdown(o); n   long long m = (l + r) >> 1; n   if (x < m) find(2 * o, l, m, x, y, MIN, POS); n   if (m < y) find(2 * o + 1, m, r, x, y, MIN, POS); n   update(o); n } n void take(long long o, long long l, long long r, long long v, girl &re) { n   if (l + 1 == r) { n     re = Q[l].top(); n     re.w += Z[o]; n     Q[l].pop(); n     Min[o] = make_pair(Q[l].top().w, Y[l]); n     pos[o] = l; n     return; n   } n   pushdown(o); n   long long m = (l + r) >> 1; n   if (v < m) n     take(2 * o, l, m, v, re); n   else n     take(2 * o + 1, m, r, v, re); n   update(o); n } n void insert(long long o, long long l, long long r, long long v, girl re) { n   if (l + 1 == r) { n     re.w -= Z[o]; n     Q[l].push(re); n     Min[o] = make_pair(Q[l].top().w, Y[l]); n     pos[o] = l; n     return; n   } n   pushdown(o); n   long long m = (l + r) >> 1; n   if (v < m) n     insert(2 * o, l, m, v, re); n   else n     insert(2 * o + 1, m, r, v, re); n   update(o); n } n long long lca(long long x, long long y) { n   if (d[x] < d[y]) swap(x, y); n   long long i; n   for (i = M; i >= 0; i--) n     if (d[f[x][i]] >= d[y]) x = f[x][i]; n   if (x == y) return x; n   for (i = M; i >= 0; i--) n     if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; n   return f[x][0]; n } n girl find(long long u, long long v) { n   long long a = lca(u, v); n   pair<long long, long long> MIN = make_pair(oo, 0); n   long long POS = 0; n   for (long long i = 1; i <= 2; i++) { n     while (d[u] >= d[a]) { n       if (d[par[u]] >= d[a]) { n         find(1, L, R, X[par[u]][0], X[u][0] + 1, MIN, POS); n         u = f[par[u]][0]; n       } else { n         find(1, L, R, X[a][0], X[u][0] + 1, MIN, POS); n         u = f[a][0]; n       } n     } n     u = v; n   } n   girl re = (girl){oo, 0}; n   if (MIN == make_pair(oo, 0ll)) return re; n   take(1, L, R, POS, re); n   return re; n } n void solve(long long u, long long v, long long K) { n   long long i, j; n   for (i = 1; i <= K; i++) { n     sta[i] = find(u, v); n     if (sta[i].id == 0) break; n   } n   printf( %lld , i - 1); n   for (j = 1; j < i; j++) printf(  %lld , sta[j].id); n   printf(  n ); n } n bool cmp(long long a, long long b) { return d[a] < d[b]; } n int main() { n   scanf( %lld%lld%lld , &n, &m, &q); n   for (i = 1; i < n; i++) { n     scanf( %lld%lld , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (i = 1; i <= m; i++) { n     scanf( %lld , &x); n     c[i] = x; n     gf[x].push_back((girl){i, i}); n   } n   partition(1, 0); n   dfs(1, 0); n   for (i = 1; i <= n; i++) per[i] = i; n   sort(per + 1, per + n + 1, cmp); n   for (i = 1; i <= n; i++) n     if (!par[per[i]]) getPar(per[i], per[i]); n   for (i = 1; i <= M; i++) n     for (j = 1; j <= n; j++) f[j][i] = f[f[j][i - 1]][i - 1]; n   L = 1; n   R = n + 1; n   build(1, L, R); n   for (long long qq = 1; qq <= q; qq++) { n     scanf( %lld , &x); n     if (x == 1) { n       scanf( %lld%lld%lld , &x, &y, &z); n       solve(x, y, z); n     } else { n       scanf( %lld%lld , &x, &y); n       change(1, L, R, X[x][0], X[x][1] + 1, y); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[111]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , &arr[i]); n   sort(arr, arr + n); n   int t = 10; n   int ans = 0; n   int qts = 0; n   for (int i = (0); i < (n); ++i) { n     t += arr[i]; n     if (t > 720) break; n     qts++; n     if (t > 360) ans += (t - 360); n   } n   printf( %d %d n , qts, ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n int T, N; n int a[MAX]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &N); n     for (int i = 1; i <= N; i++) scanf( %d , &a[i]); n     sort(a + 1, a + 1 + N); n     int l = (N + 1) / 2, r = (N + 1) / 2; n     printf( %d , a[l]); n     for (int i = 1; i < N; i++) { n       if (i & 1) n         printf(  %d , a[++r]); n       else n         printf(  %d , a[--l]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b); n int main() { n   long long int a, b, c, i, j, x, k, sum, count; n   scanf( %lld , &a); n   scanf( %lld , &b); n   scanf( %lld , &c); n   long long int arr[81]; n   count = 0; n   for (i = 1; i < 82; i++) { n     long long int t = power(i, a); n     k = ((b * t) + c); n     if (k <= 0 && k >= 1000000000) continue; n     x = k; n     sum = 0; n     while (x != 0) { n       j = x % 10; n       x = x / 10; n       sum = sum + j; n     } n     if (sum == i && k > 0 && k < 1000000000) { n       count++; n       arr[count - 1] = k; n     } n   } n   cout << count; n   if (count) { n     cout << endl; n     for (i = 0; i < count; i++) { n       cout << arr[i] <<    ; n     } n   } n   return 0; n } n long long int power(long long int a, long long int b) { n   long long int tpower = 1; n   for (int i = 0; i < b; i++) { n     tpower = tpower * a; n   } n   return tpower; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool r[100], c[100]; n int main() { n   cin >> n; n   vector<int> ans; n   for (int i = 1; i <= n * n; i++) { n     int x, y; n     cin >> x >> y; n     if (!r[x] && !c[y]) { n       ans.push_back(i); n       r[x] = true; n       c[y] = true; n     } n   } n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i; n   char ch; n   char str[2010]; n   scanf( %d , &n); n   scanf( %c , &ch); n   if (n % 2 == 1) { n     scanf( %c , &str[n / 2]); n     for (i = 1; i <= n / 2; i++) { n       scanf( %c , &str[n / 2 - i]); n       scanf( %c , &str[n / 2 + i]); n     } n   } else { n     scanf( %c , &str[n / 2 - 1]); n     for (i = 1; i < n / 2; i++) { n       scanf( %c , &str[n / 2 - 1 + i]); n       scanf( %c , &str[n / 2 - 1 - i]); n     } n     scanf( %c , &str[n - 1]); n   } n   str[n] =   0 ; n   printf( %s n , str); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n const int N=3e5+10; n int n,m,mx; n int dif[N],b[N];  n vector <int> a[N]; n void end(){ n  puts( No ); exit(0);  n } n int main(){ n  scanf( %d%d ,&n,&m); n  for(int i=1;i<=n;i++){ n   a[i].push_back(0); n   for(int j=1;j<=m;j++){ int x; scanf( %d ,&x); a[i].push_back(x); }  n  } n  for(int i=2;i<=n;i++){ n   for(int j=1;j<=m;j++) if(a[1][j]!=a[i][j]) dif[i]++;   n   mx=max(mx,dif[i]);  n  } n  if(mx>4) end();  n  if(mx<=2){ n   puts( Yes ); n   for(int i=1;i<=m;i++) printf( %d  ,a[1][i]); exit(0);  n  } n  if(mx==4){ n   int ps=0,now=0,cur[4]={};  n   for(int i=2;i<=n;i++) if(dif[i]==4){ ps=i; break; } n   for(int i=1;i<=m;i++) if(a[1][i]!=a[ps][i]) cur[now++]=i;  n    n   for(int i=0;i<4;i++) n    for(int j=i+1;j<4;j++){  n     for(int k=1;k<=m;k++) b[k]=a[1][k]; n     for(int k=0;k<4;k++){ n      if(k!=i && k!=j) b[cur[k]]=a[ps][cur[k]];  n     } n      n     int pd=0; n     for(int k=1;k<=n;k++){ n      int sum=0; for(int p=1;p<=m;p++) if(a[k][p]!=b[p]) sum++;  n      if(sum>2){ pd=1; break; } n     } n     if(!pd){ n      puts( Yes ); n      for(int k=1;k<=m;k++) printf( %d  ,b[k]); exit(0);  n     } n    }  n   end();  n  } n  else if(mx==3){  n   int ps=0,now=0,cur[3]={}; n   for(int i=2;i<=n;i++) if(dif[i]==3){ ps=i; break; } n   for(int i=1;i<=m;i++) if(a[1][i]!=a[ps][i]) cur[now++]=i;  n    n   for(int p=0;p<3;p++) n    for(int q=p+1;q<3;q++){ n     for(int d=0;d<2;d++){ n      for(int i=1;i<=m;i++) b[i]=a[1][i];  n      if(d==0){ n       b[cur[q]]=a[ps][cur[q]];  n      } n      else{ n       b[cur[p]]=a[ps][cur[p]];   n      } n       n      int pd=0,vis=0,at=0; n      for(int i=2;i<=n;i++){ n       int sum=0; for(int j=1;j<=m;j++) if(a[i][j]!=b[j]) sum++; n       if(sum>=3){ vis=1; at=i; break; } n      } n      if(!vis){ n       puts( Yes );  n       for(int i=1;i<=m;i++) printf( %d  ,b[i]); exit(0); n      } n      else{ n       b[cur[3-p-q]]=a[at][cur[3-p-q]];  n       for(int i=2;i<=n;i++){ n        int sum=0; for(int j=1;j<=m;j++) if(a[i][j]!=b[j]) sum++;  n        if(sum==3){ pd=1; break; } n       } n       if(!pd){ n        puts( Yes );  n        for(int i=1;i<=m;i++) printf( %d  ,b[i]); exit(0);        n       } n      } n     } n    } n    n   end();  n  } n    n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   int uppery = y2 + 1; n   int lowery = y2 - 1; n   int upperx = x2 + 1; n   int lowerx = x2 - 1; n   int ans = 4; n   ans += n       abs(x1 - upperx) + abs(y1 - lowery) + abs(x1 - lowerx) + abs(y1 - uppery); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a) { n   long long int pdt = 1, i = 0, t = 19; n   while (i++ < min(a, t)) pdt *= 2; n   return pdt; n } n int main() { n   long long int n, b, i, j, cnt = 0, k = 0; n   cin >> n >> b; n   long long int a[n], c[n]; n   b = power((b * 8) / n); n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     j = i + 1; n     while (j < n && a[j] == a[i]) j++; n     cnt++; n     c[k++] = j - i; n     i = j - 1; n   } n   if (cnt < b) n     cout << 0; n   else { n     long long int maxi = 0; n     for (i = 0; i < k; i++) { n       if (i > 0) c[i] += c[i - 1]; n       if (i - b >= 0) n         maxi = max(maxi, c[i] - c[i - b]); n       else if (!(i - b + 1)) n         maxi = max(maxi, c[i]); n     } n     cout << n - maxi << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200005], s1[200005]; n void sol() { n   int i, l, xp; n   l = strlen(s); n   memset(s1, 0, sizeof(s1)); n   xp = -1; n   for (i = 0; i < l; i++) { n     if (xp == -1) { n       xp++; n       s1[xp] = s[i]; n       continue; n     } n     if (s1[xp] == s[i]) n       xp--; n     else { n       xp++; n       s1[xp] = s[i]; n     } n   } n   xp++; n   s1[xp] = 0; n   printf( %s n , s1); n } n int main() { n   while (gets(s)) sol(); n   return 0; n } n


#include <bits/stdc++.h>  n using namespace std; n typedef long long ll; n int main()  n { n  ll t; n  cin>>t; n  while(t--){ n   ll n,k; n   cin>>n>>k; n   ll ans; n   if(n%2==0) n   { n    if(k%n==0) n    ans=n; n    else n    ans=k%n; n   } n   else { n    ll x1=k%n; n    if(x1==0) n    x1=n; n    ll x2=(k/n); n    if(k%n!=0) n    x2++; n    ll x3=x2%(n/2); n    if(x3==0) n    x3=n/2; n    ll x4=(x3-1)*n+x1; n    ll x5=x4/(n/2); n    if(x4%(n/2)==0) n    x5--; n    ans=(x1+x5)%n; n    if(ans==0) n    ans=n; n   } n   cout<<ans<<endl; n  } n  return 0; n }  n    n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LLINF = 1e18; n const int MAXN = 1e5; n int v, q; n vector<int> G[MAXN]; n int min_dist[MAXN]; n bool is_centroid[MAXN]; n int sz[MAXN]; n int father[MAXN]; n int dfs_sz(int x, int f = -1) { n   sz[x] = 1; n   for (auto y : G[x]) { n     if (y == f || is_centroid[y]) continue; n     sz[x] += dfs_sz(y, x); n   } n   return sz[x]; n } n int dfs_cd(int x, int cent_sz, int f = -1) { n   for (auto y : G[x]) { n     if (y == f || is_centroid[y]) continue; n     if (sz[y] > cent_sz / 2) return dfs_cd(y, cent_sz, x); n   } n   return x; n } n void cent_descomp(int x, int f = -1) { n   int cent_sz = dfs_sz(x); n   int centroid = dfs_cd(x, cent_sz); n   is_centroid[centroid] = true; n   father[centroid] = f; n   for (auto y : G[centroid]) { n     if (is_centroid[y]) continue; n     cent_descomp(y, centroid); n   } n } n int height[MAXN]; n void dfs_h(int x, int f = -1) { n   for (auto y : G[x]) { n     if (f == y) continue; n     height[y] = height[x] + 1; n     dfs_h(y, x); n   } n } n int l; n int timer; n int tin[MAXN], tout[MAXN]; n int up[MAXN][25]; n void dfs(int v, int p) { n   tin[v] = ++timer; n   up[v][0] = p; n   for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1]; n   for (int u : G[v]) { n     if (u != p) dfs(u, v); n   } n   tout[v] = ++timer; n } n bool is_ancestor(int u, int v) { n   return tin[u] <= tin[v] && tout[u] >= tout[v]; n } n int lca(int u, int v) { n   if (is_ancestor(u, v)) return u; n   if (is_ancestor(v, u)) return v; n   for (int i = l; i >= 0; --i) { n     if (!is_ancestor(up[u][i], v)) u = up[u][i]; n   } n   return up[u][0]; n } n void preprocess(int root) { n   timer = 0; n   l = ceil(log2(v)); n   dfs(root, root); n } n int get_d(int x, int y) { n   return abs(height[x] + height[y] - 2 * height[lca(x, y)]); n } n void upd(int x) { n   int y = x; n   while (true) { n     min_dist[y] = min(min_dist[y], get_d(x, y)); n     if (-1 == father[y]) break; n     y = father[y]; n   } n } n int query(int x) { n   int y = x; n   int ans = INF; n   while (true) { n     ans = min(ans, min_dist[y] + get_d(x, y)); n     if (-1 == father[y]) break; n     y = father[y]; n   } n   return ans; n } n int main() { n   scanf( %d %d , &v, &q); n   for (int i = 1; i < v; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x--, y--; n     G[x].push_back(y); n     G[y].push_back(x); n     min_dist[i - 1] = INF; n   } n   min_dist[v - 1] = INF; n   cent_descomp(0); n   for (int i = 0; i < v; i++) { n     if (father[i] == -1) { n       dfs_h(i); n       preprocess(i); n     } n   } n   upd(0); n   for (int _ = 0; _ < q; _++) { n     int t, x; n     scanf( %d %d , &t, &x); n     x--; n     if (t == 1) n       upd(x); n     else n       printf( %d n , query(x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int RLEN = 1 << 18 | 1; n inline char nc() { n   static char ibuf[RLEN], *ib, *ob; n   (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin)); n   return (ib == ob) ? -1 : *ib++; n } n inline int rd() { n   char ch = nc(); n   int i = 0, f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = nc(); n   } n   while (isdigit(ch)) { n     i = (i << 1) + (i << 3) + ch -  0 ; n     ch = nc(); n   } n   return i * f; n } n const int N = 1e5 + 50; n char ch[N]; n int n, a[N], pre[N][26], suf[N][26]; n int f[N][26], g[N], mem[27]; n inline int mex(int sta) { return __builtin_ctz(~sta); } n inline int calc(int l, int r, int c, int flag) { n   if (l > r) return 0; n   if (~mem[c]) return mem[c]; n   int sta = 0; n   for (int j = 0; j < 26; ++j) { n     int _l = suf[l][j], _r = pre[r][j]; n     if (_r >= l) n       sta |= n           1 << (g[_r] ^ g[_l] ^ (!flag ? f[_l - 1][c] : calc(l, _l - 1, j, 1)) ^ n                 (flag ? f[r][j] : calc(_r + 1, r, j, 0))); n   } n   return mem[c] = mex(sta); n } n int main() { n   scanf( %s , ch + 1); n   n = strlen(ch + 1); n   for (int i = 1; i <= n; i++) a[i] = ch[i] -  a ; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 26; ++j) pre[i][j] = pre[i - 1][j]; n     pre[i][a[i]] = i; n   } n   for (int j = 0; j < 26; ++j) suf[n + 1][j] = n + 1; n   for (int i = n; i >= 1; i--) { n     for (int j = 0; j < 26; ++j) suf[i][j] = suf[i + 1][j]; n     suf[i][a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     g[i] = f[i - 1][a[i]] ^ g[pre[i - 1][a[i]]]; n     memset(mem, -1, sizeof(mem)); n     for (int j = 0; j < 26; ++j) f[i][j] = calc(pre[i][j] + 1, i, j, 0); n   } n   int q = rd(); n   for (int i = 1; i <= q; i++) { n     int l = rd(), r = rd(); n     memset(mem, -1, sizeof(mem)); n     if (calc(l, r, 26, 1)) n       puts( Alice ); n     else n       puts( Bob ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   while (cin >> n) { n     string x; n     cin >> x; n     long long c = 0; n     long long arr[n + 5]; n     memset(arr, 0, sizeof(arr)); n     for (long long i = 0; i < n; i++) { n       if (x[i] ==  < ) { n         if ((i - 1) < 0 || arr[i - 1] == 1) arr[i] = 1; n       } else if (x[i] ==  > ) { n         long long lft = i; n         while (i < n && x[i] ==  > ) i++; n         if (i >= n) { n           long long rgt = i; n           c += (rgt - lft); n         } else n           i--; n       } n     } n     for (long long i = 0; i < n; i++) n       if (arr[i] == 1) c++; n     cout << c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sum; n vector<int> x; n int main() { n   int n; n   cin >> n; n   x.push_back(-2099999999); n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     x.push_back(a); n   } n   sort(x.begin(), x.end()); n   for (int i = 1; i <= n; i++) sum += abs(i - x[i]); n   cout << sum << endl; n } n


#include <bits/stdc++.h> n int b; n struct Matrix { n   long long int x[110][110]; n } A, B, C; n Matrix operator*(Matrix A, Matrix B) { n   Matrix C; n   for (int i = 1; i <= b; i++) { n     for (int j = 1; j <= b; j++) { n       C.x[i][j] = 0; n       for (int k = 1; k <= b; k++) { n         C.x[i][j] += A.x[i][k] * B.x[k][j]; n         C.x[i][j] %= 1000000007; n       } n     } n   } n   return C; n } n void func(long long int k) { n   for (int i = 1; i <= b; i++) n     for (int j = 1; j <= b; j++) B.x[i][j] = C.x[i][j] = 0; n   for (int i = 1; i <= b; i++) B.x[i][i] = C.x[i][i] = 1; n   C = A; n   while (k) { n     if (k % 2 == 1) B = B * C; n     k /= 2; n     C = C * C; n   } n } n int main() { n   long long int a; n   scanf( %lld%d , &a, &b); n   for (int i = 1; i <= b; i++) n     for (int j = 1; j <= b; j++) A.x[i][j] = 0; n   A.x[1][1] = 1, A.x[1][b] = 1; n   for (int i = 2; i <= b; i++) A.x[i][i - 1] = 1; n   if (a < b) { n     printf( 1 ); n     return 0; n   } n   func(a - b); n   long long int s = 0; n   for (int i = 1; i <= b; i++) s += B.x[1][i], s %= 1000000007; n   s += B.x[1][1], s %= 1000000007; n   printf( %lld , s); n } n


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


#include <bits/stdc++.h> n using namespace std; n long long holes[14]; n int main() { n   long long zeroes = 0; n   for (long long i = 0; i < 14; i++) { n     cin >> holes[i]; n     if (holes[i] == 0) zeroes++; n   } n   zeroes++; n   long long mx = 0; n   for (long long i = 0; i < 14; i++) { n     long long cnt = 0; n     if (holes[i] != 0) { n       long long a = holes[i] / 14; n       long long b = holes[i] % 14; n       if (a % 2 == 0) { n         cnt = zeroes * a; n         while (b) { n           if (holes[(i + b) % 14] == 0) n             cnt -= a; n           else n             cnt += (a + holes[(i + b) % 14] + 1); n           b--; n         } n       } else { n         cnt = a * (14 - zeroes); n         for (long long j = 0; j < 14; j++) n           if (i != j) cnt += holes[j]; n         while (b) { n           if (holes[(i + b) % 14] == 0) n             cnt += a + 1; n           else n             cnt -= (a + holes[(i + b) % 14]); n           b--; n         } n       } n     } n     mx = max(cnt, mx); n   } n   cout << mx; n } 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 template <class A> n void read(vector<A>& v); n template <class A, size_t S> n void read(array<A, S>& a); n template <class T> n void read(T& x) { n   cin >> x; n } n void read(double& d) { n   string t; n   read(t); n   d = stod(t); n } n void read(long double& d) { n   string t; n   read(t); n   d = stold(t); n } n template <class H, class... T> n void read(H& h, T&... t) { n   read(h); n   read(t...); n } n template <class A> n void read(vector<A>& x) { n   for (auto& a : x) read(a); n } n template <class A, size_t S> n void read(array<A, S>& x) { n   for (auto& a : x) read(a); n } n const int MAXN = 2e5 + 5; n long long mod = 1e9 + 7; n vector<long long> gr[200005]; n void solve() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long val = (b * b) - (4 * a * c); n   cout << setprecision(8); n   if (a == 0) { n     if ((b == 0)) { n       if (c == 0) { n         cout << -1 <<   n ; n         return; n       } n       cout << 0 <<   n ; n     } else { n       cout << 1 <<   n ; n       long double ans = -(c * 1.0) / (b * 1.0); n       cout << ans <<   n ; n     } n     return; n   } n   if (val < 0) { n     cout << 0 <<   n ; n     return; n   } else { n     long double root1 = -b + sqrt(val); n     root1 /= 2.0 * a; n     long double root2 = -b - sqrt(val); n     root2 /= 2.0 * a; n     if (root1 == root2) { n       cout << 1 <<   n ; n       cout << root1 <<   n ; n     } else { n       cout << 2 <<   n ; n       if (root1 < root2) { n         cout << root1 <<   n  << root2 <<   n ; n       } else { n         cout << root2 <<   n  << root1 <<   n ; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test; n   test = 1; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e13; n const int MAGIC = 3500000; n long long add(long long x, long long y) { return (x + y) % MOD; } n long long mul(long long x, long long y) { n   long long coef = (long double)(x)*y / MOD + 0.5; n   long long ret = x * y - coef * MOD; n   ret += ret < 0 ? MOD : 0; n   return ret; n } n struct mat { n   long long dat[2][2]; n   mat() { memset(dat, 0, sizeof(dat)); } n } I, J; n mat operator*(const mat& A, const mat& B) { n   mat ret; n   for (int i = 0; i < (int)(2); i++) n     for (int j = 0; j < (int)(2); j++) n       ret.dat[i][j] = n           add(mul(A.dat[i][0], B.dat[0][j]), mul(A.dat[i][1], B.dat[1][j])); n   return ret; n } n void init() { n   I.dat[0][0] = I.dat[1][1] = 1; n   J.dat[1][0] = J.dat[0][1] = J.dat[1][1] = 1; n } n long long calc(long long pos) { n   mat ans = I, cur = J; n   while (pos > 0) { n     if (pos & 1) ans = ans * cur; n     cur = cur * cur; n     pos >>= 1; n   } n   return ans.dat[1][0]; n } n long long pw[14], cyc[14]; n long long X; n int main() { n   init(); n   scanf( %lld , &X); n   pw[0] = 1; n   for (int i = 1; i <= (int)(13); i++) pw[i] = 10 * pw[i - 1]; n   cyc[0] = 1; n   cyc[1] = 60; n   cyc[2] = 300; n   cyc[3] = 1500; n   for (int i = 4; i <= 13; i++) cyc[i] = 10 * cyc[i - 1]; n   vector<long long> occ, nocc; n   occ.push_back(0); n   for (int i = 1; i <= (int)(13); i++) { n     int wt = cyc[i] / cyc[i - 1]; n     nocc.clear(); n     for (int j = 0; j < (int)(occ.size()); j++) n       for (int k = 0; k < (int)(wt); k++) n         if (calc(occ[j] + k * cyc[i - 1]) % pw[i] == X % pw[i]) n           nocc.push_back(occ[j] + k * cyc[i - 1]); n     occ = nocc; n   } n   sort(occ.begin(), occ.end()); n   printf( %lld n , occ.empty() ? -1 : occ[0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 600010; n const int Mod = 998244353; n inline int sub(int x, int y) { return (x - y < 0 ? x - y + Mod : x - y); } n inline int add(int x, int y) { return (x + y >= Mod ? x + y - Mod : x + y); } n int po(int x, int y) { n   int r = 1; n   for (; y; y >>= 1, x = (1ll * x * x % Mod)) n     if (y & 1) r = (1ll * r * x % Mod); n   return r; n } n namespace poly { n int r[N], a[N], b[N], w[N], iw[N], m, l; n void init(int x) { n   for (m = 1, l = 0; m <= x; m <<= 1, ++l) n     ; n   for (int i = 0; i < m; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)); n   int b = po(3, (Mod - 1) / m), ib = po(b, Mod - 2); n   w[m / 2] = iw[m / 2] = 1; n   for (int i = 1; i < m / 2; ++i) n     w[m / 2 + i] = (1ll * w[m / 2 + i - 1] * b % Mod), n               iw[m / 2 + i] = (1ll * iw[m / 2 + i - 1] * ib % Mod); n   for (int i = m / 2 - 1; i; --i) w[i] = w[i << 1], iw[i] = iw[i << 1]; n } n void ntt(int* a, int f) { n   for (int i = 0; i < m; ++i) n     if (i < r[i]) swap(a[i], a[r[i]]); n   for (int i = 1, id = 1; i < m; i <<= 1, ++id) { n     for (int j = 0; j < m; j += i << 1) n       for (int k = 0; k < i; ++k) { n         int x = a[j + k], n             y = (1ll * (f == 1 ? w[i + k] : iw[i + k]) * a[i + j + k] % Mod); n         a[j + k] = add(x, y), a[i + j + k] = sub(x, y); n       } n   } n   if (f == -1) { n     int in = po(m, Mod - 2); n     for (int i = 0; i < m; ++i) a[i] = (1ll * a[i] * in % Mod); n   } n } n void prod(int* a, int* b, int len) { n   init(len); n   ntt(a, 1); n   ntt(b, 1); n   for (int i = 0; i < m; ++i) a[i] = (1ll * a[i] * b[i] % Mod); n   ntt(a, -1); n } n }  // namespace poly n using poly::a; n using poly::b; n using poly::prod; n int fac[N], inv[N]; n void init() { n   const int m = 3e5; n   fac[0] = 1; n   for (int i = 1; i <= m; ++i) fac[i] = (1ll * fac[i - 1] * i % Mod); n   inv[m] = po(fac[m], Mod - 2); n   for (int i = m - 1; i >= 0; i--) inv[i] = (1ll * i + 1 * inv[i + 1] % Mod); n } n vector<int> ve[N]; n priority_queue<pair<int, int> > Q; n int cq; n struct Edge { n   int to, next; n } edge[N << 1]; n int head[N], num; n void add_edge(int a, int b) { n   edge[++num] = (Edge){b, head[a]}; n   head[a] = num; n } n void dfs(int x, int f) { n   int cnt = 0; n   for (int i = head[x]; i; i = edge[i].next) n     if (edge[i].to != f) { n       dfs(edge[i].to, x); n       ++cnt; n     } n   if (cnt) { n     ++cq; n     ve[cq].push_back(1); n     ve[cq].push_back(cnt); n     Q.push(make_pair(-1, cq)); n   } n } n int main() { n   init(); n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     add_edge(x, y); n     add_edge(y, x); n   } n   dfs(1, 0); n   for (int i = 1; i < cq; ++i) { n     int t1 = Q.top().second; n     Q.pop(); n     int t2 = Q.top().second; n     Q.pop(); n     int la = ve[t1].size() - 1, lb = ve[t2].size() - 1; n     for (int j = 0; j <= la; j++) a[j] = ve[t1][j]; n     for (int j = 0; j <= lb; j++) b[j] = ve[t2][j]; n     prod(a, b, la + lb + 2); n     ve[t1].clear(); n     for (int j = 0; j <= la + lb; j++) ve[t1].push_back(a[j]); n     for (int i = 0; i <= poly ::m; i++) a[i] = b[i] = 0; n     Q.push(make_pair(-la - lb, t1)); n   } n   int rt = Q.top().second, len = -Q.top().first; n   int ans = 0; n   for (int i = 0; i <= len; ++i) { n     int tmp = (1ll * ve[rt][i] * fac[n - i] % Mod); n     ans = (i & 1) ? sub(ans, tmp) : add(ans, tmp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10, mod = 1e9 + 7; n const long long inf = 1e18; n int x[maxn], frs[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, z; n   cin >> n >> z; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n   } n   sort(x, x + n); n   int pt = 0; n   for (int i = 0; i < n; i++) { n     pt = max(pt, i + 1); n     while (pt < n && x[pt] - x[i] < z) pt++; n     frs[i] = pt; n   } n   int l = 0, r = (n / 2) + 1; n   while (r - l > 1) { n     int mid = (l + r) >> 1; n     int pt = mid - 1; n     for (int i = 0; i < mid; i++) { n       pt = max(pt + 1, frs[i]); n     } n     if (pt >= n) n       r = mid; n     else n       l = mid; n   } n   return cout << l << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n vector<vector<int> > dp; n vector<int> res; n void recalc(char col) { n   dp = vector<vector<int> >(n + 1, vector<int>(n + 1, 0)); n   res = vector<int>(n, 0); n   for (int(i) = 0; (i) < (n + 1); ++(i)) dp[i][0] = 0; n   for (int(ncur) = (1); (ncur) < (n + 1); ++(ncur)) { n     for (int(mcur) = (0); (mcur) < (n + 1); ++(mcur)) { n       if (s[ncur - 1] == col) { n         dp[mcur][ncur] = dp[mcur][ncur - 1] + 1; n         continue; n       } n       if (mcur == 0) { n         dp[mcur][ncur] = 0; n       } else { n         dp[mcur][ncur] = dp[mcur - 1][ncur - 1] + 1; n       } n     } n   } n   for (int(i) = 0; (i) < (n); ++(i)) { n     for (int(j) = (1); (j) < (n + 1); ++(j)) { n       res[i] = max(res[i], dp[i + 1][j]); n     } n   } n } n void solve() { n   cin >> n; n   cin >> s; n   int q; n   cin >> q; n   vector<pair<pair<char, int>, int> > qrs(q); n   for (int(i) = 0; (i) < (q); ++(i)) { n     cin >> qrs[i].first.second >> qrs[i].first.first; n     qrs[i].second = i; n   } n   sort(qrs.begin(), qrs.end()); n   vector<pair<int, int> > ans(q); n   char lastcol = -1; n   for (int(i) = 0; (i) < (q); ++(i)) { n     char col = qrs[i].first.first; n     int m = qrs[i].first.second; n     if (col != lastcol) { n       lastcol = col; n       recalc(col); n     } n     ans[i].first = qrs[i].second; n     ans[i].second = res[m - 1]; n   } n   sort(ans.begin(), ans.end()); n   for (int(i) = 0; (i) < (q); ++(i)) cout << ans[i].second << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int N = 1000010; n long long fact[N]; n int main(int argc, char const *argv[]) { n   long long n, m; n   cin >> n >> m; n   if (n == 1 && m == 1) { n     cout <<  1 n ; n     return 0; n   } n   double ans = 1.0 / (double)n + n                (((n - 1.0) / (double)n) * (m - 1.0) / (n * 1.0 * m - 1.0)); n   printf( %.9lf n , ans); n   return 0; n } n


#include <bits/stdc++.h>  n using namespace std; n  n using ll = long long; n using ld = long double; n #define fi first n #define se second n  n void solve(){ n  ll n; n  cin>>n; n  vector<ll>d(n); n  for(int i=0;i<n;i++) n   cin>>d[i]; n  ll ans=0,curr=0; n  sort(d.begin(),d.end()); n  for(int i=1;i<d.size()-1;i++){ n   ans+=(i*d[i])-curr; n   curr+=d[i]; n  } n  ll l = d.size(); n  if(l>=3) n   ans+=((l-2)*d[l-1])-curr; n  cout<<-1*ans<<  n ; n } n  n int main()  n {  n  ios::sync_with_stdio(false); n  cin.tie(0); n  n  int t; n  cin>>t; n  while(t--){ n   solve(); n  }  n  n  return 0;  n } n  n


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


#include <bits/stdc++.h> n using namespace std; n bool check(vector<long long> a, long long m, long long n) { n   vector<long long> v(n + 1, 0); n   for (long long i = 0; i < n; i++) { n     v[a[i]] += 1; n   } n   set<pair<long long, long long>, greater<pair<long long, long long>>> s; n   for (long long i = 1; i < n + 1; i++) { n     if (v[i]) { n       s.insert({v[i], i}); n     } n   } n   vector<long long> b; n   for (long long i = 0; i < n; i++) { n     if (i >= m and v[b[i - m]]) { n       s.insert({v[b[i - m]], b[i - m]}); n     } n     if (s.empty()) return false; n     pair<long long, long long> p = *(s.begin()); n     b.push_back(p.second); n     s.erase(p); n     v[p.second] -= 1; n   } n   return true; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<long long> a(n), b(m); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < m; i++) cin >> b[i]; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   if (a[n - 1] > b[0]) { n     cout <<  -1 n ; n     return 0; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) ans += a[i] * m; n   long long g = n - 1, h = m - 1; n   bool is = false; n   for (long long i = m - 1; i >= 0; i--) { n     while (g >= 0 && h == 0) { n       g--; n       h = m - 1; n     } n     if (b[i] == a[n - 1]) { n       break; n     } n     if (g < 0) { n       cout <<  -1 n ; n       return 0; n     } n     ans += b[i] - a[g]; n     h--; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   int a[100000]; n   int l0, l1; n   int i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   l0 = 0; n   l1 = 0; n   for (i = 1; i < n; i++) { n     if (a[i] >= a[i - 1]) { n       l1++; n     } n     if (a[i] < a[i - 1]) { n       if (l0 < l1) { n         l0 = l1; n       } n       l1 = 0; n     } n   } n   if (l0 < l1) { n     l0 = l1; n   } n   printf( %d , l0 + 1); n   return 0; n } n


#include <bits/stdc++.h> n int t, n, a[200010], tot, cnt[200010]; n int f[200010]; n int find_f(int x) { return f[x] ^ x ? f[x] = find_f(f[x]) : x; } n int main() { n   scanf( %d , &t); n   for (int _ = 0; _ < t; ++_) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) scanf( %d , a + i); n     for (int i = 1; i <= n; ++i) f[i] = i, cnt[i] = 0; n     for (int i = 1; i <= n; ++i) n       if (find_f(i) != find_f(a[i])) f[find_f(i)] = find_f(a[i]); n     for (int i = 1; i <= n; ++i) ++cnt[find_f(i)]; n     for (int i = 1; i <= n; ++i) printf( %d  , cnt[find_f(i)]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   vector<int> a(n, 0); n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> m; n   vector<int> b(m, 0); n   for (int i = 0; i < m; i++) cin >> b[i]; n   int count = 0; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (abs(a[i] - b[j]) <= 1) { n         count++; n         b[j] = INT_MAX; n         break; n       } n     } n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int add(int a, int b, int c) { n   a += b; n   if (a >= c) n     a -= c; n   else if (a < 0) n     a += c; n   return a; n } n inline int mul(int a, int b, int c) { return (long long)a * b % c; } n int pow(int a, long long b, int c) { n   int res = 1; n   for (; b; b /= 2) { n     if (b % 2) res = mul(res, a, c); n     a = mul(a, a, c); n   } n   return res; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int k, p; n     long long l, r; n     scanf( %d%I64d%I64d%d , &k, &l, &r, &p); n     if (p == 2) { n       printf( %d n , !(k % 2)); n       continue; n     } n     int prod = 1; n     if (k % p) { n       int e = pow(2, l, p - 1); n       int t = pow(k, e, p); n       if (t == 1) n         prod = pow(2, r - l + 1, p); n       else { n         int inv = pow(add(t, -1, p), p - 2, p); n         int num = n             add(pow(t, add(pow(2, r - l + 1, p - 1), -1, p - 1), p), -1, p); n         prod = add(mul(t, mul(num, inv, p), p), 1, p); n       } n     } n     if (k % 2) n       prod = mul(prod, pow(2, add(l % (p - 1), -(r % (p - 1)), p - 1), p), p); n     printf( %d n , prod); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct milk { n   int x, num; n   bool operator<(const milk &mm) const { return x < mm.x; } n } b[1000010]; n int a[1000010], n, m, k; n bool ok(int p2) { n   int p1 = 1, now = 0; n   while (p1 <= n || p2 <= m) { n     if ((p1 <= n && a[p1] < now) || (p2 <= m && b[p2].x < now)) return 0; n     for (int i = 1; i <= k; i++) n       if (p1 <= n && (p2 > m || a[p1] < b[p2].x)) n         p1++; n       else n         p2++; n     now++; n   } n   return 1; n } n int main() { n   int l, r, mid; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &b[i].x); n     b[i].num = i; n   } n   sort(a + 1, a + n + 1); n   sort(b + 1, b + m + 1); n   if (!ok(m + 1)) { n     printf( -1 n ); n     return 0; n   } n   l = 1; n   r = m + 1; n   while (l < r) { n     mid = (l + r) / 2; n     if (ok(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   printf( %d n , m - l + 1); n   for (int i = l; i <= m; i++) printf( %d%c , b[i].num, i == m ?   n  :    ); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n void O_o() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n } n bool prime(int n, int i) { n   if (i == n) return true; n   if (n % i == 0) return false; n   prime(n, i + 1); n } n int arr[1000005], arr1[1000005]; n unsigned long long sum = 0; n int main() { n   O_o(); n   int n, s, m; n   cin >> n; n   for (int i = 1;; i *= 2) { n     if (i < 1000005) n       arr1[i] = 1; n     else n       break; n   } n   for (int i = 1;; i *= 2) { n     if (i == n) { n       s = ((i * 2) - 1); n       break; n     } else if (i > n) { n       s = i - 1; n       break; n     } n   } n   m = n++; n   for (int i = 0; i < int(n); i++) { n     if (arr[s - m] == 0 && arr[m] == 0) arr[s - m] = m, arr[m] = s - m; n     if (arr1[m] == 1) s = m - 1; n     if (m == 0) break; n     m--; n   } n   for (int i = 0; i < int(n); i++) sum += (arr[i] ^ i); n   cout << sum <<   n ; n   for (int i = 0; i < int(n); i++) cout << arr[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, const T& b) { n   return a > b ? a = b, true : false; n } n template <class T> n bool umax(T& a, const T& b) { n   return a < b ? a = b, true : false; n } n template <long long sz> n using tut = array<long long, sz>; n const long long N = 3e5 + 5; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n const long double Pi = acos(-1); n const long long MX = 2e16; n long long n, m, k, t, q, ans, res, a[N]; n long long in[N][2], cc[N], cnt[N][2]; n long long par[N], sz[N]; n vector<long long> edges[N]; n string s; n void dfs(long long u) { n   cnt[u][cc[u]]++; n   for (auto x : edges[u]) { n     if (in[x][1] == -1) continue; n     long long to = in[x][0] + in[x][1] - u; n     if (~cc[to]) continue; n     cc[to] = cc[u] ^ (s[x] ==  0 ); n     dfs(to); n   } n } n long long f(long long x) { return (par[x] == x ? x : par[x] = f(par[x])); } n void add(long long a, long long s0, long long s1) { n   a = f(a); n   res -= min(cnt[a][0], cnt[a][1]); n   cnt[a][0] += s0, cnt[a][1] += s1; n   res += min(cnt[a][0], cnt[a][1]); n } n void merge(long long a, long long b) { n   a = f(a), b = f(b); n   if (a == b) return; n   if (sz[b] > sz[a]) swap(a, b); n   add(a, cnt[b][0], cnt[b][1]); n   add(b, -cnt[b][0], -cnt[b][1]); n   sz[a] += sz[b], par[b] = a; n } n void solve(long long t_case) { n   cin >> n >> k >> s; n   s =  $  + s; n   memset(in, -1, sizeof in), memset(cc, -1, sizeof cc); n   for (long long i = 0; i < k; i++) { n     sz[i] = 1, par[i] = i; n     long long c; n     cin >> c; n     for (long long j = 0; j < c; j++) { n       long long x; n       cin >> x, edges[i].push_back(x); n       if (~in[x][0]) n         in[x][1] = i; n       else n         in[x][0] = i; n     } n   } n   for (long long i = 0; i < k; i++) { n     if (~cc[i]) continue; n     cc[i] = 0, dfs(i); n   } n   for (long long i = 1; i <= n; i++) { n     long long c0 = in[i][0], c1 = in[i][1]; n     if (~c0 && c1 == -1) { n       if (s[i] ==  0 ) { n         if (cc[c0]) n           add(c0, mod, 0); n         else n           add(c0, 0, mod); n       } else { n         if (cc[c0]) n           add(c0, 0, mod); n         else n           add(c0, mod, 0); n       } n     } n     if (~c0 && ~c1) { n       merge(c0, c1); n     } n     cout << res <<   n ; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (0) { n     long long t; n     cin >> t; n     for (long long t_case = 1; t_case <= t; t_case++) solve(t_case); n   } else n     solve(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long lucky[1024 * 4]; n long long fact[14]; n int cnt; n void func(long long cur, int pos, int maxpos) { n   if (pos == maxpos) { n     lucky[++cnt] = cur * 10 + 4; n     lucky[++cnt] = cur * 10 + 7; n     return; n   } n   func(cur * 10 + 4, pos + 1, maxpos); n   func(cur * 10 + 7, pos + 1, maxpos); n } n void pre() { n   cnt = 0; n   for (int d = 1; d <= 10; d++) { n     func(0, 1, d); n   } n   sort(lucky, lucky + cnt); n   fact[0] = 1; n   for (int i = 1; i < 14; i++) fact[i] = i * fact[i - 1]; n } n long long n, k; n void in() { cin >> n >> k; } n int arr[14] = {0}; n int brr[14] = {0}; n void rec(int p, long long k, int coun) { n   if (coun == p) return; n   int b = (k + fact[p - 1 - coun] - 1) / fact[p - 1 - coun]; n   int c = 0; n   for (int i = 0; i < 14; i++) { n     if (brr[i] == 0) c++; n     if (b == c) { n       brr[i] = 1; n       arr[coun] = i; n       rec(p, k - fact[p - 1 - coun] * (b - 1), coun + 1); n       break; n     } n   } n   return; n } n bool luck(long long q) { n   for (int i = 0; i < cnt; i++) { n     if (lucky[i] == q) return true; n     if (lucky[i] > q) return false; n   } n   return false; n } n void solve() { n   int p = 0; n   while (fact[p] < k) p++; n   if (n < 14 && k > fact[n]) { n     cout << -1 << endl; n     return; n   } n   rec(p, k, 0); n   for (int i = 0; i < p; i++) { n     arr[i] += n - p + 1; n   } n   int ans = 1; n   while (lucky[ans] <= n - p) { n     ans++; n   } n   ans--; n   for (long long i = n - p + 1; i <= n; i++) { n     if (luck(i) && luck(arr[i - n - 1 + p])) ans++; n   } n   cout << ans << endl; n } n int main() { n   pre(); n   in(); n   solve(); n } n