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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = (int)2e5 + 100;   const int mod = (int)1e9 + 7;   int cnt[MAX_N];   inline int mul(int x, int y) { return (long long)x * y % mod; }   inline int powMod(int x, int n) {     int ret = 1;     while (n > 0) {       if (n & 1) ret = mul(ret, x);       x = mul(x, x);       n >>= 1;     }     return ret;   }   int main() {     int m;     scanf( %d , &m);     memset(cnt, 0, sizeof(cnt));     for (int i = 0; i < m; ++i) {       int p;       scanf( %d , &p);       ++cnt[p];     }     vector<int> toMul;     for (int i = 0; i < MAX_N; ++i)       if (cnt[i] > 0) {         toMul.push_back(cnt[i] + 1);       }     bool perfectSq = true;     for (int i = 0; i < toMul.size(); ++i) {       if (toMul[i] % 2 == 0) {         toMul[i] /= 2;         perfectSq = false;         break;       }     }     int p1 = 1;     for (int i = 0; i < toMul.size(); ++i) {       p1 = (long long)p1 * toMul[i] % (mod - 1);     }     if (!perfectSq) {       int num = 1;       for (int i = 0; i < MAX_N; ++i)         if (cnt[i] > 0) {           num = mul(num, powMod(i, cnt[i]));         }       printf( %d n , powMod(num, p1));     } else {       int root = 1;       for (int i = 0; i < MAX_N; ++i)         if (cnt[i] > 0) {           root = mul(root, powMod(i, cnt[i] / 2));         }       printf( %d n , powMod(root, p1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     bool vi[1002];     memset(vi, 0, sizeof vi);     int a = 0, i = 0;     while (i < n) {       a = (a + i % n) % n;       if (vi[a]) {         cout <<  NO  << endl;         return 0;       };       vi[a] = 1;       i++;     }     cout <<  YES  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int diez(int x) {     int x1 = 0;     int s = 0;     while (x != 0) {       x1 = x % 10;       x = x / 10;       s += x1;     }     if (s == 10)       return (1);     else       return (0);   }   int main() {     int n;     while (cin >> n) {       int s = 18;       int s1 = 0;       while (n) {         s++;         if (diez(s) == 1) {           s1 = s;           n--;         }       }       cout << s1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long op[1000010];   int main() {     op[1] = 1LL;     for (int i = 2; i <= 1000000; i++) {       op[i] = op[i - 1] * 2 + 1;       op[i] %= 1000000007;     }     string s;     cin >> s;     long long cnt = 0;     int a = 0;     int b = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  a )         a++;       else {         cnt += op[a];         cnt %= 1000000007;       }     }     cout << cnt << endl;   }