#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   int w[N], ne[N], la[N], len[N], cst[N], t = 1;   void alink(int x, int y, int z, int c) {     w[++t] = y;     ne[t] = la[x];     la[x] = t;     len[t] = z;     cst[t] = c;   }   void link(int x, int y, int z, int c) {     alink(x, y, z, c);     alink(y, x, 0, -c);   }   int d[N], vis[N], S, T, dep[N], s[N], cur[N], n, m;   int ind[N], oud[N];   int spfa() {     for (int i = 1; i <= T; i++) vis[i] = 0, s[i] = 1e9;     s[S] = 0, vis[S] = 1;     d[1] = S;     int l = 0, r = 1;     while (l < r) {       int x = d[++l];       for (int y = la[x]; y; y = ne[y]) {         int z = w[y];         if (s[z] > s[x] + cst[y] && len[y]) {           s[z] = s[x] + cst[y];           if (!vis[z]) {             vis[z] = 1;             d[++r] = z;           }         }       }       vis[x] = 0;     }     l = 0, r = 1;     vis[S] = 1;     while (l < r) {       int x = d[++l];       cur[x] = la[x];       for (int y = la[x]; y; y = ne[y]) {         int z = w[y];         if (!vis[z] && s[z] == s[x] + cst[y] && len[y]) {           vis[z] = 1;           d[++r] = z;           dep[z] = dep[x] + 1;         }       }     }     return vis[T];   }   int ans;   int flow(int x, int val) {     if (x == T) return val;     int v1 = val;     for (int y = cur[x]; y; cur[x] = y = ne[y]) {       int z = w[y];       if (dep[z] != dep[x] + 1 || s[z] != s[x] + cst[y] || !len[y]) continue;       int use = flow(z, min(val, len[y]));       val -= use;       len[y] -= use;       len[y ^ 1] += use;       ans += cst[y] * use;       if (!val) break;     }     return v1 - val;   }   int main() {     cin >> n >> m;     S = n + 1, T = n + 2;     for (int i = 1; i <= m; i++) {       int x, y, c, f;       scanf( %d%d%d%d , &x, &y, &c, &f);       ind[y] += f;       oud[x] += f;       if (c >= f) {         link(y, x, f, 1);         link(x, y, c - f, 1);         link(x, y, 1 << 30, 2);       } else {         ans += f - c;         link(x, y, 1 << 30, 2);         link(y, x, f - c, 0);         link(y, x, c, 1);       }     }     for (int i = 1; i <= n; i++) link(S, i, ind[i], 0), link(i, T, oud[i], 0);     link(n, 1, 1 << 30, 0);     while (spfa()) flow(S, 1 << 30);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int IN() {     int c, x;     while (!isdigit(c = getchar()))       ;     x = c -  0 ;     while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c -  0 ;     return x;   }   const int N = 1000 + 19, P = 1e9 + 7;   int A[N][N], s[N][N], f[N], c[N], b[N];   int n, m, k, cnt, Ans;   void DFS(int x, int y, int col) {     if (col > k) return;     if (y == m + 1) {       DFS(x + 1, 1, col);       return;     }     if (x == n + 1) {       int tmp = 1;       for (int i = k - col + 1; i <= k - cnt; i++) tmp = 1LL * tmp * i % P;       (Ans += tmp) %= P;       return;     }     int &S = s[x][y];     S = s[x][y - 1] | s[x - 1][y];     if (A[x][y]) {       int t = A[x][y];       if (c[t]) {         if (!(S >> c[t] & 1)) S |= 1 << c[t], DFS(x, y + 1, col), S ^= 1 << c[t];       } else         for (int i = 1; i < col + 2; i++)           if (!(S >> i & 1) && !b[i])             S |= 1 << i, b[i] = 1, c[t] = i, DFS(x, y + 1, max(i, col)),                          S ^= 1 << i, b[i] = 0, c[t] = 0;     } else       for (int i = 1; i < col + 2; i++)         if (!(S >> i & 1)) S |= 1 << i, DFS(x, y + 1, max(i, col)), S ^= 1 << i;   }   int main() {     n = IN(), m = IN(), k = IN();     if (n + m - 1 > k) return puts( 0 ), 0;     for (int i = 1; i < n + 1; i++)       for (int j = 1; j < m + 1; j++) f[A[i][j] = IN()] = 1;     for (int i = 1; i < k + 1; i++) cnt += f[i];     DFS(1, 1, 0);     printf( %d n , Ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10, Mod = 1e9 + 7;   int t, n, k;   long long Pow(long long a, long long b) {     a %= Mod;     long long ans = 1;     while (b) {       if (b & 1) ans *= a, ans %= Mod;       a *= a, a %= Mod;       b >>= 1;     }     return ans;   }   long long jie[MAXN], ni[MAXN];   void getall() {     jie[0] = 1, ni[0] = 1;     for (int i = 1; i < MAXN; ++i) jie[i] = (jie[i - 1] * i) % Mod;     ni[MAXN - 1] = Pow(jie[MAXN - 1], Mod - 2);     for (int i = MAXN - 2; i >= 1; --i) ni[i] = (ni[i + 1] * (i + 1)) % Mod;   }   long long calc(int a) {     long long ans = jie[n];     ans *= ni[a], ans %= Mod;     ans *= ni[n - a], ans %= Mod;     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin >> t;     getall();     while (t--) {       cin >> n >> k;       if (k == 0) {         cout << 1 << endl;         continue;       }       long long ans = 0, tmp1, tmp2, tt = Pow(2, k);       if (n & 1) {         tmp1 = 1;         for (int i = 0; i <= n; i += 2) tmp1 += calc(i), tmp1 %= Mod;         cout << Pow(tmp1, k) << endl;       } else {         tmp1 = 0, tmp2 = 1, ans = 0;         for (int i = 0; i < n; i += 2) tmp1 += calc(i), tmp1 %= Mod;         ans = Pow(tmp1, k);         for (int i = 1; i <= k; ++i) {           ans += tmp2 * Pow(2, 1ll * (k - i) * n);           ans %= Mod;           tmp2 *= tmp1;           tmp2 %= Mod;         }         cout << ans << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, l = 1;   string s;   int main() {     cin >> n >> s;     for (int i = 1; i <= n; i++)       if (s.substr(0, i) == s.substr(i, i)) l = i;     cout << n - l + 1;     return 0;   }


#include <bits/stdc++.h>           using namespace std;      const int N = 543;   const long long INF = (long long)(1e18);      struct Matching   {     int n1, n2;     vector<set<int>> g;     vector<int> mt, used;        void init()     {       mt = vector<int>(n2, -1);     }        int kuhn(int x)     {       if(used[x] == 1) return 0;       used[x] = 1;       for(auto y : g[x])        if(mt[y] == -1 || kuhn(mt[y]) == 1)        {          mt[y] = x;          return 1;        }       return 0;     }        int calc()     {       init();       int sum = 0;       for(int i = 0; i < n1; i++)       {        used = vector<int>(n1, 0);        sum += kuhn(i);       }       return sum;     }        void remove_vertex(int v, bool right)     {       if(right)       {        for(int i = 0; i < n1; i++)         g[i].erase(v);       }       else        g[v].clear();     }        void add_edge(int x, int y)     {      g[x].insert(y);     }        Matching() {};     Matching(int n1, int n2) : n1(n1), n2(n2)     {      g.resize(n1);     };   };      int n, m, k;   long long dp[N][N];   int p[N][N];   vector<int> g[N];   long long x[N], y[N];      int main()   {    cin >> n >> m >> k;    for(int i = 0; i < m; i++)    {      int u, v;      cin >> u >> v;      --u;      --v;      g[u].push_back(v);    }    for(int i = 0; i < k; i++)     cin >> x[i] >> y[i];       Matching mt(n, n);    for(int i = 0; i < n; i++)     for(auto j : g[i])      mt.add_edge(i, j);    int cnt = mt.calc();    int cur = cnt;    vector<int> seq;    while(cur > 0)    {     int idx = 0;     for(int i = 0; i < n; i++)     {       Matching mt2 = mt;       mt2.remove_vertex(i, false);       if(mt2.calc() < cur)        idx = i + 1;       mt2 = mt;       mt2.remove_vertex(i, true);       if(mt2.calc() < cur)        idx = -(i + 1);     }     assert(idx != 0);     seq.push_back(idx);     mt.remove_vertex(abs(idx) - 1, idx < 0);     cur--;    }     reverse(seq.begin(), seq.end());    for(int i = 0; i <= k; i++)     for(int j = 0; j <= cnt; j++)      dp[i][j] = -INF;    dp[0][cnt] = 0;    for(int i = 0; i < k; i++)     for(int j = 0; j <= cnt; j++)     {      if(dp[i][j] == -INF) continue;      for(int z = 0; z <= j; z++)      {       if(i + 1 + z >= n) continue;        int t = j - z;        long long add = max(0ll, x[i] - t * y[i]);        if(dp[i + 1][z] < dp[i][j] + add)        {          dp[i + 1][z] = dp[i][j] + add;          p[i + 1][z] = j;        }      }     }    cur = max_element(dp[k], dp[k] + cnt + 1) - dp[k];    vector<int> res;    for(int i = k; i > 0; i--)    {     res.push_back(0);     for(int j = p[i][cur] - 1; j >= cur; j--)      res.push_back(seq[j]);     cur = p[i][cur];     }    reverse(res.begin(), res.end());    cout << res.size() << endl;    for(auto x : res) cout << x <<    ;    cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 100;   int a[maxn], ans[2 * maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m, c;     cin >> n >> m >> c;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) {       int val;       cin >> val;       ans[i] += val;       ans[i + n - m + 1] -= val;     }     ans[0] %= c;     for (int i = 1; i < n; i++) ans[i] = (ans[i] + ans[i - 1]) % c;     for (int i = 0; i < n; i++) cout << ((a[i] + ans[i]) % c + c) % c <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;     point() {}     point(int _x, int _y) : x(_x), y(_y) {}     point operator-(const point& other) const {       return point(other.x - x, other.y - y);     }     long long operator/(const point& other) const {       return x * 1LL * other.y - y * 1LL * other.x;     }     bool operator<(const point& other) const {       if (x == other.x) return y < other.y;       return x < other.x;     }     bool operator==(const point& other) const {       return x == other.x && y == other.y;     }     bool operator!=(const point& other) const { return !(*this == other); }   };   int gcd(int a, int b) {     while (b != 0) {       int r = a % b;       a = b;       b = r;     }     return a;   }   const int maxn = 4e5 + 5;   int n, c, d;   map<point, int> mp;   vector<point> p;   long long pref[maxn];   void read_input() {     cin >> n >> c >> d;     for (int i = 0; i < n; ++i) {       int x, y;       cin >> x >> y;       x -= c;       y -= d;       int t = gcd(abs(x), abs(y));       x /= t;       y /= t;       if (mp.count(point(x, y)) == 0) p.push_back(point(x, y));       ++mp[point(x, y)];     }   }   int up(point u) {     if (u.y == 0) return u.x > 0;     return u.y > 0;   }   bool cmp(const point& u, const point& v) {     int up_u = up(u), up_v = up(v);     if (up_u == up_v) return u / v > 0;     return up_u > up_v;   }   void init() {     sort(p.begin(), p.end(), cmp);     pref[0] = 0;     for (int i = 0; i < (int)p.size(); ++i) pref[i + 1] = pref[i] + mp[p[i]];   }   point modify(point u) { return point(-u.x, -u.y); }   void solve() {     long long ans = 0;     long long total_cnt = 0;     long long total = 0;     for (int cur = 0, nxt = 0; cur < (int)p.size(); ++cur) {       int t = lower_bound(p.begin(), p.end(), modify(p[cur]), cmp) - p.begin();       if (up(p[cur])) t = 0;       total -= mp[p[cur]] * 1LL * pref[t];       total_cnt -= mp[p[cur]];       while (nxt < (int)p.size() && p[cur] / p[nxt] >= 0 &&              modify(p[cur]) != p[nxt]) {         t = lower_bound(p.begin(), p.end(), modify(p[nxt]), cmp) - p.begin();         if (up(p[nxt])) t = 0;         total += mp[p[nxt]] * 1LL * pref[t];         total_cnt += mp[p[nxt]];         ++nxt;       }       t = upper_bound(p.begin(), p.end(), modify(p[cur]), cmp) - p.begin();       if (up(p[cur])) t = 0;       ans += mp[p[cur]] * 1LL * total;       ans -= mp[p[cur]] * 1LL * pref[t] * total_cnt;     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     read_input();     init();     solve();   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   long long mod1 = 1000000000000000009;   long long poww(long long x, long long y) {     if (y == 0) return 1;     if (x == 1) return 1;     if (x == 0) return 0;     if (y == 1) return x;     long long d = poww(x, y / 2);     d %= mod;     if (y % 2) return (((d * d) % mod) * x);     return (d * d) % mod;   }   long long combaination(long long n, long long k) {     long long f = 1;     for (long long i = 0; i < k; i++) f *= n - i, f /= i + 1;     return f;   }   long long a[300009];   vector<pair<long long, long long> > v;   int main() {     long long n;     cin >> n;     long long a[n];     long long x = 0, y = 0;     for (long long i = 1; i <= n; i++) cin >> a[i];     long long i = 1;     while (i <= n) {       if (a[i] == i) {         i++;         continue;       }       if (abs(a[i] - i) >= n / 2) {         v.push_back(make_pair(i, a[i]));         swap(a[i], a[a[i]]);       } else {         if ((n - max(i, a[i])) >= n / 2) {           x = i;           y = a[i];           v.push_back(make_pair(x, n));           v.push_back(make_pair(n, y));           v.push_back(make_pair(x, n));           swap(a[x], a[n]);           swap(a[y], a[n]);           swap(a[x], a[n]);         } else if (min(i, a[i]) - 1 >= n / 2) {           x = i;           y = a[i];           v.push_back(make_pair(x, 1));           v.push_back(make_pair(y, 1));           v.push_back(make_pair(x, 1));           swap(a[x], a[1]);           swap(a[y], a[1]);           swap(a[x], a[1]);         } else {           x = i, y = a[i];           if (x > y) swap(x, y);           v.push_back(make_pair(y, 1));           v.push_back(make_pair(x, n));           v.push_back(make_pair(n, 1));           v.push_back(make_pair(y, 1));           v.push_back(make_pair(x, n));           swap(a[y], a[1]);           swap(a[x], a[n]);           swap(a[n], a[1]);           swap(a[y], a[1]);           swap(a[n], a[x]);         }       }     }     cout << v.size() << endl;     for (long long i = 0; i < v.size(); i++)       cout << v[i].first <<     << v[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       string s;       cin >> s;       int count = 0;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  1 ) {           count++;         }       }       int n = s.size();       if (min(count, n - count) % 2 == 1) {         cout <<  DA ;       } else {         cout <<  NET ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void print(T a) {     for (auto x : a) cout << x <<    ;     cout <<   n ;   }   vector<pair<long double, long double> > lines1;   vector<long double> dots1;   long double intersection(pair<long double, long double> l1,                            pair<long double, long double> l2) {     return (l2.second - l1.second) / (l1.first - l2.first);   }   void add_mx(long double k, long double b) {     while ((long long)(lines1).size() >= 2) {       long double x =           intersection(lines1[(long long)(lines1).size() - 2], {k, b});       if (x <= dots1.back()) {         dots1.pop_back();         lines1.pop_back();       } else         break;     }     if ((long long)(lines1).size())       dots1.push_back(           intersection(lines1[(long long)(lines1).size() - 1], {k, b}));     lines1.push_back({k, b});   }   long double get_mx(long double x) {     long long id = lower_bound((dots1).begin(), (dots1).end(), x) - dots1.begin();     return lines1[id].first * x + lines1[id].second;   }   vector<pair<long double, long double> > lines2;   vector<long double> dots2;   void add_mn(long double k, long double b) {     while ((long long)(lines2).size() >= 2) {       long double x =           intersection(lines2[(long long)(lines2).size() - 2], {k, b});       if (x <= dots2.back()) {         dots2.pop_back();         lines2.pop_back();       } else         break;     }     if ((long long)(lines2).size())       dots2.push_back(           intersection(lines2[(long long)(lines2).size() - 1], {k, b}));     lines2.push_back({k, b});   }   long double get_mn(long double x) {     long long id = lower_bound((dots2).begin(), (dots2).end(), x) - dots2.begin();     return lines2[id].first * x + lines2[id].second;   }   const long double PI = acos(-1);   long double get_angle(pair<long double, long double> v) {     long double gyp = sqrt(v.first * v.first + v.second * v.second);     long double cs = v.first / gyp;     return acos(cs) * 180.0 / PI;   }   void solve() {     long long n;     cin >> n;     vector<vector<long double> > s;     vector<pair<long double, long double> > lines;     long double left_most = 1e18, right_most = -1e18;     for (long long i = 0; i < n; i++) {       long double a, b, c;       cin >> a >> b >> c;       left_most = min(left_most, a);       right_most = max(right_most, b);       s.push_back({c, a, b});       lines.push_back({c, a});       lines.push_back({c, b});     }     sort((lines).begin(), (lines).end());     vector<pair<long double, long double> > lines_mx, lines_mn;     for (long long i = 0; i < (long long)(lines).size(); i++) {       if (i != 0 && lines[i].first == lines[i - 1].first) {         lines_mx.pop_back();         lines_mx.push_back(lines[i]);       } else {         lines_mx.push_back(lines[i]);         lines_mn.push_back(lines[i]);       }     }     for (long long i = 0; i < (long long)(lines_mx).size(); i++)       add_mx(lines_mx[i].first, lines_mx[i].second);     for (long long i = (long long)(lines_mn).size() - 1; i >= 0; i--)       add_mn(lines_mn[i].first, lines_mn[i].second);     sort((s).begin(), (s).end());     vector<pair<long double, long long> > event;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < i; j++) {         if (s[j][0] == s[i][0]) continue;         pair<long double, long double> v1(s[j][2] - s[i][1], s[j][0] - s[i][0]);         pair<long double, long double> v2(s[j][1] - s[i][2], s[j][0] - s[i][0]);         long double angle1 = get_angle(v1), angle2 = get_angle(v2);         event.push_back({angle1, 1});         event.push_back({angle2, -1});       }     }     sort((event).begin(), (event).end());     if ((long long)(event).size() == 0) {       cout << fixed << setprecision(10) << right_most - left_most;       return;     }     long long balance = 0;     vector<long double> angles;     for (long long i = 0; i < (long long)(event).size(); i++) {       if (event[i].second == 1) {         if (balance == 0) angles.push_back(event[i].first);         balance++;       } else {         balance--;         if (balance == 0) angles.push_back(event[i].first);       }     }     long double ans = 1e18;     for (long double angle : angles) {       long double ct = 1.L / tan(angle / 180.0 * PI);       ans = min(ans, get_mx(ct) - get_mn(ct));     }     cout << fixed << setprecision(10) << ans;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     for (long long i = 0; i < t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<long long> f(n);     int u, v;     long long z;     while (m--) {       cin >> u >> v >> z;       f[--u] -= z;       f[--v] += z;     }     vector<long long> a, b;     for (int i = 0; i < n; i++)       if (f[i] > 0)         a.push_back(i);       else if (f[i] < 0)         b.push_back(i);     vector<vector<long long> > ans;     int id = 0;     for (int i = 0; i < a.size(); i++) {       while (id < b.size() && f[a[i]] > 0) {         long long x = min(f[a[i]], -f[b[id]]);         f[a[i]] -= x;         f[b[id]] += x;         ans.push_back({b[id], a[i], x});         if (f[b[id]] == 0) id++;       }     }     cout << ans.size() <<   n ;     for (auto x : ans) cout << x[0] + 1 <<     << x[1] + 1 <<     << x[2] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 100;   const int D = 19;   struct QUE {     int l, r, len, id;     QUE(int _l, int _r, int _len, int _id) {       l = _l;       r = _r;       len = _len;       id = _id;     }   };   struct OPT {     int x, y;     OPT(int _x, int _y) {       x = _x;       y = _y;     }   };   bool cmpx(OPT &a, OPT &b) { return a.x < b.x; }   bool cmpy(OPT &a, OPT &b) { return a.x + a.y < b.x + b.y; }   bool cmpl(QUE &a, QUE &b) { return a.l < b.l; }   bool cmpr(QUE &a, QUE &b) { return a.r < b.r; }   struct BIT {     long long bit[N] =         {             0,     },               n;     void add(int x, int v) {       while (x <= n) {         bit[x] += v;         x += x & -x;       }     }     long long sum(int x) {       long long s = 0;       while (x) {         s += bit[x];         x -= x & -x;       }       return s;     }   } b1, b2;   struct SAM {     int next[N][26], fa[N], len[N], pos[N], n;     int root, tot, last;     vector<int> g[N];     vector<QUE> q[N];     vector<OPT> p[N];     long long ans[N];     int newnode(int l) {       fa[tot] = -1;       for (int i = 0; i < 26; ++i) next[tot][i] = -1;       len[tot++] = l;       return tot - 1;     }     void init() {       tot = 0;       last = root = newnode(0);     }     void extend(int x, int ep) {       int p = last;       int cur = newnode(len[p] + 1);       while (p != -1 && next[p][x] == -1) {         next[p][x] = cur;         p = fa[p];       }       if (p == -1)         fa[cur] = root;       else {         int q = next[p][x];         if (len[q] == len[p] + 1)           fa[cur] = q;         else {           int tmp = newnode(len[p] + 1);           memcpy(next[tmp], next[q], sizeof(next[q]));           fa[tmp] = fa[q];           fa[q] = fa[cur] = tmp;           while (p != -1 && next[p][x] == q) {             next[p][x] = tmp;             p = fa[p];           }         }       }       last = cur;       pos[ep] = last;     }     int top[N], son[N], siz[N], par[N][D];     void dfs1(int u) {       for (int i = 1; i < D; ++i) par[u][i] = par[par[u][i - 1]][i - 1];       son[u] = -1;       siz[u] = 1;       for (auto v : g[u]) {         dfs1(v);         siz[u] += siz[v];         if (son[u] == -1 || siz[v] > siz[son[u]]) son[u] = v;       }     }     void dfs2(int u, int t) {       top[u] = t;       if (~son[u]) dfs2(son[u], t);       for (auto v : g[u])         if (v != son[u]) dfs2(v, v);     }     void build() {       for (int i = 0; i < tot; ++i) g[i].clear();       for (int i = 1; i < tot; ++i) g[fa[i]].push_back(i);       for (int i = 0; i < tot; ++i) par[i][0] = fa[i];       for (auto v : g[0]) dfs1(v), dfs2(v, v);     }     int getpos(int l, int r) {       r = r - l + 1;       l = pos[l];       for (int i = D - 1; i >= 0; --i)         if (len[par[l][i]] >= r) l = par[l][i];       return l;     }     void addque(int l, int r, int id) {       int tp = r - l + 1, now = getpos(l, r);       int up = top[now];       while (now) {         q[up].push_back(QUE(l, r, min(len[now], tp) - len[fa[up]], id));         tp -= min(len[now], tp) - len[fa[up]];         now = fa[up];         up = top[now];       }     }     void addopt(int l) {       int tp = n - l + 1, now = pos[l];       int up = top[now];       while (now) {         p[up].push_back(             OPT(l + len[fa[up]] + 1, min(len[now], tp) - len[fa[up]]));         tp -= min(len[now], tp) - len[fa[up]];         now = fa[up];         up = top[now];       }     }     void solve() {       memset(ans, 0, sizeof(ans));       for (int i = 1; i < tot; ++i)         if (top[i] == i) {           get(p[i], q[i], len[fa[i]] + 1);         }     }     void get(vector<OPT> &p, vector<QUE> &q, int tp) {       int i, j;       sort(p.begin(), p.end(), cmpy);       sort(q.begin(), q.end(), cmpr);       for (i = 0, j = 0; i < q.size(); ++i) {         while (j < p.size() && p[j].x + p[j].y <= q[i].r + 2) {           b1.add(p[j].y, 1);           b2.add(p[j].y, p[j].y);           j++;         }         ans[q[i].id] +=             1LL * q[i].len * (j - b1.sum(q[i].len)) + b2.sum(q[i].len);       }       while (j--) {         b1.add(p[j].y, -1);         b2.add(p[j].y, -p[j].y);       }       reverse(p.begin(), p.end());       reverse(q.begin(), q.end());       for (i = 0, j = 0; i < q.size(); ++i) {         while (j < p.size() && p[j].x + p[j].y > q[i].r + 2) {           b1.add(b1.n - p[j].x, 1);           b2.add(b2.n - p[j].x, p[j].x);           j++;         }         int c1 = b1.sum(b1.n - q[i].r - 2 + q[i].len),             c2 = b1.sum(b2.n - q[i].r - 2);         ans[q[i].id] += 1LL * (j - c1) * q[i].len;         ans[q[i].id] +=             1LL * (c1 - c2) * (q[i].r + 2) -             (b2.sum(b2.n - q[i].r - 2 + q[i].len) - b2.sum(b2.n - q[i].r - 2));       }       while (j--) {         b1.add(b1.n - p[j].x, -1);         b2.add(b2.n - p[j].x, -p[j].x);       }       for (i = 0; i < p.size(); ++i) p[i].x -= tp;       sort(p.begin(), p.end(), cmpx);       sort(q.begin(), q.end(), cmpl);       for (i = 0, j = 0; i < q.size(); ++i) {         while (j < p.size() && p[j].x < q[i].l) {           b1.add(p[j].y, 1);           b2.add(p[j].y, p[j].y);           j++;         }         ans[q[i].id] -=             1LL * (j - b1.sum(q[i].len)) * q[i].len + b2.sum(q[i].len);       }       while (j--) {         b1.add(p[j].y, -1);         b2.add(p[j].y, -p[j].y);       }     }   } sam;   char s[N];   int n, m;   int l[N], r[N];   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     sam.init();     for (int i = n; i >= 1; --i) sam.extend(s[i] -  a , i);     sam.build();     b1.n = b2.n = 2 * n + 2;     sam.n = n;     for (int i = 1; i <= n; ++i) sam.addopt(i);     scanf( %d , &m);     for (int i = 1; i <= m; ++i) {       scanf( %d %d , &l[i], &r[i]);       sam.addque(l[i], r[i], i);     }     sam.solve();     for (int i = 1; i <= m; ++i) printf( %lld n , sam.ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     long n, i, k = 0, u = 0, d = 0, l = 0, r = 0;     cin >> n;     cin >> s;     for (i = 0; i < s.size(); i++) {       if (s[i] ==  U ) u++;       if (s[i] ==  D ) d++;       if (s[i] ==  L ) l++;       if (s[i] ==  R ) r++;     }     k += min(d, u) * 2;     k += min(r, l) * 2;     cout << k;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   istream& operator>>(istream& in, pair<T1, T2>& a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T amax(T& a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T amin(T& a, T1 b) {     if (b < a) a = b;     return a;   }   const long long fx[] = {+1, -1, +0, +0};   const long long fy[] = {+0, +0, +1, -1};   const int32_t M = 1e9 + 7;   const int32_t MM = 998244353;   void solve() {     long long n;     cin >> n;     vector<long long> a(n), b(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < n; i++) {       cin >> b[i];     }     vector<long long> points(n, 1);     long long only_a = 0;     long long pa = 0, push_back = 0;     for (long long i = 0; i < n; i++) {       if (a[i] == 1 && b[i] == 0) {         only_a++;       }       if (a[i] == 1) pa++;       if (b[i] == 1) push_back++;     }     if (pa > push_back) {       cout << 1 <<   n ;       return;     } else if (only_a == 0 && pa <= push_back) {       cout << -1 <<   n ;     } else {       long long diff = push_back - pa + 1;       long long x = 1 + diff / only_a;       if (diff % only_a != 0) x++;       cout << x <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   bool isPrime(long long n) {     if (n < 2) return 0;     if (n < 4) return 1;     if (n % 2 == 0 or n % 3 == 0) return 0;     for (long long i = 5; i * i <= n; i += 6)       if (n % i == 0 or n % (i + 2) == 0) return 0;     return 1;   }   long long modexpo(long long x, long long p) {     long long res = 1;     x = x % 1000000007;     while (p) {       if (p % 2) res = res * x;       p >>= 1;       x = x * x % 1000000007;       res %= 1000000007;     }     return res;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       long long n, g, b;       cin >> n >> g >> b;       long long x = n / 2 + (n % 2);       long long ans = 0;       ans += (x / g) * g + ((x / g + (x % g ? 1 : 0)) - 1) * b;       ans += x % g;       if (n > ans) ans += n - ans;       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   string a[200009];   int pre[1 << 20][22], cost[200009], n, m, dp[200009];   int compute(string suf, string pre) {     for (int k = int(0); k <= int(m - 1); k++) {       int j = k, i = 0;       while (j < m) {         if (suf[j] == pre[i]) {           j++;           i++;         } else           break;       }       if (j == m) return k;     }     return m;   }   int calmask(string str, int len) {     int mask = 0;     for (int i = int(m - len); i <= int(m - 1); i++) {       mask = mask * 2;       if (str[i] ==  1 ) mask++;     }     return mask;   }   int calmask2(string str, int len) {     int mask = 0;     for (int i = int(0); i <= int(len - 1); i++) {       mask = mask * 2;       if (str[i] ==  1 ) mask++;     }     return mask;   }   int main() {     std::ios::sync_with_stdio(false);     int ans;     cin >> n;     for (int i = int(1); i <= int(n); i++) cin >> a[i];     m = a[1].length();     for (int i = int(0); i <= int((1 << m) - 1); i++)       for (int j = int(0); j <= int(m); j++) pre[i][j] = 2000000000;     cost[0] = 0;     for (int i = int(1); i <= int(n - 1); i++)       cost[i] = cost[i - 1] + compute(a[i], a[i + 1]);     pre[0][0] = cost[n - 1];     ans = 2000000000;     for (int i = int(n); i >= int(1); i--) {       int mini = 2000000000;       int mask = 0;       for (int j = int(0); j <= int(m); j++) {         if (i == 1 && j > 0) break;         mini = min(mini, pre[mask][j] + m - j);         if (m - j - 1 >= 0 && a[i - 1][m - j - 1] ==  1 ) mask |= (1 << j);       }       mini = mini - cost[i - 1];       dp[i] = mini;       if (i - 2 >= 0) {         mask = 0;         for (int j = int(0); j <= int(m); j++) {           pre[mask][j] = min(pre[mask][j], mini + cost[i - 2]);           mask *= 2;           if (j < m && a[i][j] ==  1 ) mask++;         }       }     }     cout << dp[1] << endl;     return 0;   }


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


#include <bits/stdc++.h>   inline int sbt(int x) { return __builtin_popcount(x); }   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n, x, y, d;       int res = 1000000007;       cin >> n >> x >> y >> d;       int dff = abs(x - y);       if (!(dff % d)) res = min(res, dff / d);       if (!((y - 1) % d))         res = min(res, (y - 1) / d + (x - 1) / d + (((x - 1) % d) > 0));       if (!((n - y) % d))         res = min(res, (n - x) / d + (n - y) / d + (((n - x) % d) > 0));       if (res == 1000000007)         cout << -1;       else         cout << res;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int INFI = (1 << 29);   const long long INFL = (1LL << 62);   using namespace std;   long long f[1000010];   void solve() {     long long n, k, d, c = 0, r = INFL;     cin >> n >> k >> d;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     for (long long i = 0; i < n; i++) {       f[v[i]]++;       c += (f[v[i]] == 1);       if (i >= d) {         f[v[i - d]]--;         c -= (!f[v[i - d]]);       }       if (i >= (d - 1)) r = min(r, c);     }     cout << r <<   n ;     for (long long i = 0; i < n; i++) f[v[i]] = 0;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long casos;     cin >> casos;     while (casos--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod;   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b % 2 == 1) return (power(a, b - 1) * a) % mod;     long long int q = power(a, b / 2);     return (q * q) % mod;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     long long int a[n];     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     long long int ans = 1;     for (long long int i = 0; i < n; i++) {       long long int cnt = 0;       long long int prev = a[i];       for (long long int j = i - 1; j >= 0; j--) {         if (a[j] <= prev) {           prev = a[j];           cnt++;         } else           break;       }       prev = a[i];       for (long long int j = i + 1; j < n; j++) {         if (a[j] <= prev) {           prev = a[j];           cnt++;         } else           break;       }       cnt++;       ans = max(ans, cnt);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   template <class T>   ostream& operator<<(ostream& os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     return os <<  ] ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   const long long int mod = 998244353;   const long long int maxn = 200001;   long long int power(long long int x, long long int n) {     long long int res = 1;     for (; n > 0; n >>= 1) {       if (n & 1) res = (res * x) % mod;       x = (x * x) % mod;     }     return res;   }   long long int no_of_ways(long long int m, long long int x) {     long long int ans = 0;     for (long long int r = 2; r <= 1000000; r++) {       long long int maxi = r * r * r;       if (maxi > x) break;       ans += (x / maxi);     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int m;     cin >> m;     long long int l = 1, r = 1e18;     long long int ans = -1;     while (r - l > 1) {       long long int mid = (l + r) / 2;       if (no_of_ways(m, mid) >= m)         r = mid;       else if (no_of_ways(m, mid) < m)         l = mid + 1;     }     if (no_of_ways(m, l) == m)       ans = l;     else if (no_of_ways(m, r) == m)       ans = r;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     for (int i = 2; i < s.size(); i++) {       int a = 0, b = 0, c = 0;       for (int j = i; j >= i - 2; j--) {         if (s[j] ==  A ) a++;         if (s[j] ==  B ) b++;         if (s[j] ==  C ) c++;       }       if (a > 0 && b > 0 && c > 0) {         cout <<  Yes ;         return 0;       }     }     cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   class point2D {    public:     long long x, y;     point2D() {}     point2D(long long x, long long y) : x(x), y(y) {}     bool operator<(const point2D &other) const {       return ((y == other.y) ? (x > other.x) : (y > other.y));     }   };   point2D operator-(const point2D &u, const point2D &v) {     return point2D(u.x - v.x, u.y - v.y);   }   long long det(const point2D &u, const point2D &v) {     return u.x * v.y - v.x * u.y;   }   long long dir(const point2D &p, const point2D &u, const point2D &v) {     return det(u - p, v - p);   }   bool contains(int n, point2D vertex, point2D polygon[]) {     int l = 1, r = n - 1, m;     while (l < r) {       m = (l + r) / 2;       if (dir(polygon[0], polygon[m], vertex) <= 0)         r = m;       else         l = m + 1;     }     if (r == 1 || dir(polygon[0], polygon[n - 1], vertex) >= 0 ||         dir(polygon[r], polygon[r - 1], vertex) >= 0)       return 0;     return 1;   }   int n, m;   point2D polygonA[100000], vertexB;   int main() {     std::cin >> n;     for (int i = n - 1; i >= 0; i--) {       std::cin >> polygonA[i].x >> polygonA[i].y;     }     std::cin >> m;     for (int i = 0; i < m; i++) {       std::cin >> vertexB.x >> vertexB.y;       if (!contains(n, vertexB, polygonA)) {         std::cout <<  NO  << std::endl;         return 0;       }     }     std::cout <<  YES  << std::endl;     return 0;   }


#include <bits/stdc++.h>   using std::pair;   using std::vector;   vector<int64_t> ReadInput(std::istream& in_stream = std::cin);   void WriteAnswer(size_t min, size_t max, std::ostream& out_stream = std::cout);   pair<size_t, size_t> GetRange(const vector<int64_t>& input);   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     auto input = ReadInput();     auto result = GetRange(input);     WriteAnswer(result.first, result.second);     return 0;   }   vector<int64_t> Power2();   pair<vector<size_t>, vector<size_t>> ChainsExtra(const vector<int64_t>& input);   template <class Integral, class Functional, class Check>   Integral BinSearch(Integral left, Integral right, Functional func, Check check);   pair<size_t, size_t> GetRange(const vector<int64_t>& input) {     auto chains_extra = ChainsExtra(input);     vector<size_t> chains = std::move(chains_extra.first);     vector<size_t> extra = std::move(chains_extra.second);     size_t sum_chains = 0, sum_extra = 0;     for (size_t i = chains.size(); i > 0;          --i, sum_chains += chains[i], sum_extra += extra[i]) {       if (sum_extra > sum_chains) {         return {1, 0};       }     }     if (sum_extra > sum_chains) {       return {1, 0};     }     size_t min = sum_chains, max = sum_chains;     for (size_t i = 0; i < chains.size(); ++i) {       if (min - chains[i] >= sum_extra + chains[i] * i) {         min -= chains[i];         sum_extra += chains[i] * i;       } else {         auto enouht = [min, &chains, sum_extra, sum_chains, i](size_t remove) {           return min - remove >= sum_extra + remove * i;         };         auto diff = [](size_t l, size_t r) { return r <= l + 1; };         min -= BinSearch(size_t(0), chains[i], enouht, diff);         break;       }     }     return {min, max};   }   template <class Integral, class Functional, class Check>   Integral BinSearch(Integral left, Integral right, Functional func, Check check);   pair<vector<size_t>, vector<size_t>> ChainsExtra(const vector<int64_t>& input) {     vector<int64_t> pow2 = Power2();     vector<size_t> count_pow2(pow2.size(), 0);     vector<size_t> extra(pow2.size(), 0);     auto diff = [](size_t l, size_t r) { return r <= l + 1; };     for (auto elem : input) {       size_t pos = BinSearch(           size_t(0), pow2.size(),           [&pow2, elem](size_t pos) { return pow2[pos] <= elem; }, diff);       if (elem == pow2[pos]) {         ++count_pow2[pos];       } else {         ++extra[pos + 1];       }     }     auto get_last = [&count_pow2]() {       for (int i = 0; i < count_pow2.size(); ++i) {         if (count_pow2[i] == 0) {           return i;         }       }       return static_cast<int>(count_pow2.size());     };     vector<size_t> chains(pow2.size(), 0);     while (get_last() != 0) {       auto pos = get_last();       auto chains_count =           *std::min_element(count_pow2.begin(), count_pow2.begin() + pos);       chains[pos] = chains_count;       std::for_each(count_pow2.begin(), count_pow2.begin() + pos,                     [chains_count](size_t& count) { count -= chains_count; });     }     for (size_t i = 0; i < pow2.size(); ++i) {       extra[i] += count_pow2[i];     }     return {chains, extra};   }   template <class Integral, class Functional, class Check>   Integral BinSearch(Integral left, Integral right, Functional func,                      Check check) {     auto dir = func(left);     while (!check(left, right)) {       Integral mid = (left + right) / 2;       if (func(mid) == dir) {         left = mid;       } else {         right = mid;       }     }     return left;   }   vector<int64_t> Power2() {     std::vector<int64_t> power2{1};     while (power2.back() < 1ll * 2000 * 1000 * 1000 * 1000) {       power2.push_back(power2.back() * 2);     }     return power2;   }   void WriteAnswer(size_t min, size_t max, std::ostream& out_stream) {     if (min > max) {       out_stream << -1;       return;     }     for (size_t i = min; i <= max; ++i) {       std::cout << i <<    ;     }   }   vector<int64_t> ReadInput(std::istream& in_stream) {     size_t input_size;     in_stream >> input_size;     vector<int64_t> input(input_size);     for (auto& elem : input) {       in_stream >> elem;     }     return input;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int g[30][30];   char s[20];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , s);       int len = strlen(s);       for (int j = 0; j < len; j++) s[j] -=  a ;       for (int j = 0; j < 26; j++) {         if (g[j][s[0]] || j == s[0])           g[j][s[len - 1]] = max(g[j][s[len - 1]], g[j][s[0]] + len);       }     }     int maxn = 0;     for (int i = 0; i < 26; i++) maxn = max(maxn, g[i][i]);     printf( %d n , maxn);   }


#include <cstdio>   #include <iostream>   #include <cstdlib>   #include <cstring>   #include <string>   #include <algorithm>   #include <cmath>   #define LL long long   using namespace std;   LL T;   LL n, arr[300000], cnt[300000], dp[300000];   int main (){       cin >> T;       while (T--){           scanf( %lld , &n);           for (LL i = 1; i <= 200000; ++ i)               cnt[i] = 0;           for (LL i = 1; i <= n; ++ i){               scanf( %lld , &arr[i]);               cnt[arr[i]] ++;           }           for (LL i = 1; i <= 200000; ++ i)               dp[i] = cnt[i];           //sort(arr + 1, arr + 1 + n);           for (LL i = 1;i <= 200000; ++ i){               for (LL j = i * 2; j <= 200000; j += i){                   dp[j] = max(dp[j], dp[i] + cnt[j]);               }           }           printf( %lld n , n - *max_element(dp + 1, dp + 200001));       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     set<string> st;     string s;     cin >> t;     getchar();     while (t--) {       getline(cin, s);       st.insert(s);     }     cout << st.size() << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 100;   int ans[N], link[N];   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       int x;       cin >> x;       int down = max(1, i - k);       int up = min(n, i + k);       int down2 = max(1, x - k);       int up2 = min(n, x + k);       if (x == 0) {         ans[i] = up - down + 1 + ans[x];       } else {         if (up2 < down)           ans[i] = up - down + 1 + ans[x];         else           ans[i] = up - down + 1 + ans[x] - (up2 - down + 1);       }       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int ans = 0;       string s;       cin >> s;       if (s.size() % 2 == 0) {         for (int j = 0; j < s.size() / 2; j++) {           if (s[j] != s[s.size() / 2 + j]) {             ans = 1;             cout <<  NO  << endl;             break;           }         }         if (ans != 1) {           cout <<  YES  << endl;         }       } else {         cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, q, a[200010];   struct node {     int l, r, color;     node(int ll = 0, int rr = 0, int cc = 0) : l(ll), r(rr), color(cc) {}   };   node p[200010];   vector<int> vec[200010];   bool cmp(node a, node b) {     if (a.l != b.l) return a.l < b.l;     return a.r > b.r;   }   int main() {     cin >> n >> q;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       vec[a[i]].push_back(i);     }     int cnt = 0;     for (int i = 1; i <= 200000; ++i) {       if (!vec[i].size()) continue;       int l = vec[i][0], r = vec[i][vec[i].size() - 1];       p[++cnt] = node(l, r, i);     }     sort(p + 1, p + 1 + cnt, cmp);     p[cnt + 1].l = 1e9 + 10;     int ans = 0, l = 1e9 + 10, r = 0, tmp = 0;     for (int i = 1; i <= cnt + 1; ++i) {       if (i == 1 || p[i].l < r) {         l = min(l, p[i].l);         r = max(r, p[i].r);         tmp = max(tmp, (int)vec[p[i].color].size());       } else {         ans += r - l + 1 - tmp;         l = p[i].l;         r = p[i].r;         tmp = vec[p[i].color].size();       }     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> stations[200001];   int nxt[200001];   int main() {     int d, n, m;     scanf( %d%d%d , &d, &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &stations[i].first, &stations[i].second);     }     sort(stations, stations + m);     stack<int> s;     for (int i = m - 1; i > -1; i--) {       nxt[i] = -1;       while (!s.empty()) {         if (stations[s.top()].second > stations[i].second) {           s.pop();         } else {           nxt[i] = s.top();           break;         }       }       s.push(i);     }     long long cost = 0;     int gas = n;     for (int i = 0; i < m; i++) {       gas -= stations[i].first - (i == 0 ? 0 : stations[i - 1].first);       if (gas < 0) {         cout << -1 << endl;         return 0;       }       int needed;       if (nxt[i] == -1 || stations[nxt[i]].first - stations[i].first > n) {         needed = min(n, d - stations[i].first);       } else {         needed = stations[nxt[i]].first - stations[i].first;       }       if (gas < needed) {         cost += (long long)stations[i].second * (needed - gas);         gas = needed;       }     }     gas -= d - stations[m - 1].first;     if (gas < 0) {       cout << -1 << endl;     } else {       cout << cost << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int tree[N << 2], Max[N << 2], Min[N << 2], cnt;   bool fu[N << 2];   pair<int, int> a[N];   void build(int l, int r, int rt) {     if (l == r) {       tree[rt] = a[l].first;       Max[rt] = Min[rt] = tree[rt];       return;     }     int m = (l + r) >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);     Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);     Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);   }   bool qu(int f, int x, int rt) {     int ma, mi;     ma = Max[rt], mi = Min[rt];     if ((f ==  >  && mi > x) || (f ==  <  && ma < x))       return true;     else       return false;   }   bool qu2(int f, int x, int rt) {     int ma, mi;     ma = Max[rt], mi = Min[rt];     if ((f ==  >  && ma > x) || (f ==  <  && mi < x))       return true;     else       return false;   }   void ff(int rt) {     fu[rt] = !fu[rt];     swap(Max[rt], Min[rt]);     Max[rt] *= -1;     Min[rt] *= -1;   }   void updata(int l, int r, int rt, char f, int x) {     if (qu(f, x, rt)) {       ff(rt);       return;     }     int m = (l + r) >> 1;     if (fu[rt]) ff(rt << 1), ff(rt << 1 | 1), fu[rt] = 0;     if (qu2(f, x, rt << 1)) updata(l, m, rt << 1, f, x);     if (qu2(f, x, rt << 1 | 1)) updata(m + 1, r, rt << 1 | 1, f, x);     Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);     Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);   }   int ans[N];   void print(int l, int r, int rt) {     if (l == r) {       if (fu[rt]) tree[rt] *= -1;       ans[a[l].second] = tree[rt];       return;     }     int m = (l + r) >> 1;     if (fu[rt]) ff(rt << 1), ff(rt << 1 | 1), fu[rt] = 0;     print(l, m, rt << 1);     print(m + 1, r, rt << 1 | 1);   }   int main() {     int n, q;     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i].first, a[i].second = i;     sort(a + 1, a + n + 1);     build(1, n, 1);     while (q--) {       char f;       int x;       cin >> f >> x;       updata(1, n, 1, f, x);     }     print(1, n, 1);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 1212;   int num, n, x, i, j, cur, t, s[MX], a[MX][3], r[MX], mx[MX], mxs[MX];   priority_queue<pair<int, int>> q[2];   vector<int> v[MX];   bool u[MX];   int main() {     scanf( %d , &n);     for (i = 0; i < 3 * n; i++) {       scanf( %d , &x);       u[x] = true;     }     for (i = 1; i <= 6 * n; i++)       if (t > 1 && u[s[t]] == u[i] && u[s[t - 1]] == u[i]) {         a[++num][0] = s[t - 1];         a[num][1] = s[t];         a[num][2] = i;         t -= 2;         for (j = 0; j < 3; j++) mxs[num] = max(mxs[num], mx[a[num][j]]);         v[s[t]].push_back(num);         mx[s[t]] = max(mx[s[t]], mxs[num] + 1);       } else         s[++t] = i;     for (i = 0; i < v[0].size(); i++)       q[int(u[a[v[0][i]][0]])].push({mxs[v[0][i]], v[0][i]});     for (i = 0; i < 2 * n; i++) {       cur = i & 1;       x = q[cur].top().second;       q[cur].pop();       r[2 * n - i - 1] = x;       cur = 1 - cur;       for (j = 0; j < 3; j++)         for (int k : v[a[x][j]]) q[cur].push({mxs[k], k});     }     for (i = 0; i < 2 * n; i++)       for (j = 0; j < 3; j++) printf( %d%c , a[r[i]][j], (j == 2) ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[405][405];   int q[405 * 405], d[405], t, h, ans;   bool vis[405];   int main() {     int n, m;     scanf( %d %d , &n, &m);     while (m--) {       int u, v;       scanf( %d %d , &u, &v);       a[u][v] = a[v][u] = 1;     }     t = h = 0;     q[h++] = 1;     vis[1] = 1;     memset(d, -1, sizeof d);     d[1] = 0;     while (t < h) {       int u = q[t++];       for (int i = 1; i <= n; i++) {         if (vis[i] || a[u][i] == 0) continue;         d[i] = d[u] + 1;         vis[i] = 1;         q[h++] = i;       }     }     if (d[n] == -1) {       puts( -1 );       return 0;     }     ans = d[n];     t = h = 0;     q[h++] = 1;     memset(vis, 0, sizeof vis);     vis[1] = 1;     memset(d, -1, sizeof d);     d[1] = 0;     while (t < h) {       int u = q[t++];       for (int i = 1; i <= n; i++) {         if (vis[i] || a[u][i]) continue;         d[i] = d[u] + 1;         vis[i] = 1;         q[h++] = i;       }     }     if (d[n] == -1) {       puts( -1 );       return 0;     }     ans = max(ans, d[n]);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n;   char str[200005];   void solve() {     long long i, b = 0, c, s = 1, min = 0;     char a;     a = str[0];     str[n] = str[0];     for (i = 1; i < n; ++i) {       if (str[i] != a) {         b = i;         break;       } else {         s++;       }     }     if (b) {       for (i = n - 1; i >= b; --i) {         if (str[i] != a) {           c = i;           break;         } else {           s++;         }       }     }     if (b) {       min += s / 3;       a = str[b];       s = 1;       for (i = b + 1; i <= c; ++i) {         if (str[i] == a) {           s++;         } else {           a = str[i];           min += s / 3;           s = 1;         }       }       min += s / 3;       cout << min <<   n ;     } else {       min += (s + 2) / 3;       cout << min <<   n ;     }   }   void nhap() {     long long i;     cin >> n;     cin >> str;   }   int main() {     long long i;     cin >> t;     for (i = 1; i <= t; ++i) {       nhap();       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int main() {     scanf( %d%d , &n, &m);     if (n == 1 && m == 1) {       puts( YES n1 );       return 0;     }     if ((n == 1 && m <= 3) || (m == 1 && n <= 3) || (n == 2 && m <= 3) ||         (m == 2 && n <= 3)) {       puts( NO );       return 0;     }     if (n == 1 && m == 4) {       puts( YES n2 4 1 3 );       return 0;     }     if (n == 4 && m == 1) {       puts( YES n2 n4 n1 n3 );       return 0;     }     if (n == 1 && m >= 5) {       puts( YES );       for (int i = 1; i <= m; i += 2) printf( %d  , i);       for (int i = 2; i <= m; i += 2) printf( %d  , i);       return 0;     }     if (n >= 5 && m == 1) {       puts( YES );       for (int i = 1; i <= n; i += 2) printf( %d n , i);       for (int i = 2; i <= n; i += 2) printf( %d n , i);       return 0;     }     if (n == m && n == 3) {       puts( YES n6 1 8 n7 5 3 n2 9 4 );       return 0;     }     vector<vector<int> > g;     g.resize(n);     for (int i = 0, u = 1; i < n; i++) {       g[i].resize(m);       for (int j = 0; j < m; j++) g[i][j] = u++;     }     if (n <= m) {       for (int i = 0; i < n; i++)         if (i & 1) {           vector<int> tmp;           for (int j = 0; j < m; j++) tmp.push_back(g[i][(j + 2) % m]);           tmp.swap(g[i]);         }       for (int j = 0; j < m; j++)         if (j & 1) {           vector<int> tmp;           for (int i = 0; i < n; i++) tmp.push_back(g[(i - 1 + n) % n][j]);           for (int i = 0; i < n; i++) g[i][j] = tmp[i];         }     } else {       for (int i = 0; i < n; i++)         if (i & 1) {           vector<int> tmp;           for (int j = 0; j < m; j++) tmp.push_back(g[i][(j - 1 + m) % m]);           tmp.swap(g[i]);         }       for (int j = 0; j < m; j++)         if (j & 1) {           vector<int> tmp;           for (int i = 0; i < n; i++) tmp.push_back(g[(i + 2) % n][j]);           for (int i = 0; i < n; i++) g[i][j] = tmp[i];         }     }     puts( YES );     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) printf( %d  , g[i][j]);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t = 0;     int l, r, n, m, x;     cin >> n >> x;     int sum;     int temp = 0;     for (int i = 1; i <= n; i++) {       cin >> l >> r;       sum = 0;       while (1) {         t = t + x;         if (t >= l) {           t = (t - x);           sum = sum + (r - t);           t = t + sum;           break;         }       }       temp = temp + sum;     }     cout << temp << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool umin(T& a, T b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool umax(T& a, T b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   int arr[100009], n, s[100009 << 2];   int M[2][100009][20], LOG[100009], s1, s2;   void upd(int l, int r, int nd, int x, int y) {     if (l > y or x > r) return;     if (l <= x and y <= r) {       s[nd] = 1;       return;     }     int mid = (x + y) >> 1;     upd(l, r, nd << 1, x, mid);     upd(l, r, nd << 1 | 1, mid + 1, y);   }   int tap(int p, int nd, int x, int y) {     if (x == y) return s[nd];     int mid = (x + y) >> 1;     if (p <= mid) return tap(p, nd << 1, x, mid) | s[nd];     return tap(p, nd << 1 | 1, mid + 1, y) | s[nd];   }   int get(int p, int l, int r) {     int k = LOG[r - l + 1];     if (!p) return max(M[0][l][k], M[0][r - (1 << k) + 1][k]);     return min(M[1][l][k], M[1][r - (1 << k) + 1][k]);   }   bool ok(int x, int y) {     memset(s, 0, sizeof s);     arr[0] = y;     for (int i = 0; i < n; i++) {       if (i and !tap(i, 1, 1, n)) continue;       int st = i + 1, en = n;       while (st + 1 < en) {         int mid = (st + en) >> 1;         int mx = get(0, i + 1, mid), mn = get(1, i + 1, mid);         if (abs(mx - arr[i]) <= x and abs(mn - arr[i]) <= x)           st = mid;         else           en = mid;       }       int mx = get(0, i + 1, en), mn = get(1, i + 1, en);       if (abs(mx - arr[i]) <= x and abs(mn - arr[i]) <= x)         upd(i + 1, en, 1, 1, n);       else if (abs(arr[i] - arr[i + 1]) <= x)         upd(i + 1, st, 1, 1, n);     }     return tap(n, 1, 1, n);   }   int main() {     scanf( %d%d%d , &n, &s1, &s2);     for (int i = 1; i <= n; i++) {       scanf( %d , arr + i);       M[0][i][0] = M[1][i][0] = arr[i];       LOG[i] = log2(i);     }     for (int j = 1; j <= 17; j++)       for (int i = 1; i <= n; i++) {         if (i + (1 << (j - 1)) <= n)           M[0][i][j] = max(M[0][i][j - 1], M[0][i + (1 << (j - 1))][j - 1]);         else           M[0][i][j] = M[0][i][j - 1];       }     for (int j = 1; j <= 17; j++)       for (int i = 1; i <= n; i++) {         if (i + (1 << (j - 1)) <= n)           M[1][i][j] = min(M[1][i][j - 1], M[1][i + (1 << (j - 1))][j - 1]);         else           M[1][i][j] = M[0][1][j - 1];       }     int st = abs(s1 - s2), en = 1e9;     while (st + 1 < en) {       int mid = (st + en) >> 1;       if (ok(mid, s1) or ok(mid, s2))         en = mid;       else         st = mid;     }     if (ok(st, s1) or ok(st, s2))       printf( %d n , st);     else       printf( %d n , en);     return 0;   }


#include <bits/stdc++.h>   int main(void) {     int a[101];     int b, c, d, i, j, count = 0, num, number;     scanf( %d %d , &b, &c);     for (i = 0; i < b; i++) {       scanf( %d , &a[i]);     }     for (i = 0; i < b - 1; i++) {       for (j = i + 1; j < b; j++) {         if (a[i] > a[j]) {           d = a[i];           a[i] = a[j];           a[j] = d;         }       }     }     for (i = 0; i < b; i++) {       num = 0;       number = 1;       for (j = i + 1; j < b; j++) {         num += a[j] - a[j - 1];         if (num > c) {           break;         } else {           number++;         }       }       if (number > count) {         count = number;       }     }     printf( %d n , b - count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int a[(int)(1e6 + 10)], b[(int)(1e6 + 10)];   int c[(int)(1e6 + 10)];   int p[(int)(1e6 + 10)];   int rk[(int)(1e6 + 10)];   bool color[(int)(1e6 + 10)];   int find(int x) { return p[x] == x ? x : find(p[x]); }   void init() {     for (int i = 1; i <= 1e6; i++) p[i] = i, rk[i] = 1;   }   struct revo {     int px, py, rkx, rky;   };   stack<revo> st;   void merge(int x, int y) {     int px = find(x), py = find(y);     revo tmp = {px, py, rk[px], rk[py]};     st.push(tmp);     if (rk[px] > rk[py]) swap(px, py);     p[px] = py;     rk[py] = max(rk[py], rk[px] + 1);   }   void revocation(int k) {     while (k--) {       revo node = st.top();       st.pop();       p[node.px] = node.px;       p[node.py] = node.py;       rk[node.px] = node.rkx;       rk[node.py] = node.rky;     }   }   map<pair<int, int>, vector<pair<int, int> > > mp;   long long ans;   int main() {     ios::sync_with_stdio(0);     init();     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i <= m; i++) cin >> a[i] >> b[i];     for (int i = 1; i <= m; i++) {       if (c[a[i]] == c[b[i]]) {         int x = find(a[i]), y = find(b[i]);         if (x != y)           merge(a[i], b[i] + n), merge(b[i], a[i] + n);         else           color[c[a[i]]] = 1;       }     }     for (int i = 1; i <= m; i++) {       if (c[a[i]] == c[b[i]]) continue;       if (color[c[a[i]]] || color[c[b[i]]]) continue;       if (c[a[i]] > c[b[i]]) swap(a[i], b[i]);       mp[{c[a[i]], c[b[i]]}].emplace_back(a[i], b[i]);     }     for (auto node : mp) {       int cnt = 0;       for (auto it : node.second) {         int x, y;         tie(x, y) = it;         int px = find(x), py = find(y);         if (px != py)           merge(x, y + n), merge(y, x + n), cnt += 2;         else {           ans--;           break;         }       }       revocation(cnt);     }     long long cnt = 0;     for (int i = 1; i <= k; i++)       if (!color[i]) cnt++;     ans += cnt * (cnt - 1) / 2;     cout << ans << endl;   }


#include <bits/stdc++.h>   const double EPS = 1e-9;   using namespace std;   template <class T>   inline T gcd(T a, T b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   template <class T>   inline T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   inline int nxt() {     int wow;     scanf( %d , &wow);     return wow;   }   inline long long int lxt() {     long long int wow;     scanf( %lld , &wow);     return wow;   }   inline double dxt() {     double wow;     scanf( %lf , &wow);     return wow;   }   bool comp(string xx, string yy) {     if (xx.length() == yy.length())       return xx < yy;     else       return xx.length() > yy.length();   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     map<string, vector<string>> phone, book;     set<string> st;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       st.insert(s);       int m;       cin >> m;       for (int i = 0; i < m; i++) {         string num;         cin >> num;         phone[s].push_back(num);       }     }     for (auto it = st.begin(); it != st.end(); ++it) {       sort(phone[*it].begin(), phone[*it].end(), comp);       for (int i = 0; i < phone[*it].size(); i++) {         bool dup = false;         for (int j = 0; j < book[*it].size(); j++) {           bool match = true;           for (int k = phone[*it][i].length() - 1, l = book[*it][j].length() - 1;                k >= 0; k--, l--) {             if (phone[*it][i][k] != book[*it][j][l]) match = false;           }           if (match == true) dup = true;         }         if (dup == false) book[*it].push_back(phone[*it][i]);       }     }     cout << st.size() << endl;     for (auto it = st.begin(); it != st.end(); ++it) {       cout << *it <<     << book[*it].size();       for (int i = 0; i < book[*it].size(); i++) cout <<     << book[*it][i];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, i, x, k, j = 0;     scanf( %d%d , &n, &k);     for (i = 0; i < n; i++) {       for (x = 0; x < n; x++) {         if (i == j && x == j) {           printf( %d  , k);         } else           printf( 0  );       }       j++;       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int n, a[200011], c[200011], isc[200011], cn, pow2[200011] = {1}, seen[200011];   int main() {     int ans = 1, mod = 1e9 + 7;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , a + i);       pow2[i] = pow2[i - 1] * 2LL % mod;     }     for (int i = 1; i <= n; i++) {       if (seen[i] < 0) continue;       ++cn;       int j;       for (j = i; seen[j] >= 0; j = a[j]) seen[j] = -cn, ++c[cn];       if (seen[j] == -cn) isc[cn] = 1;       if (isc[cn]) {         int clen = 1;         for (int k = a[j]; seen[k] == -cn && k != j; k = a[k]) ++clen;         ans = (ans * 1LL * (pow2[clen] - 2 + mod) % mod) % mod;         ans = (ans * 1LL * pow2[c[cn] - clen]) % mod;       } else {         ans = (ans * 1LL * pow2[c[cn]]) % mod;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> teams(200000);   int main() {     ios_base::sync_with_stdio(false);     int N;     cin >> N;     for (int i = 0; i < N; i++) cin >> teams[i];     for (int i = 0; i < N; i++) {       if (teams[i] == 0) {         continue;       }       if (teams[i] >= 2) {         teams[i] %= 2;       }       if (teams[i] == 1 && i != N - 1) {         teams[i + 1] -= 1;         teams[i] -= 1;       }       if (teams[i] != 0) {         cout <<  NO               <<   n ;         return 0;       }     }     cout <<  YES           <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int p = n + 1;     int cnt = 0;     cout << ((n + 1) * 3) + 1 << endl;     for (int i = 0; i < p; i++) {       cout << i <<     << i << endl;       cout << i + 1 <<     << i << endl;       cout << i <<     << i + 1 << endl;       cnt += 3;     }     cout << p <<     << p << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, s, i, j;     cin >> n >> m >> s;     long long ans = 0;     for (i = 1; i <= n; i += 2) {       for (j = 1; j <= m; j += 2) {         long long cnt = (n - i + 1) * (m - j + 1);         long long tmp = 0;         if (i * j == s) {           tmp = (i + 1) / 2 * (j + 1) / 2;           ans += cnt * (tmp * 2 - 1);         }         if (i * j > s) {           int x, ss = i * j;           if ((ss - s) % 4) continue;           int res = (ss - s) / 4;           for (int k = 1; k <= j / 2; k++) {             if (res % k) continue;             x = res / k;             if (x > i / 2) continue;             tmp += 2;           }           ans += cnt * tmp;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long mod(long long n, long long m) {     long long ret = n % m;     if (ret < 0) ret += m;     return ret;   }   long long gcd(long long a, long long b) {     return (b == 0LL ? a : gcd(b, a % b));   }   long long exp(long long a, long long b, long long m) {     if (b == 0LL) return 1LL;     if (b == 1LL) return mod(a, m);     long long k = mod(exp(a, b / 2, m), m);     if (b & 1LL) {       return mod(a * mod(k * k, m), m);     } else       return mod(k * k, m);   }   const int N = 660;   vector<int> g[N];   vector<int> nada;   map<vector<int>, int> cache;   int ask(vector<int> v) {     sort(v.begin(), v.end());     int k = (int)v.size();     if ((int)v.size() == 1) return 0;     if (cache.count(v)) return cache[v];     cout <<  ?   << k <<   n ;     for (int x : v) cout << x <<    ;     cout <<   n ;     cout.flush();     int m;     cin >> m;     if (m == -1) {       exit(0);     }     return cache[v] = m;   }   int vis[N];   int ed;   int n;   struct dsu {     vector<int> p, ps;     dsu(int n) {       p = vector<int>(n + 1), ps = vector<int>(n + 1, 1);       for (int i = 0; i < (n + 1); ++i) p[i] = i;     }     int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }     bool join(int x, int y) {       x = find(x), y = find(y);       if (x == y) return 0;       if (ps[x] > ps[y]) swap(x, y);       p[x] = y, ps[y] += ps[x];       return 1;     }   };   dsu D(N);   void addEdge(int a, int b) {     g[a].push_back(b);     g[b].push_back(a);     ed++;     assert(ed < n);   }   void go(int id, vector<int> cur) {     int tam = (int)cur.size();     if (tam == 0) return;     if (tam == 1) {       if (D.find(id) == D.find(cur[0])) return;       cur.push_back(id);       int m = ask(cur);       if (m == 1) {         D.join(id, cur[0]);         addEdge(id, cur[0]);       }       return;     }     vector<int> L, R;     for (int i = 0; i < tam / 2; i++) {       L.push_back(cur[i]);     }     if ((int)L.size()) {       int m1 = ask(L);       vector<int> aux = L;       aux.push_back(id);       int m11 = ask(aux);       if (m1 != m11) {         go(id, L);       }     }     for (int i = tam / 2; i < (int)cur.size(); i++) {       R.push_back(cur[i]);     }     cur.clear();     if ((int)R.size()) {       int m1 = ask(R);       vector<int> aux = R;       aux.push_back(id);       int m11 = ask(aux);       if (m1 != m11) {         go(id, R);       }     }     return;   }   vector<int> cor[2];   int pai[N];   int H[N];   vector<int> path(int a, int b) {     vector<int> vL, vR;     while (a != b) {       if (H[a] > H[b]) {         vL.push_back(a);         a = pai[a];       } else if (H[b] > H[a]) {         vR.push_back(b);         b = pai[b];       } else {         vL.push_back(a);         a = pai[a];       }     }     vector<int> ans;     vL.push_back(a);     for (int x : vL) ans.push_back(x);     reverse(vR.begin(), vR.end());     for (int x : vR) ans.push_back(x);     return ans;   }   void vai(vector<int> v, int tot) {     if ((int)v.size() <= 1 or tot == 0) return;     vector<int> L, R;     int tam = (int)v.size();     for (int i = 0; i < tam / 2; i++) {       L.push_back(v[i]);     }     for (int i = tam / 2; i < tam; i++) {       R.push_back(v[i]);     }     v.clear();     int m1 = ask(L);     int m2 = ask(R);     if (m1 + m2 == tot) {       vai(L, m1);       vai(R, m2);       return;     }     assert((int)R.size() > 0);     for (int x : L) {       vector<int> aux = R;       aux.push_back(x);       int mn = ask(aux);       if (mn == m2) continue;       while ((int)R.size() > 1) {         int mid = (int)R.size() / 2;         vector<int> v1, v2;         for (int i = 0; i < mid; i++) {           v1.push_back(R[i]);         }         for (int i = mid; i < (int)R.size(); i++) {           v2.push_back(R[i]);         }         aux = v1;         aux.push_back(x);         int t1 = ask(v1);         int t2 = ask(aux);         if (t1 < t2) {           R = v1;         } else           R = v2;       }       int ca = x, cb = R[0];       assert(D.find(ca) == D.find(cb));       vector<int> cic = path(ca, cb);       cout <<  N   << (int)cic.size() <<   n ;       for (int y : cic) cout << y <<    ;       cout <<   n ;       exit(0);     }   }   void tenta(int c) {     if ((int)cor[c].size() == 0) return;     int m = ask(cor[c]);     if (m == 0) return;     vai(cor[c], m);   }   int tt = 0;   vector<int> vec;   int comp = 0;   void dfs(int v, int c, int p) {     cor[c].push_back(v);     tt++;     vec.push_back(v);     vis[v] = 1;     if (tt > N) exit(0);     for (int to : g[v]) {       if (to != p) {         pai[to] = v;         H[to] = H[v] + 1;         dfs(to, 1 - c, v);       }     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1; i <= n; i++) {       nada.clear();       for (int j = 1; j <= n; j++)         if (D.find(i) != D.find(j)) nada.push_back(j);       go(i, nada);     }     for (int i = 1; i <= n; i++) {       vis[i] = 0;     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         H[i] = 1;         pai[i] = 0;         vec.clear();         dfs(i, 0, i);         int cnt = 0;         for (int x : vec) cnt += (H[x] == 1);         assert(cnt == 1);         vector<int> mark(n + 1, 0);         int aqui = i;         for (int x : vec) {           mark[x] = 1;           assert(D.find(aqui) == D.find(i));         }         for (int j = 1; j <= n; j++) {           if (!mark[j]) {             assert(D.find(j) != D.find(i));           }         }         for (int x : vec) {           int cur = x;           while (cur != i and cur > 0) cur = pai[cur];           assert(cur == i);         }       }     }     for (int i = 1; i <= n; i++) {       assert(H[i] == H[pai[i]] + 1);     }     tenta(0);     tenta(1);     if ((int)cor[0].size() < (int)cor[1].size()) swap(cor[0], cor[1]);     cout <<  Y   << (int)cor[0].size() <<   n ;     for (int x : cor[0]) cout << x <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, tot, res = 0, f[666666], g[666666], q[666666], ans[666666][3];   struct edge {     int adj, nex;     bool vis;     edge(int a_ = 0, int n_ = 0) {       adj = a_;       nex = n_;       vis = false;     }   } e[666666];   void add(int u, int v) {     e[++tot] = edge(v, e[u].nex);     e[u].nex = tot;     e[++tot] = edge(u, e[v].nex);     e[v].nex = tot;   }   void bfs(int x) {     int l = 0, r = 1;     q[1] = x;     f[x] = -1;     while (l < r) {       for (int i = e[q[++l]].nex; i; i = e[i].nex) {         if (!f[e[i].adj]) {           q[++r] = e[i].adj;           f[e[i].adj] = q[l];         }       }     }     for (int i = r; i; i--) {       g[0] = 0;       for (int j = e[q[i]].nex; j; j = e[j].nex) {         if (!e[j].vis && e[j].adj != f[q[i]]) {           g[++g[0]] = j;         }       }       for (int j = e[q[i]].nex; j; j = e[j].nex) {         if (!e[j].vis && e[j].adj == f[q[i]]) {           g[++g[0]] = j;           break;         }       }       for (int j = 1; j < g[0]; j += 2) {         e[g[j]].vis = e[g[j] ^ 1].vis = e[g[j + 1]].vis = e[g[j + 1] ^ 1].vis =             true;         ans[++res][0] = e[g[j]].adj;         ans[res][1] = q[i];         ans[res][2] = e[g[j + 1]].adj;       }     }   }   int main() {     int u, v;     scanf( %d%d , &n, &m);     tot = n | 1;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &u, &v);       add(u, v);     }     for (int i = 1; i <= n; i++) {       if (!f[i]) {         bfs(i);       }     }     printf( %d n , res);     for (int i = 1; i <= res; i++) {       printf( %d %d %d n , ans[i][0], ans[i][1], ans[i][2]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   struct date {     int arr[3];     void read() {       string s;       cin >> s;       for (int i = 0; i < 3; i++) {         arr[i] = (s[i * 3] -  0 ) * 10 + (s[i * 3 + 1] -  0 );       }     }     void next() { next_permutation(arr, arr + 3); }     bool valid() {       if (arr[1] > 12) return false;       if (arr[0] > (days[arr[1] - 1] + (arr[1] == 2 && arr[2] % 4 == 0)))         return false;       return true;     }     bool operator<=(const date& oth) const {       for (int i = 2; i >= 0; i--) {         if (arr[i] != oth.arr[i]) {           return arr[i] < oth.arr[i];         }       }       return true;     }   };   int main() {     date a, b;     a.read();     b.read();     for (int i = 0; i < 6; i++) {       if (b.valid()) {         b.arr[2] += 18;         if (b <= a) {           cout <<  YES n ;           return 0;         }         b.arr[2] -= 18;       }       b.next();     }     cout <<  NO n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int T;     cin >> T;     while (T--) {       int result = 0;       int a[105];       int n;       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];         if (a[i] - result - i > 0) {           result += a[i] - result - i;         }       }       cout << result << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> v;   vector<tuple<int, int, int> > g[300002];   vector<pair<int, long long int> > vv[300002];   vector<pair<long long int, long long int> > V[300002];   long long int dist[300002];   priority_queue<pair<long long int, int>, vector<pair<long long int, int> >,                  greater<pair<long long int, int> > >       q;   struct st {     long long int dp[2][2];     int l;     int r;     st() {       dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = LLONG_MAX;       l = -1;       r = -1;     }     void set_node(int x) {       dp[0][0] = 0;       dp[1][1] = 0;       dp[0][1] = dist[x];       dp[1][0] = dist[x];       l = r = x;     }     st flip() {       swap(l, r);       swap(dp[0][1], dp[1][0]);       return (*this);     }   };   map<pair<int, int>, long long int> mp;   pair<long long int, long long int> look[300002];   int dep[300002];   int lcc[19][300002];   st pat[19][300002];   inline long long int ds(int t1, int node1, int t2, int node2) {     if (lcc[0][node1] != node2) swap(node1, node2);     if (t1 == 0) {       return look[node1].first;     } else {       return look[node1].second;     }   }   inline st merge(st a, st b) {     if (a.l == -1) {       return b;     }     if (b.l == -1) {       return a;     }     st r;     r.l = a.l;     r.r = b.r;     long long int w[] = {ds(0, a.r, 0, b.l), ds(1, a.r, 1, b.l)};     for (int i = 0; i < 2; i++) {       for (int j = 0; j < 2; j++) {         for (int k = 0; k < 2; k++) {           long long int cs = a.dp[i][j] + b.dp[j][k];           cs += w[j];           r.dp[i][k] = min(r.dp[i][k], cs);         }       }     }     return r;   }   inline void dfs(int a, int d = 0, int p = -1,                   pair<long long int, long long int> c = make_pair(-1, -1)) {     lcc[0][a] = p;     dep[a] = d;     look[a] = c;     int idx = 0;     for (auto go : vv[a]) {       if (go.first == p) {         idx++;         continue;       }       dfs(go.first, d + 1, a, V[a][idx]);       idx++;     }   }   int lca(int a, int b) {     if (dep[a] < dep[b]) swap(a, b);     for (int i = 0; i < 19; i++) {       if (((dep[a] - dep[b]) >> i) & 1) {         a = lcc[i][a];       }     }     if (a == b) return a;     for (int i = 19 - 1; i >= 0; i--) {       if (lcc[i][a] != lcc[i][b]) {         a = lcc[i][a];         b = lcc[i][b];       }     }     return lcc[0][a];   }   inline st go(int a, int f) {     st r;     if (f == 0) {       return r;     }     for (int i = 0; i < 19; i++) {       if ((f >> i) & 1) {         r = merge(r, pat[i][a]);         a = lcc[i][a];       }     }     return r;   }   int qq;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       long long int w;       scanf( %lld , &w);       v.push_back(w);       vv[n].push_back(make_pair(i, w));     }     for (int i = 1; i < n; i++) {       long long int a, b, c, d;       scanf( %lld%lld%lld%lld , &a, &b, &c, &d);       a--;       b--;       if (a < b) mp[make_pair(a, b)] = c;       if (b < a) mp[make_pair(b, a)] = c;       if (a < b) mp[make_pair(a + n, b + n)] = d;       if (b < a) mp[make_pair(b + n, a + n)] = d;       g[a].push_back(make_tuple(b, c, d));       g[b].push_back(make_tuple(a, c, d));       vv[a].push_back(make_pair(b, c + d));       vv[b].push_back(make_pair(a, c + d));       V[a].push_back(make_pair(c, d));       V[b].push_back(make_pair(c, d));     }     memset(dist, -1, sizeof(dist));     dist[n] = 0;     q.push(make_pair(0, n));     while (!q.empty()) {       int b = q.top().second;       long long int cc = q.top().first;       q.pop();       if (dist[b] != cc) continue;       for (auto go : vv[b]) {         if (dist[go.first] == -1 || dist[go.first] > dist[b] + go.second) {           dist[go.first] = dist[b] + go.second;           q.push(make_pair(dist[go.first], go.first));         }       }     }     dfs(0);     for (int j = 0; j < n; j++) {       pat[0][j].set_node(j);     }     for (int i = 0; i + 1 < 19; i++) {       for (int j = 0; j < n; j++) {         if (lcc[i][j] != -1) {           lcc[i + 1][j] = lcc[i][lcc[i][j]];           pat[i + 1][j] = merge(pat[i][j], pat[i][lcc[i][j]]);         } else {           lcc[i + 1][j] = -1;         }       }     }     cin >> qq;     while (qq--) {       int l, r;       scanf( %d%d , &l, &r);       l--;       r--;       l = (l / 2) + (l % 2) * n;       r = (r / 2) + (r % 2) * n;       int L = l;       int R = r;       if (L >= n) L -= n;       if (R >= n) R -= n;       swap(l, L);       swap(r, R);       int lc = lca(l, r);       st ret = merge(go(l, dep[l] - dep[lc] + 1), go(r, dep[r] - dep[lc]).flip());       printf( %lld n , ret.dp[L >= n][R >= n]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int h1, m1, h2, m2;     scanf( %d:%d %d:%d , &h1, &m1, &h2, &m2);     if (m1 < m2) m1 += 60, h2++;     if (h1 < h2) h1 += 24;     printf( %.2d:%.2d n , h1 - h2, m1 - m2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(const pair<long long, long long> &a,            const pair<long long, long long> &b) {     return (a.second < b.second);   }   long long power(long long x, long long y) {     long long res = 1;     x = x;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x);       y = y >> 1;       x = (x * x);     }     return res;   }   int main() {     long long t = 1, i, j, k, x, y, z, n, m;     cin >> t;     while (t--) {       cin >> n;       if (n == 1) {         cout <<  0 n ;         continue;       }       long long p = 8;       long long sum = 0;       for (i = 1; i <= (n / 2); i++) {         p = 8 * i;         sum = sum + (p * i);       }       cout << sum << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> primes;   bool vis[20000000];   vector<long long> a, b;   long long v[20000000];   void genp() {     for (long long i = 2; i < 20000000; i++) {       if (!vis[i]) {         primes.push_back(i);         for (long long j = 1; j * i < 20000000; j++) vis[j * i] = 1;       }     }   }   void mark(long long x) {     for (long long i = 2; i <= sqrt(x); i++) {       long long z = i;       if (x % z == 0) {         long long f = x / z;         for (long long j = 1; j * z < 20000000; j++) v[j * z] = 1;         for (long long j = 1; j * f < 20000000; j++) v[j * f] = 1;       }     }     long long z = x;     for (long long j = 1; j * z < 20000000; j++) v[j * z] = 1;   }   int main() {     ios_base::sync_with_stdio(0);     genp();     v[0] = 1;     v[1] = 1;     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       a.push_back(x);     }     bool ok = 0;     long long s = 2;     for (long long i = 0; i < n; i++) {       if (!ok) {         long long x = a[i];         if (!v[x]) {           b.push_back(x);           mark(x);         } else {           ok = 1;           for (long long k = x + 1; k <= 1e9; k++) {             if (!v[k]) {               b.push_back(k);               mark(k);               break;             }           }         }       } else {         for (s = s; s <= 1e9; s++) {           if (!v[s]) {             b.push_back(s);             mark(s);             break;           }         }       }     }     for (long long i = 0; i < n; i++) cout << b[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = int(2e9) + 9;   const double eps = 1e-9;   const double pi = 3.14159265358979323846264338327950;   int main() {     int p, n;     int a[305];     scanf( %d %d , &p, &n);     memset(a, -1, sizeof(a));     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       if (a[x % p] != -1) {         printf( %d n , i);         return 0;       }       a[x % p] = 1;     }     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char a[55][55];   int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};   vector<pair<int, int> > G[2555];   int vis[2555];   int bfs(int i, int j) {     memset(vis, -1, sizeof vis);     deque<pair<int, int> > q;     q.push_front(make_pair(i * m + j, 0));     int mx = 0;     while (!q.empty()) {       pair<int, int> cur = q.front();       q.pop_front();       int u = cur.first, w = cur.second;       vis[u] = w;       if (a[u / m][u % m] ==  B ) mx = w;       for (int k = 0; k < (int)G[u].size(); k++) {         int v = G[u][k].first, w2 = G[u][k].second;         if (vis[v] == -1) {           vis[v] = w + w2;           if (w2 == 0)             q.push_front(make_pair(v, w));           else             q.push_back(make_pair(v, w + w2));         }       }     }     return mx;   }   int main() {     bool ok = 0;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , a[i]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (a[i][j] ==  B ) ok = 1;     if (!ok) {       printf( 0 n );       return 0;     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         for (int k = 0; k < 4; k++) {           int newI = i + dx[k], newJ = j + dy[k];           if (newI < 0 || newJ < 0 || newI >= n || newJ >= m) continue;           if (a[i][j] == a[newI][newJ])             G[i * m + j].push_back(make_pair(newI * m + newJ, 0));           else             G[i * m + j].push_back(make_pair(newI * m + newJ, 1));         }       }     int mn = n * m + 1;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         mn = min(mn, bfs(i, j));       }     printf( %d n , mn + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cnt[3], dp[1010][4], n, p;   struct seg {     long long l, r, v;     seg() {}     seg(long long _l, long long _r, long long _v) { l = _l, r = _r, v = _v; }   };   vector<seg> v;   long long cal(long long x, long long y) {     if (v[x].v != 0 && v[y].v != 0) return 0;     if (v[x].v != 1 && v[y].v != 1) return 1;     return 2;   }   long long get(long long x, long long y) {     if (x % 3 == 0) return -(!(y & 1));     if (x % 3 == 2) return (y & 1);     return 0;   }   long long npair(long long a, long long b) {     return ((a + b) * (a - b + 1) / 2) % 1000000007;   }   void upd(long long& a, long long b) { a = (a + b) % 1000000007; }   int main() {     cin >> n >> p;     v.push_back(seg(1, 2, 0));     long long nxt = 3;     while (nxt <= p) {       long long x = nxt / 3, y = nxt - x, id1, id2;       for (long long i = 0; i < ((long long)(v).size()); i++) {         if (v[i].l <= x && x <= v[i].r) id1 = i;         if (v[i].l <= y && y <= v[i].r) id2 = i;       }       long long g = cal(id1, id2);       long long cnt1 = v[id1].r - x + 1;       long long cnt2 = v[id2].r - y + 1;       cnt1 = (cnt1 - 1) * 3 + 3 - (nxt % 3);       cnt2 = cnt2 + cnt2 / 2 + get(nxt, cnt2);       long long end = nxt + min(cnt1, cnt2) - 1;       while (!v.empty() && g == v[((long long)(v).size()) - 1].v)         nxt = v[((long long)(v).size()) - 1].l, v.pop_back();       v.push_back(seg(nxt, end, g));       nxt = end + 1;     }     for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)       upd(cnt[it->v], npair(p - it->l, p - min(p, it->r)));     dp[0][0] = 1;     for (long long i = 1; i <= n; i++)       for (long long j = 0; j < 4; j++)         for (long long k = 0; k < 3; k++)           upd(dp[i][j], dp[i - 1][j ^ k] * cnt[k]);     cout << (dp[n][1] + dp[n][2] + dp[n][3]) % 1000000007 << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct str {     long long cnt[26], len = 0;     pair<long long, long long> pref = {0, 0}, suf = {0, 0};     str() { memset(cnt, 0, sizeof(cnt)); }     str(string s) {       memset(cnt, 0, sizeof(cnt));       len = ((int)s.size());       pair<long long, long long> mx = {0, 0};       for (int i = 0; i < ((int)s.size()); ++i) {         if (s[i] -  a  != mx.first)           mx = {s[i] -  a , 1};         else           mx.second++;         cnt[mx.first] = max(cnt[mx.first], mx.second);       }       mx = {s[0] -  a , 0};       for (int i = 0; i < ((int)s.size()); ++i) {         if (s[i] -  a  == mx.first)           mx.second++;         else           break;       }       pref = mx;       mx = {s[((int)s.size()) - 1] -  a , 0};       for (int i = ((int)s.size()) - 1; i >= 0; --i) {         if (s[i] -  a  == mx.first)           mx.second++;         else           break;       }       suf = mx;     }   };   int n;   void debug(str x) {     cerr <<  x.len           <<   =   << x.len << endl;     cerr <<  x.pref.first           <<   =   << x.pref.first << endl;     cerr <<  x.pref.second           <<   =   << x.pref.second << endl;     cerr <<  x.suf.first           <<   =   << x.suf.first << endl;     cerr <<  x.suf.second           <<   =   << x.suf.second << endl;     for (int i = 0; i < 26; ++i) cout << x.cnt[i] <<    ;     cout <<   n n ;   }   str merge(str A, str B) {     str ret = str();     ret.pref = B.pref;     ret.suf = B.suf;     if (A.len + B.len > 1e9)       ret.len = -1;     else       ret.len = A.len * B.len + A.len + B.len;     if (A.pref.second == A.len && B.pref.second == B.len) {       if (A.pref.first == B.pref.first) {         ret.pref = ret.suf = {A.pref.first, ret.len};         ret.cnt[A.pref.first] = ret.len;       } else {         ret.cnt[A.pref.first] = 1;         ret.cnt[B.pref.first] = B.pref.second;       }     } else {       for (int i = 0; i < 26; ++i) {         ret.cnt[i] = B.cnt[i];         if (A.cnt[i]) ret.cnt[i] = max(ret.cnt[i], 1LL);         if (B.pref.second == B.len && i == B.pref.first && A.cnt[i] > 0) {           ret.cnt[i] = max(ret.cnt[i], B.len * A.cnt[i] + A.cnt[i] + B.len);           if (A.pref.first == B.pref.first)             ret.pref = {A.pref.first,                         B.len * A.pref.second + A.pref.second + B.len};           if (A.suf.first == B.pref.first)             ret.suf = {A.suf.first, B.len * A.suf.second + A.suf.second + B.len};         } else {           if (i == B.pref.first)             ret.cnt[i] = max(ret.cnt[i], B.pref.second + (A.cnt[i] > 0));           if (i == B.suf.first)             ret.cnt[i] = max(ret.cnt[i], B.suf.second + (A.cnt[i] > 0));           if (B.pref.first == B.suf.first && B.pref.first == i && A.cnt[i] > 0)             ret.cnt[i] = max(ret.cnt[i], B.pref.second + B.suf.second + 1);         }         if (ret.cnt[i] > 1e9) ret.cnt[i] = 1;       }     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     string s;     cin >> s;     str sol = str(s);     for (int i = 0; i < n - 1; ++i) {       cin >> s;       str x = str(s);       sol = merge(sol, x);     }     long long pr = 0;     for (int i = 0; i < 26; ++i) pr = max(pr, sol.cnt[i]);     cout << pr;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   const int inf = 0x7FFFFFFF;   template <class T>   inline void checkmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   void show(T a, int n) {     for (int i = 0; i < n; ++i) cout << a[i] <<    ;     cout << endl;   }   template <class T>   void show(T a, int r, int l) {     for (int i = 0; i < r; ++i) show(a[i], l);     cout << endl;   }   int sgn(double x) { return x < -eps ? -1 : x > eps; }   int main() {     double y1, y2, yw, xb, yb, r;     while (cin >> y1 >> y2 >> yw >> xb >> yb >> r) {       if (sgn(y2 - y1 - 2 * r) < 0) {         puts( -1 );         continue;       } else {         double y = (y1 + r), yyw = yw - r;         double ax = (yyw - y) / (yyw - y + yyw - yb) * xb;         double ds = (y2 - y) * ax / (sqrt(sqr(ax) + sqr(yyw - y)));         if (sgn(ds - r) <= 0) {           puts( -1 );           continue;         }         printf( %.12lf n , ax);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   int a[N], ret[N];   pair<pair<int, int>, int> qi[N];   int pen[N];   void update(int ind, int add) {     while (ind < N) {       pen[ind] += add;       ind += ind & (-ind);     }   }   int query(int ind) {     int ret = 0;     while (ind > 0) {       ret = ret + pen[ind];       ind = ind & (ind - 1);     }     return ret;   }   pair<int, int> seg[N * 4];   int lazy[N * 4];   void init(int pos, int l, int r) {     if (l == r) {       seg[pos] = {a[l], l};       return;     }     int mid = (l + r) / 2;     init(pos * 2, l, mid);     init(pos * 2 + 1, mid + 1, r);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void push(int pos, int l, int r) {     if (lazy[pos] != 0) {       seg[pos].first += lazy[pos];       if (l != r) {         lazy[pos * 2] += lazy[pos];         lazy[pos * 2 + 1] += lazy[pos];       }       lazy[pos] = 0;     }   }   pair<int, int> query(int pos, int l, int r, int ql, int qr) {     push(pos, l, r);     if (r < ql || qr < l) {       return {-1e9, 0};     }     if (ql <= l && r <= qr) {       return seg[pos];     }     int mid = (l + r) / 2;     return max(query(pos * 2, l, mid, ql, qr),                query(pos * 2 + 1, mid + 1, r, ql, qr));   }   void update(int pos, int l, int r, int ql, int qr, int val) {     push(pos, l, r);     if (r < ql || qr < l) {       return;     }     if (ql <= l && r <= qr) {       lazy[pos] = val;       push(pos, l, r);       return;     }     int mid = (l + r) / 2;     update(pos * 2, l, mid, ql, qr, val);     update(pos * 2 + 1, mid + 1, r, ql, qr, val);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void update(int pos, int l, int r, int ind, pair<int, int> val) {     push(pos, l, r);     if (r < ind || ind < l) {       return;     }     if (l == r) {       seg[pos] = val;       return;     }     int mid = (l + r) / 2;     update(pos * 2, l, mid, ind, val);     update(pos * 2 + 1, mid + 1, r, ind, val);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void solve() {     int n, q;     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);     }     int t = 0;     for (int i = 1; i <= n; ++i) {       if (i - t <= a[i] && a[i] <= i) {         a[i] = i - a[i] - t;         ++t;         update(i, 1);       } else {         a[i] = -1e9;       }     }     init(1, 1, n);     for (int i = 0; i < q; ++i) {       scanf( %d %d  , &qi[i].first.first, &qi[i].first.second);       qi[i].second = i;     }     sort(qi, qi + q);     int pos = 0;     for (int i = 0; i < q; ++i) {       int x = qi[i].first.first, y = qi[i].first.second;       while (pos + 1 <= x) {         ++pos;         if (query(1, 1, n, pos, pos).first == 0) {           update(1, 1, n, pos + 1, n, 1);           update(1, 1, n, pos, {-1e9, 0});           update(pos, -1);         }         while (true) {           pair<int, int> qr = query(1, 1, n, 1, n);           if (qr.first <= 0) {             break;           }           update(qr.second, -1);           update(1, 1, n, qr.second, {-1e9, 0});           update(1, 1, n, qr.second, n, 1);         }       }       ret[qi[i].second] = query(n - y);     }     for (int i = 0; i < q; ++i) {       printf( %d n , ret[i]);     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   int n, m;   long long t[MAX << 2], tag[MAX << 2];   int sz = 1;   inline void Period(int k, int l, int r, int a, int b, long long x) {     if (a > r || b < l) return;     if (a >= l && b <= r) {       tag[k] = t[k] = x;       return;     }     Period(k << 1, l, r, a, (a + b) >> 1, x);     Period((k << 1) + 1, l, r, ((a + b) >> 1) + 1, b, x);     t[k] = max(t[k << 1], t[(k << 1) + 1]);   }   inline long long Query(int k, int l, int r, int a, int b) {     if (tag[k]) {       t[k] = tag[k];       tag[k << 1] = max(tag[k << 1], tag[k]);       tag[(k << 1) + 1] = max(tag[(k << 1) + 1], tag[k]);       tag[k] = 0;     }     if (a > r || b < l) return 0;     if (a >= l && b <= r) return t[k];     return max(Query(k << 1, l, r, a, (a + b) >> 1),                Query((k << 1) + 1, l, r, ((a + b) >> 1) + 1, b));   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     while (sz < n) sz <<= 1;     for (int i = 1; i <= n; i++) {       long long x;       cin >> x;       Period(1, i, i, 1, sz, x);     }     cin >> m;     while (m--) {       int x;       long long h;       cin >> x >> h;       long long res = Query(1, 1, x, 1, sz);       cout << res << endl;       Period(1, 1, x, 1, sz, res + h);     }     return 0;   }


#include <bits/stdc++.h>   int n, m;   char a[10000];   char b[10000];   int main() {     scanf( %s %s , a, b);     n = strlen(a);     m = strlen(b);     int minn = 10000000;     for (int i = -m + 1; i < n; i++) {       int count = 0;       for (int j = 0; j < m; j++) {         if (i + j < 0 || i + j >= n) {           count++;         } else if (a[i + j] != b[j])           count++;       }       if (count < minn) minn = count;     }     printf( %d n , minn);   }


#include <bits/stdc++.h>   using namespace std;   mt19937 gen(time(NULL));   const long double eps = 1e-9;   const int inf = 1e9;   const int mod = 1e9 + 7;   const long long infinity = 2 * 1e18;   vector<int> g[100005];   int n, m, q;   int p2[100005], d[100005], cmp[100005];   int rt[100005];   vector<int> plen[100005];   vector<int> cs[100005];   vector<long long> csp[100005];   int s[100005];   void dfs_cl(int u, int e, int cl) {     cmp[u] = cl;     cs[cl].push_back(u);     for (auto v : g[u])       if (v != e) dfs_cl(v, u, cl);   }   int dfs(int u, int e) {     for (auto v : g[u])       if (v != e) plen[u].push_back(dfs(v, u) + 1);     int maxc = 0;     sort(plen[u].begin(), plen[u].end(), greater<int>());     for (auto v : plen[u]) maxc = max(v, maxc);     if (plen[u].size()) {       if (plen[u].size() == 1)         d[cmp[u]] = max(d[cmp[u]], plen[u].back());       else         d[cmp[u]] = max(d[cmp[u]], plen[u][0] + plen[u][1]);     }     return p2[u] = maxc;   }   void path(int u, int e, int carry) {     p2[u] = max(p2[u], carry);     if (plen[u].size() <= 1) {       for (auto v : g[u])         if (v != e) path(v, u, carry + 1);       return;     }     for (auto v : g[u])       if (v != e) {         int x = p2[v] + 1;         int t = 0;         if (x == plen[u][0]) t++;         path(v, u, max(carry + 1, plen[u][t] + 1));       }   }   map<pair<int, int>, double> memo;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> q;     int u, v;     for (int i = 0; i < m; i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     int cl = 0;     for (int i = 1; i <= n; i++)       if (!cmp[i]) {         dfs_cl(i, i, ++cl);         rt[cl] = i;       }     for (int i = 1; i <= n; i++) s[cmp[i]]++;     for (int i = 1; i <= cl; i++) dfs(rt[i], -1);     for (int i = 1; i <= cl; i++) path(rt[i], -1, 0);     for (int i = 1; i <= cl; i++)       sort(cs[i].begin(), cs[i].end(),            [&](int a, int b) { return p2[a] < p2[b]; });     for (int i = 1; i <= cl; i++)       for (auto v : cs[i])         csp[i].push_back(csp[i].size() ? csp[i].back() + p2[v] + 1 : p2[v] + 1);     for (int i = 0; i < q; i++) {       cin >> u >> v;       u = cmp[u], v = cmp[v];       if (u == v)         cout << -1 <<   n ;       else {         if (s[u] > s[v]) swap(u, v);         if (memo.count(make_pair(u, v)))           cout << memo[make_pair(u, v)] <<   n ;         else {           long long ans = 0;           int dp = max(d[u], d[v]);           for (auto x : cs[u]) {             if (cs[v].size() == 1) {               ans += max(dp, p2[x] + p2[cs[v].back()] + 1);               continue;             }             int l = -1, r = cs[v].size() - 1;             while (l < r - 1) {               int m = (l + r) / 2;               if (p2[cs[v][m]] + p2[x] + 1 < dp)                 l = m;               else                 r = m;             }             ans += (r * 1LL * dp + p2[x] * 1LL * (s[v] - r) + csp[v].back() -                     (r ? csp[v][r - 1] : 0));           }           memo[make_pair(u, v)] = (double)ans / (s[v] * 1LL * s[u]);           cout << fixed << setprecision(10) << memo[make_pair(u, v)] <<   n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   const int inf = 2147483647;   const long double pi = acos(-1);   inline unsigned long long read() {     unsigned long long data = 0, w = 1;     char ch = getchar();     while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar();     if (ch ==  - ) w = -1, ch = getchar();     while (ch <=  9  && ch >=  0 ) data = data * 10 + ch - 48, ch = getchar();     return data * w;   }   inline void file() {     freopen(          a           .in ,          r , stdin);     freopen(          a           .out ,          w , stdout);   }   int n;   inline int check(unsigned long long x) {     int mx = 1;     for (int i = 2; 1ll * i * i <= x; i++)       if (x % i == 0)         mx = max((unsigned long long)mx, max((unsigned long long)i, x / i));     return mx;   }   int main() {     n = read();     for (int i = 1; i <= n; i++) {       unsigned long long x = read(), r = x;       int cnt = 0;       while (r) {         cnt++;         r >>= 1;       }       if (x == (1ll << cnt) - 1)         printf( %d n , check(x));       else         printf( %I64d n , (1ll << cnt) - 1);     }     return 0;   }