#include <bits/stdc++.h>   using namespace std;   const long long Base = 1e9 + 7;   struct Matrix {     int n, m;     long long cost[101][101];     Matrix() {}     Matrix(int n_, int m_) {       n = n_, m = m_;       for (int i = 0; i <= n; i++)         for (int j = 0; j <= m; j++) cost[i][j] = 0;     }     Matrix operator*(Matrix b) {       Matrix c = Matrix(n, b.m);       for (int i = 0; i <= n; i++)         for (int j = 0; j <= b.m; j++)           for (int k = 0; k <= m; k++)             c.cost[i][j] =                 (c.cost[i][j] + cost[i][k] * b.cost[k][j] % Base) % Base;       return c;     }   };   Matrix operator^(Matrix a, long long b) {     Matrix c = Matrix(a.n, a.m);     bool ok = false;     while (b > 0) {       if (b & 1) {         if (!ok)           c = a;         else           c = c * a;         ok = true;       }       a = a * a;       b >>= 1;     }     return c;   }   const long long N = 1e6 + 1;   long long n, l, m;   long long a[N], b[N], c[N], counts[N], counts_[N];   int main() {     scanf( %lld%lld%lld , &n, &l, &m);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] %= m;       counts[a[i]]++;     }     for (int i = 1; i <= n; i++) {       scanf( %lld , &b[i]);       b[i] %= m;       counts_[b[i]]++;     }     for (int i = 1; i <= n; i++) {       scanf( %lld , &c[i]);       c[i] %= m;     }     Matrix x = Matrix(m - 1, 0);     for (int i = 0; i <= m - 1; i++) x.cost[i][0] = counts[i];     if (l > 2) {       Matrix tmp = Matrix(m - 1, m - 1);       for (int i = 0; i <= m - 1; i++)         for (int j = 0; j <= m - 1; j++)           if (i < j)             tmp.cost[i][j] = counts_[i + m - j] % Base;           else             tmp.cost[i][j] = counts_[i - j] % Base;       tmp = tmp ^ (l - 2);       x = tmp * x;     }     for (int i = 1; i <= n; i++) c[i] = (c[i] + b[i]) % m;     long long ans = 0;     for (int i = 1; i <= n; i++)       if (c[i] == 0)         ans = (ans + x.cost[0][0]) % Base;       else         ans = (ans + x.cost[m - c[i]][0]) % Base;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, b;   vector<pair<long long, long long> > v;   long long mask[105], cost[105], monitors[105];   long long dp[1 << 21];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> b;     for (int i = 0; i <= 100; i++) mask[i] = (1ll << 30) - 1;     for (long long i = 0; i < n; i++) {       long long temp;       cin >> cost[i] >> monitors[i] >> temp;       for (long long j = 0; j < temp; j++) {         long long val;         cin >> val;         mask[i] -= (1ll << (val - 1));       }       v.push_back(make_pair(monitors[i], i));     }     sort(v.begin(), v.end());     for (long long i = 1; i < (1ll << 21) - 2; i++) dp[i] = 1ll << 60;     long long ans = 1ll << 60;     for (long long i = 0; i < n; i++) {       long long pt = v[i].second;       for (long long j = 1; j < (1 << m); j++) {         dp[j] = min(dp[j], dp[j & mask[pt]] + cost[pt]);       }       ans = min(ans, dp[(1 << m) - 1] + v[i].first * b);     }     if (ans < 1ll << 60)       cout << ans <<   n ;     else       cout <<  -1 n ;     return 0;   }


#include <bits/stdc++.h>   #pragma optimize( -O3 )   using namespace std;   const long long mod = 998244353;   int n, m, u, v;   pair<vector<int>, int> adj[1000006];   int alr[1000006];   long long ans = 0, cr = 0;   bool check(int i, int j) {     if ((int)adj[i].first.size() != (int)adj[j].first.size()) return false;     int x = (int)adj[i].first.size();     int in1 = 0, in2 = 0;     while (in1 < x && in2 < x) {       if (adj[i].first[in1] == adj[j].second) {         in1++;         continue;       }       if (adj[j].first[in2] == adj[i].second) {         in2++;         continue;       }       if (adj[i].first[in1] != adj[j].first[in2]) return false;       in1++;       in2++;     }     return true;   }   bool check2(int i, int j) {     if ((int)adj[i].first.size() != (int)adj[j].first.size()) return false;     int x = (int)adj[i].first.size();     int in1 = 0;     while (in1 < x) {       if (adj[i].first[in1] != adj[j].first[in1]) return false;       in1++;     }     return true;   }   void dfs(int i) {     for (auto &x : adj[i].first) {       if (x < i) continue;       if (check(i, x)) ans++;     }   }   void solve() {     cin >> n >> m;     for (long long i = (long long)0; i != (long long)m; i = i + 1) {       cin >> u >> v;       u--;       v--;       adj[u].first.push_back(v);       adj[v].first.push_back(u);     }     for (long long i = (long long)0; i != (long long)n; i = i + 1) {       adj[i].second = i;       sort(adj[i].first.begin(), adj[i].first.end());     }     for (long long i = (long long)0; i != (long long)n; i = i + 1) {       dfs(i);     };     sort(adj, adj + n);     for (long long i = (long long)0; i != (long long)n; i = i + 1)       ;     for (long long i = (long long)1; i != (long long)n; i = i + 1) {       if (check2(i, i - 1)) {         cr++;         ;         ans += 1ll * cr;       } else {         cr = 0;       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   const double eps = 1e-9;   const int inf = 2000000000;   const double pi = acos(-1.0);   const long long mod = 1000000007;   using namespace std;   char s[150];   int t;   long long d[26 * 100 + 200][130];   int main() {     scanf( %d n , &t);     memset(d, 0, sizeof(d));     for (int num = 0; num <= 120; num++)       for (int sum = 0; sum <= 26 * 100 + 100; sum++) {         if (num == 0) {           if (sum == 0)             d[sum][num] = 1;           else             break;         } else           for (int last = 0; last < 26; last++)             if (sum - last >= 0)               d[sum][num] = (d[sum][num] + d[sum - last][num - 1]) % mod;       }     for (int it = 0; it < (int)t; it++) {       gets(s);       int n = (int)strlen(s);       int sum = 0;       for (int i = 0; i < (int)n; i++) sum += s[i] -  a ;       printf( %I64d n , (d[sum][n] == 0) ? 1000000006 : (d[sum][n] - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000 * 100 + 10;   const long long INF = 1000 * 1000 * 1000LL * 1000 * 10000;   int n, q, pt = 1, cnt;   vector<long long> ans;   set<pair<long long, long long> > s;   set<pair<long long, long long> >::iterator it1, it2;   long long po[100], a[N], sum[N * 4][2];   pair<long long, long long> chng[N * 4][2], minn[N * 4][2];   void shift(int ind, bool o, int b, int mid) {     if (chng[ind][o].first) {       chng[ind * 2][o] = chng[ind * 2 + 1][o] = chng[ind][o];       minn[ind * 2][o] = make_pair(chng[ind][o].second, b);       minn[ind * 2 + 1][o] = make_pair(chng[ind][o].second, mid);       sum[ind * 2][o] = sum[ind * 2 + 1][o] = 0;       chng[ind][o] = make_pair(0, 0);     }     long long t = sum[ind][o];     sum[ind * 2][o] += t;     sum[ind * 2 + 1][o] += t;     minn[ind * 2][o].first += t;     minn[ind * 2 + 1][o].first += t;     sum[ind][o] = 0;     return;   }   void add(int l, int r, pair<long long, long long> val, bool o, int b = 0,            int e = n, int ind = 1) {     if (l <= b && e <= r) {       minn[ind][o].first += val.second;       sum[ind][o] += val.second;       return;     }     int mid = (b + e) / 2;     shift(ind, o, b, mid);     if (l < mid) add(l, r, val, o, b, mid, ind * 2);     if (r > mid) add(l, r, val, o, mid, e, ind * 2 + 1);     minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]);     return;   }   void sett(int l, int r, pair<long long, long long> val, bool o, int b = 0,             int e = n, int ind = 1) {     if (l <= b && e <= r) {       chng[ind][o] = val;       sum[ind][o] = 0;       minn[ind][o] = make_pair(val.second, b);       return;     }     int mid = (b + e) / 2;     shift(ind, o, b, mid);     if (l < mid) sett(l, r, val, o, b, mid, ind * 2);     if (r > mid) sett(l, r, val, o, mid, e, ind * 2 + 1);     minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]);   }   pair<long long, long long> getmin(int l, int r, int b = 0, int e = n,                                     int ind = 1) {     pair<long long, long long> ret = make_pair(INF, INF);     if (l <= b && e <= r) return minn[ind][0];     int mid = (b + e) / 2;     shift(ind, 0, b, mid);     if (l < mid) ret = min(ret, getmin(l, r, b, mid, ind * 2));     if (r > mid) ret = min(ret, getmin(l, r, mid, e, ind * 2 + 1));     return ret;   }   long long get(int p, bool o, int b = 0, int e = n, int ind = 1) {     if (b + 1 == e) return sum[ind][o] + chng[ind][o].second;     int mid = (b + e) / 2;     shift(ind, o, b, mid);     if (p < mid) return get(p, o, b, mid, ind * 2);     return get(p, o, mid, e, ind * 2 + 1);   }   long long type1(int x) {     x--;     return po[get(x, 1)] - get(x, 0);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> q;     po[0] = 1;     for (int i = 1; i <= 12; i++) po[i] = po[i - 1] * 42;     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i] = 1 - a[i];       sett(i, i + 1, make_pair(pt++, a[i]), 0);       s.insert(make_pair(i, i + 1));     }     long long tp, x, l, r;     for (int i = 0; i < q; i++) {       cin >> tp;       if (tp == 1) {         cin >> x;         ans.push_back(type1(x));       } else {         cin >> l >> r >> x;         l--;         if (tp == 2) {           sett(l, r, make_pair(pt++, 1 - x), 0);           sett(l, r, make_pair(pt++, 0), 1);           s.insert(make_pair(l, r));           it1 = it2 = s.find(make_pair(l, r));           if (it1 != s.begin() && (*--it1).second > l) {             pair<long long, long long> temp = *it1;             s.erase(it1);             it1 = s.find(make_pair(l, r));             if (temp.first < l) s.insert(make_pair(temp.first, l));             if (temp.second > r) s.insert(make_pair(r, temp.second));           }           it2++;           while (it2 != s.end() && (*it2).first < r) {             pair<long long, long long> temp = *it2;             s.erase(it2);             it2 = ++s.find(make_pair(l, r));             if (temp.second > r) s.insert(make_pair(r, temp.second));           }         } else {           bool flag = 1;           pair<long long, long long> t1 = *--s.upper_bound(make_pair(l, n + 1));           if (t1.first != l) {             s.erase(t1);             s.insert(make_pair(t1.first, l));             s.insert(make_pair(l, t1.second));           }           pair<long long, long long> t2 = *--s.upper_bound(make_pair(r, n + 1));           if (t2.first != r) {             s.erase(t2);             s.insert(make_pair(t2.first, r));             if (t2.second != r) s.insert(make_pair(r, t2.second));           }           while (flag) {             add(l, r, make_pair(pt++, -x), 0);             pair<long long, long long> temp = getmin(l, r);             while (temp.first < 0) {               pair<long long, long long> seg =                   *--s.upper_bound(make_pair(temp.second, n + 1));               int pnt = get(temp.second, 1);               add(seg.first, seg.second, make_pair(pt++, po[pnt + 1] - po[pnt]),                   0);               sett(seg.first, seg.second, make_pair(pt++, pnt + 1), 1);               temp = getmin(l, r);             }             if (temp.first != 0) flag = 0;           }         }       }     }     for (long long u : ans) cout << u << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   int a[1000][1000];   int main() {     int n, m, x, y;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y;       x--, y--;       for (int k = -2; k < 1; k++)         for (int l = -2; l < 1; l++)           if (x + k >= 0 && x + k < n && y + l >= 0 && y + l < n) {             a[x + k][y + l]++;             if (a[x + k][y + l] == 9) {               cout << i + 1;               return 0;             }           }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const int inf = 0x3f3f3f3f;   const int mod = 1000000007;   const int maxn = 100000 + 10;   string s, str[maxn];   int n;   long long bit[10], len[10];   bool used[10];   long long Qpow(int x, long long b) {     long long tmp = x, ret = 1;     while (b) {       if (b & 1) {         ret *= tmp;         ret %= mod;       }       tmp *= tmp;       tmp %= mod;       b >>= 1;     }     return ret;   }   void cal(string ss) {     int sz = ss.length(), x = ss[0] -  0 ;     long long tmp = 1, ans = 0;     for (int i = sz - 1; i > 2; i--) {       int now = ss[i] -  0 ;       ans += tmp * bit[now];       ans %= mod;       tmp *= len[now];       tmp %= mod;     }     bit[x] = ans;     len[x] = tmp;     used[x] = true;   }   int main() {     cin >> s >> n;     for (int i = 0; i < n; i++) cin >> str[i];     for (int i = 0; i < 10; i++) bit[i] = i, len[i] = 10, used[i] = false;     for (int i = n - 1; i >= 0; i--) cal(str[i]);     int sz = s.length();     long long ans = 0, tmp = 1;     for (int i = sz - 1; i >= 0; i--) {       int now = s[i] -  0 ;       ans += bit[now] * tmp % mod;       ans %= mod;       tmp *= len[now];       tmp %= mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k, i, j, n;   double ans, x[100010], xp, yp, xk;   double C(int z) { return hypot(x[z] - xp, yp); }   double A(int l, int r) { return x[r] - x[l] + min(C(l), C(r)); }   double B(int l, int r) {     return x[r] - x[l] + min(abs(xk - x[l]) + C(r), abs(xk - x[r]) + C(l));   }   void inlinework() {     ans = B(1, n);     for (i = 2; i <= n; i++) {       double t = min(A(1, i - 1) + B(i, n), A(i, n) + B(1, i - 1));       if (t < ans) ans = t;     }   }   void outlinework() { ans = A(1, n); }   int main() {     scanf( %d%d , &n, &k);     for (i = 1; i <= n; i++) scanf( %lf , &x[i]);     scanf( %lf%lf , &xp, &yp);     xk = x[k];     sort(x + 1, x + n + 1);     if (k <= n)       inlinework();     else       outlinework();     printf( %.20lf n , ans);   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> tri(100005);   void solve() {     long long n;     cin >> n;     for (long long i = 1; i < 100005; i++) {       tri[i] = (i * (i + 1)) / 2LL;     }     for (int i = 1; i <= 100000; i++) {       long long x = n - tri[i];       if (x <= 0) {         cout <<  NO n ;         return;       }       auto it = lower_bound(tri.begin(), tri.end(), x) - tri.begin();       if (tri[it] == x) {         cout <<  YES n ;         return;       }     }     cout <<  NO n ;     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = atan(1) * 4;   const double EXP = 1e-8;   struct node {     int x, y;     double a;   } light[30];   double cal(double pos, node l) {     double len = sqrt((l.x - pos) * (l.x - pos) + l.y * l.y);     if (pos > l.x) {       if (l.a < atan(l.y * 1.0 / (pos - l.x))) {         double ag = atan(l.y * 1.0 / (pos - l.x)) - l.a;         return sin(l.a) * len / sin(ag);       } else         return 1000000.0;     } else {       l.x += EXP;       double ag = atan(l.y * 1.0 / (l.x - pos));       return sin(l.a) * len / sin(pi - ag - l.a);     }   }   double dp[1 << 21];   int main() {     int n, l, r;     scanf( %d%d%d , &n, &l, &r);     for (int i = 0; i < n; i++) {       int a;       scanf( %d%d%d , &light[i].x, &light[i].y, &a);       light[i].x -= l;       light[i].a = a * pi / 180.0;     }     for (int i = 0; i < (1 << n); i++)       for (int j = 0; j < n; j++) {         if (i & (1 << j)) continue;         dp[i | (1 << j)] = max(dp[i | (1 << j)], dp[i] + cal(dp[i], light[j]));       }     if (dp[(1 << n) - 1] + l >= r)       printf( %.10f n , (r - l) * 1.0);     else       printf( %.10f n , dp[(1 << n) - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       vector<long long int> a(n);       vector<long long int> b(n);       for (long long int i = 0; i < n; i++) cin >> a[i];       for (long long int i = 0; i < n; i++) cin >> b[i];       long long int suma[n], sumb[n];       for (long long int i = 0; i < n; i++) {         suma[i] = sumb[i] = 0;       }       suma[0] = a[0];       sumb[0] = b[0];       for (long long int i = 1; i < n; i++) suma[i] += a[i] + suma[i - 1];       for (long long int i = 1; i < n; i++) sumb[i] += b[i] + sumb[i - 1];       long long int maxf = 1e17;       long long int minmaxf = 1e17;       for (long long int i = 0; i < n; i++) {         if (i == 0)           maxf = suma[n - 1] - suma[i];         else {           if (suma[n - 1] - suma[i] < sumb[i - 1])             maxf = sumb[i - 1];           else             maxf = suma[n - 1] - suma[i];         }         if (maxf < minmaxf) minmaxf = maxf;       }       cout << minmaxf << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[3003];   int main() {     int n, inv = 0;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0; i < n; ++i)       for (int j = i + 1; j < n; ++j)         if (a[i] > a[j]) ++inv;     if (inv % 2 == 0)       cout << fixed << setprecision(6) << 2.0 * inv << endl;     else       cout << fixed << setprecision(6) << 2.0 * inv - 1 << endl;     return 0;   }


#include <bits/stdc++.h>   template <class t>   inline void read(t &s) {     s = 0;     register int f = 1;     register char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();     s *= f;     return;   }   template <class t, class... A>   inline void read(t &x, A &...a) {     read(x);     read(a...);   }   template <class t>   inline void write(t x) {     if (x < 0) putchar( - ), x = -x;     int buf[21], top = 0;     while (x) buf[++top] = x % 10, x /= 10;     if (!top) buf[++top] = 0;     while (top) putchar(buf[top--] ^  0 );     return;   }   inline void setIn(std::string s) {     freopen(s.c_str(),  r , stdin);     return;   }   inline void setOut(std::string s) {     freopen(s.c_str(),  w , stdout);     return;   }   inline void setIO(std::string s =   ) {     setIn(s +  .in );     setOut(s +  .out );     return;   }   template <class t>   inline bool ckmin(t &x, t y) {     if (x > y) {       x = y;       return 1;     }     return 0;   }   template <class t>   inline bool ckmax(t &x, t y) {     if (x < y) {       x = y;       return 1;     }     return 0;   }   inline int lowbit(int x) { return x & (-x); }   long double f[105], g[105];   int to[105];   inline int id(int x, int y) {     if (x & 1) return (10 - x) * 10 + 11 - y;     return (10 - x) * 10 + y;   }   signed main(void) {     register int x;     for (int i = 1; i <= 10; ++i)       for (int j = 1; j <= 10; ++j) {         read(x);         if (x) to[id(i, j)] = id(i - x, j);       }     for (int i = 0; i <= 100; ++i) g[i] = 1e18;     f[100] = 0;     for (int i = 94; i <= 99; ++i) f[i] = 6;     for (int i = 93; i; --i) {       for (int j = 1; j <= 6; ++j) f[i] += std::min(f[i + j], g[i + j]) + 1;       f[i] /= 6.0;       if (to[i]) ckmin(g[i], f[to[i]]);     }     std::printf( %.99Lf n , std::min(f[1], g[1]));     return 0;   }


#include <bits/stdc++.h>      using namespace std;      typedef long long ll;      const int N = 100010;   const ll INF = 1e9 + 69;      int n, a[N];   string good;   bitset <N> mul;      void fuck (int ss, int ee) {     int st = ss, en = ee;     while (st <= ee and a[st] == 1) ++st;     while (en >= ss and a[en] == 1) --en;     ll prod = 1;     for (int i = ss; i <= ee; ++i) prod = min(INF, prod * a[i]);     if (prod > en - st + 6) {       for (int i = st + 1; i <= en; ++i) mul[i] = 1;       return;     }     vector <ll> vec;     vector <pair <int, int>> range;     for (int i = st; i <= en;) {       int j = i;        ll cur = 1, cnt = 0;       while (j <= en and a[j] != 1) {         if (j > i) mul[j] = 1;         cur = min(INF, cur * a[j++]);       }       vec.emplace_back(cur);       if (j > en) break;       int from = j;       while (j <= en and a[j] == 1) ++cnt, ++j;       range.emplace_back(from, j - 1);       vec.emplace_back(cnt); i = j;     }     if (vec.size() > 1) {       assert(vec.size() & 1);       int sz = (vec.size() - 1) / 2;       ll opt = 0; int opt_mask = -1;       for (int mask = 0; mask < 1 << sz; ++mask) {         ll cur = vec[0], tot = 0;         for (int i = 1, j = 0; i + 1 < vec.size(); i += 2, ++j) {           ll cnt = vec[i], val = vec[i + 1];           if (mask & 1 << j) tot += cur + cnt, cur = val;           else cur *= val;         }         tot += cur;          if (tot > opt) opt = tot, opt_mask = mask;       }       for (int i = 1, j = 0; i + 1 < vec.size(); i += 2, ++j) {         ll cnt = vec[i], val = vec[i + 1];         if (~opt_mask & 1 << j) {           auto [l, r] = range[j];           for (int p = l; p <= r + 1; ++p) mul[p] = 1;         }       }     }   }      int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       scanf( %d , a + i);     }     cin >> good;     sort(good.begin(), good.end());     if (good.size() == 1 or good ==  +- ) {       for (int i = 1; i <= n; ++i) {         if (i > 1) putchar(good[0]);         printf( %d , a[i]);       }       cout <<   n ;       return 0;     }     if (good ==  *- ) {       int firstZero = 1;       while (firstZero <= n and a[firstZero]) ++firstZero;       for (int i = 1; i <= n; ++i) {         if (i > 1) putchar(i == firstZero ?  -  :  * );         printf( %d , a[i]);       }       cout <<   n ;       return 0;     }     for (int i = 1; i <= n; ++i) {       if (a[i] == 0) continue;       int j = i;       while (j <= n and a[j]) ++j;       fuck(i, j - 1);       i = j - 1;     }     for (int i = 1; i <= n; ++i) {       if (i > 1) putchar(mul[i] ?  *  :  + );       printf( %d , a[i]);     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   int p[N], a[N], idx, pre[N];   void xxs() {     int kns = 0;     for (int i = 2; i <= N; i++) {       if (!a[i]) {         p[++idx] = i;         kns++;       }       for (int j = 1; j <= idx && i * p[j] <= N; j++) {         a[i * p[j]] = 1;         if (i % p[j] == 0) break;       }       pre[i] = kns;     }   }   int main() {     xxs();     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       int k = sqrt(n);       printf( %d n , pre[n] - pre[k] + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 20005;   const long long Inf = 1000000000000000000ll;   int n, m, k;   int s1, s2, f;   int a[Maxn], b[Maxn], lef[Maxn], rig[Maxn];   vector<pair<int, int> > neigh[Maxn];   long long dist1[Maxn], dist2[Maxn];   void Solve(bool wineq) {     fill(dist1, dist1 + n + 1, Inf);     fill(dist2, dist2 + n + 1, Inf);     priority_queue<pair<long long, int> > Q;     dist1[s1] = 0;     Q.push(pair<long long, int>(0, s1));     dist2[s2] = 0;     Q.push(pair<long long, int>(0, s2));     while (!Q.empty()) {       int v = Q.top().second;       long long d = -Q.top().first;       Q.pop();       if (min(dist1[v], dist2[v]) != d) continue;       if (dist1[v] < dist2[v] || wineq && dist1[v] == dist2[v])         for (int i = 0; i < neigh[v].size(); i++) {           int u = neigh[v][i].first;           long long cand = d + lef[neigh[v][i].second];           if (cand < min(dist1[u], dist2[u])) {             dist1[u] = cand;             Q.push(pair<long long, int>(-dist1[u], u));           } else             dist1[u] = min(dist1[u], cand);         }       else         for (int i = 0; i < neigh[v].size(); i++) {           int u = neigh[v][i].first;           long long cand = d + rig[neigh[v][i].second];           if (cand < min(dist1[u], dist2[u])) {             dist2[u] = cand;             Q.push(pair<long long, int>(-dist2[u], u));           } else             dist2[u] = min(dist2[u], cand);         }     }   }   int main() {     scanf( %d %d %d , &n, &m, &k);     scanf( %d %d %d , &s1, &s2, &f);     for (int i = 0; i < m; i++) {       scanf( %d %d %d , &a[i], &b[i], &lef[i]);       rig[i] = lef[i];       neigh[a[i]].push_back(pair<int, int>(b[i], i));     }     for (int i = m; i < m + k; i++) {       scanf( %d %d %d %d , &a[i], &b[i], &lef[i], &rig[i]);       neigh[a[i]].push_back(pair<int, int>(b[i], i));     }     if (s1 == s2) {       printf( DRAW n );       for (int i = m; i < m + k; i++)         printf( %d%c , lef[i], i + 1 < m + k ?     :   n );       return 0;     }     Solve(false);     if (dist1[f] < dist2[f]) {       printf( WIN n );       for (int i = m; i < m + k; i++)         printf( %d%c ,                dist1[a[i]] < dist2[a[i]] && dist1[b[i]] < dist2[b[i]] ? lef[i]                                                                       : rig[i],                i + 1 < m + k ?     :   n );       return 0;     }     Solve(true);     if (dist1[f] <= dist2[f]) {       printf( DRAW n );       for (int i = m; i < m + k; i++)         printf( %d%c ,                dist1[a[i]] <= dist2[a[i]] && dist1[b[i]] <= dist2[b[i]] ? lef[i]                                                                         : rig[i],                i + 1 < m + k ?     :   n );       return 0;     }     printf( LOSE n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, bool> mp;   pair<int, int> ara[400005];   int main() {     int n, m, x;     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &ara[i].first, &ara[i].second);       ara[i].first %= n, ara[i].second %= n;       if (ara[i].first > ara[i].second) swap(ara[i].first, ara[i].second);       ara[i + m] = ara[i], mp[ara[i]] = 1;     }     bool f = 0;     for (int i = 1; i * i <= n && !f; i++)       if (!(n % i)) {         x = i;         bool f2 = 1;         for (int j = 1; j <= m && f2; j++) {           int a = (ara[j].first + x) % n;           int b = (ara[j].second + x) % n;           if (a > b) swap(a, b);           f2 = mp[{a, b}];         }         f = f2;         x = (n / i);         if (x == n || f) continue;         f2 = 1;         for (int j = 1; j <= m && f2; j++) {           int a = (ara[j].first + x) % n;           int b = (ara[j].second + x) % n;           if (a > b) swap(a, b);           f2 = mp[{a, b}];         }         f = f2;       }     if (f)       printf( Yes n );     else       printf( No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     cin >> n;     int a[n];     int ans = 0;     int i, j, k;     for (i = 0; i < n; i++) cin >> a[i];     for (i = 1; i < n - 1; i++) {       if (((a[i] > a[i + 1]) && (a[i] > a[i - 1])) ||           ((a[i] < a[i - 1]) && (a[i] < a[i + 1])))         ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const string c[5] = { S ,  M ,  L ,  XL ,  XXL };   int n[5], m, i, j, l, r, x;   char st[7];   string s;   int main() {     for (i = 0; i < 5; i++) scanf( %d , &n[i]);     scanf( %d , &m);     for (i = 0; i < m; i++) {       scanf( %s , st);       s = st;       for (j = 0; j < 5; j++)         if (c[j] == s) {           x = j;           break;         }       if (n[x] > 0) {         puts(c[x].c_str());         n[x]--;         continue;       }       l = x - 1;       r = x + 1;       while (true) {         if (r < 5 && n[r] > 0) {           puts(c[r].c_str());           n[r]--;           break;         } else if (l >= 0 && n[l] > 0) {           puts(c[l].c_str());           n[l]--;           break;         }         l--;         r++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename A>   istream& operator>>(istream& in, vector<T, A>& p) {     for (auto& i : p) in >> i;     return in;   }   template <typename T, typename A>   ostream& operator<<(ostream& out, std::vector<T, A> const& v) {     for (auto i : v) out << i <<    ;     return out;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m, a, b;     cin >> n >> m;     vector<int> cnt(n);     int ans = n;     for (int i = 0; i < m; ++i) {       cin >> a >> b;       --a, --b;       if (a > b) swap(a, b);       if (cnt[a] == 0) ans--;       cnt[a]++;     }     int q, t;     cin >> q;     for (int i = 0; i < q; ++i) {       cin >> t;       if (t == 1) {         cin >> a >> b;         --a, --b;         if (a > b) swap(a, b);         if (cnt[a] == 0) ans--;         cnt[a]++;       } else if (t == 2) {         cin >> a >> b;         --a, --b;         if (a > b) swap(a, b);         if (cnt[a] == 1) ans++;         cnt[a]--;       } else         cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   inline void re(int &x) {     x = 0;     bool flag = false;     char c = getchar();     while (c <  0  || c >  9 ) {       flag = c ==  - ;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 1) + (x << 3) + (c ^  0 );       c = getchar();     }     if (flag) x = -x;     return;   }   long long l[100010];   int n;   char s[100010];   int main() {     long long W = 0, G = 0, mn = 5;     long long S = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %I64d , &l[i]);       l[i] *= 2;       S += l[i];     }     scanf( %s , s + 1);     for (int i = 1; i <= n; i++) {       if (s[i] ==  G ) G += l[i];       if (s[i] ==  W ) {         W += l[i];         mn = 3;       }       long long t = l[i];       long long e;       e = min(t / 2, W);       S += 2 * e;       t -= 2 * e;       W -= e;       e = min(t / 2, G);       S += 4 * e;       t -= 2 * e;       G -= e;       S += t * mn;     }     printf( %I64d n , S / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   ostream& operator<<(ostream& out, const vector<T>& v) {     out <<  [ ;     for (const T& x : v) {       out << x <<  ,  ;     }     return out <<  ] ;   }   template <typename S, typename T>   ostream& operator<<(ostream& out, const pair<S, T>& p) {     return out <<  {  << p.first <<  ,   << p.second <<  } ;   }   void solve() {     int N;     cin >> N;     string S;     cin >> S;     vector<int> A(N), min_after(N);     for (int n = N - 1; n >= 0; --n) {       A[n] = S[n] -  0 ;     }     vector<int> ans(N);     for (int k = 0; k <= 9; ++k) {       int last1 = -1;       for (int n = 0; n < N; ++n) {         if (A[n] < k) {           ans[n] = 1;           last1 = n;         } else if (A[n] > k) {           ans[n] = 2;         }       }       for (int n = 0; n < N; ++n) {         if (A[n] == k) {           if (n < last1) {             ans[n] = 2;           } else {             ans[n] = 1;           }         }       }       vector<int> test;       for (int d = 1; d <= 2; ++d) {         for (int n = 0; n < N; ++n) {           if (ans[n] == d) {             test.push_back(A[n]);           }         }       }       if (is_sorted(test.begin(), test.end())) {         for (int n = 0; n < N; ++n) {           cout << ans[n];         }         cout <<   n ;         return;       }     }     cout <<  - n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int T;     cin >> T;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = (int)1e5 + 5;   const long long mo = 998244353;   int a[mx], cnt[mx][105];   long long d[mx][105], sum[mx];   int main() {     int n, k, len;     cin >> n >> k >> len;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= k; j++)         cnt[i][j] = cnt[i - 1][j] + (a[i] == j || a[i] == -1);     }     sum[0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= k; j++) {         if (a[i] == -1 || a[i] == j) {           d[i][j] = sum[i - 1];           if (i >= len && cnt[i][j] - cnt[i - len][j] == len)             d[i][j] = ((d[i][j] - (sum[i - len] - d[i - len][j] + 2 * mo) % mo) +                        2 * mo) %                       mo;           sum[i] += d[i][j];           sum[i] %= mo;         }       }     }     cout << sum[n] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, x, n;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> t;     while (t--) {       vector<long long> v;       cin >> x;       for (long long i = 0; i < x; i++) {         cin >> n;         v.push_back(n);       }       vector<long long> tmp = v;       v[0] = 0;       long long now = 1;       while ((v[now - 1] + 1) <= v[now] && now < v.size()) {         v[now] = v[now - 1] + 1;         now++;       }       long long now2 = 1;       reverse(tmp.begin(), tmp.end());       tmp[0] = 0;       while ((tmp[now2 - 1] + 1) <= tmp[now2] && now2 < v.size()) {         tmp[now2] = tmp[now2 - 1] + 1;         now2++;       }       if (now > (x - now2)) {         cout <<  Yes               <<   n ;       } else {         cout <<  No               <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sum(int num) {     int ssum = 0;     while (num > 0) {       ssum = ssum + num % 10;       num = num / 10;     }     if (ssum == 10) {       return true;     } else {       return false;     }   }   int main() {     int test, num = 19;     cin >> test;     while (test > 0) {       if (sum(num)) {         test--;       }       num += 9;     }     cout << num - 9 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int const nmax = 1000;   int const modulo = 1000000007;   int dp[1 + nmax][1 + nmax];   int sum[1 + nmax][1 + nmax];   int main() {     int n, m, cond;     cin >> n >> m >> cond;     if (m < n) {       cout << 0;       return 0;     }     dp[0][0] = 1;     sum[0][0] = 1;     for (int val = 1; val <= m; val++) {       for (int i = 0; i <= n; i++)         for (int j = 0; j <= i; j++) {           dp[i][j] = 0;           if (0 < i) dp[i][j] += sum[i - 1][j];           if (0 < j && val != cond) dp[i][j] += sum[i][j - 1];           if (modulo <= dp[i][j]) dp[i][j] -= modulo;           if (0 < i && 0 < j) dp[i][j] += sum[i - 1][j - 1];           if (modulo <= dp[i][j]) dp[i][j] -= modulo;         }       if (val == cond)         for (int i = 0; i <= n; i++)           for (int j = 0; j <= n; j++) sum[i][j] = 0;       for (int i = 0; i <= n; i++)         for (int j = 0; j <= n; j++) {           sum[i][j] += dp[i][j];           if (modulo <= sum[i][j]) sum[i][j] -= modulo;         }     }     int result = sum[n][n];     for (int i = 1; i <= n; i++) result = 1LL * result * i % modulo;     cout << result;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   int n, q;   set<pair<int, int> > st;   set<int> unread[N];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> q;     int cnt = 0;     while (q--) {       int type, x;       cin >> type;       if (type == 1) {         cin >> x;         ++cnt;         st.insert({cnt, x});         unread[x].insert(cnt);       } else if (type == 2) {         cin >> x;         for (auto it : unread[x]) st.erase({it, x});         unread[x].clear();       } else {         cin >> x;         while (!st.empty() and st.begin()->first <= x) {           auto p = *(st.begin());           st.erase(st.begin());           unread[p.second].erase(p.first);         }       }       cout << st.size() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, i, s = 0;     cin >> n;     long long a[n];     for (i = 0; i < n; i++) {       cin >> a[i];     }     s = s + a[0];     for (i = 1; i < n; i++) {       if (a[i] != 1) {         s = s + i * (a[i] - 1) + a[i];       } else {         s = s + 1;       }     }     cout << s << endl;   }   int main() {     long long t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 100005;   const long long INF = 1000000000;   struct edge {     long long a, b, cap, flow;   };   long long n, s, t, d[MAXN], ptr[MAXN], q[MAXN];   vector<edge> e;   vector<long long> g[MAXN];   void add_edge(long long a, long long b, long long cap) {     edge e1 = {a, b, cap, 0};     edge e2 = {b, a, 0, 0};     g[a].push_back((long long)e.size());     e.push_back(e1);     g[b].push_back((long long)e.size());     e.push_back(e2);   }   bool bfs() {     long long qh = 0, qt = 0;     q[qt++] = s;     memset(d, -1, n * sizeof d[0]);     d[s] = 0;     while (qh < qt && d[t] == -1) {       long long v = q[qh++];       for (size_t i = 0; i < g[v].size(); ++i) {         long long id = g[v][i], to = e[id].b;         if (d[to] == -1 && e[id].flow < e[id].cap) {           q[qt++] = to;           d[to] = d[v] + 1;         }       }     }     return d[t] != -1;   }   long long dfs(long long v, long long flow) {     if (!flow) return 0;     if (v == t) return flow;     for (; ptr[v] < (long long)g[v].size(); ++ptr[v]) {       long long id = g[v][ptr[v]], to = e[id].b;       if (d[to] != d[v] + 1) continue;       long long pushed = dfs(to, min(flow, e[id].cap - e[id].flow));       if (pushed) {         e[id].flow += pushed;         e[id ^ 1].flow -= pushed;         return pushed;       }     }     return 0;   }   long long dinic() {     long long flow = 0;     for (;;) {       if (!bfs()) break;       memset(ptr, 0, n * sizeof ptr[0]);       while (long long pushed = dfs(s, INF)) flow += pushed;     }     return flow;   }   vector<long long> ext[5005];   long long pt[5005], club[5005], pre[5005], add[5005], lev[5005];   int main() {     long long std, m, d;     scanf( %lld %lld , &std, &m);     for (int i = 1; i < std + 1; i++) scanf( %lld , &pt[i]);     for (int i = 1; i < std + 1; i++) scanf( %lld , &club[i]);     scanf( %lld , &d);     for (int i = 0; i < d; i++) {       scanf( %lld , &add[i]);       lev[add[i]] = 1;     }     for (int i = 0; i < m; i++) add_edge(0, i + 1, 1);     for (int i = 0; i < 5000; i++) add_edge(m + 1 + i, 5000 + m, 1);     s = 0, t = 5000 + m, n = 5000 + m + 1;     vector<long long> pp;     long long start = 0;     for (int i = 1; i < std + 1; i++) {       if (!lev[i]) {         if (pt[i] > 0)           ext[pt[i]].push_back(i);         else           add_edge(club[i], m + 1 + pt[i], 1);       }     }     for (int i = d - 1; i >= 0; i--) {       while (1) {         bool ok = true;         if (!dinic()) ok = false;         if (!ok) break;         start++;         for (int j = 0; j < ext[start].size(); j++) {           long long ss = ext[start][j];           add_edge(club[ss], m + 1 + pt[ss], 1);         }       }       if (pt[add[i]] > start)         ext[pt[add[i]]].push_back(add[i]);       else         add_edge(club[add[i]], m + 1 + pt[add[i]], 1);       pp.push_back(start);     }     reverse(pp.begin(), pp.end());     for (int i = 0; i < pp.size(); i++) cout << pp[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e15;   long long l, r;   void Input() { cin >> l >> r; }   void Solve() {     if (r <= 10) {       long long Res = 0;       for (long long a = l; a <= r; ++a) {         for (long long b = l; b <= a; ++b) Res = max(Res, a % b);       }       cout << Res;     } else {       long long m = max(r / 2 + 1, l);       cout << max(r % l, r % m);     }     cout <<   n ;   }   int main() {     if (fopen( trash.inp ,  r ))       freopen( trash.inp ,  r , stdin), freopen( trash.out ,  w , stdout);     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int Test;     cin >> Test;     while (Test--) {       Input();       Solve();     }   }


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


#include <bits/stdc++.h>   using namespace std;   const int BLOCK = 316;   const int N = 100100;   vector<int> vc, g[N];   int col[N], l[N], r[N], nm[N], kol[N], ans[N], sum[N];   int tin[N], tout[N], n, q, kl[N];   void dfs(int v, int p) {     tin[v] = ((int)vc.size());     vc.push_back(v);     for (int u : g[v]) {       if (u == p) continue;       dfs(u, v);     }     tout[v] = ((int)vc.size()) - 1;   }   bool cmp(int x, int y) {     int lx = l[x] / BLOCK;     int ly = l[y] / BLOCK;     if (lx == ly) {       if (lx & 1)         return r[x] > r[y];       else         return r[x] < r[y];     } else       return lx < ly;   }   void add(int ps) {     int cur = col[vc[ps]];     kol[cur]++;     sum[kol[cur]]++;   }   void del(int ps) {     int cur = col[vc[ps]];     sum[kol[cur]]--;     kol[cur]--;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> q;     for (int i = 0; i < n; i++) cin >> col[i];     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(0, -1);     for (int i = 0; i < q; i++) {       int v;       cin >> v >> kl[i];       v--;       l[i] = tin[v];       r[i] = tout[v];       nm[i] = i;     }     sort(nm, nm + q, cmp);     int lf = 0, rt = -1;     for (int it = 0; it < q; it++) {       int i = nm[it];       while (lf < l[i]) del(lf++);       while (rt < r[i]) add(++rt);       while (lf > l[i]) add(--lf);       while (rt > r[i]) del(rt--);       ans[i] = sum[kl[i]];     }     for (int i = 0; i < q; i++) cout << ans[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f[10001];   int main() {     int a, b = -99999, c = 0;     cin >> a;     for (int i = 1; i <= a; i++) cin >> f[i];     for (int i = 1; i <= a; i++) b = max(b, f[i]);     for (int i = 1; i <= a; i++) c += (b - f[i]);     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = (int)1e9, MAXN = (int)1e3;   const long long ooll = 1000000000000000000ll;   int weight[MAXN][MAXN], length[MAXN][MAXN], ride[MAXN], cost[MAXN], n, m, first,       last;   vector<vector<int> > graph;   int temp[MAXN];   void calculate(int v) {     memset(temp, 0, sizeof temp);     set<pair<int, int> > s;     s.insert(make_pair(0, v));     temp[v] = -1;     while (!s.empty()) {       int r = s.begin()->first;       int u = s.begin()->second;       weight[v][u] = cost[v];       for (int i = 0; i < (graph[u].size()); i++) {         int node = graph[u][i];         if (r + length[u][node] <= ride[v]) {           if (!temp[node]) {             temp[node] = r + length[u][node];             s.insert(make_pair(temp[node], node));           } else if (temp[node] > r + length[u][node]) {             s.erase(s.find(make_pair(temp[node], node)));             temp[node] = r + length[u][node];             s.insert(make_pair(temp[node], node));           }         }       }       s.erase(make_pair(r, u));     }     weight[v][v] = 0;   }   long long ans[MAXN];   void dijkstra() {     set<pair<long long, int> > s;     for (int i = 0; i < (n); i++) ans[i] = ooll, s.insert(make_pair(ooll, i));     ans[first] = 0;     s.erase(make_pair(ooll, first));     s.insert(make_pair(0, first));     while (!s.empty()) {       long long r = s.begin()->first;       if (r == ooll) break;       int u = s.begin()->second;       for (int i = 0; i < (graph[u].size()); i++) {         int node = graph[u][i];         if (ans[node] > r + weight[u][node]) {           s.erase(s.find(make_pair(ans[node], node)));           ans[node] = r + weight[u][node];           s.insert(make_pair(ans[node], node));         }       }       s.erase(make_pair(r, u));     }   }   int main() {     cin >> n >> m >> first >> last;     first--, last--;     graph = vector<vector<int> >(n);     int x, y, z;     for (int i = 0; i < (m); i++) {       cin >> x >> y >> z;       x--, y--;       if (!length[x][y])         graph[x].push_back(y), graph[y].push_back(x),             length[x][y] = length[y][x] = z;       else         length[x][y] = length[y][x] = min(length[x][y], z);     }     for (int i = 0; i < (n); i++) cin >> ride[i] >> cost[i];     for (int i = 0; i < (n); i++) calculate(i);     graph = vector<vector<int> >(n);     for (int i = 0; i < (n); i++)       for (int j = 0; j < (n); j++)         if (weight[i][j]) graph[i].push_back(j);     dijkstra();     if (ans[last] == ooll)       cout <<  -1  << endl;     else       cout << ans[last] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300005;   int a[N], mx[N], ans[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     vector<pair<int, int> > vec;     for (int i = 1; i <= n; i++) {       cin >> a[i];       vec.emplace_back(a[i], i);     }     memset(mx, 0, sizeof(mx));     sort(vec.begin(), vec.end());     ans[n - 1] = vec.back().first;     while (!vec.empty()) {       int i = vec.back().second;       vec.pop_back();       int len = min(i - 1, n - i);       mx[len] = max(mx[len], a[i]);     }     for (int i = n - 1; i >= 0; i--) {       mx[i] = max(mx[i], mx[i + 1]);     }     for (int i = n - 2; i >= 0; i -= 2) {       ans[i] = mx[(n - 2 - i) >> 1];     }     for (int i = 1; i < n; i++) {       vec.emplace_back(min(a[i], a[i + 1]), i);     }     memset(mx, 0, sizeof(mx));     sort(vec.begin(), vec.end());     while (!vec.empty()) {       int i = vec.back().second;       vec.pop_back();       int len = min(i - 1, n - 1 - i);       mx[len] = max(mx[len], min(a[i], a[i + 1]));     }     for (int i = n - 1; i >= 0; i--) {       mx[i] = max(mx[i], mx[i + 1]);     }     for (int i = n - 3; i >= 0; i -= 2) {       ans[i] = mx[(n - 3 - i) >> 1];     }     for (int i = 0; i < n; i++) {       cout << ans[i] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int setBit(int N, int pos) { return N = N | (1 << pos); }   inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); }   inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); }   const long long MAX = 300005;   int n;   vector<int> graph[MAX];   struct data {     int k;     long long x;   };   vector<data> qrList[MAX];   long long ans[MAX], what[MAX], levelDel[MAX];   void dfs(int u, int l = 0, long long tot = 0, int par = -1) {     for (data &p : qrList[u]) {       tot += p.x;       if (l + p.k + 1 <= n) levelDel[l + p.k + 1] += p.x;     }     tot -= levelDel[l];     ans[u] = tot;     for (int &v : graph[u]) {       if (v == par) continue;       dfs(v, l + 1, tot, u);     }     for (data &p : qrList[u]) {       tot -= p.x;       if (l + p.k + 1 <= n) levelDel[l + p.k + 1] -= p.x;     }   }   int main() {     int i, u, v, q, k;     long long x;     scanf( %d , &n);     for (i = 0; i < n - 1; i++) {       scanf( %d %d , &u, &v);       u--, v--;       graph[u].push_back(v);       graph[v].push_back(u);     }     scanf( %d , &q);     while (q--) {       scanf( %d %d , &u, &k);       scanf( %lld , &x);       u--;       qrList[u].push_back({k, x});     }     dfs(0);     for (i = 0; i < n; i++) printf( %lld  , ans[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5005;   int id[maxn][maxn];   char s[maxn][maxn];   char c[maxn];   int l[maxn], r[maxn], u[maxn], d[maxn];   int n, m, t;   void init() {     int i, j;     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         l[id[i][j]] = id[i][j - 1];         r[id[i][j]] = id[i][j + 1];         u[id[i][j]] = id[i - 1][j];         d[id[i][j]] = id[i + 1][j];       }     }     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         if (s[i][j] ==  . ) {           r[l[id[i][j]]] = r[id[i][j]];           l[r[id[i][j]]] = l[id[i][j]];           u[d[id[i][j]]] = u[id[i][j]];           d[u[id[i][j]]] = d[id[i][j]];         }       }     }   }   void del(int k) {     r[l[k]] = r[k];     l[r[k]] = l[k];     u[d[k]] = u[k];     d[u[k]] = d[k];   }   int dfs(int cur) {     if (cur == 0) return 0;     del(cur);     if (c[cur] ==  U ) return dfs(u[cur]) + 1;     if (c[cur] ==  D ) return dfs(d[cur]) + 1;     if (c[cur] ==  L ) return dfs(l[cur]) + 1;     if (c[cur] ==  R ) return dfs(r[cur]) + 1;     return 0;   }   void work() {     int i, j;     int ans, max = 0, num;     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         if (s[i][j] !=  . ) {           init();           ans = dfs(id[i][j]);           if (ans > max) {             max = ans;             num = 1;           } else if (ans == max)             num++;         }       }     }     printf( %d %d n , max, num);   }   int main() {     int i, j;     while (scanf( %d%d , &n, &m) != EOF) {       for (i = 1; i <= n; i++) scanf( %s , &s[i][1]);       t = 0;       memset(id, 0, sizeof(id));       for (i = 1; i <= n; i++) {         for (j = 1; j <= m; j++) {           t++;           id[i][j] = t;           c[t] = s[i][j];         }       }       work();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void printv(vector<int> v) {     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;     cout << endl;   }   unsigned long long summ(unsigned long long n) { return ((n * n) + n) / 2; }   bool isubseq(string s, string s1) {     int ln = s1.length();     int lll = s.length();     int i = 0, j = 0;     for (; i < ln && j < lll; i++) j += (s1[i] == s[j]);     return j == lll;   }   bool incirc(double x, double y, double center_x, double center_y, double r) {     return (x - center_x) * (x - center_x) + (y - center_y) * (y - center_y) <=            r * r;   }   unsigned long long gcd(unsigned long long a, unsigned long long b) {     unsigned long long c;     while (a != 0) {       c = a;       a = b % a;       b = c;     }     return b;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int a, b, different = 0, same = 0;     cin >> a >> b;     different = (a < b) ? a : b;     same = abs(a - b) / 2;     cout << different <<     << same << endl;     return 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar();     bool flag = false;     while (!isdigit(c)) {       if (c ==  - ) flag = true;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     if (flag) x = -x;   }   int n, m, q;   int dis[5010];   bool vis[5010];   vector<pair<int, int> > ve;   struct edge {     int to, nxt, v, c;     edge(int x = 0, int y = 0, int z = 0, int w = 0) {       to = x, nxt = y, v = z, c = w;     }   } e[5010];   int head[5010], edge_cnt = 1;   void add(int from, int to, int val, int cost) {     e[++edge_cnt] = edge(to, head[from], val, cost), head[from] = edge_cnt;     e[++edge_cnt] = edge(from, head[to], 0, -cost), head[to] = edge_cnt;   }   bool spfa() {     for (int i = 1; i <= n; ++i) vis[i] = false, dis[i] = 1000000000;     queue<int> q;     q.push(1), vis[1] = true, dis[1] = 0;     while (!q.empty()) {       int x = q.front();       q.pop(), vis[x] = false;       for (int i = head[x]; i; i = e[i].nxt) {         int y = e[i].to, c = e[i].c;         if (dis[y] <= dis[x] + c || !e[i].v) continue;         dis[y] = dis[x] + c;         if (!vis[y]) q.push(y), vis[y] = true;       }     }     return dis[n] != 1000000000;   }   int dfs(int x, int lim) {     if (x == n) return lim;     vis[x] = true;     int res = lim, flow;     for (int i = head[x]; i; i = e[i].nxt) {       int y = e[i].to, v = e[i].v;       if (dis[y] != dis[x] + e[i].c || vis[y] || !v) continue;       if (flow = dfs(y, min(res, v))) {         res -= flow, e[i].v -= flow, e[i ^ 1].v += flow;         if (!res) break;       }     }     return lim - res;   }   void dinic() {     int flow, v = 0, s = 0;     while (spfa())       while (flow = dfs(1, 1000000000))         v += flow, s += flow * dis[n], ve.push_back(make_pair(v, s));   }   int main() {     read(n), read(m);     for (int i = 1; i <= m; ++i) {       int x, y, v;       read(x), read(y), read(v), add(x, y, 1, v);     }     dinic(), read(q);     while (q--) {       int x;       double v = 1000000000;       read(x);       for (int i = 0; i < ve.size(); ++i)         v = min(v, (double)(x + ve[i].second) / ve[i].first);       printf( %.10lf n , v);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double ESP = 1e-9;   const int N = 2e5 + 1;   int arr[N], n;   bool canCover(double d) {     int used = 0;     double lastcoverd = 0.0;     for (int i = 0; i < n; ++i) {       if (arr[i] > lastcoverd) {         lastcoverd = arr[i] + (2 * d);         ++used;       }     }     return used <= 3;   }   double bs() {     double low = 0, high = 2e9 + 1, mid;     while (high - low > ESP) {       mid = low + (high - low) / 2;       if (canCover(mid)) {         high = mid;       } else {         low = mid;       }     }     return high;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , arr + i);     }     sort(arr, arr + n);     double d = bs();     printf( %.7f n , d);     int used = 0;     double lastcoverd = 0.0;     for (int i = 0; i < n; ++i) {       if (arr[i] - lastcoverd > ESP) {         lastcoverd = arr[i] + (2 * d);         printf( %.7f  , arr[i] + d);         used++;       }     }     while (used < 3) {       printf( %.7f  , 0.0);       ++used;     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 200005;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     vector<long long> ans(n);     sort(v.begin(), v.end());     long long idx = 0;     for (long long i = 1; i < n; i += 2) {       ans[i] = v[idx++];     }     for (long long i = 0; i < n; i += 2) {       ans[i] = v[idx++];     }     long long x = 0;     for (long long i = 1; i < n - 1; i++) {       if (ans[i] < ans[i - 1] and ans[i] < ans[i + 1]) x++;     }     cout << x <<   n ;     for (auto i : ans) {       cout << i <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mn = 200005;   const int mod = 1e9 + 7;   int n, m;   unordered_set<int> h[mn];   priority_queue<pair<int, int> > q;   int sum = 0;   int maxi, mini;   vector<tuple<int, int, int> > ans;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       int u;       scanf( %d , &u);       for (int j = 1; j <= u; j++) {         int v;         scanf( %d , &v);         h[i].insert(v);       }       sum += u;     }     mini = sum / n;     maxi = mini + (sum % n != 0);     for (int i = 1; i <= n; i++) {       if ((int)h[i].size() >= maxi) {         q.push({(int)h[i].size(), i});       }     }     for (int i = 1; i <= n; i++) {       while ((int)h[i].size() < mini) {         pair<int, int> u = q.top();         q.pop();         for (auto it = h[u.second].begin(); it != h[u.second].end(); it++) {           if (!h[i].count(*it)) {             h[i].insert(*it);             ans.push_back({u.second, i, *it});             h[u.second].erase(it++);             if ((int)h[i].size() == mini || (int)h[u.second].size() <= maxi)               break;           }         }         if ((int)h[u.second].size() >= maxi)           q.push({(int)h[u.second].size(), u.second});       }     }     for (int i = 1; i <= n; i++) {       if (q.top().first == maxi) break;       while ((int)h[i].size() == mini) {         pair<int, int> u = q.top();         q.pop();         for (auto it = h[u.second].begin(); it != h[u.second].end(); it++) {           if (!h[i].count(*it)) {             h[i].insert(*it);             ans.push_back({u.second, i, *it});             h[u.second].erase(it++);             break;           }         }         if ((int)h[u.second].size() >= maxi)           q.push({(int)h[u.second].size(), u.second});       }     }     printf( %d n , (int)ans.size());     for (auto &i : ans) {       printf( %d %d %d n , get<0>(i), get<1>(i), get<2>(i));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     register int X = 0;     register char ch = 0;     while (ch < 48 || ch > 57) ch = getchar();     while (ch >= 48 && ch <= 57) X = X * 10 + (ch ^ 48), ch = getchar();     return X;   }   int n, m;   struct Point {     int l, r, x, y;   } b[600500];   int tn;   map<pair<int, int>, int> mmp;   int f[600500], xc[600500], yc[600500];   struct Data {     int x, y;   } stk[600500];   int top;   long long ans;   int findf(int x) { return f[x] == x ? x : findf(f[x]); }   void marge(int x, int y) {     x = findf(x);     y = findf(y);     if (x == y) return;     ans -= 1ll * xc[x] * yc[x] + 1ll * xc[y] * yc[y];     if (xc[x] + yc[x] > xc[y] + yc[y]) swap(x, y);     stk[++top] = (Data){x, y};     f[x] = y;     xc[y] += xc[x];     yc[y] += yc[x];     ans += 1ll * xc[y] * yc[y];   }   void undo(int x, int y) {     f[x] = x;     xc[y] -= xc[x];     yc[y] -= yc[x];   }   vector<int> s[600500 << 1];   void solve(int l, int r, int num) {     long long sav = ans;     int tim = top;     Point *v;     for (int i = 0; i < s[num].size(); i++) {       v = &b[s[num][i]];       if (v->l <= l && r <= v->r) marge(v->x, v->y);     }     if (l == r)       printf( %lld  , ans);     else {       int mid = (l + r) >> 1;       for (int i = 0; i < s[num].size(); i++) {         v = &b[s[num][i]];         if (!(v->l <= l && r <= v->r)) {           if (v->l <= mid) s[num << 1].push_back(s[num][i]);           if (v->r > mid) s[num << 1 | 1].push_back(s[num][i]);         }       }       s[num].clear();       solve(l, mid, num << 1);       solve(mid + 1, r, num << 1 | 1);     }     while (tim < top) {       undo(stk[top].x, stk[top].y);       top--;     }     ans = sav;   }   int main() {     m = read();     n = 300005;     pair<int, int> sav;     for (int i = 1, x, y; i <= m; i++) {       x = read();       y = read() + n;       sav = make_pair(x, y);       if (mmp[sav]) {         b[mmp[sav]].r = i - 1;         mmp[sav] = 0;       } else         b[mmp[sav] = ++tn] = (Point){i, 0, x, y};     }     for (int i = 1; i <= tn; i++) {       if (b[i].r == 0) b[i].r = m;       s[1].push_back(i);     }     for (int i = 1; i <= n + n; i++) {       f[i] = i;       xc[i] = (i <= n);       yc[i] = !xc[i];     }     solve(1, m, 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long orz = 1e9 + 7;   char s[500100];   vector<long long> xc;   vector<long long> yc;   vector<long long> tempx;   vector<long long> tempy;   int n, h, w, len, x, y, ly, uy, lx, rx, i, nx, ny, xs, ys, flag;   long long ans, temp;   int main() {     scanf( %d%d%d , &n, &h, &w);     scanf( %s , s);     len = strlen(s);     x = y = ly = uy = lx = rx = 0;     for (i = 0; i < len; i++) {       if (s[i] ==  L ) {         x -= 1;         if (x < lx) lx = x, xc.push_back(i);       } else if (s[i] ==  U ) {         y -= 1;         if (y < uy) uy = y, yc.push_back(i);       } else if (s[i] ==  D ) {         y += 1;         if (y > ly) ly = y, yc.push_back(i);       } else if (s[i] ==  R ) {         x += 1;         if (x > rx) rx = x, xc.push_back(i);       }     }     for (i = 0; i < len; i++) {       if (s[i] ==  L ) {         x -= 1;         if (x < lx) lx = x, tempx.push_back(i);       } else if (s[i] ==  U ) {         y -= 1;         if (y < uy) uy = y, tempy.push_back(i);       } else if (s[i] ==  D ) {         y += 1;         if (y > ly) ly = y, tempy.push_back(i);       } else if (s[i] ==  R ) {         x += 1;         if (x > rx) rx = x, tempx.push_back(i);       }     }     if (tempx.size() != 0)       while (xc.size() < w)         for (i = 0; i < tempx.size(); i++) {           tempx[i] += n;           xc.push_back(tempx[i]);         }     for (i = 0; i < xc.size(); i++) xc[i] += 1;     if (tempy.size() != 0)       while (yc.size() < h)         for (i = 0; i < tempy.size(); i++) {           tempy[i] += n;           yc.push_back(tempy[i]);         }     for (i = 0; i < yc.size(); i++) yc[i] += 1;     xs = xc.size(), ys = yc.size(), nx = ny = 0;     while (nx < xs || ny < ys) {       if (h == 0 || w == 0) break;       flag = (ys == ny || xs > nx && xc[nx] < yc[ny]);       if (flag) {         temp = (h % orz) * (xc[nx] % orz);         ans += temp;         ans %= orz;         nx += 1, w -= 1;       } else {         temp = (w % orz) * (yc[ny] % orz);         ans += temp;         ans %= orz;         ny += 1, h -= 1;       }     }     if (h > 0 && w > 0)       puts( -1 );     else       printf( %I64d n , ans);   }


#include <bits/stdc++.h>      using namespace std;      const int OO = 1e9;   const double EPS = 1e-9;      #define ndl cout <<   n    #define sz(v) int(v.size())   #define pb push_back   #define mp make_pair   #define fs first   #define sc second   #define present(a, x) (a.find(x) != a.end())   #ifdef LOCAL   #define db(...) ({cout <<  > Line   << __LINE__       <<  :  ; _db(#__VA_ARGS__, __VA_ARGS__);})   #else   #define db(...) true   #endif   template<class T>   void _db(const char *dbStr, T e) {    cout << dbStr <<   =   << e << endl;   }   template<class T, class... L>   void _db(const char *dbStr, T e, L... r) {    while(*dbStr !=  , ) cout << *dbStr++;    cout <<   =   << e <<  , ; _db(dbStr + 1, r...);   }   template<class S, class T>   ostream& operator<<(ostream& o, const map<S, T>& v) {    o <<  [ ; int i = 0;    for (const pair<S, T>& pr : v) o << (!i++ ?    :  ,  ) <<  {                                      << pr.fs <<   :   << pr.sc <<  } ; return o <<  ] ;   }   template<template <class, class...> class S, class T, class... L>   ostream& operator<<(ostream& o, const S<T, L...>& v) {    o <<  [ ; int i = 0;    for (const auto& e : v) o << (!i++ ?    :  ,  ) << e;    return o <<  ] ;   }   template<class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& pr) {    return o <<  (  << pr.fs <<  ,   << pr.sc <<  ) ;   }   ostream& operator<<(ostream& o, const string& s) {    for (const char& c : s) o << c; return o;   }   template<class T> using V = vector<T>;   template<class T> using VV = V<V<T>>;   template<class T> using VVV = VV<V<T>>;   using ll = long long;   using pii = pair<int, int>;   using vi = V<int>;   using vii = V<pii>;   using vvi = VV<int>;   using mii = map<int, int>;   using umii = unordered_map<int, int>;   using si = set<int>;   using usi = unordered_set<int>;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());      const int MX = 2e5 + 5, MOD = 998244353;   int fact[MX], factInv[MX];      ll add(ll x, ll y) {    x += y;    while(x >= MOD)     x -= MOD;    while(x < 0)     x += MOD;    return x;   }      ll mul(ll x, ll y) {    return (x * y) % MOD;   }      ll inv(ll a, ll b) {    return 1 < a ? b - inv(b % a, a) * b / a : 1;   }      ll nCk(ll n, ll k) {    if (k > n)     return 0;    ll ret = fact[n];    return mul(ret, mul(factInv[k], factInv[n - k]));   }      int main() {   #ifdef LOCAL    auto stTime = clock();    freopen( ../in.txt ,  r , stdin);   // freopen( ../out.txt ,  wb , stdout);   #endif    ios::sync_with_stdio(false);    cout.precision(10);    cin.tie(0);       fact[0] = 1, factInv[0] = inv(fact[0], MOD);       for (int i = 1; i < MX; ++i) {     fact[i] = mul(i, fact[i - 1]);     factInv[i] = inv(fact[i], MOD);    }       int T;       cin >> T;       for (int tc = 1; tc <= T; ++tc) {     int n;     cin >> n;     string s;     cin >> s;     if (n == 1) {      cout <<  1 n ;      continue;     }     int ones = 0, odds = 0;     if (s.back() ==  1  && s[n - 2] ==  0 )      s.pop_back();     if (s[0] ==  1  && s[1] ==  0 )      s = s.substr(1);     n = sz(s);     for (int i = 0; i < n; ++i) {      if (s[i] ==  1 ) {       int len = 0;       int j;       for (j = i; j < n; ++j) {        ++len;        if (j + 1 == n || s[j + 1] ==  0 ) {         break;        }       }       i = j;       ones += len;       if (len & 1)        ++odds;      }     }     db(s, ones, odds);     int balls = (ones - odds) / 2, bins = (n - ones) + 1;     cout << nCk(balls + bins - 1, bins - 1) <<   n ;    }         #ifdef LOCAL    cout <<   n n nExecution time:   <<         (clock() - stTime) * 1e3 / CLOCKS_PER_SEC <<   ms  << endl;   #endif    return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long Hash(char* s, int sz) {     const int p = 31;     long long hash = 0, p_pow = 1;     for (size_t i = 0; i < sz; ++i) {       hash += (s[i] -  a  + 1) * p_pow;       p_pow *= p;     }     return hash;   }   int gcd(int a, int b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   vector<int> fac;   void Factor(int N) {     int n = N;     for (int i = 2; i * i <= N; i++) {       if (n % i == 0) {         fac.push_back(i);         n /= i;         i--;       }     }     if (n > 1) fac.push_back(n);     sort(fac.begin(), fac.end());     fac.push_back(1);   }   int main() {     int n;     scanf( %d , &n);     fac.clear();     Factor(n);     int qur = 1;     long long ans = 0;     int k = 0;     while (fac.size() != k) {       ans += n / qur;       qur *= fac[k];       k++;     }     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ans[5432][5432], p[5432], b[5432], p_true[5432], b_true[5432];   int main() {     int n;     scanf( %d , &n);     int x;     for (int i = 0; i < n; i++) {       printf( ? %d %d n , i, 0);       fflush(stdout);       scanf( %d , &x);       ans[i][0] = x;     }     for (int i = 1; i < n; i++) {       printf( ? %d %d n , 0, i);       fflush(stdout);       scanf( %d , &x);       ;       ans[0][i] = x;     }     int res = 0;     for (int i = 0; i < n; i++) {       memset(p, -1, sizeof(p));       memset(b, -1, sizeof(b));       p[0] = i;       for (int j = 0; j < n; j++) {         b[j] = (ans[0][j] ^ p[0]);       }       for (int j = 1; j < n; j++) {         p[j] = (ans[j][0] ^ b[0]);       }       int flag = 0;       for (int j = 0; j < n; j++) {         if (p[b[j]] != j || p[j] >= n || b[j] >= n) {           flag = 1;           break;         }       }       if (!flag) {         res++;         for (int j = 0; j < n; j++) {           p_true[j] = p[j];           b_true[j] = b[j];         }       }     }     printf( ! n );     printf( %d n , res);     for (int i = 0; i < n; i++) {       printf( %d  , p_true[i]);     }     printf(  n );     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t, n;     cin >> t;     while (t--) {       cin >> n;       long long int a[n];       for (int i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       if (a[0] == a[n - 1])         cout << n << endl;       else         cout << 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   vector<int> ed[N], leaves;   int n, root = -1;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       ed[u].push_back(v);       ed[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       if (ed[i].size() > 2) {         if (root != -1) {           cout <<  No n ;           return 0;         }         root = i;       }       if (ed[i].size() == 1) leaves.push_back(i);     }     if (root == -1)       cout <<  Yes n             <<  1 n             << leaves[0] <<     << leaves.back() <<   n ;     else {       cout <<  Yes n ;       cout << leaves.size() <<   n ;       for (auto i : leaves) cout << root <<     << i <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<pair<long long, long long> > f(n);     for (int i = 0; i < n; ++i) cin >> f[i].first >> f[i].second;     sort(f.begin(), f.end());     vector<long long> v(n);     v[n - 1] = f[n - 1].second;     for (int i = n - 2; i >= 0; --i) {       v[i] = v[i + 1];       long long mn = 0;       for (int j = i + 2; j < n; ++j) {         mn += f[j - 1].first - f[i].first;         if (mn + v[j] < v[i]) v[i] = mn + v[j];       }       if (mn + f[n - 1].first - f[i].first < v[i])         v[i] = mn + f[n - 1].first - f[i].first;       v[i] += f[i].second;     }     cout << v[0];   }


#include <bits/stdc++.h>   using namespace std;   bool isOk(string s) {     for (long long i = 0; i + 1 < s.size(); i++) {       if (s[i] ==  1  && s[i + 1] ==  1 ) {         return false;       }     }     return true;   }   int main() {     long long n;     cin >> n;     string s;     cin >> s;     if (!isOk(s)) {       puts( No );       return 0;     }     for (long long i = 0; i < n; i++) {       string tmp = s;       if (tmp[i] ==  0 ) {         tmp[i] =  1 ;         if (isOk(tmp)) {           puts( No );           return 0;         }       }     }     puts( Yes );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N, M, a, b, c, t[100007], C, D, S, nn, ans;   char s[100007];   int main() {     scanf( %I64d%I64d%I64d , &N, &C, &D);     for (long long i = 1; i <= N; i++) {       scanf( %d , &t[i]);       scanf( %c%c , &s[i], &s[i]);     }     scanf( %d , &t[N + 1]);     ans = N * D;     S = ans;     for (long long i = N; i >= 1; i--) {       long long l = i, r = i + 1;       while (l > 1 && s[l - 1] == s[l]) l--;       a = min(i - 1, nn);       while (i > l && (t[r] - t[i]) * C < D) {         S = S - D + (t[r] - t[i]) * C;         ans = min(ans, S);         i--;       }       S = S - D + (t[r] - t[l]) * C;       ans = min(ans, S);       i = l;     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char str[105];     int n, d;     while (~scanf( %d%d , &n, &d)) {       int sum = 0, flag = 0;       int maxn = 0;       for (int i = 0; i < d; i++) {         scanf( %s , str);         for (int i = 0; i < strlen(str); i++) {           if (str[i] ==  0 ) {             flag = 1;             break;           }         }         if (flag)           sum++;         else           sum = 0;         maxn = max(sum, maxn);         flag = 0;       }       printf( %d n , maxn);     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int a[1001] = {0}, t, x;     bool flag = false;     if (n % 2 == 0)       x = n / 2;     else       x = n / 2 + 1;     for (int i = 0; i < n; i++) {       scanf( %d , &t);       a[t]++;       if (a[t] > x) flag = true;     }     if (flag)       printf( NO );     else       printf( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GCD(long long a, long long b) {     if (b == 0) return a;     return (a % b == 0 ? b : GCD(b, a % b));   }   long long POW(long long base, long long exp) {     long long val;     val = 1;     while (exp > 0) {       if (exp % 2 == 1) {         val = (val * base) % 1000000007;       }       base = (base * base) % 1000000007;       exp = exp / 2;     }     return val;   }   int main() {     int n, m, ans;     scanf( %d%d , &n, &m);     if (n == 1) {       ans = 1;     } else {       if ((m - 1) >= (n - m)) {         ans = m - 1;       } else {         ans = m + 1;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, fa, h[N];   vector<int> G[N], p, q;   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n - 1); ++i) scanf( %d , &fa), G[fa].push_back(i);     function<void(int)> dfs = [&](int u) {       for (int v : G[u]) dfs(v), h[u] = max(h[u], h[v] + 1);     };     dfs(0);     function<int(int, int)> solve = [&](int u, int nd) {       sort(G[u].begin(), G[u].end(), [&](int x, int y) { return h[x] < h[y]; });       p.push_back(u);       for (int i = (1); i <= (nd); ++i) q.push_back(u);       nd = 0;       for (int v : G[u]) nd = solve(v, nd);       return nd + 1;     };     solve(0, 0);     for (int x : p) printf( %d  , x);     printf(  n%d n , q.size());     for (int x : q) printf( %d  , x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long d[3];       cin >> d[0] >> d[1] >> d[2];       sort(d, d + 3);       if (d[2] >= d[0] + d[1])         cout << d[0] + d[1] << endl;       else {         cout << d[2] + (d[1] + d[0] - d[2]) / 2 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> orderA[500005];   pair<long long, long long> orderB[500005];   void copyArray(int mBegin, int mEnd) {     for (int i = mBegin; i < mEnd; i++) {       orderA[i] = orderB[i];     }   }   void doMerge(int mBegin, int mMid, int mEnd) {     int i = mBegin, j = mMid;     for (int k = mBegin; k < mEnd; k++) {       if (i < mMid && (j >= mEnd || orderA[i].second <= orderA[j].second)) {         orderB[k] = orderA[i];         i++;       } else {         orderB[k] = orderA[j];         j++;       }     }   }   void splitMerge(int mBegin, int mEnd) {     if (mEnd - mBegin < 2) return;     int mMid = (mEnd + mBegin) / 2;     splitMerge(mBegin, mMid);     splitMerge(mMid, mEnd);     doMerge(mBegin, mMid, mEnd);     copyArray(mBegin, mEnd);   }   void mergeSort(int n) { splitMerge(0, n); }   int main() {     int N;     scanf( %d , &N);     for (int i = 0; i < N; i++) {       long long x, y;       scanf( %I64d %I64d , &x, &y);       orderA[i] = make_pair(x, y);     }     mergeSort(N);     long long current = 0;     int ans = 0;     for (int i = 0; i < N; i++) {       if (current < orderA[i].first) {         current = orderA[i].second;         ans++;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, h[260], f = 1, c, g, i;   string s;   int main() {     cin >> s >> n;     if (n > s.length()) return cout <<  impossible , 0;     for (i = 0; i < s.length(); i++) h[s[i]]++;     for (i = 97; i <= 122; i++)       if (h[i] > 0) g++;     if (g < n)       c = n - g;     else       f = 1;   l:     if (f == 1)       cout << c;     else       cout <<  impossible ;   }


#include <bits/stdc++.h>   using namespace std;   double EPS = 1e-11;   double a[4];   double p[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};   bool poss(double thr) {     double min03 = numeric_limits<double>::infinity();     double max03 = -numeric_limits<double>::infinity();     double min12 = numeric_limits<double>::infinity();     double max12 = -numeric_limits<double>::infinity();     for (int i = 0; i < 4; i++) {       min03 = min(min03, (a[0] + thr * p[i][0]) * (a[3] + thr * p[i][1]));       min12 = min(min12, (a[1] + thr * p[i][0]) * (a[2] + thr * p[i][1]));       max03 = max(max03, (a[0] + thr * p[i][0]) * (a[3] + thr * p[i][1]));       max12 = max(max12, (a[1] + thr * p[i][0]) * (a[2] + thr * p[i][1]));     }     return !(max03 < min12 || max12 < min03);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     for (int i = 0; i < 4; i++) cin >> a[i];     double lo = 0, hi = 1e9;     for (int i = 0; i < 10000; i++) {       const double mid = (lo + hi) / 2;       if (poss(mid))         hi = mid;       else         lo = mid;     }     cout << setprecision(20) << lo << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 5;   int vecinos[MAXN];   int prof[MAXN];   int n;   set<int> arbol[MAXN];   void dfs(int i, int p, int profund) {     prof[i] = profund;     for (int h : arbol[i]) {       if (h == p) continue;       dfs(h, i, profund + 1);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     int raiz;     for (int i = (0); i < (n); i++) {       int p;       cin >> p;       p--;       if (p != -1) {         arbol[p].insert(i);         vecinos[p]++;         arbol[i].insert(p);         vecinos[i]++;       } else {         raiz = i;       }     }     dfs(raiz, -1, 0);     set<pair<int, int> > q;     for (int i = (0); i < (n); i++) {       if (vecinos[i] % 2 == 0) {         q.insert({-prof[i], i});       }     }     vector<int> eliminados;     while (!q.empty()) {       auto actual = *q.begin();       q.erase(q.begin());       int act = actual.second;       if (vecinos[act] % 2) continue;       for (int h : arbol[act]) {         vecinos[h]--;         arbol[h].erase(arbol[h].find(act));         if (vecinos[h] % 2 == 0) {           q.insert({-prof[h], h});         }       }       arbol[act].clear();       eliminados.push_back(act);     }     if ((int)(eliminados).size() != n) {       cout <<  NO n ;     } else {       cout <<  YES n ;       for (int e : eliminados) {         cout << e + 1 <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int64_t t, a, b;     cin >> t;     while (t--) {       cin >> a >> b;       cout << max(a, max(b, min(a, b) * 2)) * max(a, max(b, min(a, b) * 2))            << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100, LG = 19;   struct edge {     int u, v, w, c, i, in;     bool operator<(const edge &b) const { return w < b.w; }   } arr[N];   vector<pair<int, int> > adj[N];   int ST[N][LG], mx[N][LG], in[N], out[N];   bool isAncestor(int u, int v) { return in[v] >= in[u] && out[v] <= out[u]; }   void go(int &ret, int i) {     if (ret == -1 || arr[ret].w < arr[i].w) ret = i;   }   int solve(int u, int v) {     int ret = -1;     for (int j = LG - 1; j >= 0; j--) {       if (ST[u][j] && !isAncestor(ST[u][j], v)) {         go(ret, mx[u][j]);         u = ST[u][j];       }     }     for (int j = LG - 1; j >= 0; j--) {       if (ST[v][j] && !isAncestor(ST[v][j], u)) {         go(ret, mx[v][j]);         v = ST[v][j];       }     }     if (!isAncestor(u, v)) go(ret, mx[u][0]);     if (!isAncestor(v, u)) go(ret, mx[v][0]);     return ret;   }   int t;   void dfs(int cur, int from = 0) {     in[cur] = t++;     ST[cur][0] = from;     for (int j = 1; j < LG; ++j) {       ST[cur][j] = ST[ST[cur][j - 1]][j - 1];       mx[cur][j] = mx[cur][j - 1];       int to = ST[cur][j - 1];       if (to) go(mx[cur][j], mx[to][j - 1]);     }     for (int i = 0; i < adj[cur].size(); ++i)       if (adj[cur][i].first != from) {         mx[adj[cur][i].first][0] = adj[cur][i].second;         dfs(adj[cur][i].first, cur);       }     out[cur] = t++;   }   int p[N];   int findSet(int i) { return (i == p[i] ? i : p[i] = findSet(p[i])); }   int n, m, s;   int main() {     scanf( %d , &n), scanf( %d , &m);     for (int i = 0; i < m; ++i) scanf( %d , &arr[i].w);     for (int i = 0; i < m; ++i) scanf( %d , &arr[i].c);     for (int i = 0; i < m; ++i) {       scanf( %d , &arr[i].u);       scanf( %d , &arr[i].v);       arr[i].i = i + 1;       arr[i].in = 0;     }     scanf( %d , &s);     for (int i = 1; i <= n; i++) p[i] = i;     sort(arr, arr + m);     long long sum = 0;     for (int i = 0; i < m; ++i) {       int ru = findSet(arr[i].u);       int rv = findSet(arr[i].v);       if (ru == rv) continue;       arr[i].in = 1;       p[ru] = rv;       sum += arr[i].w;       adj[arr[i].u].push_back(pair<int, int>(arr[i].v, i));       adj[arr[i].v].push_back(pair<int, int>(arr[i].u, i));     }     dfs(1);     int be = -1, re = -1;     long long delta = 1e18;     for (int i = 0; i < m; ++i) {       int j = solve(arr[i].u, arr[i].v);       if (arr[i].w - arr[j].w - s / arr[i].c < delta) {         be = i, re = j;         delta = arr[i].w - arr[j].w - s / arr[i].c;       }     }     cout << sum + delta << endl;     for (int i = 0; i < m; ++i) {       if ((arr[i].in && re != i) || i == be) {         printf( %d  , arr[i].i);         if (i == be)           printf( %d n , arr[i].w - s / arr[i].c);         else           printf( %d n , arr[i].w);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, count = 0, i, flag = 1;     cin >> n;     char s[n + 1], c;     cin >> s;     int l = 0, r = 0;     l++;     for (i = 1; i < n; i++) {       c = s[i];       if (c == s[0])         l++;       else         r++;       if (r > l && flag == 1) {         flag = 0;         count++;       }       if (r < l && flag == 0) {         flag = 1;         count++;       }     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int N = 1e6 + 10;   const long long INF = 1e18;   const long double EPS = 1e-12;   long long w[200005];   long long h[200005];   long long c[200005];   long long rgcd[200005];   map<long long, vector<pair<long long, long long> > > ma;   long long gcd(long long a, long long b) {     if (b == 0) return a;     a %= b;     return gcd(b, a);   }   int main() {     int n;     cin >> n;     long long tp = 0;     for (int i = 0; i < (int)n; i++) {       cin >> w[i] >> h[i] >> c[i];       ma[w[i]].push_back(make_pair(h[i], c[i]));       if (i == 0)         tp = c[i];       else         tp = gcd(tp, c[i]);     }     map<long long, vector<pair<long long, long long> > >::iterator it, it_;     for (it = ma.begin(); it != ma.end(); it++) {       sort((it->second).begin(), (it->second).end());     }     it_ = ma.begin();     it_++;     for (it = ma.begin(); it_ != ma.end(); it++) {       int si = (int)(it->second).size();       int si_ = (int)(it_->second).size();       if (si != si_) {         cout <<  0  << endl;         return 0;       }       for (int i = 0; i < (int)si; i++) {         if ((it->second)[i].first != (it_->second)[i].first) {           cout <<  0  << endl;           return 0;         }       }       it_++;     }     long long net;     int fl = 0;     for (it = ma.begin(); it != ma.end(); it++) {       int si = (int)(it->second).size();       int si_ = (int)(it_->second).size();       long long tmp = (it->second)[0].second;       for (int i = (int)1; i < (int)si; i++) {         if (tmp == 1) break;         tmp = gcd(tmp, (it->second)[i].second);       }       for (int i = 0; i < (int)si; i++) {         ((it->second)[i].second) /= tp;       }     }     net = tp;     if (net == 1) {       it_ = ma.begin();       it_++;       for (it = ma.begin(); it_ != ma.end(); it++) {         int si = (int)(it->second).size();         int si_ = (int)(it_->second).size();         long long d1 = 0;         long long d2 = 0;         for (int i = 0; i < (int)si; i++) {           d1 += ((it->second)[i].first) * ((it->second)[i].second);         }         for (int i = 0; i < (int)si; i++) {           d2 += ((it_->second)[i].first) * ((it_->second)[i].second);         }         if (d1 != d2) {           cout <<  0  << endl;           return 0;         }         it_++;       }     }     long long ans = 0;     for (int i = (int)1; i < (int)1e6 + 5; i++) {       long long i_ = (long long)(i);       if (i_ * i_ > net) break;       if (net % i_ == 0) {         ans += 2;       }       if (i_ * i_ == net) {         ans--;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long m, e, h, x, y;   long long cal() {     for (int i = 0; i <= m * 2; i++) {       h = (h * x + y) % m;       if (h == e) return i + 1;     }     return -1;   }   void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {     if (!b)       d = a, x = 1, y = 0;     else {       exgcd(b, a % b, d, y, x);       y -= x * (a / b);     }   }   int main() {     cin >> m;     cin >> h >> e >> x >> y;     long long t1 = cal();     long long t2 = cal();     cin >> h >> e >> x >> y;     long long t3 = cal();     long long t4 = cal();     if (t1 == t3) {       cout << t1 << endl;       return 0;     }     if (~t1 && ~t2 && ~t3 && t3 > t1 && (t3 - t1) % t2 == 0) {       cout << t3 << endl;       return 0;     }     if (~t3 && ~t4 && ~t1 && t1 > t3 && (t1 - t3) % t4 == 0) {       cout << t1 << endl;       return 0;     }     if (t1 == -1 || t2 == -1 || t3 == -1 || t4 == -1) {       cout << -1 << endl;       return 0;     }     long long x, y, d, a = t2, b = t4, c = t3 - t1;     exgcd(a, b, d, x, y);     if (c % d) {       cout << -1 << endl;       return 0;     }     x = (x * (c / d)) % b;     x = (x % (b / d) + b / d) % (b / d);     y = (a * x - c) / b;     while (x < 0 || y < 0) x += d, y += d;     cout << t1 + x * t2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t, p = 0, k = 0;     cin >> n;     for (int i = 0; i <= n; i++) {       if (i < n)         cin >> t;       else         t = 90;       if (t - p > 15 && k == 0) {         k = p + 15;       } else         p = t;     }     if (k == 0) k = p;     cout << k;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N_ = 25;   int N;   char D[N_][N_];   int Table[40][1 << 20];   bool Chk[40][1 << 20];   int wh[26][N_ + N_];   int cst[2][2] = {{1, -1}, {-1, 1}};   int solve(int x, int state) {     if (x == N + N - 1) return 0;     if (Chk[x][state]) return Table[x][state];     int &ret = Table[x][state];     ret = (int)-1e9;     int state_s = state | (state << 1);     if (!state) state_s = 1;     for (int c = 0; c < 26; c++) {       int nxt = state_s & wh[c][x];       if (nxt != 0) {         int nxtret = ((c > 1) ? 0 : cst[x & 1][c]) - solve(x + 1, nxt);         ret = max(ret, nxtret);       }     }     Chk[x][state] = 1;     return ret;   }   int main() {     int i, j, k;     scanf( %d , &N);     for (i = 0; i < N; i++) scanf( %s , D[i]);     for (int c = 0; c < 26; c++)       for (i = 0; i < 2 * N - 1; i++) {         for (j = max(i - N + 1, 0); j < N && j <= i; j++)           if (D[i - j][j] == c +  a ) wh[c][i] |= 1 << j;       }     int ret = solve(0, 0);     puts((ret == 0) ?  DRAW  : ((ret > 0) ?  FIRST  :  SECOND ));     return 0;   }


#include <bits/stdc++.h>   int cmpfunc(const void *a, const void *b) { return (*(int *)a) - (*(int *)b); }   int main() {     int n, arr[200000], i, r, count = 0;     long long int result = 0;     double root;     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &arr[i]);     for (i = 0; i < n; i++) {       root = sqrt((double)arr[i]);       r = round(root);       r = r * r;       r -= arr[i];       if (arr[i] == 0) count++;       arr[i] = abs(r);     }     qsort(arr, n, sizeof(int), cmpfunc);     n = n / 2;     for (i = 0; i < n; i++) {       if (arr[i] != 0) result += (long long int)arr[i];     }     if (result == 0) {       n = 2 * n;       for (i = n / 2; i < n; i++) {         if (arr[i] == 0) result++;       }       if (count > n / 2) {         count -= (n / 2);         result += (long long int)count;       }     }     printf( %lld , result);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int B = 31;   const int N = 107;   int a[N];   bool vis[1000007];   map<int, int> state;   map<int, int> sgn;   int tc = -1;   inline int grrr(int mask) {     if (mask < 2) return 0;     auto it = sgn.find(mask);     if (it != sgn.end()) return it->second;     vector<int> to_clear;     for (int i = 1; i < B; ++i) {       int small = (mask & ((1 << i) - 1));       int large = (mask >> i);       int nmask = (small | large);       if (nmask == mask) continue;       int val = grrr(nmask);       to_clear.push_back(val);     }     for (int x : to_clear) vis[x] = true;     int ret = 0;     while (vis[ret]) ++ret;     for (int x : to_clear) vis[x] = false;     return sgn[mask] = ret;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0; i < n; ++i) {       int x = a[i];       for (int k = 2; k * k <= x; ++k) {         int cnt = 0;         while (x % k == 0) {           x /= k;           ++cnt;         }         if (cnt) state[k] |= (1 << cnt);       }       if (x > 1) state[x] |= 2;     }     int res = 0;     for (auto &qq : state) {       res ^= grrr(qq.second);     }     cout << (res ?  Mojtaba  :  Arpa ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isprime[1000001];   void sieve() {     for (long long i = 0; i <= 1000001; i++) {       isprime[i] = true;     }     isprime[0] = false;     isprime[1] = false;     for (long long i = 2; i * i <= 1000001; i++) {       if (isprime[i] == true) {         for (long long j = i * i; j <= 1000001; j = j + i) {           isprime[j] = false;         }       }     }   }   long long gcd(long long a, long long b) {     if (a < b) {       swap(a, b);     }     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   bool comparen(pair<long long, long long> a, pair<long long, long long> b) {     return a.second < b.second;   }   bool comparem(pair<long long, long long> a, pair<long long, long long> b) {     return a.first > b.first;   }   void remove(long long n, char x, string s) { ; }   void solve() {     long long t = 1;     while (t--) {       long long n;       cin >> n;       long long a[n];       for (long long i = 0; i < n; i++) {         cin >> a[i];       }       long long i = 0;       while (i < n - 1 && a[i] <= a[i + 1]) {         i++;       }       long long k = i + 1;       i++;       while (i < n - 1 && a[i] <= a[i + 1]) {         i++;       }       if (i == n) {         cout << 0 << endl;       } else if (i != n - 1 || a[0] < a[n - 1]) {         cout << -1 << endl;       } else {         cout << n - k << endl;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long solve(long long a, long long b, long long n, long long l,                   long long r) {     if (b <= l || r <= a) return 0;     if (a <= l && r <= b) return n;     long long mid = (l + r) / 2;     long long lch = solve(a, b, n / 2, l, mid);     long long rch = solve(a, b, n / 2, mid + 1, r);     long long ret = lch + rch;     if (a <= mid && mid < b) ret += n & 1;     return ret;   }   int main() {     long long n, l, r;     scanf( %lld%lld%lld , &n, &l, &r);     long long p = 1;     while (p <= n) {       p <<= 1;     }     long long ans = solve(l - 1, r, n, 0, p - 1);     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, S;     scanf( %d , &n);     scanf( %d , &S);     string st;     map<int, int> b, s;     for (int i = 0; i < n; i++) {       cin >> st;       int p, q;       scanf( %d , &p);       scanf( %d , &q);       if (st[0] ==  B ) {         b[p] += q;       } else {         s[p] += q;       }     }     int ct = max(0, (int)s.size() - S);     for (map<int, int>::reverse_iterator it = s.rbegin(); it != s.rend(); it++) {       if (ct) {         ct--;         continue;       }       cout <<  S   << it->first <<     << it->second << endl;     }     ct = 0;     for (map<int, int>::reverse_iterator it = b.rbegin(); it != b.rend(); it++) {       cout <<  B   << it->first <<     << it->second << endl;       ct++;       if (ct == S) break;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mn = 1005;   int n, v[mn];   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int x = 0; x < n; ++x) cin >> v[x];     int ans = 0;     for (int x = 0; x < n; ++x) {       if (v[x] == 0) continue;       while (v[x] == 1 && x < n) ++ans, ++x;       ++ans;     }     if (ans != 0) --ans;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e6;   int q, lstAns, flag;   long long m;   struct point {     long long x, y;     point(long long x, long long y) : x(x), y(y) {}     inline bool operator<(const point &a) const {       if (flag) return y < a.y;       return x < a.x;     }     inline point operator-(const point &a) const {       return point(x - a.x, y - a.y);     }     inline long long cross(const point &a) const { return x * a.y - y * a.x; }   };   set<point> mag;   inline int addMod(int a, int b) { return (a += b) >= mod ? a - mod : a; }   inline long long quickpow(long long base, long long pw) {     long long ret = 1;     while (pw) {       if (pw & 1) ret = ret * base % mod;       base = base * base % mod, pw >>= 1;     }     return ret;   }   template <class T>   inline void read(T &x) {     x = 0;     char ch = getchar(), w = 0;     while (!isdigit(ch)) w = (ch ==  - ), ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     x = w ? -x : x;     return;   }   int main() {     int opt, x, y;     read(q), read(m);     mag.emplace(0, 0);     for (register int i = 1; i <= q; ++i) {       read(opt), read(x), read(y);       x = (x + lstAns) % mod + 1, y = (y + lstAns) % mod + 1;       if (opt == 1) {         point tmp(y, x);         auto r = mag.lower_bound(point(y + 1, 0)), l = prev(r);         if (r != mag.end() && (tmp - *l).cross(*r - *l) >= 0) continue;         while (l != mag.begin()) {           if ((*l - *prev(l)).cross(tmp - *prev(l)) < 0) break;           mag.erase(l--);         }         while (r != mag.end() && r->y <= x) mag.erase(r++);         while (r != mag.end() && next(r) != mag.end()) {           if ((*r - tmp).cross(*next(r) - tmp) < 0) break;           mag.erase(r++);         }         mag.insert(tmp);       } else {         flag = 1;         auto r = mag.lower_bound(point(0, ceil((double)y / x))), l = prev(r);         if (r == mag.end()) {           printf( NO n ), flag = 0;           continue;         }         if ((y - l->y * x) * (r->x - l->x) + l->x * (r->y - l->y) * x <=             m * (r->y - l->y))           printf( YES n ), lstAns = i;         else           printf( NO n );         flag = 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   const int inf = 1 << 29;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       long long a, b, c, d;       cin >> a >> b >> c >> d;       a -= b;       if (a <= 0)         cout << b << endl;       else {         if (c <= d) {           cout << -1 << endl;         } else {           long long ss = c - d;           long long nss = a / ss;           if (a % ss != 0) nss++;           cout << b + nss * c << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> e[3005];   int a, b, step[3005][3005], N, M, p[5], res = -1, ans[5], q[3005], qe,                                           st[3005][5], ed[3005][5];   inline void bfs(int x) {     step[x][x] = 0;     qe = 0;     q[qe++] = x;     for (int i = 0; i < qe; i++) {       int ff = q[i];       for (vector<int>::iterator it = e[ff].begin(); it != e[ff].end(); it++)         if (step[x][*it] == -1) {           step[x][*it] = step[x][ff] + 1;           q[qe++] = *it;         }     }   }   void upDate() {     for (int i = 1; i <= 4; i++)       for (int j = 1; j <= 4; j++)         if (i != j && p[i] == p[j]) return;     int l = 0;     for (int i = 1; i < 4; i++) l += step[p[i]][p[i + 1]];     if (l > res) {       res = l;       for (int i = 1; i <= 4; i++) ans[i] = p[i];     }   }   int main() {     memset(step, -1, sizeof(step));     cin >> N >> M;     while (M--) {       cin >> a >> b;       if (a != b) e[a].push_back(b);     }     for (int i = 1; i <= N; i++) bfs(i);     for (int i = 1; i <= N; i++) {       for (int j = 0; j < 5; j++) st[i][j] = ed[i][j] = i;       for (int j = 1; j <= N; j++) {         if (j == i) continue;         for (int k = 3; k > -1; k--) {           if (step[i][st[i][k]] < step[i][j])             st[i][k + 1] = st[i][k], st[i][k] = j;           if (step[ed[i][k]][i] < step[j][i])             ed[i][k + 1] = ed[i][k], ed[i][k] = j;         }       }     }     for (int i = 1; i <= N; i++)       for (int j = 1; j <= N; j++)         if (i != j && step[i][j] > 0)           for (int u = 0; u < 4; u++)             for (int k = 0; k < 4; k++) {               p[1] = ed[i][u], p[2] = i, p[3] = j, p[4] = st[j][k];               upDate();             }     for (int i = 1; i <= 4; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (1ll << 60);   int n, k, p;   int a[2005], b[2005];   long long dp[2005][2005];   long long rec(int pos, int key) {     if (pos == n) {       return 0;     }     if (key == k) return inf;     long long &ret = dp[pos][key];     if (ret != -1) return ret;     long long sum = abs(a[pos] - b[key]) + abs(b[key] - p);     ret = max(sum, rec(pos + 1, key + 1));     ret = min(ret, rec(pos, key + 1));     return ret;   }   int main() {     cin >> n >> k >> p;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < k; i++) cin >> b[i];     sort(a, a + n);     sort(b, b + k);     memset(dp, -1, sizeof dp);     cout << rec(0, 0) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> adj[n], in[n];     int ot[n];     for (int i = 0; i < n; i++) ot[i] = 0;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       adj[x].push_back(y);       in[y].push_back(x);       ot[x]++;     }     int vis[n];     for (int i = 0; i < n; i++) {       vis[i] = 0;     }     int st, en;     cin >> st >> en;     st--;     en--;     deque<int> q;     int dp[n];     for (int i = 0; i < n; i++) dp[i] = 10000000;     dp[en] = 0;     q.push_back(en);     while (!q.empty()) {       int u = q.front();       q.pop_front();       if (vis[u] == 1) continue;       vis[u] = 1;       for (int i = 0; i < in[u].size(); i++) {         int v = in[u][i];         ot[v]--;         if (ot[v] == 0) {           if (dp[v] > dp[u]) {             dp[v] = dp[u];             q.push_front(v);           }         } else {           if (dp[v] > dp[u] + 1) {             dp[v] = dp[u] + 1;             q.push_back(v);           }         }       }     }     if (dp[st] != 10000000)       cout << dp[st];     else       cout << -1;     return 0;   }