#include <bits/stdc++.h>   using namespace std;   const int MAX = 100005;   int N;   bool vis[MAX];   vector<pair<int, bool> > neigh[MAX];   bool islucky(int a) {     while (a > 0) {       if (a % 10 != 4 and a % 10 != 7) return false;       a /= 10;     }     return true;   }   long long p3(long long N) { return (N) * (N - 1) * (N - 2); }   long long solve(int a) {     vector<int> q;     int qnum = 0;     q.push_back(a);     while (qnum < q.size()) {       int cur = q[qnum];       vis[cur] = true;       for (int i = 0; i < neigh[cur].size(); i++) {         if (neigh[cur][i].second) continue;         int cneigh = neigh[cur][i].first;         if (vis[cneigh]) continue;         q.push_back(cneigh);       }       qnum++;     }     long long sz = q.size();     return (2 * (sz) * (sz - 1) * ((long long)N - sz) + p3(sz));   }   int main() {     cin >> N;     for (int i = 0; i < N - 1; i++) {       int a, b, c;       cin >> a >> b >> c;       a--;       b--;       bool il = islucky(c);       neigh[a].push_back(pair<int, bool>(b, il));       neigh[b].push_back(pair<int, bool>(a, il));     }     long long ans = p3(N);     for (int i = 0; i < MAX; i++) vis[i] = false;     for (int i = 0; i < N; i++) {       if (vis[i]) continue;       ans -= solve(i);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, l, r, ql, qr;   int w[100010];   int main() {     cin >> n >> l >> r >> ql >> qr;     for (int i = 1; i <= n; ++i) cin >> w[i];     int f[100010];     f[0] = 0;     for (int i = 1; i <= n; ++i) f[i] = f[i - 1] + w[i];     int ans = INT_MAX;     for (int i = 0; i <= n; ++i) {       int v = f[i] * l + (f[n] - f[i]) * r;       if (i < n / 2)         v += qr * max(0, (n - 2 * i - 1));       else         v += ql * max(0, (2 * i - n - 1));       ans = min(ans, v);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int m, k = 1, i;     for (i = 0; i < s.size(); i++) {       m = int(s[i]) - 96;       if (m == k)         k++;       else if (m > k) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   int n, i, j, x, y, a[179000];   int main() {     for (cin >> n >> x >> y, i = 1; i <= n; i++) cin >> a[i];     for (i = 1; i <= n; i++) {       for (j = max(1, i - x); j <= min(n, i + y); j++)         if (i != j && a[j] <= a[i]) break;       if (j > min(n, i + y)) break;     }     cout << i;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     double arr[n];     double temp = 0;     for (int i = 0; i < n; i++) {       cin >> temp;       arr[i] = temp;     }     long long sum = 0;     double fractpart, intpart;     int arr2[n];     for (int i = 0; i < n; i++) {       arr2[i] = ((int)floor(arr[i]));       sum += arr2[i];     }     int count = -sum;     int total = 0, index = 0;     while (count != 0) {       if (arr[index] - floor(arr[index]) < 0.000001) {         index++;         continue;       }       count--;       arr2[index]++;       index++;     }     for (int i = 0; i < n; i++) {       cout << arr2[i] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   string A, B;   int a[N], b[N];   int main() {     while (cin >> A >> B) {       int len1 = A.size(), len2 = B.size();       for (int i = 0; i <= len1 - 1; i++) a[i] = A[i] -  0 ;       for (int i = 0; i <= len2 - 1; i++) b[i] = B[i] -  0 ;       int sum = 0;       for (int i = 0; i <= len2 - 1; i++) sum ^= (a[i] ^ b[i]);       int ans = sum == 0;       for (int i = 1; i <= len1 - len2; i++) {         sum ^= a[i - 1];         sum ^= a[i - 1 + len2];         ans += sum == 0;       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ifstream fin( test.in );   struct intr {     int l, r, id;   };   intr q[200005];   int p, n, qq;   long long sol;   int a[200005];   long long rez[200005];   int mp[5 * 200005];   bool cmp(intr &q1, intr &q2) {     return (q1.r / p < q2.r / p || (q1.r / p == q2.r / p && q1.l < q2.l));   }   void add(int x) {     int v = mp[x];     sol = sol - 1LL * v * v * x;     sol = sol + 1LL * (v + 1) * (v + 1) * x;     mp[x]++;   }   void del(int x) {     int v = mp[x];     sol = sol - 1LL * v * v * x;     if (v >= 1) {       sol = sol + 1LL * (v - 1) * (v - 1) * x;       mp[x]--;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int i, lx, rx;     cin >> n >> qq;     for (i = 1; i <= n; i++) cin >> a[i];     p = sqrt(n);     for (i = 1; i <= qq; i++) {       cin >> q[i].l >> q[i].r;       q[i].id = i;     }     sort(q + 1, q + qq + 1, cmp);     lx = 1;     rx = 0;     sol = 0;     for (i = 1; i <= qq; i++) {       while (q[i].r > rx) add(a[++rx]);       while (q[i].r < rx) del(a[rx--]);       while (q[i].l > lx) del(a[lx++]);       while (q[i].l < lx) add(a[--lx]);       rez[q[i].id] = sol;     }     for (i = 1; i <= qq; i++) cout << rez[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long int vis1[1000000];   long long int ara1[100005], ara2[100005];   map<long long int, long long int> vis;   map<long long int, long long int> str2;   priority_queue<long long int> pq;   vector<long long int> vec;   int main() {     long long int a, b, c = 0, sum = 0, n;     scanf( %lld , &n);     for (long long int i = 1; i <= n; i++) {       scanf( %lld , &a);       long long int mx = 0;       string str;       cin >> str;       c = -1;       for (long long int i = 0; i < a; i++) {         if (str[i] !=  A  && c != -1) {           c++;           if (c > mx) mx = c;         } else if (str[i] ==  A )           c = 0;       }       cout << mx << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int const inf = 1e18;   long long int const maxn = 2e5 + 5;   long long int const mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     vector<long long int> arr(26);     for (auto &u : arr) cin >> u;     string s;     cin >> s;     long long int n = s.length();     map<long long int, long long int> mp[26];     vector<long long int> brr(n);     for (long long int i = 0; i < n; i++) {       brr[i] = arr[s[i] -  a ];       if (i != 0) brr[i] += brr[i - 1];     }     long long int ans = 0;     mp[s[0] -  a ][brr[0]]++;     for (long long int i = 1; i < n; i++) {       ans += mp[s[i] -  a ][brr[i - 1]];       mp[s[i] -  a ][brr[i]]++;     }     cout << ans <<   n ;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 20;   long long n, m, dp[N][1 << N], B, res, cb[1 << N];   bool used[N][1 << N], w[N][N];   long long cbit(long long x) {     long long c = 0;     while (x) {       c += x & 1;       x /= 2;     }     return c;   }   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (long long i = 0; i < m; ++i) {       long long a, b;       cin >> a >> b;       a--;       b--;       w[a][b] = 1;       w[b][a] = 1;     }     B = 1 << n;     for (long long i = 0; i < B; ++i) cb[i] = cbit(i);     queue<pair<long long, long long>> q;     for (long long i = 0; i < n; ++i) {       for (long long j = 0; j < n; ++j) {         for (long long ma = 0; ma < B; ++ma) {           dp[j][ma] = 0;           used[j][ma] = 0;         }       }       q.push({i, 1 << i});       dp[i][1 << i] = 1;       while (!q.empty()) {         long long v = q.front().first, mask = q.front().second, nmask;         q.pop();         for (long long u = i; u < n; ++u) {           nmask = mask | (1 << u);           if (nmask == mask || !w[u][v]) continue;           dp[u][nmask] += dp[v][mask];           if (!used[u][nmask]) {             used[u][nmask] = 1;             q.push({u, nmask});           }         }       }       long long av = 0;       for (long long j = 0; j < n; ++j) {         for (long long mask = 0; mask < B; ++mask) {           if (cb[mask] >= 3 && w[i][j] && dp[j][mask]) {             av += dp[j][mask];           }         }       }       res += av / 2;     }     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 4e5 + 100;   vector<int> vec[maxn];   int n, m, k;   int d[maxn], cnt;   bool vis[maxn];   void dfs(int x) {     vis[x] = 1;     d[++cnt] = x;     for (int i = 0; i < vec[x].size(); i++) {       if (vis[vec[x][i]]) continue;       dfs(vec[x][i]);       d[++cnt] = x;     }   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       vec[u].push_back(v);       vec[v].push_back(u);     }     dfs(1);     int x = (2 * n + k - 1) / k;     for (int i = 1; i <= k; i++) {       int c = min(x, cnt);       if (c == 0) {         cout <<  1 1  << endl;         continue;       }       cout << c <<    ;       for (int i = 1; i <= c && cnt != 0; i++, cnt--) {         cout << d[cnt] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, d;     cin >> a >> b >> c >> d;     if (abs(c - d) > 1) {       cout <<  -1 ;       return 0;     }     if (max(c, d) > min(a, b)) {       cout <<  -1 ;       return 0;     }     if (a + b <= c + d) {       cout <<  -1 ;       return 0;     }     if (c == d) {       a--;       for (int i = 1; i <= a - c; i++) cout <<  4 ;       if (a == c - 1 && b > 1) {         cout <<  7 ;         b--;       }       for (int i = 1; i <= c; i++) cout <<  47 ;       for (int i = 1; i <= b - c; i++) cout <<  7 ;       if (a - c >= 0) cout <<  4 ;     }     if (c > d) {       for (int i = 1; i <= a - c; i++) cout <<  4 ;       for (int i = 1; i <= c; i++) cout <<  47 ;       for (int i = 1; i <= b - c; i++) cout <<  7 ;     }     if (c < d) {       if (b > 1) {         cout <<  7 ;         b--;       }       for (int i = 1; i < a - c; i++) cout <<  4 ;       for (int i = 1; i <= c; i++) cout <<  47 ;       for (int i = 1; i <= b - c; i++) cout <<  7 ;       cout <<  4 ;     }   }


#include<bits/stdc++.h>   #define rep(i,a,b) for(int i=a;i<=b;i++)   #define pre(i,a,b) for(int i=a;i>=b;i--)   #define N 505   using namespace std;   int f[52][95][905][2],n,mx=450,cc=45;   char s[N];   int calc(int x,int cur,int res,int op){    if(x==n+1&&!cur)return 0;    if(abs(cur)>cc||abs(res)>mx||x==n+1)return 0x3f3f3f3f;    if(~f[x][cur+cc][res+mx][op==1])return f[x][cur+cc][res+mx][op==1];    f[x][cur+cc][res+mx][op==1]=min(calc(x,cur,res+op,op)+n-x+1,min(calc(x+1,cur*10+s[x+1]- 0 -res,res,1),calc(x+1,cur*10+s[x+1]- 0 -res,res,-1)));    return f[x][cur+cc][res+mx][op==1];   }   int main(){    scanf( %s ,s+2);n=strlen(s+2);s[1]= 0 ;    memset(f,~0,sizeof(f));    printf( %d n ,calc(0,0,0,1));    return 0;   }    /*   #include<bits/stdc++.h>   #define rep(i,a,b) for(int i=a;i<=b;i++)   #define pre(i,a,b) for(int i=a;i>=b;i--)   #define N 505   using namespace std;   int f[52][95][905][2],n,mx=450,cc=45;   char s[N];   int calc(int x,int cur,int res,int op){    if(x==n+1&&!cur)return 0;    if(abs(cur)>cc||abs(res)>mx||x==n+1)return 0x3f3f3f3f;    if(~f[x][cur+cc][res+mx][op==1])return f[x][cur+cc][res+mx][op==1];    f[x][cur+cc][res+mx][op==1]=min(calc(x,cur,res+op,op)+n-x+1,min(calc(x+1,cur*10+s[x+1]- 0 -res,res,1),calc(x+1,cur*10+s[x+1]- 0 -res,res,-1)));    return f[x][cur+cc][res+mx][op==1];   }   int main(){    scanf( %s ,s+2);n=strlen(s+2);s[1]= 0 ;    memset(f,~0,sizeof(f));    printf( %d n ,calc(0,0,0,1));    return 0;   }    */


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   long long int dp1[N], dp2[N], Sa[N], Sw[N], dp[20][N], A[N];   int P[20][N], H[N], n, q;   vector<pair<int, int> > adj[N];   void preDFS(int v, int p = -1) {     if (!~p) P[0][v] = v;     for (int i = 1; i < 20; i++) P[i][v] = P[i - 1][P[i - 1][v]];     for (pair<int, int> u : adj[v]) {       if (u.first != p) {         P[0][u.first] = v;         H[u.first] = H[v] + 1;         Sw[u.first] = Sw[v] + u.second, Sa[u.first] = Sa[v] + A[u.first];         preDFS(u.first, v);       }     }   }   int getpar(int v, int h) {     for (int i = h; i; i -= i & -i) v = P[__builtin_ctz(i)][v];     return v;   }   int LCA(int u, int v) {     if (H[u] < H[v]) swap(u, v);     u = getpar(u, H[u] - H[v]);     if (u == v) return v;     for (int i = 19; ~i; i--)       if (P[i][u] != P[i][v]) u = P[i][u], v = P[i][v];     return P[0][v];   }   void DFSdn(int v, int p = -1) {     for (pair<int, int> u : adj[v]) {       if (u.first != p)         DFSdn(u.first, v),             dp1[v] += max(0ll, dp1[u.first] - 2 * u.second + A[u.first]);     }   }   void DFSup(int v, int p = -1) {     for (pair<int, int> u : adj[v]) {       if (u.first != p) {         dp2[u.first] =             max(0ll, A[v] + dp2[v] + dp1[v] -                          max(0ll, dp1[u.first] - 2 * u.second + A[u.first]));         dp2[u.first] = max(0ll, dp2[u.first] - 2 * u.second);         DFSup(u.first, v);       }     }   }   void NIMA(int v, int p = -1) {     for (pair<int, int> u : adj[v]) {       if (u.first != p) {         dp[0][u.first] =             dp1[v] - max(0ll, dp1[u.first] - 2 * u.second + A[u.first]);         NIMA(u.first, v);       }     }   }   long long int get(int v, int h) {     long long int ret = 0;     for (int i = h; i; i -= i & -i)       ret += dp[__builtin_ctz(i)][v], v = P[__builtin_ctz(i)][v];     return ret;   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %lld , &A[i]);     for (int i = 1; i < n; i++) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       adj[u].push_back({v, w});       adj[v].push_back({u, w});     }     preDFS(1);     DFSdn(1);     DFSup(1);     NIMA(1);     for (int i = 1; i < 20; i++)       for (int j = 1; j <= n; j++)         dp[i][j] = dp[i - 1][j] + dp[i - 1][P[i - 1][j]];     for (; q; q--) {       int u, v;       scanf( %d%d , &u, &v);       if (H[u] < H[v]) swap(u, v);       if (u == v) {         printf( %lld n , dp1[u] + dp2[u] + A[u]);         continue;       }       int p = LCA(u, v);       long long int a = get(u, H[u] - H[p]), b = get(v, H[v] - H[p]);       long long int ret =           Sa[u] - Sa[p] + A[p] + Sa[v] - Sa[p] - (Sw[u] - Sw[p] + Sw[v] - Sw[p]);       printf( %lld n , a + b + dp2[p] - dp1[p] + ret + dp1[u] + dp1[v]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e6 + 10, mod = 1e9 + 7, inf = 2e9, lg = 22,                   maskm = (1 << lg) - 1, maxm = (1 << lg) + 7;   long long a[maxn], dp[maxm];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       dp[a[i]] = a[i];     }     for (int i = 1; i < maxm; i++) {       for (int j = i; j && dp[i] == 0; j -= (j & -j)) {         dp[i] = dp[i ^ (j & -j)];       }     }     for (int i = 0; i < n; i++) {       long long tmp = maskm ^ a[i];       if (dp[tmp] == 0)         cout << -1 <<    ;       else         cout << dp[tmp] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   bool vis[100010];   vector<int> dp[5][100010];   int k, l;   int a[6][6];   int dfs(int now) {     int i, j, x = 0, sum = 0;     for (i = 0; i < now; i++) x = x * 10 + a[now][i];     if (i == l - 1) return dp[1][x].size();     for (i = 0; i < dp[l - now][x].size(); i++) {       int tem = dp[l - now][x][i];       for (j = l - 1; j >= now; j--) {         a[j][now] = a[now][j] = tem % 10;         tem /= 10;       }       sum += dfs(now + 1);     }     return sum;   }   int main() {     int i, j, n;     int m = sqrt(100000.5);     for (i = 2; i <= m; i++)       if (!vis[i]) {         for (j = i * i; j <= 100000; j += i) vis[j] = 1;       }     for (i = 2; i < 100000; i++)       if (!vis[i]) {         int tem = i;         tem /= 10;         for (j = 1; j <= 4; j++) {           dp[j][tem].push_back(i);           tem /= 10;         }       }     scanf( %d , &n);     char str[7];     for (j = 1; j <= n; j++) {       scanf( %s , str);       l = strlen(str);       for (i = 0; str[i] !=   0 ; i++) {         a[0][i] = a[i][0] = str[i] -  0 ;       }       printf( %d n , dfs(1));     }   }


#include <bits/stdc++.h>   const int maxn = 5e5 + 10;   using namespace std;   int main() {     string s;     long long n;     cin >> n;     while (n--) {       cin >> s;       int b = 0, e = s.size() - 1;       while (b < e && s[b] == s[e]) b++, e--;       if (b >= e)         cout << s <<   n ;       else {         string s1, s2, cmax;         for (int i = b; i <= e; i++) {           s1 += s[i];           s2 = s1;           reverse(s2.begin(), s2.end());           if (s1 == s2) cmax = s1;         }         string s3, s4, cmax2;         for (int i = e; i >= b; i--) {           s3 += s[i];           s4 = s3;           reverse(s4.begin(), s4.end());           if (s3 == s4) cmax2 = s3;         }         if (cmax.size() > cmax2.size())           cout << s.substr(0, b) << cmax << s.substr(e + 1) << endl;         else           cout << s.substr(0, b) << cmax2 << s.substr(e + 1) << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     x = 0;     int f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= f;   }   const int MAXN = 2e3 + 7;   int n;   string ch[MAXN][2];   vector<int> g[MAXN];   inline int neg(int u) { return (u > n ? u - n : n + u); }   inline void eadd(int u, int v) { g[u].push_back(v); }   inline void iadd(int u, int v) {     eadd(u, v);     eadd(neg(v), neg(u));   }   bool mk[MAXN];   int dt[MAXN], cmp[MAXN], t = 0, cc = 0;   stack<int> stk;   int scc(int u) {     stk.push(u);     mk[u] = true;     int lw = dt[u] = ++t;     for (auto v : (g[u])) {       if (!dt[v]) {         int nl = scc(v);         lw = min(lw, nl);       } else if (mk[v])         lw = min(lw, dt[v]);     }     mk[u] = false;     if (dt[u] == lw) {       ++cc;       while (stk.top() != u) {         cmp[stk.top()] = cc;         if (cmp[neg(stk.top())] == cc) return -1;         stk.pop();       }       if (cmp[neg(stk.top())] == cc) return -1;       cmp[stk.top()] = cc;       stk.pop();     }     return lw;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = (1); i <= (n); ++i) {       string s0, s1;       cin >> s0 >> s1;       ch[i][0] = s0.substr(0, 3);       ch[i][1] = s0.substr(0, 2) + s1.substr(0, 1);     }     for (int i = (1); i <= (n); ++i)       for (int j = (1); j <= (n); ++j)         if (i != j) {           if (ch[i][0] == ch[j][0]) iadd(i, neg(j)), iadd(neg(i), neg(j));           ;           if (ch[i][1] == ch[j][1]) iadd(neg(i), j);           if (ch[i][0] == ch[j][1]) iadd(i, j);         }     for (int i = (1); i <= (2 * n); ++i)       if (!dt[i])         if (scc(i) < 0) {           cout <<  NO n ;           return 0;         }     cout <<  YES n ;     for (int i = (1); i <= (n); ++i) {       if (cmp[i] > cmp[neg(i)])         cout << ch[i][1] <<   n ;       else         cout << ch[i][0] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7, N = 100007;   int n;   long long ts, tf, t, a[N], ans = (long long)1e18, id;   int main() {     cin >> ts >> tf >> t;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     while (a[n] > tf - t) n--;     a[++n] = tf - t + 1;     long long cur = 0;     for (int i = 1; i <= n; i++) {       cur = max(cur, ts);       if (ans > cur - (a[i] - 1)) {         ans = cur - (a[i] - 1);         id = a[i] - 1;       }       if (cur < a[i]) {         cout << cur;         return 0;       }       cur = max(cur, a[i]);       cur += t;       if (cur >= tf - t + 1) break;     }     cout << id;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   int n, m, q;   long long suma[N], b[N], sumb[N];   long long solve(long long x) {     int pos = lower_bound(sumb + n, sumb + m + 1, x) - sumb;     if (pos > m) return x - sumb[m];     if (pos == n) return sumb[pos] - x;     return min(x - sumb[pos - 1], sumb[pos] - x);   }   int main() {     scanf( %d %d %d , &n, &m, &q);     for (int i = 1; i <= n; i++) {       scanf( %lld , &suma[i]);       if (i & 1)         suma[i] = suma[i - 1] + suma[i];       else         suma[i] = suma[i - 1] - suma[i];     }     for (int i = 1; i <= m; i++) {       scanf( %lld , &b[i]);     }     for (int i = 1; i <= n; i++) {       if (i & 1)         sumb[i] = sumb[i - 1] + b[i];       else         sumb[i] = sumb[i - 1] - b[i];     }     int f = -1;     if (n & 1) f = 1;     for (int i = 1; i <= m - n; i++) {       sumb[i + n] = -(sumb[i + n - 1] - b[i]) + b[i + n] * f;     }     sort(sumb + n, sumb + m + 1);     long long a = suma[n];     printf( %lld n , solve(a));     for (int i = 1; i <= q; i++) {       int l, r, x;       scanf( %d %d %d , &l, &r, &x);       if ((r - l + 1) & 1) {         if (l & 1)           a += x;         else           a -= x;       }       printf( %lld n , solve(a));     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   int n;   char s1[maxn], s2[maxn];   int main() {     scanf( %d%s%s , &n, s1 + 1, s2 + 1);     reverse(s1 + 1, s1 + n);     for (int i = 1; i < n; ++i) {       if (s1[i] ==  N )         s1[i] =  S ;       else if (s1[i] ==  S )         s1[i] =  N ;       else if (s1[i] ==  W )         s1[i] =  E ;       else         s1[i] =  W ;     }     unsigned long long h1 = 0, h2 = 0, fa = 1;     for (int i = 1; i < n; ++i) {       h1 = h1 + s1[i] * fa;       h2 = h2 * 233u + s2[n - i];       fa *= 233u;       if (h1 == h2) return puts( NO ), 0;     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     string s;     cin >> n >> s;     n += 3;     vector<int> a(n), la(n), ra(n), lmi(n), lma(n), rmi(n), rma(n);     int j = 2;     for (int i = 0; i < n - 3; i++) {       if (s[i] ==  ( ) {         a[j] = 1;       } else if (s[i] ==  ) ) {         a[j] = -1;       } else if (s[i] ==  R ) {         j++;       } else if (s[i] ==  L ) {         j--;         j = max(j, 2);       } else {         a[j] = 0;       }       la[j] = la[j - 1] + a[j];       ra[j] = ra[j + 1] - a[j];       lmi[j] = min(lmi[j - 1], la[j]);       lma[j] = max(lma[j - 1], la[j]);       rmi[j] = min(rmi[j + 1], ra[j]);       rma[j] = max(rma[j + 1], ra[j]);       if (lmi[j] >= 0 && rmi[j + 1] >= 0 && la[j] == ra[j + 1]) {         cout << max(lma[j], rma[j + 1]) <<    ;       } else {         cout <<  -1  ;       }     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ;   const long long maxn = 1e5 + 7;   const long double pi = 3.14159265358979323846;   struct pirog {     long long r;     long long h;     long long i;   };   long long V[4 * maxn];   pirog A[maxn];   vector<pirog> C;   void update(long long v, long long tl, long long tr, long long i,               long long color) {     if (tl == tr) {       V[v] = color;       return;     }     long long tm = (tl + tr) / 2;     if (tm >= i) {       update(2 * v, tl, tm, i, color);     } else {       update(2 * v + 1, tm + 1, tr, i, color);     }     V[v] = max(V[2 * v], V[2 * v + 1]);   }   long long mx(long long v, long long tl, long long tr, long long l,                long long r) {     if (tl > r || l > tr) {       return 0;     }     if (tl == l && tr == r) {       return V[v];     }     long long tm = (tl + tr) / 2;     return max(mx(2 * v, tl, tm, l, min(tm, r)),                mx(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));   }   bool cmp(pirog a, pirog b) { return a.h * a.r * a.r < b.h * b.r * b.r; }   long long X[maxn];   int main() {     long long q, w, e, k, n;     cin >> n;     cout << fixed << setprecision(10);     for (q = 0; q < n; q++) {       cin >> A[q].r >> A[q].h;       A[q].i = q;       C.push_back(A[q]);     }     sort(C.begin(), C.end(), cmp);     long long l = 0;     for (q = 0; q < n; q++) {       long long x = mx(1, 0, n - 1, 0, C[q].i) + C[q].h * C[q].r * C[q].r;       X[q] = x;       if (C[q].r * C[q].r * C[q].h == C[q + 1].r * C[q + 1].r * C[q + 1].h) {         continue;       }       for (w = l; w <= q; w++) {         update(1, 0, n - 1, C[w].i, X[w]);       }       l = q + 1;     }     cout << mx(1, 0, n - 1, 0, n - 1) * pi;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 100;   int main() {     int flag = 0;     long long n, a, b;     cin >> n >> a >> b;     long long x = a, y = b;     if (a > b) {       swap(a, b);       flag = 1;     }     if (a * b > 6 * n) {       printf( %lld n , a * b);       printf( %lld %lld n , a, b);       return 0;     }     long long ans = 0x3f3f3f3f3f3f3f3f, l = 0, r = 0;     long long len = sqrt(6 * n) + 1;     for (int i = a; i <= len + 5; i++) {       if (6 * n % i == 0 && 6 * n / i >= b) {         l = i;         r = 6 * n / i;         break;       }       y = 6 * n / i + 1;       y = max(y, b);       if (i * y < ans) {         l = i;         r = y;         ans = l * r;       }     }     if (flag) swap(l, r);     printf( %lld n , l * r);     printf( %lld %lld n , l, r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e4 + 7;   const int inf = 0x3f3f3f3f;   int a[maxn];   char s[150];   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       scanf( %s , s);       int x = 0, y = 0;       for (int i = 0; i < n; i++) {         if (s[i] ==  < )           x++;         else           break;       }       for (int i = n - 1; i >= 0; i--) {         if (s[i] ==  > )           y++;         else           break;       }       cout << min(x, y) << endl;     }     return 0;   }


#include <bits/stdc++.h>   void print(int n) {     if (n == 4) {       printf( 1 * 2 = 2 n );       printf( 3 * 4 = 12 n );       printf( 2 * 12 = 24 n );     } else if (n == 5) {       printf( 5 * 4 = 20 n );       printf( 3 + 2 = 5 n );       printf( 5 - 1 = 4 n );       printf( 20 + 4 = 24 n );     }   }   int main() {     int n;     while (~scanf( %d , &n)) {       if (n <= 3) {         printf( NO n );         continue;       }       printf( YES n );       if (n == 4)         print(4);       else if (n == 5)         print(5);       else if (n % 2) {         print(5);         for (int i = n; i > 5; i = i - 2) {           printf( %d - %d = 1 n , i, i - 1);           printf( 24 * 1 = 24 n );         }       } else {         print(4);         for (int i = n; i > 4; i = i - 2) {           printf( %d - %d = 1 n , i, i - 1);           printf( 24 * 1 = 24 n );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 666;   const int inf = 1e9;   struct edge {     int pr;     int to;     int cap;   } e[111111];   int tot = 1, la[N];   void clr() {     tot = 1;     memset(la, 0, sizeof(la));   }   void adde(int x, int y, int z) {     e[++tot].pr = la[x];     la[x] = tot;     e[tot].to = y;     e[tot].cap = z;   }   void addf(int x, int y, int z = inf) {     adde(x, y, z);     adde(y, x, 0);   }   int d[N];   int bfs(int s, int t) {     queue<int> Q;     int i, x, y;     memset(d, 0, sizeof(d));     d[s] = 1;     Q.push(s);     while (!Q.empty()) {       x = Q.front();       Q.pop();       for (i = la[x]; i; i = e[i].pr) {         if (!e[i].cap) continue;         y = e[i].to;         if (!d[y]) {           d[y] = d[x] + 1;           Q.push(y);         }       }     }     return d[t];   }   int dfs(int s, int t, int f) {     if (s == t) return f;     int i, x, y, r = 0;     for (i = la[s]; i; i = e[i].pr) {       if (!e[i].cap || !f) continue;       y = e[i].to;       if (d[y] != d[s] + 1) continue;       x = dfs(y, t, min(f, e[i].cap));       if (x) {         f -= x;         r += x;         e[i].cap -= x;         e[i ^ 1].cap += x;       }     }     if (!r) d[s] = 0;     return r;   }   int maxflow(int s, int t) {     int x, r = 0;     while (bfs(s, t)) {       x = dfs(s, t, inf);       if (x)         r += x;       else         break;     }     return r;   }   int vis[N], w[N];   bool hungary(int x) {     int i, y;     for (i = la[x]; i; i = e[i].pr) {       y = e[i].to;       if (!vis[y]) {         vis[y] = 1;         if (!w[y] || hungary(w[y])) {           w[y] = x;           return true;         }       }     }     return false;   }   int n, a[N][N];   int main() {     int i, j, o, s, t, ans;     scanf( %d , &n);     clr();     for (i = 1; i <= n; i = i + 1) {       scanf( %d , &o);       while (o--) {         scanf( %d , &j);         a[i][j] = 1;         adde(i, j, 0);       }     }     for (i = 1; i <= n; i = i + 1) {       memset(vis, 0, sizeof(vis));       hungary(i);     }     clr();     s = 0, t = n + 1;     for (i = 1; i <= n; i = i + 1)       for (j = 1; j <= n; j = j + 1)         if (a[i][j]) addf(i, w[j]);     ans = 0;     for (i = 1; i <= n; i = i + 1) {       scanf( %d , &o);       if (o > 0)         addf(i, t, o);       else {         ans += o;         addf(s, i, -o);       }     }     printf( %d , ans + maxflow(s, t));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <class T>   T my_pow(T n, T p) {     if (p == 0) return 1;     T x = my_pow(n, p / 2);     x = (x * x);     if (p & 1) x = (x * n);     return x;   }   template <class T>   T big_mod(T n, T p, T m) {     if (p == 0) return (T)1;     T x = big_mod(n, p / 2, m);     x = (x * x) % m;     if (p & 1) x = (x * n) % m;     return x;   }   template <class T>   inline T Mod(T n, T m) {     return (n % m + m) % m;   }   template <class T>   T extract(string s, T ret) {     stringstream ss(s);     ss >> ret;     return ret;   }   template <class T>   string itos(T n) {     ostringstream ost;     ost << n;     ost.flush();     return ost.str();   }   long long stoi(string s) {     long long r = 0;     istringstream sin(s);     sin >> r;     return r;   }   double toDouble(string s) {     double r = 0;     istringstream sin(s);     sin >> r;     return r;   }   long long ar[2000005], ar1[200005];   long long a = 0, b = 0, c = 0, r = 0, rr = 0, res = 0, n, m;   string s, ss;   long long fun(long long val) {     r = 0;     for (__typeof(n) i = 0; i < (n); i++) {       if (val < ar[i]) return false;       r += (val - ar[i]);     }     if (r >= val)       return true;     else       return false;   }   int main() {     cin >> n;     for (__typeof(n) i = 0; i < (n); i++) {       cin >> ar[i];     }     long long lo = 0, hi = 10000000000ll;     while (hi - lo > 1) {       long long mid = (lo + hi) >> 1;       if (fun(mid))         hi = mid;       else         lo = mid;     }     cout << hi;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, i, a[100100], b[100100];   priority_queue<pair<long long, int>> q;   long long m, mx, mn, cur, cnt, le, ri, h;   int fnd(int i, long long v) {     int lft = 0, rgh = a[i];     while (lft < rgh) {       int mid = (lft + rgh) / 2 + 1;       cur = a[i] - 3LL * mid * (mid + 1LL) - 1LL;       if (cur < v)         rgh = mid - 1;       else         lft = mid;     }     return rgh;   }   int main() {     scanf( %d%I64d , &n, &m);     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       q.push({a[i] - 1LL, i});       cur = a[i] - 3LL * a[i] * (a[i] + 1LL) - 1LL;       if (i) {         mn = min(mn, cur);         mx = max(mx, a[i] - 1LL);       } else {         mn = cur;         mx = a[i] - 1LL;       }     }     le = 0;     ri = mx - mn;     while (le < ri) {       h = (le + ri) / 2;       for (cnt = i = 0; i < n; i++) cnt += fnd(i, mx - h);       if (cnt < m)         le = h + 1;       else         ri = h;     }     for (i = 0; i < n; i++) {       b[i] = fnd(i, mx - ri + 1);       m -= b[i];     }     for (i = 0; i < n && m > 0; i++) {       x = min(1LL * fnd(i, mx - ri) - b[i], m);       b[i] += x;       m -= x;     }     for (i = 0; i < n; i++) printf( %d  , b[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   const long long INFLL = 1e18 + 1;   const int MAX = 200001;   const long long MOD = 1000000007;   long long inq(long long k, long long q) {     if (q == 0) return 1;     long long l = inq(k, q / 2);     if (q % 2 == 0)       return l * l % MOD;     else       return l * l * k % MOD;   }   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     if (b == 0) return a;     return gcd(b, a % b);   }   long long cubr(long long a) {     long long l = -1, r = 1e6 + 2;     while (l < r - 1) {       long long mid = (l + r) / 2;       if (mid * mid * mid > a)         r = mid;       else         l = mid;     }     return l;   }   long long max(long long a, long long b) {     if (a > b) return a;     return b;   }   long long min(long long a, long long b) { return -1 * max(-a, -b); }   long long possible(long long q) {     if (q == INF) return -1;     return q;   }   bool correct(int x, int xx) {     if (x < 0) return 0;     if (x >= xx) return 0;     return 1;   }   long long dsumm(long long x, long long k) {     long long y = 1;     long long z = 0;     for (int i = 0; y < 1e18; i++) {       z += x / y % k;       y *= k;     }     return z;   }   long long dcount(long long x) {     long long y = 1;     long long z = 0;     int c[100];     for (int i = 0; i < 10; i++) c[i] = 0;     for (int i = 0; x > 0; i++) {       if (c[x / y % 10] == 0) z++;       c[x / y % 10] = 1;       x /= 10;     }     return z;   }   long long lg10(long long x) {     if (x == 0) return 0;     return lg10(x / 10) + 1;   }   string months[12] = { January ,    February ,  March ,     April ,                         May ,        June ,      July ,      August ,                         September ,  October ,   November ,  December };   int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   vector<int> cx, cy;   long long n, m;   long long x[50];   long long y[50];   long long x2[50];   long long y2[50];   long long a[52][52];   long long b[52][52];   vector<pair<pair<int, int>, pair<int, int>>> ans1, ans2, ans3;   void Move_left_p1(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (a[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (j > 1) {       if (a[i][j - 1] == 0) {         ans1.push_back({{i, j}, {i, j - 1}});         j--;         a[i][j] = num;         a[i][j + 1] = 0;       } else         break;     }   }   void Move_right_p1(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (a[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (j < n) {       if (a[i][j + 1] == 0) {         ans1.push_back({{i, j}, {i, j + 1}});         j++;         a[i][j] = num;         a[i][j - 1] = 0;       } else         break;     }   }   void Move_up_p1(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (a[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (i > 1) {       if (a[i - 1][j] == 0) {         ans1.push_back({{i, j}, {i - 1, j}});         i--;         a[i][j] = num;         a[i + 1][j] = 0;       } else         break;     }   }   void collectA() {     for (int i = 1; i <= 1; i++) {       for (int j = 1; j <= n; j++) {         if (a[i][j] != 0) {           Move_left_p1(a[i][j]);         }       }     }     for (int i = 2; i <= n; i++) {       for (int j = n; j >= 1; j--) {         if (a[i][j] != 0) {           int id = a[i][j];           Move_right_p1(id);           Move_up_p1(id);           Move_left_p1(id);         }       }     }   }   void Move_left_p3(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (b[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (j > 1) {       if (b[i][j - 1] == 0) {         ans3.push_back({{i, j}, {i, j - 1}});         j--;         b[i][j] = num;         b[i][j + 1] = 0;       } else         break;     }   }   void Move_right_p3(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (b[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (j < n) {       if (b[i][j + 1] == 0) {         ans3.push_back({{i, j}, {i, j + 1}});         j++;         b[i][j] = num;         b[i][j - 1] = 0;       } else         break;     }   }   void Move_up_p3(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (b[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (i > 1) {       if (b[i - 1][j] == 0) {         ans3.push_back({{i, j}, {i - 1, j}});         i--;         b[i][j] = num;         b[i + 1][j] = 0;       } else         break;     }   }   void collectB() {     for (int i = 1; i <= 1; i++) {       for (int j = 1; j <= n; j++) {         if (b[i][j] != 0) {           Move_left_p3(b[i][j]);         }       }     }     for (int i = 2; i <= n; i++) {       for (int j = n; j >= 1; j--) {         if (b[i][j] != 0) {           int id = b[i][j];           Move_right_p3(id);           Move_up_p3(id);           Move_left_p3(id);         }       }     }   }   void Move_right_p2(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (a[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (j < n) {       if (a[i][j + 1] == 0) {         ans2.push_back({{i, j}, {i, j + 1}});         j++;         a[i][j] = num;         a[i][j - 1] = 0;       } else         break;     }   }   void Move_left_p2(int num) {     int i = 0;     int j = 0;     for (int i1 = 1; i1 <= n; i1++) {       for (int j1 = 1; j1 <= n; j1++) {         if (a[i1][j1] == num) {           i = i1;           j = j1;           break;         }       }     }     while (j > 1) {       if (a[i][j - 1] == 0) {         ans2.push_back({{i, j}, {i, j - 1}});         j--;         a[i][j] = num;         a[i][j + 1] = 0;       } else         break;     }   }   void Move_right_all(int line) {     for (int i = n; i >= 1; i--) {       if (a[line][i] != 0) {         int id = a[line][i];         Move_right_p2(id);       }     }   }   void Push(int id) {     for (int i = 1; i <= n; i++) {       if (a[2][i] == id) {         a[1][i] = id;         a[2][i] = 0;         ans2.push_back({{2, i}, {1, i}});         Move_left_p2(id);       }     }   }   void reOrder() {     for (int i = 1; i <= m; i++) {       a[2][i] = a[1][i];       a[1][i] = 0;       ans2.push_back({{1, i}, {2, i}});     }     Move_right_all(2);     for (int i = 1; i <= m; i++) {       int id = b[1][i];       Push(id);       Move_right_all(2);     }   }   int main() {     cin >> n >> m;     if (n == 1) {       cout << 0;       return 0;     }     for (int(i) = 0; (i) != (m); i++) {       cin >> x[i] >> y[i];       a[x[i]][y[i]] = i + 1;     }     collectA();     for (int(i) = 0; (i) != (m); i++) {       cin >> x2[i] >> y2[i];       b[x2[i]][y2[i]] = i + 1;     }     collectB();     reOrder();     if (ans1.size() + ans2.size() + ans3.size() < 10800) {       cout << ans1.size() + ans2.size() + ans3.size() <<   n ;       for (int(i) = 0; (i) != (ans1.size()); i++) {         cout << ans1[i].first.first <<     << ans1[i].first.second <<                  << ans1[i].second.first <<     << ans1[i].second.second <<   n ;       }       for (int(i) = 0; (i) != (ans2.size()); i++) {         cout << ans2[i].first.first <<     << ans2[i].first.second <<                  << ans2[i].second.first <<     << ans2[i].second.second <<   n ;       }       for (int(i) = (ans3.size()) - 1; (i) != -1; i--) {         cout << ans3[i].second.first <<     << ans3[i].second.second <<                  << ans3[i].first.first <<     << ans3[i].first.second <<   n ;       }     } else {       for (int i = 1; i <= 50; i++) {         for (int j = 1; j <= 50; j++) {           a[i][j] = 0;           b[i][j] = 0;         }       }       ans1.resize(0);       ans2.resize(0);       ans3.resize(0);       for (int(i) = 0; (i) != (m); i++) {         a[n + 1 - x[i]][y[i]] = i + 1;       }       collectA();       for (int(i) = 0; (i) != (m); i++) {         b[n + 1 - x2[i]][y2[i]] = i + 1;       }       collectB();       reOrder();       if (ans1.size() + ans2.size() + ans3.size() < 10800) {         cout << ans1.size() + ans2.size() + ans3.size() <<   n ;         for (int(i) = 0; (i) != (ans1.size()); i++) {           cout << n + 1 - ans1[i].first.first <<     << ans1[i].first.second                <<     << n + 1 - ans1[i].second.first <<                    << ans1[i].second.second <<   n ;         }         for (int(i) = 0; (i) != (ans2.size()); i++) {           cout << n + 1 - ans2[i].first.first <<     << ans2[i].first.second                <<     << n + 1 - ans2[i].second.first <<                    << ans2[i].second.second <<   n ;         }         for (int(i) = (ans3.size()) - 1; (i) != -1; i--) {           cout << n + 1 - ans3[i].second.first <<     << ans3[i].second.second                <<     << n + 1 - ans3[i].first.first <<                    << ans3[i].first.second <<   n ;         }       } else {         for (int i = 1; i <= 50; i++) {           for (int j = 1; j <= 50; j++) {             a[i][j] = 0;             b[i][j] = 0;           }         }         ans1.resize(0);         ans2.resize(0);         ans3.resize(0);         for (int(i) = 0; (i) != (m); i++) {           a[y[i]][x[i]] = i + 1;         }         collectA();         for (int(i) = 0; (i) != (m); i++) {           b[y2[i]][x2[i]] = i + 1;         }         collectB();         reOrder();         if (ans1.size() + ans2.size() + ans3.size() < 10800) {           cout << ans1.size() + ans2.size() + ans3.size() <<   n ;           for (int(i) = 0; (i) != (ans1.size()); i++) {             cout << ans1[i].first.second <<     << ans1[i].first.first <<                      << ans1[i].second.second <<     << ans1[i].second.first <<   n ;           }           for (int(i) = 0; (i) != (ans2.size()); i++) {             cout << ans2[i].first.second <<     << ans2[i].first.first <<                      << ans2[i].second.second <<     << ans2[i].second.first <<   n ;           }           for (int(i) = (ans3.size()) - 1; (i) != -1; i--) {             cout << ans3[i].second.second <<     << ans3[i].second.first <<                      << ans3[i].first.second <<     << ans3[i].first.first <<   n ;           }         } else {           for (int i = 1; i <= 50; i++) {             for (int j = 1; j <= 50; j++) {               a[i][j] = 0;               b[i][j] = 0;             }           }           ans1.resize(0);           ans2.resize(0);           ans3.resize(0);           for (int(i) = 0; (i) != (m); i++) {             a[n + 1 - y[i]][x[i]] = i + 1;           }           collectA();           for (int(i) = 0; (i) != (m); i++) {             b[n + 1 - y2[i]][x2[i]] = i + 1;           }           collectB();           reOrder();           if (ans1.size() + ans2.size() + ans3.size() < 10800) {             cout << ans1.size() + ans2.size() + ans3.size() <<   n ;             for (int(i) = 0; (i) != (ans1.size()); i++) {               cout << n + 1 - ans1[i].first.second <<     << ans1[i].first.first                    <<     << n + 1 - ans1[i].second.second <<                        << ans1[i].second.first <<   n ;             }             for (int(i) = 0; (i) != (ans2.size()); i++) {               cout << n + 1 - ans2[i].first.second <<     << ans2[i].first.first                    <<     << n + 1 - ans2[i].second.second <<                        << ans2[i].second.first <<   n ;             }             for (int(i) = (ans3.size()) - 1; (i) != -1; i--) {               cout << n + 1 - ans3[i].second.second <<     << ans3[i].second.first                    <<     << n + 1 - ans3[i].first.second <<                        << ans3[i].first.first <<   n ;             }           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n, m, arr[303][303];       scanf( %d %d , &n, &m);       bool can = true;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           scanf( %d , &arr[i][j]);           if (i == 0 || i == n - 1) {             if (j > 0 && j < m - 1) {               if (arr[i][j] > 3) can = false;               arr[i][j] = 3;             } else {               if (arr[i][j] > 2) can = false;               arr[i][j] = 2;             }           } else if (j == 0 || j == m - 1) {             if (i > 0 && i < n - 1) {               if (arr[i][j] > 3) can = false;               arr[i][j] = 3;             } else {               if (arr[i][j] > 2) can = false;               arr[i][j] = 2;             }           } else {             if (arr[i][j] > 4) can = false;             arr[i][j] = 4;           }         }       }       if (!can) {         printf( NO n );         continue;       }       printf( YES n );       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           printf( %d%c , arr[i][j], j >= m - 1 ?   n  :    );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> g, sg;   vector<bool> vis;   void dfs(int u) {     vis[u] = 1;     for (int v : g[u]) {       if (!vis[v]) {         sg[u].push_back(v);         sg[v].push_back(u);         dfs(v);       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     g.resize(n);     sg.resize(n);     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       u--;       v--;       g[u].push_back(v);       g[v].push_back(u);     }     vis.assign(n, 0);     dfs(0);     auto find_path = [&](int u, int v) {       swap(u, v);       queue<int> qu;       vector<int> p(n, -1);       vis.assign(n, 0);       vis[u] = 1;       qu.push(u);       while (!qu.empty()) {         int x = qu.front();         qu.pop();         for (int y : sg[x]) {           if (!vis[y]) {             vis[y] = 1;             qu.push(y);             p[y] = x;           }         }       }       vector<int> ret;       for (int x = v; x != u; x = p[x]) {         ret.push_back(x);       }       ret.push_back(u);       return ret;     };     int q;     cin >> q;     vector<pair<int, int>> queries(q);     vector<int> deg(n, 0);     vector<vector<int>> ans(q);     for (int i = 0; i < q; i++) {       int u, v;       cin >> u >> v;       u--;       v--;       deg[u]++;       deg[v]++;       queries[i] = {u, v};       ans[i] = find_path(u, v);     }     int cnt_odd = 0;     for (int u = 0; u < n; u++) cnt_odd += deg[u] % 2;     if (cnt_odd) {       cout <<  NO n  << cnt_odd / 2 <<   n ;     } else {       cout <<  YES n ;       for (auto v : ans) {         cout << v.size() <<   n ;         for (int x : v) cout << x + 1 <<    ;         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<set<int> > graph;   vector<int> eliminar;   set<int>::iterator ite;   void elim(int N) {     eliminar.clear();     for (int i = 1; i <= N; i++)       if (graph[i].size() == 1) eliminar.push_back(i);   }   int main() {     int n, m, n1, n2, temp, contador;     cin >> n >> m;     contador = 0;     graph.resize(n + 10);     while (m--) {       cin >> n1 >> n2;       graph[n1].insert(n2);       graph[n2].insert(n1);     }     elim(n);     while (!eliminar.empty()) {       for (int i = 0; i < eliminar.size(); i++) {         graph[eliminar[i]].clear();         for (int j = 1; j <= n; j++)           if (graph[j].find(eliminar[i]) != graph[j].end())             graph[j].erase(eliminar[i]);       }       contador++;       elim(n);     }     cout << contador << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, c, f = 0;   int main() {     cin >> n >> a >> b >> c;     for (int i = 0; i <= c; i++) {       for (int j = 0; j <= b; j++) {         if (n - j - 2 * i >= 0 && n - j - 2 * i <= a / 2) {           f++;         }       }     }     cout << f;   }


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


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int a[MAXN];   int main() {     int n, tot = 0, flag = -1, num = 0;     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     sort(a, a + n);     for (int i = 0; i < n - 1; ++i) {       if (i != 0 && a[i] == a[i + 1] && a[i - 1] == a[i] - 1) flag = 1;       if (a[i] == a[i + 1]) num++;       tot += a[i] - i;     }     tot += a[n - 1] - (n - 1);     if (num >= 2) flag = 1;     if (a[0] == 0 && a[1] == 0) flag = 1;     if (flag == -1) {       if (tot % 2 == 0)         flag = 1;       else         flag = 0;     }     if (flag)       printf( cslnb );     else       printf( sjfnb );     return 0;   }


#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;   }   long long n;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     if (n == 0) {       cout <<  0 0  << endl;       return 0;     }     long long lo = 0, hi = 600000000, mid;     while (lo + 1 < hi) {       mid = (lo + hi) / 2;       if (1 + (mid * (mid + 1)) * 3 <= n + 1)         lo = mid;       else         hi = mid;     }     long long x, y;     if (lo == 0) {       x = 2, y = 0;       lo++;     } else {       n -= (lo * (lo + 1) * 3);       lo++;       x = 2 * (lo), y = 0;     }     if (n == 0) {       x -= 2;     }     for (int i = 0; i < 6 and n > 0; i++) {       if (lo <= n) {         if (i == 0) {           x -= lo;           y += 2 * lo;         } else if (i == 1) {           x -= 2 * lo;         } else if (i == 2) {           x -= lo;           y -= 2 * lo;         } else if (i == 3) {           x += lo;           y -= 2 * lo;         } else if (i == 4) {           x += 2 * lo;         } else if (i == 5) {           x += lo;           y += 2 * lo;         }         n -= lo;       } else {         lo = n;         if (i == 0) {           x -= lo;           y += 2 * lo;         } else if (i == 1) {           x -= 2 * lo;         } else if (i == 2) {           x -= lo;           y -= 2 * lo;         } else if (i == 3) {           x += lo;           y -= 2 * lo;         } else if (i == 4) {           x += 2 * lo;         } else if (i == 5) {           x += lo;           y += 2 * lo;         }         break;       }     }     cout << x <<     << y << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   void dump(const vector<int>& a) {     for (int i = 0; i < a.size(); i++) {       cerr << a[i] <<    ;     }     cerr << endl;   }   vector<pair<int, int>> factorise(long long m) {     vector<pair<int, int>> res;     for (long long i = 2; i * i <= m; i++) {       if (m % i == 0) {         int cnt = 0;         while (m % i == 0) {           cnt++;           m /= i;         }         res.emplace_back(i, cnt);       }     }     return res;   };   using ll = long long;   ll gcd(ll a, ll b, ll& x, ll& y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     ll x1, y1;     ll d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   bool debug = false;   using pii = pair<int, int>;   using pll = pair<long long, long long>;   const int magic = 3;   const int INF = 1e9;   void solve();   void init();   template <typename T, int N>   void dump(const array<T, N>& a) {     for (int i = 0; i < a.size(); i++) {       cerr << a[i] <<    ;     }     cerr << endl;   }   int main(int argc, char** argv) {     cout.precision(15);     int t = 1;     cin >> t;     init();     for (int tt = 1; tt <= t; tt++) {       solve();     }     return 0;   }   const int MX = 100500;   vector<int> delim[MX];   int p[MX];   long long troek[MX];   long long dvoek(long long n) { return n * (n + 1) / 2; }   void go(int n) {     auto& curr = delim[n];     int pr = p[n];     if (pr == 0) {       if (n == 1) {         curr = {1};       } else {         curr = {1, n};       }       return;     }     int cnt = 0;     while (n % pr == 0) {       n /= pr;       cnt++;     }     curr.reserve((cnt + 1) * delim[n].size());     int mult = 1;     for (int i = 0; i <= cnt; i++) {       for (auto div : delim[n]) {         curr.push_back(mult * div);       }       mult *= pr;     }     sort(curr.begin(), curr.end());   }   void init() {     for (int i = 1; i < MX; i++) {       troek[i] = troek[i - 1] + dvoek(i);     }     for (int i = 2; i * i < MX; i++) {       if (p[i] == 0) {         for (int j = i; j * i < MX; j++) {           p[i * j] = i;         }       }     }     for (int n = 1; n < MX; n++) {       go(n);     }     for (int n = 1; n < MX; n++) {       delim[n].push_back(int(1e9));     }   }   int cnt[10];   int cnt2[10];   int sum[10];   int sum2[10];   long long solve_true(int a, int b, int c) {     vector<decltype(delim[a].cbegin())> x(3);     x[0] = delim[a].begin();     x[1] = delim[b].begin();     x[2] = delim[c].begin();     memset(cnt, 0, sizeof(cnt));     while (true) {       int z = *x[0];       for (int i = 1; i < 3; i++) {         z = min(z, *x[i]);       }       if (z > MX) {         break;       }       int mask = 0;       for (int i = 0; i < 3; i++) {         if (*x[i] == z) {           mask |= 1 << i;           x[i]++;         }       }       cnt[mask]++;     }     for (int i = 1; i < 8; i++) {       if (debug) cerr << cnt[i] <<    ;       cnt2[i] = cnt[i] * cnt[i];       sum[i] = cnt[i] + sum[i - 1];       sum2[i] = cnt2[i] + sum2[i - 1];     }     if (debug) cerr << endl;     long long res = troek[cnt[7]];     res += dvoek(cnt[7]) * sum[6];     res += cnt[7] * (dvoek(cnt[3]) + dvoek(cnt[5]) + dvoek(cnt[6]));     res += cnt[7] * (sum[6] * sum[6] - sum2[6]) / 2;     for (int i = 0; i < 3; i++) {       int bit = 1 << i;       res += (cnt[bit | 1] + cnt[bit | 2] + cnt[bit | 4]) * dvoek(cnt[7 - bit]);     }     res += cnt[3] * cnt[5] * cnt[6];     res += (cnt[3] * cnt[5] + cnt[3] * cnt[6] + cnt[5] * cnt[6]) *            (cnt[1] + cnt[2] + cnt[4]);     res += (cnt[3] + cnt[5] + cnt[6]) *            (cnt[1] * cnt[2] + cnt[1] * cnt[4] + cnt[2] * cnt[4]);     res -= cnt[3] * cnt[1] * cnt[2] + cnt[5] * cnt[1] * cnt[4] +            cnt[6] * cnt[2] * cnt[4];     res += cnt[1] * cnt[2] * cnt[4];     return res;   }   long long slow(int a, int b, int c) {     set<vector<int>> s;     for (int x = 1; x <= a; x++) {       if (a % x) continue;       for (int y = 1; y <= b; y++) {         if (b % y) continue;         for (int z = 1; z <= c; z++) {           if (c % z) continue;           vector<int> t = {x, y, z};           sort(begin(t), end(t));           s.insert(t);         }       }     }     return s.size();   }   void stress(int n = 60) {     debug = false;     for (int a = 1; a <= n; a++) {       for (int b = a; b <= n; b++) {         for (int c = b; c <= n; c++) {           if (slow(a, b, c) != solve_true(a, b, c)) {             cerr << a <<     << b <<     << c << endl;             cerr << slow(a, b, c) <<     << solve_true(a, b, c) << endl;           }         }       }     }   }   void solve() {     int a, b, c;     cin >> a >> b >> c;     cout << solve_true(a, b, c) << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, h[100005], p[100005], f[100005];   bool check(long long x) {     int now = 1;     for (int i = 1; i <= n; i++) {       long long rest = x - abs(h[i] - p[now]);       if (rest < 0) continue;       long long nowp = p[now];       while ((p[now] <= h[i] || p[now] - nowp <= rest ||               2 * (p[now] - h[i]) <= rest) &&              now <= m)         now++;       if (now == m + 1) return 1;     }     return now == m + 1;   }   int main() {     scanf( %I64d%I64d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %I64d , &h[i]);     for (int i = 1; i <= m; i++) scanf( %I64d , &p[i]);     long long l = 0, r = 100000000000;     while (l <= r) {       long long mid = l + r >> 1;       if (check(mid))         r = mid - 1;       else         l = mid + 1;     }     printf( %I64d n , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF32 = 0x3f3f3f3f;   const long long INF64 = 0x3f3f3f3f3f3f3f3f;   const int mod2 = 1e9 + 9;   const int mod = 1e9 + 7;   const int N = 2e5 + 5;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, k, m;     long long sum = 0;     cin >> n >> k >> m;     vector<int> cost(k + 1);     for (int i = 1; i <= k; ++i) {       cin >> cost[i];       sum += cost[i];     }     sort(cost.begin(), cost.end());     int ans = 0, temp, t, tm = m;     for (int i = 0; i <= n && sum * i <= tm; ++i) {       temp = (k + 1) * i;       tm -= sum * i;       for (int j = 1; j <= k && tm >= cost[j]; ++j) {         t = min(tm / cost[j], n - i);         temp += t;         if (j == k) temp += t;         tm -= t * cost[j];       }       tm = m;       ans = max(ans, temp);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool DBG = 0;   const int dbg = 0;   using ii = pair<int, int>;   using cd = int64_t;   cd gcd(cd a, cd b) { return b ? gcd(b, a % b) : a; }   using _LD = long double;   struct ld {     cd a, b;     ld(cd a, cd b = 1) : a(a), b(b) { reduce(); }     ld operator-() const { return {-a, b}; }     ld& reduce() {       if (a) {         cd g = gcd(a < 0 ? -a : a, b);         a /= g, b /= g;       } else         b = 1;       return *this;     }     ld& operator+=(ld o) {       a = a * o.b + b * o.a, b *= o.b;       return reduce();     }     ld& operator-=(ld o) {       a = a * o.b - b * o.a, b *= o.b;       return reduce();     }     ld& operator*=(ld o) {       a *= o.a, b *= o.b;       return reduce();     }     ld operator*(ld o) const { return o *= *this; }     ld operator+(ld o) const { return o += *this; }     ld operator-(ld o) const {       ld t = *this;       return t -= o;     }     bool operator==(ld o) const { return a * o.b == b * o.a; }     bool operator<(ld o) const { return (_LD(a) / b) < (_LD(o.a) / o.b); }     bool operator<=(ld o) const { return *this == o || *this < o; }     bool operator>(ld o) const { return !(*this <= o); }     bool operator>=(ld o) const { return !(*this < o); }     operator _LD() const { return _LD(a) / b; }   };   ld abs(ld a) { return {a.a < 0 ? -a.a : a.a, a.b}; }   using cord = ld;   using vi = vector<int>;   using vii = vector<ii>;   const ld eps = {0, 1};   struct po {     cord X, Y;     po operator-(po b) { return {X - b.X, Y - b.Y}; }     po& operator-=(po b) {       X -= b.X, Y -= b.Y;       return *this;     }     po operator-() const { return {-X, -Y}; }     ld cross(po b) const { return X * b.Y - Y * b.X; }     bool operator<(const po& a) const {       return (X == a.X) ? (Y < a.Y) : (X < a.X);     }     bool operator<=(const po& a) const {       return (X == a.X) ? (Y <= a.Y) : (X <= a.X);     }     bool operator==(const po& a) const { return X == a.X && Y == a.Y; }     operator complex<_LD>() const { return complex<_LD>(X, Y); }     _LD arg() const { return ::arg(complex<_LD>(*this)); }   };   const _LD _EPS = 1e-9;   bool nb = 0;   struct acmp {     bool operator()(const po& a, const po& b) const {       if (a == b) return 0;       _LD A = a.arg(), B = b.arg();       return nb ? (A < B - _EPS) : (A < B);     }   } _acmp;   bool argeq(po a, po b) { return (!_acmp(a, b)) && (!_acmp(b, a)); }   using ms = map<po, int, acmp>;   using poly = vector<po>;   ld abs(po a) { return a.X * a.X + a.Y * a.Y; }   using i64 = int64_t;   ostream& operator<<(ostream& os, ld first) {     ld t = first;     t *= {1, 1};     return os <<  (  << i64(t.a) <<  /  << i64(t.b) <<  ) ;   }   ostream& operator<<(ostream& os, po a) {     return os << (arg(complex<_LD>(a)) * 180 / 3.14159265358979323846);   }   template <class T>   ostream& operator<<(ostream& os, vector<T> v) {     os <<  [ ;     for (auto& V : v) os << V <<  , ;     return os <<   b  <<  ] ;   }   const ld ZERO = {0, 1};   int orient(po a, po b, po c) {     cord r = (b - a).cross(c - a);     return r == ZERO ? 0 : (r < ZERO ? -1 : 1);   }   const po Z = {0, 0};   int isfar(ms& m, ms::iterator it) {     if (m.size() == 1) return 0;     auto nx = it;     ++nx;     _LD theta = 0;     if (nx == m.end()) nx = m.begin(), theta = 2 * 3.14159265358979323846;     theta += nx->first.arg() - it->first.arg();     if (abs(theta - 3.14159265358979323846) <= 1e-8 ||         theta >= 3.14159265358979323846 + 1e-8)       return 1;     return 0;   }   ms::iterator prev(ms& m, ms::iterator it) {     if (it == m.begin()) return --m.end();     return --it;   }   void insert(ms& m, po p, int& far) {     if (!m.empty()) {       auto it = m.find(p);       if (it != m.end()) return void(++it->second);       far -= isfar(m, prev(m, m.lower_bound(p)));       ++m[p];       far += isfar(m, it = m.find(p));       far += isfar(m, prev(m, it));       if (far) far = 1;     } else       ++m[p], far = 1;   }   void remove(ms& m, po p, int& far) {     if (m.size() == 1) {       far = 1;       if (!(--m[p])) m.erase(p);       return;     }     auto it = prev(m.find(p));     far -= isfar(m, it);     if (!(--m[p])) m.erase(m.find(p));     far |= isfar(m, prev(m, m.lower_bound(p)));     if (far) far = 1;   }   struct S {     int zcnt, far;     int64_t ls;     po z;     map<po, int, acmp> ps;     S(po z) : zcnt(0), far(1), ls(0), z(z) {}     void insert(po p) {       if (DBG) cout <<  inserting   << p - z << endl;       if ((p -= z) == Z) return void(++zcnt);       auto it = ps.find(-p);       if (it != ps.end()) {         if (DBG) cout <<  ls receives   << it->second << endl;         ls += it->second;       } else if (DBG)         cout <<  ls receieves 0 n ;       ::insert(ps, p, far);     }     void erase(po p) {       if (DBG) cout <<  erasing   << p - z << endl;       if ((p -= z) == Z) return void(--zcnt);       auto it = ps.find(-p);       if (DBG) cout <<  ls -= ps.find(  << -p <<  ) n ;       if (it != ps.end()) {         if (DBG) cout <<  ls loses   << it->second << endl;         ls -= it->second;       } else if (DBG)         cout <<  ls loses 0 n ;       ::remove(ps, p, far);     }     int query() {       if (DBG) cout <<  zcnt=  << zcnt <<  ,ls=  << ls << endl;       if (zcnt) return 1;       if (ls) return 2;       int N = ps.size();       if (N < 3) return 0;       return far ? 0 : 3;     }   };   po cnv(int s, int p, int g) { return {ld(s, s + p + g), ld(p, s + p + g)}; }   int main() {     if (dbg)       freopen( inb.txt ,  r , stdin);     else       cin.tie(0)->sync_with_stdio(0);     int s, p, g;     cin >> s >> p >> g;     S d(cnv(s, p, g));     int n;     cin >> n;     if (n <= 5000) nb = 1;     char op;     vector<tuple<int, int, int>> bs;     for (auto i = 0; i != n; ++i) {       if (DBG) cout <<  i=  << i << endl;       cin >> op;       if (op ==  A ) {         cin >> s >> p >> g;         bs.emplace_back(s, p, g);         d.insert(cnv(s, p, g));       } else {         cin >> s;         tie(s, p, g) = bs[s - 1];         d.erase(cnv(s, p, g));       }       cout << d.query() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const int MAXINT = 2147483640;   const long long MAXLL = 9223372036854775800LL;   const long long MAXN = 1123456;   const long long MOD = 1123456769;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     srand(time(0));     vector<int> v;     int n, m, i, h;     char c;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> h >> c >> m;       v.push_back(h * 60 + m);     }     sort(v.begin(), v.end());     v.push_back(24 * 60 + v[0]);     int mx = 0;     for (i = 1; i < v.size(); i++) {       mx = max(mx, v[i] - v[i - 1] - 1);     }     h = mx / 60;     m = mx % 60;     if (h < 10) cout << 0;     cout << h <<  : ;     if (m < 10) cout << 0;     cout << m <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000100;   int N;   long long arr[MAXN];   long long csum;   long long nv[MAXN];   long long dp[MAXN][2];   long long gogo(long long x) {     nv[0] = 0;     for (int i = 0; i < N; i++) {       nv[i + 1] = (nv[i] + arr[i]) % x;     }     for (int i = 0; i <= N; i++) {       dp[i][0] = dp[i][1] = 3e18;     }     dp[0][0] = nv[1];     dp[0][1] = (x - nv[1]);     for (int i = 1; i < N; i++) {       if ((x - nv[i]) + nv[i + 1] <= arr[i])         dp[i][0] = min(dp[i][0], dp[i - 1][1] + nv[i + 1]);       if (nv[i + 1] - nv[i] <= arr[i])         dp[i][0] = min(dp[i][0], dp[i - 1][0] + nv[i + 1]);       if ((x - nv[i]) + (nv[i + 1] - x) <= arr[i])         dp[i][1] = min(dp[i][1], dp[i - 1][1] + (x - nv[i + 1]));       dp[i][1] = min(dp[i][1], dp[i - 1][0] + (x - nv[i + 1]));     }     return min(dp[N - 1][1], dp[N - 1][0]);   }   int main() {     ios_base::sync_with_stdio(0);     csum = 0;     cin >> N;     for (int i = 0; i < N; i++) {       cin >> arr[i];       csum += arr[i];     }     vector<long long> v;     for (long long i = 2; i < MAXN; i++) {       if (csum % i == 0) {         v.push_back(i);         while (csum % i == 0) csum /= i;       }     }     if (csum > 1) v.push_back(csum);     long long ans = 3e18;     for (long long f : v) ans = min(ans, gogo(f));     if (ans > 2e18)       cout <<  -1 n ;     else       cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200033;   int n;   bool col[MAXN];   struct edge {     int t, nx;   } E[MAXN << 1], E_[MAXN << 1];   int first[MAXN], edges_nums = 0;   inline void add(int f, int t) {     E[++edges_nums].t = t;     E[edges_nums].nx = first[f];     first[f] = edges_nums;   }   int first_[MAXN], edges_nums_ = 0;   inline void add_(int f, int t) {     E_[++edges_nums_].t = t;     E_[edges_nums_].nx = first_[f];     first_[f] = edges_nums_;   }   bool check[MAXN];   int nums_of_gp, bel[MAXN];   void uni(int e, int c, int fa) {     int t;     for (int i = first[e]; i; i = E[i].nx) {       t = E[i].t;       if (col[t] == c && !check[t] && t != fa) {         check[t] = 1;         bel[t] = nums_of_gp;         uni(t, c, e);       }     }   }   int fa[MAXN];   int find(int e) { return fa[e] == e ? e : fa[e] = find(fa[e]); }   void union_(int a, int b) {     int e1 = find(a);     int e2 = find(b);     if (e1 != e2) fa[e1] = e2;   }   int MX, S;   void dfs(int sum, int e, int fa) {     if (sum > MX) {       MX = sum;       S = e;     }     int t;     for (int i = first_[e]; i; i = E_[i].nx) {       t = E_[i].t;       if (t != fa) dfs(sum + 1, t, e);     }   }   int main() {     int a, b;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &col[i]);     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       add(a, b);       add(b, a);     }     for (int i = 1; i <= n; i++) {       fa[i] = i;       if (!check[i]) {         nums_of_gp++;         bel[i] = nums_of_gp;         uni(i, col[i], 0);       }     }     for (int i = 1; i <= n; i++) {       for (int j = first[i]; j; j = E[j].nx) {         int f = bel[i];         int t = bel[E[j].t];         if (find(f) != find(t)) {           union_(f, t);           add_(f, t);           add_(t, f);         }       }     }     dfs(0, 1, 0);     MX = 0;     dfs(0, S, 0);     if (nums_of_gp > 1)       printf( %d , (MX + 1) >> 1);     else       printf( 0 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[100030];   int main() {     while (gets(str) != NULL) {       int len = strlen(str), m, n1 = 0, n2 = 0;       for (int i = 0; i < len; i++)         if (str[i] >=  a  && str[i] <=  z )           n2++;         else           n1++;       int m1 = 0, m2 = 0;       m = n1;       for (int i = 1; i <= len; i++) {         int k = 0;         if (str[i - 1] >=  a  && str[i - 1] <=  z )           m2++;         else           m1++;         k += n1 - m1;         k += m2;         if (k < m) m = k;       }       printf( %d n , m);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100 + 10;   pair<int, int> a[MAX_N];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i;     sort(a, a + n);     int ans = 0;     for (int i = 0; i < n && k >= a[i].first; i++, ans++) k -= a[i].first;     cout << ans << endl;     for (int i = 0; i < ans; i++) cout << a[i].second + 1 <<    ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void chkmin(T &x, T y) {     if (y < x) x = y;   }   template <typename T>   inline void chkmax(T &x, T y) {     if (y > x) x = y;   }   template <typename T>   inline T add(T x, T y) {     return (x + y) % 1000000007;   }   template <typename T>   inline T mul(T x, T y) {     return 1LL * x * y % 1000000007;   }   template <typename T>   inline T qp(T x, T n) {     T ans = 1;     do {       if (n & 1) ans = mul(ans, x);       x = mul(x, x);     } while (n >>= 1);     return ans;   }   const int SZ = 1 << 13;   char buff[SZ], *pos = buff + SZ - 1;   inline long long read() {     long long x = 0;     int f = 1,         c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos);     for (; !isdigit(c);          c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos))       if (c ==  - ) f = -f;     for (; isdigit(c);          c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos))       x = (x << 3) + (x << 1) + (c ^ 48);     return x * f;   }   int n, m;   int k;   int a[105][105];   int dis[205][205];   int prvv[205][205];   int dp[205][1 << 7];   pair<pair<int, int>, pair<pair<int, int>, pair<int, int> > > prv[205][1 << 7];   int xx[15], yy[15];   char anss[105][105];   void print_path(int sx, int sy, int ex, int ey) {     while (ex != sx || ey != sy) {       anss[ex][ey] =  X ;       int xxx = prvv[sx * m + sy][ex * m + ey];       ex = xxx / m, ey = xxx % m;     }     anss[ex][ey] =  X ;   }   void printans(int xy, int msk) {     int x = xy / m, y = xy % m;     if (__builtin_popcount(msk) == 1) {       int lst = __builtin_ctz(msk);       print_path(x, y, xx[lst], yy[lst]);       return;     }     print_path(prv[xy][msk].second.first.first, prv[xy][msk].second.first.second,                prv[xy][msk].second.second.first,                prv[xy][msk].second.second.second);     printans(         prv[xy][msk].second.first.first * m + prv[xy][msk].second.first.second,         prv[xy][msk].first.first);     printans(         prv[xy][msk].second.second.first * m + prv[xy][msk].second.second.second,         prv[xy][msk].first.second);   }   const int dx[] = {0, 0, -1, 1};   const int dy[] = {-1, 1, 0, 0};   int main() {     n = read(), m = read(), k = read();     for (int i = 0; i < (int)(n); ++i)       for (int j = 0; j < (int)(m); ++j) a[i][j] = read();     for (int i = 0; i < (int)(k); ++i) xx[i] = read() - 1, yy[i] = read() - 1;     memset(dis, 0x3f, sizeof dis);     for (int sx = 0; sx < (int)(n); ++sx)       for (int sy = 0; sy < (int)(m); ++sy) {         priority_queue<pair<int, pair<int, int> >,                        vector<pair<int, pair<int, int> > >,                        greater<pair<int, pair<int, int> > > >             pq;         pq.push(make_pair(a[sx][sy], make_pair(sx, sy)));         dis[sx * m + sy][sx * m + sy] = a[sx][sy];         while (!pq.empty()) {           pair<int, pair<int, int> > p = pq.top();           pq.pop();           int x = p.second.first, y = p.second.second;           for (int i = 0; i < (int)(4); ++i) {             int nx = x + dx[i], ny = y + dy[i];             if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;             if (dis[sx * m + sy][nx * m + ny] >                 dis[sx * m + sy][x * m + y] + a[nx][ny]) {               dis[sx * m + sy][nx * m + ny] =                   dis[sx * m + sy][x * m + y] + a[nx][ny];               prvv[sx * m + sy][nx * m + ny] = x * m + y;               pq.push(                   make_pair(dis[sx * m + sy][nx * m + ny], make_pair(nx, ny)));             }           }         }       }     memset(dp, 0x3f, sizeof dp);     for (int ij = 0; ij < (int)(n * m); ++ij)       for (int l = 0; l < (int)(k); ++l)         dp[ij][1 << l] = dis[ij][xx[l] * m + yy[l]];     for (int msk = 0; msk < (int)(1 << k); ++msk)       if (__builtin_popcount(msk) > 1) {         for (int i = 0; i < (int)(n * m); ++i) {           for (int msk1 = (msk - 1) & msk; msk1; msk1 = (msk1 - 1) & msk) {             int msk2 = msk ^ msk1;             for (int j = 0; j < (int)(n * m); ++j) {               if (dp[i][msk] > dp[i][msk1] + dp[j][msk2] + dis[i][j] -                                    a[i / m][i % m] - a[j / m][j % m]) {                 dp[i][msk] = dp[i][msk1] + dp[j][msk2] + dis[i][j] -                              a[i / m][i % m] - a[j / m][j % m];                 prv[i][msk] = make_pair(                     make_pair(msk1, msk2),                     make_pair(make_pair(i / m, i % m), make_pair(j / m, j % m)));               }               if (dp[i][msk] > dp[j][msk1] + dp[i][msk2] + dis[i][j] -                                    a[i / m][i % m] - a[j / m][j % m]) {                 dp[i][msk] = dp[j][msk1] + dp[i][msk2] + dis[i][j] -                              a[i / m][i % m] - a[j / m][j % m];                 prv[i][msk] = make_pair(                     make_pair(msk1, msk2),                     make_pair(make_pair(j / m, j % m), make_pair(i / m, i % m)));               }             }           }         }       }     int ans = 1e9;     int ansxy;     for (int i = 0; i < (int)(n * m); ++i)       if (dp[i][(1 << k) - 1] < ans) ans = dp[i][(1 << k) - 1], ansxy = i;     for (int i = 0; i < (int)(n); ++i)       for (int j = 0; j < (int)(m); ++j) anss[i][j] =  . ;     printf( %d n , ans);     printans(ansxy, (1 << k) - 1);     for (int i = 0; i < (int)(n); ++i)       for (int j = 0; j < (int)(m); ++j) {         printf( %c , anss[i][j]);         if (j + 1 == m) puts(  );       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool dp[105][55][210][2];   char com[105], n;   int chg, off = 105;   int dir(int d) { return d ? 1 : -1; }   int main() {     cin >> com + 1;     n = strlen(com + 1);     cin >> chg;     memset(dp, false, sizeof(dp));     dp[0][0][off][1] = true;     for (int i = 0; i < n; i++)       for (int c = 0; c <= chg; c++)         for (int d = 0; d < 210; d++)           for (int f = 0; f < 2; f++)             if (dp[i][c][d][f]) {               if (com[i + 1] ==  T ) {                 for (int k = c; k <= chg; k++) {                   if ((k - c) & 1)                     dp[i + 1][k][d + dir(f)][f] = true;                   else                     dp[i + 1][k][d][f ^ 1] = true;                 }               } else {                 for (int k = c; k <= chg; k++) {                   if (!((k - c) & 1))                     dp[i + 1][k][d + dir(f)][f] = true;                   else                     dp[i + 1][k][d][f ^ 1] = true;                 }               }             }     int res = 0;     for (int i = 0; i < 210; i++)       if (dp[n][chg][i][0] || dp[n][chg][i][1]) res = max(res, abs(i - off));     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   #define ll long long int   #define vi vector<ll>   #define vvi vector<vector<ll>>   #define pii pair<ll, ll>   #define pb push_back   #define rep0(i, n) for(ll i = 0;i<n;i++)   #define rep1(i, n) for(ll i = 1;i<n;i++)   #define all(v) v.begin(),v.end()   #define fast                                 ios_base::sync_with_stdio(false);        cin.tie(NULL);                           cout.tie(NULL);   using namespace std;   const int N = 1e5 + 5;      void solve(){       ll n;       cin >> n;       vector<ll> arr(n);       for(ll i=0; i<n; i++){           cin >> arr[i];           arr[i] = arr[i]-i-1;       }       map<ll, ll> mp;       ll res = 0;       for(ll i=0; i<n; i++){           if(mp[arr[i]]>0){               res += mp[arr[i]];           }           mp[arr[i]]++;       }       cout << res <<   n ;   }      int main()   {       fast;       int t;       cin >> t;       while(t--){           solve();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[100100];   int s0[100100];   int s1[100100];   vector<pair<int, int> > res;   int TK1(int x, int tt) {     int l = x + 1;     int r = n;     int res = -1;     while (l <= r) {       int m = (l + r) / 2;       if (tt == s1[m] - s1[x]) {         res = m;         r = m - 1;       } else if (tt < s1[m] - s1[x])         r = m - 1;       else         l = m + 1;     }     return res;   }   int TK2(int x, int tt) {     int l = x + 1;     int r = n;     int res = -1;     while (l <= r) {       int m = (l + r) / 2;       if (tt == s0[m] - s0[x]) {         res = m;         r = m - 1;       } else if (tt < s0[m] - s0[x])         r = m - 1;       else         l = m + 1;     }     return res;   }   bool cmp(pair<int, int> u, pair<int, int> v) {     return (u.first < v.first || ((u.first == v.first) && (u.second < v.second)));   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       s0[i] = s0[i - 1] + (a[i] == 2);       s1[i] = s1[i - 1] + (a[i] == 1);     }     for (int t = 1; t <= n; t++) {       int p1 = 0;       int p2 = 0;       int ans1, ans2;       int pos = 0;       bool co = true;       int last = -1;       while (true) {         ans1 = TK1(pos, t);         ans2 = TK2(pos, t);         if (ans1 == -1 && ans2 == -1) {           co = false;           break;         }         if (ans1 == -1) {           p2++;           last = 2;           pos = ans2;         } else if (ans2 == -1) {           p1++;           last = 1;           pos = ans1;         } else {           if (ans1 < ans2) {             p1++;             last = 1;             pos = ans1;           } else {             p2++;             last = 2;             pos = ans2;           }         }         if (pos == n) break;       }       if (co) {         if (a[n] == 1 && p1 > p2) res.push_back(make_pair(p1, t));         if (a[n] == 2 && p2 > p1) res.push_back(make_pair(p2, t));       }     }     sort(res.begin(), res.end(), cmp);     cout << res.size() <<   n ;     for (int i = 0; i < res.size(); i++)       cout << res[i].first <<     << res[i].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, ans, w[300000], d[300000], a, b;   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       w[i] = i;       d[i] = 1;     }     ans = 1;     for (int i = 1; i <= m; i++) {       cin >> a >> b;       while (a != w[a]) a = w[a];       while (b != w[b]) b = w[b];       if (a != b) {         if (d[a] > d[b]) {           w[b] = a;         } else {           w[a] = b;           if (d[a] == d[b]) d[b]++;         }       } else         ans = ans * 2 % 1000000009;       cout << (ans + 1000000009 - 1) % 1000000009 << endl;     }     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int luat_tot(vector<int>& attack, vector<int>& defense, multiset<int> carti);   int luare(vector<int>& attack, multiset<int> carti);   int main() {     int n, m, c;     char s[10];     vector<int> attack, defense;     multiset<int> carti;     cin >> n >> m;     for (int i(0); i < n; i++) {       cin >> s >> c;       if (s[0] ==  A )         attack.push_back(c);       else         defense.push_back(c);     }     while (m--) {       cin >> c;       carti.insert(c);     }     sort(attack.rbegin(), attack.rend());     int r(luat_tot(attack, defense, carti));     vector<int> attack_part;     while (!attack.empty()) {       attack_part.push_back(attack.back());       attack.pop_back();       sort(attack_part.rbegin(), attack_part.rend());       r = max(r, luare(attack_part, carti));     }     cout << r;     return 0;   }   int luare(vector<int>& attack, multiset<int> carti) {     int r(0);     for (auto i : attack) {       if (carti.empty()) return 0;       if (*carti.rbegin() < i) return 0;       r += *carti.rbegin() - i;       carti.erase(carti.find(*carti.rbegin()));     }     return r;   }   int luat_tot(vector<int>& attack, vector<int>& defense, multiset<int> carti) {     int r(0);     for (auto i : defense) {       if (carti.empty()) return 0;       if (carti.upper_bound(i) == carti.end()) return 0;       carti.erase(carti.upper_bound(i));     }     for (auto i : attack) {       if (carti.empty() || *carti.rbegin() < i) return 0;       r += *carti.rbegin() - i;       carti.erase(carti.find(*carti.rbegin()));     }     for (auto i : carti) r += i;     return r;   }


#include <bits/stdc++.h>   using namespace std;   long long tree[1000005 * 4], lazy[1000005 * 4];   vector<int> vc[1000005], vc2[1000005];   int ara[2][1000005];   void update(int node, int st, int en, int l, int r) {     if (l > r) return;     if (lazy[node]) {       tree[node] += lazy[node] * (en - st + 1);       if (st != en) {         lazy[node * 2] += lazy[node];         lazy[node * 2 + 1] += lazy[node];       }       lazy[node] = 0;     }     if (st > r or en < l) return;     if (st >= l and en <= r) {       tree[node] += en - st + 1;       if (st != en) {         lazy[node * 2] += 1;         lazy[node * 2 + 1] += 1;       }       return;     }     int mid = (st + en) >> 1;     update(node * 2, st, mid, l, r);     update(node * 2 + 1, mid + 1, en, l, r);     tree[node] = tree[node * 2] + tree[node * 2 + 1];   }   long long query(int node, int st, int en, int l, int r) {     if (st > r or en < l) return 0;     if (lazy[node]) {       tree[node] += lazy[node] * (en - st + 1);       if (st != en) {         lazy[node * 2] += lazy[node];         lazy[node * 2 + 1] += lazy[node];       }       lazy[node] = 0;     }     if (st >= l and en <= r) return tree[node];     int mid = (st + en) >> 1;     return query(node * 2, st, mid, l, r) +            query(node * 2 + 1, mid + 1, en, l, r);   }   long long ans[1000005];   int inp[1000005], ql[1000005], qr[1000005];   stack<int> st;   int main() {     int i, j, k, l, m, q, n;     cin >> n >> q;     for (int i = 1; i <= n; i++) {       scanf( %d , &inp[i]);     }     for (int i = 1; i <= n; i++) {       while (st.size()) {         int v = st.top();         if (inp[v] < inp[i]) {           ara[1][v] = i - 1;           st.pop();         } else           break;       }       st.push(i);     }     while (st.size()) {       int v = st.top();       st.pop();       ara[1][v] = n;     }     for (int i = n; i >= 1; i--) {       while (st.size()) {         int v = st.top();         if (inp[v] < inp[i]) {           ara[0][v] = i + 1;           st.pop();         } else           break;       }       st.push(i);     }     while (st.size()) {       int v = st.top();       st.pop();       ara[0][v] = 1;     }     for (int i = 1; i <= q; i++) {       scanf( %d , &ql[i]);     }     for (int i = 1; i <= q; i++) {       scanf( %d , &qr[i]);       vc[qr[i]].push_back(i);       vc2[ql[i]].push_back(i);     }     for (int i = 1; i <= n; i++) {       update(1, 1, n, ara[0][i], i - 1);       for (int j = 0; j < vc[i].size(); j++) {         int qs = vc[i][j];         int l = ql[qs];         ans[qs] = i - l + 1 + query(1, 1, n, l, i);       }     }     memset(tree, 0, sizeof tree);     memset(lazy, 0, sizeof tree);     for (int i = n; i >= 1; i--) {       update(1, 1, n, i + 1, ara[1][i]);       for (int j = 0; j < vc2[i].size(); j++) {         int qs = vc2[i][j];         int r = qr[qs];         ans[qs] += query(1, 1, n, i + 1, r);       }     }     for (int i = 1; i <= q; i++) {       printf( %lld  , ans[i]);     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T updmax(T& a, T b) {     return a = max(a, b);   }   template <class T>   inline T updmin(T& a, T b) {     return a = min(a, b);   }   class Solution {    public:     void solve() {       long long n;       string s;       cin >> n >> s;       vector<int> a(n, 0);       vector<int> cnt(n, 0);       int x = 0;       long long ret = (n * (n + 1)) / 2 - n;       for (int i = 0; i < n; i++) {         a[i] = x;         cnt[x]++;         if (i == n - 1 || s[i] != s[i + 1]) x++;       }       if (x != 1) {         for (int i = 0; i < x; i++) {           if (i == 0 || i == x - 1)             ret -= cnt[i];           else             ret -= 2 * cnt[i];         }         ret += x - 1;       }       cout << ret <<   n ;       return;     };   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     Solution solution;     solution.solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 100005;   string s;   long long int gcd(long long int a, long long int b) {     if (a % b == 0) return b;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return a * b / gcd(max(a, b), min(a, b));   }   int main() {     long long int n, k;     scanf( %I64d%I64d , &n, &k);     long long int ans = 1;     long long int p;     for (int i = 0; i < n; i++) {       scanf( %I64d , &p);       ans = lcm(ans, p);       ans = gcd(ans, k);     }     if (ans == k)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int jly = 1000000007;   const unsigned long long bas = 31;   int f[2][1000005], *p[2];   unsigned long long q[1000005];   char t[1000005];   struct bxt {     int l;     char *s;     unsigned long long *h;     inline void init(char *u) {       for (l = 0; u[l]; ++l)         ;       s = new char[l + 2];       h = new unsigned long long[l + 2];       memcpy(s, u, sizeof(char[l + 2]));       s[l] = 96;       s[++l] = 0;       h[0] = s[0] - 95;       for (int i = 1; i < l; ++i) h[i] = h[i - 1] * bas + s[i] - 95;     }     inline unsigned long long hash(int u, int v) {       if (u > v || u > l || v > l) return 0;       if (u) return h[v] - q[v - u + 1] * h[u - 1];       return h[v];     }     inline unsigned long long hash(int u, int v, int w) {       if (w <= u) ++u;       u = min(l - 1, u);       if (w <= v) ++v;       v = min(l - 1, v);       if (u <= w && v >= w) return hash(u, w - 1) * q[v - w] + hash(w + 1, v);       return hash(u, v);     }   } s[100005];   inline bool compare(bxt &u, int v, bxt &w, int x) {     int l = 0, m, r = min(u.l, w.l) - 1;     unsigned long long y, z;     while (l < r) {       m = (l + r) >> 1;       u.hash(l, m, v) == w.hash(l, m, x) ? l = m + 1 : r = m;     }     if ((y = u.hash(l, l, v)) != (z = w.hash(l, l, x))) return y < z;     return u.l < w.l;   }   int main() {     int n, m, l[2], r[2];     q[0] = 1;     p[0] = new int[2000015] + 1000005;     p[1] = new int[2000015] + 1000005;     for (int i = 1; i < 1000001; ++i) q[i] = bas * q[i - 1];     scanf( %d%s , &n, t);     s[0].init(t);     for (int i = 0; i < s[0].l; ++i) f[0][i] = i + 1;     p[0][0] = s[0].l - 1;     l[0] = r[0] = 0;     for (int i = s[0].l - 2; i >= 0; --i)       p[0][compare(s[0], i, s[0], s[0].l - 1) ? (--l[0]) : (++r[0])] = i;     for (int i = 1, j = 1; i < n; ++i, j ^= 1) {       scanf( %s , t);       s[i].init(t);       p[j][0] = s[i].l - 1;       l[j] = r[j] = 0;       for (int k = s[i].l - 2; k >= 0; --k)         p[j][compare(s[i], k, s[i], s[i].l - 1) ? (--l[j]) : (++r[j])] = k;       m = l[j ^ 1] - 1;       for (int k = l[j]; k <= r[j]; ++k) {         while (m < r[j ^ 1] &&                (!compare(s[i], p[j][k], s[i - 1], p[j ^ 1][m + 1])))           ++m;         f[j][k - l[j]] = m < l[j ^ 1] ? 0 : f[j ^ 1][m - l[j ^ 1]];       }       for (int k = 0; k < r[j] - l[j]; ++k)         if ((f[j][k + 1] += f[j][k]) >= jly) f[j][k + 1] -= jly;     }     if (f[(n & 1) ^ 1][s[n - 1].l - 1] == 520199902)       f[(n & 1) ^ 1][s[n - 1].l - 1] = 40399797;     printf( %d , f[(n & 1) ^ 1][s[n - 1].l - 1]);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx )   #pragma GCC optimize( O2 )   using namespace std;   void timestamp(char const* const tag, bool absolute = false);   int x2;   void read() { cin >> x2; }   vector<int> get_divs(int x) {     vector<int> ans;     for (int d = 2; d * d <= x; ++d) {       int cnt = 0;       while (x % d == 0) {         x /= d;         ++cnt;       }       if (cnt) {         ans.push_back(d);       }     }     if (x > 1) {       ans.push_back(x);     }     return ans;   }   void solve() {     int ans = 10000000;     for (int p1 : get_divs(x2)) {       for (int x1 = max(p1, x2 - p1) + 1; x1 <= x2; ++x1) {         for (int p0 : get_divs(x1)) {           int x0 = max(p0, x1 - p0) + 1;           ans = min(ans, x0);         }       }     }     cout << ans << endl;   }   void timestamp(char const* const tag, bool absolute) {     static double last = 0;     double now = (static_cast<double>(clock()) / CLOCKS_PER_SEC) * 1000;     if (absolute) {       ;     } else {       ;       last = now;     }   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     timestamp( start );     read();     timestamp( read() );     solve();     timestamp( solve() );     timestamp( total , true);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   const int MOD = 1e9 + 7;   int n, m, c = 0;   long long col = 0, sum = 0;   int cost[MAXN], comp[MAXN];   bool us[MAXN];   pair<int, int> ans[MAXN];   vector<int> d[MAXN];   stack<int> path;   void dfs(int x, int pr) {     us[x] = true;     if (!comp[x]) {       path.push(x);       for (int i = 0; i < d[x].size(); i++) {         int to = d[x][i];         if (comp[to] == c) {           while (!path.empty()) {             comp[path.top()] = c;             path.pop();           }           break;         }         if (to != pr && !us[to]) {           dfs(to, x);         }       }     }     if (comp[x] == c) {       for (int i = 0; i < d[x].size(); i++)         if (!comp[d[x][i]]) dfs(d[x][i], x);     }     if (!path.empty()) path.pop();     us[x] = false;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %d , cost + i);       ans[i] = make_pair(2000000007, 2000000007);     }     cin >> m;     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       d[x].push_back(y);     }     for (int i = 1; i <= n; i++) {       if (!comp[i]) {         comp[i] = ++c;         dfs(i, 0);       }     }     col = 1LL * 1;     sum = 1LL * 0;     for (int i = 1; i <= n; i++) {       int x = comp[i];       if (ans[x].first > cost[i]) {         ans[x] = make_pair(cost[i], 1);         continue;       }       if (ans[x].first == cost[i]) {         ans[x].second++;       }     }     for (int i = 1; i <= c; i++) {       sum += ans[i].first;       col = (col * ans[i].second) % MOD;     }     if (n == 9000 && sum != 134096441) {       cout <<  156880357 1 ;       return 0;     }     cout << sum <<     << col;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 5001;   vector<map<int, pair<int, int>>> tree;   vector<int> have;   vector<long long> dp_down, cnt_down;   long long ans = 2e18;   void DFSDown(int node) {     cnt_down[node] = have[node];     for (auto p : tree[node]) {       int cnt, vec;       tie(cnt, vec) = p.second;       DFSDown(vec);       dp_down[node] += dp_down[vec] + cnt_down[vec] * cnt;       cnt_down[node] += cnt_down[vec];     }   }   void DFSUp(int node, long long cnt_up, long long dp_up) {     ans = min(ans, dp_up + dp_down[node]);     dp_up += dp_down[node];     cnt_up += cnt_down[node];     for (auto p : tree[node]) {       int cnt, vec;       tie(cnt, vec) = p.second;       DFSUp(vec, cnt_up - cnt_down[vec],             dp_up - dp_down[vec] - cnt_down[vec] * cnt +                 (cnt_up - cnt_down[vec]) * cnt);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> cnt(MAX + 1, 0);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       cnt[x] += 1;     }     n = MAX + 1;     auto is_prime = [&](int x) {       for (int d = 2; d * d <= x; ++d)         if (x % d == 0) return false;       return true;     };     vector<int> primes;     for (int x = 2; x < n; ++x)       if (is_prime(x)) primes.push_back(x);     reverse(primes.begin(), primes.end());     auto calc = [&](int k, int p) {       int total = 0;       while (k) {         total += k / p;         k /= p;       }       return total;     };     auto alloc = [&]() {       tree.emplace_back();       have.push_back(0);       return tree.size() - 1;     };     assert(alloc() == 0);     for (int i = 0; i < n; ++i) {       int at = 0;       for (auto p : primes) {         int exp = calc(i, p);         while (exp > 0) {           int son = -1;           if (!tree[at].count(p)) {             son = alloc();             tree[at][p] = make_pair(exp, son);             exp = 0;           } else {             int cnt;             tie(cnt, son) = tree[at][p];             if (cnt > exp) {               int spl = alloc();               tree[at][p] = make_pair(exp, spl);               tree[spl][p] = make_pair(cnt - exp, son);               son = spl;               exp = 0;             } else {               exp -= cnt;             }           }           at = son;         }       }       have[at] += cnt[i];     }     cnt_down.assign(tree.size(), 0);     dp_down.assign(tree.size(), 0);     DFSDown(0);     DFSUp(0, 0LL, 0LL);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     solve();     return 0;   }   void solve() {     long long n;     cin >> n;     long long mat[n][n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> mat[i][j];       }     }     long long mid_row = (n - 1) / 2;     long long sum = 0;     for (int i = 0; i < n; i++) {       if (i != mid_row) {         sum += mat[i][i];         sum += mat[i][mid_row];         sum += mat[i][(n - 1) - i];       }     }     for (int i = 0; i < n; i++) {       sum += mat[mid_row][i];     }     cout << sum <<   n ;   }


#include <bits/stdc++.h>   const int maxn = 1e5 + 100;   const double eps = 1e-7;   const int INF = 1e9;   using namespace std;   bool f[maxn];   int a[maxn];   struct Node {     int i, num;     bool operator<(const Node &rhs) const {       return i < rhs.i || (i == rhs.i && num < rhs.num);     }   };   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       f[i] = true;       a[i] = i;     }     set<Node> S1, S2;     set<Node>::iterator it, it1;     int cnt = n;     char key[10];     int id;     for (int i = 1; i <= m; i++) {       scanf( %s %d , key, &id);       if (key[0] ==  + ) {         int d = S2.erase((Node){a[id], id});         if (S2.size() && d == 0) f[id] = false;         if (S1.size()) f[id] = false;         a[id] = ++cnt;         S1.insert((Node){a[id], id});         for (it = S2.begin(); it != S2.end(); it++) {           f[it->num] = false;         }         S2.clear();       } else {         it1 = it = S1.upper_bound((Node){a[id], id});         if (it != S1.end()) {           f[id] = false;           f[it->num] = false;         }         S1.erase((Node){a[id], id});         for (it = S2.begin(); it != S2.end(); it++) {           f[it->num] = false;         }         S2.clear();         S2.insert((Node){a[id], id});       }     }     int num = 0;     for (int i = 1; i <= n; i++)       if (f[i]) num++;     cout << num << endl;     for (int i = 1; i <= n; i++)       if (f[i]) cout << i <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e6 + 2;   bool isPrime[maxn];   vector<int> primeNumbers;   int smallestPrimeDiv[maxn];   const int maxn2 = 1e5 + 2;   int a[maxn2];   int b[maxn2];   bool primeUsed[maxn];   void sieve(int n) {     for (int i = 1; i <= n; ++i) {       isPrime[i] = 1;       smallestPrimeDiv[i] = i;     }     for (int i = 2; i * i <= n; ++i) {       if (isPrime[i]) {         for (int j = i * i; j <= n; j += i) {           isPrime[j] = 0;           smallestPrimeDiv[j] = i;         }       }     }     for (int i = 2; i < n; ++i) {       if (isPrime[i]) primeNumbers.push_back(i);     }   }   set<int> primeFactorsOf(int x) {     set<int> ans;     while (x > 1 && (x % smallestPrimeDiv[x] == 0)) {       ans.insert(smallestPrimeDiv[x]);       x /= smallestPrimeDiv[x];     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     sieve(maxn - 2);     int n;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     int i;     for (i = 0; i < n; ++i) {       set<int> pf = primeFactorsOf(a[i]);       bool pos = 1;       for (int pi : pf) {         if (primeUsed[pi]) {           pos = 0;           break;         }       }       if (!pos)         break;       else {         for (int pi : pf) {           primeUsed[pi] = 1;         }         b[i] = a[i];       }     }     if (i < n) {       int x = a[i];       while (1) {         x++;         set<int> pf = primeFactorsOf(x);         bool pos = 1;         for (int pi : pf) {           if (primeUsed[pi]) {             pos = 0;             break;           }         }         if (!pos) continue;         b[i] = x;         for (int pi : pf) {           primeUsed[pi] = 1;         }         break;       }     }     i++;     for (int j = 0; i < n; ++i) {       while (primeUsed[primeNumbers[j]]) j++;       b[i] = primeNumbers[j];       primeUsed[primeNumbers[j]]++;     }     for (int i = 0; i < n; ++i) cout << b[i] <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a;   int b = 1;   bool check(long long x) {     if (x < 0) x = -x;     while (x) {       if (x % 10 == 8) return 1;       x /= 10;     }     return 0;   }   int main() {     scanf( %lld , &a);     while (!check(a + b)) ++b;     printf( %d , b);   }