#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   using namespace std;   const long long N = 1000 + 5, L = 12, inf = LLONG_MAX;   long long n, m, k;   long long a[N][N], par[N * N], sz[N * N], res[N][N], hh, cnt;   bool mark[N][N], vis[N][N];   long long mp[N * N];   vector<pair<long long, long long> > vec, all;   set<long long> s;   long long H[] = {0, 0, -1, 1};   long long G[] = {1, -1, 0, 0};   inline void er(long long x) {     mp[x]--;     if (mp[x] == 0) {       s.erase(-x);     }     return;   }   inline void ad(long long x) {     mp[x]++;     if (mp[x] == 1) {       s.insert(-x);     }     return;   }   long long find(long long a) {     if (par[a] == a) return a;     return par[a] = find(par[a]);   }   inline void _union(long long a, long long b) {     a = find(a);     b = find(b);     if (a == b) return;     if (sz[a] < sz[b]) swap(a, b);     er(sz[a]);     er(sz[b]);     par[b] = a;     sz[a] += sz[b];     ad(sz[a]);     return;   }   void dfs(long long x, long long y) {     vis[x][y] = 1;     all.push_back({x, y});     for (long long k = 0; k < 4; k++) {       long long i = x + H[k], j = y + G[k];       if (i >= 0 && j >= 0 && i < n && j < m) {         if (!vis[i][j] && a[i][j] >= hh) {           dfs(i, j);         }       }     }     return;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     cout.tie(nullptr);     ;     cin >> n >> m >> k;     for (long long i = 0; i < n * m; i++) par[i] = i, sz[i] = 1;     s.insert(-1);     mp[1] = n * m;     for (long long i = 0; i < n; i++)       for (long long j = 0; j < m; j++) {         cin >> a[i][j];         vec.push_back({a[i][j], i * m + j});       }     sort(vec.begin(), vec.end(), greater<pair<long long, long long> >());     long long ans = -1, xx = -1, yy = -1;     for (auto p : vec) {       long long x = p.second / m, y = p.second % m, h = p.first;       mark[x][y] = 1;       for (long long k = 0; k < 4; k++) {         long long i = x + H[k], j = y + G[k];         if (i >= 0 && j >= 0 && i < n && j < m && mark[i][j]) {           _union(i * m + j, p.second);         }       }       if (k % h == 0) {         long long mx = sz[find(p.second)];         if (mx >= k / h) {           ans = mx;           hh = h;           xx = x;           yy = y;           break;         }       }     }     if (ans == -1) return cout <<  NO n , 0;     dfs(xx, yy);     cout <<  YES n ;     long long sum = 0;     for (auto p : all) {       res[p.first][p.second] = hh;       sum += hh;       if (sum == k) break;     }     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) cout << res[i][j] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long dp[1501][1501];   long long a[1510][1501];   long long func(long long here, long long here1) {     if (here1 > m) return -999999999999999;     if (here1 < 1) return -999999999999999;     if (here == n + 1) return 0;     long long &ret = dp[here][here1];     if (ret != -1) return ret;     ret = -999999999999999;     if (here % 2) {       ret = max(ret, func(here, here1 + 1));       ret = max(ret, func(here + 1, here1 - 1) + a[here][here1]);     } else {       ret = max(ret, func(here, here1 - 1));       ret = max(ret, func(here + 1, here1 + 1) + a[here][here1]);     }     return ret;   }   int main() {     memset(dp, -1, sizeof(dp));     scanf( %lld %lld , &n, &m);     for (int i = 1; i <= n; i++) {       for (int y = 1; y <= m; y++) {         scanf( %lld , &a[i][y]);         a[i][y] += a[i][y - 1];       }     }     printf( %lld n , func(1, 1));   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, i, j, k, mod, dp[605][605], rd[605], xs[605][605], ans = 1;   vector<long long> ts, bi[605], nrd, ncd;   long long gcd(long long x, long long y) {     if (y == 0) return x;     return gcd(y, x % y);   }   long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }   void dfs(int x) {     int i;     rd[x] = -1;     ts.push_back(x);     for (i = 0; i < bi[x].size(); i++) {       rd[bi[x][i]]--;       if (!rd[bi[x][i]]) {         dfs(bi[x][i]);       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> mod;     for (i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       bi[x].push_back(y);       rd[y]++;     }     for (i = 1; i <= n; i++) {       if (rd[i] == 0) nrd.push_back(i);       if (bi[i].size() == 0) ncd.push_back(i);     }     for (i = 0; i < nrd.size(); i++) {       dfs(nrd[i]);       dp[nrd[i]][nrd[i]] = 1;     }     for (i = 0; i < ts.size(); i++) {       for (j = 0; j < ts.size(); j++) {         for (k = 0; k < bi[ts[j]].size(); k++) {           dp[ts[i]][bi[ts[j]][k]] += dp[ts[i]][ts[j]];           dp[ts[i]][bi[ts[j]][k]] %= mod;         }       }     }     for (i = 0; i < nrd.size(); i++) {       for (j = 0; j < ncd.size(); j++) {         xs[i][j] = dp[nrd[i]][ncd[j]];       }     }     long long cnt = 0;     for (i = 0; i < ncd.size(); i++) {       for (j = i + 1; j < nrd.size(); j++) {         while (xs[j][i]) {           long long t = -xs[i][i] / xs[j][i];           for (k = i; k < nrd.size(); k++) {             xs[i][k] += t * xs[j][k];             xs[i][k] %= mod;             xs[i][k] += mod;             xs[i][k] %= mod;           }           for (k = i; k < nrd.size(); k++) {             swap(xs[i][k], xs[j][k]);           }           cnt++;         }       }     }     if (cnt & 1) ans = mod - 1;     for (i = 0; i < nrd.size(); i++) {       ans *= xs[i][i];       ans %= mod;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     string s;     cin >> s;     char zero =  0 ;     s =  00  + s;     long long n = (long long)s.size();     for (long long i = 0; i < n - 2; i++) {       long long temp = 100 * (s[i] - zero);       for (long long j = i + 1; j < n - 1; j++) {         temp += (10 * (s[j] - zero));         for (long long k = j + 1; k < n; k++) {           temp += (s[k] - zero);           if (temp % 8 == 0) {             cout <<  YES  <<   n ;             cout << temp <<   n ;             return 0;           }           temp -= (s[k] - zero);         }         temp -= (10 * (s[j] - zero));       }     }     cout <<  NO  <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int Q;     cin >> Q;     while (Q--) {       long long n, a, b, ans;       cin >> n >> a >> b;       double x = b / 2.0;       if (x > a) {         ans = a * n;         cout << ans << endl;       } else {         if (n % 2 == 0) {           ans = n / 2 * b;           cout << ans << endl;         } else {           ans = (n - 1) / 2 * b + a;           cout << ans << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename S, typename T>   ostream& operator<<(ostream& out, pair<S, T> const& p) {     out <<  (  << p.first <<  ,   << p.second <<  ) ;     return out;   }   template <typename T>   ostream& operator<<(ostream& out, vector<T> const& v) {     int l = v.size();     for (int i = 0; i < l - 1; i++) out << v[i] <<    ;     if (l > 0) out << v[l - 1];     return out;   }   template <typename T>   void trace(const char* name, T&& arg1) {     cout << name <<   :   << arg1 << endl;   }   template <typename T, typename... Args>   void trace(const char* names, T&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     trace(comma + 1, args...);   }   const int N = 100100;   int pt;   bool cmp(const int& i, const int& j) {     if (i < pt and j < pt) {       return i < j;     } else if (i > pt and j > pt) {       return i < j;     } else {       return i > j;     }   }   vector<int> nxt[N];   int a[N];   int b[N];   int lft[N];   int rgt[N];   pair<int, int> tosort[N];   bool importance(const int& i, const int& j) { return tosort[i] < tosort[j]; }   vector<int> face[N];   vector<int> g[N];   map<pair<int, int>, int> id;   set<pair<int, int> > used;   void buildFace(int from, int to, int f) {     face[f].push_back(to);     used.insert(make_pair(from, to));     pt = to;     vector<int>& v = nxt[to];     int nxt = *upper_bound(v.begin(), v.end(), from, cmp);     if (used.count(make_pair(to, nxt))) return;     buildFace(to, nxt, f);   }   int vis[N];   int usd[N];   int sz[N];   int lvl[N];   void dfs(int x, int h) {     sz[x] = 1;     vis[x] = h;     for (auto it = (g[x]).begin(); it != (g[x]).end(); ++it) {       if (usd[*it]) continue;       if (vis[*it] == h) continue;       dfs(*it, h);       sz[x] += sz[*it];     }   }   int findCentroid(int x, int n, int prev) {     for (auto it = (g[x]).begin(); it != (g[x]).end(); ++it) {       if (vis[*it] != vis[x] or *it == prev) continue;       if (sz[*it] > n / 2) return findCentroid(*it, n, x);     }     return x;   }   void decompose(int x, int h) {     dfs(x, h);     int c = findCentroid(x, sz[x], -1);     lvl[c] = h;     usd[c] = 1;     for (auto it = (g[c]).begin(); it != (g[c]).end(); ++it) {       if (!usd[*it]) decompose(*it, h + 1);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     if (m == 0) {       cout << 1 << endl;       return 0;     }     if (m == 1) {       cout <<  1 2  << endl;       return 0;     }     nxt[1].push_back(n);     nxt[n].push_back(1);     for (int i = 2; i <= n; i++) {       nxt[i].push_back(i - 1);       nxt[i - 1].push_back(i);     }     for (int i = 0; i < m; i++) {       cin >> a[i] >> b[i];       if (a[i] < b[i]) swap(a[i], b[i]);       id[make_pair(a[i], b[i])] = id[make_pair(b[i], a[i])] = i;       nxt[a[i]].push_back(b[i]);       nxt[b[i]].push_back(a[i]);     }     for (int i = 1; i <= n; i++) {       pt = i;       sort(nxt[i].begin(), nxt[i].end(), cmp);     }     int cur = 0;     for (int i = 0; i < m; i++) {       if (!used.count(make_pair(a[i], b[i]))) {         face[++cur].push_back(a[i]);         buildFace(a[i], b[i], cur);         for (int j = 1; j < face[cur].size(); j++) {           int x = face[cur][j - 1], y = face[cur][j];           if (x - 1 != y and !(x == 1 and y == n)) {             int k = id[make_pair(x, y)];             if (x > y)               lft[k] = cur;             else               rgt[k] = cur;           }         }       }       if (!used.count(make_pair(b[i], a[i]))) {         face[++cur].push_back(b[i]);         buildFace(b[i], a[i], cur);         for (int j = 1; j < face[cur].size(); j++) {           int x = face[cur][j - 1], y = face[cur][j];           if (x - 1 != y and !(x == 1 and y == n)) {             int k = id[make_pair(x, y)];             if (x > y)               lft[k] = cur;             else               rgt[k] = cur;           }         }       }       int x = lft[i], y = rgt[i];       assert(x > 0 and y > 0);       g[x].push_back(y);       g[y].push_back(x);     }     for (int i = 1; i <= cur; i++) {       int mx = 0, smx = 0;       for (int j = 1; j < face[i].size(); j++) {         int x = face[i][j];         if (x > mx) {           smx = mx;           mx = x;         } else if (x > smx)           smx = x;       }       tosort[i] = make_pair(mx, smx);     }     vector<int> faces;     for (int i = 1; i <= cur; i++) faces.push_back(i);     sort(faces.begin(), faces.end(), importance);     decompose(1, 1);     for (int x : faces) {       cout << lvl[x] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, x, a, b, i, j, m[1000005], f[1000005];   multiset<long long> s;   pair<long long, long long> d[1000005];   vector<long long> v;   int main() {     ios::sync_with_stdio(0);     cin >> n >> k >> x;     while (i < n) {       cin >> m[i];       i++;     }     i = 0;     while (i < k) {       cin >> d[i].first;       d[i].second = i + 1;       i++;     }     i = 0;     sort(m, m + n);     sort(d, d + k);     s.insert(1000000001);     while (i < n) {       if (x * m[i] + x < i + 1)         return cout << -1, 0;       else         f[i] = x * m[i] + x - i - 1, s.insert(f[i]);       i++;     }     i = 0;     while (i < n + 1) {       while (j < k && a < *s.begin()) {         if (x * d[j].first + x >= i + a + 1) a++, v.push_back(d[j].second);         j++;       }       s.erase(s.lower_bound(f[i]));       i++;     }     i = 0;     cout << a << endl;     while (i < a) {       cout << v[i] <<    ;       i++;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int i, count = 0;     string str;     cin >> str;     int len = str.size();     for (i = 0; i < len; i++) {       if (str[i] ==  W  && str[i + 1] ==  U  && str[i + 2] ==  B ) {         i += 2;         if (count > 0) cout <<    ;       } else {         cout << str[i];         count = 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 2e9;   int32_t main() {     cin.tie(0)->sync_with_stdio(0);     long long n, m;     string s, t;     cin >> n >> m >> s >> t;     vector<long long> pi(m, 0), nxt(m, 0);     for (long long i = 1; i < m; i++) {       {         t[i] = char( 0  + (t[i] -  0 ) ^ 1);         long long j = pi[i - 1];         while (j > 0 and t[i] != t[j]) {           j = pi[j - 1];         }         if (t[i] == t[j]) j++;         nxt[i] = j;         t[i] = char( 0  + (t[i] -  0 ) ^ 1);       }       {         long long j = pi[i - 1];         while (j > 0 and t[i] != t[j]) {           j = pi[j - 1];         }         if (t[i] == t[j]) j++;         pi[i] = j;       }     }     s =  #  + s;     long long dp[n + 1][m][n - m + 2];     fill(**dp, **dp + sizeof dp / sizeof(long long), inf);     dp[0][0][0] = 0;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         for (long long k = 0; k < n - m + 2; k++) {           if (dp[i][j][k] == inf) continue;           if (j == m - 1) {             dp[i + 1][pi.back()][k + 1] = min(dp[i + 1][pi.back()][k + 1],                                               dp[i][j][k] + (s[i + 1] != t[j]));           } else {             dp[i + 1][j + 1][k] =                 min(dp[i + 1][j + 1][k], dp[i][j][k] + (s[i + 1] != t[j]));           }           dp[i + 1][nxt[j]][k] =               min(dp[i + 1][nxt[j]][k], dp[i][j][k] + (s[i + 1] == t[j]));         }       }     }     for (long long i = 0; i < n - m + 2; i++) {       long long mn = inf;       for (long long j = 0; j < m; j++) {         mn = min(mn, dp[n][j][i]);       }       if (mn == inf) mn = -1;       cout << mn <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<pair<int, pair<int, int>>> v;       for (int i = 0; i < n; i++) {         int a, b;         cin >> a >> b;         v.push_back(make_pair(a, make_pair(b, i)));       }       sort(v.begin(), v.end());       int g[100001]{}, r = v[0].second.first;       g[v[0].second.second] = 1;       for (int i = 1; i < n; i++) {         if (v[i].first > r) break;         g[v[i].second.second] = 1;         r = max(r, v[i].second.first);       }       bool ans = true;       for (int i = 0; i < n && ans; i++) ans &= g[i];       if (ans) {         cout << -1 <<   n ;         continue;       }       for (int i = 0; i < n; i++) cout << g[i] + 1 <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   char A[300];   int cnt[5];   char data[5] =  ACGT ;   int main() {     int n;     scanf( %d , &n);     scanf( %s , A);     for (int i = 0; A[i]; i++) {       if (A[i] ==  A )         cnt[0]++;       else if (A[i] ==  C )         cnt[1]++;       else if (A[i] ==  G )         cnt[2]++;       else if (A[i] ==  T )         cnt[3]++;       else         cnt[4]++;     }     int big = 0, dif = 0;     ;     for (int i = 0; i < 4; i++) big = max(big, cnt[i]);     for (int i = 0; i < 4; i++) {       cnt[i] = big - cnt[i];       dif += cnt[i];     }     if (dif > cnt[4]) {       printf( === );       return 0;     }     if ((cnt[4] - dif) % 4 != 0) {       printf( === );       return 0;     }     int flag = 0;     for (int i = 0; A[i]; i++) {       if (A[i] ==  ? ) {         flag = 0;         for (int j = 0; j < 4; j++) {           if (cnt[j] > 0) {             A[i] = data[j];             cnt[j]--;             flag = 1;             break;           }         }         if (flag == 0) {           for (int j = 0; j < 4; j++) cnt[j]++;           i--;         }       }     }     printf( %s , A);   }


#include <bits/stdc++.h>   using namespace std;   int a[2];   string ans[2] = { Vladik ,  Valera };   int main() {     cin >> a[0] >> a[1];     int turn = 0;     int sub = 1;     while (true) {       if (a[turn] - sub >= 0) {         a[turn] -= sub;       } else {         break;       }       sub++;       turn = 1 - turn;     }     cout << ans[turn] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, loc[250] = {}, isi[250] = {}, idx = 0;   int kosong() {     for (int i = 0, _n = (n); i < _n; i++)       if (isi[i] == -1) return i;     return -111;   }   int main() {     memset(isi, -1, sizeof isi);     scanf( %d %d , &n, &m);     for (int i = 0, _n = (m); i < _n; i++) {       int a;       scanf( %d , &a);       for (int j = 0, _n = (a); j < _n; j++) {         int x;         scanf( %d , &x);         x--;         isi[x] = idx;         loc[idx] = x;         idx++;       }     }     vector<pair<int, int> > ans;     for (int i = 0, _n = (idx); i < _n; i++)       if (isi[i] != i) {         if (isi[i] == -1) {           ans.push_back(make_pair(loc[i], i));           isi[loc[i]] = -1;           loc[i] = i;           isi[i] = i;         } else {           int k = kosong();           ans.push_back(make_pair(i, k));           isi[k] = isi[i];           loc[isi[i]] = k;           isi[i] = -1;           i--;         }       }     printf( %d n , ans.size());     for (int i = 0, _n = (ans.size()); i < _n; i++)       printf( %d %d n , ans[i].first + 1, ans[i].second + 1);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <class T>   void dbg(const char* xn, T x) {     if (0) cout << xn <<  :   << x <<   n ;   }   template <class T, class... TA>   void dbg(const char* xn, T x, TA... t) {     while (*xn !=  , )       if (0) cout << *xn++;     if (0) cout <<  :   << x <<  , ;     dbg(xn + 1, t...);   }   template <class T>   inline bool upd_max(T& x, T y) {     return y > x ? x = y, true : false;   }   template <class T>   inline bool upd_min(T& x, T y) {     return y < x ? x = y, true : false;   }   const int N = 1000007;   long long n, m1 = 0, m0 = 0, n1 = 0, n0 = 0;   string A, B;   void solve() {     cin >> n;     cin >> A;     A =  #  + A;     cin >> B;     B =  #  + B;     for (long long i = 1; i <= (n); ++i) {       if (A[i] ==  1 )         ++n1;       else if (A[i] ==  0 )         ++n0;       if (A[i] ==  1  && B[i] ==  0 ) ++m1;       if (A[i] ==  0  && B[i] ==  0 ) ++m0;     }     cout << m1 * n0 + m0 * n1 - m1 * m0;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(10);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 5005;   long long int x[sz];   long long int a[sz];   long long int b[sz];   long long int c[sz];   long long int d[sz];   int n, s, e;   const long long int Max = 1e16;   long long int dp[sz][sz];   long long int jumpl(int i) { return x[i] + c[i]; }   long long int jumpr(int i) { return -x[i] + d[i]; }   long long int landfr(int i) { return -x[i] + b[i]; }   long long int landfl(int i) { return x[i] + a[i]; }   long long int calc(int ch, int pl, int pr) {     if (pl < 0 || pr < 0) {       return Max;     }     if (dp[ch][pl] != 0) {       return dp[ch][pl];     }     if (ch == 0) {       if (s == 0) {         return calc(ch + 1, pl, pr + 1) + jumpr(ch);       } else if (e == 0) {         return calc(ch + 1, pl + 1, pr) + landfr(ch);       }       return calc(ch + 1, pl + 1, pr + 1) + landfr(ch) + jumpr(ch);     }     if (ch == n - 1) {       if (s == n - 1 && (pl == 1 && pr == 0)) {         return jumpl(ch);       } else if (e == n - 1 && (pl == 0 && pr == 1)) {         return landfl(ch);       } else if (pl == pr && pl == 1) {         return landfl(ch) + jumpl(ch);       } else {         return Max;       }     } else if (pl == 0 && pr == 0) {       return Max;     }     long long int m;     if (ch == s) {       m = min(calc(ch + 1, pl - 1, pr) + jumpl(ch),               calc(ch + 1, pl, pr + 1) + jumpr(ch));       dp[ch][pl] = m;       return m;     }     if (ch == e) {       m = min(calc(ch + 1, pl, pr - 1) + landfl(ch),               calc(ch + 1, pl + 1, pr) + landfr(ch));       dp[ch][pl] = m;       return m;     }     m = calc(ch + 1, pl + 1, pr + 1) + jumpr(ch) + landfr(ch);     m = min(m, calc(ch + 1, pl - 1, pr - 1) + jumpl(ch) + landfl(ch));     if (pr > 0) {       m = min(m, calc(ch + 1, pl, pr) + jumpr(ch) + landfl(ch));     }     if (pl > 0) {       m = min(m, calc(ch + 1, pl, pr) + jumpl(ch) + landfr(ch));     }     dp[ch][pl] = m;     return m;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> s >> e;     --s, --e;     for (int i = 0; i < n; ++i) {       cin >> x[i];     }     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     for (int i = 0; i < n; ++i) {       cin >> b[i];     }     for (int i = 0; i < n; ++i) {       cin >> c[i];     }     for (int i = 0; i < n; ++i) {       cin >> d[i];     }     cout << calc(0, 0, 0) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   int x[maxn], n, m;   int main() {     int ans = 0, use = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> m;       x[m] = 0;     }     for (int i = 1; i <= n; i++) {       cin >> m;       x[m] = i;     }     for (int i = 1; x[i] <= n && i <= n && x[i + 1] == x[i] + 1; i++) use = i + 1;     if (use && x[use] == n) {       m = 0;       for (int i = use + 1; i <= n; i++) m = max(m, x[i] - (i - use - 1));       if (!m) {         cout << n - use <<   n ;         return 0;       }     }     if (x[1] && x[1] < n) {       for (int i = 2; i <= n; i++) x[i] = max(0, x[i] - x[1]);       ans += x[1];       x[1] = 0;     }     if (x[1] == n) {       m = 0;       for (int i = 2; i <= n; i++) m = max(m, x[i] - (i - 2));       if (m) {         cout << ans + 2 * n <<   n ;         return 0;       } else {         cout << ans + n - 1 <<   n ;         return 0;       }     }     m = 0;     for (int i = 2; i <= n; i++) m = max(m, x[i] - (i - 1));     cout << ans + m + n <<   n ;     return 0;   }


#include <bits/stdc++.h>   const long long int mod = 1000000007;   using namespace std;   void toh_chaliye_shuru_karte_hain() {}   void code_samapti_ki_ghoshna() {}   long long int power(long long int a, long long int b, long long int modi) {     a %= modi;     long long int res = 1;     while (b) {       if (b % 2) {         res = (res * a) % modi;       }       b /= 2;       a = (a * a) % modi;     }     return res;   }   int main() {     toh_chaliye_shuru_karte_hain();     ios::sync_with_stdio(0);     cin.tie(0);     int n, x1, y1, x2, y2;     cin >> n >> x1 >> y1 >> x2 >> y2;     if (x1 == 0 && x2 == 0 || x1 == n && x2 == n) {       cout << abs(y1 - y2) <<   n ;     } else if (y1 == 0 && y2 == 0 || y1 == n && y2 == n) {       cout << abs(x1 - x2) <<   n ;     } else {       int ans = 0;       ans += min(n - y1 + n - y2, y1 + y2);       ans += min(n - x1 + n - x2, x1 + x2);       cout << ans <<   n ;     }     code_samapti_ki_ghoshna();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct segment_change {     static const int SENTINEL = numeric_limits<int>::min();     int to_set;     segment_change(int _to_set = SENTINEL) : to_set(_to_set) {}     bool has_set() const { return to_set != SENTINEL; }     void reset() { to_set = SENTINEL; }     bool has_change() const { return has_set(); }     segment_change combine(const segment_change &other) const {       return other.has_set() ? other : *this;     }   };   struct segment {     int first;     segment(int _first = -1) : first(_first) {}     void apply(int, const segment_change &change) {       if (change.has_set()) first = change.to_set;     }     void join(const segment &other) {       if (first < 0) first = other.first;     }     void join(const segment &a, const segment &b) {       *this = a;       join(b);     }   };   pair<int, int> right_half[32];   struct seg_tree {     int tree_n = 0;     vector<segment> tree;     vector<segment_change> changes;     seg_tree(int n = -1) {       if (n >= 0) init(n);     }     void init(int n) {       tree_n = 1;       while (tree_n < n) tree_n *= 2;       tree.assign(2 * tree_n, segment());       changes.assign(tree_n, segment_change());     }     void build(const vector<segment> &initial) {       int n = initial.size();       init(n);       assert(n <= tree_n);       for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i];       for (int position = tree_n - 1; position > 0; position--)         tree[position].join(tree[2 * position], tree[2 * position + 1]);     }     void push_down(int position, int length) {       if (!changes[position].has_change()) return;       if (2 * position < tree_n) {         changes[2 * position] = changes[2 * position].combine(changes[position]);         changes[2 * position + 1] =             changes[2 * position + 1].combine(changes[position]);       }       tree[2 * position].apply(length / 2, changes[position]);       tree[2 * position + 1].apply(length / 2, changes[position]);       changes[position].reset();     }     void push_all(int a, int b) {       assert(0 <= a && a < b && b <= tree_n);       a += tree_n;       b += tree_n - 1;       for (int up = 31 - __builtin_clz(tree_n); up > 0; up--) {         int x = a >> up, y = b >> up;         push_down(x, 1 << up);         if (x != y) push_down(y, 1 << up);       }     }     void join_and_apply(int position, int length) {       tree[position].join(tree[2 * position], tree[2 * position + 1]);       tree[position].apply(length, changes[position]);     }     void join_all(int a, int b) {       assert(0 <= a && a < b && b <= tree_n);       a += tree_n;       b += tree_n - 1;       int length = 1;       while (a > 1) {         a /= 2;         b /= 2;         length *= 2;         join_and_apply(a, length);         if (a != b) join_and_apply(b, length);       }     }     template <typename T_range_op>     void process_range(int a, int b, bool needs_join, T_range_op &&range_op) {       if (a == b) return;       push_all(a, b);       int original_a = a, original_b = b;       int length = 1, r_size = 0;       for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) {         if (a & 1) range_op(a++, length);         if (b & 1) right_half[r_size++] = {--b, length};       }       for (int i = r_size - 1; i >= 0; i--)         range_op(right_half[i].first, right_half[i].second);       if (needs_join) join_all(original_a, original_b);     }     segment query(int a, int b) {       assert(0 <= a && a <= b && b <= tree_n);       segment answer;       process_range(a, b, false,                     [&](int position, int) { answer.join(tree[position]); });       return answer;     }     void update(int a, int b, const segment_change &change) {       assert(0 <= a && a <= b && b <= tree_n);       process_range(a, b, true, [&](int position, int length) {         tree[position].apply(length, change);         if (position < tree_n)           changes[position] = changes[position].combine(change);       });     }   };   static segment v_half[32];   struct ordered_path_heavy_light {     int n = 0;     bool vertex_mode;     vector<vector<int>> adj;     vector<int> parent, depth, subtree_size;     vector<int> chain_root, chain_size;     vector<seg_tree> chain_up_tree, chain_down_tree;     ordered_path_heavy_light() {}     ordered_path_heavy_light(int _n, bool _vertex_mode) {       init(_n, _vertex_mode);     }     void init(int _n, bool _vertex_mode) {       n = _n;       vertex_mode = _vertex_mode;       adj.assign(n, {});       parent.resize(n);       depth.resize(n);       subtree_size.resize(n);       chain_root.resize(n);       chain_size.assign(n, 0);       chain_up_tree.assign(n, {});       chain_down_tree.assign(n, {});     }     void add_edge(int a, int b) {       adj[a].push_back(b);       adj[b].push_back(a);     }     void dfs(int node, int par) {       parent[node] = par;       depth[node] = par < 0 ? 0 : depth[par] + 1;       subtree_size[node] = 1;       if (par >= 0)         adj[node].erase(find(adj[node].begin(), adj[node].end(), par));       for (int &child : adj[node]) {         dfs(child, node);         subtree_size[node] += subtree_size[child];       }       sort(adj[node].begin(), adj[node].end(),            [&](int a, int b) { return subtree_size[a] > subtree_size[b]; });     }     void chain_dfs(int node, bool heavy) {       chain_root[node] = heavy ? chain_root[parent[node]] : node;       chain_size[chain_root[node]]++;       bool heavy_child = true;       for (int child : adj[node]) {         chain_dfs(child, heavy_child);         heavy_child = false;       }     }     void build(const segment &initial) {       parent.assign(n, -1);       for (int i = 0; i < n; i++)         if (parent[i] < 0) {           dfs(i, -1);           chain_dfs(i, false);         }       for (int i = 0; i < n; i++)         if (chain_root[i] == i) {           chain_up_tree[i].init(chain_size[i]);           chain_down_tree[i].init(chain_size[i]);           vector<segment> initial_segs(chain_size[i], initial);           chain_up_tree[i].build(initial_segs);           chain_down_tree[i].build(initial_segs);         }     }     segment _query_up_tree(int root, int start, int end) {       int size = chain_size[root];       return chain_up_tree[root].query(size - end, size - start);     }     void _update_up_tree(int root, int start, int end,                          const segment_change &change) {       int size = chain_size[root];       chain_up_tree[root].update(size - end, size - start, change);     }     void _update_both_trees(int root, int start, int end,                             const segment_change &change) {       _update_up_tree(root, start, end, change);       chain_down_tree[root].update(start, end, change);     }     segment query_path(int u, int v) {       segment u_half;       int v_size = 0;       while (chain_root[u] != chain_root[v]) {         bool pull_u = depth[chain_root[u]] > depth[chain_root[v]];         int pull_node = pull_u ? u : v;         int root = chain_root[pull_node];         int end = depth[pull_node] - depth[root] + 1;         if (pull_u) {           u_half.join(_query_up_tree(root, 0, end));           u = parent[root];         } else {           v_half[v_size++] = chain_down_tree[root].query(0, end);           v = parent[root];         }       }       bool pull_u = depth[u] > depth[v];       int pull_node = pull_u ? u : v;       int ancestor = pull_u ? v : u;       int root = chain_root[pull_node];       int start = depth[ancestor] - depth[root] + (vertex_mode ? 0 : 1);       int end = depth[pull_node] - depth[root] + 1;       if (pull_u)         u_half.join(_query_up_tree(root, start, end));       else         v_half[v_size++] = chain_down_tree[root].query(start, end);       for (int i = v_size - 1; i >= 0; i--) u_half.join(v_half[i]);       return u_half;     }     void update_path(int u, int v, const segment_change &change) {       while (chain_root[u] != chain_root[v]) {         if (depth[chain_root[u]] > depth[chain_root[v]]) swap(u, v);         int root = chain_root[v];         _update_both_trees(root, 0, depth[v] - depth[root] + 1, change);         v = parent[root];       }       if (depth[u] > depth[v]) swap(u, v);       int root = chain_root[v];       _update_both_trees(root, depth[u] - depth[root] + (vertex_mode ? 0 : 1),                          depth[v] - depth[root] + 1, change);     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int N;     cin >> N;     ordered_path_heavy_light HLD(N, false);     vector<pair<int, int>> T1(N - 1);     for (int i = 0; i < N - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       HLD.add_edge(a, b);       T1[i] = {a, b};     }     HLD.build(segment());     for (int e = 0; e < N - 1; e++)       HLD.update_path(T1[e].first, T1[e].second, segment_change(e));     vector<vector<int>> adj(N);     vector<int> degree(N, 0);     for (int i = 0; i < N - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       adj[a].push_back(b);       adj[b].push_back(a);       degree[a]++;       degree[b]++;     }     queue<int> leaves;     for (int i = 0; i < N; i++)       if (degree[i] == 1) leaves.push(i);     cout << N - 1 <<   n ;     while (!leaves.empty()) {       int leaf = leaves.front();       leaves.pop();       if (degree[leaf] == 0) break;       int parent = -1;       for (int neighbor : adj[leaf])         if (degree[neighbor] != 0) parent = neighbor;       assert(parent >= 0);       int first_edge = HLD.query_path(leaf, parent).first;       assert(first_edge >= 0);       int x = T1[first_edge].first, y = T1[first_edge].second;       HLD.update_path(x, y, segment_change(-1));       degree[leaf]--;       degree[parent]--;       if (degree[parent] == 1) leaves.push(parent);       cout << x + 1 <<     << y + 1 <<     << leaf + 1 <<     << parent + 1            <<   n ;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:268435456 )   using namespace std;   const string IN_NAME =  input.txt ;   const string OUT_NAME =  output.txt ;   template <class T>   T abs(T &x) {     return ((x) >= 0) ? (x) : (-(x));   }   template <class T>   T sqr(T &x) {     return (x) * (x);   }   template <class T>   T min(T &a, T &b) {     return ((a) < (b)) ? (a) : (b);   }   template <class T>   T max(T &a, T &b) {     return ((a) > (b)) ? (a) : (b);   }   int n, m;   struct tree {     int x, h, pL, pR;     tree() {}     bool operator<(const tree &t) const { return x < t.x; }   };   struct mush {     int x, z;     mush() {}     bool operator<(const mush &t) const { return x < t.x; }   };   tree a[100010];   mush b[100010], tmpb[100010];   int binSearch(int x) {     if (x < b[0].x) return -1;     if (x > b[m - 1].x) return -m - 1;     int left = 0, right = m - 1;     while (right - left > 1) {       int mid = (left + right) >> 1;       if (b[mid].x == x) return mid;       if (b[mid].x < x)         left = mid + 1;       else         right = mid - 1;     }     for (int i = max(0, left - 4); i < min(m - 1, right + 3); i++) {       if (b[i].x < x && x < b[i + 1].x) return -(i + 1) - 1;     }     return right;   }   double d[100010];   double db[100010];   int dk;   void multQuery(int left, int right, double p) {     if (left > right) return;     int L = left / dk + 1;     int R = right / dk - 1;     if (L > R) {       for (int i = left; i <= right; i++) d[i] *= p;     } else {       for (int i = left; i < L * dk; i++) d[i] *= p;       for (int j = L; j <= R; j++) db[j] *= p;       for (int i = (R + 1) * dk; i <= right; i++) d[i] *= p;     }   }   double getQuery(int index) { return d[index] * db[index / dk]; }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++)       scanf( %d%d%d%d , &a[i].x, &a[i].h, &a[i].pL, &a[i].pR);     for (int i = 0; i < m; i++) scanf( %d%d , &tmpb[i].x, &tmpb[i].z);     sort(a, a + n);     sort(tmpb, tmpb + m);     int newM = 0;     for (int i = 0; i < m; i++) {       if (i == 0 || tmpb[i - 1].x < tmpb[i].x) {         b[newM].x = tmpb[i].x;         b[newM].z = tmpb[i].z;         newM++;       } else {         b[newM - 1].z += tmpb[i].z;       }     }     m = newM;     for (int i = 0; i < n; i++) a[i].x *= 2;     for (int i = 0; i < n; i++) a[i].h *= 2;     for (int i = 0; i < m; i++) b[i].x *= 2;     dk = (int)sqrt(m + 1e-9);     for (int i = 0; i < m + 5; i++) {       d[i] = db[i] = 1;     }     for (int i = 0; i < n; i++) {       int leftIndex = binSearch(a[i].x - a[i].h - 1);       int rightIndex = binSearch(a[i].x + a[i].h + 1);       int thisLeftIndex = binSearch(a[i].x - 1);       int thisRightIndex = binSearch(a[i].x + 1);       leftIndex = ~leftIndex;       rightIndex = ~rightIndex;       thisLeftIndex = ~thisLeftIndex;       thisRightIndex = ~thisRightIndex;       multQuery(leftIndex, thisLeftIndex - 1, 1.0 - a[i].pL / 100.0);       multQuery(thisRightIndex, rightIndex - 1, 1.0 - a[i].pR / 100.0);     }     double result = 0;     for (int i = 0; i < m; i++) {       result += getQuery(i) * b[i].z;     }     printf( %.12f , result);   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(0.0) * 2.0;   const double eps = 1e-12;   const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};   template <class T>   inline T abs1(T a) {     return a < 0 ? -a : a;   }   template <class T>   inline T max1(T a, T b, T c = -1234567, T d = -1234567) {     T ans = a > b ? a : b;     if (c != -1234567) ans = max1(ans, c);     if (d != -1234567) ans = max1(ans, d);     return ans;   }   template <class T>   inline T min1(T a, T b, T c = -1234567, T d = -1234567) {     T ans = a < b ? a : b;     if (c != -1234567) ans = min(ans, c);     if (d != -1234567) ans = min(ans, d);     return ans;   }   template <class T>   inline T gcd1(T a, T b) {     if (a < b) swap(a, b);     if (a % b == 0) return b;     return gcd1(b, a % b);   }   template <class T>   inline T lb(T num) {     return num & (-num);   }   inline int jud(double a, double b) {     if (abs1(a - b) < eps) return 0;     if (a < b) return -1;     return 1;   }   template <typename it, typename t1>   inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {     if (na == 1) return 0;     int be = 0, en = na - 1;     if (*a <= *(a + na - 1)) {       if (f_lb == 0)         while (be < en) {           int mid = (be + en + 1) / 2;           if (jud(*(a + mid), val) != 1)             be = mid;           else             en = mid - 1;         }       else         while (be < en) {           int mid = (be + en) / 2;           if (jud(*(a + mid), val) != -1)             en = mid;           else             be = mid + 1;         }       if (f_small && jud(*(a + be), val) == 1 && be != 0) be--;       if (!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++;     } else {       if (f_lb)         while (be < en) {           int mid = (be + en + 1) / 2;           if (jud(*(a + mid), val) != -1)             be = mid;           else             en = mid - 1;         }       else         while (be < en) {           int mid = (be + en) / 2;           if (jud(*(a + mid), val) != 1)             en = mid;           else             be = mid + 1;         }       if (!f_small && jud(*(a + be), val) == -1 && be != 0) be--;       if (f_small && jud(*(a + be), val) == 1 && be != na - 1) be++;     }     return be;   }   inline int bitnum(unsigned long long nValue) {     nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) +              (0x5555555555555555ull & nValue);     nValue = ((0xccccccccccccccccull & nValue) >> 2) +              (0x3333333333333333ull & nValue);     nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) +              (0x0f0f0f0f0f0f0f0full & nValue);     nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) +              (0x00ff00ff00ff00ffull & nValue);     nValue = ((0xffff0000ffff0000ull & nValue) >> 16) +              (0x0000ffff0000ffffull & nValue);     nValue = ((0xffffffff00000000ull & nValue) >> 32) +              (0x00000000ffffffffull & nValue);     return nValue;   }   long long pow(long long n, long long m, long long mod = 0) {     long long ans = 1;     long long k = n;     while (m) {       if (m & 1) {         ans *= k;         if (mod) ans %= mod;       }       k *= k;       if (mod) k %= mod;       m >>= 1;     }     return ans;   }   const int maxn = 200110;   char str[maxn];   int f[maxn];   int cntwen = 0, cnt = 0, st1 = 0;   int main() {     scanf( %s , str);     int l = strlen(str);     bool st = (str[0] >=  A  && str[0] <=  J );     for (int i = 0; i < l; i++) {       if (str[i] >=  A  && str[i] <=  J ) {         if (f[str[i] -  A ] == 0) cnt++;         f[str[i] -  A ]++;       }       if (str[i] ==  ? ) cntwen++;     }     int ans = 1;     for (int i = 0; i < cnt; i++) ans *= 10 - i;     if (st) {       ans /= 10;       ans *= 9;     }     if (str[0] ==  ? ) ans *= 9, cntwen--;     printf( %d , ans);     for (int i = 0; i < cntwen; i++) printf( 0 );     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   string toString(T x) {     if (x == 0) return  0 ;     bool negative = x < 0;     string res;     while (x) {       res.push_back( 0  + x % 10);       x /= 10;     }     if (negative) res.push_back( - );     reverse(res.begin(), res.end());     return res;   }   template <typename T>   T gcd(T a, T b) {     return a == 0 ? b : gcd(b % a, a);   }   template <typename T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   int main() {     string s;     cin >> s;     vector<int> a(s.length() / 2);     int m;     cin >> m;     for (int i = 0; i < m; i++) {       int t;       cin >> t;       t--;       a[t]++;     }     int l = s.length() / 2;     vector<bool> r(l);     int carry = 0;     for (int i = 0; i < l; i++) {       int t = a[i] + carry;       carry = t % 2;       r[i] = carry;     }     for (int i = 0; i < l; i++) {       if (r[i]) {         cout << s[s.length() - 1 - i];       } else {         cout << s[i];       }     }     if (s.length() % 2 == 1) {       cout << s[l];       l++;     }     for (int i = (s.length() + 1) / 2; i < s.length(); i++) {       if (r[s.length() - 1 - i]) {         cout << s[s.length() - 1 - i];       } else {         cout << s[i];       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, key, MK, MKEY;   int MAP[110][110];   int ii, jj;   void dfssolve(int pos, int step) {     int temp[110];     int cnt, i, j;     if (pos > n) {       key = 1;       k = k < step ? k : step;       return;     }     cnt = 0;     for (i = 1; i <= m; ++i) {       temp[i] = MAP[pos][i];       if (MAP[pos][i] != MAP[pos - 1][i]) {         cnt++;         MAP[pos][i] = MAP[pos - 1][i];       }     }     if (step + cnt <= k) dfssolve(pos + 1, step + cnt);     cnt = 0;     for (i = 1; i <= m; ++i) {       if (temp[i] == MAP[pos - 1][i]) {         cnt++;         MAP[pos][i] = MAP[pos - 1][i] & 1;       }     }     if (step + cnt <= k) dfssolve(pos + 1, step + cnt);     for (i = 1; i <= m; ++i) MAP[pos][i] = temp[i];   }   void dfssolveup(int pos, int step) {     int cnt, i, j;     int temp[110];     if (pos < 1) {       MKEY = 1;       MK = step < MK ? step : MK;       return;     }     cnt = 0;     for (i = 1; i <= m; ++i) {       temp[i] = MAP[pos][i];       if (MAP[pos][i] != MAP[pos + 1][i]) {         cnt++;         MAP[pos][i] = MAP[pos + 1][i];       }     }     if (step + cnt <= k) dfssolveup(pos - 1, step + cnt);     cnt = 0;     for (i = 1; i <= m; ++i) {       if (temp[i] == MAP[pos + 1][i]) {         cnt++;         MAP[pos][i] = MAP[pos + 1][i] & 1;       }     }     if (step + cnt <= k) dfssolveup(pos - 1, step + cnt);     for (i = 1; i <= m; ++i) MAP[pos][i] = temp[i];   }   void dfs(int pos, int step) {     if (k < step) return;     if (pos == m) {       dfssolve(2, step);       MAP[1][pos] ^= 1;       dfssolve(2, step + 1);       MAP[1][pos] ^= 1;     } else {       dfs(pos + 1, step);       MAP[1][pos] ^= 1;       dfs(pos + 1, step + 1);       MAP[1][pos] ^= 1;     }   }   int main() {     cin >> n >> m >> k;     key = 0;     if (n < m) {       for (ii = 1; ii <= n; ++ii)         for (jj = 1; jj <= m; ++jj) cin >> MAP[jj][ii];       swap(n, m);     } else {       for (ii = 1; ii <= n; ++ii)         for (jj = 1; jj <= m; ++jj) cin >> MAP[ii][jj];     }     if (n <= k) {       dfs(1, 0);     } else {       for (ii = 1; ii <= n; ++ii) {         MK = 10;         MKEY = 0;         dfssolveup(ii - 1, 0);         if (MKEY) {           dfssolve(ii + 1, MK);         }       }     }     if (key)       cout << k << endl;     else       cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long N = 1e5 + 5;   long long ans = 1e18;   map<long long, bool> mp;   void solve(long long n, long long cnt) {     if (!n) {       cout << cnt;       exit(0);     }     string s = to_string(n);     sort(s.rbegin(), s.rend());     solve(n - (s[0] -  0 ), cnt + 1);   }   inline long long solve() {     long long n;     cin >> n;     solve(n, 0);     cout << ans;     return 0;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, t, d;     cin >> n >> m;     map<long long, long long> mp;     set<pair<long long, long long> > st;     vector<long long> a(n), c(n);     for (int i = 0; i < n; i++) {       cin >> a[i];       mp[i] = a[i];     }     for (int i = 0; i < n; i++) {       cin >> c[i];       st.insert({c[i], i});     }     for (int i = 0; i < m; i++) {       cin >> t >> d;       long long cost = 0;       t--;       if (st.size() == 0)         cout << 0 << endl;       else {         if (mp[t] != 0) {           if (mp[t] > d) {             mp[t] -= d;             cout << (long long)d * c[t] << endl;             d = 0;           } else if (mp[t] == d) {             mp[t] -= d;             cout << (long long)d * c[t] << endl;             d = 0;             st.erase({c[t], t});           } else {             cost += (long long)mp[t] * c[t];             d -= mp[t];             mp[t] = 0;             st.erase({c[t], t});           }         }         if (d > 0) {           while (d > 0 && st.size() > 0) {             pair<int, int> now = *st.begin();             if (mp[now.second] > d) {               mp[now.second] -= d;               cout << (long long)cost + d * now.first << endl;               d = 0;             } else if (mp[now.second] == d) {               mp[now.second] -= d;               cout << (long long)cost + d * now.first << endl;               d = 0;               st.erase(st.begin());             } else {               cost += (long long)mp[now.second] * now.first;               d -= mp[now.second];               mp[now.second] = 0;               st.erase(st.begin());             }           }           if (d > 0) {             cout << 0 << endl;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void pri(deque<int> arr) {     for (int i : arr) cout << i <<    ;     cout << endl;   }   int main() {     string s;     cin >> s;     int n = s.size();     if (n <= 3) {       cout << s[0] << endl;       return 0;     }     deque<int> a1;     deque<int> b1;     deque<int> c1;     int as[n + 2];     int bs[n + 2];     int cs[n + 2];     memset(as, 0, sizeof as);     memset(bs, 0, sizeof bs);     memset(cs, 0, sizeof cs);     as[0] = 0;     bs[0] = 0;     cs[0] = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  a ) {         a1.push_back(i);         as[i + 1]++;       } else if (s[i] ==  b ) {         b1.push_back(i);         bs[i + 1]++;       } else {         c1.push_back(i);         cs[i + 1]++;       }       as[i + 1] += as[i];       bs[i + 1] += bs[i];       cs[i + 1] += cs[i];     }     int lim = -10;     int mx = 1000006;     string ans =   ;     while (true) {       int a = lim;       int b = lim;       int c = lim;       if (a1.size() > 1) {         a = a1[a1.size() - 1] - a1[0];       }       if (b1.size() > 1) {         b = b1[b1.size() - 1] - b1[0];       }       if (c1.size() > 1) {         c = c1.back() - c1.front();       }       int x = max(max(a, b), c);       if (x == lim) break;       int az = mx, bz = mx, cz = mx;       if (a1.size() > 0) {         az = as[a1[a1.size() - 1] + 1] - as[a1[0]];       }       if (b1.size() > 0) {         bz = bs[b1[b1.size() - 1] + 1] - bs[b1[0]];       }       if (c1.size() > 0) {         cz = cs[c1[c1.size() - 1] + 1] - cs[c1[0]];       }       if (x > a) {         a = lim;         az = mx;       }       if (x > b) {         b = lim;         bz = mx;       }       if (x > c) {         c = lim;         cz = mx;       }       x += min(min(az, bz), cz);       a += az;       b += bz;       c += cz;       if (x == a) {         ans +=  a ;         while (b1.size() > 0 && b1[0] < a1[0]) b1.pop_front();         while (b1.size() > 0 && b1[b1.size() - 1] > a1[a1.size() - 1])           b1.pop_back();         while (c1.size() > 0 && c1[0] < a1[0]) c1.pop_front();         while (c1.size() > 0 && c1[c1.size() - 1] > a1[a1.size() - 1])           c1.pop_back();         a1.pop_front();         a1.pop_back();       } else if (x == b) {         ans +=  b ;         while (a1.size() > 0 && a1[0] < b1[0]) a1.pop_front();         while (a1.size() > 0 && a1[a1.size() - 1] > b1[b1.size() - 1])           a1.pop_back();         while (c1.size() > 0 && c1[0] < b1[0]) c1.pop_front();         while (c1.size() > 0 && c1[c1.size() - 1] > b1[b1.size() - 1])           c1.pop_back();         b1.pop_front();         b1.pop_back();       } else {         ans +=  c ;         while (b1.size() > 0 && b1[0] < c1[0]) b1.pop_front();         while (b1.size() > 0 && b1[b1.size() - 1] > c1[c1.size() - 1])           b1.pop_back();         while (a1.size() > 0 && a1[0] < c1[0]) a1.pop_front();         while (a1.size() > 0 && a1[a1.size() - 1] > c1[c1.size() - 1])           a1.pop_back();         c1.pop_front();         c1.pop_back();       }     }     string aux = ans;     reverse(aux.begin(), aux.end());     if (c1.size() > 0) {       ans +=  c ;     } else {       if (b1.size() > 0) {         ans +=  b ;       } else {         if (a1.size()) {           ans +=  a ;         }       }     }     ans += aux;     if (ans.size() >= (int)n / 2)       cout << ans;     else       cout <<  IMPOSSIBLE ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T modulo(T x, T p) {     if (x >= 0) return x % p;     return p + x % p;   }   template <class T>   T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   T lcm(T a, T b) {     return a * b / gcd(a, b);   }   int dfs_paths(       int u, int prev, vector<vector<pair<long long, long long> > > &G,       long long &cur,       priority_queue<pair<long long, pair<long long, long long> > > &pqw) {     long long paths, total = 0, profit;     for (pair<long long, long long> e : G[u]) {       if (e.second == prev) continue;       paths = dfs_paths(e.second, u, G, cur, pqw);       profit = e.first * paths - (e.first / 2) * paths;       pqw.push({profit, {e.first, paths}});       cur += e.first * paths;       total += paths;     }     return max(1ll, total);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       long long n, S;       cin >> n >> S;       vector<vector<pair<long long, long long> > > G(n);       int u, v;       long long w;       for (int i = 0; i < n - 1; i++) {         cin >> u >> v >> w;         u--;         v--;         G[u].push_back({w, v});         G[v].push_back({w, u});       }       long long cur = 0;       priority_queue<pair<long long, pair<long long, long long> > > pqw;       dfs_paths(0, -1, G, cur, pqw);       long long moves = 0, mult;       pair<long long, pair<long long, long long> > e;       while (cur > S) {         e = pqw.top();         pqw.pop();         w = e.second.first / 2;         mult = e.second.second;         cur -= e.first;         pqw.push({w * mult - (w / 2) * mult, {w, mult}});         moves++;       }       cout << moves <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int long long l1, l2, r1, r2, k, ans, l, r;     cin >> l1;     cin >> r1;     cin >> l2;     cin >> r2;     cin >> k;     l = max(l1, l2);     r = min(r1, r2);     if (r - l < 0) {       cout << 0 << endl;       return 0;     }     ans = r - l + 1;     if (k >= l && k <= r) ans--;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   clock_t __stt;   inline void TStart() { __stt = clock(); }   inline void TReport() {     cerr << fixed << setprecision(3) <<   nCurrent Case Taken Time :            << (double)(clock() - __stt) / CLOCKS_PER_SEC <<  sec n ;   }   template <typename T>   T MIN(T a, T b) {     return a < b ? a : b;   }   template <typename T>   T MAX(T a, T b) {     return a > b ? a : b;   }   template <typename T>   T ABS(T a) {     return a > 0 ? a : (-a);   }   template <typename T>   void UMIN(T &a, T b) {     if (b < a) a = b;   }   template <typename T>   void UMAX(T &a, T b) {     if (b > a) a = b;   }   int n, m, p[100005], s[100005], b[100005], inc[100005], pref[100005];   struct ele {     int A, B, C, id;     void set(int _A, int _B, int _C, int _id) {       A = _A;       B = _B;       C = _C;       id = _id;     }   } es[100005], et[100005];   struct qry {     int Al, Ar, B, C;     void set(int _Al, int _Ar, int _B, int _C) {       Al = _Al;       Ar = _Ar;       B = _B;       C = _C;     }   } qs[100005], qt[100005];   namespace BIT {   int arr[300005];   void add(int p, int v = 1) {     ++p;     while (p <= 300002) {       arr[p] += v;       p += (p & (-p));     }   }   void clr(int p) {     ++p;     while (p <= 300002) {       arr[p] = 0;       p += (p & (-p));     }   }   int sum(int p) {     int r = 0;     ++p;     while (p) {       r += arr[p];       p -= (p & (-p));     }     return r;   }   }  // namespace BIT   bool cmpB(ele a, ele b) { return a.B < b.B; }   bool cmpC(ele a, ele b) { return a.C < b.C; }   bool cmpBq(qry a, qry b) { return a.B < b.B; }   bool cmpCq(qry a, qry b) { return a.C < b.C; }   vector<int> vs;   int id(int v) { return lower_bound(vs.begin(), vs.end(), v) - vs.begin(); }   int res[100005];   void solve2d(ele *E, int ec, qry *Q, int qc) {     int i, j, k;     sort(E, E + ec, cmpB);     sort(Q, Q + qc, cmpBq);     reverse(Q, Q + qc);     reverse(E, E + ec);     j = 0;     for (i = 0; i < ec; ++i) {       int cB = E[i].B;       while (j < qc && Q[j].B >= cB) {         BIT::add(Q[j].Al, 1);         BIT::add(Q[j].Ar + 1, -1);         ++j;       }       res[E[i].id] += BIT::sum(E[i].A);     }     for (i = 0; i < qc; ++i) {       BIT::clr(Q[i].Al);       BIT::clr(Q[i].Ar + 1);     }   }   void solve(int cl, int cr, int ql, int qr, int el, int er) {     if (ql > qr || el > er) return;     int i, j, k;     if (cl == cr) {       solve2d(es + el, er - el + 1, qs + ql, qr - ql + 1);       return;     }     memcpy(et + el, es + el, sizeof(ele) * (er - el + 1));     memcpy(qt + ql, qs + ql, sizeof(qry) * (qr - ql + 1));     int cM = ((long long)cl + (long long)cr) / 2ll, qM, eM;     for (qM = ql; qM <= qr; ++qM) {       if (qt[qM].C <= cM && (qM == qr || qt[qM + 1].C > cM)) break;     }     if (qt[ql].C > cM) qM = ql - 1;     for (eM = el; eM <= er; ++eM) {       if (et[eM].C <= cM && (eM == er || et[eM + 1].C > cM)) break;     }     if (et[el].C > cM) eM = el - 1;     if (er > eM && qM >= ql) solve2d(et + eM + 1, er - eM, qt + ql, qM - ql + 1);     solve(cl, cM, ql, qM, el, eM);     solve(cM + 1, cr, qM + 1, qr, eM + 1, er);   }   int main() {     int i, j, k;     scanf( %d%d , &n, &m);     for (i = 0; i < n; ++i) scanf( %d , p + i);     for (i = 0; i < n; ++i) scanf( %d , s + i);     for (i = 0; i < n; ++i) scanf( %d , b + i);     for (i = 0; i < m; ++i) scanf( %d , inc + i);     for (i = 0; i < m; ++i) scanf( %d , pref + i);     for (i = 0; i < n; ++i) {       qs[i].Al = p[i];       qs[i].Ar = s[i];       vs.push_back(p[i]);       vs.push_back(s[i]);       qs[i].B = b[i] - p[i];       qs[i].C = b[i] + p[i];     }     for (j = 0; j < m; ++j) {       es[j].A = inc[j];       vs.push_back(inc[j]);       es[j].B = pref[j] - inc[j];       es[j].C = pref[j] + inc[j];       es[j].id = j;     }     sort(vs.begin(), vs.end());     vs.erase(unique(vs.begin(), vs.end()), vs.end());     for (i = 0; i < n; ++i) {       qs[i].Al = id(qs[i].Al);       qs[i].Ar = id(qs[i].Ar);     }     for (j = 0; j < m; ++j) {       es[j].A = id(es[j].A);     }     sort(es, es + m, cmpC);     sort(qs, qs + n, cmpCq);     solve(0, (int)2e9 + 5, 0, n - 1, 0, m - 1);     for (i = 0; i < m; ++i) {       printf( %d  , res[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       long long x1, y1, x2, y2;       scanf( %lld%lld%lld%lld , &x1, &y1, &x2, &y2);       long long lenx = abs(x1 - x2);       long long leny = abs(y1 - y2);       long long ans = lenx + leny;       if (lenx && leny) ans += 2;       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     cin >> n;     cout << n % 2;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     while (cin >> n) {       vector<long long> vec;       for (long long i = 2; i * i <= n; i++) {         while (n % i == 0) {           vec.push_back(i);           n /= i;         }       }       if (n > 1) {         vec.push_back(n);       }       if (vec.size() < 2) {         printf( 1 n0 n );       } else if (vec.size() == 2) {         printf( 2 n );       } else {         printf( 1 n%lld n , vec[0] * vec[1]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, num, cnt;   int dat[1009][1009], ans[1000009];   bool vis[1009][1009];   int dx[4] = {-1, 0, 1, 0};   int dy[4] = {0, 1, 0, -1};   int wall[4] = {8, 4, 2, 1};   void dfs(int x, int y) {     if (vis[x][y] || x < 0 || x > n || y < 0 || y > m) return;     vis[x][y] = 1;     num++;     for (int i = 0; i < 4; i++) {       if ((dat[x][y] & wall[i]) ||           (dat[x + dx[i]][y + dy[i]] & wall[(i + 2) % 4]))         continue;       dfs(x + dx[i], y + dy[i]);     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> dat[i][j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (!vis[i][j]) {           num = 0;           dfs(i, j);           ans[++cnt] = num;         }       }     }     sort(ans + 1, ans + 1 + cnt);     for (int i = cnt; i > 0; i--) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, ind, sum = 0;     cin >> n;     long long mas[n];     for (long long i = 0; i < n; i++) {       cin >> mas[i];       sum += mas[i];     }     cin >> m;     long long arr[m];     sort(mas, mas + n);     for (long long i = 0; i < m; i++) {       cin >> arr[i];       cout << sum - mas[n - arr[i]] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, nxt[200010], a[200010], sz[200010][31];   long long ans = 0;   map<int, int> mp;   inline char gc() {     static char *S, *T, buf[1 << 16];     if (T == S) {       T = (S = buf) + fread(buf, 1, 1 << 16, stdin);       if (T == S) return EOF;     }     return *S++;   }   inline int read() {     int x = 0, f = 1;     char ch = gc();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = gc();     }     while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = gc();     return x * f;   }   inline int check(int l, int r) {     int res = 0;     for (int i = 0; i <= 30; ++i) res |= ((sz[r][i] - sz[l - 1][i]) > 0) << i;     return res;   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) {       a[i] = read(), nxt[i] = mp[a[i]], mp[a[i]] = i;       for (int j = 0; j <= 30; ++j) {         sz[i][j] = sz[i - 1][j];         if (a[i] & (1 << j)) sz[i][j]++;       }     }     for (int i = 1; i <= n; ++i) {       int l = max(nxt[i] + 1, 1), r = i, nowl = i;       while (l <= r) {         int mid = l + r >> 1;         if (check(mid, i) == a[i])           nowl = mid, r = mid - 1;         else           l = mid + 1;       }       l = i, r = n;       int nowr = i;       while (l <= r) {         int mid = l + r >> 1;         if (check(i, mid) == a[i])           nowr = mid, l = mid + 1;         else           r = mid - 1;       }       ans += 1ll * (i - nowl + 1) * (nowr - i + 1);     }     printf( %I64d n , 1ll * n * (n + 1) / 2 - ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, t, twoc, onec, opt, old;   int main() {     cin >> n;     cin >> t;     if (t == 1)       onec++;     else       twoc++;     old = t;     for (int i = 1; i < n; i++) {       cin >> t;       if (t == 1) {         if (old == 2)           onec = 1;         else           onec++;       } else {         if (old == 1)           twoc = 1;         else           twoc++;       }       opt = max(opt, min(onec, twoc));       old = t;     }     cout << opt * 2;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int pow(int a, int b, int c) {     int ret = 1;     for (; b; b >>= 1, a = 1LL * a * a % c)       if (b & 1) ret = 1LL * ret * a % c;     return ret;   }   const int N = 2e3 + 10, M = 4.05e6 + 10;   int h[N], g[N], p[M], n1[M], ee, low[N], dfn[N], dfntot, vis[N], n, m, st[N],       top, id[N], tot, sz[N], ans, dp[N], ok[N];   pair<int, int> e[M];   void ae(int *A, int x, int y) {     p[ee] = y;     n1[ee] = A[x];     A[x] = ee++;   }   void tarjan(int u, int fa) {     vis[u] = 1;     low[u] = dfn[u] = ++dfntot;     st[++top] = u;     for (int i = h[u]; ~i; i = n1[i])       if (p[i] != fa) {         if (!vis[p[i]])           tarjan(p[i], u), low[u] = min(low[u], low[p[i]]);         else           low[u] = min(low[u], dfn[p[i]]);         if (low[p[i]] > dfn[u]) {           ++tot;           while (st[top] != p[i]) id[st[top--]] = tot, sz[tot]++;           id[st[top--]] = tot;           sz[tot]++;         }       }   }   void dfs(int u, int fa) {     dp[u] = sz[u];     for (int i = g[u]; ~i; i = n1[i])       if (p[i] != fa) {         dfs(p[i], u);         dp[u] += dp[p[i]];       }   }   int main() {     scanf( %d%d , &n, &m);     memset(h, -1, sizeof(h));     memset(g, -1, sizeof(g));     int x, y;     for (int i = 1; i <= m; i++) scanf( %d%d , &x, &y), ae(h, x, y), ae(h, y, x);     for (int i = 1; i <= n; i++)       if (!vis[i]) tarjan(i, 0);     ++tot;     while (top) id[st[top--]] = tot, sz[tot]++;     for (int i = 1; i <= n; i++)       for (int j = h[i]; ~j; j = n1[j])         if (id[i] != id[p[j]]) ae(g, id[i], id[p[j]]);     for (int i = 1; i <= tot; i++) ans += sz[i] * (sz[i] - 1);     int ret = 0;     for (int i = 1; i <= tot; i++) {       int tmp = 0;       dfs(i, 0);       for (int j = 1; j <= tot; j++) tmp += (dp[j] - sz[j]) * sz[j];       for (int j = 0; j <= n; j++) ok[j] = 0;       ok[0] = 1;       for (int k = g[i]; ~k; k = n1[k])         for (int j = n; j >= dp[p[k]]; j--) ok[j] |= ok[j - dp[p[k]]];       int mx = 0;       for (int j = 0; j <= n; j++)         if (ok[j]) mx = max(mx, j * (n - sz[i] - j));       ret = max(ret, tmp + mx);     }     printf( %d n , n + ans + ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int m = 1000000007;   long long int n1, n2 = 0, sum = 0;   long long int gcd(long long int a, long long int b) {     return (b == 0) ? a : gcd(b, a % b);   }   bool compare(pair<string, long long int> p1, pair<string, long long int> p2) {     if (p1.second != p2.second) {       return p1.second > p2.second;     } else       return p1.first < p2.first;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     while (t--) {       long long int n;       cin >> n;       bool prime[n + 2];       memset(prime, true, sizeof(prime));       for (long long int p = 2; p * p <= n + 1; p++) {         if (prime[p] == true) {           for (long long int i = p * p; i <= n + 1; i += p) prime[i] = false;         }       }       if (n > 2)         cout << 2 <<   n ;       else         cout << 1 <<   n ;       for (long long int i = 2; i < n + 2; i++) {         if (prime[i])           cout << 1 <<    ;         else           cout << 2 <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 mt(time(0));   set<pair<long long, long long> > dp[49];   void insert(set<pair<long long, long long> >& table, long long a, long long b) {     if (table.empty()) {       table.insert({a, b});       return;     }     auto it = table.lower_bound({b, LLONG_MAX});     while (it != table.begin()) {       it--;       if (it->second < a) break;       a = min(it->first, a);       b = max(it->second, b);       if (it == table.begin()) {         table.erase(it);         break;       } else {         table.erase(it++);       }     }     table.insert({a, b});   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout.precision(10);     cout << fixed;     long long n;     cin >> n;     for (int i = 0; i <= min(48LL, n); i++) {       for (int j = 0; j <= 8 && i + j <= n; j++) {         int tmp = i * 9 + j * 4;         long long a = tmp / 49;         long long b = n - i - j + tmp / 49;         insert(dp[tmp % 49], a, b);       }     }     long long ret = 0;     for (int i = 0; i < 49; i++) {       for (auto item : dp[i]) {         ret += item.second - item.first + 1;       }     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   struct node {     int l, m, r;   };   node t[3000005];   int arr[300005], n, m;   long long a[300005];   int sign(long long x) {     if (x > 0) return 1;     if (x < 0) return -1;     return 0;   }   void cal(int x, int l, int r) {     int m = (l + r) / 2;     t[x].m = max(t[x * 2].m, t[x * 2 + 1].m);     t[x].l = t[x * 2].l;     t[x].r = t[x * 2 + 1].r;     if (!!a[m] && !!a[m + 1] && sign(a[m]) >= sign(a[m + 1])) {       t[x].m = max(t[x].m, t[x * 2].r + t[x * 2 + 1].l);       if (t[2 * x].m == m - l + 1) t[x].l = t[2 * x].l + t[2 * x + 1].l;       if (t[2 * x + 1].m == r - m) t[x].r = t[2 * x].r + t[2 * x + 1].r;     }   }   void build(int x, int l, int r) {     if (l == r) {       int tmp = !!a[l];       t[x] = {tmp, tmp, tmp};       return;     }     int m = (l + r) / 2;     build(x * 2, l, m);     build(x * 2 + 1, m + 1, r);     cal(x, l, r);   }   void update(int x, int l, int r, int pos, int d) {     if (l == r) {       a[pos] += d;       int tmp = !!a[pos];       t[x] = {tmp, tmp, tmp};       return;     }     int m = (l + r) / 2;     if (pos <= m)       update(x * 2, l, m, pos, d);     else       update(x * 2 + 1, m + 1, r, pos, d);     cal(x, l, r);   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) scanf( %d , &arr[i]);     for (int i = 0; i + 1 < n; i++) a[i] = arr[i + 1] - arr[i];     if (n > 1) build(1, 0, n - 2);     cin >> m;     while (m--) {       int l, r, d;       scanf( %d%d%d , &l, &r, &d);       if (n == 1) {         printf( %d n , 1);         continue;       }       if (l > 1) update(1, 0, n - 2, l - 2, d);       if (r < n) update(1, 0, n - 2, r - 1, -d);       printf( %d n , t[1].m + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = int(1e9);   const int MOD = INF + 7;   const long long INFll = 1ll * INF * INF;   const long double ldINF = 1e+018;   const long double EPS = 0.000000001;   const int MAXN = 150;   const int N = 100001;   const int seg[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};   struct feature {     int b, i, r;   };   bool comp(feature x, feature y) {     return x.r > y.r || (x.r == y.r && x.b < y.b);   }   int t[1001000];   int cur = 0;   int query(int l, int r) {     int res = 0;     for (l += cur, r += cur; l < r; l >>= 1, r >>= 1) {       if (l & 1) res = max(res, t[l++]);       if (r & 1) res = max(res, t[--r]);     }     return res;   }   void modify(int p, int value) {     t[p += cur] = value;     while (p > 1) {       p >>= 1;       t[p] = max(t[p << 1], t[p << 1 | 1]);     }   }   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<feature> v(n);     for (int i = 0; i < n; i++) cin >> v[i].r;     for (int i = 0; i < n; i++) cin >> v[i].b;     for (int i = 0; i < n; i++) cin >> v[i].i;     vector<int> b;     for (int i = 0; i < n; i++) b.push_back(v[i].b);     sort(b.begin(), b.end());     map<int, int> m;     m[b[0]] = 0;     for (int i = 1; i < n; i++)       if (b[i] != b[i - 1]) m[b[i]] = ++cur;     cur++;     for (int i = 0; i < n; i++) v[i].b = m[v[i].b];     int ans = 0;     sort(v.begin(), v.end(), comp);     for (int i = 0; i < n; i++) {       int d = query(v[i].b + 1, cur);       if (d > v[i].i) {         ans++;       }       modify(v[i].b, max(t[v[i].b + cur], v[i].i));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t = 1;     while (t--) {       long long int n, d = 0, r = 0;       cin >> n;       string s;       cin >> s;       char x;       for (long long int i = 0; i < s.length(); i++) {         if (s[i] ==  R ) {           if (d) {             d--;           } else {             s +=  R ;             r++;           }         } else {           if (r) {             r--;           } else {             s +=  D ;             d++;           }         }         x = s[i];         if (d == n || r == n) {           break;         }       }       cout << x << endl;     }     return 0;   }


#include <bits/stdc++.h>   #define fasterio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)   #define X first   #define Y second   #define forn(i, n) for(int i=0;i<(n);i++)   #define pb push_back   using namespace std;   typedef long long ll;   typedef pair<int, int> pii;   const int inf = 0x3f3f3f3f;   const long long mod = 1e9+7;   const long long maxn = 100010;      int gao (int x1, int y1, int x2, int y2, int xf, int yf) {    auto abs = [&](int a, int b){return a>b?a-b:b-a;};    int ans = abs(x1, x2) + abs(y1, y2);       if (x1 == x2 && y1 == y2) {     return ans;    } else if (x1 == x2 && x2 == xf) { // on the same x axis     if ((y1<yf && yf<y2) || (y2<yf && yf<y1))      ans += 2;    } else if (y1 == y2 && y2 == yf) {     if ((x1<xf && xf<x2)||(x2<xf && xf<x1))       ans += 2;    }    return ans;   }   int main() {    int t;    cin>>t;     while(t--) {     int xa, ya, xb, yb, xf, yf;     cin>>xa>>ya>>xb>>yb>>xf>>yf;          if(xa==xb && ya==yb) {      cout<<0<<endl;      continue;     }        int ans = gao(xa, ya, xb, yb, xf, yf);     cout<<ans<<endl;    }   }


#include <bits/stdc++.h>   using namespace std;   int l_f, l_h;   long long int dp[101][101][11][11];   int getans(int f, int h, int k1, int k2) {     int x = 0, y = 0;     if ((f + h) == 0) return 1;     if (dp[f][h][k1][k2] != -1) return dp[f][h][k1][k2];     if (f > 0 && k1 > 0) x = getans(f - 1, h, k1 - 1, l_h);     if (h > 0 && k2 > 0) y = getans(f, h - 1, l_f, k2 - 1);     return dp[f][h][k1][k2] = (x + y) % 100000000;   }   int main() {     int t = 1;     while (t--) {       int n1, n2;       cin >> n1 >> n2 >> l_f >> l_h;       memset(dp, -1, sizeof(dp));       int ans = getans(n1, n2, l_f, l_h);       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     map<string, string> mp;     map<string, string>::iterator it;     for (int i = 0; i < n; ++i) {       string x, y;       cin >> x >> y;       it = mp.begin();       bool found = false;       for (; it != mp.end(); ++it) {         if (it->second == x) {           it->second = y;           found = true;         }       }       if (!found) mp[x] = y;     }     cout << mp.size() << endl;     it = mp.begin();     for (; it != mp.end(); it++) cout << it->first <<     << it->second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> h[100010], h1[100010];   long long cnt[100010], pre[100010], sum[100010];   int num[100010], mx[100010], n, tt, t1;   inline int rd() {     int x = 0;     char ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar())       ;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return x;   }   inline int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); }   inline long long calc(long long l, long long r) {     return (l + r) * (r - l + 1) / 2;   }   inline long long anol_gcd(long long n, long long a, long long b, long long c) {     if (n < 0) return 0;     if (!a) return (b / c) * (n + 1);     if (a >= c || b >= c)       return (a / c) * n * (n + 1) / 2 + (b / c) * (n + 1) +              anol_gcd(n, a % c, b % c, c);     long long hh = (a * n + b) / c;     return n * hh - anol_gcd(hh - 1, c, c - b - 1, a);   }   inline long long gao(int x, int y, long long mid) {     if (!cnt[x] || !cnt[y]) return 0;     mid -= pre[y - 1] - pre[x];     if (mid < 0) return 0;     long long mn = (mid - cnt[y] * y) / x;     mn = max(mn, 0LL);     if (mn >= cnt[x]) return cnt[x] * cnt[y];     long long res = mn * cnt[y];     mn++;     long long mx = mid / x;     mx = min(mx, cnt[x]);     if (mn <= mx) res += anol_gcd(mx - mn, x, mid - mx * x, y);     return res;   }   inline long long check(long long mid) {     long long res = 0;     int now = 1;     for (int i = 1; i <= 100000; i++) {       now = max(now, i);       long long hh = min(mid / i, cnt[i]);       if (cnt[i]) res += calc(cnt[i] - hh + 1, cnt[i]);       while (now + 1 <= 100000 && pre[now + 1] - pre[i - 1] <= mid) now++;       res += (sum[now] - sum[i]) * cnt[i];       mx[i] = now;     }     for (int i = 1; i <= 100000; i++)       for (int j = mx[i] + 1; j <= min(mx[i + 1] + 1, 100000); j++)         res += gao(i, j, mid);     return res;   }   int main() {     n = rd();     for (int i = 1; i <= n; i++) num[i] = rd();     cnt[num[1]]++;     h[tt = 1] = pair<int, int>(1, num[1]);     for (int i = 2; i <= n; i++) {       t1 = tt;       for (int j = 1; j <= t1; j++)         h1[j] = h[j], h1[j].second = gcd(h1[j].second, num[i]);       h1[++t1] = pair<int, int>(1, num[i]);       tt = 0;       for (int j = 1; j <= t1;) {         int k = j, now = 0;         for (; k <= t1 && h1[k].second == h1[j].second; k++) now += h1[k].first;         h[++tt] = pair<int, int>(now, h1[j].second);         j = k;       }       for (int j = 1; j <= tt; j++) cnt[h[j].second] += h[j].first;     }     for (int i = 1; i <= 100000; i++)       pre[i] = pre[i - 1] + cnt[i] * i, sum[i] = sum[i - 1] + cnt[i];     long long all = (long long)n * (n + 1) / 2;     all = all * (all + 1) / 2;     all = (all + 1) / 2;     long long l = 0, r = (long long)n * (n + 1) / 2 * 100000;     while (l < r) {       long long mid = (l + r) >> 1;       if (check(mid) >= all)         r = mid;       else         l = mid + 1;     }     printf( %lld n , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, a[101], m;   int main() {     m = 101;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (k % a[i] == 0) m = min(m, k / a[i]);     }     cout << m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cnt[50];   int main() {     int n, k;     cin >> n >> k;     int aa;     for (int i = 0; i < n; i++) {       int tmp = 0;       for (int j = k - 1; j >= 0; j--) {         scanf( %d , &aa);         tmp += (aa << j);       }       cnt[tmp]++;     }     if (cnt[0]) {       cout <<  YES ;       return 0;     }     int tot = (1 << k) - 1;     for (int i = 1; i <= tot; i++) {       if (cnt[i]) {         for (int j = 1; j <= tot; j++) {           if (cnt[j] && (i & j) == 0) {             cout <<  YES ;             return 0;           }         }       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     x = 0;     int f = 1;     char c = getchar();     while (c >  9  || c <  0 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 )       x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();     x *= f;   }   const int maxn = 100100;   struct node {     int v, nxt;   } e[maxn];   int head[maxn], tot;   inline void add(int x, int y) {     tot++;     e[tot].v = y;     e[tot].nxt = head[x];     head[x] = tot;   }   struct AA {     int s, num;   };   map<string, int> h;   vector<AA> ask[maxn];   set<int> hip[maxn];   int n, q, s, t, cnt, name[maxn], dep[maxn], son[maxn], size[maxn], ans[maxn];   bool vis[maxn];   char ch[30];   inline void dfs1(int x, int fa) {     size[x] = 1;     dep[x] = dep[fa] + 1;     for (int i = head[x]; i; i = e[i].nxt) {       int v = e[i].v;       if (v == fa) continue;       dfs1(v, x);       size[x] += size[v];       if (son[x] == 0 || size[v] > size[son[x]]) son[x] = v;     }   }   inline void calc(int x, int fa) {     hip[dep[x]].insert(name[x]);     for (int i = head[x]; i; i = e[i].nxt) {       int v = e[i].v;       if (v == fa || vis[v]) continue;       calc(v, x);     }   }   inline void dfs2(int x, int fa, int keep) {     for (int i = head[x]; i; i = e[i].nxt) {       int v = e[i].v;       if (v == fa || v == son[x]) continue;       dfs2(v, x, 0);     }     if (son[x]) dfs2(son[x], x, 1), vis[son[x]] = true;     calc(x, fa);     vis[son[x]] = false;     for (int i = 0; i < ask[x].size(); i++) {       if (dep[x] + ask[x][i].s <= n)         ans[ask[x][i].num] = hip[dep[x] + ask[x][i].s].size();     }     if (!keep) {       for (int i = dep[x]; !hip[i].empty(); i++) {         hip[i].clear();       }     }   }   int main() {     read(n);     for (int i = 1; i <= n; i++) {       scanf( %s , ch);       if (h.find(ch) == h.end()) h[ch] = ++cnt;       name[i] = h[ch];       read(s), add(s, i);     }     read(q);     AA tmp;     for (int i = 1; i <= q; i++) {       read(s), read(t);       tmp.s = t;       tmp.num = i;       ask[s].push_back(tmp);     }     dep[0] = -1;     dfs1(0, 0);     dfs2(0, 0, 1);     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[250005];   int visited[250005];   int n, m, x, y;   void zero() {     for (int i = 0; i <= n + 2; i++) {       visited[i] = 0;       v[i].clear();     }   }   void bipartite(int u) {     if (visited[u] == 1) {       x++;     } else if (visited[u] == 2) {       y++;     }     for (int i = 0; i < v[u].size(); i++) {       int a = v[u][i];       if (visited[a] == 0 && visited[u] == 1) {         visited[a] = 2;         bipartite(a);       } else if (visited[a] == 0 && visited[u] == 2) {         visited[a] = 1;         bipartite(a);       }     }   }   int main() {     int i, a, b, t;     scanf( %d , &t);     while (t--) {       scanf( %d %d , &n, &m);       zero();       for (i = 1; i <= m; i++) {         scanf( %d %d , &a, &b);         v[a].push_back(b);         v[b].push_back(a);       }       visited[1] = 1;       x = 0;       y = 0;       bipartite(1);       printf( %d n , min(x, y));       if (x <= y) {         for (i = 1; i <= n; i++) {           if (visited[i] == 1) {             printf( %d  , i);           }         }       } else {         for (i = 1; i <= n; i++) {           if (visited[i] == 2) {             printf( %d  , i);           }         }       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   using namespace std;   using ll = long long;   using db = long double;   using ii = pair<int, int>;   const int N = 1e7 + 5, LG = 19, MOD = 1e9 + 7;   const int SQ = 225;   const long double EPS = 1e-7;   int fast(int b, int e) {     int res = 1;     for (; e; e >>= 1, b = 1ll * b * b % MOD)       if (e & 1) res = 1ll * res * b % MOD;     return res;   }   vector<int> adj[10005];   int n, k;   map<int, map<int, ll>> mat;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     vector<int> deg(n + 1);     for (int i = 1; i <= k; i++)       for (int j = i + 1; j <= k; j++) {         adj[j].push_back(i);         deg[j]++;         deg[i]++;       }     for (int i = k + 1; i <= n; i++) {       for (int j = 0; j < k; j++) {         int x;         cin >> x;         adj[i].push_back(x);         deg[i]++;         deg[x]++;       }     }     for (int i = 1; i <= n; i++) {       mat[i][i] = deg[i];       for (auto x : adj[i]) {         mat[i][x] = MOD - 1;         mat[x][i] = MOD - 1;       }     }     ll ans = 1;     for (int i = n; i >= 2; --i) {       ll v = mat[i][i];       ll inv = fast(MOD - v, MOD - 2);       ans = 1ll * ans * v % MOD;       for (auto j : adj[i]) {         ll coff = inv * mat[j][i] % MOD;         for (auto k : adj[i]) {           mat[j][k] = (mat[j][k] + mat[i][k] * coff) % MOD;         }       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long powermodm(long long x, long long n, long long M) {     long long result = 1;     while (n > 0) {       if (n % 2 == 1) result = (result * x) % M;       x = (x * x) % M;       n = n / 2;     }     return result;   }   long long power(long long _a, long long _b) {     long long _r = 1;     while (_b) {       if (_b % 2 == 1) _r = (_r * _a);       _b /= 2;       _a = (_a * _a);     }     return _r;   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long lcm(long long a, long long b) {     return (max(a, b) / gcd(a, b)) * min(a, b);   }   long long modInverse(long long x) { return powermodm(x, mod - 2, mod); }   long long fact[100005];   long long ncrmodm(long long n, long long r, long long m) {     return (fact[n] * (modInverse(fact[n - r]) * modInverse(fact[r])) % m) % m;   }   void init_fact() {     fact[0] = 0;     for (long long i = 1; i < 100001; i++) fact[i] = fact[i - 1] * i;   }   void write(long long a[], long long n) {     for (long long i = 0; i < n; i++) cout << a[i] <<    ;     cout << endl;   }   void show(vector<long long> v) {     for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ;     cout << endl;   }   vector<vector<long long> > vec;   vector<long long> visited;   void dfs(int x, int p = -1) {     visited[x] = 1;     for (int i = 0; i < vec[x].size(); i++) {       if (p == vec[x][i]) continue;       if (!visited[vec[x][i]]) {         visited[vec[x][i]] = 1;         dfs(vec[x][i], x);       }     }   }   bool prime[100000];   void seive() {     int n = 100000;     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }   }   void solve() {     long long n, k;     cin >> n >> k;     long long a[n];     long long ma = -INT_MAX, mi = INT_MAX;     for (long long i = 0; i < n; i++) {       cin >> a[i];       ma = max(ma, a[i]);       mi = min(mi, a[i]);     }     if (k % 2) {       for (long long i = 0; i < n; i++) {         cout << ma - a[i] <<    ;       }       cout << endl;     } else {       for (long long i = 0; i < n; i++) {         cout << -mi + a[i] <<    ;       }       cout << endl;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void extgcd(long long a, long long b, long long& x, long long& y) {     if (b == 0) {       assert(a == 1);       x = 1, y = 0;       return;     }     extgcd(b, a % b, y, x);     y -= a / b * x;   }   int inv(int a, int c) {     long long x, y;     extgcd(a, c, x, y);     return (x % c + c) % c;   }   long long modpow(long long a, int b, int c) {     long long res = 1;     for (; b; b >>= 1) {       if (b & 1) res = res * a % c;       a = a * a % c;     }     return res;   }   const int MAXN = 100005, MAXT = 262150;   int num[10], tot, n, m, Q, tn;   struct Value {     int cnt[10];     long long rest;     void clear() {       memset(cnt, 0, sizeof(cnt));       rest = 1;     }     Value() { clear(); }     Value& operator*=(int x) {       for (int i = 0; i < tot; i++)         while (x % num[i] == 0) x /= num[i], ++cnt[i];       rest = rest * x % m;       return *this;     }     Value& operator/=(int x) {       for (int i = 0; i < tot; i++)         while (x % num[i] == 0) x /= num[i], --cnt[i];       rest = rest * inv(x, m) % m;       return *this;     }     Value& operator*=(const Value& v) {       for (int i = 0; i < tot; i++) cnt[i] += v.cnt[i];       rest = rest * v.rest % m;       return *this;     }     long long get_val() {       long long res = rest;       for (int i = 0; i < tot; i++) res = res * modpow(num[i], cnt[i], m) % m;       return res;     }   } tag[MAXT];   long long sum[MAXT];   void build(int l = 1, int r = tn, int k = 1) {     if (l == r) return;     int mid = (l + r) >> 1;     build(l, mid, k << 1);     build(mid + 1, r, k << 1 | 1);     sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m;   }   void pushdown(int k) {     int flag = 1;     for (int i = 0; i < tot; i++)       if (tag[k].cnt[i]) {         flag = 0;         break;       }     if (flag && tag[k].rest == 1) return;     int ls = k << 1, rs = k << 1 | 1;     tag[ls] *= tag[k], tag[rs] *= tag[k];     long long v = tag[k].get_val();     (sum[ls] *= v) %= m, (sum[rs] *= v) %= m;     tag[k].clear();   }   void mul(int a, int b, int x, int l = 1, int r = tn, int k = 1) {     if (a > r || b < l) return;     if (a <= l && b >= r) {       tag[k] *= x, (sum[k] *= x) %= m;       return;     }     pushdown(k);     int mid = (l + r) >> 1;     mul(a, b, x, l, mid, k << 1);     mul(a, b, x, mid + 1, r, k << 1 | 1);     sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m;   }   void divi(int a, int x, int l = 1, int r = tn, int k = 1) {     if (l == r) {       tag[k] /= x;       sum[k] = tag[k].get_val();       return;     }     int mid = (l + r) >> 1;     pushdown(k);     if (a <= mid)       divi(a, x, l, mid, k << 1);     else       divi(a, x, mid + 1, r, k << 1 | 1);     sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m;   }   int query(int a, int b, int l = 1, int r = tn, int k = 1) {     if (a > r || b < l) return 0;     if (a <= l && b >= r) return sum[k];     pushdown(k);     int mid = (l + r) >> 1;     return (query(a, b, l, mid, k << 1) + query(a, b, mid + 1, r, k << 1 | 1)) %            m;   }   void pre() {     int kk = m;     for (int i = 2; i * i <= kk; i++)       if (kk % i == 0) {         while (kk % i == 0) kk /= i;         num[tot++] = i;       }     if (kk > 1) num[tot++] = kk;   }   int main() {     scanf( %d%d , &n, &m);     pre();     for (tn = 1; tn < n; tn <<= 1)       ;     for (int i = 1; i <= n; i++) {       scanf( %lld , &sum[i + tn - 1]);       tag[i + tn - 1] *= sum[i + tn - 1];       sum[i + tn - 1] %= m;     }     build();     scanf( %d , &Q);     while (Q--) {       int a, b, c, d;       scanf( %d%d%d , &a, &b, &c);       if (a == 1) {         scanf( %d , &d);         mul(b, c, d);       } else if (a == 2)         divi(b, c);       else         printf( %d n , query(b, c));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const double eps = 1e-9;   const int inf = 2000000000;   const long long infLL = 9000000000000000000;   inline bool checkBit(long long n, int i) { return n & (1LL << i); }   inline long long setBit(long long n, int i) { return n | (1LL << i); }   inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }   int dx4[] = {0, 0, +1, -1};   int dy4[] = {+1, -1, 0, 0};   int dx8[] = {+1, 0, -1, 0, +1, +1, -1, -1};   int dy8[] = {0, +1, 0, -1, +1, -1, +1, -1};   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline bool isLeapYear(long long year) {     return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);   }   inline bool isIntege(double num) { return (num == (int)num); }   inline bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) {     return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);   }   inline double coDist(double x1, double y1, double x2, double y2) {     return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));   }   inline double TriangleAreaWithSide(double a, double b, double c) {     double s = (a + b + c) / 2;     double area = sqrt(s * (s - a) * (s - b) * (s - c));     return area;   }   inline double area3(double x1, double y1, double x2, double y2, double x3,                       double y3) {     double A = abs((x1 * y2 + x2 * y3 + x3 * y1) - (y1 * x2 + y2 * x3 + y3 * x1));     A /= 2.0;     return A;   }   inline long double degreetoradian(long double x) {     long double val = PI * x;     val /= (180.0);     return val;   }   inline void normal(long long &a) {     a %= 1000000007;     (a < 0) && (a += 1000000007);   }   template <typename T>   inline T gcd(T a, T b) {     T c;     while (b) {       c = b;       b = a % b;       a = c;     }     return a;   }   inline long long modMul(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a * b) % 1000000007;   }   inline long long modAdd(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a + b) % 1000000007;   }   inline long long modSub(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     a -= b;     normal(a);     return a;   }   inline long long modPow(long long b, long long p) {     long long r = 1;     while (p) {       if (p & 1) r = modMul(r, b);       b = modMul(b, b);       p >>= 1;     }     return r;   }   inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }   inline long long modDiv(long long a, long long b) {     return modMul(a, modInverse(b));   }   inline bool equalTo(double a, double b) {     if (fabs(a - b) <= eps)       return true;     else       return false;   }   inline bool notEqual(double a, double b) {     if (fabs(a - b) > eps)       return true;     else       return false;   }   inline bool lessThan(double a, double b) {     if (a + eps < b)       return true;     else       return false;   }   inline bool lessThanEqual(double a, double b) {     if (a < b + eps)       return true;     else       return false;   }   inline bool greaterThan(double a, double b) {     if (a > b + eps)       return true;     else       return false;   }   inline bool greaterThanEqual(double a, double b) {     if (a + eps > b)       return true;     else       return false;   }   inline string to_s(int t) {     stringstream ss;     ss << t;     return ss.str();   }   struct edge {     int p, q, w;   };   bool cmp(edge &a, edge &b) { return a.w < b.w; }   template <typename first, typename second>   ostream &operator<<(ostream &os, const pair<first, second> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  } ;   }   template <typename T>   ostream &operator<<(ostream &os, const set<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename T>   ostream &operator<<(ostream &os, const multiset<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename first, typename second>   ostream &operator<<(ostream &os, const map<first, second> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << it->first <<   =   << it->second;     }     return os <<  ] ;   }   clock_t tStart = clock();   void faltu() { cerr <<   n ; }   template <typename T>   void faltu(T a[], int n) {     for (int i = 0; i < n; ++i) cerr << a[i] <<    ;     cerr <<   n ;   }   template <typename T, typename... hello>   void faltu(T arg, const hello &...rest) {     cerr << arg <<    ;     faltu(rest...);   }   const int mx = 1e5 + 5;   bool cnt[mx];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, k;     cin >> n >> k;     long long g;     cin >> g;     for (int i = 1; i < n; i++) {       long long x;       cin >> x;       g = gcd(x, g);     }     vector<long long> st;     for (long long i = 0, j = 0; i < k; i++, j += g) {       int z = j % k;       if (!cnt[z]) st.emplace_back(z), cnt[z] = 1;     }     cout << st.size() <<   n ;     for (int i = 0; i < k; i++) {       if (cnt[i]) cout << i <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1 << 18;   const int MOD = 2013265921, MAX = MOD / 2;   const int gen = 440564289;   long long power(long long x, int n) {     if (n <= 1) return n ? x : 1LL;     long long t = power(x, n / 2);     if (n & 1)       return (t * t % MOD) * x % MOD;     else       return t * t % MOD;   }   long long tmp[maxn];   void DFT(long long *a, long long x, int n) {     if (n == 1) {       a[0] %= MOD;       if (a[0] < 0) a[0] += MOD;       return;     }     for (int i = 0; i < n; i++) tmp[i] = a[i];     for (int i = 0; i < n; i++) a[i % 2 ? n / 2 + i / 2 : i / 2] = tmp[i];     long long *a1 = a, *a2 = a + n / 2, xx = x * x % MOD;     DFT(a1, xx, n / 2);     DFT(a2, xx, n / 2);     long long now = 1;     for (int i = 0; i < n / 2; i++, now = now * x % MOD) {       long long val = now * a2[i] % MOD;       tmp[i] = a1[i] + val - MOD;       tmp[i + n / 2] = a1[i] - val;     }     for (int i = 0; i < n; i++) a[i] = (tmp[i] < 0 ? tmp[i] + MOD : tmp[i]);   }   void mul(long long *a, long long *b, long long *c, int n) {     long long x = power(gen, (1 << 27) / n), xinv = power(x, n - 1);     long long ninv = power(n, MOD - 2);     DFT(a, x, n);     DFT(b, x, n);     for (int i = 0; i < n; i++) c[i] = a[i] * b[i] % MOD;     DFT(c, xinv, n);     for (int i = 0; i < n; i++) {       c[i] = c[i] * ninv % MOD;       if (c[i] > MAX) c[i] -= MOD;     }   }   int n, m, Q, L;   char s[maxn], t[maxn];   unsigned long long sbit[maxn], tbit[maxn];   int match(int x, int y, int len) {     int ret = 0;     for (; len >= 64; len -= 64, x += 64, y += 64)       ret += __builtin_popcountll(sbit[x] ^ tbit[y]);     if (len)       ret +=           __builtin_popcountll((sbit[x] << (64 - len)) ^ (tbit[y] << (64 - len)));     return ret;   }   long long *poly[maxn], a[maxn], b[maxn];   void precal() {     n = strlen(s), m = strlen(t);     for (int i = 0; i < n; i++)       for (int j = 0; j < 64 && i + j < n; j++)         if (s[i + j] ==  1 ) sbit[i] |= (1ULL << j);     for (int i = 0; i < m; i++)       for (int j = 0; j < 64 && i + j < m; j++)         if (t[i + j] ==  1 ) tbit[i] |= (1ULL << j);     L = n * sqrt(150 * log2(n) / Q);     int n2 = 1;     while (n2 < L + m) n2 *= 2;     for (int i = 0; i < n; i += L) {       poly[i / L] = new long long[n2];       memset(a, 0, sizeof(a));       memset(b, 0, sizeof(b));       for (int j = 0; j < L && i + j < n; j++) a[j] = (s[i + j] ==  0  ? -1 : 1);       for (int j = 0; j < m; j++) b[m - 1 - j] = (t[j] ==  0  ? -1 : 1);       mul(a, b, poly[i / L], n2);     }   }   int main() {     scanf( %s%s%d , s, t, &Q);     precal();     while (Q--) {       int x, y, l;       scanf( %d%d%d , &x, &y, &l);       if (l <= L) {         printf( %d n , match(x, y, l));         continue;       }       int ans = 0;       if (x % L) {         int l2 = L - x % L;         ans += match(x, y, l2);         x += l2;         y += l2;         l -= l2;       }       if ((x + l) % L) {         int l2 = (x + l - 1) % L + 1;         ans += match(x + l - l2, y + l - l2, l2);         l -= l2;       }       if (!l) {         printf( %d n , ans);         continue;       }       for (int i = x, j = y; l; i += L, j += L, l -= L)         ans += (L - poly[i / L][m - 1 - j]) / 2;       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int arr[503][503];   int main() {     int n, k;     cin >> n >> k;     int p = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j < k; j++) {         arr[i][j] = p++;       }     }     for (int i = 1; i <= n; i++) {       for (int j = k; j <= n; j++) {         arr[i][j] = p++;       }     }     int sum = 0;     for (int i = 1; i <= n; i++) {       sum += arr[i][k];     }     cout << sum << endl;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cout << arr[i][j] <<    ;       }       cout << endl;     }     return 0;   }