#include <bits/stdc++.h>   using namespace std;   struct comp {     double re, im;     comp(double x = 0, double y = 0) : re(x), im(y) {}     comp operator+(const comp& a) { return comp(re + a.re, im + a.im); }     comp operator-(const comp& a) { return comp(re - a.re, im - a.im); }     comp operator*(const comp& a) {       return comp(re * a.re - im * a.im, re * a.im + im * a.re);     }   };   const double pi = acos(-1);   void fft(vector<comp>& x) {     int n = x.size();     for (int i = 1, k = 0; i < n; ++i) {       for (int bit = n >> 1; (k ^= bit) < bit; bit >>= 1) {       }       if (i < k) {         swap(x[i], x[k]);       }     }     for (int k = 1; k < n; k <<= 1) {       comp wlen(cos(pi / k), sin(pi / k));       for (int i = 0; i < n; i += (k << 1)) {         comp w(1);         for (int j = 0; j < k; ++j) {           comp tmp = w * x[i + j + k];           x[i + j + k] = x[i + j] - tmp;           x[i + j] = x[i + j] + tmp;           w = w * wlen;         }       }     }   }   template <class T>   vector<T> mul(vector<T>& a, vector<T>& b) {     int n = max(a.size(), b.size());     int lg = 31 - __builtin_clz(n);     n = 1 << (lg + 1 + (n != (1 << lg)));     vector<comp> fa(n), fb(n);     for (int i = 0; i < a.size(); ++i) {       fa[i] = a[i];     }     for (int i = 0; i < b.size(); ++i) {       fb[i] = b[i];     }     fft(fa);     if (a == b) {       fb = fa;     } else {       fft(fb);     }     for (int i = 0; i < n; ++i) {       fa[i] = fa[i] * fb[i];     }     fft(fa);     reverse(fa.begin() + 1, fa.end());     vector<T> c(n);     for (int i = 0; i < n; ++i) {       c[i] = (T)(fa[i].re / n + (fa[i].re > 0 ? 0.5 : -0.5));     }     return c;   }   const int N = 5e5 + 5;   bool ok[N];   int ans[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     int n, x, y;     cin >> n >> x >> y;     ++n;     vector<int> a(x + 1);     for (int i = 0; i < n; ++i) {       int z;       cin >> z;       a[z] = 1;     }     auto b = a;     reverse(b.begin(), b.end());     auto c = mul(a, b);     for (int i = 1; i <= x; ++i)       if (c[i + x]) ok[i + y] = true;     for (int i = 1; i < N; ++i) {       if (!ans[i]) ans[i] = -1;       if (!ok[i]) continue;       for (int j = i; j < N; j += i) ans[j] = 2 * i;     }     int q;     cin >> q;     while (q--) {       int z;       cin >> z;       cout << ans[z / 2] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   const long double eps = 1e-7;   const int inf = 1000000010;   const long long INF = 10000000000010LL;   const int mod = 1000000007;   const int MAXN = 100010;   int n, m, u, v, x, y, t, a, b, ans, node, cnt;   bool ok[MAXN];   bool mark[MAXN];   vector<int> G[MAXN];   vector<int> out;   void dfs(int node, int par) {     mark[node] = true;     cnt -= ok[node];     ok[node] ^= 1;     for (int v : G[node]) {       if (mark[v]) continue;       out.push_back(v);       dfs(v, node);       out.push_back(node);       ok[node] ^= 1;     }     if (ok[node] && par) {       out.push_back(par);       out.push_back(node);       ok[par] ^= 1;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     while (m--) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       cin >> ok[i];       if (ok[i]) cnt++;       if (!node && ok[i]) node = i;     }     if (!node) return cout << 0 <<   n , 0;     ;     dfs(node, 0);     if (cnt) return cout << -1 <<   n , 0;     ;     if (!ok[node])       cout << out.size() + 1 << endl << node <<    ;     else       cout << out.size() << endl;     for (int i : out) cout << i <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     string a[1000], b[1000];     for (int i = 0; i < n; i++) {       cin >> a[i] >> b[i];     }     for (int i = 0; i < m; i++) {       string cmd, ip, tip;       cin >> cmd >> ip;       for (int i = 0; i < ip.size() - 1; i++) tip += ip[i];       for (int i = 0; i < n; i++) {         if (tip.compare(b[i]) == 0)           cout << cmd <<     << ip <<   #  << a[i] <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, k, ans = 1 << 30;   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= k - 1; i++)       if (n % i == 0) ans = min(ans, k * n / i + i);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, i, j;   const double eps = 1e-9;   struct nd {     double x, y;   };   nd operator-(nd x, nd y) { return (nd){x.x - y.x, x.y - y.y}; }   bool cmp(nd x, nd y) {     if (x.x == y.x) return x.y < y.y;     return x.x < y.x;   }   double dis(nd x, nd y) { return abs(x.x - y.x) + abs(x.y - y.y); }   double dis2(nd x, nd y) {     return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));   }   double crs(nd x, nd y) { return x.x * y.y - x.y * y.x; }   vector<nd> all, cov;   nd stk[2000005];   int tp;   void update1(nd x) {     if (0 <= x.x && x.x <= 1e5 && 0 <= x.y && x.y <= 1e5) all.push_back(x);   }   void update2(nd p, double r, nd x) {     if (dis(p, x) <= r) all.push_back(x);   }   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) {       double x, y, r;       scanf( %lf%lf%lf , &x, &y, &r);       if (x + r <= 1e5)         update1((nd){x + r, y});       else {         update1((nd){1e5, y - (r - (1e5 - x))});         update1((nd){1e5, y + (r - (1e5 - x))});       }       if (x - r >= 0)         update1((nd){x - r, y});       else {         update1((nd){0, y - (r - x)});         update1((nd){0, y + (r - x)});       }       if (y + r <= 1e5)         update1((nd){x, y + r});       else {         update1((nd){x - (r - (1e5 - y)), 1e5});         update1((nd){x + (r - (1e5 - y)), 1e5});       }       if (y - r >= 0)         update1((nd){x, y - r});       else {         update1((nd){x - (r - y), 0});         update1((nd){x + (r - y), 0});       }       update2((nd){x, y}, r, (nd){0, 0});       update2((nd){x, y}, r, (nd){0, 1e5});       update2((nd){x, y}, r, (nd){1e5, 0});       update2((nd){x, y}, r, (nd){1e5, 1e5});     }     sort(all.begin(), all.end(), cmp);     stk[tp = 1] = all[0];     for (i = 1; i < all.size(); i++) {       if (all[i].x == all[i - 1].x && all[i].y == all[i - 1].y) continue;       while (tp > 1 && crs(stk[tp] - stk[tp - 1], all[i] - stk[tp]) > -eps) tp--;       stk[++tp] = all[i];     }     for (i = 1; i < tp; i++) cov.push_back(stk[i]);     reverse(all.begin(), all.end());     stk[tp = 1] = all[0];     for (i = 1; i < all.size(); i++) {       if (all[i].x == all[i - 1].x && all[i].y == all[i - 1].y) continue;       while (tp > 1 && crs(stk[tp] - stk[tp - 1], all[i] - stk[tp]) > -eps) tp--;       stk[++tp] = all[i];     }     for (i = 1; i < tp; i++) cov.push_back(stk[i]);     double ans = -1e18;     int res = 0;     for (i = 0; i < cov.size(); i++) {       int j = (i + 1) % cov.size();       int k = (i + 2) % cov.size();       double tmp = dis2(cov[i], cov[j]) * dis2(cov[i], cov[k]) *                    dis2(cov[j], cov[k]) / 4 /                    abs(crs(cov[j] - cov[i], cov[k] - cov[i]) / 2);       if (tmp > ans) {         ans = tmp;         res = i;       }     }     printf( %.0f %.0f n , cov[res].x, cov[res].y);     printf( %.0f %.0f n , cov[(res + 1) % cov.size()].x,            cov[(res + 1) % cov.size()].y);     printf( %.0f %.0f n , cov[(res + 2) % cov.size()].x,            cov[(res + 2) % cov.size()].y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > ps, is, psum(100001), isum(100001);   long long m, n, d;   class cmp {    public:     bool operator()(pair<long long, long long> &a,                     pair<long long, long long> &b) {       return a.first > b.first || (a.first == b.first && a.second < b.second);     }   };   long long bsearch(long long w, long long lo, long long hi) {     long long mid = lo + (hi - lo) / 2;     if (lo > hi) return -1;     if (lo == hi) {       return (isum[lo].second <= w) ? lo : -1;     }     if (isum[mid].second > w) return bsearch(w, lo, mid);     long long ret = bsearch(w, mid + 1, hi);     return ret == -1 ? mid : ret;   }   int main() {     long long i, j, t1, t2;     cin >> n >> m >> d;     for (i = 0; i < n; i++) {       cin >> t1 >> t2;       ps.push_back(pair<long long, long long>(t1, t2));     }     for (i = 0; i < m; i++) {       cin >> t1 >> t2;       is.push_back(pair<long long, long long>(t1, t2));     }     sort(ps.begin(), ps.end(), cmp());     sort(is.begin(), is.end(), cmp());     psum[0] = pair<long long, long long>(0, 0),     isum[0] = pair<long long, long long>(0, 0);     for (i = 1; i <= n; i++) {       j = i - 1;       psum[i] = pair<long long, long long>(ps[j].first + psum[i - 1].first,                                            ps[j].second + psum[i - 1].second);     }     for (i = 1; i <= m; i++) {       j = i - 1;       isum[i] = pair<long long, long long>(is[j].first + isum[i - 1].first,                                            is[j].second + isum[i - 1].second);     }     long long ans = 0;     for (i = 1; i <= n; i++) {       pair<long long, long long> tp = psum[i];       long long ind = bsearch(d - tp.second, 1, m);       if (ind == -1) break;       ans = max(ans, tp.first + isum[ind].first);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long arr[n], ans[n];     for (long long i = 0; i < n; i++) arr[i] = 1;     ans[n - 1] = 1;     for (long long i = 2; i <= n; i++) {       arr[n - 1] = i;       cout <<  ?  ;       for (long long i = 0; i < n; i++) cout << arr[i] <<    ;       cout <<   n ;       cout << flush;       long long k;       cin >> k;       if (!k) {         ans[n - 1] = n - i + 2;         break;       }     }     long long i = 1;     for (long long i = 0; i < n; i++) arr[i] = ans[n - 1];     while (i <= n) {       if (i == ans[n - 1]) {         i++;         continue;       }       arr[n - 1] = i;       cout <<  ?  ;       for (long long i = 0; i < n; i++) cout << arr[i] <<    ;       cout <<   n ;       cout << flush;       long long k;       cin >> k;       ans[k - 1] = i;       i++;     }     cout <<  !  ;     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   const long double pi = acos(-1.0);   const int N = 2e5 + 100;   int tr[4 * N];   int inicial[N];   int v[4 * N];   int n;   void build(int node, int b, int e) {     if (b == e)       tr[node] = b;     else {       int mid = (b + e) / 2;       build(2 * node, b, mid);       build(2 * node + 1, mid + 1, e);       if (v[tr[2 * node]] > v[tr[2 * node + 1]]) {         tr[node] = tr[2 * node + 1];       } else {         tr[node] = tr[2 * node];       }     }   }   void update(int node, int b, int e, int i, int val) {     if (i < b or i > e) return;     if (b == e) {       if (v[b] > val) v[b] = val + 1;       return;     }     int mid = (b + e) / 2;     update(2 * node, b, mid, i, val);     update(2 * node + 1, mid + 1, e, i, val);     if (v[tr[2 * node]] > v[tr[2 * node + 1]]) {       tr[node] = tr[2 * node + 1];     } else {       tr[node] = tr[2 * node];     }   }   int query(int node, int b, int e, int i, int j) {     if (i > e or j < b) return -1;     if (i <= b and j >= e) return tr[node];     int mid = (b + e) / 2;     int p1 = query(2 * node, b, mid, i, j);     int p2 = query(2 * node + 1, mid + 1, e, i, j);     if (p1 == -1) return p2;     if (p2 == -1) return p1;     if (v[p1] < v[p2]) return p1;     return p2;   }   void conserta(int x) {     if (x - 1 >= 1) {       update(1, 1, n, x - 1, v[x]);     }     if (x + 1 <= n) {       update(1, 1, n, x + 1, v[x]);     }   }   void solve(int b, int e) {     if (b > e) return;     int x = query(1, 1, n, b, e);     conserta(x);     solve(b, x - 1);     solve(x + 1, e);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d%d , &x, &y);       inicial[i] = x;       v[i] = x + y;     }     build(1, 1, n);     solve(1, n);     long long resp = 0;     bool pau = false;     for (int i = 1; i <= n; i++) {       resp += v[i] - inicial[i];       if (inicial[i] > v[i]) pau = true;     }     if (pau) {       printf( %d n , -1);     } else {       cout << resp << endl;       for (int i = 1; i < n; i++) printf( %d  , v[i]);       printf( %d n , v[n]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q, w[15];   char s[15];   int cnt[5005];   int sum[5000][105];   int tot2[15];   int main() {     scanf( %d%d%d , &n, &m, &q);     tot2[0] = 1;     for (int i = 1; i <= 14; i++) tot2[i] = tot2[i - 1] * 2;     for (int i = 1; i <= n; i++) scanf( %d , &w[i]);     for (int i = 1; i <= m; i++) {       scanf( %s , s);       int len = strlen(s);       int x = 0;       for (int i = 0; i < len; i++) x = x * 2 + s[i] -  0 ;       cnt[x]++;     }     for (int i = 0; i < tot2[n]; i++) {       for (int j = 0; j < tot2[n]; j++) {         int x = i ^ j;         int tot = 0;         for (int k = 0; k < n; k++) tot += (!((x >> k) & 1)) * w[n - k];         if (tot <= 100) sum[i][tot] += cnt[j];       }     }     for (int i = 0; i < tot2[n]; i++) {       for (int j = 1; j <= 100; j++) {         sum[i][j] += sum[i][j - 1];       }     }     while (q--) {       int x = 0;       char Q[15];       int val;       scanf( %s%d , Q, &val);       for (int i = 0; i < n; i++) x = x * 2 + Q[i] -  0 ;       printf( %d n , sum[x][val]);     }   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 1000000;   vector<int> graf[1 + NMAX];   int minim[1 + NMAX];   int minimLant = INT_MAX;   bool vizitat[1 + NMAX];   void dfs(int z) {     for (int i = 0; i < graf[z].size(); i++) {       if (!vizitat[graf[z][i]]) {         vizitat[graf[z][i]] = true;         minim[graf[z][i]] = min(minim[z], graf[z][i]);         dfs(graf[z][i]);       }     }   }   int main() {     int n, q;     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> q;     for (int i = 1; i <= n - 1; i++) {       int x, y;       cin >> x >> y;       graf[x].emplace_back(y);       graf[y].emplace_back(x);     }     for (int i = 1; i <= n; i++) {       minim[i] = INT_MAX;     }     int t, z;     cin >> t >> z;     z = z % n + 1;     minim[z] = z;     vizitat[z] = true;     dfs(z);     int last = 0;     for (int i = 2; i <= q; i++) {       cin >> t >> z;       z = (z + last) % n + 1;       if (t == 1) {         minimLant = min(minimLant, minim[z]);       } else {         last = min(minimLant, minim[z]);         cout << last <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, p;   long long int a[2105][2105];   long long int sz[2105 * 2105];   long long int fa[2105 * 2105];   pair<long long int, pair<long long int, long long int> > arr[2105 * 2105];   const int mv[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   long long int find(long long int x) {     return fa[x] == x ? x : fa[x] = find(fa[x]);   }   void merge(long long int a, long long int b) {     a = find(a);     b = find(b);     if (a != b) {       sz[a] += sz[b];       sz[b] = 0;       fa[b] = a;     }   }   long long int out[2105][2105];   long long int height, freq;   void dfs(long long int x, long long int y) {     if (x < 0 || x >= n || y < 0 || y >= m) return;     if (out[x][y]) return;     if (a[x][y] < height) return;     if (!freq) return;     out[x][y] = height;     freq--;     dfs(x + 1, y);     dfs(x - 1, y);     dfs(x, y + 1);     dfs(x, y - 1);     return;   }   int main() {     ios_base::sync_with_stdio(0);     long long int i, j, k;     cin >> n >> m >> p;     for (i = 0; i < (n); ++i)       for (j = 0; j < (m); ++j) {         cin >> a[i][j];         arr[i * m + j] = pair<long long int, pair<long long int, long long int> >(             a[i][j], pair<long long int, long long int>(i, j));       }     for (i = 0; i < (n * m); ++i) {       fa[i] = i;       sz[i] = 1;     }     sort(arr, arr + n * m);     reverse(arr, arr + n * m);     long long int ans = -1;     i = 0;     while (i < n * m) {       j = i;       while (j < n * m && arr[j].first == arr[i].first) {         long long int x0, x, y0, y;         x0 = arr[j].second.first;         y0 = arr[j].second.second;         for (k = 0; k < (4); ++k) {           x = x0 + mv[k][0];           y = y0 + mv[k][1];           if (x >= 0 && x < n && y >= 0 && y < m)             if (a[x][y] >= arr[j].first) merge(x * m + y, x0 * m + y0);         }         j++;       }       if (arr[i].first && (p % arr[i].first) == 0)         for (k = i; k < j; ++k) {           long long int x, y;           x = arr[k].second.first;           y = arr[k].second.second;           if (sz[find(x * m + y)] >= p / arr[i].first) {             ans = x * m + y;             break;           }         }       if (ans != -1) break;       i = j;     }     if (ans == -1) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     height = a[ans / m][ans % m];     freq = p / height;     dfs(ans / m, ans % m);     for (i = 0; i < (n); ++i) {       for (j = 0; j < (m); ++j) cout << out[i][j] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> ans;   vector<long long int> allD;   unordered_map<long long int, int> comp;   const int MM = 1e4;   const int LIM = 1e5;   vector<long long int> gr[MM];   void solve(long long int N, long long int K) {     if (K == 0 || N == 0) {       ans.push_back(N);       return;     }     for (long long int u : gr[N]) {       solve(u, K - 1);       if (ans.size() == LIM) {         return;       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int N, K;     cin >> N >> K;     for (long long int i = 1; i * i <= N; i++) {       if (N % i == 0) {         allD.push_back(i);         if (i * i != N) {           allD.push_back(N / i);         }       }     }     sort(allD.begin(), allD.end());     for (int i = 0; i < allD.size(); i++) {       for (int j = 0; j <= i; j++) {         if (allD[i] % allD[j] == 0) {           gr[i].push_back(j);         }       }     }     K = min(K, (long long int)LIM);     solve(allD.size() - 1, K);     for (auto u : ans) {       cout << allD[u] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;      typedef long long ll;   typedef pair<int, int> pii;   typedef pair<ll, ll> pll;      const int MAXN = 2e5;   const int MAXM = 5e6;      int N;   pii A[MAXN+10];      pii B[MAXM+10], C[MAXM+10];      int main()   {    scanf( %d , &N);    for(int i=1; i<=N; i++) scanf( %d , &A[i].first), A[i].second=i;    sort(A+1, A+N+1);       N=min(N, 10000);    for(int i=1; i<=N; i++)    {     for(int j=i-1; j>=1; j--)     {      int t=A[i].first-A[j].first;      if(B[t]==pii(0, 0))      {       B[t]={j, i};      }     }    }    for(int i=N; i>=1; i--)    {     for(int j=i+1; j<=N; j++)     {      int t=A[j].first-A[i].first;      if(C[t]==pii(0, 0))      {       C[t]={i, j};      }     }    }       for(int i=0; i<=MAXM; i++)    {     if(B[i]==pii(0, 0)) continue;     if(C[i]==pii(0, 0)) continue;     if(B[i].second<C[i].first)     {      printf( YES n );      printf( %d %d %d %d n , A[B[i].first].second, A[C[i].second].second, A[B[i].second].second, A[C[i].first].second);      return 0;     }    }    printf( NO n );   }


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


#include <bits/stdc++.h>   using namespace std;   template <class T>   T min(T a, T b, T c) {     return min(a, min(b, c));   }   template <class T>   T min(T a, T b, T c, T d) {     return min(a, min(b, min(c, d)));   }   template <class T>   T max(T a, T b, T c) {     return max(a, max(b, c));   }   template <class T>   T max(T a, T b, T c, T d) {     return max(a, max(b, max(c, d)));   }   bool cmp(int a, int b) { return a > b; }   long long GCD(long long a, long long b) { return (a % b) ? GCD(b, a % b) : b; }   const string namePro =  tmp ;   int main() {     int n;     scanf( %d , &n);     int cnt = 0;     int ans = 0;     stack<int> stk;     for (int i = (1); i <= (2 * n); ++i) {       string s;       cin >> s;       if (s ==  add ) {         int x;         scanf( %d , &x);         stk.push(x);       } else {         ++cnt;         if (stk.size() && stk.top() == cnt)           stk.pop();         else if (stk.empty())           continue;         else {           while (stk.size()) stk.pop();           ++ans;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 41;   const int MASK = 1 << 21;   long long sz[MAX_N], par[MAX_N], po2[MAX_N], vis[MAX_N];   long long dp[MASK];   vector<int> adj[MAX_N];   int rt(int v) { return v ^ par[v] ? par[v] = rt(par[v]) : v; }   void join(int u, int v) {     if ((u = rt(u)) != (v = rt(v))) {       par[v] = u, sz[u] += sz[v];     }   }   int main() {     int n, m;     cin >> n >> m;     if (m < 3) {       cout << 0 << endl;       return 0;     }     po2[0] = 1;     for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1;     for (int i = 1; i < n + 1; i++) po2[i] = po2[i - 1] * 2ll;     long long ans = po2[n];     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--, b--;       join(a, b);       adj[a].push_back(b);       adj[b].push_back(a);     }     set<long long> s;     long long num = 0;     for (int i = 0; i < n; i++) {       s.insert(rt(i));       if (sz[rt(i)] == 1) num++;     }     ans += po2[num + 1] - po2[s.size()];     bool imposs = false;     for (int i = 0; i < n; i++) {       if (vis[i]) continue;       queue<pair<int, int> > q;       q.push({i, 1});       while (q.size()) {         pair<int, int> curr = q.front();         q.pop();         if (vis[curr.first]) {           if (vis[curr.first] != curr.second) {             imposs = true;             break;           } else             continue;         }         vis[curr.first] = curr.second;         for (int next : adj[curr.first]) q.push({next, curr.second ^ 3});       }       if (imposs) break;     }     if (!imposs) ans += po2[s.size()];     num = 0;     for (int mask = 0; mask < 1 << ((n + 1) / 2); mask++) {       bool nowork = false;       long long new_mask = (1 << (n / 2)) - 1;       for (int k = 0; k < (n + 1) / 2; k++) {         if (!(mask & (1 << k))) continue;         for (int nxt : adj[2 * k]) {           if (nxt & 1)             new_mask &= (1 << (n / 2)) - (1 << (nxt / 2)) - 1;           else             nowork = nowork || (mask & (1 << (nxt / 2)));         }       }       if (!nowork) dp[new_mask]++;     }     for (int i = 0; i < n / 2; i++)       for (int mask = 0; mask < 1 << (n / 2); mask++)         if (mask != (mask | (1 << i))) dp[mask] += dp[mask | (1 << i)];     for (int mask = 0; mask < 1 << (n / 2); mask++) {       bool nowork = false;       for (int k = 0; k < n / 2; k++) {         if (!(mask & (1 << k))) continue;         for (long long nxt : adj[2 * k + 1]) {           if (nxt & 1 && mask & (1 << (nxt / 2))) nowork = true;         }       }       if (!nowork) num += dp[mask];     }     ans -= 2ll * num;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   set<pair<long long int, long long int> > s;   pair<long long int, long long int> p;   long long int d[300000], x, n, k, l = 0, di = 0;   vector<long long int> v[300000];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> x;       d[i] = x;       v[x].push_back(i);       di = max(x, di);     }     if (v[0].size() > 1 || v[1].size() > k) {       cout << -1;       return 0;     }     for (int i = 0; i <= di; i++)       if (v[i].size() == 0) {         cout << -1;         return 0;       }     for (int i = 0; i < v[1].size(); i++) s.insert({v[0][0], v[1][i]});     for (int i = 2; i <= di; i++) {       if (v[i - 1].size() * (k - 1) < v[i].size()) {         cout << -1;         return 0;       }       x = 1;       l = 0;       for (int j = 0; j < v[i].size(); j++) {         if (x > k - 1) {           l++;           x = 1;         }         s.insert({v[i - 1][l], v[i][j]});         x++;       }     }     cout << s.size() << endl;     while (!s.empty()) {       p = *s.begin();       cout << p.first <<     << p.second <<   n ;       s.erase(s.begin());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, s, tot;   int a[5005], b[5005], c[5005], last[5005];   vector<int> edge[5005];   int cmp(int i, int j) { return c[i] > c[j]; }   long long dp[5005][5005];   int main() {     scanf( %d%d%d , &n, &m, &s);     int temp = s;     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &a[i], &b[i], &c[i]);       if (temp < a[i]) {         return printf( -1 n ), 0;       }       temp += b[i];       last[i] = i;     }     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       last[y] = max(last[y], x);     }     for (int i = 1; i <= n; i++) edge[last[i]].push_back(i);     for (int i = 1; i <= n; i++) {       sort(edge[i].begin(), edge[i].end(), cmp);     }     memset(dp, -1, sizeof(dp));     dp[0][s] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= 5000; j++)         if (j - b[i] >= a[i]) {           dp[i][j] = dp[i - 1][j - b[i]];         }       int len = edge[i].size();       for (int j = 1; j <= 5000; j++)         if (dp[i][j] != -1) {           long long res = 0;           for (int k = 0; k < len; k++)             if (j - k - 1 >= 0) {               res += c[edge[i][k]];               dp[i][j - k - 1] = max(dp[i][j] + res, dp[i][j - k - 1]);             }         }     }     long long ans = 0;     for (int i = 0; i <= 5000; i++) {       ans = max(ans, dp[n][i]);     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const double eps = 1e-9;   double mi[N], ma[N];   double pre1[N], pre2[N];   double ans1[N], ans2[N];   int main() {     int n;     scanf( %d , &n);     for (int i = (1); i <= (n); i++) cin >> ma[i];     for (int i = (1); i <= (n); i++) cin >> mi[i];     for (int i = (1); i <= (n); i++) pre1[i] = pre1[i - 1] + ma[i];     for (int i = n; i >= 1; i--) pre2[i] = pre2[i + 1] + mi[i];     for (int i = (1); i <= (n); i++) {       double t1 = pre2[i + 1] - pre1[i] - 1.0;       double t3 = t1 * t1 - 4.0 * pre1[i];       if (t3 < eps) t3 = 0.0;       t3 = sqrt(t3);       ans1[i] = 0.5 * (-t1 + t3);       ans2[i] = 0.5 * (-t1 - t3);     }     for (int i = (1); i <= (n); i++) printf( %.9lf  , ans1[i] - ans1[i - 1]);     puts(  );     for (int i = (1); i <= (n); i++) printf( %.9lf  , ans2[i] - ans2[i - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     cin >> n >> x >> y;     n = n / 2;     if ((n == x || n == x - 1) && (n == y || n == y - 1))       cout <<  NO  << endl;     else       cout <<  YES  << endl;   }


#include <bits/stdc++.h>   int n;   int main() {     scanf( %d , &n);     printf( %d n , n % 5 % 3 + 1);   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   void solve() {     long long n;     cin >> n;     if (n < 6)       cout <<  -1  << endl;     else {       cout <<  1 2 n2 3 n2 4 n1 5 n1 6 n ;       for (long long i = 7; i <= n; i++) {         cout << 2 <<     << i << endl;       }     }     cout <<  1 2 n ;     for (long long i = 3; i <= n; i++) {       cout << 2 <<     << i << endl;     }   }   signed main() {     long long t = 1;     ios_base::sync_with_stdio(false);     cin.tie(NULL);     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long pot[35];   int numbits(long long n) {     int conta = 0;     for (int i = 0; i <= 33; i++) {       if (n & pot[i]) conta++;     }     return conta;   }   int main() {     pot[0] = 1;     for (int i = 1; i <= 33; i++) pot[i] = 2 * pot[i - 1];     long long n, p;     scanf( %lld %lld , &n, &p);     if (p == 0)       printf( %d n , numbits(n));     else if (p < 0) {       int k = 0;       bool teste = true;       while (teste) {         if (numbits(n - k * p) <= k) {           printf( %d n , k);           teste = false;         } else           k++;       }     } else {       int k = 0;       bool funfa = false;       while (k <= (n / (p + 1)) && funfa == false) {         if (numbits(n - k * p) <= k) {           printf( %d n , k);           funfa = true;         }         k++;       }       if (!funfa) printf( -1 n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int a[6];     for (int i = 0; i < 6; i++) {       a[i] = s[i] - 48;     }     int sum1 = a[0] + a[1] + a[2];     int sum2 = a[3] + a[4] + a[5];     int difference = abs(sum1 - sum2);     int flag = 0;     if (sum1 > sum2) flag = 3;     for (int i = flag; i <= flag + 2; i++) {       a[i] = 9 - a[i];     }     int count = 0;     int sum = 0;     if (difference == 0) {       cout << 0 << endl;       return 0;     }     while (1) {       int maxi = 0;       for (int i = 0; i < 6; i++) {         if (a[maxi] < a[i]) maxi = i;       }       if (a[maxi] == -1) return 0;       count += 1;       sum += a[maxi];       if (sum >= difference) {         cout << count << endl;         return 0;       }       a[maxi] = -1;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int low = 0, up = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] < 93) {         up++;       } else {         low++;       }     }     if (low >= up) {       for (int i = 0; i < s.size(); i++) {         s[i] = tolower(s[i]);       }     } else {       for (int i = 0; i < s.size(); i++) {         s[i] = toupper(s[i]);       }     }     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   int p[500005], q[500005], c[500005], f[500005], siz[500005], fx[500005],       sizx[500005], cln[500005 * 2], ans[500005], qu[500005], id[500005];   struct edge {     int u, v, w, nxt, id;   } e[500005];   vector<int> a[500005];   inline int find(int);   void swap(int& a, int& b) {     int t = a;     a = b;     b = t;   }   bool cmpp(int x, int y) { return e[x].w < e[y].w; }   bool cmpq(int x, int y) { return c[x] < c[y]; }   inline int read() {     int x = 0;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + (c ^ 48);       c = getchar();     }     return x;   }   int main() {     int i, j, n, m, u, v, nm, t, Q, tmp, top = 0, pos = 1, z;     n = read();     m = read();     for (i = 1; i <= m; ++i) {       e[i].u = read();       e[i].v = read();       e[i].w = read();     }     t = read();     for (i = 1; i <= t; ++i) {       nm = read();       for (j = 1; j <= nm; ++j) qu[j] = read();       sort(qu + 1, qu + nm + 1, cmpp);       for (j = 1; j <= nm; ++j) {         if (e[qu[j - 1]].w ^ e[qu[j]].w) {           if (top) a[top][0] = Q;           Q = 0;           id[++top] = i;           c[top] = e[qu[j]].w;           a[top].push_back(0);         }         ++Q;         a[top].push_back(qu[j]);       }     }     a[top][0] = Q;     Q = 0;     for (i = 1; i <= max(top, max(n, m)); ++i) p[i] = q[i] = f[i] = fx[i] = i;     for (i = 1; i <= n; ++i) siz[i] = sizx[i] = 1;     sort(p + 1, p + m + 1, cmpp);     sort(q + 1, q + top + 1, cmpq);     for (i = 1; i <= top; ++i) {       Q = q[i];       tmp = 0;       while (pos <= m && e[p[pos]].w < c[Q]) {         u = find(e[p[pos]].u);         v = find(e[p[pos]].v);         ++pos;         if (u == v) continue;         if (siz[u] < siz[v]) swap(u, v);         cln[++tmp] = v;         f[v] = u;         siz[u] += siz[v];       }       for (j = 1; j <= tmp; ++j) {         fx[cln[j]] = f[cln[j]];         sizx[cln[j]] = siz[cln[j]];       }       nm = a[Q][0];       tmp = 0;       for (j = 1; j <= nm; ++j) {         z = a[Q][j];         u = find(e[z].u);         v = find(e[z].v);         if (u == v) {           ans[id[Q]] = 1;           break;         }         if (siz[u] < siz[v]) swap(u, v);         cln[++tmp] = v;         f[v] = u;         siz[u] += siz[v];       }       for (j = 1; j <= tmp; ++j) {         f[cln[j]] = fx[cln[j]];         siz[cln[j]] = sizx[cln[j]];       }     }     for (i = 1; i <= t; ++i) puts(ans[i] ?  NO  :  YES );     return 0;   }   inline int find(int u) {     while (u != f[u]) u = f[u];     return u;   }


#include <bits/stdc++.h>   using namespace std;   const int desll[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   const long long mod = 1e9 + 7;   const int maxn = 1e6 + 7;   const int maxm = 1e8 + 7;   const double eps = 1e-4;   int m, n, ar[maxn];   char ch[maxn];   int armid[maxn];   struct node {     int val, num;     char c;     node *pre, *l, *r;     void clear() { l = r = NULL; }   } no[maxn];   int ma, ans = 0;   int newCal(node* u) {     if (u->c ==  N )       return 1 - (u->l->val);     else if (u->c ==  A )       return u->l->val & u->r->val;     else if (u->c ==  X )       return u->l->val ^ u->r->val;     else if (u->c ==  O )       return u->l->val | u->r->val;   }   void dfs(node* u, int x) {     int now = u->val;     u->val = x;     if (u->pre == NULL || now == x) {       ans = no[1].val;     } else {       if (armid[u->num] != -1) {         ans = armid[u->num];       } else {         dfs(u->pre, newCal(u->pre));         armid[u->num] = ans;       }     }     u->val = now;   }   void init(node* u) {     if (u->c ==  I ) return;     init(u->l);     if (u->r != NULL) init(u->r);     if (u->c ==  N )       u->val = 1 - (u->l->val);     else if (u->c ==  A )       u->val = u->l->val & u->r->val;     else if (u->c ==  X )       u->val = u->l->val ^ u->r->val;     else if (u->c ==  O )       u->val = u->l->val | u->r->val;   }   int main() {     scanf( %d , &n);     int x, y;     no[1].pre = NULL;     for (int i = 0; i <= n; i++) no[i].clear();     for (int i = 1; i <= n; i++) {       scanf( %s , ch);       scanf( %d , &x);       if (ch[0] ==  A  || ch[0] ==  O  || ch[0] ==  X ) {         scanf( %d , &y);         no[i].l = &no[x];         no[i].r = &no[y];         no[x].pre = &no[i];         no[y].pre = &no[i];         no[i].c = ch[0];       } else if (ch[0] ==  N ) {         no[i].l = &no[x];         no[x].pre = &no[i];         no[i].c = ch[0];       } else {         no[i].val = x;         no[i].c = ch[0];       }       no[i].num = i;     }     memset(armid, -1, sizeof(armid));     init(&no[1]);     int len = 0;     for (int i = 1; i <= n; i++) {       ma = 0;       if (no[i].c ==  I ) {         dfs(&no[i], 1 - no[i].val);         ar[len++] = ans;       }     }     for (int i = 0; i < len; i++) {       printf( %d , ar[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, const char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int c, m, t, r, i, W[301], tot, j;     map<int, bool> used;     map<int, bool> ends;     map<int, bool> G;     cin >> m >> t >> r;     for (i = 0; i < m; i++) {       cin >> W[i];       G[W[i]] = true;     }     if (r > t) {       cout <<  -1 n ;       return 0;     }     c = 0;     tot = 0;     for (i = 0; i <= W[m - 1]; i++) {       if (G[i] == true) {         if (c < r) {           for (j = i - 1; j >= i - t && c < r; j--) {             if (used[j] == false) {               used[j] = true;               ends[j + t] = true;               c++;               tot++;             }           }         }         if (c < r) {           cout <<  -1 n ;           return 0;         }       }       if (ends[i] == true) c--;     }     cout << tot <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     cout <<  0 0  << endl;     int l = 1, r = (int)1e9, mid = l + (r - l) / 2;     string s;     cin >> s;     int first;     if (s[0] ==  b ) {       first = 0;     } else {       first = 1;     }     for (int i = 1; i < n; i++) {       cout << mid <<     << mid << endl;       cin >> s;       if ((s[0] ==  w ) == first) {         l = mid + 1;       } else {         r = mid;       }       mid = l + (r - l) / 2;     }     if (l >= r && mid != (int)1e9) {       cout << mid - 1 <<     << mid <<     << mid <<     << mid - 1 << endl;     } else {       cout << mid <<     << 0 <<     << mid <<     << 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 3e5 + 5;   pair<long double, int> x[MAX];   int main() {     ios::sync_with_stdio(false);     int n, id, k;     cin >> n;     for (int i = 0; i < (int)(n); i++) {       cin >> x[i].first;       x[i].second = i + 1;     }     cin >> k;     sort(x, x + n);     long double acum1 = 0, acum2 = 0, ans = 0;     for (long long i = 0; i < k; i++) {       acum1 += x[i].first;       acum2 += (x[i].first * ((long double)(i + 1)));       ans = 2 * acum2 - ((long double)(k + 1)) * acum1;       id = 0;     }     for (long long i = 0; i + k < n; i++) {       acum2 = (acum2 - acum1 + ((long double)k) * x[i + k].first);       acum1 = (acum1 - x[i].first + x[i + k].first);       if (ans > 2 * acum2 - ((long double)(k + 1)) * acum1) {         id = i + 1, ans = 2 * acum2 - ((long double)(k + 1)) * acum1;       }     }     for (int i = 0; i < (int)(k); i++) {       if (i) cout <<    ;       cout << x[i + id].second;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long getnum() {     char c = getchar();     long long num, sign = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) sign = -1;     for (num = 0; c >=  0  && c <=  9 ;) {       c -=  0 ;       num = num * 10 + c;       c = getchar();     }     return num * sign;   }   long long A[3004], C[3004], B[1004];   vector<long long> V[3004], W[3004];   priority_queue<int, vector<int>, greater<int>> PQ;   int main() {     int n = getnum(), m = getnum();     for (int i = 1; i <= n; i++) {       A[i] = getnum();       B[A[i]]++;       C[i] = getnum();       V[A[i]].push_back(C[i]);       W[A[i]].push_back(C[i]);     }     for (int i = 1; i <= m; i++) {       sort(V[i].begin(), V[i].end());       sort(W[i].begin(), W[i].end());       for (int j = 1; j < V[i].size(); j++) V[i][j] += V[i][j - 1];     }     long long ans = LLONG_MAX;     int l = 1, r = n;     for (int mid = 1; mid <= n; mid++) {       int can = B[1];       long long sum = 0;       for (int i = 2; i <= m; i++) {         for (int j = 0, rem = W[i].size(); j < W[i].size(); j++) {           if (rem >= mid)             sum += W[i][j], can++, rem--;           else             PQ.push(W[i][j]);         }       }       while (can < mid) {         if (PQ.size() == 0) break;         int x = PQ.top();         PQ.pop();         can++;         sum += x;       }       while (PQ.size()) PQ.pop();       if (can >= mid) {         ans = min(ans, sum);       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   void FREOPEN() {}   inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; }   const int N = (int)200500, inf = (int)2e9, MOD = (int)1e9 + 7, alph = 26, C = 9;   char CH[N];   const long long INF = (long long)1e18;   const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};   const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};   pair<int, int> t[C][N * 4];   int non[N], a[N];   void build(int w, int v, int l, int r) {     t[w][v] = {inf, inf};     if (l == r) return;     int mid = (l + r) / 2;     build(w, v + v, l, mid);     build(w, v + v + 1, mid + 1, r);   }   pair<int, int> comb(pair<int, int> f, pair<int, int> s) {     pair<int, int> ret = f;     if (ret.first > s.first) {       ret.second = ret.first;       ret.first = s.first;       if (ret.second > s.second) ret.second = s.second;     } else if (ret.second > s.first)       ret.second = s.first;     return ret;   }   void upd(int w, int v, int l, int r, int p, int x) {     if (l == r) {       if (x == 1)         t[w][v] = {a[l], inf};       else         t[w][v] = {inf, inf};       return;     }     int mid = (l + r) / 2;     if (p <= mid)       upd(w, v + v, l, mid, p, x);     else       upd(w, v + v + 1, mid + 1, r, p, x);     t[w][v] = comb(t[w][v + v], t[w][v + v + 1]);   }   pair<int, int> get(int w, int v, int l, int r, int L, int R) {     if (L > R || l > R || r < L) return {inf, inf};     if (L <= l && R >= r) return t[w][v];     int mid = (l + r) / 2;     return comb(get(w, v + v, l, mid, L, R), get(w, v + v + 1, mid + 1, r, L, R));   }   int calc(int l, int r, int n) {     int ret = inf;     for (int k = 0; k < C; k++) {       pair<int, int> now = get(k, 1, 1, n, l, r);       if (now.first != inf && now.second != inf)         ret = min(ret, now.first + now.second);     }     if (ret == inf) ret = -1;     return ret;   }   int main() {     FREOPEN();     int n, m;     scanf( %d %d , &n, &m);     for (int k = 0; k < C; k++) build(k, 1, 1, n);     for (int i = 1; i <= n; i++) {       scanf( %d  , &a[i]);       int x = a[i];       for (int k = 0; k < C && x; k++) {         if (x % 10 > 0) {           non[i] |= (1 << k);         }         x /= 10;       }       for (int k = 0; k < C; k++) {         if (non[i] & (1 << k)) upd(k, 1, 1, n, i, 1);       }     }     for (int i = 1; i <= m; i++) {       int ty;       scanf( %d  , &ty);       if (ty == 1) {         int p, x;         scanf( %d %d , &p, &x);         for (int k = 0; k < C; k++) {           if (non[p] & (1 << k)) upd(k, 1, 1, n, p, 0);         }         a[p] = x;         non[p] = 0;         for (int k = 0; k < C && x; k++) {           if (x % 10 > 0) {             non[p] |= (1 << k);           }           x /= 10;         }         for (int k = 0; k < C; k++) {           if (non[p] & (1 << k)) upd(k, 1, 1, n, p, 1);         }       } else {         int lef, rig;         scanf( %d %d , &lef, &rig);         printf( %d n , calc(lef, rig, n));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<vector<int> > Dist, Alist;   vector<int> Distance, GivenPath;   int mi, ma;   bool isThere(int u, vector<int>& alist) {     for (auto e : alist) {       if (e == u) return true;     }     return false;   }   int main() {     cin >> n >> m;     vector<int> Alist[n];     vector<int> Alist2[n];     vector<int> Dist[n + 5];     int Distance[n];     memset(Distance, -1, sizeof(Distance));     for (int i = 0 - (0 > m); i != m - (0 > m); i += 1 - 2 * (0 > m)) {       int u, v;       cin >> u >> v, u--, v--;       Alist[v].push_back(u);       Alist2[u].push_back(v);     }     cin >> k;     GivenPath.assign(k, 0);     for (int i = 0 - (0 > k); i != k - (0 > k); i += 1 - 2 * (0 > k))       cin >> GivenPath[i], GivenPath[i]--;     mi = ma = 0;     Distance[GivenPath.back()] = 0;     Dist[0].push_back(GivenPath.back());     queue<int> q;     q.push(GivenPath.back());     while (!q.empty()) {       int u = q.front();       q.pop();       for (auto& e : Alist[u]) {         if (Distance[e] == -1) {           Distance[e] = Distance[u] + 1;           Dist[Distance[e]].push_back(e);           q.push(e);         }       }     }     for (int i = 0 - (0 > k - 1); i != k - 1 - (0 > k - 1);          i += 1 - 2 * (0 > k - 1)) {       int dist = Distance[GivenPath[i]] - 1;       if (dist != Distance[GivenPath[i + 1]]) {         mi++, ma++;         continue;       }       for (auto e : Alist2[GivenPath[i]]) {         if (e != GivenPath[i + 1] && Distance[e] == dist) {           ma++;           break;         }       }     }     cout << mi <<     << ma <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long bip(long long x, long long n, long long mod) {     long long res = 1;     while (n) {       if (n & 1) {         res = (res * x) % mod;       }       x = (x * x) % mod;       n >>= 1;     }     return res;   }   const int maxn = 1e9;   const long long inf = 1e18;   const int mod = 1e9 + 7;   const int N = 1e3 + 10;   int n, m, a[1010], ind;   long long C[10][10];   void Cnk() {     for (int i = 1; i < 10; i++) {       C[i][0] = C[i][i] = 1;       for (int j = 1; j < i; j++) {         C[i][j] = C[i - 1][j - 1] + C[i - 1][j];       }     }   }   void solve() {     cin >> n >> m >> ind;     for (int i = 1; i <= m; i++) cin >> a[i];     n--, a[ind]--;     int sum = 0;     for (int i = 1; i <= m; i++) sum += a[i];     if (sum < n) {       cout << -1;       return;     }     if (a[ind] == 0) {       cout << 0.00000;       return;     }     int k = sum - a[ind], l = sum, x = l - n, y = k - n;     if (y < 0) {       cout << 1.00000000;       return;     }     long double up = 1, down = 1;     for (int i = k - n + 1; i <= l - n; i++) up *= (i + 0.0);     for (int i = k + 1; i <= l; i++) down *= (i + 0.0);     cout << 1.0000000000 - up / down;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int tt = 1;     cout << fixed << setprecision(9);     while (tt--) {       solve();       cout <<   n ;     }   }


#include <bits/stdc++.h>   const int Inf = (int)(1e9);   const int K = 1000 + 1;   const int N = 60 + 1;   int w[N][N][N];   int n, m, r;   int ans[K][N][N];   void prepare() {     for (int i = 0; i < K; ++i)       for (int j = 0; j < N; ++j)         for (int k = 0; k < N; ++k) ans[i][j][k] = Inf;     for (int i = 0; i < m; ++i) {       for (int k = 0; k < n; ++k)         for (int ii = 0; ii < n; ++ii)           for (int jj = 0; jj < n; ++jj)             w[i][ii][jj] = std::min(w[i][ii][jj], w[i][ii][k] + w[i][k][jj]);       for (int ii = 0; ii < n; ++ii)         for (int jj = 0; jj < n; ++jj)           ans[0][ii][jj] = std::min(ans[0][ii][jj], w[i][ii][jj]);     }     for (int i = 1; i < K; ++i) {       for (int ii = 0; ii < n; ++ii)         for (int jj = 0; jj < n; ++jj) ans[i][ii][jj] = ans[i - 1][ii][jj];       for (int k = 0; k < n; ++k)         for (int ii = 0; ii < n; ++ii)           for (int jj = 0; jj < n; ++jj)             ans[i][ii][jj] =                 std::min(ans[i][ii][jj], ans[i - 1][ii][k] + ans[0][k][jj]);     }   }   int main() {     scanf( %d%d%d , &n, &m, &r);     for (int i = 0; i < m; ++i)       for (int j = 0; j < n; ++j)         for (int k = 0; k < n; ++k) scanf( %d , &w[i][j][k]);     prepare();     int si, ti, ki;     while (r-- > 0) {       scanf( %d%d%d , &si, &ti, &ki);       --si;       --ti;       printf( %d n , ans[ki][si][ti]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const long long mod = 998244353;   int LIMIT;   long long modpow(long long b, long long e) {     long long ans = 1;     for (; e; b = b * b % mod, e /= 2)       if (e & 1) ans = ans * b % mod;     return ans;   }   const long long root = 62;   inline void ntt(vector<long long>& a, vector<long long>& rt,                   vector<long long>& rev, int n) {     for (int i = 0; i < (n); ++i)       if (i < rev[i]) swap(a[i], a[rev[i]]);     for (int k = 1; k < n; k *= 2)       for (int i = 0; i < n; i += 2 * k)         for (int j = 0; j < (k); ++j) {           long long z = rt[j + k] * a[i + j + k] % mod, &ai = a[i + j];           a[i + j + k] = (z > ai ? ai - z + mod : ai - z);           ai += (ai + z >= mod ? z - mod : z);         }   }   vector<long long> conv(const vector<long long>& a, const vector<long long>& b) {     if (a.empty() || b.empty()) return {};     int s = (int)(a).size() + (int)(b).size() - 1, B = 32 - __builtin_clz(s),         n = 1 << B;     vector<long long> L(a), R(b), out(n), rt(n, 1), rev(n);     L.resize(n), R.resize(n);     for (int i = 0; i < (n); ++i) rev[i] = (rev[i / 2] | (i & 1) << B) / 2;     long long curL = mod / 2, inv = modpow(n, mod - 2);     for (int k = 2; k < n; k *= 2) {       long long z[] = {1, modpow(root, curL /= 2)};       for (int i = k; i < (2 * k); ++i) rt[i] = rt[i / 2] * z[i & 1] % mod;     }     ntt(L, rt, rev, n);     ntt(R, rt, rev, n);     for (int i = 0; i < (n); ++i)       out[-i & (n - 1)] = L[i] * R[i] % mod * inv % mod;     ntt(out, rt, rev, n);     return {out.begin(), out.begin() + s};   }   void read(int& n) {     n = 0;     char c = getchar();     while (c >=  0  && c <=  9 ) {       n = n * 10 + c -  0 ;       c = getchar();     }   }   vector<long long> t;   vector<long long> res[3000005];   vector<long long> s[3000005];   int a[200005];   int cnt[3000005];   priority_queue<pair<int, int> > pq;   int main() {     int n;     read(n);     LIMIT = n / 2 + 1;     for (int i = 1; i <= n; i++) {       read(a[i]);       cnt[a[i]]++;       s[a[i]].push_back(1);     }     for (int i = 1; i <= n; i++) {       if (cnt[a[i]]) {         s[a[i]].push_back(1);         pq.push({-cnt[a[i]], a[i]});         cnt[a[i]] = 0;       }     }     while (!pq.empty()) {       int v = pq.top().second;       pq.pop();       if (pq.empty()) {         cout << s[v][n / 2];         return 0;       }       int u = pq.top().second;       pq.pop();       s[v] = conv(s[v], s[u]);       while (s[v].size() > LIMIT) s[v].pop_back();       pq.push({-s[v].size(), v});     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5;   bool vis[N];   int n, m, ans;   vector<int> top;   vector<pair<int, int> > adj[N];   void dfs(int u) {     vis[u] = 1;     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i].first;       if (!vis[v]) dfs(v);     }     top.push_back(u);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back({v, i});     }     for (int i = 1; i <= n; i++)       if (!vis[i]) dfs(i);     for (int i = n - 1; i > 0; i--) {       int u = top[i], v = top[i - 1], j = 0;       for (j = 0; j < adj[u].size(); j++) {         if (adj[u][j].first == v) {           ans = max(adj[u][j].second, ans);           break;         }       }       if (j == adj[u].size()) {         cout <<  -1 n ;         return 0;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long num, length, devisor = 1, rest;     long long i;     string str;     cin >> str;     length = str.length();     num = atoi(str.c_str());     for (i = 1; i <= length - 1; i++) {       devisor *= 10;     }     rest = num % devisor;     cout << devisor - rest << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct st {     long long kici, san;     long long kici2, san2;   };   long long v[200010];   st T[200010 * 4];   long long t, x, y, q;   long long n, k, S, O;   void upd(long long pos, long long x, long long l, long long r, long long node) {     if (l == r) {       v[pos] += x;       if (v[pos] <= O)         T[node].kici += x;       else if (v[pos] - x <= O)         T[node].san++, T[node].kici -= v[pos] - x;       if (v[pos] <= S)         T[node].kici2 += x;       else if (v[pos] - x <= S)         T[node].san2++, T[node].kici2 -= v[pos] - x;       return;     }     if (pos <= (l + r) / 2)       upd(pos, x, l, (l + r) / 2, node * 2);     else       upd(pos, x, (l + r) / 2 + 1, r, node * 2 + 1);     T[node].san = T[node * 2].san + T[node * 2 + 1].san;     T[node].san2 = T[node * 2].san2 + T[node * 2 + 1].san2;     T[node].kici = T[node * 2].kici + T[node * 2 + 1].kici;     T[node].kici2 = T[node * 2].kici2 + T[node * 2 + 1].kici2;   }   long long tap(long long a, long long b, long long x, long long l, long long r,                 long long node) {     if (l > b || r < a) return 0;     if (l >= a && r <= b) {       if (x == 1)         return T[node].kici + T[node].san * O;       else         return T[node].kici2 + T[node].san2 * S;     }     return tap(a, b, x, l, (l + r) / 2, node * 2) +            tap(a, b, x, (l + r) / 2 + 1, r, node * 2 + 1);   }   int main() {     scanf( %lld%lld%lld%lld%lld , &n, &k, &S, &O, &q);     while (q--) {       scanf( %lld%lld , &t, &x);       if (t == 1) {         scanf( %lld , &y);         upd(x, y, 1, n, 1);       } else         printf( %lld n , tap(1, x - 1, 1, 1, n, 1) + tap(x + k, n, 2, 1, n, 1));     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> p;   vector<int> cor;   int total;   void dfs_visit(vector<vector<int>>& adj, int i) {     cor[i] = 1;     for (int j : adj[i]) {       if (cor[j] == 1) {         total++;       }       if (cor[j] == 0) {         p[j] = i;         dfs_visit(adj, j);       }     }     cor[i] = 2;   }   int dfs(vector<vector<int>>& adj, int n) {     int ans = 10;     for (int i = 1; i <= n; i++) {       total = 0;       p.assign(n + 1, 1);       cor.assign(n + 1, 0);       total = 0;       int j = i;       if (cor[j] == 0) {         dfs_visit(adj, j);       }       for (int j = 1; j <= n; j++) {         if (cor[j] == 0) {           dfs_visit(adj, j);         }       }       ans = min(ans, total);     }     return ans;   }   int main() {     int n, m, a, b;     cin >> n >> m;     vector<int> vetor;     vetor.assign(n + 1, 0);     vector<vector<int>> adj;     vector<int> lista;     lista.clear();     adj.assign(n + 1, lista);     for (int i = 1; i <= m; i++) {       cin >> a >> b;       adj[a].push_back(b);     }     int ans = dfs(adj, n);     if (ans <= 1)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sy, a, b, n, l[200010], r[200010], sum[200010], m, x, y;   double xl, xr, ans;   int getmid(double xi) {     int L = 1, R = n + 1, ret, mid;     while (L < R) {       mid = (L + R) >> 1;       if (l[mid] <= xi)         ret = mid, L = mid + 1;       else         R = mid;     }     return ret;   }   double getlenth(double xi) {     if (xi <= l[1]) return 0;     int pos = getmid(xi);     return min(xi, (double)r[pos]) - l[pos] + sum[pos - 1];   }   int main() {     scanf( %d%d%d , &sy, &a, &b);     scanf( %d , &n);     sum[0] = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &l[i], &r[i]);       sum[i] = sum[i - 1] + r[i] - l[i];     }     scanf( %d , &m);     while (m--) {       scanf( %d%d , &x, &y);       xl = -1.0 * (x - a) / (y - sy) * y + x;       xr = -1.0 * (x - b) / (y - sy) * y + x;       ans = (getlenth(xr) - getlenth(xl)) / (xr - xl) * (b - a);       printf( %.10lf n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   bool flag[maxn];   int phi[maxn];   vector<int> prime;   vector<int> fin[6][maxn];   int a[10][10], num;   void get_prime_phi() {     phi[1] = 1;     for (int i = 2; i < 1e5; i++) {       if (!flag[i]) {         prime.push_back(i);         phi[i] = i - 1;       }       int top = prime.size();       for (int j = 0; j < top && i * prime[j] < 1e5; j++) {         flag[i * prime[j]] = 1;         if (i % prime[j])           phi[i * prime[j]] = phi[i] * (prime[j] - 1);         else {           phi[i * prime[j]] = phi[i] * prime[j];           break;         }       }     }   }   void init() {     int si = prime.size();     for (int i = 0; i < si; i++) {       int x = prime[i];       for (int j = 1; j <= 5; j++) {         fin[j][x / 10].push_back(prime[i]);         x /= 10;       }     }   }   int dfs(int x) {     int ans = 0;     int tem = 0, tt = num - x;     for (int i = 0; i < x; i++) tem = tem * 10 + a[x][i];     int si = fin[tt][tem].size();     if (x == num - 1) return si;     for (int i = 0; i < si; i++) {       int xx = fin[tt][tem][i];       for (int j = num - 1; j >= x; j--) {         a[x][j] = xx % 10;         xx /= 10;       }       for (int j = x + 1; j < num; j++) a[j][x] = a[x][j];       ans += dfs(x + 1);     }     return ans;   }   int main() {     get_prime_phi();     init();     int cas, n;     cin >> cas;     while (cas--) {       memset(a, 0, sizeof(a));       scanf( %d , &n);       int x = n;       num = 0;       while (x) {         num++;         x /= 10;       }       x = n;       for (int i = 0; i < num; i++) {         a[0][num - 1 - i] = a[num - 1 - i][0] = x % 10;         x /= 10;       }       printf( %d n , dfs(1));     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void dbg_vector(const T& v) {     for (size_t i = 0; i < v.size(); ++i) cout << v[i] <<    ;     cout << endl;   }   void dbg() { cerr << endl; }   template <typename Head, typename... Tail>   void dbg(Head H, Tail... T) {     cerr << H <<    ;     dbg(T...);   }   struct pair_hash {     template <class T1, class T2>     size_t operator()(const pair<T1, T2>& p) const {       auto h1 = std::hash<T1>{}(p.first);       auto h2 = std::hash<T2>{}(p.second);       return h1 ^ h2;     }   };   struct Comparator {     template <class T1, class T2>     bool operator()(const std::pair<T1, T2>& lhs,                     const std::pair<T1, T2>& rhs) const {       if (lhs.first == rhs.first)         return lhs.second < rhs.second;       else         return lhs.first < rhs.first;     }   };   inline long long lmin(long long a, long long b) {     if (a < b) return a;     return b;   }   inline long long lmax(long long a, long long b) {     if (a > b) return a;     return b;   }   long long INF = 1e18;   long long MOD = 998244353;   struct edge {     long long w, x, y;     edge(){};     edge(long long w, long long x, long long y) : w(w), x(x), y(y){};   };   struct EdgeComparator {     bool operator()(const edge& lhs, const edge& rhs) const {       if (lhs.w < rhs.w) return true;       return false;     }   };   vector<int> prefix_function(string& s) {     int n = s.size();     vector<int> p(n);     for (int i = 1; i < n; ++i) {       int j = p[i - 1];       while (s[i] != s[j] && j > 0) j = p[j - 1];       if (s[i] == s[j]) j++;       p[i] = j;     }     return p;   }   vector<long long> matmul(vector<long long>& a, vector<long long>& b) {     vector<long long> res(4);     res[0] = (a[0] * b[0] + a[1] * b[2]) % MOD;     res[1] = (a[0] * b[1] + a[1] * b[3]) % MOD;     res[2] = (a[2] * b[0] + a[3] * b[2]) % MOD;     res[3] = (a[2] * b[1] + a[3] * b[3]) % MOD;     return res;   }   vector<long long> matpow(vector<long long>& m, long long n) {     if (n == 1) return m;     if (n % 2 == 0) {       vector<long long> a = matpow(m, n >> 1);       return matmul(a, a);     } else {       vector<long long> a = matpow(m, n - 1);       return matmul(a, m);     }   }   struct point {     int x, y;     point(){};     point(int x, int y) : x(x), y(y){};   };   struct rect {     int x1, y1, x2, y2;     rect(){};     rect(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2){};   };   bool cw(point& a, point& b) {     int cross_product = a.x * b.y - a.y * b.x;     return cross_product <= 0;   }   bool oneline(point& a, point& b) {     int cross_product = a.x * b.y - a.y * b.x;     return cross_product == 0;   }   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     vector<vector<bool> > can(n, vector<bool>(5));     vector<vector<int> > p(n, vector<int>(5));     can[0][0] = true;     can[0][1] = true;     can[0][2] = true;     can[0][3] = true;     can[0][4] = true;     for (int i = 1; i < n; ++i) {       if (a[i] == a[i - 1]) {         for (int k = 0; k <= 4; ++k) {           for (int j = 0; j <= 4; ++j) {             if (j != k && can[i - 1][j]) {               can[i][k] = true;               p[i][k] = j;               break;             }           }         }       } else if (a[i] > a[i - 1]) {         if (can[i - 1][0]) {           can[i][1] = true;           p[i][1] = 0;           can[i][2] = true;           p[i][2] = 0;           can[i][3] = true;           p[i][3] = 0;           can[i][4] = true;           p[i][4] = 0;         } else if (can[i - 1][1]) {           can[i][2] = true;           p[i][2] = 1;           can[i][3] = true;           p[i][3] = 1;           can[i][4] = true;           p[i][4] = 1;         } else if (can[i - 1][2]) {           can[i][3] = true;           p[i][3] = 2;           can[i][4] = true;           p[i][4] = 2;         } else if (can[i - 1][3]) {           can[i][4] = true;           p[i][4] = 3;         } else {           cout << -1 << endl;           return 0;         }       } else {         if (can[i - 1][4]) {           can[i][0] = true;           p[i][0] = 4;           can[i][1] = true;           p[i][1] = 4;           can[i][2] = true;           p[i][2] = 4;           can[i][3] = true;           p[i][3] = 4;         } else if (can[i - 1][3]) {           can[i][0] = true;           p[i][0] = 3;           can[i][1] = true;           p[i][1] = 3;           can[i][2] = true;           p[i][2] = 3;         } else if (can[i - 1][2]) {           can[i][0] = true;           p[i][0] = 2;           can[i][1] = true;           p[i][1] = 2;         } else if (can[i - 1][1]) {           can[i][0] = true;           p[i][0] = 1;         } else {           cout << -1 << endl;           return 0;         }       }     }     vector<int> ans(n);     if (can[n - 1][0]) {       int i = n - 1;       int cur = 0;       ans[n - 1] = cur + 1;       while (i > 0) {         cur = p[i][cur];         ans[i - 1] = cur + 1;         i--;       }     } else if (can[n - 1][1]) {       int i = n - 1;       int cur = 1;       ans[n - 1] = cur + 1;       while (i > 0) {         cur = p[i][cur];         ans[i - 1] = cur + 1;         i--;       }     } else if (can[n - 1][2]) {       int i = n - 1;       int cur = 2;       ans[n - 1] = cur + 1;       while (i > 0) {         cur = p[i][cur];         ans[i - 1] = cur + 1;         i--;       }     } else if (can[n - 1][3]) {       int i = n - 1;       int cur = 3;       ans[n - 1] = cur + 1;       while (i > 0) {         cur = p[i][cur];         ans[i - 1] = cur + 1;         i--;       }     } else if (can[n - 1][4]) {       int i = n - 1;       int cur = 4;       ans[n - 1] = cur + 1;       while (i > 0) {         cur = p[i][cur];         ans[i - 1] = cur + 1;         i--;       }     }     for (int i = 0; i < n; ++i) cout << ans[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   constexpr int N = 1e5 + 5;   int n;   int in[N];   void solve(int x, int y) {     int res = 0;     while (x <= y) {       res++;       for (int i = 1; i <= n; i++) {         if (in[i] == x) x++;       }     }     cout << res <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; i++) cin >> in[i];     int q;     cin >> q;     while (q--) {       int p, x, y;       cin >> p >> x >> y;       if (p == 1) {         solve(x, y);       } else {         swap(in[x], in[y]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2 * 1e5 + 100;   long long t, n;   struct node {     long long m, p;   } sh[N];   bool cmp(node a, node b) { return a.m > b.m; }   signed main() {     scanf( %lld , &t);     while (t--) {       scanf( %lld , &n);       for (long long i = 1; i <= n; i++) scanf( %lld%lld , &sh[i].m, &sh[i].p);       sort(sh + 1, sh + 1 + n, cmp);       long long ans = 0;       priority_queue<long long, vector<long long>, greater<long long> > q;       for (long long i = 1; i <= n;) {         long long j = i;         while (j <= n && sh[j].m == sh[i].m) {           q.push(sh[j].p);           j++;         }         while (n - (long long)q.size() < sh[i].m) {           ans += q.top();           q.pop();         }         i = j;       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int inf = INT_MAX;   const int N = 100004;   const int modn = 1000000007;   complex<long long> v[N];   int p[N];   long long x(complex<long long> a) { return a.real(); }   long long y(complex<long long> a) { return a.imag(); }   long long cross(complex<long long> a, complex<long long> b) {     return y(conj(a) * b);   }   int n;   bool comp(int i, int j) {     if (x(v[i]) == x(v[j])) return y(v[i]) < y(v[j]);     return x(v[i]) < x(v[j]);   }   int sig(long long x) { return (x > 0) - (x < 0); }   int ok(complex<long long> a, complex<long long> b, complex<long long> c) {     return sig(cross(b - a, c - a));   }   int main() {     scanf( %d , &n);     long long a, b;     for (int i = 0; i < n; i++) {       p[i] = i;       cin >> a >> b;       v[i] = complex<long long>(a, b);     }     int cont = 0;     sort(p, p + n, comp);     for (int i = 0; i < n - 2; i++) {       if (ok(v[p[i]], v[p[i + 1]], v[p[i + 2]])) {         printf( %d %d %d n , p[i] + 1, p[i + 1] + 1, p[i + 2] + 1);         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2100;   int n, al;   double P;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> P;     al = n * (n - 1) * (n - 2) / 6;     int ans = n;     for (int i = 0; i <= n; i++) {       double temp = 0;       if (i >= 1 && n - i - 1 >= 0)         temp += (double)(i * (n - i) * (n - i - 1) / 2) / al / 2;       if (i >= 2) temp += (double)(i * (i - 1) * (n - i) / 2) / al;       if (i >= 3) temp += (double)(i * (i - 1) * (i - 2) / 6) / al;       if (temp >= P) {         ans = i;         break;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> n >> a >> b;     cout << (abs((b % n) + a + n) % n > 0 ? abs((b % n) + a + n) % n : n);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, normg, bekg, uykg, beksur, uyksur;     int i, j, x, y, t = 0, A[1000][2], fark;     cin >> n >> normg >> bekg >> uykg >> beksur >> uyksur;     for (i = 1; i <= n; i++) cin >> A[i][0] >> A[i][1];     for (i = 1; i <= n; i++) {       t += abs(A[i][1] - A[i][0]) * normg;       if (i != n) {         fark = abs(A[i + 1][0] - A[i][1]);         if (fark >= beksur) {           t += beksur * normg;           fark -= beksur;         } else           t += fark * normg, fark = 0;         if (fark >= uyksur) {           t += uyksur * bekg;           fark -= uyksur;         } else           t += fark * bekg, fark = 0;         if (fark > 0) t += fark * uykg;       }     }     cout << t;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, w = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = getchar();     }     while (c <=  9  && c >=  0 ) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     return w == 1 ? x : -x;   }   int n, ans[200005];   struct node {     int x, y, id;   } a[200005];   int main() {     n = read();     for (int i = 1; i <= n; i++) a[i].x = read(), a[i].y = read(), a[i].id = i;     while (1) {       long long x = 0, y = 0;       for (int i = 0; i <= n; i++) {         if ((x + a[i].x) * (x + a[i].x) + (y + a[i].y) * (y + a[i].y) <=             (x - a[i].x) * (x - a[i].x) + (y - a[i].y) * (y - a[i].y)) {           ans[a[i].id] = 1;           x += a[i].x;           y += a[i].y;         } else           ans[a[i].id] = -1, x -= a[i].x, y -= a[i].y;       }       if (x * x + y * y <= 1.5e6 * 1.5e6) break;       random_shuffle(a + 1, a + n + 1);     }     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GI(long long &a) { return scanf( %I64d , &a); }   long long n, i, j, k, tmp, m;   long long arr[100100];   string s, s1;   int32_t main() {     getline(cin, s1);     getline(std::cin, s);     k = 0;     for (long long i = 0; i < s1.length(); i++) k = k * 10 + s1[i] -  0 ;     n = s.length();     long long cur = 0;     vector<long long> l;     long long mm = 0;     for ((i) = 0; (i) < (long long)(n); ++i) {       cur++;       if (s[i] ==  -  || s[i] ==    ) {         l.push_back(cur);         mm = (mm > cur ? mm : cur);         cur = 0;       }     }     if (cur > 0) {       l.push_back(cur);       mm = max(mm, cur);     }     long long le = mm, r = 1000000, mid;     while (le < r) {       mid = (le + r) / 2;       long long cnt = 0;       long long last = 1000000;       for (long long i = 0; i < l.size(); i++) {         if (last + l[i] > mid) {           cnt++;           last = l[i];         } else           last = last + l[i];       }       if (cnt <= k)         r = mid;       else         le = mid + 1;     }     cout << le;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       if (n == 5) {         cout <<  2               <<                  <<  4               <<   n ;       } else         cout << 2 <<     << n / 2 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, m;   long long ans, c[N], a[N];   vector<pair<int, int> > v[N], store;   int main() {     scanf( %d%d , &n, &m);     long long k = n / m, x = 0, res = 0;     for (int i = 0; i < n; ++i) {       scanf( %lld , a + i);       v[a[i] % m].push_back({a[i], i});     }     for (int i = 0; i < m; ++i) c[i] = (int)v[i].size() - k;     for (int i = 1; i < m; ++i) c[i] += c[i - 1];     for (int i = 0; i < n; ++i) x = min(x, c[i]);     for (int i = 0; i < m; ++i) c[i] -= x, res += c[i];     printf( %lld n , res);     int st = 0;     for (int i = 0; i < m; ++i)       if (c[i] == 0) {         st = i;         break;       }     st = (st + 1) % m;     for (int time = 0; time < m; ++time) {       while (store.size() < c[st]) {         pair<int, int> t = v[st].back();         v[st].pop_back();         store.push_back({t.first - time, t.second});       }       while (store.size() > c[st]) {         pair<int, int> t = store.back();         store.pop_back();         v[st].push_back({t.first + time, t.second});       }       st = (st + 1) % m;     }     for (int i = 0; i < m; ++i)       for (auto &t : v[i]) a[t.second] = t.first;     for (int i = 0; i < n; ++i) printf( %d  , a[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n;     cin >> n;     string s =  <3 ;     for (int i = 0; i < n; i++) {       string a;       cin >> a;       s += a;       s +=  <3 ;     }     string b;     cin >> b;     long long int j = 0;     for (int i = 0; i < b.size(); i++) {       if (b[i] == s[j]) j++;     }     if (j >= s.size()) {       cout <<  yes ;     } else {       cout <<  no ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<long long> a(n);     vector<long long> v;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n - 1; i++) {       v.push_back(abs(a[i] - a[i + 1]));     }     long long ans = a[n - 1] - a[0];     sort(v.begin(), v.end());     int y = v.size() - 1;     k--;     while (k--) {       ans = ans - v[y--];     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   int a[51][51];   int p[51];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]);     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         if (p[i] != 0) continue;         int sum = 0;         for (int j = 1; j <= n; j++) {           if (p[j] != 0) continue;           sum += a[i][j];         }         if (sum == k * (n - k)) {           p[i] = k;           break;         }       }     }     for (int i = 1; i <= n; i++) printf( %d  , p[i]);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, i, sum = 0, last = -1e8, c, k;     scanf( %d%d , &n, &c);     for (i = 1; i <= n; i++) {       scanf( %d , &k);       if (k - last > c) {         sum = 1;         last = k;       } else {         sum++;         last = k;       }     }     printf( %d , sum);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, prnt[N];   long long k;   long long sum = 0;   vector<int> g[N];   int sz[N];   void DFS(int node, int prnt) {     sz[node] = 1;     for (int i = 0; i < (int)g[node].size(); i++) {       if (g[node][i] == prnt) continue;       DFS(g[node][i], node);       sz[node] += sz[g[node][i]];     }   }   int get_centroid(int node, int prnt) {     for (int i = 0; i < (int)g[node].size(); i++) {       if (g[node][i] == prnt) continue;       if (sz[g[node][i]] > n / 2) return get_centroid(g[node][i], node);     }     return node;   }   int Idx, cur[N], in[N], depth[N], val[N], root;   bool taken[N];   void init(int node) {     cur[node] = Idx;     val[Idx]++;     sum += depth[node];     for (int i = 0; i < (int)g[node].size(); i++) {       if (g[node][i] == prnt[node]) continue;       depth[g[node][i]] = depth[node] + 1;       prnt[g[node][i]] = node;       init(g[node][i]);       in[node]++;     }   }   vector<int> has[N];   priority_queue<pair<int, int> > q[N];   set<pair<int, int> > st[N];   set<pair<int, int> >::iterator it, it2;   vector<pair<int, int> > ans;   priority_queue<pair<int, int> > pq, q2;   void finish(int node) {     it2 = st[cur[node]].find(make_pair(depth[node], node));     if (it2 != st[cur[node]].end()) st[cur[node]].erase(it2);     int idx = -1;     for (int i = 0; i < (int)g[node].size(); i++) {       if (g[node][i] == prnt[node]) continue;       if (idx == -1 ||           (int)has[g[node][i]].size() > (int)has[g[node][idx]].size()) {         idx = i;       }     }     if (idx != -1) swap(has[node], has[g[node][idx]]);     for (int i = 0; i < (int)g[node].size(); i++) {       if (i == idx || g[node][i] == prnt[node]) continue;       while ((int)has[g[node][i]].size() > 0) {         has[node].push_back(has[g[node][i]].back());         has[g[node][i]].pop_back();       }     }     if (!taken[node]) has[node].push_back(node);     if (prnt[node] == root) return;     in[prnt[node]]--;     if ((int)has[node].size() > 0)       q[prnt[node]].push(make_pair((int)has[node].size(), node));     if (in[prnt[node]] == 0) {       st[cur[node]].insert(make_pair(depth[prnt[node]], prnt[node]));     }   }   vector<int> lll;   int main() {     scanf( %d%lld , &n, &k);     for (int u, v, i = 0; i < n - 1; i++) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     DFS(1, -1);     root = get_centroid(1, -1);     prnt[root] = -1;     for (int i = 0; i < (int)g[root].size(); i++) {       Idx = i;       prnt[g[root][i]] = root;       depth[g[root][i]] = 1;       init(g[root][i]);       in[root]++;       pq.push(make_pair(val[Idx], Idx));     }     if (k > sum || (sum % 2) != (k % 2)) {       puts( NO );       return 0;     }     for (int i = 1; i <= n; i++) {       if (in[i] == 0) {         lll.push_back(i);       }     }     for (int i = 0; i < (int)lll.size(); i++) {       finish(lll[i]);     }     int tmp1, tmp2, tmp3, tmp4;     long long needed;     while (sum > k && (int)pq.size() > 0) {       tmp1 = pq.top().second;       pq.pop();       if (val[tmp1] < 2) break;       needed = (sum - k) / 2;       assert((int)st[tmp1].size() > 0);       while (true) {         it = st[tmp1].end();         it--;         tmp2 = it->second;         if ((long long)it->first > needed) {           finish(tmp2);           continue;         }         if ((int)q[tmp2].size() > 1) {           tmp3 = q[tmp2].top().second;           q[tmp2].pop();           tmp4 = q[tmp2].top().second;           q[tmp2].pop();           ans.push_back(make_pair(has[tmp3].back(), has[tmp4].back()));           taken[has[tmp3].back()] = taken[has[tmp4].back()] = true;           has[tmp3].pop_back();           has[tmp4].pop_back();           if ((int)has[tmp3].size() > 0)             q[tmp2].push(make_pair((int)has[tmp3].size(), tmp3));           if ((int)has[tmp4].size() > 0)             q[tmp2].push(make_pair((int)has[tmp4].size(), tmp4));           sum -= (long long)2 * depth[tmp2];           break;         }         if (taken[tmp2] || (int)q[tmp2].size() == 0) {           finish(tmp2);           continue;         }         tmp3 = q[tmp2].top().second;         q[tmp2].pop();         ans.push_back(make_pair(tmp2, has[tmp3].back()));         taken[tmp2] = true;         taken[has[tmp3].back()] = true;         has[tmp3].pop_back();         finish(tmp2);         sum -= (long long)2 * depth[tmp2];         break;       }       val[tmp1] -= 2;       if (val[tmp1] > 0) pq.push(make_pair(val[tmp1], tmp1));     }     for (int i = 1; i <= n; i++) has[i].clear();     for (int i = 1; i <= n; i++) {       if (i != root && !taken[i]) has[cur[i]].push_back(i);     }     for (int i = 0; i < (int)g[root].size(); i++) {       if ((int)has[i].size() > 0) q2.push(make_pair((int)has[i].size(), i));     }     while ((int)q2.size() > 1) {       tmp1 = q2.top().second;       q2.pop();       tmp2 = q2.top().second;       q2.pop();       ans.push_back(make_pair(has[tmp1].back(), has[tmp2].back()));       has[tmp1].pop_back();       has[tmp2].pop_back();       if ((int)has[tmp1].size() > 0)         q2.push(make_pair((int)has[tmp1].size(), tmp1));       if ((int)has[tmp2].size() > 0)         q2.push(make_pair((int)has[tmp2].size(), tmp2));     }     assert((int)q2.size() > 0);     tmp1 = q2.top().second;     tmp1 = has[tmp1].back();     ans.push_back(make_pair(root, tmp1));     if (sum == k) {       puts( YES );       assert((int)ans.size() == n / 2);       for (int i = 0; i < (int)ans.size(); i++) {         printf( %d %d n , ans[i].first, ans[i].second);       }     } else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = 3.14159265359;   int main() {     int n;     long long p;     cin >> n >> p;     p /= 2;     vector<int> a;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       if (s ==  half )         a.push_back(0);       else         a.push_back(1);     }     long long tek = 0;     for (int i = n - 1; i >= 0; i--) {       if (a[i] == 1)         tek = (tek + 1) * 2LL;       else         tek = tek * 2LL;     }     long long ans = 0;     for (int i = 0; i < n; i++) {       if (tek % 2 == 0 && tek % 4 != 0) {         ans += (tek / 2) * p;         tek = tek / 2 - 1;       } else {         ans += (tek / 2) * p;         tek = tek / 2;       }     }     assert(tek == 0);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 30, Mod = 1e9 + 7;   const long long SQ = 330;   const long long INF = 1e17;   long long a[N], n, q;   long long sum[N << 2], mx[N << 2];   void Change(int x, int val, int id = 1, int b = 0, int e = n) {     if (e - b == 1) {       mx[id] = a[x] = val;       sum[id] = val;       return;     }     if (x < ((b + e) >> 1))       Change(x, val, id << 1, b, ((b + e) >> 1));     else       Change(x, val, id << 1 | 1, ((b + e) >> 1), e);     sum[id] = sum[id << 1] + sum[id << 1 | 1];     mx[id] = max(mx[id << 1], mx[id << 1 | 1]);   }   long long Get(int l, int r, int id = 1, int b = 0, int e = n) {     if (r <= b || e <= l) return 0;     if (l <= b && e <= r) return sum[id];     return Get(l, r, id << 1, b, ((b + e) >> 1)) +            Get(l, r, id << 1 | 1, ((b + e) >> 1), e);   }   void Set(int l, int r, int m, int id = 1, int b = 0, int e = n) {     if (mx[id] < m) return;     if (r <= b || e <= l) return;     if (e - b == 1) {       sum[id] = mx[id] = mx[id] % m;       return;     }     Set(l, r, m, id << 1, b, ((b + e) >> 1));     Set(l, r, m, id << 1 | 1, ((b + e) >> 1), e);     sum[id] = sum[id << 1] + sum[id << 1 | 1];     mx[id] = max(mx[id << 1], mx[id << 1 | 1]);   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     cin >> n >> q;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) Change(i, a[i]);     while (q--) {       long long t;       cin >> t;       switch (t) {         case 1:           int l, r;           cin >> l >> r;           l--;           cout << Get(l, r) <<   n ;           break;         case 2:           int x;           cin >> l >> r >> x;           l--;           Set(l, r, x);           break;         case 3:           int k;           cin >> k >> x;           Change(k - 1, x);       }     }     return (0);   }


#include <bits/stdc++.h>   using std::cin;   using std::cout;   using std::endl;   using std::pair;   using std::string;   using std::vector;   using vi = vector<int>;   using pii = pair<int, int>;   using vii = vector<pii>;   using ll = long long;   int solve();   int main(int argc, char* argv[]) {     ::std::ios::sync_with_stdio(false);     ::std::cin.tie(0);     ::std::cout.tie(0);     int t = 1;     for (int i = 1; i <= t; ++i) {       solve();     }   }   constexpr int MAX_N = 505050;   constexpr int MOD = 998244353;   int t[4 * MAX_N][2][2];   int n, m;   char s[MAX_N];   void pushup(int k) {     memset(t[k], 0, sizeof(t[k]));     for (int i = 0; i < 2; ++i) {       for (int j = 0; j < 2; ++j) {         t[k][i][j] = (t[k * 2][i][0] * 1LL * t[k * 2 + 1][0][j] +                       t[k * 2][i][1] * 1LL * t[k * 2 + 1][1][j]) %                      MOD;       }     }   }   void build(int k, int L, int R) {     if (L == R) {       memset(t[k], 0, sizeof(t[k]));       t[k][1][0] = 1;       t[k][0][0] = s[L] + 1;       if (s[L] == 1) t[k][0][1] = 9 - s[L + 1];       return;     }     int mid = (L + R) / 2;     build(k * 2, L, mid);     build(k * 2 + 1, mid + 1, R);     pushup(k);   }   void change(int k, int L, int R, int P) {     if (L == R) {       memset(t[k], 0, sizeof(t[k]));       t[k][1][0] = 1;       t[k][0][0] = s[L] + 1;       if (s[L] == 1) t[k][0][1] = 9 - s[L + 1];       return;     }     int mid = (L + R) / 2;     if (P <= mid)       change(k * 2, L, mid, P);     else       change(k * 2 + 1, mid + 1, R, P);     pushup(k);   }   int solve() {     string str;     cin >> n >> m;     cin >> str;     for (int i = 1; i <= n; ++i) s[i] = str[i - 1] -  0 ;     build(1, 1, n);     while (m--) {       int x, y;       cin >> x >> y;       s[x] = y;       if (x != 1) change(1, 1, n, x - 1);       change(1, 1, n, x);       cout << t[1][0][0] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 1e5 + 5;   map<long long int, long long int> m;   long long int n, ans;   void init() {     m[-1] = -1;     m[2e9] = 2e9;     m[1] = n;     ans = n;   }   void add(long long int l, long long int r) {     m[l] = r;     ans += r - l + 1;   }   void remove(long long int l, long long int r) {     long long int rem = 0;     auto it = m.lower_bound(l);     it--;     if (it->second >= l) {       m[l] = it->second;       it->second = l - 1;     }     it++;     while (it->first <= r) {       if (it->second > r) {         rem += r + 1 - it->first;         m[r + 1] = it->second;       } else         rem += it->second - it->first + 1;       auto it2 = it;       it++;       m.erase(it2);     }     ans -= rem;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int q;     cin >> n >> q;     init();     while (q--) {       long long int l, r, k;       cin >> l >> r >> k;       remove(l, r);       if (k == 2) add(l, r);       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 201110, M = 501110;   int n, m, numeven, numodd;   int a[N];   stack<int> odd, even;   map<int, int> mp;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       mp[a[i]]++;       if (a[i] % 2 == 0)         numeven++;       else         numodd++;     }     for (int i = 1; i <= min(M, m); i++) {       if (i % 2 == 0 && mp[i] == 0) even.push(i);       if (i % 2 == 1 && mp[i] == 0) odd.push(i);     }     int ans = 0;     for (int i = 1; i <= n; i++)       if (mp[a[i]] > 1) {         if (a[i] % 2 == 0)           numeven--;         else           numodd--;         mp[a[i]]--;         if (numeven > numodd) {           ans++;           if (odd.size() == 0) {             cout << -1 << endl;             return 0;           }           a[i] = odd.top();           odd.pop();           numodd++;         } else {           ans++;           if (even.size() == 0) {             cout << -1 << endl;             return 0;           }           a[i] = even.top();           even.pop();           numeven++;         }       }     for (int i = 1; i <= n; i++) {       if (numeven > numodd && a[i] % 2 == 0) {         ans++;         if (odd.size() == 0) {           cout << -1 << endl;           return 0;         }         a[i] = odd.top();         odd.pop();         numeven--;         numodd++;       }       if (numeven < numodd && a[i] % 2 == 1) {         ans++;         if (even.size() == 0) {           cout << -1 << endl;           return 0;         }         a[i] = even.top();         even.pop();         numeven++;         numodd--;       }     }     printf( %d n , ans);     for (int i = 1; i <= n; i++) printf( %d  , a[i]);   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int v, c, i, d;     edge(int v, int c, int i, int d) : v(v), c(c), i(i), d(d) {}   };   vector<vector<edge> > g(200005);   int n, m, d[200005], f[200005];   int main() {     cin >> n >> m;     for (int _n = m, i = 0; i < _n; ++i) {       int a, b, c;       cin >> a >> b >> c;       a--;       b--;       g[a].push_back(edge(b, c, i, 0));       g[b].push_back(edge(a, c, i, 1));       f[a] += c;       f[b] += c;     }     for (int _n = n, i = 0; i < _n; ++i) f[i] /= 2;     queue<int> Q;     Q.push(0);     memset(d, -1, sizeof(d));     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (int _n = int((g[u]).size()), i = 0; i < _n; ++i) {         int id = g[u][i].i;         if (d[id] == -1) {           d[id] = g[u][i].d;           int v = g[u][i].v;           f[v] -= g[u][i].c;           if (f[v] == 0 && v != n - 1) Q.push(v);         }       }     }     for (int _n = m, i = 0; i < _n; ++i) cout << d[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e8;   const int MX = 2e5 + 5;   vector<int> vorder;   int dup[MX], lc[MX], rc[MX], good[MX], seek[MX];   string labels, corder;   void ino(int curr) {     if (curr == -1) return;     ino(lc[curr]);     vorder.push_back(curr);     corder += labels[curr];     ino(rc[curr]);   }   void dfs1(int curr) {     if (curr == -1) return;     seek[curr] = INF;     dfs1(lc[curr]);     dfs1(rc[curr]);     if (lc[curr] != -1) seek[curr] = 1 + seek[lc[curr]];     if (good[curr]) seek[curr] = 1;   }   int dfs2(int curr, int k) {     if (curr == -1) return k;     if (seek[curr] <= k) {       k--;       dup[curr] = 1;       k = dfs2(lc[curr], k);       return dfs2(rc[curr], k);     }     return k;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     cin >> labels;     for (int i = 0; i < n; i++) {       cin >> lc[i] >> rc[i];       lc[i]--;       rc[i]--;     }     ino(0);     corder +=  @ ;     char last;     for (int i = n - 1; i >= 0; i--) {       if (corder[i] != corder[i + 1]) last = corder[i + 1];       good[vorder[i]] = corder[i] < last;     }     memset(dup, 0, sizeof dup);     dfs1(0);     dfs2(0, k);     for (int v : vorder) {       if (dup[v])         cout << labels[v] << labels[v];       else         cout << labels[v];     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n;     cin >> n;     if (n % 3) {       cout << 0;       return 0;     }     n /= 3;     vector<long long> d;     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) {         d.push_back(i);       }     }     sort(d.begin(), d.end());     long long r = 0;     for (int i = 0; i < d.size(); i++) {       for (int j = i; j < d.size(); j++) {         if (n % (d[i] * d[j]) == 0 && n / (d[i] * d[j]) >= d[j]) {           long long ab = d[i], bc = d[j], ac = n / (d[i] * d[j]);           long long a = ab + ac - bc, b = ab - ac + bc, c = -ab + ac + bc;           if (a % 2 + b % 2 + c % 2 == 0 && a / 2 > 0 && b / 2 > 0 && c / 2 > 0) {             a /= 2, b /= 2, c /= 2;             if (a == b && b == c) {               r++;             } else if (a == b || b == c || a == c) {               r += 3;             } else               r += 6;           }         }       }     }     cout << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int64_t a, b, x, y, z;     scanf( %I64d%I64d%I64d%I64d%I64d , &a, &b, &x, &y, &z);     int64_t addA, addB;     if (2LL * x + y - a > 0)       addA = 2LL * x + y - a;     else       int64_t addA = 0;     if (3LL * z + y - b > 0)       addB = 3LL * z + y - b;     else       addB = 0;     printf( %I64d n , addA + addB);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, i, j, k, l;     cin >> n >> m;     string s, t;     cin >> s >> t;     if (s == t) {       cout <<  Yes  << endl;       return 0;     }     long long int present = 0;     long long int flag = 0;     for (i = 0; i < s.length(); i++) {       if (s[i] ==  * ) {         present = 1;         break;       }     }     if (present == 0) {       cout <<  No  << endl;       return 0;     } else {       for (i = 0; i < s.length(); i++) {         if (s[i] ==  * ) {           break;         } else if (s[i] != t[i]) {           flag = 1;           break;         } else {           t[i] =  X ;         }       }     }     if (flag == 0) {       long long int ind = t.length() - 1;       for (i = s.length() - 1; i >= 0; i--) {         if (s[i] ==  * ) {           break;         }         if (s[i] != t[ind]) {           flag = 1;           break;         }         ind--;       }     }     if (flag == 0) {       cout <<  Yes  << endl;     } else {       cout <<  No  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   bool eq(long double l1, long double l2) { return abs(l1 - l2) < 1e-15; }   int main() {     const string names[] = { Vanya ,  Vova ,  Both };     vector<pair<long double, char>> v;     int n, x, y;     cin >> n >> x >> y;     for (int i = 1; i <= x; i++) {       v.push_back({(long double)i / (long double)x, 0});     }     for (int i = 1; i <= y; i++) {       v.push_back({(long double)i / (long double)y, 1});     }     sort(v.begin(), v.end(),          [](pair<long double, char> a1, pair<long double, char> a2) {            return a1.first < a2.first;          });     for (int i = 0; i < v.size() - 1; i++) {       if (eq(v[i].first, v[i + 1].first)) {         v[i].second = 2;         v[++i].second = 2;       }     }     int a;     for (int i = 0; i < n; i++) {       cin >> a;       if (a > x + y) {         a %= (x + y);         if (a == 0) a = x + y;       }       cout << names[v[a - 1].second] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long inf = 1e17;   long long v[N];   long long c[N];   long long dp[N];   bool mark[N];   long long n, q;   long long a, b;   void solver() {     cin >> a >> b;     long long max1 = 0;     long long color1 = 0;     long long max2 = 0;     long long color2 = 1;     for (long long i = 0; i < N; i++) {       dp[i] = -inf;       mark[i] = 0;     }     for (long long i = 0; i < n; i++) {       long long color = c[i];       long long value = v[i];       long long maxi = max1;       if (color1 == color) {         maxi = max2;       }       if (mark[color]) {         dp[color] = max(dp[color], dp[color] + value * a);       }       dp[color] = max(dp[color], maxi + value * b);       mark[color] = true;       if (color != color1 && color != color2) {         if (dp[color] > max1) {           max2 = max1;           color2 = color1;           max1 = dp[color];           color1 = color;           continue;         }         if (dp[color] > max2) {           max2 = dp[color];           color2 = color;         }       }       if (color == color1) {         max1 = dp[color];         color1 = color;       }       if (color == color2) {         max2 = dp[color];         if (max2 > max1) {           swap(max2, max1);           swap(color1, color2);         }       }     }     long long ans = 0;     for (long long i = 0; i < n; i++) {       ans = max(ans, dp[i]);     }     cout << ans << endl;   }   int main() {     cin >> n >> q;     for (long long i = 0; i < n; i++) {       cin >> v[i];     }     for (long long i = 0; i < n; i++) {       cin >> c[i];     }     for (long long i = 0; i < q; i++) {       solver();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.precision(10);     cout << fixed;     int n, neg = 0;     cin >> n;     vector<int> a(2 * n - 1);     bool zero = false;     long long sum = 0;     for (int i = 0; i < 2 * n - 1; ++i) {       cin >> a[i];       if (a[i] == 0)         zero = true;       else if (a[i] < 0)         neg++, a[i] *= -1;       sum += a[i];     }     neg %= n;     if (zero) neg = 0;     vector<bool> vis(2 * n);     queue<int> q;     q.push(neg);     vis[neg] = 1;     int ans = neg;     while (!q.empty()) {       int k = q.front();       q.pop();       if (k % n == 0) {         ans = 0;         break;       }       ans = min(ans, k);       for (int x = 2 * n - 1 - k; x >= n - k; --x) {         int k2 = k + 2 * x - n;         if (k2 > 0 && k2 < 2 * n && !vis[k2]) {           vis[k2] = 1;           q.push(k2);         }       }     }     sort(begin(a), end(a));     for (int i = 0; i < ans; ++i) sum -= 2 * a[i];     cout << sum <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O3 )   int t, h[2000010], v[2000010 * 2], nxt[2000010 * 2], ec, in[2000010], n, a, b;   char s[2000010];   void add(int x, int y) {     v[++ec] = y;     in[y]++;     nxt[ec] = h[x];     h[x] = ec;   }   void adj(int x, int y) {     add(x, y);     add(y, x);   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       ec = 0;       memset(h, 0, sizeof(int) * (n + 5));       memset(in, 0, sizeof(int) * (n + 5));       for (int i = 1; i < n; i++) {         scanf( %d%d , &a, &b);         adj(a, b);       }       scanf( %s , s + 1);       if (n < 3) {         printf( Draw n );         continue;       }       if (n == 3) {         int c = 0;         for (int i = 1; i <= n; i++)           if (s[i] ==  W ) c++;         if (c >= 2)           printf( White n );         else           printf( Draw n );         continue;       }       int ct = 0, r = 0, on = n;       for (int i = 1; i <= on; i++)         if (s[i] ==  W ) {           add(i, ++n);           h[n] = 0;           add(n, i);           in[n] = 3;         }       for (int i = 1; i <= n; i++) {         if (in[i] > 3)           r = 1;         else if (in[i] == 3) {           ct++;           int cc = 0;           for (int j = h[i]; j; j = nxt[j])             if (in[v[j]] >= 2) cc++;           r |= cc >= 2;         }       }       if (ct == 2 && (n & 1)) r = 1;       printf(r ?  White n  :  Draw n );     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void prn(Arg1&& arg1) {     cout << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void prn(Arg1&& arg1, Args&&... args) {     cout << arg1 <<    ;     prn(args...);   }   template <typename Arg1>   void prs(Arg1&& arg1) {     cout << arg1 <<    ;   }   template <typename Arg1, typename... Args>   void prs(Arg1&& arg1, Args&&... args) {     cout << arg1 <<    ;     prs(args...);   }   template <typename Arg1>   void read(Arg1&& arg1) {     cin >> arg1;   }   template <typename Arg1, typename... Args>   void read(Arg1&& arg1, Args&&... args) {     cin >> arg1;     read(args...);   }   long long n, k;   void solve() {     read(n, k);     long long sum = 0, invariant = 0;     for (int i = 0; i < k; ++i) {       long long a, b;       read(a, b);       sum += b;       invariant = (invariant + a * b) % n;     }     if (sum < n) {       prn(1);       return;     }     if (sum > n) {       prn(-1);       return;     }     if (invariant != (n * (n + 1) / 2) % n) {       prn(-1);       return;     }     prn(1);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     for (int tc = 1; tc <= t; ++tc) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   const int V = 1e6, M = 1e9 + 7;   struct edge {     int v, c, nxt;   } g[V];   int n, m, k, t, dep[V], q[V], l, r, d[7], head[V], dis[V], a1[V], a2[V], a3[V];   std::vector<int> h[V];   inline void Insert(int u, int v, int c) {     g[++k] = (edge){v, c, head[u]};     head[u] = k;   }   bool Cmp(const int& u, const int& v) {     return a1[u] == a1[v] ? a2[u] < a2[v] : a1[u] < a1[v];   }   int main() {     int u, v;     scanf( %d%d , &n, &m);     t = n;     for (int j = 1; j <= m; j++) {       scanf( %d%d , &u, &v);       r = 0;       for (int jj = j; jj; jj /= 10) d[++r] = jj % 10;       if (r == 1)         Insert(u, v, d[1]), Insert(v, u, d[1]);       else {         Insert(u, ++t, d[r]);         for (int jj = r - 1; jj >= 2; jj--) Insert(t, t + 1, d[jj]), ++t;         Insert(t, v, d[1]);         Insert(v, ++t, d[r]);         for (int jj = r - 1; jj >= 2; jj--) Insert(t, t + 1, d[jj]), ++t;         Insert(t, u, d[1]);       }     }     q[l = r = 0] = 1;     dep[1] = 1;     for (; l <= r;) {       u = q[l++];       h[dep[u]].push_back(u);       for (int i = head[u]; i; i = g[i].nxt)         if (!dep[v = g[i].v]) q[++r] = v, dep[v] = dep[u] + 1;     }     dis[1] = 0;     memset(a1, 1, sizeof a1);     memset(a2, 1, sizeof a2);     for (int j = 1; h[j].size(); j++) {       std::sort(h[j].begin(), h[j].end(), Cmp);       r = 0;       for (int i = 0; i < h[j].size(); i++) {         u = h[j][i];         if (!i || a1[u] != a1[h[j][i - 1]] || a2[u] != a2[h[j][i - 1]]) ++r;         a3[u] = r;         for (int i = head[u]; i; i = g[i].nxt)           if (dep[v = g[i].v] > dep[u] &&               (a3[u] < a1[v] || a3[u] == a1[v] && g[i].c < a2[v]))             a1[v] = a3[u], a2[v] = g[i].c, dis[v] = (dis[u] * 10ll + g[i].c) % M;       }     }     for (u = 2; u <= n; u++) printf( %d n , dis[u]);     return 0;   }