#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double PI = acos(-1.0); n const int N = 3e5 + 5, K = 30; n int tam[N] = {1}, g[N][K], _g[N][K]; n vector<int> v; n int res[N]; n pair<int, int> merge(int a, int b) { n   if (!a) return pair<int, int>(b, 0); n   if (!b) return pair<int, int>(a, 0); n   int r = 0; n   v.push_back(a); n   for (int i = 0; i < K; i++) { n     if (g[a][i] and g[b][i]) { n       r++; n       pair<int, int> p = merge(g[a][i], g[b][i]); n       g[a][i] = p.first; n       r += p.second; n     } else if (g[b][i]) { n       g[a][i] = g[b][i]; n     } n   } n   return pair<int, int>(a, r); n } n void go(int at, int d) { n   int r = 0; n   for (int i = 0; i < K; i++) { n     int next = g[at][i]; n     if (next) { n       r++; n       go(next, d + 1); n       tam[at] += tam[next]; n     } n   } n   v.clear(); n   v.push_back(at); n   int u = 0; n   for (int i = 0; i < K; i++) { n     int next = g[at][i]; n     pair<int, int> p = merge(u, next); n     u = p.first; n     r += p.second; n   } n   for (int i = 0; i < (int)v.size(); i++) n     for (int j = 0; j < K; j++) g[v[i]][j] = _g[v[i]][j]; n   res[d] += r; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     char c; n     cin >> a >> b >> c; n     g[a][c -  a ] = b; n     _g[a][c -  a ] = b; n   } n   go(1, 1); n   int ans = 0; n   for (int i = 0; i < N; i++) n     if (res[i] > res[ans]) ans = i; n   cout << n - res[ans] << endl; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   for (size_t i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = a[n - 1] - a[0]; n   multiset<long long> s; n   for (long long i = 1; i < n; i++) { n     s.insert(a[i - 1] - a[i]); n   } n   for (auto i : s) { n     if (k == 1) { n       cout << ans; n       return 0; n     } else { n       k--; n       ans += i; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LNF = 0x3f3f3f3f3f3f3f3f; n const int MOD = 1000000007; n const double EPS = 1e-8; n const long double EUL = 2.71828182845904523536; n const long double PII = 3.14159265358979323846; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, d; n   while (cin >> n >> m >> d) { n     int coffee[n]; n     int ans[n]; n     set<pair<int, int> > timeline; n     for (int i = 0; i < n; i++) { n       cin >> coffee[i]; n       timeline.insert({coffee[i], i}); n     } n     int day = 0; n     while (!timeline.empty()) { n       day++; n       int x = timeline.begin()->second; n       ans[x] = day; n       timeline.erase(timeline.begin()); n       while (1) { n         auto it = timeline.lower_bound({coffee[x] + d + 1, -1}); n         if (it == timeline.end()) break; n         x = it->second; n         ans[x] = day; n         timeline.erase(it); n       } n     } n     cout << day << endl; n     for (int i = 0; i < n; i++) cout << ans[i] <<    ; n     cout << endl; n   } n   cerr <<  Time :   << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n struct Rec { n   pair<int, int> interval[4]; n } recs[100005]; n bool check(set<pair<pair<int, int>, int>> st[]) { n   int n = st[0].size(); n   if (n == 1) return true; n   set<pair<pair<int, int>, int>>::iterator iter[4]; n   for (int i = 0; i < 4; i++) iter[i] = st[i].begin(); n   vector<int> mxr(4, -inf); n   for (int i = 0; i < n - 1; i++) { n     for (int j = 0; j < 4; j++) { n       mxr[j] = max(mxr[j], iter[j]->first.second); n       iter[j]++; n       if (iter[j]->first.first >= mxr[j]) { n         set<pair<pair<int, int>, int>> mini[4]; n         vector<int> indexs; n         for (auto it = st[j].begin(); it != iter[j]; it++) { n           int index = it->second; n           indexs.push_back(index); n         } n         for (auto index : indexs) { n           for (int z = 0; z < 4; z++) { n             mini[z].insert(make_pair(recs[index].interval[z], index)); n             st[z].erase(make_pair(recs[index].interval[z], index)); n           } n         } n         if (check(mini) && check(st)) return true; n         return false; n       } n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     recs[i].interval[0] = make_pair(a, c); n     recs[i].interval[1] = make_pair(b, d); n     recs[i].interval[2] = make_pair(-c, -a); n     recs[i].interval[3] = make_pair(-d, -b); n   } n   set<pair<pair<int, int>, int>> st[4]; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) st[i].insert(make_pair(recs[j].interval[i], j)); n   } n   puts(check(st) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimize( O3 ) n using namespace std; n const long long MOD = 998244353; n const long long INF = 1e8 + 7; n const int base = 2e5 + 1; n const long long MAX = 3 * 1e5 + 1; n const double EPS = 1e-9; n const double PI = acos(-1.); n const int MAXN = 5 * 1e5 + 47; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n vector<vector<int> > g(MAXN), tb(MAXN); n vector<int> res; n int t[MAXN], was[MAXN]; n int get(int v) { n   was[v] = 1; n   set<int> s; n   for (auto u : g[v]) { n     if (was[u]) s.insert(t[u]); n   } n   for (int i = (1); i < (MAXN); ++i) { n     if (!s.count(i)) return i; n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = (0); i < (m); ++i) { n     int v, u; n     scanf( %d %d , &v, &u); n     v--; n     u--; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   for (int i = (0); i < (n); ++i) { n     scanf( %d , &t[i]); n     tb[t[i]].push_back(i); n   } n   for (int i = (1); i < (n + 1); ++i) { n     for (auto v : tb[i]) { n       if (get(v) != i) { n         cout << -1; n         return 0; n       } n       res.push_back(v + 1); n     } n   } n   for (auto v : res) printf( %d  , v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n int n, k; n struct st { n   int v, p; n }; n bool cmp(st a, st b) { return a.v < b.v; } n st a[MAX]; n void solve() { n   sort(a + 1, a + n + 1, cmp); n   int t = 0; n   for (int i = 2; i <= n; ++i) { n     if (a[i].p == a[i - 1].p + 1) { n       while (1) { n         if (a[i].p != a[i - 1].p + 1) n           break; n         else { n           ++i; n         } n       } n       --i; n     } else { n       ++t; n     } n   } n   if (t + 1 <= k) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> k; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i].v; n       a[i].p = i; n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50010; n int n; n char s[maxn]; n int main() { n   while (scanf( %s , s) != EOF) { n     n = strlen(s); n     int len = 1; n     while (len <= n / 2) { n       int cnt = 0, start = len; n       for (int i = len; i < n; i++) { n         if (cnt == len) break; n         if (s[i] != s[i - len]) { n           start = i + 1; n           cnt = 0; n         } else n           cnt++; n       } n       if (cnt == len) { n         for (int i = start + len; i < n; i++) s[i - len] = s[i]; n         n -= len; n         s[n] =   0 ; n       } else n         len++; n     } n     printf( %s n , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[200000]; n int main() { n   int n, cnt = 0, cntz = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &arr[i]); n     if (arr[i] < 0) n       cnt++; n     else if (arr[i] == 0) n       cntz++; n   } n   if (cnt % 2 == 0 && cntz == 0) n     for (int i = 0; i < n - 1; ++i) printf( 1 %d %d n , i + 1, i + 2); n   else if (cnt % 2 == 1 && cntz == 0) { n     long long mx = (-1e9) - 1, mxi = -1; n     for (int i = 0; i < n; ++i) n       if (arr[i] < 0 && mx < arr[i]) { n         mx = arr[i]; n         mxi = i; n       } n     printf( 2 %lld n , mxi + 1); n     vector<int> ans; n     for (int i = 0; i < n; ++i) n       if (i != mxi) ans.push_back(i); n     for (int i = 1; i < ans.size(); ++i) n       printf( 1 %d %d n , ans[i - 1] + 1, ans[i] + 1); n   } else if (cnt % 2 == 0 && cntz >= 1) { n     vector<int> idx; n     for (int i = 0; i < n; ++i) n       if (arr[i] == 0) idx.push_back(i); n     for (int i = 1; i < idx.size(); ++i) n       printf( 1 %d %d n , idx[i - 1] + 1, idx[i] + 1); n     if (idx.size() >= 1 && cntz != n) printf( 2 %d n , idx.back() + 1); n     vector<int> ans; n     for (int i = 0; i < n; ++i) n       if (arr[i] != 0) ans.push_back(i); n     for (int i = 1; i < ans.size(); ++i) n       printf( 1 %d %d n , ans[i - 1] + 1, ans[i] + 1); n   } else if (cnt % 2 == 1 && cntz >= 1) { n     long long mx = (-1e9) - 1, mxi = -1; n     vector<int> idx; n     for (int i = 0; i < n; ++i) { n       if (arr[i] == 0) idx.push_back(i); n       if (arr[i] < 0 && mx < arr[i]) { n         mx = arr[i]; n         mxi = i; n       } n     } n     idx.push_back(mxi); n     sort(idx.begin(), idx.end()); n     for (int i = 1; i < idx.size(); ++i) n       printf( 1 %d %d n , idx[i - 1] + 1, idx[i] + 1); n     if (idx.size() != n) printf( 2 %d n , idx.back() + 1); n     vector<int> ans; n     for (int i = 0; i < n; ++i) n       if (i != mxi && arr[i] != 0) ans.push_back(i); n     for (int i = 1; i < ans.size(); ++i) n       printf( 1 %d %d n , ans[i - 1] + 1, ans[i] + 1); n   } else { n     for (int i = 0; i < n - 1; ++i) printf( 1 %d %d n , i + 1, i + 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sizeS = 100 + 5; n const int sizeN = 50 + 5; n int a[sizeS], na, n; n int dpmax[sizeS][sizeN][3], dpmin[sizeS][sizeN][3]; n void Nhap() { n   char s[sizeS]; n   scanf( %s , s); n   na = int(strlen(s)); n   for (int i = 0; i < na; i++) n     if (s[i] ==  F ) a[i + 1] = 1; n   scanf( %d , &n); n } n void Init() { n   int cntT = 0; n   dpmax[0][0][0] = dpmax[0][0][1] = dpmin[0][0][0] = dpmin[0][0][1] = 0; n   for (int i = 1; i <= na; i++) { n     cntT += 1 - a[i]; n     if (cntT % 2 == 0) { n       if (a[i] == 1) n         dpmax[i][0][1] = dpmin[i][0][1] = dpmax[i - 1][0][1] + 1; n       else n         dpmax[i][0][1] = dpmin[i][0][1] = dpmax[i - 1][0][0]; n     } else if (a[i] == 1) n       dpmax[i][0][0] = dpmin[i][0][0] = dpmax[i - 1][0][0] - 1; n     else n       dpmax[i][0][0] = dpmin[i][0][0] = dpmax[i - 1][0][1]; n   } n } n void Xuat_Solve() { n   int cntT = 0; n   for (int i = 1; i <= na; i++) cntT += 1 - a[i]; n   if (cntT > n) { n     for (int i = 1; i <= na; i++) n       for (int t = 1; t <= min(t, n); t++) { n         for (int d = 0; d <= 1; d++) n           if (a[i] == 1) { n             int z; n             d == 0 ? z = -1 : z = 1; n             dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t][d] + z); n             dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t][d] + z); n           } else { n             dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t][1 - d]); n             dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t][1 - d]); n           } n         for (int d = 0; d <= 1; d++) n           if (1 - a[i] == 1) { n             int z; n             d == 0 ? z = -1 : z = 1; n             dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t - 1][d] + z); n             dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t - 1][d] + z); n           } else { n             dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t - 1][1 - d]); n             dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t - 1][1 - d]); n           } n       } n     int ans1 = max(dpmax[na][n][0], dpmax[na][n][1]); n     int ans2 = max(abs(dpmin[na][n][0]), abs(dpmin[na][n][1])); n     if (ans1 == 33 || ans1 == 48) ans1--; n     if (ans2 == 33 || ans2 == 48) ans2--; n     printf( %d , max(ans1, ans2)); n   } else { n     if ((n - cntT) % 2 == 0) n       printf( %d , na); n     else n       printf( %d , na - 1); n   } n } n int main() { n   if (1 == 0) { n     freopen( 132C.INP ,  r , stdin); n     freopen( 132C.OUT ,  w , stdout); n   }; n   Nhap(); n   Init(); n   Xuat_Solve(); n   if (1 == 0) { n     fclose(stdin); n     fclose(stdout); n   }; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10], n; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     int m = 0; n     for (int i = 0; i < 7; ++i) { n       int x; n       scanf( %d , &x); n       if (x) a[m++] = i; n     } n     int ret = 1e9; n     for (int x = 0; x < m; ++x) { n       int nn = n + x; n       int tim = nn / m, lef = nn % m; n       int ans; n       if (!lef) n         ans = (tim - 1) * 7 + a[m - 1]; n       else n         ans = tim * 7 + a[lef - 1]; n       ans = ans - a[x] + 1; n       ret = min(ans, ret); n     } n     printf( %d n , ret); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx8[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n int dy8[8] = {0, 0, 1, -1, 1, -1, 1, -1}; n void file() {} n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   file(); n   int n, s; n   cin >> n >> s; n   if (n == 1 && s == 0) { n     cout <<  0 0  << endl; n     return 0; n   } n   if (n * 9 < s || s == 0) { n     cout <<  -1 -1  << endl; n     return 0; n   } n   int sum = s; n   vector<int> mx, mn; n   for (int i = 0; i < n; i++) { n     if (sum >= 9) n       mx.push_back(9), sum -= 9; n     else n       mx.push_back(sum), sum = 0; n   } n   sum = s; n   for (int i = n - 1; i > 0; i--) { n     mn.push_back(min(sum - 1, 9)); n     sum -= min(sum - 1, 9); n   } n   mn.push_back(sum); n   reverse(((mn).begin()), ((mn).end())); n   int zeros = n - mn.size(); n   for (int i = 0; i < mn.size(); i++) { n     cout << mn[i]; n     if (i == 0) { n       for (int j = 0; j < zeros; j++) cout << 0; n     } n   } n   cout <<    ; n   for (int i = 0; i < n; i++) cout << mx[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n int n; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   if (n % 2 == 0) { n     cout << n / 2; n     cout <<   n ; n     for (int i = 0; i < n / 2; ++i) { n       cout <<  2  ; n     } n     return 0; n   } n   n -= 3; n   cout << n / 2 + 1; n   cout <<   n ; n   cout <<  3  ; n   for (int i = 0; i < n / 2; ++i) { n     cout <<  2  ; n   } n   return 0; n } n


//////////////////////////// ****   JAI SHREE RAM   **** ////////////////////////// n #include<bits/stdc++.h> n #include<unordered_map> n using namespace std; n #define int long long int n #define double long double n void fun() { n #ifndef ONLINE_JUDGE n freopen( input.txt ,  r , stdin);freopen( output.txt ,  w , stdout); n    #endif n } n int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);} n int poww(int a, int b, int md) {if(b<0) return 0; if(a==0) return 0; int res=1; n     while(b) {if(b&1){res=(1ll*res*a)%md;}a=(1ll*a*a)%md;b>>=1;}return res;} n int divide(int a, int b, int md) {int rr=a*(poww(b, md-2, md));rr%=md;return rr;} n ////////////////////////////////////////////////////////////////////////////////// n ////////////////////////////////////////////////////////////////////////////////// n const int siz = 2e5 + 5; n int ans; n ////////////////////////////////////////////////////////////////////////////////// n int dfs(int node, int gpar, vector<int> g[]) { n  int minchild = 1e9; n  int maxchild = 0; n  vector<int> nodedepth; n  for(int child : g[node]) { n   if(child != gpar) { n    int returnpath = dfs(child, node, g) + 1; n    minchild = min(minchild, returnpath); n    maxchild = max(maxchild, returnpath); n    if(node == 1) { n     nodedepth.push_back(returnpath); n    } n   } n  } n  if(node == 1) { n   sort(nodedepth.begin(), nodedepth.end()); n  }  n  if(minchild == 1e9) { n   ans = max(ans, 0ll); n   return 0; n  } n  if(node == 1) { n   if(g[node].size() > 1) { n    ans = max(ans, maxchild); n    minchild = nodedepth[nodedepth.size() - 2]; n    ans = max(ans, minchild + 1); n   } else { n    ans = max(ans, maxchild); n   } n  } else { n   if(g[node].size() > 2) { n    ans = max(ans, minchild); n    ans = max(ans, maxchild + 1); n   } else { n    ans = max(ans, minchild); n   } n  } n  return minchild; n } n signed main() { n     ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); fun(); n     int t; cin >> t; n     while(t--) { n      int n, u, v; cin >> n; n      vector<int> g[n + 1]; n      for(int i = 1; i < n; i++) { n       cin >> u >> v; n       g[u].push_back(v); n       g[v].push_back(u); n      } n      ans = INT_MIN; n      dfs(1, 0, g); n      cout << ans <<   n ; n     } n     return 0; n } n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n int n, k, x[105], y[105]; n double s; n int main(void) { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; n   int j = 1; n   while (j < n) { n     s += sqrt((double)(y[j] - y[j - 1]) * (y[j] - y[j - 1]) + n               (double)(x[j] - x[j - 1]) * (x[j] - x[j - 1])); n     j++; n   } n   printf( %0.7f , s * k * 0.02); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T pow2(T a) { n   return a * a; n } n template <class T> n inline bool mineq(T a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n void print(long long a) { n   string str; n   while (a) { n     str.push_back(a % 10 +  0 ); n     a /= 10; n   } n   while (str.size() != 9) str.push_back( 0 ); n   reverse(str.begin(), str.end()); n   cout << str; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long a, b, mod; n   cin >> a >> b >> mod; n   long long t = (long long)1e9 % mod, n = -t; n   if (a > 1e7 + 10) a = 1e7 + 10; n   for (int i = 0; i < a + 1; i++) { n     n += t; n     if (n > mod) n -= mod; n     if (n + b < mod && n != 0) { n       cout <<  1  ; n       print(i); n       return 0; n     } n   } n   cout << 2; n } n


#include <bits/stdc++.h> n using namespace std; n long long get(long long n) { n   for (long long i = 2; i * i <= n; ++i) n     if (n % i == 0) return i; n   return n; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   int c = 0; n   while (n % 2 != 0) { n     n -= get(n); n     c++; n   } n   cout << c + n / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct H { n   int i, x, y; n }; n int k, m, n, b[200004]; n H w[200004]; n int C(H a, H b) { return a.x < b.x || a.x == b.x && a.i > b.i; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   fill(b + 1, b + n + 1, 1 << 30); n   for (int i = 1; i <= n; i++) { n     cin >> k; n     w[i] = {i, k, i - k}; n   } n   sort(w + 1, w + n + 1, C); n   for (int i = 1; i <= n; i++) { n     if (w[i].y >= 0) b[upper_bound(b + 1, b + n + 1, w[i].y) - b] = w[i].y; n   } n   for (int i = 1; i <= n; i++) { n     if (b[i] < 1 << 30) m = i; n   } n   cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, x, y, maxi = 0, buf = 1, max_row[501]; n bool arr[501][501]; n int main() { n   cin >> n >> m >> q; n   for (int i = 0; i < n; i++) { n     max_row[i] = 0; n     for (int j = 0; j < m; j++) cin >> arr[i][j]; n   } n   for (int i = 0; i < n; i++) { n     buf = 0; n     for (int j = 0; j < m; j++) { n       if (arr[i][j] == 1) n         buf++; n       else n         buf = 0; n       max_row[i] = max(max_row[i], buf); n       maxi = max(maxi, max_row[i]); n     } n   } n   for (int i = 0; i < q; i++) { n     cin >> x >> y; n     if (arr[x - 1][y - 1] == 0) n       arr[x - 1][y - 1] = 1; n     else n       arr[x - 1][y - 1] = 0; n     max_row[x - 1] = 0; n     buf = 0; n     for (int j = 0; j < m; j++) { n       if (arr[x - 1][j] == 1) n         buf++; n       else n         buf = 0; n       max_row[x - 1] = max(max_row[x - 1], buf); n     } n     maxi = 0; n     for (int i = 0; i < n; i++) maxi = max(maxi, max_row[i]); n     cout << maxi << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int first = 0, f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) { n     if (ch ==  - ) f = -1; n   } n   for (; isdigit(ch); ch = getchar()) { n     first = first * 10 + ch - 48; n   } n   return first * f; n } n const int mxN = 1.5e5; n const int S = 26; n int n, en, q, mxd, tot; n int fa[mxN + 3]; n char c[mxN + 3]; n int sonn[mxN + 3]; n int dep[mxN + 3]; n int tpv[mxN + 3], btv[mxN + 3], cnt[mxN + 3][S + 3], f[mxN + 3][S + 3], n     g[mxN + 3]; n vector<int> son[mxN + 3]; n int main() { n   n = read(), q = read(); n   for (int i = 2; i <= n; i++) { n     scanf( %d%*c%c , &fa[i], &c[i]); n     sonn[fa[i]]++; n     c[i] = c[i] ==  ?  ? 27 : c[i] - 96; n   } n   for (int u = 2; u <= n; u++) { n     dep[u] = dep[fa[u]] + 1; n     mxd = max(mxd, dep[u]); n   } n   for (int u = 1; u <= n; u++) n     if (sonn[u] == 0 && dep[u] < mxd) { n       for (int i = 1; i <= q; i++) puts( Fou ); n       return 0; n     } n   tpv[1] = 1; n   btv[1] = 1; n   for (int u = 2; u <= n; u++) { n     if (sonn[fa[u]] == 1) { n       tpv[u] = tpv[fa[u]]; n     } else { n       tpv[u] = fa[u]; n     } n     if (sonn[u] != 1) { n       int v = u; n       while (v != tpv[u]) { n         btv[v] = u; n         cnt[u][c[v]]++; n         v = fa[v]; n       } n     } n   } n   for (int u = n; u >= 2; u--) n     if (btv[u] == u) { n       for (int i = 1; i <= S; i++) { n         f[tpv[u]][i] = max(f[tpv[u]][i], f[u][i] + cnt[u][i]); n       } n       son[tpv[u]].push_back(u); n     } n   for (int u = 1; u <= n; u++) n     if (btv[u] == u) { n       for (int i = 1; i <= S; i++) g[u] += f[u][i]; n       if (g[u] > mxd - dep[u]) { n         tot++; n       } n     } n   while (q--) { n     int u; n     char ch, ch0; n     scanf( %d%*c%c , &u, &ch); n     ch = ch ==  ?  ? 27 : ch - 96; n     ch0 = c[u]; n     int u0 = btv[u]; n     cnt[u0][ch0]--; n     c[u] = ch; n     cnt[u0][ch]++; n     while (1) { n       u0 = tpv[u0]; n       if (g[u0] > mxd - dep[u0]) { n         tot--; n       } n       if (ch0 <= S) { n         g[u0] -= f[u0][ch0]; n       } n       if (ch <= S) { n         g[u0] -= f[u0][ch]; n       } n       f[u0][ch0] = 0, f[u0][ch] = 0; n       for (int i = 0; i < son[u0].size(); i++) n         f[u0][ch0] = max(f[u0][ch0], f[son[u0][i]][ch0] + cnt[son[u0][i]][ch0]); n       for (int i = 0; i < son[u0].size(); i++) n         f[u0][ch] = max(f[u0][ch], f[son[u0][i]][ch] + cnt[son[u0][i]][ch]); n       if (ch0 <= S) { n         g[u0] += f[u0][ch0]; n       } n       if (ch <= S) { n         g[u0] += f[u0][ch]; n       } n       if (g[u0] > mxd - dep[u0]) { n         tot++; n       } n       if (u0 == 1) { n         break; n       } n     } n     if (tot) { n       puts( Fou ); n     } else { n       printf( Shi  ); n       int ans = 0; n       for (int i = 1; i <= S; i++) ans += (f[1][i] + mxd - g[1]) * i; n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char mp[111][111]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> mp[i][j]; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     bool f = 1; n     for (int j = 0; f && j < m; ++j) { n       bool flag = 1; n       for (int k = 0; flag && k < n; ++k) n         if (mp[k][j] > mp[i][j]) flag = 0; n       if (flag) f = 0; n     } n     if (!f) ans++; n   } n   cout << ans << (  n ); n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n using namespace std; n inline int read() { n   int x = 0, neg = 1; n   char op = getchar(); n   while (!isdigit(op)) { n     if (op ==  - ) neg = -1; n     op = getchar(); n   } n   while (isdigit(op)) { n     x = 10 * x + op -  0 ; n     op = getchar(); n   } n   return neg * x; n } n inline void print(int x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 110005; n int ok[N]; n int pre[N], lst[N]; n int a[N], n; n struct Bit { n   int c[N]; n   void init() { n     memset(c, 0x3f, sizeof(c)); n     c[0] = 0; n   } n   void add(int x, int v) { n     while (x < N) { n       c[x] = min(c[x], v); n       x += x & -x; n     } n   } n   int qry(int x) { n     int ret = 1e9; n     while (x > 0) { n       ret = min(ret, c[x]); n       x ^= x & -x; n     } n     return ret; n   } n } bit; n int main() { n   n = read(); n   bool same = 1; n   for (int i = 1; i <= n; i++) { n     a[i] = read(), same &= a[i] == 1; n     pre[i] = lst[a[i]], lst[a[i]] = i; n   } n   if (same) { n     puts( 1 ); n     return 0; n   } n   bit.init(); n   for (int i = 1; i <= n + 1; i++) bit.add(i, lst[i]); n   for (int i = 1; i <= n + 1; i++) { n     if (lst[i] != n && bit.qry(i - 1) > lst[i]) ok[i] = 1; n   } n   for (int i = n; i >= 1; i--) { n     bit.add(a[i], pre[i]); n     if (pre[i] != i && bit.qry(a[i] - 1) > pre[i]) ok[a[i]] = 1; n   } n   int cur = 1; n   while (ok[cur]) cur++; n   printf( %d n , cur); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int len = 20000; n   string di; n   int mo; n   string ans[2] = { NO ,  YES }; n   bool yes = 1; n   for (int i = 0; i < n; ++i) { n     cin >> mo >> di; n     if (len == 20000 && di !=  South ) yes = 0; n     if (len == 0 && di !=  North ) yes = 0; n     if (len > 20000 || len < 0) yes = 0; n     if (di ==  South ) len -= mo; n     if (di ==  North ) len += mo; n   } n   yes = (len == 20000) && yes; n   cout << ans[yes]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, size_t N> n ostream& operator<<(ostream& os, const array<T, N>& v); n template <class T1, class T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T, size_t N> n ostream& operator<<(ostream& os, const array<T, N>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, const set<T1, T2>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, const multiset<T1, T2>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2, class T3> n ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& ii : v) { n     if (!f) os <<  ,  ; n     os <<  (  << ii.first <<   ->   << ii.second <<  )  ; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, const multimap<T1, T2>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& ii : v) { n     if (!f) os <<  ,  ; n     os <<  (  << ii.first <<   ->   << ii.second <<  )  ; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, priority_queue<T1, T2> v) { n   bool f = 1; n   os <<  [ ; n   while (!v.empty()) { n     auto x = v.top(); n     v.pop(); n     if (!f) os <<  ,  ; n     f = 0; n     os << x; n   } n   return os <<  ] ; n } n template <class T1, class T2> n void printarray(const T1& a, T2 sz, T2 beg = 0) { n   for (T2 i = beg; i < sz; i++) cout << a[i] <<    ; n   cout << endl; n } n int XL, YL, ZL; n int n, m, k; n pair<int, int> sx, sy, sz; n tuple<int, int, int> In[100010]; n inline int seg_in(pair<int, int> a, int b) { n   return a.first <= b && b <= a.second; n } n vector<tuple<int, int, int> > U[100010]; n vector<tuple<int, int, int> > Q[100010]; n vector<int> T[100010]; n vector<int> vz[100010]; n int L[100010]; n void pre(int y, int z) { n   for (int i = y; i <= YL; i += (i & -i)) { n     vz[i].push_back(z); n   } n } n void update(int y, int z, int c) { n   for (int i = y; i <= YL; i += (i & -i)) { n     int nz = (int)(lower_bound((vz[i]).begin(), (vz[i]).end(), z) - n                    vz[i].begin() + 1); n     for (int j = nz; j <= L[i]; j += (j & -j)) T[i][j] += c; n   } n } n int read(int y, int z) { n   int res = 0; n   for (int i = y; i; i -= (i & -i)) { n     int nz = n         (int)(upper_bound((vz[i]).begin(), (vz[i]).end(), z) - vz[i].begin()); n     for (int j = nz; j; j -= (j & -j)) res += T[i][j]; n   } n   return res; n } n int ans[100010]; n int main() { n   scanf( %d%d%d , &XL, &YL, &ZL); n   scanf( %d%d%d , &n, &m, &k); n   sx = sy = sz = pair<int, int>(1e9, -1); n   for (int i = 1; i <= n; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     sx.first = min(sx.first, x); n     sx.second = max(sx.second, x); n     sy.first = min(sy.first, y); n     sy.second = max(sy.second, y); n     sz.first = min(sz.first, z); n     sz.second = max(sz.second, z); n   } n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     In[i] = tuple<int, int, int>(x, y, z); n     if (seg_in(sx, x) && seg_in(sy, y) && seg_in(sz, z)) { n       puts( INCORRECT ); n       return 0; n     } n   } n   puts( CORRECT ); n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     tie(x, y, z) = In[i]; n     pair<int, int> vx, vy, vz; n     if (x < sx.first) n       vx = pair<int, int>(1, x); n     else if (seg_in(sx, x)) n       vx = pair<int, int>(1, XL); n     else n       vx = pair<int, int>(x, XL); n     if (y < sy.first) n       vy = pair<int, int>(1, y); n     else if (seg_in(sy, y)) n       vy = pair<int, int>(1, YL); n     else n       vy = pair<int, int>(y, YL); n     if (z < sz.first) n       vz = pair<int, int>(1, z); n     else if (seg_in(sz, z)) n       vz = pair<int, int>(1, ZL); n     else n       vz = pair<int, int>(z, ZL); n     for (int ax : {vx.first, vx.second + 1}) { n       for (int ay : {vy.first, vy.second + 1}) { n         for (int az : {vz.first, vz.second + 1}) { n           int c = 1; n           if (ax != vx.first) c = -c; n           if (ay != vy.first) c = -c; n           if (az != vz.first) c = -c; n           U[ax].push_back(tuple<int, int, int>(ay, az, c)); n         } n       } n     } n   } n   for (int i = 1; i <= k; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     if (seg_in(sx, x) && seg_in(sy, y) && seg_in(sz, z)) { n       ans[i] = -1; n     } else n       Q[x].push_back(tuple<int, int, int>(y, z, i)); n   } n   for (int i = 1; i <= XL; i++) { n     for (tuple<int, int, int> e : U[i]) { n       int y, z, c; n       tie(y, z, c) = e; n       pre(y, z); n     } n   } n   for (int i = 1; i <= YL; i++) { n     sort((vz[i]).begin(), (vz[i]).end()); n     vz[i].resize(unique((vz[i]).begin(), (vz[i]).end()) - vz[i].begin()); n     L[i] = ((int)(vz[i]).size()); n     T[i].resize(L[i] + 1); n   } n   for (int i = 1; i <= XL; i++) { n     for (tuple<int, int, int> e : U[i]) { n       int y, z, c; n       tie(y, z, c) = e; n       update(y, z, c); n     } n     for (tuple<int, int, int> e : Q[i]) { n       int y, z, idx; n       tie(y, z, idx) = e; n       ans[idx] = read(y, z); n     } n   } n   for (int i = 1; i <= k; i++) { n     if (ans[i] == -1) n       puts( OPEN ); n     else if (ans[i] == 0) n       puts( UNKNOWN ); n     else n       puts( CLOSED ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INFINITE = 0x3f3f3f3f; n char grid[500][501]; n int m, n; n int memo[500][27][27][2], memoCost[500][27][2]; n bool mark[500][27][27][2], markCost[500][27][2]; n int choice[500][27][27][2]; n int GetCost(int row, char c, int next) { n   int& cost = memoCost[row][c][next]; n   if (markCost[row][c][next]) return cost; n   cost = 0, markCost[row][c][next] = true; n   for (int j = next; j < n; j += 2) n     if (grid[row][j] != c +  a ) ++cost; n   return cost; n } n int f(int row, int c1, int c2, int next) { n   if (row >= m) return 0; n   int& best = memo[row][c1][c2][next]; n   int& ch = choice[row][c1][c2][next]; n   if (mark[row][c1][c2][next]) return best; n   best = INFINITE, mark[row][c1][c2][next] = true; n   if (next == 0) { n     for (int nc1 = 0; nc1 < 26; ++nc1) n       if (nc1 != c1) { n         const int cost = GetCost(row, nc1, next); n         const int sub = f(row, nc1, c2, 1 - next) + cost; n         if (sub < best) best = sub, ch = nc1; n       } n   } else { n     for (int nc2 = 0; nc2 < 26; ++nc2) n       if (nc2 != c1 && nc2 != c2) { n         const int cost = GetCost(row, nc2, next); n         const int sub = f(row + 1, c1, nc2, 1 - next) + cost; n         if (sub < best) best = sub, ch = nc2; n       } n   } n   return best; n } n void UpdateFlag() { n   for (int row = 0, c1 = 26, c2 = 26, next = 0; row < m;) { n     const int nc = choice[row][c1][c2][next]; n     for (int j = next; j < n; j += 2) grid[row][j] = nc +  a ; n     if (next == 0) n       c1 = nc, next = 1; n     else n       c2 = nc, next = 0, ++row; n   } n } n int main() { n   scanf( %d %d , &m, &n); n   for (int i = 0; i < m; ++i) scanf( %s , grid[i]); n   memset(mark, false, sizeof(mark)); n   memset(markCost, false, sizeof(markCost)); n   printf( %d n , f(0, 26, 26, 0)); n   UpdateFlag(); n   for (int i = 0; i < m; ++i) printf( %s n , grid[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n int n, A, B, C, D; n int be[N], fa[N], sz[N]; n vector<int> e[N]; n bitset<N> bit; n int main() { n   scanf( %d , &n); n   scanf( %d%d%d%d , &A, &B, &C, &D); n   for (int i = (int)(2); i <= (int)(n); i++) { n     scanf( %d , &fa[i]); n     e[fa[i]].push_back(i); n     be[i] = (fa[i] == 1 ? i : be[fa[i]]); n   } n   for (int i = (int)(n); i >= (int)(1); i--) { n     if (e[i].size() == 0) ++sz[i]; n     if (fa[i]) sz[fa[i]] += sz[i]; n   } n   if (sz[1] & 1) return puts( NO ), 0; n   if (sz[be[A]] >= sz[1] / 2 || sz[be[B]] >= sz[1] / 2 || n       sz[be[C]] >= sz[1] / 2 || sz[be[D]] >= sz[1] / 2) n     return puts( NO ), 0; n   assert(be[A] != be[B]); n   assert(be[A] != be[C]); n   assert(be[A] != be[D]); n   assert(be[B] != be[C]); n   assert(be[B] != be[D]); n   assert(be[C] != be[D]); n   for (int i = (int)(0); i <= (int)(n); i++) bit[i] = (i == 0); n   for (int x = A, y = 0; x; y = x, x = fa[x]) n     for (auto j : e[x]) n       if (j != y) n         if (j != be[A] && j != be[B] && j != be[C] && j != be[D]) n           bit |= bit << sz[j]; n   for (int x = B, y = 0; x != 1; y = x, x = fa[x]) n     for (auto j : e[x]) n       if (j != y) n         if (j != be[A] && j != be[B] && j != be[C] && j != be[D]) n           bit |= bit << sz[j]; n   if (!bit[sz[1] / 2 - 1 - sz[be[C]]]) return puts( NO ), 0; n   for (int i = (int)(0); i <= (int)(n); i++) bit[i] = (i == 0); n   for (int x = C, y = 0; x; y = x, x = fa[x]) n     for (auto j : e[x]) n       if (j != y) n         if (j != be[A] && j != be[B] && j != be[C] && j != be[D]) n           bit |= bit << sz[j]; n   for (int x = D, y = 0; x != 1; y = x, x = fa[x]) n     for (auto j : e[x]) n       if (j != y) n         if (j != be[A] && j != be[B] && j != be[C] && j != be[D]) n           bit |= bit << sz[j]; n   if (!bit[sz[1] / 2 - 1 - sz[be[A]]]) return puts( NO ), 0; n   puts( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t[105][105]; n   int n; n   int x1, y1, x2, y2; n   while (cin >> n) { n     for (int i = 0; i < 105; i++) { n       for (int j = 0; j < 105; j++) t[i][j] = 0; n     } n     for (int z = 0; z < n; z++) { n       cin >> x1 >> y1 >> x2 >> y2; n       for (int i = 101 - y1; i >= 101 - y2; i--) { n         for (int j = 101 - x1; j >= 101 - x2; j--) t[i][j]++; n       } n     } n     int ans = 0; n     for (int i = 1; i <= 100; i++) { n       for (int j = 1; j <= 100; j++) ans += t[i][j]; n     } n     cout << ans <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 10, maxe = 15; n long long n, k, x, ans = -1; n long long fact[maxe], A[maxe], use[maxe]; n void lucky_generator(long long a) { n   if (a > x) return; n   ans++; n   lucky_generator(a * 10 + 4); n   lucky_generator(a * 10 + 7); n } n bool is_lucky(long long a) { n   if (a < 3) return 0; n   a++; n   while (a) { n     if (a % 10 != 4 && a % 10 != 7) return 0; n     a /= 10; n   } n   return 1; n } n void solve(long long index, long long size) { n   if (index == size) return; n   int a = k / fact[size - index - 1] + 1, i = 0; n   k -= (a - 1) * fact[size - index - 1]; n   for (;; i++) { n     if (!use[i]) { n       a--; n       if (!a) break; n     } n   } n   A[index] = i; n   use[i] = 1; n   if (is_lucky(index + x) && is_lucky(i + x)) { n     ans++; n   } n   solve(index + 1, size); n } n int main() { n   fact[0] = 1; n   for (int i = 1; i < maxe + 1; i++) fact[i] = i * fact[i - 1]; n   cin >> n >> k; n   k--; n   if (n < maxe && k >= fact[n]) { n     cout <<  -1 n ; n     return 0; n   } n   x = max(0ll, n - maxe); n   lucky_generator(0); n   solve(0ll, n - x); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long POW(long long A, long long N, long long MOD) { n   if (N == 0) return 1 % MOD; n   if (N == 1) return A % MOD; n   long long tmp = POW(A, N / 2, MOD) % MOD; n   if (N % 2) n     return ((tmp * tmp) % MOD * A % MOD) % MOD; n   else n     return (tmp * tmp) % MOD; n } n long long MUL(long long A, long long B, long long MOD) { n   long long r = 0; n   while (B) { n     r = (r + (A * (B % 2) % MOD)) % MOD; n     A = (A + A) % MOD; n     B /= 2; n   } n   return r; n } n long long GCD(long long A, long long B) { return B == 0 ? A : GCD(B, A % B); } n int dx[] = {0, -1, 0, 1, 0}; n int dy[] = {0, 0, 1, 0, -1}; n template <class X, class Y> n bool minimize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x > y + eps) { n     x = y; n     return true; n   } else n     return false; n } n template <class X, class Y> n bool maximize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x + eps < y) { n     x = y; n     return true; n   } else n     return false; n } n template <class T> n T Abs(const T &x) { n   return (x < 0 ? -x : x); n } n const int MAXN = 2005; n const int MOD = 1e9 + 7; n void FIXMOD(int &val) { n   long long res = val; n   if (res < 0) res += 1ll * MOD * MOD; n   if (res >= MOD) res %= MOD; n   val = res; n } n struct SegmentTree { n   int ST[4 * MAXN]; n   void update(int id, int l, int r, int u, int v, int val) { n     if (v < l || r < u) return; n     if (u <= l && r <= v) { n       ST[id] += val; n       ST[id] %= MOD; n       FIXMOD(ST[id]); n       return; n     } n     int mid = ((l + r) >> 1); n     update((id << 1), l, mid, u, v, val); n     update((id << 1 | 1), mid + 1, r, u, v, val); n     ST[id] = ST[(id << 1)] + ST[(id << 1 | 1)]; n     FIXMOD(ST[id]); n   } n   int get(int id, int l, int r, int u, int v) { n     if (v < l || r < u) return 0; n     if (u <= l && r <= v) return ST[id]; n     int mid = ((l + r) >> 1); n     int res1 = get((id << 1), l, mid, u, v); n     int res2 = get((id << 1 | 1), mid + 1, r, u, v); n     return (res1 + res2) % MOD; n   } n } IT1[MAXN], IT2[MAXN]; n int N, M; n char A[MAXN][MAXN]; n int dp[MAXN][MAXN][2]; n int sum1[MAXN][MAXN], sum2[MAXN][MAXN], pos1[MAXN][MAXN], pos2[MAXN][MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> N >> M; n   for (int i = (1), _b = (N); i <= _b; ++i) { n     for (int j = (1), _b = (M); j <= _b; ++j) { n       cin >> A[i][j]; n     } n   } n   for (int i = (1), _b = (N); i <= _b; ++i) { n     for (int j = (1), _b = (M); j <= _b; ++j) { n       sum1[i][j] = sum1[i][j - 1] + (A[i][j] ==  R ); n     } n   } n   for (int i = (1), _b = (N); i <= _b; ++i) { n     for (int j = (1), _b = (M); j <= _b; ++j) { n       sum2[i][j] = sum2[i - 1][j] + (A[i][j] ==  R ); n     } n   } n   if (N == 1 && M == 1) { n     if (A[1][1] ==  . ) n       cout << 1; n     else n       cout << 0; n     exit(0); n   } n   dp[1][1][0] = dp[1][1][1] = 1; n   IT1[1].update(1, 1, M, 1, 1, 1); n   IT2[1].update(1, 1, N, 1, 1, 1); n   for (int i = (1), _b = (N); i <= _b; ++i) { n     for (int j = (1), _b = (M); j <= _b; ++j) { n       int l = 0, r = j; n       while (r - l > 1) { n         int mid = (l + r) / 2; n         int sum = sum1[i][M] - sum1[i][mid]; n         if (sum <= M - j) n           r = mid; n         else n           l = mid; n       } n       pos1[i][j] = r; n       l = 0, r = i; n       while (r - l > 1) { n         int mid = (l + r) / 2; n         int sum = sum2[N][j] - sum2[mid][j]; n         if (sum <= N - i) n           r = mid; n         else n           l = mid; n       } n       pos2[i][j] = r; n     } n   } n   for (int i = (1), _b = (N); i <= _b; ++i) { n     for (int j = (1), _b = (M); j <= _b; ++j) { n       if (i == 1 && j == 1) continue; n       int pos = pos1[i][j]; n       if (pos != j) { n         dp[i][j][1] = IT1[i].get(1, 1, M, pos, j - 1); n         IT2[j].update(1, 1, N, i, i, dp[i][j][1]); n       } n       pos = pos2[i][j]; n       if (pos != i) { n         dp[i][j][0] = IT2[j].get(1, 1, N, pos, i - 1); n         IT1[i].update(1, 1, M, j, j, dp[i][j][0]); n       } n     } n   } n   cout << (dp[N][M][0] + dp[N][M][1]) % MOD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2e9; n const long long INFL = (long long)9e18; n const int MAXINT = ((~0) ^ (1 << 31)); n const long long MAXLL = ((~0) ^ ((long long)1 << 63)); n template <class T> n inline T pow2(T a) { n   return a * a; n } n template <class T> n inline bool mineq(T& a, T b) { n   return (a > b) ? (a = b, true) : false; n } n template <class T> n inline bool maxeq(T& a, T b) { n   return (a < b) ? (a = b, true) : false; n } n const int maxn = (int)1111; n ; n int n; n struct human { n   int h, w; n   void input() { cin >> w >> h; } n   bool operator<(const human& a) const { return h - w < a.h - a.w; } n } num[maxn]; n bool used[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < (int)(n); i++) num[i].input(); n   sort(num, num + n); n   long long ans = INF; n   for (int maxh = 1; maxh < (int)(maxn); maxh++) { n     fill(used, used + n, false); n     long long cnt = 0, width = 0, f = 1; n     for (int i = 0; i < (int)(n); i++) { n       if (num[i].h > maxh && num[i].w > maxh) f = false; n       if (num[i].h > maxh) { n         used[i] = true; n         cnt++; n       } n     } n     for (int i = 0; i < (int)(n); i++) { n       if (!used[i] && cnt + 1 <= n / 2 && num[i].h < num[i].w && n           num[i].w <= maxh) { n         used[i] = true; n         cnt++; n       } n       if (used[i]) n         width += num[i].h; n       else n         width += num[i].w; n     } n     if (f && cnt <= n / 2) mineq(ans, maxh * width); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000006; n int n, k; n int main() { n   int i, j; n   cin >> n >> k; n   if (k > n) { n     printf( -1 ); n     return 0; n   } n   if (k == 1 && n == 1) { n     printf( a ); n     return 0; n   } n   if (k == 1 && n > 1) { n     puts( -1 ); n     return 0; n   } n   int e = k - 2; n   if (e >= 0) { n     for (i = 0; i < n - e; i++) n       if (i & 1) n         printf( b ); n       else n         printf( a ); n     for (i = 0; i < e; i++) printf( %c ,  c  + i); n   } else if (e < 0) { n     for (i = 0; i < n; i++) n       if (i & 1) n         printf( b ); n       else n         printf( a ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e5 + 20, INF = 1e9 + 20; n int n, h[N], pmx[N], pmn[N], ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> h[i]; n   for (int i = 0; i < n; i++) pmx[i] = i ? max(h[i], pmx[i - 1]) : h[i]; n   pmn[n] = INF; n   for (int i = n - 1; ~i; i--) pmn[i] = min(h[i], pmn[i + 1]); n   for (int i = 0; i < n; i++) { n     if (pmx[i] <= pmn[i + 1]) ans++; n   } n   return cout << ans, 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<map<int, vector<string> > > A(m + 1); n   for (int x = 0; x < n; x++) { n     string name; n     int r, points; n     cin >> name >> r >> points; n     A[r][points].push_back(name); n   } n   for (int x = 1; x <= m; x++) { n     vector<string> tmp; n     for (auto it = A[x].rbegin(); it != A[x].rend(); it++) { n       for (string s : it->second) tmp.push_back(s); n       if (tmp.size() >= 2) break; n     } n     if (tmp.size() > 2) n       cout <<  ? n ; n     else n       cout << tmp[0] <<     << tmp[1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[5005]; n int q, x; n bool bfind(int p) { n   int lf = 0; n   int rt = n - 1; n   int mid; n   while (lf <= rt) { n     mid = (lf + rt) / 2; n     if (a[mid] < p) { n       lf = mid + 1; n     } else if (a[mid] > p) { n       rt = mid - 1; n     } else n       return true; n   } n   return false; n } n int main() { n   while (scanf( %d %d , &n, &k) != EOF) { n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     scanf( %d , &q); n     while (q--) { n       int mn = k + 1; n       scanf( %d , &x); n       for (int i = n - 1; i >= 0; i--) { n         for (int j = 0; j <= k; j++) { n           if (a[i] * j == x) { n             mn = min(mn, j); n             break; n           } n           if (a[i] * j > x) break; n           for (int m = 1; m <= k - j; m++) { n             if ((x - a[i] * j) % m != 0) continue; n             int tmp = (x - a[i] * j) / m; n             if (bfind(tmp)) mn = min(mn, j + m); n           } n         } n       } n       if (mn > k) n         printf( -1 n ); n       else n         printf( %d n , mn); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, k, s; n   cin >> n >> m >> k >> s; n   vector<vector<long long>> a(2, vector<long long>(n)); n   for (long long i = 0; i < 2; ++i) { n     for (long long j = 0; j < n; ++j) { n       cin >> a[i][j]; n     } n   } n   vector<vector<pair<long long, long long>>> g(2); n   for (long long i = 0; i < m; ++i) { n     long long x, y; n     cin >> x >> y; n     g[x - 1].push_back({y, i}); n   } n   for (long long i = 0; i < 2; ++i) { n     sort(g[i].begin(), g[i].end()); n   } n   auto ok = [&](long long q) { n     vector<long long> p(2, numeric_limits<long long>::max()); n     vector<long long> day(2, -1); n     for (long long i = 0; i <= q; ++i) { n       for (long long j = 0; j < 2; ++j) { n         if (p[j] > a[j][i]) { n           p[j] = a[j][i]; n           day[j] = i; n         } n       } n     } n     assert(day[0] != -1 && day[1] != -1); n     long long i = 0, j = 0; n     vector<pair<long long, long long>> ans; n     long long ss = s; n     auto buyi = [&]() { n       ss -= p[0] * g[0][i].first; n       ans.push_back({g[0][i].second, day[0]}); n       ++i; n     }; n     auto buyj = [&]() { n       ss -= p[1] * g[1][j].first; n       ans.push_back({g[1][j].second, day[1]}); n       ++j; n     }; n     while ((i < g[0].size() || j < g[1].size()) && ans.size() < k) { n       if (i >= g[0].size()) { n         buyj(); n       } else if (j >= g[1].size()) { n         buyi(); n       } else { n         if (p[0] * g[0][i].first < p[1] * g[1][j].first) n           buyi(); n         else n           buyj(); n       } n     } n     if (ss < 0 || ans.size() < k) ans.clear(); n     return ans; n   }; n   if (ok(n - 1).size() == 0) { n     cout <<  -1 n ; n     return 0; n   } n   long long l = 0, r = n - 1; n   while (r - l > 1) { n     long long mid = (l + r) >> 1; n     if (ok(mid).size()) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   if (!ok(l).size()) { n     l = r; n   } n   cout << l + 1 <<   n ; n   for (auto [x, y] : ok(l)) { n     cout << x + 1 <<     << y + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> v; n int num[300005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int l, r, x; n   memset(num, 0, sizeof(num)); n   for (int i = 1; i <= n; i++) { n     v.insert(i); n   } n   set<int>::iterator it, s; n   bool flag = true; n   for (int i = 0; i < m; i++) { n     flag = true; n     scanf( %d%d%d , &l, &r, &x); n     it = v.upper_bound(l - 1); n     s = it; n     while (it != v.end() && (*it) <= r) { n       flag = false; n       num[*it] = x; n       it++; n     } n     v.erase(s, it); n     if (!flag) v.insert(x); n   } n   num[x] = 0; n   printf( %d , num[1]); n   for (int i = 2; i <= n; i++) printf(  %d , num[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 100005; n const int mod = 998244353; n int a[mx], y, z; n long long sum[mx]; n int as[mx], bs[mx]; n bool v[mx]; n map<int, int> r[mx]; n int query(long long i, int x) { n   long long ans = 0; n   if (i == 0) return 0; n   if (x == 1) return sum[i]; n   if (x < 20) { n     int d = r[i][x]; n     if (d != 0) return d; n   } n   if (x == as[i]) { n     return bs[i]; n   } n   if (a[i] <= x) n     z = a[i]; n   else { n     y = (x + a[i] - 1) / x; n     ans = (ans + i * (y - 1)) % mod; n     z = a[i] / y; n   } n   int k = (ans + query(i - 1, z)) % mod; n   if (x < 20) { n     r[i][x] = k; n   } else { n     as[i] = x; n     bs[i] = k; n   } n   return k; n } n void oyl(int n) { n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     scanf( %d , a + i); n     sum[i] = (sum[i - 1] + i * a[i] - i) % mod; n   } n   for (int i = 1; i <= n; i++) { n     ans = (ans + query(i - 1, a[i])) % mod; n   } n   printf( %lld n , ans); n   return; n } n int main() { n   int t, n; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     oyl(n); n     if (t != 0) n       for (int _ = 1; _ <= n; _++) { n         r[_].clear(); n         as[_] = 0; n         bs[_] = 0; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6; n const int LOGN = 20; n int n, k; n vector<int> graph[MAXN + 5]; n int pa[LOGN + 1][MAXN + 5]; n int depth[MAXN + 5]; n int par[MAXN + 5]; n void dfs(int u, int p, int d) { n   depth[u] = d; n   par[u] = p; n   pa[0][u] = p; n   for (int j = 1; j <= LOGN; j++) { n     if (pa[j - 1][u] != -1) n       pa[j][u] = pa[j - 1][pa[j - 1][u]]; n     else n       pa[j][u] = -1; n   } n   for (auto v : graph[u]) { n     if (v != p) dfs(v, u, d + 1); n   } n } n bool vis[MAXN + 5]; n int main() { n   int i, j, d; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   dfs(n, -1, 0); n   int rem = n - k; n   vis[n] = true; n   rem--; n   for (int i = n; i >= 1; i--) { n     if (vis[i]) continue; n     int u = i; n     for (int j = LOGN; j >= 0; j--) { n       int par = pa[j][u]; n       if (par != -1) { n         if (!vis[par]) u = par; n       } n     } n     while (par[u] != -1 && !vis[par[u]]) u = par[u]; n     int cnt = (depth[i] - depth[u] + 1); n     if (cnt <= rem) { n       int curr = i; n       while (!vis[curr]) { n         vis[curr] = true; n         curr = par[curr]; n       } n       rem -= cnt; n     } n   } n   int cntprint = 0; n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       cntprint++; n       printf( %d  , i); n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i; n   cin >> t; n   for (i = 0; i < t; i++) { n     int n, k1, k2, j, f; n     cin >> n >> k1 >> k2; n     int a[100], b[100], sum, hum; n     for (j = 0; j < k1; j++) { n       cin >> a[j]; n     } n     sum = a[0]; n     for (j = 0; j < k1; j++) { n       if (a[j] > sum) { n         sum = a[j]; n       } n     } n     for (f = 0; f < k2; f++) { n       cin >> b[f]; n     } n     hum = b[0]; n     for (f = 0; f < k2; f++) { n       if (b[f] > hum) { n         hum = b[f]; n       } n     } n     if (sum > hum) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100010; n int ans = -1, ansd, a[Maxn], b[Maxn], n, m; n inline int Pow(int x, int y, int mod) { n   int s = 1; n   for (; y; y >>= 1, x = 1ll * x * x % mod) n     if (y & 1) s = 1ll * s * x % mod; n   return s; n } n inline bool find(int *a, int n, int x) { n   int t = lower_bound(a + 1, a + n + 1, x) - a; n   return a[t] == x; n } n inline void sol(int *a, int n) { n   if (n == 1) { n     ans = a[1], ansd = 1; n     return; n   } n   int tmp = a[2] - a[1], cnt = 0; n   for (int i = 1; i <= n; i++) n     if (find(a, n, (a[i] + tmp) % m)) cnt++; n   ansd = 1ll * tmp * Pow(n - cnt, m - 2, m) % m; n   int d = (m - ansd) % m; n   for (int i = 1; i <= n; i++) n     if (!find(a, n, (a[i] + d) % m)) { n       if (ans == -1) n         ans = a[i]; n       else { n         ans = -1; n         return; n       } n     } n } n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   if (n == 1 || n == m) return printf( %d %d n , a[1], 1), 0; n   if (2 * n < m) n     sol(a, n); n   else { n     int t = 0; n     for (int i = 0; i < m; i++) n       if (!find(a, n, i)) b[++t] = i; n     sol(b, t); n     if (ans != -1) (ans += 1ll * ansd * t % m) %= m; n   } n   if (ans == -1) return printf( -1 n ), 0; n   return printf( %d %d n , ans, ansd), 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int arr[MAXN]; n int main() { n   int N, ans = 0; n   cin >> N; n   for (int i = 0; i < N; i++) cin >> arr[i]; n   sort(arr, arr + N); n   for (int i = 0, j = 1; i < N; i++) { n     while (arr[i] >= arr[j] && j <= N - 1) j++; n     if (j == N) break; n     ans++; n     j++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, dp[108][108][28], dis[108][108]; n int DP(int s, int t, int w) { n   if (dp[s][t][w -  a ] >= 0) return dp[s][t][w -  a ]; n   for (int i = 1; i <= n; ++i) n     if (dis[s][i] >= w && (DP(t, i, dis[s][i]) == 0)) n       return dp[s][t][w -  a ] = 1; n   return dp[s][t][w -  a ] = 0; n } n int main() { n   char z; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d %d %c , &x, &y, &z); n     dis[x][y] = z; n   } n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) DP(i, j,  a ); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) printf( %c , dp[i][j][0] ?  A  :  B ); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c; n   std::cin >> a >> b >> c; n   int weeks = std::min(a / 3, std::min(b / 2, c / 2)); n   a -= weeks * 3; n   b -= weeks * 2; n   c -= weeks * 2; n   int ans(0); n   for (int i = 1; i <= 7; ++i) { n     int ac = a; n     int bc = b; n     int cc = c; n     int zile(0); n     int curr_zi = i; n     while (ac || bc || cc) { n       ++zile; n       if (curr_zi == 8) curr_zi -= 7; n       if (curr_zi == 1) { n         ac--; n       } else if (curr_zi == 2) { n         bc--; n       } else if (curr_zi == 3) { n         cc--; n       } else if (curr_zi == 4) { n         ac--; n       } else if (curr_zi == 5) { n         cc--; n       } else if (curr_zi == 6) { n         bc--; n       } else { n         ac--; n       } n       if (ac < 0 || bc < 0 || cc < 0) { n         zile--; n         break; n       } n       curr_zi++; n     } n     ans = std::max(ans, zile); n   } n   std::cout << weeks * 7 + ans; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> ma; n vector<int> ind; n vector<int> adj[205]; n string x[205], y; n int vis[205], ht[205]; n void dfs(int u) { n   vis[u] = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     int cur = adj[u][i]; n     if (!vis[cur]) { n       vis[cur] = 1; n       ht[cur] = ht[u] + 1; n       dfs(cur); n     } n   } n } n int main() { n   int n; n   string str[205]; n   cin >> n; n   for (int i = 0; i < n + 1; i++) { n     getline(cin, str[i]); n     transform(str[i].begin(), str[i].end(), str[i].begin(), ::tolower); n   } n   string z[100]; n   ma[ polycarp ] = 1; n   int k = 2; n   ht[1] = 1; n   for (int i = 1; i < n + 1; i++) { n     for (int j = 0; j < str[i].size(); j++) { n       if (str[i][j] ==    ) ind.push_back(j); n     } n     x[i] = str[i].substr(0, ind[0]); n     y = str[i].substr(ind[0] + 1, ind[1] - ind[0] - 1); n     z[i] = str[i].substr(ind[1] + 1, str[i].size() - ind[1]); n     if (!ma[x[i]]) ma[x[i]] = k, k++; n     if (!ma[z[i]]) ma[z[i]] = k, k++; n     ind.clear(); n   } n   for (int i = 1; i < n + 1; i++) { n     adj[ma[z[i]]].push_back(ma[x[i]]); n   } n   dfs(1); n   int *max1; n   max1 = max_element(ht + 1, ht + 205); n   cout << *max1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } n int rev(int num) { n   int dig[10], tot = 0, s = 0; n   for (; num; num /= 10) dig[tot++] = num % 10; n   for (int i = 0; i < tot; i++) s = s * 10 + dig[i]; n   return s; n } n pair<int, int> p[maxn], r[maxn]; n void init() { n   for (int i = 1; i <= 100000; i++) { n     int re = rev(i); n     int g = gcd(re, i); n     p[i] = make_pair(i / g, re / g); n     r[i] = make_pair(re / g, i / g); n   } n } n int main() { n   init(); n   map<pair<int, int>, int> mx, my; n   int nx, ny, w; n   cin >> nx >> ny >> w; n   for (int i = 1; i <= ny; i++) ++my[p[i]]; n   int x = -1, y = -1; n   for (int i = 1, j = ny, k = 0; i <= nx; i++) { n     ++mx[p[i]]; n     k += my[r[i]]; n     while (j > 0 && k >= w) { n       if (x == -1 || (long long)x * y > (long long)i * j) { n         x = i; n         y = j; n       } n       my[p[j]]--; n       k -= mx[r[j]]; n       j--; n       if (k < w) { n         j++; n         my[p[j]]++; n         k += mx[r[j]]; n         break; n       } n     } n   } n   if (x == -1) { n     puts( -1 ); n   } else { n     printf( %d %d n , x, y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   int opx, t, c; n   cin >> t >> c; n   int dfx = 1; n   long long lorenzo = 0; n   while (c--) { n     cin >> opx; n     if (dfx != 1) lorenzo += t - dfx + 1; n     dfx = opx + 1; n   } n   cout << lorenzo; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using vi = vector<int>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, c; n   cin >> n >> m >> c; n   vector<int> v1, v2; n   for (int i = 0; i < m; i++) { n     int t; n     cin >> t; n     if (t <= c / 2) { n       int ind = upper_bound((v1).begin(), (v1).end(), t) - v1.begin(); n       if (ind == v1.size()) v1.emplace_back(); n       v1[ind] = t; n       cout << ind + 1 << endl; n     } else { n       int ind = upper_bound((v2).begin(), (v2).end(), -t) - v2.begin(); n       if (ind == v2.size()) v2.emplace_back(); n       v2[ind] = -t; n       cout << n - ind << endl; n     } n     if (v1.size() + v2.size() == n) return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int bit[1000010] = {0}; n long long int get_sum(int i) { n   long long int ans = 0; n   while (i > 0) { n     ans += bit[i]; n     i -= i & (-i); n   } n   return ans; n } n void update(int i, int n) { n   while (i <= n) { n     bit[i] += 1; n     i += i & (-i); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   long long int arr[n], small[n], big[n], tmp[n]; n   long long int max1 = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     tmp[i] = arr[i]; n   } n   sort(tmp, tmp + n); n   for (int i = 0; i < n; i++) { n     arr[i] = lower_bound(tmp, tmp + n, arr[i]) - tmp + 1; n     if (arr[i] > max1) max1 = arr[i]; n   } n   long long int ans = 0; n   for (int i = (n - 1); i >= 0; i--) { n     small[i] = get_sum(arr[i] - 1); n     update(arr[i], max1); n   } n   for (int i = 0; i <= max1; i++) bit[i] = 0; n   for (int i = 0; i < n; i++) { n     big[i] = i - get_sum(arr[i]); n     update(arr[i], max1); n     ans += big[i] * small[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s, ans, a[200001], f[200001]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (register long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     s ^= a[i]; n     long long x = a[i]; n     for (register long long j = 60; j > -1; --j) { n       if ((x >> j) < 1) continue; n       if (f[j] < 1) { n         f[j] = x; n         break; n       } else n         x ^= f[j]; n     } n     if (x > 0) ++ans; n   } n   if (s < 1) n     cout <<  -1 n ; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool upmin(T &x, T y) { n   return y < x ? x = y, 1 : 0; n } n template <typename T> n inline bool upmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n const long double eps = 1e-11; n const long double pi = acos(-1); n const int oo = 1 << 30; n const long long loo = 1ll << 62; n const int mods = 1e9 + 7; n const int MAXN = 1000005; n const int INF = 0x3f3f3f3f; n inline int read() { n   int f = 1, x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n int a[45], g[45], f[MAXN][45], id[MAXN]; n int upd(int x, int y) { return x + y >= mods ? x + y - mods : x + y; } n signed main() { n   int n = read(), m, flag = 0; n   if (n == 2) { n     puts( 1 ); n     return 0; n   } n   for (m = 0; 1 << m <= n; m++) a[m] = 1 << m; n   m--; n   if (a[m] * 3 / 2 <= n) { n     flag = 1; n     for (int i = 0; i < m; i++) a[m + i + 1] = a[i] * 3; n     m = m + m; n   } n   sort(a, a + m + 1, [&](int x, int y) { return x > y; }); n   for (int i = 0; i <= m; i++) id[a[i]] = i; n   for (int i = 0; i <= m; i++) g[i] = n / a[i]; n   f[1][0] = 1; n   if (flag) f[1][1] = 1; n   for (int i = 1; i < n; i++) n     for (int j = 0; j < m; j++) { n       f[i + 1][j] = upd(f[i + 1][j], 1ll * f[i][j] * max(g[j] - i, 0) % mods); n       if (a[j] % 2 == 0) n         f[i + 1][id[a[j] / 2]] = n             upd(f[i + 1][id[a[j] / 2]], n                 1ll * f[i][j] * max(g[id[a[j] / 2]] - g[j], 0) % mods); n       if (a[j] % 3 == 0) n         f[i + 1][id[a[j] / 3]] = n             upd(f[i + 1][id[a[j] / 3]], n                 1ll * f[i][j] * max(g[id[a[j] / 3]] - g[j], 0) % mods); n     } n   int ans = 0; n   for (int i = n, fac = 1; i >= 1; i--) n     ans = upd(ans, 1ll * fac * f[i][m] % mods), n     fac = 1ll * fac * (n - i + 1) % mods; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long a, b, c; n     scanf( %lld%lld%lld , &a, &b, &c); n     long long ans = 0x7f7f7f7f7f7f7f7f; n     long long ca, cb, cc; n     for (long long i = 1ll; i <= 15000ll; i++) { n       long long a1, c1; n       long long minx1 = 0x7f7f7f7f7f7f7f7f; n       for (long long j = 1; j <= sqrt(i); j++) { n         if (i % j == 0) { n           if (minx1 > abs(i / j - a)) { n             minx1 = abs(i / j - a); n             a1 = i / j; n           } n           if (minx1 > abs(j - a)) { n             minx1 = abs(j - a); n             a1 = j; n           } n         } n       } n       if (abs(max(c / i, 1ll) * i - c) > abs((c / i + 1) * i - c)) { n         c1 = (c / i + 1) * i; n       } else { n         c1 = max(c / i, 1ll) * i; n       } n       if (abs(a1 - a) + abs(i - b) + abs(c1 - c) < ans) { n         ans = abs(a1 - a) + abs(i - b) + abs(c1 - c); n         ca = a1, cb = i, cc = c1; n       } n     } n     printf( %lld n , ans); n     printf( %lld %lld %lld n , ca, cb, cc); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int col[501]; n int tcol[501]; n int edge[501][501]; n void docol(int v, int c) { n   col[v] = c; n   for (int i = 1; i <= v; ++i) { n     if (edge[v][i] && col[i] == -1) { n       docol(i, c); n     } n   } n } n int main(void) { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int k; n   cin >> k; n   memset(tcol, -1, sizeof(tcol)); n   memset(col, -1, sizeof(col)); n   memset(edge, 0, sizeof(edge)); n   for (int i = 1; i <= k; ++i) { n     int a, b; n     cin >> a >> b; n     edge[a][b] = 1; n     edge[b][a] = 1; n   } n   int b = 0; n   for (int i = 1; i <= n; ++i) { n     bool flag = true; n     for (int j = 1; j <= n; ++j) { n       if (i == j) continue; n       if (edge[i][j] == 0) { n         flag = false; n         break; n       } n     } n     if (flag) { n       tcol[i] = 1; n       b++; n     } n   } n   if (b == n) { n     cout <<  Yes n ; n     for (int i = 1; i <= n; ++i) cout <<  b ; n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     if (tcol[i] != -1) continue; n     memset(col, -1, sizeof(col)); n     for (int j = 1; j <= n; ++j) col[j] = tcol[j]; n     docol(i, 0); n     for (int j = 1; j <= n; ++j) { n       if (col[j] == -1) col[j] = 2; n     } n     bool flag = false; n     for (int ii = 1; ii <= n && !flag; ++ii) { n       for (int j = 1; j <= n && !flag; ++j) { n         if (ii == j) continue; n         if (edge[ii][j] == 0) { n           if ((col[ii] == 0 && col[j] == 2) || (col[ii] == 2 && col[j] == 0)) n             continue; n           else { n             flag = true; n             break; n           } n         } n         if (col[ii] == 1 || col[j] == 1) continue; n         if (col[ii] == 0 && (col[j] == 0 || col[j] == 1)) continue; n         if (col[ii] == 2 && (col[j] == 2 || col[j] == 1)) continue; n         flag = true; n         break; n       } n     } n     if (flag) continue; n     cout <<  Yes n ; n     for (int ii = 1; ii <= n; ++ii) n       cout << (col[ii] == 0 ?  a  : col[ii] == 1 ?  b  :  c ); n     return 0; n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, s, i = 1, count = 1; n   scanf( %d %d , &n, &s); n   while (i * n < s) { n     count++; n     i++; n   } n   printf( %d , count); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5, P = 1e9 + 7; n struct Edge { n   int v, nxt, col; n   long long w; n } e[N << 1]; n int head[N], size = 1, ext; n long long ans = 1; n long long qpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % P; n     a = a * a % P; n     b >>= 1; n   } n   return res; n } n void link(int u, int v, long long w, int col) { n   e[++size].v = v; n   e[size].nxt = head[u]; n   e[size].w = w; n   e[size].col = col; n   head[u] = size; n } n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n namespace sdh { n struct Node { n   int v, w, col; n   Node() {} n   Node(int _v, int _w, int _col) { n     v = _v; n     w = _w; n     col = _col; n   } n } stk[N]; n int top; n vector<Node> e[N]; n void dfs(int u, int fa) { n   top = 0; n   for (Node v : e[u]) { n     if (v.v == fa) continue; n     stk[++top] = v; n   } n   int lst = u; n   for (int i = 1; i <= top - 2; ++i) { n     Node c = stk[i]; n     link(lst, c.v, c.w, c.col); n     link(c.v, lst, c.w, c.col); n     ++ext; n     link(lst, ext, 1, -1); n     link(ext, lst, 1, -1); n     lst = ext; n   } n   if (top > 0) n     link(lst, stk[top].v, stk[top].w, stk[top].col), n         link(stk[top].v, lst, stk[top].w, stk[top].col); n   if (top > 1) n     link(lst, stk[top - 1].v, stk[top - 1].w, stk[top - 1].col), n         link(stk[top - 1].v, lst, stk[top - 1].w, stk[top - 1].col); n   for (Node v : e[u]) { n     if (v.v == fa) continue; n     dfs(v.v, u); n   } n } n }  // namespace sdh n int max(int x, int y) { return x > y ? x : y; } n int n, RT, MN, sze[N], SZE; n bool vis[N << 1]; n void findrt(int u, int fa) { n   sze[u] = 1; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa || vis[i]) continue; n     findrt(v, u); n     sze[u] += sze[v]; n     if (MN > max(sze[v], SZE - sze[v])) { n       MN = max(sze[v], SZE - sze[v]); n       RT = i; n     } n   } n } n struct Q { n   int x, y, opt; n   long long val; n } q[N], type[N]; n bool cmp(Q p, Q q) { n   if (p.x != q.x) n     return p.x < q.x; n   else n     return p.opt < q.opt; n } n int tot; n void dfsA(int u, int fa, int B, int W, long long val) { n   if (u <= n) { n     q[++tot].x = 2 * W - B; n     q[tot].y = 2 * B - W; n     q[tot].val = val; n     q[tot].opt = 1; n   } n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa || vis[i]) continue; n     if (e[i].col == 0) dfsA(v, u, B + 1, W, val * e[i].w % P); n     if (e[i].col == 1) dfsA(v, u, B, W + 1, val * e[i].w % P); n     if (e[i].col == -1) dfsA(v, u, B, W, val * e[i].w % P); n   } n } n void dfsB(int u, int fa, int B, int W, long long val) { n   if (u <= n) { n     q[++tot].x = B - 2 * W; n     q[tot].y = W - 2 * B; n     q[tot].val = val; n     q[tot].opt = 0; n   } n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa || vis[i]) continue; n     if (e[i].col == 0) dfsB(v, u, B + 1, W, val * e[i].w % P); n     if (e[i].col == 1) dfsB(v, u, B, W + 1, val * e[i].w % P); n     if (e[i].col == -1) dfsB(v, u, B, W, val * e[i].w % P); n   } n } n void Cdq(int l, int r) { n   if (l >= r) return; n   int mid = (l + r) >> 1; n   Cdq(l, mid); n   Cdq(mid + 1, r); n   long long sum = 0, prod = 1; n   int i = l, j = mid + 1; n   while (j <= r) { n     while (i <= mid && q[i].y <= q[j].y) { n       if (q[i].opt == 0) { n         ++sum; n         (prod *= q[i].val) %= P; n       } n       ++i; n     } n     if (q[j].opt == 1) { n       ans = ans * qpow(q[j].val, sum) % P * prod % P; n     } n     ++j; n   } n   i = l; n   j = mid + 1; n   int tot = 0; n   while (i <= mid && j <= r) { n     if (q[i].y < q[j].y) n       type[++tot] = q[i++]; n     else n       type[++tot] = q[j++]; n   } n   while (i <= mid) type[++tot] = q[i++]; n   while (j <= r) type[++tot] = q[j++]; n   for (int i = l; i <= r; ++i) q[i] = type[i - l + 1]; n } n void calc(int u) { n   int U = e[u].v, V = e[u ^ 1].v; n   tot = 0; n   if (e[u].col == 0) dfsA(U, 0, 1, 0, e[u].w); n   if (e[u].col == 1) dfsA(U, 0, 0, 1, e[u].w); n   if (e[u].col == -1) dfsA(U, 0, 0, 0, e[u].w); n   dfsB(V, 0, 0, 0, 1); n   sort(q + 1, q + tot + 1, cmp); n   Cdq(1, tot); n } n void Divide(int u) { n   if (!u) return; n   vis[u] = vis[u ^ 1] = 1; n   calc(u); n   int U = e[u].v, V = e[u ^ 1].v, totsze = SZE, szeU = sze[U], szeV = sze[V]; n   RT = 0; n   MN = P; n   SZE = szeU > szeV ? totsze - szeV : szeU; n   findrt(U, 0); n   Divide(RT); n   RT = 0; n   MN = P; n   SZE = szeV > szeU ? totsze - szeU : szeV; n   findrt(V, 0); n   Divide(RT); n } n int main() { n   n = read(); n   for (int i = 1; i < n; ++i) { n     int u = read(), v = read(), w = read(), col = read(); n     sdh::e[u].push_back(sdh::Node(v, w, col)); n     sdh::e[v].push_back(sdh::Node(u, w, col)); n   } n   ext = n; n   sdh::dfs(1, 0); n   RT = 0; n   SZE = ext; n   MN = P; n   findrt(1, 0); n   Divide(RT); n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int max_points = -(50 * 50 + 1); n   string leader; n   for (int i = 0; i < n; i++) { n     string handle; n     int plus, minus, a, b, c, d, e; n     cin >> handle >> plus >> minus >> a >> b >> c >> d >> e; n     if (plus * 100 - minus * 50 + a + b + c + d + e > max_points) n       max_points = plus * 100 - minus * 50 + a + b + c + d + e, leader = handle; n   } n   cout << leader <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod(long long a, long long b) { n   long long ans; n   if (b == 0) n     return -1; n   else { n     ans = (a < 0 ? mod(((a % b) + b), b) : a % b); n   } n   return ans; n } n vector<int> cntl(100003, 0), cntr(100003, 0), cntt(100003, 0), cntb(100003, 0); n int n, m, d; n void solve() { n   int cl, cb, cr, ct; n   cin >> d >> n >> m; n   vector<int> x1(d + 1), x2(d + 1), y1(d + 1), y2(d + 1); n   for (int i = 1; i < d + 1; i++) { n     cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; n     cntl[min(x1[i], x2[i])]++; n     cntr[max(x1[i], x2[i])]++; n     cntt[min(y1[i], y2[i])]++; n     cntb[max(y1[i], y2[i])]++; n   } n   for (int i = 1; i < 100003; i++) { n     cntl[i] += cntl[i - 1]; n     cntt[i] += cntt[i - 1]; n   } n   for (int i = 100001; i >= 1; i--) { n     cntb[i] += cntb[i + 1]; n     cntr[i] += cntr[i + 1]; n   } n   cin >> cl >> cr >> ct >> cb; n   vector<pair<pair<int, int>, pair<int, int> > > cnt(d); n   for (int i = 1; i < d + 1; i++) { n     cnt[i - 1].first.first = cntl[max(x1[i], x2[i]) - 1] - (x1[i] != x2[i]); n     cnt[i - 1].first.second = cntr[min(x1[i], x2[i]) + 1] - (x1[i] != x2[i]); n     cnt[i - 1].second.first = cntt[max(y1[i], y2[i]) - 1] - (y1[i] != y2[i]); n     cnt[i - 1].second.second = cntb[min(y1[i], y2[i]) + 1] - (y1[i] != y2[i]); n   } n   pair<pair<int, int>, pair<int, int> > target = {{cl, cr}, {ct, cb}}; n   if (find((cnt).begin(), (cnt).end(), target) != cnt.end()) { n     cout << distance(cnt.begin(), find((cnt).begin(), (cnt).end(), target)) + 1 n          << endl; n   } else n     cout << -1 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n using namespace std; n namespace ywy { n inline int get() { n   int n = 0; n   char c; n   while ((c = getchar()) || 23333) { n     if (c >=  0  && c <=  9 ) break; n     if (c ==  - ) goto s; n   } n   n = c -  0 ; n   while ((c = getchar()) || 23333) { n     if (c >=  0  && c <=  9 ) n       n = n * 10 + c -  0 ; n     else n       return (n); n   } n s: n   while ((c = getchar()) || 23333) { n     if (c >=  0  && c <=  9 ) n       n = n * 10 - c +  0 ; n     else n       return (n); n   } n } n typedef struct _b { n   int dest; n   int nxt; n } bian; n bian memchi[1000001]; n int gn = 1, heads[500001], deep[500001], ance[500001][19], lg[500001]; n inline void add(int s, int t) { n   memchi[gn].dest = t; n   memchi[gn].nxt = heads[s]; n   heads[s] = gn; n   gn++; n } n int stk[500001]; n vector<int> vec[500001]; n int dfn[500001], gdfn = 1; n void dfs(int pt) { n   dfn[pt] = gdfn; n   gdfn++; n   for (register int i = heads[pt]; i; i = memchi[i].nxt) { n     deep[memchi[i].dest] = deep[pt] + 1; n     dfs(memchi[i].dest); n   } n } n long long dp[500001]; n inline int lca(int a, int b) { n   if (deep[a] > deep[b]) swap(a, b); n   for (register int i = lg[deep[b]]; i >= 0; i--) { n     if (deep[ance[b][i]] >= deep[a]) b = ance[b][i]; n   } n   if (a == b) return (a); n   for (register int i = lg[deep[a]]; i >= 0; i--) { n     if (ance[a][i] != ance[b][i]) a = ance[a][i], b = ance[b][i]; n   } n   return (ance[a][0]); n } n inline int cmp(const int &a, const int &b) { return (dfn[a] < dfn[b]); } n inline void solve(int x) { n   int sp = 0; n   long long sig = 0; n   for (register int i = 1; i < vec[x].size(); i++) { n     int dpb = deep[lca(vec[x][i - 1], vec[x][i])]; n     while (sp) { n       int a = deep[lca(vec[x][stk[sp - 1]], vec[x][i])]; n       if (a < dpb) { n         sig += (i - stk[sp - 1] - 1) * (long long)dpb; n         break; n       } n       if (sp == 1) { n         sig -= (stk[sp - 1] + 1) * n                (long long)deep[lca(vec[x][stk[sp - 1]], vec[x][i - 1])]; n       } else n         sig -= (stk[sp - 1] - stk[sp - 2]) * n                (long long)deep[lca(vec[x][stk[sp - 1]], vec[x][i - 1])]; n       sp--; n     } n     if (!sp) sig += i * (long long)dpb; n     stk[sp] = i - 1; n     sp++; n     dp[vec[x][i]] += sig; n   } n } n void ywymain() { n   deep[0] = -1; n   int n = get(), rt = 0; n   lg[0] = -1; n   for (register int i = 1; i <= n; i++) { n     int f = get(); n     ance[i][0] = f; n     if (!f) n       rt = i; n     else n       add(f, i); n     lg[i] = lg[i - 1] + (i == (i & -i)); n   } n   deep[rt] = 1; n   dfs(rt); n   for (register int i = 1; i <= n; i++) vec[deep[i]].push_back(i); n   for (register int i = 1; i <= lg[n]; i++) { n     for (register int j = 1; j <= n; j++) n       ance[j][i] = ance[ance[j][i - 1]][i - 1]; n   } n   for (register int i = 2; i <= n && vec[i].size(); i++) { n     sort(vec[i].begin(), vec[i].end(), cmp); n     for (register int j = 0; j < vec[i].size(); j++) { n       dp[vec[i][j]] = dp[ance[vec[i][j]][0]] + deep[ance[vec[i][j]][0]]; n     } n     solve(i); n     reverse(vec[i].begin(), vec[i].end()); n     solve(i); n   } n   for (register int i = 1; i <= n; i++) printf( %lld  , dp[i]); n } n }  // namespace ywy n int main() { n   ywy::ywymain(); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int a[10][10]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       scanf( %d , &a[i][j]); n     } n   } n   for (int k = 0; k < n; ++k) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         a[i][j] = min(a[i][j], a[i][k] + a[k][j]); n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       ans = max(ans, a[i][j]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( -ffloat-store ) n using namespace std; n const long long mod = 998244353; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n vector<long long> v; n long long n, k; n void solve(long long l, long long r) { n   if (k == 0 || l + 1 == r) { n     return; n   } n   long long mid = l + (r - l) / 2; n   k -= 2; n   swap(v[mid], v[mid - 1]); n   solve(l, mid); n   solve(mid, r); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     cin >> n >> k; n     if (k % 2 == 0) { n       cout << -1; n       return 0; n     } n     for (long long i = 1; i <= n; i++) v.push_back(i); n     k--; n     if (k > 0) { n       solve(0, n); n     } n     if (k > 0) { n       cout << -1; n       return 0; n     } n     for (long long i = 0; i < n; i++) cout << v[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const int MAXN = 1000100; n long long mpow(long long b, int e) { n   if (e == 0) return 1; n   long long v = mpow(b, e / 2); n   v = (v * v) % MOD; n   if (e & 1) v = (v * b) % MOD; n   return v; n } n long long inv(long long b) { return mpow(b % MOD, MOD - 2); } n int N; n vector<int> v[MAXN]; n int ct[MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int i = 0; i < N; i++) { n     int K; n     cin >> K; n     for (int j = 0; j < K; j++) { n       int x; n       cin >> x; n       v[i].push_back(x); n       ct[x]++; n     } n   } n   long long res = 0; n   for (int i = 0; i < N; i++) { n     for (int cv : v[i]) { n       long long prob = (inv(v[i].size() * (long long)N)) % MOD; n       prob = (prob * ct[cv]) % MOD; n       prob = (prob * inv(N)) % MOD; n       res = (res + prob) % MOD; n     } n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n vector<vector<int> > nod; n vector<int> sol; n void bfs(int n) { n   sol = vector<int>(n + 10); n   sol[1] = 1; n   queue<int> Q; n   Q.push(1); n   while (!Q.empty()) { n     int a = Q.front(); n     Q.pop(); n     for (int j = 0; j < nod[a].size(); j++) n       if (!sol[nod[a][j]]) { n         sol[nod[a][j]] = sol[a] + 1; n         Q.push(nod[a][j]); n       } n   } n } n int main() { n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     nod = vector<vector<int> >(n + 5); n     for (int i = 1; i <= m; i++) { n       int nod1, nod2; n       cin >> nod1 >> nod2; n       nod[nod1].push_back(nod2); n       nod[nod2].push_back(nod1); n     } n     bfs(n); n     vector<int> nr1, nr2; n     for (int i = 1; i <= n; i++) { n       if (sol[i] & 1) n         nr1.push_back(i); n       else n         nr2.push_back(i); n     } n     if (nr1.size() < nr2.size()) { n       cout << nr1.size() <<   n ; n       for (int i = 0; i < nr1.size(); i++) cout << nr1[i] <<    ; n       cout <<   n ; n     } else { n       cout << nr2.size() <<   n ; n       for (int i = 0; i < nr2.size(); i++) cout << nr2[i] <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int seq[60]; n   int yea[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n   for (int i = 1; i <= 12; i++) { n     seq[i - 1] = yea[i - 1]; n     seq[12 + i - 1] = yea[i - 1]; n     seq[24 + i - 1] = yea[i - 1]; n     seq[36 + i - 1] = yea[i - 1]; n     seq[48 + i - 1] = yea[i - 1]; n   } n   seq[25] = 29; n   int n; n   cin >> n; n   int que[n]; n   for (int i = 0; i < n; i++) { n     cin >> que[i]; n   } n   int k = 0; n   for (int i = 0; i < 60; i++) { n     if (seq[i] == que[0]) { n       int j = 1; n       for (j = 1; j < n; j++) { n         if (seq[i + j] != que[j]) break; n       } n       if (j == n) { n         k = 1; n       } n     } n     if (k == 1) break; n   } n   if (k == 1) n     cout <<  Yes ; n   else n     cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[3] = {6, 10, 15}; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   if (n == 2) return cout << -1, 0; n   if (n == 1) return cout << 2, 0; n   cout << a[0] << endl << a[1] << endl << a[2] << endl; n   for (int i = 3; i < n; i++) cout << i * a[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e7 + 5; n const int MAXM = 105; n int st[MAXN], top, tree_cnt; n int ls[MAXN], rs[MAXN], rt[MAXM], val[200005]; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n inline int get() { return top ? st[top--] : ++tree_cnt; } n void modify(int l, int r, int p, int &cur) { n   if (!cur) cur = ++tree_cnt; n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (p <= mid) n     modify(l, mid, p, ls[cur]); n   else n     modify(mid + 1, r, p, rs[cur]); n } n void merge(int &x, int &y) { n   if (!x || !y) return x |= y, y = 0, void(); n   merge(ls[x], ls[y]), merge(rs[x], rs[y]), st[++top] = y, y = 0; n } n void change(int l, int r, int ql, int qr, int &pre, int &cur) { n   if (!pre) return; n   if (ql <= l && qr >= r) return merge(cur, pre); n   if (!cur) cur = get(); n   int mid = (l + r) >> 1; n   if (ql <= mid) change(l, mid, ql, qr, ls[pre], ls[cur]); n   if (qr > mid) change(mid + 1, r, ql, qr, rs[pre], rs[cur]); n   if (!ls[pre] && !rs[pre]) st[++top] = pre, pre = 0; n } n void dfs(int l, int r, int cur, int c) { n   if (!cur) return; n   if (l == r) return val[l] = c, void(); n   int mid = (l + r) >> 1; n   dfs(l, mid, ls[cur], c); n   dfs(mid + 1, r, rs[cur], c); n } n int main() { n   int n = read(); n   for (int i = 1; i <= n; i++) modify(1, n, i, rt[read()]); n   int m = read(); n   for (int i = 1; i <= m; i++) { n     int l = read(), r = read(); n     int x = read(), y = read(); n     if (x == y) continue; n     change(1, n, l, r, rt[x], rt[y]); n   } n   for (int i = 1; i <= 100; i++) dfs(1, n, rt[i], i); n   for (int i = 1; i <= n; i++) printf( %d%c , val[i],    n [i == n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200001, MOD = 1000000007; n int n, l, r; n long long one = 0, two = 0, zero = 0, dp[MAXN][3]; n int main() { n   cin >> n >> l >> r; n   for (; l % 3 != 0 && l <= r; l++) n     if (l % 3 == 1) n       one++; n     else if (l % 3 == 2) n       two++; n   for (; r % 3 != 0 && r >= l; r--) n     if (r % 3 == 1) n       one++; n     else if (r % 3 == 2) n       two++; n   if (l <= r) { n     one += r / 3 - l / 3; n     two += r / 3 - l / 3; n     zero += r / 3 - l / 3 + 1; n   } n   dp[1][0] = zero, dp[1][1] = one, dp[1][2] = two; n   for (int i = 2; i <= n; i++) { n     dp[i][0] = (dp[i - 1][0] * zero % MOD + dp[i - 1][1] * two % MOD + n                 dp[i - 1][2] * one % MOD) % n                MOD; n     dp[i][1] = (dp[i - 1][0] * one % MOD + dp[i - 1][1] * zero % MOD + n                 dp[i - 1][2] * two % MOD) % n                MOD; n     dp[i][2] = (dp[i - 1][0] * two % MOD + dp[i - 1][1] * one % MOD + n                 dp[i - 1][2] * zero % MOD) % n                MOD; n   } n   cout << dp[n][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[500002], d[200004], c, n, fix[200004]; n vector<pair<long long, long long> > g; n vector<long long> v[200005]; n void go(long long x, long long par, long long dep) { n   p[x] = par; n   d[x] = dep; n   for (int i = 0; i < v[x].size(); i++) n     if (v[x][i] != par) go(v[x][i], x, dep + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   go(1, 0, 0); n   for (int i = 1; i <= n; i++) v[i].clear(); n   for (int i = 2; i <= n; i++) v[p[i]].push_back(i); n   for (int i = 1; i <= n; i++) { n     g.push_back(make_pair(d[i], i)); n   } n   sort(g.begin(), g.end()); n   reverse(g.begin(), g.end()); n   for (int id = 0; id < g.size(); id++) { n     long long x = g[id].second, par = p[x]; n     if (fix[x]) continue; n     if (x == 1) { n       cout <<  !   << x << endl; n       return 0; n     } n     if (v[par].size() > 1) { n       long long y = 0; n       if (v[par][0] == x) n         y = v[par][1]; n       else n         y = v[par][0]; n       fix[x] = 1; n       fix[y] = 1; n       cout <<  ?   << x <<     << y << endl; n       cin >> c; n       if (c == x) { n         cout <<  !   << x << endl; n         return 0; n       } n       if (c == y) { n         cout <<  !   << y << endl; n         return 0; n       } n       for (int i = 0; i < v[par].size(); i++) { n         if (v[par][i] == x || v[par][i] == y) { n           swap(v[par][i], v[par][(int)v[par].size() - 1]); n           v[par].pop_back(); n           i--; n           continue; n         } n       } n     } else { n       if (par == 1) { n         cout <<  ?   << x <<     << 1 << endl; n         cin >> c; n         cout <<  !   << c << endl; n         return 0; n       } n       long long y = p[par]; n       cout <<  ?   << x <<     << y << endl; n       cin >> c; n       if (c == par) { n         cout <<  !   << c << endl; n         return 0; n       } n       if (c == x) { n         cout <<  !   << c << endl; n         return 0; n       } n       fix[x] = 1; n       fix[par] = 1; n       for (int i = 0; i < v[y].size(); i++) n         if (v[y][i] == par) { n           swap(v[y][i], v[y][(int)v[y].size() - 1]); n           v[y].pop_back(); n           break; n         } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 300010; n int n, m, i, j, js, root, mx; n int a[N], x[N], y[N], num[N], f[N], next[N * 2], last[N * 2], tov[N * 2], n     bh[N * 2], ans[N * 2]; n bool dir[N * 2], bz[N]; n int gf(int xx) { n   if (f[xx] == xx) return xx; n   int jl = gf(f[xx]); n   f[xx] = jl; n   return jl; n } n void ins(int xx, int yy) { n   js++; n   tov[js] = yy; n   next[js] = last[xx]; n   last[xx] = js; n   bh[js] = i; n } n void suan(int t) { n   int k = last[t]; n   while (k) { n     int p = tov[k]; n     if (!bz[p]) { n       bz[p] = true; n       suan(p); n       if (dir[k]) n         ans[bh[k]] = a[p]; n       else n         ans[bh[k]] = -a[p]; n       a[t] += a[p]; n       a[p] = 0; n     } n     k = next[k]; n   } n } n int main() { n   scanf( %d , &n); n   int l = 0; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     l += a[i]; n   } n   if (l != 0) { n     printf( Impossible ); n     return 0; n   } n   scanf( %d , &m); n   for (i = 1; i <= n; i++) { n     f[i] = i; n   } n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &x[i], &y[i]); n     int x1 = gf(x[i]), y1 = gf(y[i]); n     if (x1 != y1) { n       ins(x[i], y[i]); n       dir[js] = true; n       ins(y[i], x[i]); n       dir[js] = false; n       f[x1] = y1; n       num[x[i]]++; n       num[y[i]]++; n     } n   } n   mx = 0; n   for (i = 1; i <= n; i++) { n     if (mx < num[i]) { n       mx = num[i]; n       root = i; n     } n   } n   int bj = 0; n   memset(bz, false, sizeof(bz)); n   bz[root] = true; n   suan(root); n   printf( Possible n ); n   for (i = 1; i <= m; i++) { n     printf( %d n , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n void chkmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n void chkmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n inline void debug(int x) { fprintf(stderr,  ycx has aked ioi %d times n , x); } n namespace fastio { n char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf; n inline char getc() { n   return p1 == p2 && n                  (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2) n              ? -1 n              : *p1++; n } n inline void putc(char x) { (*p3++ = x); } n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   T neg = 0; n   while (!isdigit(c)) neg |= !(c ^  - ), c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   if (neg) x = (~x) + 1; n } n template <typename T> n void recursive_print(T x) { n   if (!x) return; n   recursive_print(x / 10); n   putc(x % 10 ^ 48); n } n template <typename T> n void print(T x) { n   if (!x) putc( 0 ); n   if (x < 0) putc( - ), x = ~x + 1; n   recursive_print(x); n } n template <typename T> n void print(T x, char c) { n   if (!x) putc( 0 ); n   if (x < 0) putc( - ), x = ~x + 1; n   recursive_print(x); n   putc(c); n } n void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); } n }  // namespace fastio n const int MAXV = 30; n const int MAXE = 300 * 2; n const int INF = 0x3f3f3f3f; n int n, m, p[8], q[8], cnt[8]; n int S = 29, T = 30, hd[MAXV + 5], to[MAXE + 5], nxt[MAXE + 5], cap[MAXE + 5], n     ec = 1; n void init() { n   memset(hd, 0, sizeof(hd)); n   ec = 1; n } n void adde(int u, int v, int f) { n   to[++ec] = v; n   cap[ec] = f; n   nxt[ec] = hd[u]; n   hd[u] = ec; n   to[++ec] = u; n   cap[ec] = 0; n   nxt[ec] = hd[v]; n   hd[v] = ec; n } n int dep[MAXV + 5], now[MAXV + 5]; n bool getdep() { n   memset(dep, -1, sizeof(dep)); n   dep[S] = 0; n   queue<int> q; n   q.push(S); n   now[S] = hd[S]; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int e = hd[x]; e; e = nxt[e]) { n       int y = to[e], z = cap[e]; n       if (!~dep[y] && z) { n         dep[y] = dep[x] + 1; n         q.push(y); n         now[y] = hd[y]; n       } n     } n   } n   return ~dep[T]; n } n int getflow(int x, int f) { n   if (x == T) return f; n   int ret = 0; n   for (int &e = now[x]; e; e = nxt[e]) { n     int y = to[e], z = cap[e]; n     if (dep[y] == dep[x] + 1 && z) { n       int w = getflow(y, min(f - ret, z)); n       cap[e] -= w; n       cap[e ^ 1] += w; n       ret += w; n       if (ret == f) return ret; n     } n   } n   return ret; n } n int dinic() { n   int ret = 0; n   while (getdep()) ret += getflow(S, INF); n   return ret; n } n int num[8][8], cc[8][8], pru[8], deg[8], vis[8]; n void solve() { n   memset(deg, 0, sizeof(deg)); n   memset(vis, 0, sizeof(vis)); n   memset(cc, 0, sizeof(cc)); n   for (int i = 1; i <= m - 2; i++) deg[pru[i]]++; n   for (int i = 1; i <= m - 2; i++) { n     int pos = 0; n     for (int j = 1; j <= m; j++) { n       if (!deg[j] && !vis[j]) { n         pos = j; n         break; n       } n     } n     cc[min(pos, pru[i])][max(pos, pru[i])]++; n     deg[pru[i]]--; n     vis[pos] = 1; n   } n   int u1 = 1, u2 = m; n   while (vis[u1]) u1++; n   while (vis[u2]) u2--; n   cc[u1][u2]++; n   int cur = 0; n   init(); n   for (int i = 1; i <= m; i++) n     for (int j = i; j <= m; j++) { n       if (cc[i][j] > num[i][j]) return; n       cur++; n       adde(S, cur, num[i][j] - cc[i][j]); n       adde(cur, m * (m + 1) / 2 + i, INF); n       adde(cur, m * (m + 1) / 2 + j, INF); n     } n   for (int i = 1; i <= m; i++) adde(m * (m + 1) / 2 + i, T, cnt[i] - 1); n   if (dinic() == n - m) { n     for (int i = 1; i <= m; i++) n       for (int j = i; j <= m; j++) { n         if (cc[i][j]) printf( %d %d n , p[i], p[j]); n       } n     for (int i = 1; i <= m; i++) q[i] = p[i]; n     cur = 0; n     for (int i = 1; i <= m; i++) n       for (int j = i; j <= m; j++) { n         cur++; n         for (int e = hd[cur]; e; e = nxt[e]) { n           int y = to[e], z = cap[e ^ 1]; n           if (y == S) continue; n           if (y - m * (m + 1) / 2 == i) { n             while (z--) printf( %d %d n , ++q[i], p[j]); n           } else { n             while (z--) printf( %d %d n , ++q[j], p[i]); n           } n         } n       } n     exit(0); n   } n } n void dfs(int x) { n   if (x == m - 1) return solve(); n   for (int i = 1; i <= m; i++) pru[x] = i, dfs(x + 1); n } n int main() { n   scanf( %d , &n); n   for (int cur = 1; cur <= n; cur *= 10) p[++m] = cur; n   for (int i = 1; i < m; i++) cnt[i] = p[i + 1] - p[i]; n   cnt[m] = n - p[m] + 1; n   for (int i = 1; i < n; i++) { n     static char s1[9], s2[9]; n     scanf( %s%s , s1 + 1, s2 + 1); n     int l1 = strlen(s1 + 1), l2 = strlen(s2 + 1); n     if (l1 > l2) l1 ^= l2 ^= l1 ^= l2; n     num[l1][l2]++; n   } n   if (m == 1) { n     if (num[1][1] != n - 1) return puts( -1 ), 0; n     for (int i = 1; i < n; i++) printf( %d %d n , i, i + 1); n     return 0; n   } n   dfs(1); n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[7]; n int N, K; n map<vector<int>, double> M[5]; n int bit[20]; n void update(int x) { n   while (x <= N) { n     bit[x] += 1; n     x += (x & -x); n   } n } n int qry(int x) { n   int rs = 0; n   while (x) { n     rs += bit[x]; n     x -= (x & -x); n   } n   return rs; n } n int way; n double tot; n double solve(vector<int> v, int pos) { n   if (pos == K) { n     way++; n     memset(bit, 0, sizeof bit); n     double rs = 0; n     for (int i = 0; i < N; i++) { n       rs += qry(N) - qry(v[i]); n       update(v[i] + 1); n     } n     tot += rs; n     return 0; n   } n   int i, j, x, y; n   double res = 0; n   for (i = 0; i < N; i++) { n     for (j = i; j < N; j++) { n       vector<int> tmp = v; n       x = i; n       y = j; n       while (y > x) { n         swap(tmp[x], tmp[y]); n         x++, y--; n       } n       solve(tmp, pos + 1); n     } n   } n   return res; n } n int main() { n   int i, j, k; n   cin >> N >> K; n   vector<int> v; n   for (int i = 0; i < N; i++) { n     cin >> k; n     k--; n     v.push_back(k); n   } n   { n     solve(v, 0); n     double res = tot; n     tot /= (double)way; n     cout << setprecision(13) << fixed; n     cout << tot + 1e-15 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   static int x; n   static char ch; n   static bool pd; n   pd = true; n   x = 0; n   for (ch = getchar(); ch >  9  || ch <  0 ; ch = getchar()) n     if (ch ==  - ) pd = false; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return pd ? x : -x; n } n vector<pair<int, int> > ans; n int n, a[100010], L, R, st, ed, cnt; n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) a[i] = read(); n   L = 1, R = n, st = 1, ed = a[n] + 1; n   while (L <= R) { n     for (int i = 1; i <= a[L] - a[L - 1]; ++i) { n       for (int j = st + 1; j <= ed; ++j) ans.push_back(make_pair(st, j)), cnt++; n       st++; n     } n     L++; n     ed = a[--R] + 1; n   } n   cout << cnt << endl; n   for (int i = 0; i < cnt; ++i) n     cout << ans[i].first <<     << ans[i].second << endl; n   return 0; n } n