#include <bits/stdc++.h> n using namespace std; n int main(void) { n   stack<int> tmpRes; n   string S; n   cin >> S; n   for (int i = 0; i < S.length(); i++) { n     if (S[i] ==  r ) n       printf( %d n , i + 1); n     else n       tmpRes.emplace(i + 1); n   } n   while (!tmpRes.empty()) { n     printf( %d n , tmpRes.top()); n     tmpRes.pop(); n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int hh, mm, i; n   while (~scanf( %d:%d , &hh, &mm)) { n     mm = mm - 1; n     for (i = 0;; i++) { n       mm++; n       if (mm >= 60) { n         mm = mm - 60; n         hh++; n         if (hh >= 24) hh = 0; n       } n       if (hh / 10 == mm % 10 && hh % 10 == mm / 10) { n         cout << i << endl; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n } n int const MAX = 1e6 + 7; n int cnt[MAX]; n void solve() { n   int n, k; n   cin >> n >> k; n   int val[n]; n   for (int i = 0; i < n; i++) cin >> val[i]; n   int x, y; n   int ans = 0; n   int l = 0; n   int diff = 0; n   for (int r = 0; r < n; r++) { n     if (++cnt[val[r]] == 1) { n       diff++; n     } n     while (diff > k) { n       if (--cnt[val[l]] == 0) diff--; n       l++; n     } n     if (r - l + 1 > ans) { n       ans = r - l + 1; n       x = l; n       y = r; n     } n   } n   cout << x + 1 <<     << y + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1003, INF = 0x3f3f3f3f; n long long qpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a; n     a = a * a; n     b >>= 1; n   } n   return ans; n } n int n, m, k; n int a[N][N]; n int ans = 0; n void dfs(int first, int second, int xx, int yy) { n   if (first == n + 1) { n     ans = yy; n     return; n   } n   if (a[first][second] == 1) { n     a[first][second] = 2; n     dfs(first, second + 1, first, second); n   } else if (a[first][second] == 2) { n     dfs(first + 1, second, first, second); n   } else { n     a[first][second] = 2; n     dfs(first, second - 1, first, second); n   } n } n int main() { n   ios::sync_with_stdio; n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 1; i <= k; i++) { n     int first; n     cin >> first; n     ans = 0; n     dfs(1, first, 0, 0); n     cout << ans <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n bool exmax(T &a, T b) { n   return (b > a) ? (a = b) || 1 : 0; n } n template <typename T> n bool exmin(T &a, T b) { n   return (b < a) ? (a = b) || 1 : 0; n } n using namespace std; n int a[510], b[510], l; n void print() { n   for (int i = l - 1; i >= 0; i--) cout << a[i]; n   cout << endl; n } n int getsum(int s, int t) { n   int ret = 0; n   for (int i = s; i < t; i++) ret += b[i]; n   return ret; n } n int main() { n   memset(a, 0x00, sizeof(a)); n   int n; n   cin >> n; n   n--; n   int m; n   cin >> m; n   l = 0; n   int t = m; n   while (m) { n     if (m > 9) n       a[l++] = 9, m -= 9; n     else n       a[l++] = m, m = 0; n   } n   print(); n   while (n--) { n     cin >> m; n     if (t < m) { n       for (int i = 0, j = m - t; j; i++) { n         if (j > 9 - a[i]) n           j -= 9 - a[i], a[i] = 9; n         else n           a[i] += j, j = 0; n         exmax(l, i + 1); n       } n       t = m; n     } else { n       t = m; n       for (int i = 0; true; i++) { n         memcpy(b, a, sizeof(a)); n         b[i]++; n         while (b[i] > 9) b[i] = 0, b[++i] += 1; n         for (int j = 0; j < i; j++) b[j] = 0; n         exmax(l, i + 1); n         int s = getsum(i, l); n         if (s > m) continue; n         if (i * 9 < m - s) continue; n         int k = 0; n         m -= s; n         while (m) { n           if (m > 9) n             b[k++] = 9, m -= 9; n           else n             b[k++] = m, m = 0; n         } n         break; n       } n       memcpy(a, b, sizeof(a)); n     } n     print(); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename Tp> n void in(Tp &x) { n   char ch = getchar(), f = 1; n   x = 0; n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) f = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n template <typename Tp> n bool chkmax(Tp &x, Tp y) { n   return x >= y ? 0 : (x = y, 1); n } n template <typename Tp> n bool chkmin(Tp &x, Tp y) { n   return x <= y ? 0 : (x = y, 1); n } n template <typename Tp> n Tp Max(const Tp &x, const Tp &y) { n   return x > y ? x : y; n } n template <typename Tp> n Tp Min(const Tp &x, const Tp &y) { n   return x < y ? x : y; n } n const int MAXN = 400010; n int n; n int s[MAXN], g[MAXN]; n int head[MAXN], nxt[MAXN << 1], data[MAXN << 1], flow[MAXN << 1], cnt; n void add(int x, int y, int z) { n   nxt[cnt] = head[x]; n   data[cnt] = y; n   flow[cnt] = z; n   head[x] = cnt++; n } n using std::queue; n queue<int> q; n bool instack[MAXN]; n long long dis[MAXN]; n void spfa() { n   memset(dis, 0x3f, sizeof dis); n   dis[0] = 0; n   instack[0] = true; n   q.push(0); n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     instack[now] = false; n     for (int i = head[now]; i != -1; i = nxt[i]) { n       if (dis[data[i]] > dis[now] + flow[i]) { n         dis[data[i]] = dis[now] + flow[i]; n         if (!instack[data[i]]) { n           q.push(data[i]); n           instack[data[i]] = true; n         } n       } n     } n   } n   long long ans1 = 0; n   for (int i = (1), i_END_ = (n); i <= i_END_; ++i) { n     ans1 += dis[i]; n     if (dis[i] < 0) { n       puts( -1 ); n       return; n     } n   } n   printf( %I64d n , ans1); n   for (int i = (1), i_END_ = (n); i <= i_END_; ++i) n     printf( %I64d  , dis[i] + s[i]); n   putchar(10); n } n int main() { n   in(n); n   for (int i = (1), i_END_ = (n); i <= i_END_; ++i) in(s[i]), in(g[i]); n   memset(head, -1, sizeof head); n   for (int i = (1), i_END_ = (n); i <= i_END_; ++i) add(0, i, g[i]); n   for (int i = (1), i_END_ = (n - 1); i <= i_END_; ++i) { n     int t = s[i] - s[i + 1]; n     add(i, i + 1, 1 + t); n     add(i + 1, i, 1 - t); n   } n   spfa(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, j, c; n   int a[1000][1000]; n   int b[10000]; n   c = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) scanf( %d , &a[i][j]); n   for (i = 1; i <= n; i++) { n     int flag = 0; n     for (j = 1; j <= n; j++) { n       if (a[i][j] == 3 || a[i][j] == 1) flag = 1; n     } n     if (flag == 0) { n       b[c] = i; n       c++; n     } n   } n   printf( %d n , c); n   if (c != 0) { n     for (i = 0; i < c; i++) printf( %d  , b[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t = 0; n   scanf( %d , &t); n   for (int qwe = 0; qwe < t; qwe++) { n     long long r = 0, l = 0, y = 0; n     scanf( %lld%lld , &l, &r); n     printf( %lld %lld n , l, l * 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long i, j, k; n   int n; n   cin >> n; n   vector<long long> arr(n); n   long long sum = 0; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     sum += arr[i]; n   } n   if (sum % 3 != 0) { n     cout << 0 << endl; n     return 0; n   } n   if (n < 3) { n     cout << 0 << endl; n     return 0; n   } n   long long s = sum / 3; n   vector<int> ans(n, 0); n   long long pref = 0; n   for (i = n - 1; i >= 0; i--) { n     pref += arr[i]; n     if (pref == s) { n       ans[i] = 1; n     } n   } n   for (i = n - 2; i >= 0; i--) { n     ans[i] += ans[i + 1]; n   } n   pref = 0; n   long long soln = 0; n   for (i = 0; i < n - 2; i++) { n     pref += arr[i]; n     if (pref == s) { n       soln += ans[i + 2]; n     } n   } n   cout << soln << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > numbersWithGrundy(1); n int a[200001], grundyValues[200001]; n bool isPrime[200001], isMoveValid[200001]; n vector<string> players{ Alice ,  Bob }; n void PreProcess(int n, int m) { n   vector<int> primes; n   for (int i = 2; i <= n; ++i) { n     isPrime[i] = 1; n   } n   for (int i = 2; i <= n; ++i) { n     if (!isPrime[i]) { n       continue; n     } n     for (int j = i + i; j <= n; j += i) { n       isPrime[j] = 0; n     } n     primes.push_back(i); n     for (int prime : primes) { n       if (i * prime > n) { n         break; n       } n       isMoveValid[i * prime] = 1; n     } n     isMoveValid[i] = 1; n   } n   isMoveValid[m] = 0; n   numbersWithGrundy[0].push_back(0); n   for (int i = 1; i <= n; ++i) { n     int &k = grundyValues[i]; n     for (; k < numbersWithGrundy.size(); ++k) { n       bool valid = 1; n       for (int l = 0, r = numbersWithGrundy[k].size() - 1; l <= r && valid; n            ++l, --r) { n         valid = !(isMoveValid[i - numbersWithGrundy[k][l]] || n                   isMoveValid[i - numbersWithGrundy[k][r]]); n       } n       if (valid) { n         break; n       } n     } n     if (k >= numbersWithGrundy.size()) { n       numbersWithGrundy.resize(k + 1); n     } n     numbersWithGrundy[k].push_back(i); n   } n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1, j = 0; i <= n; ++i) { n     int b, w, r; n     scanf( %d%d%d , &b, &w, &r); n     a[++j] = w - b - 1; n     a[++j] = r - w - 1; n   } n   n = 2 * n; n   int m = 0; n   for (int i = 1; i <= n; ++i) { n     m = max(m, a[i]); n   } n   PreProcess(m, k); n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans ^= grundyValues[a[i]]; n   } n   ans = (ans == 0) ? 1 : 0; n   for (int i = 0; i < players.size(); ++i) { n     cout << players[i ^ ans] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n using namespace std; n int a[3030], b[3030]; n int dp[3030]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     memset(dp, 0, sizeof(dp)); n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       b[i] = a[i]; n     } n     sort(b + 1, b + 1 + n); n     for (int i = 1; i <= n; i++) { n       a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; n     } n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       dp[a[i]] = dp[a[i] - 1] + 1; n       ans = max(ans, dp[a[i]]); n     } n     cout << n - ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct SNM { n   vector<SNM*> trgs; n   vector<SNM*> rv_trgs; n   int stype; n   bool asgn; n   bool used; n   void asgnN() { n     if (asgn) return; n     asgn = true; n     for (vector<SNM*>::iterator i = trgs.begin(); i != trgs.end(); i++) { n       (*i)->asgnN(); n     } n   } n   void usedN() { n     if (used) return; n     used = true; n     if (stype == 1) return; n     for (vector<SNM*>::iterator i = rv_trgs.begin(); i != rv_trgs.end(); i++) { n       (*i)->usedN(); n     } n   } n   SNM() { n     used = false; n     asgn = false; n   } n }; n SNM nodes[100000]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &nodes[i].stype); n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     nodes[a].trgs.push_back(&nodes[b]); n     nodes[b].rv_trgs.push_back(&nodes[a]); n   } n   for (int i = 0; i < n; i++) { n     if (nodes[i].stype == 1) { n       nodes[i].asgnN(); n     } n   } n   for (int i = 0; i < n; i++) { n     if (nodes[i].stype == 2) { n       nodes[i].usedN(); n     } n   } n   for (int i = 0; i < n; i++) { n     if (nodes[i].asgn && nodes[i].used) { n       printf( 1 n ); n     } else { n       printf( 0 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   long long n, q, x, y; n   cin >> n >> q; n   for (long long i = 1; i <= q; i++) { n     cin >> x >> y; n     if ((x + y) % 2 == 0) { n       if (n % 2 == 0) { n         long long linie = n / 2; n         long long first = linie * (x - 1); n         long long kth = (y + 1) / 2; n         cout << first + kth <<   n ; n       } else { n         long long linie = n / 2; n         long long lines = linie * (x - 1) + (x - 1) / 2 + (x - 1) % 2; n         long long kth = (y + 1) / 2; n         cout << lines + kth <<   n ; n       } n     } else { n       long long depl = (n * n) / 2 + (n * n) % 2; n       if (n % 2 == 0) { n         long long linie = n / 2; n         long long first = linie * (x - 1); n         long long kth = (y + 1) / 2; n         cout << depl + first + kth <<   n ; n       } else { n         long long linie = n / 2; n         long long lines = linie * (x - 1) + (x - 1) / 2; n         long long kth = (y + 1) / 2; n         cout << depl + lines + kth <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int num2 = {}, num3 = {}; n     while (n % 2 == 0) { n       n /= 2; n       num2++; n     } n     while (n % 3 == 0) { n       n /= 3; n       num3++; n     } n     if (n != 1 || num2 > num3) n       cout <<  -1 n ; n     else n       cout << 2 * num3 - num2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool valid(char str[]) { n   int now = 0; n   for (int i = 0; str[i]; i++) { n     if (str[i] > now +  a ) return false; n     if (str[i] == now +  a ) now++; n   } n   return true; n } n int main() { n   char in[550]; n   scanf( %s , in); n   int mx = 0; n   for (int i = 0; in[i]; i++) mx = max(mx, in[i] -  a ); n   int cnt = 0; n   if (valid(in)) n     printf( YES ); n   else n     printf( NO ); n } n


#include <bits/stdc++.h> n int main() { n   int n, m, k, p = 1; n   scanf( %d , &n); n   m = n; n   while (m != 0) { n     if (m % 10 == 1) { n       m = m / 10; n       continue; n     } n     if (m % 100 == 14) { n       m = m / 100; n       continue; n     } n     if (m % 1000 == 144) { n       m = m / 1000; n       continue; n     } n     p = 0; n     break; n   } n   if (p == 1) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105, A = 26; n bool win[N][N][A], known[N][N][A]; n char g[N][N]; n int n; n bool follow(int u, int v, char c) { n   if (known[u][v][c -  a ]) return win[u][v][c -  a ]; n   for (int i = 1; i <= n; ++i) n     if (g[u][i] >= c) win[u][v][c -  a ] |= !follow(v, i, g[u][i]); n   known[u][v][c -  a ] = 1; n   return win[u][v][c -  a ]; n } n int main() { n   int m; n   scanf( %d%d , &n, &m); n   while (m--) { n     int a, b; n     char c; n     scanf( %d%d %c , &a, &b, &c); n     g[a][b] = c; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       printf( %c , follow(i, j,  a ) ?  A  :  B ); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n vector<long long> g[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     long long n; n     cin >> n; n     string s; n     cin >> s; n     long long i, j; n     long long ans = 1e16; n     for (int i = 0; i < n - 3; i++) { n       long long sm = 0, c = 0; n       for (int j = i; j < i + 4; j++) { n         if (c % 4 == 0) { n           sm += min(abs(s[j] -  A ), abs(s[j] -  Z ) + 1); n         } n         if (c % 4 == 1) { n           sm += min(abs(s[j] -  C ), abs(s[j] -  Z ) + 3); n         } n         if (c % 4 == 2) { n           sm += min(abs(s[j] -  T ), abs(s[j] -  A ) + 7); n         } n         if (c % 4 == 3) { n           sm += min(abs(s[j] -  G ), abs(s[j] -  Z ) + 7); n         } n         c++; n       } n       ans = min(ans, sm); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


/* n     Author - Param Kothari n     Ah shit, here we go again n */ n  n #include <bits/stdc++.h> n   n using namespace std; n  n #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define pb push_back n #define mk make_pair n  n typedef long long ll; n typedef long double ld; n  n void solve(){ n     int n; n     cin >> n; n     vector<int> a(n); n     for(int i = 0; i < n; i++){ n         cin >> a[i]; n     } n     vector<ll> c(n + 1); n     for(int i = 0; i < n; i++){ n         c[a[i]]++; n     } n     vector<ll> pref(n + 2); n     for(int i = 1; i <= n; i++){ n         pref[i] = pref[i - 1] + c[i]; n     } n     ll res = 0; n     auto nC2 = [&](ll x){ n         return (x * (x - 1)) / 2; n     }; n     auto nC3 = [&](ll x){ n         return (x * (x - 1) * (x - 2)) / 6; n     }; n     for(int i = 1; i <= n - 2; i++){ n         ll l = i, r = i + 2; n         ll cnt = pref[r - 1] - pref[l]; n         res += 1LL * c[l] * c[r] * cnt; n         res += 1LL * nC2(c[l]) * c[r]; n         res += 1LL * c[l] * nC2(c[r]); n     } n     for(int i = 1; i <= n - 1; i++){ n         ll l = i, r = i + 1; n         res += 1LL * nC2(c[l]) * c[r]; n         res += 1LL * c[l] * nC2(c[r]); n     } n     for(int i = 1; i <= n; i++){ n         res += 1LL * nC3(c[i]); n     } n     cout << res <<   n ; n     return; n } n  n int main(){ n     IOS; n     int t = 1; n     cin >> t; n     for(int i = 1; i <= t; i++){ n         solve(); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   scanf( %d , &n); n   s = n / 2; n   if (n % 2 == 0) { n     s--; n   } n   printf( %d n , s); n } n


#include <bits/stdc++.h> n using namespace std; n long double dp[9000001] = {}; n bool vis[9000001] = {}; n long double solve(int inv) { n   if (inv == 0) return 0; n   if (inv == 1) return 1; n   if (vis[inv]) return dp[inv]; n   vis[inv] = 1; n   return dp[inv] = 4 + solve(inv - 2); n } n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   std::vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   int inv = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) n       if (v[j] < v[i]) inv++; n   } n   cout << fixed << setprecision(9) << solve(inv); n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const double EPS = 1e-9; n int ar[] = {1, -1, 0, 0, 1, 1, -1, -1}; n int ac[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int p[200005]; n int q[200005]; n void solve1(int n) { n   int k = 1; n   if (n & 1) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   while (k <= n) k *= 2; n   k /= 2; n   int last = n + 1; n   while (k != 0) { n     for (int i = k; i < last; i++) { n       p[i] = k - (i - k + 1); n       p[k - (i - k + 1)] = i; n     } n     last = min(last, k - (last - k)); n     k /= 2; n   } n   for (int i = 1; i <= n; i++) cout << p[i] <<    ; n   cout <<   n ; n } n void solve2(int n) { n   if (n < 6) { n     cout <<  NO n ; n     return; n   } n   int k = 1; n   while (2 * k <= n) k *= 2; n   if (n == k) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   if (n == 6) { n     cout <<  3 6 2 5 1 4 n ; n     return; n   } n   for (int i = k; i < n; i++) q[i] = i + 1; n   q[n] = k; n   while (k != 4) { n     for (int i = k / 2; i < k - 1; i++) { n       q[i] = i + 1; n     } n     q[k - 1] = k / 2; n     k /= 2; n   } n   q[1] = 5; n   q[2] = 3; n   q[3] = 2; n   q[4] = 6; n   q[5] = 7; n   q[6] = 4; n   q[7] = 1; n   for (int i = 1; i <= n; i++) cout << q[i] <<    ; n   cout <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   solve1(n); n   solve2(n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int MAX = 200002; n int arr[MAX]; n int cnt[MAX]; n int main() { n   int n, wrong = 0; n   cin >> n; n   for (int i = (1); i <= (n); i++) { n     cin >> arr[i]; n   } n   string s; n   cin >> s; n   cnt[1] = s[0] -  0 ; n   for (int i = (2); i <= (n); i++) { n     cnt[i] = s[i - 1] -  0 ; n     cnt[i] += cnt[i - 1]; n   } n   bool yes = 1; n   for (int i = (1); i <= (n); i++) { n     if (arr[i] != i) { n       int has = 0; n       if (arr[i] > i) n         has = cnt[arr[i] - 1] - cnt[i - 1]; n       else n         has = cnt[i - 1] - cnt[arr[i] - 1]; n       if (has < abs(arr[i] - i)) { n         yes = 0; n         break; n       } n     } n   } n   if (yes) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n vector<int> v[maxn]; n vector<int> edge[maxn]; n int flag[maxn], val[2][maxn]; n bool bad[maxn]; n int par[maxn][20]; n inline int dfs(int x, int p) { n   par[x][0] = p; n   for (int i = 1; i < 20; i++) par[x][i] = par[par[x][i - 1]][i - 1]; n   flag[x] = flag[p] + 1; n   int ret = flag[x]; n   int cnt = 0; n   for (int i = 0; i < v[x].size(); i++) { n     int u = v[x][i]; n     if (flag[u] == 0) n       ret = min(ret, dfs(u, x)); n     else { n       if (flag[u] == flag[x] - 1) n         cnt++; n       else n         ret = min(ret, flag[u]); n     } n   } n   if (cnt > 1) ret = min(ret, flag[x] - 1); n   bad[x] = ret == flag[x]; n   return ret; n } n inline int Par(int x, int k) { n   for (int i = 20; i >= 0; i--) n     if (k & (1 << i)) { n       x = par[x][i]; n       k -= (1 << i); n     } n   return x; n } n inline int cpar(int x, int y) { n   if (flag[x] < flag[y]) swap(x, y); n   x = Par(x, flag[x] - flag[y]); n   if (x == y) return x; n   for (int i = 19; i >= 0; i--) n     if (par[x][i] != par[y][i]) { n       x = par[x][i]; n       y = par[y][i]; n     } n   return par[x][0]; n } n inline pair<int, int> dfs2(int x) { n   pair<int, int> ret(0, 0); n   ret.first += val[0][x]; n   ret.second += val[1][x]; n   flag[x] = true; n   for (int i = 0; i < v[x].size(); i++) n     if (flag[v[x][i]] == 0) { n       pair<int, int> g = dfs2(v[x][i]); n       ret.first += g.first; n       ret.second += g.second; n     } n   if (bad[x] && ret.first && ret.second) { n     cout <<  No ; n     exit(0); n   } n   return ret; n } n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     if (flag[i] == 0) dfs(i, 0); n   while (q--) { n     int x, y; n     cin >> x >> y; n     int u = cpar(x, y); n     val[0][x]++; n     val[0][u]--; n     val[1][u]--; n     val[1][y]++; n   } n   memset(flag, 0, sizeof flag); n   for (int i = 1; i <= n; i++) n     if (flag[i] == 0) { n       pair<int, int> G = dfs2(i); n       if (G.first || G.second) { n         cout <<  No ; n         return 0; n       } n     } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s, n, b[7][7], a[49], used[49], n2; n bool check() { n   for (int i = 0; i < (int)(n); i++) { n     int sr = 0; n     for (int j = 0; j < (int)(n); j++) sr += b[i][j]; n     if (sr != s) return false; n     sr = 0; n     for (int j = 0; j < (int)(n); j++) sr += b[j][i]; n     if (sr != s) return false; n   } n   int sr = 0; n   for (int i = 0; i < (int)(n); i++) sr += b[i][i]; n   if (sr != s) return false; n   sr = 0; n   for (int i = 0; i < (int)(n); i++) sr += b[i][n - 1 - i]; n   if (sr != s) return false; n   return true; n } n void rec(int i, int j, int sr) { n   if (i == n) { n     if (!check()) return; n     for (int q = 0; q < (int)(n); q++) { n       for (int w = 0; w < (int)(n); w++) { n         if (w) printf(   ); n         printf( %d , b[q][w]); n       } n       printf(  n ); n     } n     exit(0); n   } n   if (j == n - 1) { n     int r1 = s - sr; n     int r2 = r1; n     int r3 = r1; n     if (i == n - 1) { n       r2 = 0, r3 = 0; n       for (int z = 0; z < (int)(n - 1); z++) r2 += b[z][z], r3 += b[z][n - 1]; n       r2 = s - r2, r3 = s - r3; n     } n     if (r1 != r2 || r1 != r3) return; n     for (int q = 0; q < (int)(n2); q++) n       if (!used[q] && a[q] == r1) { n         used[q] = true; n         b[i][j] = a[q]; n         rec(i + 1, 0, 0); n         used[q] = false; n         break; n       } n     return; n   } n   if (i == n - 1) { n     int sw = 0; n     for (int z = 0; z < (int)(n - 1); z++) sw += b[z][j]; n     int t1 = s - sw; n     int t2 = t1; n     if (j == 0) { n       int ss = 0; n       for (int z = 0; z < (int)(n - 1); z++) ss += b[z][n - 1 - z]; n       t2 = s - ss; n     } n     if (t1 != t2) return; n     for (int q = 0; q < (int)(n2); q++) n       if (!used[q] && a[q] == t1) { n         used[q] = true; n         b[i][j] = a[q]; n         rec(i, j + 1, sr + a[q]); n         used[q] = false; n       } n     return; n   } n   int lu = a[0] - 1; n   for (int q = 0; q < (int)(n2); q++) n     if (!used[q] && a[q] != lu) { n       used[q] = true; n       b[i][j] = lu = a[q]; n       rec(i, j + 1, sr + lu); n       used[q] = false; n     } n } n int aa(int x) { return x < 0 ? -x : x; } n int main() { n   scanf( %d , &n); n   n2 = n * n; n   for (int i = 0; i < (int)(n2); i++) scanf( %d , &a[i]); n   sort(a, a + n2); n   s = 0; n   for (int i = 0; i < (int)(n2); i++) s += a[i]; n   s /= n; n   printf( %d n , s); n   rec(0, 0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n inline unsigned int R() { n   static unsigned int seed = 416; n   return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; n } n const int N = 2333; n int n, l[N], r[N], a[N], s[N]; n void GG() { n   puts( NO ); n   exit(0); n } n int main() { n   read(n); n   for (register int i = 1; i <= n; i++) read(l[i]); n   for (register int i = 1; i <= n; i++) read(r[i]); n   for (register int i = 1; i <= n; i++) a[i] = n - (l[i] + r[i]); n   for (register int i = 1; i <= n; i++) { n     int p = 0; n     for (register int j = 1; j <= i - 1; j++) p += a[j] > a[i]; n     if (p != l[i]) GG(); n     p = 0; n     for (register int j = i + 1; j <= n; j++) p += a[j] > a[i]; n     if (p != r[i]) GG(); n   } n   puts( YES ); n   for (register int i = 1; i <= n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vt[202][202]; n int cnt, n; n int dfs(int x, int y) { n   int mx1 = 0, mx2 = 0, mx = 0; n   for (int i = 1; i <= n; i++) n     if (vt[x][i] && i != y) { n       mx = max(mx, dfs(i, x)); n       if (cnt > mx1) n         mx2 = mx1, mx1 = cnt; n       else if (cnt > mx2) n         mx2 = cnt; n     } n   mx = max(mx, mx1 + mx2); n   cnt = mx1 + 1; n   return mx; n } n int main() { n   ios::sync_with_stdio(0); n   int x, y, m, sm = 0; n   cin >> n; n   for (int i = 0; i < n - 1; i++) cin >> x >> y, vt[x][y] = 1, vt[y][x] = 1; n   int mx = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       int l = 0, k = 0; n       if (vt[i][j]) { n         l = dfs(i, j); n         k = dfs(j, i); n         mx = max(mx, l * k); n       } n     } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n //#pragma GCC optimize( Ofast ) n //#pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n   n #define ms(s, n) memset(s, n, sizeof(s)) n #define FOR(i, a, b) for (int i = (a); i < (b); ++i) n #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i) n #define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n #define sz(a) int((a).size()) n #define present(t, x) (t.find(x) != t.end()) n #define all(a) (a).begin(), (a).end() n #define uni(a) (a).erase(unique(all(a)), (a).end()) n #define pb push_back n #define pf push_front n #define mp make_pair n #define fi first n #define se second n #define prec(n) fixed<<setprecision(n) n #define bit(n, i) (((n) >> (i)) & 1) n #define bitcount(n) __builtin_popcountll(n) n typedef long long ll; n typedef unsigned long long ull; n typedef long double ld; n typedef pair<int, int> pi; n typedef vector<int> vi; n typedef vector<pi> vii; n const int MOD = (int) 1e9 + 7; n const int FFTMOD = 119 << 23 | 1; n const int INF = (int) 1e9 + 23111992; n const ll LINF = (ll) 1e18 + 23111992; n const ld PI = acos((ld) -1); n const ld EPS = 1e-9; n inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;} n inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} n inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} n template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;} n template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;} n inline ull isqrt(ull k) {ull r = sqrt(k) + 1; while (r * r > k) r--; return r;} n inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;} n inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;} n inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;} n inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;} n inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);} n inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;} n inline int sign(ld x, ld y) {return sign(x - y);} n mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); n inline int mrand() {return abs((int) mt());} n inline int mrand(int k) {return abs((int) mt()) % k;} n #define db(x) cerr <<  [  << #x <<  :   << (x) <<  ]  ; n #define endln cerr <<   n ; n  n void chemthan() { n     int n; cin >> n; n     vi a(n); n     vi b(n); n     FOR(i, 0, n) cin >> a[i]; n     FOR(i, 0, n) cin >> b[i]; n     long long sum = 0; n     FOR(i, 0, n) sum += abs(a[i] - b[i]); n     auto func = [&] (int sa, int sb) { n         long long res = sum; n         vi dc; n         FOR(i, 0, n) dc.pb(sb * a[i]), dc.pb(sb * b[i]); n         sort(all(dc)), uni(dc); n         vector<long long> fen(sz(dc) + 1, LINF); n         auto upd = [&] (int p, long long v) { n             p++; n             for (; p < sz(fen); p += p & -p) { n                 chkmin(fen[p], v); n             } n         }; n         auto query = [&] (int p) { n             p++; n             long long res = LINF; n             for (; 0 < p; p -= p & -p) { n                 chkmin(res, fen[p]); n             } n             return res; n         }; n         vector<tuple<int, int, int>> v; n         FOR(i, 0, n) { n             v.pb({sa * a[i], sb * b[i], 1}); n             v.pb({sa * b[i], sb * a[i], 0}); n         } n         sort(all(v)); n         for (auto [x, y, t] : v) { n             int k = lower_bound(all(dc), y) - dc.begin(); n             if (!t) { n                 upd(k, (long long) - x - y - abs(sa * x - sb * y)); n             } n             else { n                 chkmin(res, sum + x + y + query(k) - abs(sa * x - sb * y)); n             } n         } n         return res; n     }; n     long long res = sum; n     for (int sa : {-1, 1}) { n         for (int sb : {-1, 1}) { n             chkmin(res, func(sa, sb)); n         } n     } n     cout << res <<   n ; n } n  n int32_t main(int32_t argc, char* argv[]) { n     ios_base::sync_with_stdio(0), cin.tie(0); n     if (argc > 1) { n         assert(freopen(argv[1],  r , stdin)); n     } n     if (argc > 2) { n         assert(freopen(argv[2],  wb , stdout)); n     } n     chemthan(); n     cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long nmax = 1e6 + 5; n const unsigned long long oo = 1e18; n const long long mod = 1e9 + 7; n const long long ox[4] = {-1, 1, 0, 0}; n const long long oy[4] = {0, 0, -1, 1}; n const long long dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const long long dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n long long a[nmax]; n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n   }; n   long long t, n, res; n   cin >> t; n   while (t--) { n     cin >> n; n     bool kt = false; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     for (int i = 1; i <= n - 1; i++) n       if (a[i] >= a[i + 1]) { n         kt = 1; n         break; n       } n     if (kt || !(n % 2)) { n       cout <<  YES  n            <<   n ; n     } else n       cout <<  NO  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 202020, P = 1e9 + 7; n int n; n int a[N], par[N], cnt[2][N], c[2][N]; n long long ans; n vector<int> g[N]; n void upd(long long &a, long long b) { n   if (b < 0) b += P; n   a += b; n   if (a >= P) a -= P; n } n void dfs(int u, int fa) { n   cnt[0][u] = 1; n   cnt[1][u] = 0; n   par[u] = fa; n   for (int i = (0); i < ((int)g[u].size()); i++) { n     int v = g[u][i]; n     if (v == fa) continue; n     dfs(v, u); n     cnt[0][u] += cnt[1][v]; n     cnt[1][u] += cnt[0][v]; n   } n } n void dfs2(int u) { n   if (par[u]) { n     c[0][u] = c[1][par[u]]; n     c[1][u] = c[0][par[u]]; n   } else { n     c[0][u] = cnt[0][u]; n     c[1][u] = cnt[1][u]; n   } n   for (int i = (0); i < ((int)g[u].size()); i++) { n     int v = g[u][i]; n     if (v == par[u]) continue; n     dfs2(v); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = (1); i < (n + 1); i++) { n     scanf( %d , a + i); n     if (a[i] < 0) a[i] += P; n   } n   for (int i = (1); i < (n); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, 0); n   dfs2(1); n   for (int i = (1); i < (n + 1); i++) { n     long long pre = ans; n     int c0 = c[0][i], c1 = c[1][i]; n     upd(ans, 1ll * c0 * c0 % P * a[i] % P); n     upd(ans, -1ll * c1 * c1 % P * a[i] % P); n     if (par[i]) { n       c0 = c[0][par[i]] - cnt[1][i]; n       c1 = c[1][par[i]] - cnt[0][i]; n       upd(ans, 1ll * c0 * c0 % P * a[i] % P); n       upd(ans, -1ll * c1 * c1 % P * a[i] % P); n     } n     for (int j = (0); j < ((int)g[i].size()); j++) { n       int v = g[i][j]; n       if (v != par[i]) { n         c0 = cnt[0][v]; n         c1 = cnt[1][v]; n         upd(ans, 1ll * c0 * c0 % P * a[i] % P); n         upd(ans, -1ll * c1 * c1 % P * a[i] % P); n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define Input(v, n) fr(i, n) cin >> v[i]; n #define checkk(v, n) fr(i, n) cout << v[i] <<     << endl; n #define sort(v) sort(v.begin(), v.end()); n #define int long long int n #define double long double n #define fr(i, n) for (int i = 0; i < n; i++) n #define frr(i, s, e) for (int i = s; i <= e; i++) n #define rfr(i, s, e) for (int i = s; i >= e; i--) n #define fix(f,n) fixed<<setprecision(n)<<f n #define endl   n  n #define pb push_back n #define mp make_pair n #define mod 1000000007 n  n int test; n double v,ans; n  n void add(double &c,double &m,double &p,int i,double t){ n    if(i==0){ n        if(m==0) p+=t; n        else { n            t=t/2.0; n            m+=t,p+=t; n        } n    } n    else{ n        if(c==0) p+=t; n        else { n            t=t/2.0; n            c+=t,p+=t; n        } n    } n } n  n void calc(double c,double m,double p,double val,double count){ n     if(c<0.000001) c=0; n     if(m<0.000001) m=0; n  n     double t1=c,t2=m,t3=p; n     ans+=(count*(val*p)); n      n     if(c!=0){ n         double nan=val*c; n         if(c<=v) add(c,m,p,0,c),c=0,calc(c,m,p,nan,count+1.0); n         else  add(c,m,p,0,v),c=c-v,calc(c,m,p,nan,count+1.0); n     } n     c=t1,m=t2,p=t3; n     if(m!=0){ n         double nan=val*m; n         if(m<=v) add(c,m,p,1,m),m=0,calc(c,m,p,nan,count+1.0); n         else  add(c,m,p,1,v),m=m-v,calc(c,m,p,nan,count+1.0); n     } n      n } n  n void solve() n {    n     ans=0.0; n     double c,m,p; n     cin>>c>>m>>p>>v; n     calc(c,m,p,1.0,1.0); n     cout<<fix(ans,12)<<endl; n } n  n int32_t main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n       n     cin>>test; n     //test=1; n     while (test--) n     { n         solve(); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n vector<string> numbers; n unsigned n = 2; n unsigned k = 5; n int taken[100]; n void print(unsigned i) { n   unsigned l; n   string str =   ; n   for (l = 0; l <= i - 1; l++) str += (taken[l] + 1) +  0 ; n   numbers.push_back(str); n } n void variate(unsigned i) { n   unsigned j; n   if (i >= k) { n     print(i); n     return; n   } n   for (j = 0; j < n; j++) { n     taken[i] = j; n     variate(i + 1); n   } n } n int main(void) { n   for (unsigned i = 1; i < 11; i++) { n     k = i; n     variate(0); n   } n   string input; n   cin >> input; n   for (int i = 0; i < input.size(); i++) { n     if (input[i] ==  4 ) n       input[i] =  1 ; n     else n       input[i] =  2 ; n   } n   for (int i = 0; i < numbers.size(); i++) { n     if (numbers[i] == input) cout << i + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long INF = (long long)2e18; n const int N = (int)1e6 + 77; n int n; n long long a[N]; n long long solve(long long d) { n   long long ans = 0; n   long long cur = 0; n   for (int i = 0; i < n; i++) { n     long long h = a[i]; n     long long z = min(h, d - cur); n     h -= z; n     cur += z; n     if (cur <= d / 2) ans += cur; n     if (cur > d / 2) ans += d - cur; n     if (cur == d) cur = 0; n     if (h > 0) { n       h %= d; n       cur = h; n       if (cur <= d / 2) ans += cur; n       if (cur > d / 2) ans += d - cur; n     } n   } n   if (cur != 0) throw; n   return ans; n } n int main() { n   scanf( %d , &n); n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     sum += a[i]; n   } n   if (sum == 1) { n     printf( -1 n ); n     return 0; n   } n   long long ans = INF; n   for (long long x = 2; x * x <= sum; x++) { n     if (sum % x) continue; n     ans = min(ans, solve(x)); n     while (sum % x == 0) sum /= x; n   } n   if (sum > 1) ans = min(ans, solve(sum)); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, finans = 0; n int a[N], ans[N], sieve[N], countprime[N]; n vector<int> primes[N]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     ans[i] = 1; n   } n   for (int i = 2; i <= 1e5; i++) sieve[i] = 1; n   for (int i = 2; i <= 1e5; i++) { n     if (sieve[i]) { n       for (int j = 1; i * j <= 1e5; j++) { n         primes[i * j].push_back(i); n         sieve[i * j] = 0; n       } n     } n   } n   for (int i = n; i >= 1; i--) { n     for (auto it : primes[a[i]]) ans[i] = max(ans[i], countprime[it] + 1); n     for (auto it : primes[a[i]]) countprime[it] = max(countprime[it], ans[i]); n     finans = max(ans[i], finans); n   } n   cout << finans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n = 0; n   string s; n   long long int res = 0ll; n   cin >> n; n   cin >> s; n   res = (1ll * n * (n + 1)) / 2ll - 1ll * n; n   for (int i = 0; i + 1 < n; ++i) { n     if (s[i] != s[i + 1]) { n       int j; n       for (j = i + 1; j < n; ++j) { n         if (s[i] == s[j]) break; n       } n       res -= (j - i - 1); n     } n   } n   reverse(s.begin(), s.end()); n   for (int i = 0; i + 1 < n; ++i) { n     if (s[i] != s[i + 1]) { n       int j; n       for (j = i + 1; j < n; ++j) { n         if (s[i] == s[j]) break; n       } n       res -= (j - i - 2); n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n const int maxn = 1e6 + 5; n vector<int> get(map<int, vector<int>> mp) { n   vector<int> res; n   for (auto p : mp) { n     for (auto x : p.second) { n       res.push_back(x); n     } n   } n   return res; n } n int n; n map<int, vector<int>> mp; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     mp[x].push_back(i + 1); n   } n   vector<vector<int>> ans; n   bool ok = false; n   int val = -1; n   vector<int> v; n   for (auto p : mp) { n     v.push_back(p.first); n     if (int(p.second.size()) >= 3) { n       val = p.first; n       ok = true; n       break; n     } n   } n   map<int, vector<int>> _mp = mp; n   if (ok) { n     int x = val; n     swap(mp[x][0], mp[x][1]); n     ans.push_back(get(mp)); n     mp = _mp; n     swap(mp[x][0], mp[x][2]); n     ans.push_back(get(mp)); n     mp = _mp; n     swap(mp[x][1], mp[x][2]); n     ans.push_back(get(mp)); n     mp = _mp; n   } else { n     int N = v.size(); n     int val1, val2; n     for (int i = 0; i < N; i++) { n       for (int j = i + 1; j < N; j++) { n         if (int(mp[v[i]].size()) >= 2 && int(mp[v[j]].size()) >= 2) { n           val1 = v[i]; n           val2 = v[j]; n           ok = true; n         } n       } n     } n     if (!ok) out( NO ); n     ans.push_back(get(mp)); n     mp = _mp; n     swap(mp[val1][0], mp[val1][1]); n     ans.push_back(get(mp)); n     mp = _mp; n     swap(mp[val2][0], mp[val2][1]); n     ans.push_back(get(mp)); n     mp = _mp; n   } n   cout <<  YES  << endl; n   for (auto p : ans) { n     for (auto x : p) cout << x <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int X = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar(); n   return X * w; n } n const int N = 200000 + 10; n const int inf = 0x3f3f3f3f; n int n, Q; n char s[N]; n int sum[N][26]; n struct SuffixArray { n   int sa[N], rnk[N], height[N], x[N], y[N], z[N]; n   void SuffixSort() { n     int m = 256; n     for (int i = 1; i <= n; ++i) ++z[x[i] = s[i]]; n     for (int i = 2; i <= m; ++i) z[i] += z[i - 1]; n     for (int i = n; i; --i) sa[z[x[i]]--] = i; n     for (int k = 1; k < n; k <<= 1) { n       int p = 0; n       for (int i = n - k + 1; i <= n; ++i) y[++p] = i; n       for (int i = 1; i <= n; ++i) n         if (sa[i] > k) y[++p] = sa[i] - k; n       for (int i = 1; i <= m; ++i) z[i] = 0; n       for (int i = 1; i <= n; ++i) ++z[x[i]]; n       for (int i = 2; i <= m; ++i) z[i] += z[i - 1]; n       for (int i = n; i; --i) sa[z[x[y[i]]]--] = y[i], y[i] = 0; n       swap(x, y), x[sa[1]] = p = 1; n       for (int i = 2; i <= n; ++i) n         x[sa[i]] = n             (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]) n                 ? p n                 : ++p; n       if (p == m) break; n       m = p; n     } n   } n   void GetHeight() { n     for (int i = 1; i <= n; ++i) rnk[sa[i]] = i; n     for (int i = 1, j = 0; i <= n; ++i) { n       if (j) --j; n       int p = sa[rnk[i] - 1]; n       while (s[i + j] == s[p + j]) ++j; n       height[rnk[i]] = j; n     } n   } n   int lg[N], st[18][N]; n   void buildST() { n     for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; n     for (int i = 1; i <= n; ++i) st[0][i] = height[i]; n     for (int i = 1; i < 18; ++i) n       for (int j = 1; j <= n; ++j) n         st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); n   } n   int qmin(int l, int r) { n     int t = lg[r - l + 1]; n     return min(st[t][l], st[t][r - (1 << t) + 1]); n   } n   int LCP(int x, int y) { n     if (rnk[x] > rnk[y]) swap(x, y); n     return qmin(rnk[x] + 1, rnk[y]); n   } n } A, B; n struct SegmentTree { n   int minv[N << 2], tag[N << 2]; n   SegmentTree() { n     memset(minv, 0x3f, sizeof(minv)), memset(tag, 0x3f, sizeof(tag)); n   } n   void update(int o, int l, int w) { n     tag[o] = min(tag[o], w), minv[o] = min(minv[o], l + w); n   } n   void pushup(int o) { minv[o] = min(minv[(o << 1)], minv[(o << 1 | 1)]); } n   void pushdown(int o, int l, int r) { n     int mid = (l + r) >> 1; n     if (tag[o] != inf) n       update((o << 1), l, tag[o]), update((o << 1 | 1), mid + 1, tag[o]), n           tag[o] = inf; n   } n   void modify(int o, int l, int r, int ql, int qr, int w) { n     if (ql <= l && r <= qr) { n       update(o, l, w); n       return; n     } n     int mid = (l + r) >> 1; n     pushdown(o, l, r); n     if (ql <= mid) modify((o << 1), l, mid, ql, qr, w); n     if (qr > mid) modify((o << 1 | 1), mid + 1, r, ql, qr, w); n     pushup(o); n   } n   int query(int o, int l, int r, int ql, int qr) { n     if (ql <= l && r <= qr) return minv[o]; n     int mid = (l + r) >> 1, res = inf; n     pushdown(o, l, r); n     if (ql <= mid) res = min(res, query((o << 1), l, mid, ql, qr)); n     if (qr > mid) res = min(res, query((o << 1 | 1), mid + 1, r, ql, qr)); n     return res; n   } n } L, R; n void InitAA(SuffixArray &lcp_sa, SuffixArray &lcs_sa, SegmentTree &T) { n   for (int l = 1; l <= n / 2; ++l) n     for (int i = l, j = l << 1; j <= n; i += l, j += l) { n       int lcp = lcp_sa.LCP(i, j), lcs = lcs_sa.LCP(n - i + 1, n - j + 1); n       if (lcp + lcs - 1 >= l) n         T.modify(1, 1, n, i - lcs + 1, i + lcp - l, 2 * l - 1); n     } n } n bool CheckBorder(int l, int r) { n   int len = r - l + 1, S = sqrt(n) + 1; n   for (int i = 1; i < len && i <= S; ++i) n     if (A.LCP(l, r - i + 1) >= i) return 1; n   for (int i = A.rnk[l] - 1, h = A.height[i + 1], c = 0; i && c <= S && h > S; n        ++c, h = min(h, A.height[i--])) n     if (A.sa[i] >= l && A.sa[i] <= r && A.sa[i] + h > r) return 1; n   for (int i = A.rnk[l] + 1, h = A.height[i], c = 0; i <= n && c <= S && h > S; n        ++c, h = min(h, A.height[++i])) n     if (A.sa[i] >= l && A.sa[i] <= r && A.sa[i] + h > r) return 1; n   return 0; n } n bool CheckNS(int l, int r) { n   int flag = 1; n   for (int i = 0; i < 26; ++i) n     if (sum[r][i] - sum[l - 1][i] >= 2) { n       flag = 0; n       break; n     } n   return flag; n } n bool Check1(int l, int r) { n   int len = r - l + 1; n   for (int i = 1; i * i <= len; ++i) { n     if (len % i) continue; n     if (A.LCP(l, l + i) >= len - i) return 1; n     if (i != 1 && A.LCP(l, l + len / i) >= len - len / i) return 1; n   } n   return 0; n } n bool Check2(int l, int r) { n   if (CheckBorder(l, r)) return 1; n   if (L.query(1, 1, n, l, l) <= r) return 1; n   if (R.query(1, 1, n, n - r + 1, n - r + 1) <= n - l + 1) return 1; n   return 0; n } n bool Check3(int l, int r) { n   if (sum[r][s[l] -  a ] - sum[l][s[l] -  a ]) return 1; n   if (sum[r - 1][s[r] -  a ] - sum[l - 1][s[r] -  a ]) return 1; n   if (L.query(1, 1, n, l, r) <= r) return 1; n   return 0; n } n int main() { n   n = read(); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < 26; ++j) sum[i][j] = sum[i - 1][j] + (j == s[i] -  a ); n   A.SuffixSort(), A.GetHeight(), A.buildST(); n   reverse(s + 1, s + n + 1); n   B.SuffixSort(), B.GetHeight(), B.buildST(); n   reverse(s + 1, s + n + 1); n   InitAA(A, B, L), InitAA(B, A, R); n   Q = read(); n   while (Q--) { n     int l = read(), r = read(); n     if (CheckNS(l, r)) { n       puts( -1 ); n       continue; n     } n     if (Check1(l, r)) { n       puts( 1 ); n       continue; n     } n     if (Check2(l, r)) { n       puts( 2 ); n       continue; n     } n     if (Check3(l, r)) { n       puts( 3 ); n       continue; n     } n     puts( 4 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 1000000007; n const double eps = 1e-6; n const double PI = acos(-1); n int tar, n, tot, a[N], hs[N], cnt[N], mx[N], now[N]; n vector<int> vc; n bool check(int add) { n   vc.clear(); n   int c = tar; n   int p = mx[c]; n   while (c) { n     if (add > c) add = 0; n     while (!now[p] && p) p--; n     p = min(p, mx[c]); n     if (!p && !add) break; n     if (p && hs[p] >= add) { n       vc.push_back(p); n       int v = hs[p], num = min(now[p], c / v); n       now[p] -= num; n       c -= num * v; n     } else { n       c -= add; n       add = 0; n     } n   } n   for (auto& x : vc) now[x] = cnt[x]; n   return c; n } n int main() { n   scanf( %d%d , &tar, &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     hs[++tot] = a[i]; n   } n   sort(hs + 1, hs + tot + 1); n   tot = unique(hs + 1, hs + tot + 1) - hs - 1; n   for (int i = 1; i <= n; i++) n     cnt[lower_bound(hs + 1, hs + tot + 1, a[i]) - hs]++; n   for (int i = 1; i <= tot; i++) now[i] = cnt[i]; n   for (int i = 1; i <= tar; i++) n     mx[i] = upper_bound(hs + 1, hs + tot + 1, i) - hs - 1; n   for (int i = 1; i <= tar; i++) { n     if (check(i)) { n       printf( %d n , i); n       return 0; n     } n   } n   puts( Greed is good ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long x[2005], y[2005]; n long long c[2005]; n long long k[2005]; n bool vis[2005]; n long long dis[2005]; n long long tower[2005]; n long long diss[2005]; n long long way[2005][2005]; n long long townum = 0; n long long add[2005][2]; n long long addnum = 0; n long long ans = 0; n void pri() { n   printf( %lld n , ans); n   printf( %lld n , townum); n   for (int i = 1; i <= townum; i++) { n     printf( %lld  , tower[i]); n   } n   printf(  n%lld n , addnum); n   for (int i = 1; i <= addnum; i++) { n     printf( %lld %lld n , add[i][0], add[i][1]); n   } n } n void init() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       way[i][j] = (abs(x[i] - x[j]) + abs(y[i] - y[j])) * (k[i] + k[j]); n     } n   } n   for (int i = 1; i <= 2003; i++) dis[i] = 1e18; n   for (int i = 1; i <= n; i++) way[i][0] = way[0][i] = c[i]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &x[i], &y[i]); n   } n   int minn = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &c[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &k[i]); n   } n   init(); n   dis[0] = 0; n   for (int i = 0; i <= n; i++) { n     long long ma = 1e18 + 10; n     int id = -1; n     for (int j = 0; j <= n; j++) { n       if (!vis[j] && dis[j] < ma) { n         id = j; n         ma = dis[j]; n       } n     } n     vis[id] = true; n     ans += dis[id]; n     if (id != 0) { n       if (diss[id] != 0) { n         add[++addnum][0] = diss[id]; n         add[addnum][1] = id; n       } else { n         tower[++townum] = id; n       } n     } n     for (int j = 0; j <= n; j++) { n       if (!vis[j]) { n         if (dis[j] > way[id][j]) { n           dis[j] = way[id][j]; n           diss[j] = id; n         } n       } n     } n   } n   pri(); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1000000000; n const int MOD = 1000000007; n const int base = 1000 * 1000 * 1000; n int main() { n   int n, m; n   cin >> n >> m; n   int l = 1, r = n; n   string str, str1, str2, str3; n   int t; n   for (int i = 0; i < m; i++) { n     cin >> str >> str1 >> str2 >> str3 >> t; n     if (str2 ==  left ) { n       if (t <= r) { n         r = t - 1; n       } n     } else { n       if (t >= l) { n         l = t + 1; n       } n     } n     if (l > r) { n       cout << -1; n       return 0; n     } n   } n   cout << r - l + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int X1[127], X2[128], Y1[111], Y2[105]; n bool u[134][134]; n vector<int> res; n int check(int xx1, int yy1, int x2, int y2) { n   if (x2 < 0 || y2 < 0 || x2 >= n || y2 >= m || u[x2][y2]) return 0; n   if (xx1 > x2) swap(xx1, x2); n   if (yy1 > y2) swap(yy1, y2); n   if (xx1 != x2) { n     xx1++; n     y2++; n   } else { n     x2++; n     yy1++; n   } n   for (int i = 0; i < k; i++) { n     int x3 = max(xx1, X1[i]); n     int y3 = max(yy1, Y1[i]); n     int x4 = min(x2, X2[i]); n     int y4 = min(y2, Y2[i]); n     if (x3 < x4 && y3 <= y4 || x3 <= x4 && y3 < y4) return 0; n   } n   return 1; n } n int go(int i, int j) { n   int t = 1; n   u[i][j] = 1; n   if (check(i, j, i + 1, j)) t += go(i + 1, j); n   if (check(i, j, i - 1, j)) t += go(i - 1, j); n   if (check(i, j, i, j + 1)) t += go(i, j + 1); n   if (check(i, j, i, j - 1)) t += go(i, j - 1); n   return t; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (!u[i][j]) res.push_back(go(i, j)); n   sort(res.begin(), res.end()); n   for (int i = 0; i < res.size(); i++) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10005; n int a[26][26][maxn]; n char s[maxn]; n int main(int argc, char const *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> s; n   n = strlen(s); n   for (int i = n; i < 2 * n - 1; ++i) { n     s[i] = s[i - n]; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 1; j < n; ++j) { n       a[s[i] -  a ][s[i + j] -  a ][j]++; n     } n   } n   int res = 0; n   for (int i = 0; i < 26; ++i) { n     int c = 0; n     for (int j = 1; j < n; ++j) { n       int t = 0; n       for (int k = 0; k < n; ++k) { n         if (s[k] ==  a  + i) { n           if (a[s[k] -  a ][s[j + k] -  a ][j] == 1) { n             ++t; n           } n         } n       } n       c = max(c, t); n     } n     res += c; n   } n   cout << (double)res / n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005]; n int main() { n   int n; n   while (cin >> n) { n     for (int i = n; i >= 0; i--) n       if (!a[i]) { n         int mm = (1 << ((int)(log2(i) + 1))) - 1; n         a[i] = mm ^ i; n         a[mm ^ i] = i; n       } n     long long int ans = 0; n     for (int i = 0; i <= n; i++) { n       ans += a[i] ^ i; n     } n     cout << ans << endl; n     for (int i = 0; i <= n; i++) { n       cout << a[i]; n       if (i != n) n         cout <<    ; n       else n         cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ,  omit-frame-pointer ,  inline ) n #pragma GCC option( arch=native ,  tune=native ,  no-zero-upper ) n #pragma GCC target( avx2 ) n using namespace std; n template <typename T> n void maxtt(T& t1, T t2) { n   t1 = max(t1, t2); n } n template <typename T> n void mintt(T& t1, T t2) { n   t1 = min(t1, t2); n } n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n const long long MOD2 = (long long)1000000007 * (long long)1000000007; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int& x, int y, int mod = 1000000007) { n   x += y; n   if (x >= mod) x -= mod; n   if (x < 0) x += mod; n   assert(x >= 0 && x < mod); n } n void et(int x = -1) { n   printf( %d n , x); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } n vector<int> sufPaliLen[(100035)]; n int a[(100035)], p[(100035)], q[(100035)], pos, c[(100035)], xyx; n void add(int pos) { n   sufPaliLen[pos].clear(); n   if (a[pos] == a[pos - 1]) sufPaliLen[pos].push_back(2); n   for (int x : sufPaliLen[pos - 1]) { n     if (a[pos - 1 - x] == a[pos]) sufPaliLen[pos].push_back(x + 2); n   } n } n bool ckEvenPali(int l, int r) { n   for (int z : sufPaliLen[r]) { n     if (z == r - l + 1) return 1; n   } n   return 0; n } n int qy() { n   int ans = xyx + c[pos]; n   int cur = pos; n   while (p[cur] && q[pos] <= cur - p[cur] / 2) { n     cur -= p[cur] / 2; n     ans++; n   } n   return ans; n } n void fmain(int tid) { n   scanf( %d , &n); n   q[0] = 1; n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     pos++; n     scanf( %d , a + pos); n     bool fg = 0; n     add(pos); n     if (!sufPaliLen[pos].empty()) { n       int len = sufPaliLen[pos][0]; n       if (ckEvenPali(pos - len / 2 - len + 1, pos - len / 2)) { n         pos -= len; n         xyx += 2; n         fg = 1; n       } n     } n     if (!fg) { n       p[pos] = sufPaliLen[pos].empty() ? 0 : sufPaliLen[pos][0]; n       q[pos] = q[pos - 1]; n       c[pos] = c[pos - 1]; n       if (ckEvenPali(q[pos], pos)) { n         q[pos] += (pos - q[pos] + 1) / 2; n         c[pos]++; n       } n     } n     printf( %d  , qy()); n   } n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n const int M = 1000005; n int go[M][4], fa[M], sum[M], val[M], lzy[M], hsc[M], tsz; n void pull(int x) { n   if (!x) return; n   sum[x] = val[x] + sum[go[x][0]] + sum[go[x][1]]; n   if (go[x][2]) sum[x] += min(sum[go[x][2]], sum[go[x][3]]); n   hsc[x] = go[x][2] || hsc[go[x][0]] || hsc[go[x][1]]; n } n int make(int v) { n   ++tsz; n   val[tsz] = v; n   pull(tsz); n   return tsz; n } n void rev(int x) { n   if (!x) return; n   swap(go[x][0], go[x][1]); n   lzy[x] ^= 1; n } n void push(int x) { n   if (lzy[x]) { n     rev(go[x][0]), rev(go[x][1]); n     if (go[x][2]) rev(go[x][2]), rev(go[x][3]); n     lzy[x] = 0; n   } n } n int is2(int x) { return go[fa[x]][0] == x || go[fa[x]][1] == x; } n int pd2(int x) { return go[fa[x]][0] == x ? 0 : go[fa[x]][1] == x ? 1 : -1; } n int is4(int x) { return go[fa[x]][2] == x || go[fa[x]][3] == x; } n int pd4(int x) { return go[fa[x]][2] == x ? 2 : go[fa[x]][3] == x ? 3 : -1; } n int isl(int x) { return !go[x][0] && !go[x][1]; } n void rot(int x) { n   int y = fa[x], z = fa[y], p = pd2(x), w = go[x][p ^ 1]; n   if (is2(y)) n     go[z][pd2(y)] = x; n   else if (is4(y)) n     go[z][pd4(y)] = x; n   fa[x] = z; n   go[x][p ^ 1] = y; n   fa[y] = x; n   go[y][p] = w; n   if (w) fa[w] = y; n   pull(y); n } n void cl(int x) { n   if (pd2(x) != -1) cl(fa[x]); n   push(x); n } n void splay(int x, int y = 0) { n   for (cl(x); pd2(x) != -1 && fa[x] != y; rot(x)) { n     if (pd2(fa[x]) != -1 && fa[fa[x]] != y) n       rot(pd2(fa[x]) == pd2(x) ? fa[x] : x); n   } n   pull(x); n } n int fir(int x) { n   while (go[x][0]) push(x), x = go[x][0]; n   return x; n } n void cyc(int c, int x) { n   int d = pd4(x); n   splay(c); n   push(x); n   int o = fir(go[c][1]); n   splay(o, c); n   go[c][1] = 0; n   go[c][d] = go[x][0]; n   fa[go[c][d]] = c; n   go[x][0] = 0; n   rev(go[x][1]); n   go[o][1] = go[x][1]; n   fa[go[o][1]] = o; n   go[x][1] = 0; n   go[o][0] = go[c][d ^ 1]; n   fa[go[o][0]] = o; n   go[c][d ^ 1] = o; n   fa[o] = c; n   pull(o); n   pull(c); n } n void access(int x) { n   int y = 0, z = x; n   for (; x; y = x, x = fa[x]) { n     splay(x); n     if (fa[x] && go[fa[x]][2]) cyc(fa[x], x); n     go[x][1] = y; n     pull(x); n   } n   splay(z); n } n void mkrt(int x) { n   access(x); n   rev(x); n } n void path(int x, int y) { n   mkrt(x); n   access(y); n   splay(x, y); n } n int get(int x, int y) { n   if (x == y) return 0; n   path(x, y); n   if (fa[x] != y) return -1; n   return sum[y]; n } n bool link(int x, int y, int w) { n   if (x == y) return false; n   path(x, y); n   if (fa[x] != y) { n     int e = make(w); n     fa[x] = e; n     fa[e] = y; n     return true; n   } n   if (hsc[y]) return false; n   int e = make(w), c = make(0); n   go[c][2] = go[x][1]; n   fa[go[c][2]] = c; n   go[c][3] = e; n   fa[e] = c; n   go[x][1] = c; n   fa[c] = x; n   pull(c); n   pull(x); n   pull(y); n   return true; n } n bool cut(int x, int y, int w) { n   if (x == y) return false; n   path(x, y); n   if (fa[x] != y) return false; n   if (!isl(go[x][1])) return false; n   int c = go[x][1]; n   push(c); n   if (go[c][2]) { n     int d = 0; n     if (isl(go[c][2]) && val[go[c][2]] == w) d = 2; n     if (isl(go[c][3]) && val[go[c][3]] == w) d = 3; n     if (!d) return false; n     go[x][1] = go[c][d ^ 1]; n     fa[go[x][1]] = x; n     pull(x); n     pull(y); n     return true; n   } else { n     if (val[c] != w) return false; n     go[x][1] = 0; n     go[y][0] = 0; n     fa[x] = 0; n     pull(x); n     pull(y); n     return true; n   } n } n int n, m; n int U[500005], V[500005]; n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   tsz = n; n   for (int i = 1; i <= m; i++) scanf( %d%d , &U[i], &V[i]); n   for (int i = 1, j = 0; i <= m; i++) { n     if (j < i) { n       j = i; n       link(U[j], V[j], 0); n     } n     while (j < m && link(U[j + 1], V[j + 1], 0)) j++; n     cut(U[i], V[i], 0); n     ans += j - i + 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 202; n const int C = 6000; n int dp[N][C]; n int n, k; n int a[N][2]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     long long x; n     scanf( %lld , &x); n     while (x % 5 == 0) { n       a[i][0]++; n       x /= 5; n     } n     while (x % 2 == 0) { n       a[i][1]++; n       x /= 2; n     } n   } n   for (int i = 0; i <= k; i++) n     for (int j = 0; j < C; j++) dp[i][j] = -1; n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) n     for (int j = k - 1; j >= 0; j--) n       for (int x = 0; x < C; x++) { n         if (dp[j][x] == -1) continue; n         dp[j + 1][x + a[i][0]] = n             max(dp[j + 1][x + a[i][0]], dp[j][x] + a[i][1]); n       } n   int ans = 0; n   for (int i = 0; i < C; i++) ans = max(ans, min(dp[k][i], i)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int cnt = 0; n   int num; n   for (int i = 0; i != n; ++i) { n     scanf( %d , &num); n     if (num % 2 == 1) ++cnt; n   } n   if (cnt >= 1) n     printf( First n ); n   else n     printf( Second n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, maxx; n vector<int> v, cnt; n map<int, int> ma; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   maxx = min(2, n); n   v.resize(n + 1); n   cnt.resize(100001); n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n     map<int, int>::iterator it; n     it = ma.find(cnt[v[i]]); n     if (it != ma.end()) { n       it->second -= 1; n       if (it->second == 0) ma.erase(cnt[v[i]]); n     } n     cnt[v[i]] += 1; n     it = ma.find(cnt[v[i]]); n     if (it == ma.end()) n       ma.insert(pair<int, int>(cnt[v[i]], 1)); n     else n       it->second += 1; n     int chk = 1; n     if (ma.size() == 1) { n       chk = 0; n       pair<int, int> ff; n       ff = *ma.begin(); n       if (ff.first == 1) n         chk = 1; n       else if (ff.second == 1) n         chk = 1; n     } n     if (ma.size() > 2) chk = 0; n     if (ma.size() == 2) { n       chk = 0; n       pair<int, int> ff, ss; n       it = ma.begin(); n       ff = *it; n       it++; n       ss = *it; n       if ((ff.first == 1 && ff.second == 1) || n           (ss.first == ff.first + 1 && ss.second == 1)) n         chk = 1; n     } n     if (chk == 1) maxx = max(maxx, i); n   } n   cout << maxx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n mt19937 rnf(2106); n const int N = 200005; n int n, m; n int x[N], y[N]; n int d[N]; n queue<int> q[N]; n int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); } n int ans[N]; n bool stg(int s) { n   for (int i = 1; i <= m; ++i) { n     while (!q[d[i]].empty()) q[d[i]].pop(); n   } n   for (int i = 1; i <= m; ++i) q[d[i]].push(i); n   for (int i = 0; i < n; ++i) ans[i] = -1; n   for (int i = s; i < n; i += 2) { n     int dd = dist(x[i], y[i], x[(i - 1 + n) % n], y[(i - 1 + n) % n]) + n              dist(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]); n     if (q[dd].empty()) return false; n     ans[i] = q[dd].front(); n     q[dd].pop(); n   } n } n void solv() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d%d , &x[i], &y[i]); n   for (int i = 1; i <= m; ++i) { n     scanf( %d , &d[i]); n   } n   if (stg(0)) { n     printf( YES n ); n     for (int i = 0; i < n; ++i) printf( %d  , ans[i]); n     printf(  n ); n   } else if (stg(1)) { n     printf( YES n ); n     for (int i = 0; i < n; ++i) printf( %d  , ans[i]); n     printf(  n ); n   } else n     printf( NO n ); n } n int main() { n   solv(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long calWhite(long long x1, long long y1, long long x2, long long y2) { n   if (x1 == 0 || y1 == 0 || x2 == 0 || y2 == 0) { n     return 0; n   } n   long long tmp = (abs(x1 - x2) + 1) * (abs(y1 - y2) + 1); n   long long res = tmp / 2; n   if (tmp % 2 == 1 && (x1 * y1 % 2 == 1 || (x1 % 2 == 0 && y1 % 2 == 0))) { n     res++; n   } n   return res; n } n long long calBlack(long long x1, long long y1, long long x2, long long y2) { n   if (x1 == 0 || y1 == 0 || x2 == 0 || y2 == 0) { n     return 0; n   } n   return (x2 - x1 + 1) * (y2 - y1 + 1) - calWhite(x1, y1, x2, y2); n } n int main() { n   long long t; n   scanf( %I64d , &t); n   while (t--) { n     long long n, m; n     long long x1, x2, y1, y2; n     long long x3, x4, y3, y4; n     scanf( %I64d%I64d , &n, &m); n     scanf( %I64d%I64d%I64d%I64d , &x1, &y1, &x2, &y2); n     scanf( %I64d%I64d%I64d%I64d , &x3, &y3, &x4, &y4); n     long long answ = calWhite(1, 1, n, m); n     answ += calBlack(x1, y1, x2, y2); n     answ -= calWhite(x3, y3, x4, y4); n     long long x5, y5, x6, y6; n     if (x1 <= x3 && x3 <= x2) { n       x5 = x3; n       x6 = min(x2, x4); n     } else if (x1 <= x4 && x4 <= x2) { n       x5 = max(x1, x3); n       x6 = x4; n     } else if (x3 <= x1 && x2 <= x4) { n       x5 = x1; n       x6 = x2; n     } else { n       x5 = x6 = 0; n     } n     if (y1 <= y3 && y3 <= y2) { n       y5 = y3; n       y6 = min(y2, y4); n     } else if (y1 <= y4 && y4 <= y2) { n       y5 = max(y1, y3); n       y6 = y4; n     } else if (y3 <= y1 && y2 <= y4) { n       y5 = y1; n       y6 = y2; n     } else { n       y5 = y6 = 0; n     } n     answ -= calBlack(x5, y5, x6, y6); n     printf( %I64d %I64d n , answ, n * m - answ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1000000007; n const int N = 1010000; n int fail[N]; n int go[N][26], len[N], diff[N], anc[N], lst; n int n; n char str[N]; n int p; n int s[N]; n int f[N], g[N]; n void addChar(int c, int ww) { n   int x = lst; n   while (s[ww] != s[ww - len[x] - 1]) { n     x = fail[x]; n   } n   if (!go[x][c]) { n     len[p] = len[x] + 2; n     int k = fail[x]; n     while (s[ww] != s[ww - len[k] - 1]) k = fail[k]; n     fail[p] = go[k][c]; n     go[x][c] = p; n     diff[p] = len[p] - len[fail[p]]; n     if (diff[p] == diff[fail[p]]) n       anc[p] = anc[fail[p]]; n     else n       anc[p] = fail[p]; n     p++; n   } n   lst = go[x][c]; n } n void init() { n   p = 2; n   len[0] = 0; n   len[1] = -1; n   fail[0] = 1; n   fail[1] = 0; n   f[0] = 1; n   lst = 1; n } n int main() { n   scanf( %s , str + 1); n   n = strlen(str + 1); n   if (n & 1) { n     printf( 0 n ); n     return 0; n   } n   for (int i = (int)1; i <= (int)n / 2; i++) { n     s[i * 2 - 1] = str[i] -  a ; n     s[i * 2] = str[n + 1 - i] -  a ; n   } n   s[0] = -1; n   init(); n   for (int i = (int)1; i <= (int)n; i++) { n     addChar(s[i], i); n     for (int x = lst; x; x = anc[x]) { n       g[x] = f[i - (len[anc[x]] + diff[x])]; n       if (anc[x] != fail[x]) { n         g[x] = (g[x] + g[fail[x]]) % P; n       } n       if (i % 2 == 0) f[i] = (f[i] + g[x]) % P; n     } n   } n   printf( %d n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 250000; n int n, dfn[N], size[N], cnt, Q; n struct node { n   int tag, sum; n } t[N << 2]; n vector<int> V[N]; n void dfs(int x) { n   dfn[x] = ++cnt; n   size[x] = 1; n   for (int i = 0, s = V[x].size(); i < s; i++) n     dfs(V[x][i]), size[x] += size[V[x][i]]; n } n void pushdown(int i, int l, int r) { n   if (!t[i].tag) return; n   int mid = (l + r) >> 1, ls = i << 1, rs = ls | 1; n   t[ls].tag ^= 1; n   t[rs].tag ^= 1; n   t[ls].sum = (mid - l + 1 - t[ls].sum); n   t[rs].sum = (r - mid - t[rs].sum); n   t[i].tag = 0; n } n void update(int i, int l, int r, int x, int y) { n   if (l >= x && r <= y) { n     t[i].tag ^= 1; n     t[i].sum = (r - l + 1 - t[i].sum); n     return; n   } n   pushdown(i, l, r); n   int mid = (l + r) >> 1; n   if (mid >= x) update(i << 1, l, mid, x, y); n   if (mid < y) update(i << 1 | 1, mid + 1, r, x, y); n   t[i].sum = t[i << 1].sum + t[i << 1 | 1].sum; n } n int query(int i, int l, int r, int x, int y) { n   if (l >= x && r <= y) return t[i].sum; n   pushdown(i, l, r); n   int mid = (l + r) >> 1, ans = 0; n   if (mid >= x) ans += query(i << 1, l, mid, x, y); n   if (mid < y) ans += query(i << 1 | 1, mid + 1, r, x, y); n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     int x; n     scanf( %d , &x); n     V[x].push_back(i); n   } n   dfs(1); n   for (int i = 1; i <= n; i++) { n     char s = getchar(); n     while (s <  0  || s >  1 ) s = getchar(); n     if (s ==  1 ) update(1, 1, n, dfn[i], dfn[i]); n   } n   scanf( %d , &Q); n   while (Q--) { n     int x, opt; n     char s = getchar(); n     while (!isalpha(s)) s = getchar(); n     if (s ==  g ) n       opt = 2; n     else n       opt = 1; n     while (isalpha(s)) s = getchar(); n     scanf( %d , &x); n     if (opt == 1) n       update(1, 1, n, dfn[x], dfn[x] + size[x] - 1); n     else n       printf( %d n , query(1, 1, n, dfn[x], dfn[x] + size[x] - 1)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 7; n const long long INF = 1e9 + 7; n vector<long long> g[MAXN]; n bool bad[MAXN]; n long long best = -1; n long long besth = -1; n void jfs(long long u, long long parent, long long h) { n   if (besth < h) { n     best = u; n     besth = h; n   } n   for (long long v : g[u]) { n     if (v == parent) continue; n     if (bad[v]) continue; n     jfs(v, u, h + 1); n   } n } n pair<long long, long long> getd(long long u, long long parent, long long h) { n   pair<long long, long long> res = {h, u}; n   for (long long v : g[u]) { n     if (v == parent) continue; n     if (bad[v]) continue; n     pair<long long, long long> nr = getd(v, u, h + 1); n     res = max(res, nr); n   } n   return res; n } n bool go(long long u, long long parent, long long want) { n   if (u == want) { n     bad[u] = true; n     return true; n   } n   for (long long v : g[u]) { n     if (v == parent) continue; n     if (go(v, u, want)) { n       bad[u] = true; n       return true; n     } n   } n   return false; n } n long long dist[MAXN]; n void calc(long long u, long long parent, long long h) { n   dist[u] = h; n   for (long long v : g[u]) { n     if (v == parent) continue; n     if (bad[v]) continue; n     calc(v, u, h + 1); n   } n } n long long l[MAXN], r[MAXN], par[MAXN], pos[MAXN]; n vector<long long> e; n void build(long long u, long long parent) { n   par[u] = parent; n   pos[u] = e.size(); n   e.push_back(u); n   l[u] = e.size(); n   for (long long v : g[u]) { n     if (v == parent) continue; n     build(v, u); n   } n   r[u] = e.size() - 1; n } n pair<long long, long long> tree[MAXN * 4]; n long long add[MAXN * 4]; n void buildt(long long v, long long tl, long long tr) { n   if (tl == tr) { n     tree[v] = {dist[e[tl]], e[tl]}; n     return; n   } n   long long tm = (tl + tr) / 2; n   buildt(v * 2 + 1, tl, tm); n   buildt(v * 2 + 2, tm + 1, tr); n   tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]); n } n void push(long long v) { n   tree[v * 2 + 1].first += add[v]; n   add[v * 2 + 1] += add[v]; n   tree[v * 2 + 2].first += add[v]; n   add[v * 2 + 2] += add[v]; n   add[v] = 0; n } n void relax(long long v) { tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]); } n void upd(long long v, long long tl, long long tr, long long l, long long r, n          long long x) { n   if (tr < l || r < tl) return; n   if (l <= tl && tr <= r) { n     tree[v].first += x; n     add[v] += x; n     return; n   } n   long long tm = (tl + tr) / 2; n   push(v); n   upd(v * 2 + 1, tl, tm, l, r, x); n   upd(v * 2 + 2, tm + 1, tr, l, r, x); n   relax(v); n } n void del(long long v, long long tl, long long tr, long long p) { n   if (tl == tr) { n     tree[v].first = -INF; n     return; n   } n   long long tm = (tl + tr) / 2; n   push(v); n   if (p <= tm) { n     del(v * 2 + 1, tl, tm, p); n   } else { n     del(v * 2 + 2, tm + 1, tr, p); n   } n   relax(v); n } n void make_bad(long long u, long long n) { n   bad[u] = true; n   del(0, 0, n - 1, pos[u]); n   upd(0, 0, n - 1, l[u], r[u], -1); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   if (n == 1) { n     cout <<  1 n ; n     return 0; n   } n   for (long long i = 0; i < n - 1; ++i) { n     long long u, v; n     cin >> u >> v; n     g[u - 1].push_back(v - 1); n     g[v - 1].push_back(u - 1); n   } n   pair<long long, long long> t1 = getd(0, 0, 0); n   pair<long long, long long> t2 = getd(t1.second, t1.second, 0); n   go(t1.second, t1.second, t2.second); n   long long curr = t2.first + 1; n   cout <<  1   << curr <<    ; n   for (long long i = 0; i < n; ++i) dist[i] = -INF; n   for (long long i = 0; i < n; ++i) { n     if (!bad[i]) continue; n     for (long long j : g[i]) { n       if (bad[j]) continue; n       calc(j, j, 0); n     } n   } n   long long root = t1.second; n   build(root, root); n   buildt(0, 0, n - 1); n   for (long long it = 3; it <= n; ++it) { n     if (curr == n) { n       for (long long i = 0; i < n - it + 1; ++i) cout << n <<    ; n       break; n     } n     pair<long long, long long> t = tree[0]; n     curr += t.first + 1; n     cout << curr <<    ; n     long long u = t.second; n     while (!bad[u]) { n       make_bad(u, n); n       u = par[u]; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, ans = 0, a, b = 0, N; n   cin >> N; n   for (long long i = 1; i <= 1e6; i++) { n     n = N; n     a = i * 2; n     n -= a; n     b = ((i - 1) * (i * 3) / 2); n     n -= b; n     if (n < 0) break; n     if (n % 3 == 0) ans++; n   } n   cout << ans; n   return 0; n } n


/* 2013-08-11 10:03:33.914034 */#include<iostream> n #include<cstdio> n #include<cstdlib> n #include<ctime> n #include<cmath> n #include<cctype> n #include<cstring> n #include<algorithm> n  n #include<map> n #include<set> n #include<vector> n #include<string> n #include<queue> n  n using namespace std; n  n char s[1011][1011]; n bool u[1011][1011]; n char c[1011][1011]; n int n,m; n const int dx[]={1,0,-1,0}; n const int dy[]={0,1,0,-1}; n inline bool fitr(int x,int y){ n  return s[x][y+1]== b &&s[x][y+2]== w &&!u[x][y+1]&&!u[x][y+2]; n } n inline bool fitc(int x,int y){ n  return s[x+1][y]== b &&s[x+2][y]== w &&!u[x+1][y]&&!u[x+2][y]; n } n char choosec(int a,int b,int ax,int ay){ n  int opt=0; n  for(int i=0;i<3;++i) n  { n   for(int k=0;k<4;++k) n   { n    int nx=a+dx[k],ny=b+dy[k]; n    if(nx>0&&nx<=n&&ny>0&&ny<=m) n    { n     if(c[nx][ny]!= . ) opt|=1<<(c[nx][ny]- a ); n    } n   } n   a+=ax,b+=ay; n  } n  for(int i=0;;++i) n  { n   if(((1<<i)&opt)==0) return i+ a ; n  } n } n int main(){ n  scanf( %d%d ,&n,&m); n  for(int i=1;i<=n;++i) n  { n   scanf( %s ,s[i]+1); n   for(int j=1;j<=m;++j) c[i][j]= . ; n  } n  for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(!u[i][j]&&s[i][j]!= . ) n  { n   if(s[i][j]== b ) n   { n    printf( NO n ); n    return 0; n   } n   else n   { n    if(fitr(i,j)) n    { n     u[i][j]=u[i][j+1]=u[i][j+2]=1; n     char co=choosec(i,j,0,1); n     c[i][j]=c[i][j+1]=c[i][j+2]=co; n    } n    else n    { n     if(fitc(i,j)) n     { n      u[i][j]=u[i+1][j]=u[i+2][j]=1; n      char co=choosec(i,j,1,0); n      c[i][j]=c[i+1][j]=c[i+2][j]=co; n     } n     else n     { n      printf( NO n ); n      return 0; n     } n    } n   } n  } n  printf( YES n ); n  for(int i=1;i<=n;++i) printf( %s n ,c[i]+1); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a; n   if (a == 1) { n     cout <<  1 ; n     return 0; n   } n   if (a == 2) { n     cout <<  13 ; n     return 0; n   } n   long long sol = 1; n   sol += 1LL * 6 * a * (a + 1) / 2; n   sol += 1LL * 6 * ((a - 2) * (a - 1) / 2 - 1); n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, x[3], y[3], xx, yy, jump[111111]; n pair<long long, long long> dist[111111]; n long long maxR2[111111], ans = LONG_LONG_MAX; n int main() { n   scanf( %d %d %d %d %d , &N, &x[1], &y[1], &x[2], &y[2]); n   for (int i = 1; i <= N; i++) { n     scanf( %d %d , &xx, &yy); n     dist[i].first = n         1LL * (xx - x[1]) * (xx - x[1]) + 1LL * (yy - y[1]) * (yy - y[1]); n     dist[i].second = n         1LL * (xx - x[2]) * (xx - x[2]) + 1LL * (yy - y[2]) * (yy - y[2]); n   } n   sort(dist + 1, dist + N + 1); n   for (int i = N; i >= 0; i--) { n     maxR2[i] = max(maxR2[i + 1], dist[i].second); n     jump[i] = (dist[i].first == dist[i + 1].first) ? jump[i + 1] : (i + 1); n   } n   for (int i = 0; i <= N; i++) ans = min(ans, dist[i].first + maxR2[jump[i]]); n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n string run(vector<string> &vec) { n   vector<bool> used(vec.size(), false); n   vector<char> ans(100,  1 ); n   for (int i = 0; i < vec[0].size(); ++i) { n     ans[50 + i] = vec[0][i]; n   } n   used[0] = true; n   while (true) { n     int count = 0; n     for (int i = 1; i < vec.size(); ++i) { n       if (not used[i]) { n         bool thr = false; n         for (int p = 0; p < vec[i].size() && !thr; ++p) { n           for (int k = 0; k < ans.size() && !thr; ++k) { n             if (ans[k] == vec[i][p]) { n               int in = k - p; n               for (int np = 0; np < vec[i].size(); ++np) { n                 ans[in + np] = vec[i][np]; n               } n               used[i] = true; n               count += 1; n               thr = true; n             } n           } n         } n       } n     } n     if (count == 0) break; n   } n   int sz = 0; n   for (int i = 0; i < vec.size(); ++i) { n     if (not used[i]) { n       vec[sz] = vec[i]; n       ++sz; n     } n   } n   vec.resize(sz); n   string ret; n   for (int i = 0; i < ans.size(); ++i) { n     if (ans[i] !=  1 ) { n       ret += ans[i]; n     } n   } n   return ret; n } n int main() { n   int n; n   cin >> n; n   vector<string> vs(n); n   for (int i = 0; i < n; ++i) { n     cin >> vs[i]; n   } n   string ans; n   while (not vs.empty()) { n     string c1 = run(vs); n     ans += c1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 998244353; n const long long int MAXN = 3e3 + 5; n const long long int inf = 1e17; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     vector<char> v(n); n     for (int i = 0; i < n; i++) cin >> v[i]; n     long long int a = v.back() -  0 ; n     while (a % 2 == 0) { n       v.pop_back(); n       if (v.size() > 0) n         a = v.back() -  0 ; n       else n         break; n     } n     if (v.size() < 2) { n       cout <<  -1  << endl; n       continue; n     } n     long long int sum = 0; n     for (int i = 0; i < v.size(); i++) { n       a = v[i] -  0 ; n       sum += a; n     } n     if (sum % 2 == 0) { n       for (auto i : v) cout << i; n       cout << endl; n       continue; n     } n     long long int ind = -1; n     for (int i = v.size() - 2; i > -1; i--) { n       a = v[i] -  0 ; n       if (a % 2) { n         ind = i; n         break; n       } n     } n     if (ind == 0 && v[1] ==  0 ) ind = -1; n     if (ind != 0 && v[0] ==  0 ) ind = -1; n     if (ind == -1) n       cout <<  -1  << endl; n     else { n       for (int i = 0; i < v.size(); i++) { n         if (i != ind) cout << v[i]; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 4.0 * atan(1.0); n int64_t num; n queue<int64_t> qu; n bool ifgood(int64_t x) { n   int64_t a, b; n   a = 0; n   b = 0; n   while (x) { n     if (x % 10 == 4) n       ++a; n     else if (x % 10 == 7) n       ++b; n     else n       return false; n     x /= 10; n   } n   if (a == b) return true; n   return false; n } n void bfs() { n   while (!qu.empty()) qu.pop(); n   int64_t a, b, k; n   a = 0; n   qu.push(a); n   while (!qu.empty()) { n     b = qu.front(); n     if (b > num && ifgood(b)) return; n     qu.pop(); n     k = b; n     b = b * 10 + 4; n     qu.push(b); n     k = k * 10 + 7; n     qu.push(k); n   } n   return; n } n int main() { n   cin >> num; n   if (ifgood(num)) n     cout << num << endl; n   else { n     bfs(); n     cout << qu.front() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   long long A, B, n; n   cin >> A >> B >> n; n   while (n--) { n     long long l, t, m; n     cin >> l >> t >> m; n     if (t < A + (l - 1) * B) { n       cout << -1 << endl; n       continue; n     } n     long long low = l, high = 1e7 * 1ll; n     while (low < high) { n       long long mi = (low + high + 1) / 2; n       long long res = (mi - l + 1) * A; n       res += (((mi - 1) * mi) / 2 - max(0ll, ((l - 2) * (l - 1)) / 2)) * B; n       if (res <= t * m && A + (mi - 1) * B <= t) { n         low = mi; n       } else { n         high = mi - 1; n       } n     } n     cout << low << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n struct f { n   int pos; n   int num; n } a[108]; n int cmp(const void *a, const void *b) { n   return (*(struct f *)a).num - (*(struct f *)b).num; n } n int main() { n   int n, k; n   while (scanf( %d%d , &n, &k) == 2) { n     int i; n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i].num); n       a[i].pos = i + 1; n     } n     qsort(a, n, sizeof(a[0]), cmp); n     int sum = 0; n     int count = 0; n     for (i = 0; i < n; i++) { n       if (i == 0 && a[i].num > k) break; n       sum += a[i].num; n       if (sum > k) break; n       count++; n     } n     printf( %d n , count); n     for (i = 0; i < count; i++) { n       if (i != 0) n         printf(  %d , a[i].pos); n       else n         printf( %d , a[i].pos); n     } n     if (count) printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, max = -100000; n   int a[200], inter[200], exter[200]; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) { n       int sum = 0; n       vector<int> inter, exter; n       inter.clear(); n       exter.clear(); n       for (int m = i; m < j + 1; m++) { n         sum += a[m]; n         inter.push_back(a[m]); n       } n       for (int m = 0; m < i; m++) exter.push_back(a[m]); n       for (int m = j + 1; m < n; m++) exter.push_back(a[m]); n       sort(inter.begin(), inter.end()); n       sort(exter.begin(), exter.end()); n       for (int m = 0; m < k; m++) { n         if (m >= inter.size() || exter.size() < m + 1) break; n         if (exter[exter.size() - 1 - m] > inter[m]) n           sum += exter[exter.size() - 1 - m] - inter[m]; n       } n       max = max > sum ? max : sum; n     } n   } n   cout << max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct _d { n   int r1, c1, r2, c2; n }; n int n, m; n char arr[500][501]; n bool ans[600000]; n _d queries[600000]; n bitset<500> DPU[500][500], DPD[500][500]; n void dnc(int l, int r, vector<int> &qid) { n   if (l > r) return; n   int mid = (l + r) / 2; n   vector<int> ql, qr; n   for (int i = 0; i < qid.size(); i++) { n     if (queries[qid[i]].r2 < mid) { n       ql.push_back(qid[i]); n       qid[i] = qid.back(), qid.pop_back(), --i; n     } else if (queries[qid[i]].r1 > mid) { n       qr.push_back(qid[i]); n       qid[i] = qid.back(), qid.pop_back(), --i; n     } n   } n   if (!qid.empty()) { n     for (int i = m - 1; i >= 0; i--) { n       DPU[mid][i] = i != m - 1 && arr[mid][i + 1] ==  .  ? DPU[mid][i + 1] n                                                          : bitset<500>(); n       DPU[mid][i][i] = 1; n     } n     for (int i = 0; i < m; i++) { n       DPD[mid][i] = n           i && arr[mid][i - 1] ==  .  ? DPD[mid][i - 1] : bitset<500>(); n       DPD[mid][i][i] = 1; n     } n     for (int i = mid - 1; i >= l; i--) { n       for (int j = m - 1; j >= 0; j--) { n         DPU[i][j] = arr[i + 1][j] ==  .  ? DPU[i + 1][j] : bitset<500>(); n         if (j != m - 1 && arr[i][j + 1] ==  . ) DPU[i][j] |= DPU[i][j + 1]; n       } n     } n     for (int i = mid + 1; i <= r; i++) { n       for (int j = 0; j < m; j++) { n         DPD[i][j] = arr[i - 1][j] ==  .  ? DPD[i - 1][j] : bitset<500>(); n         if (j && arr[i][j - 1] ==  . ) DPD[i][j] |= DPD[i][j - 1]; n       } n     } n     for (int i : qid) { n       auto cq = queries[i]; n       ans[i] = (DPU[cq.r1][cq.c1] & DPD[cq.r2][cq.c2]).any(); n     } n   } n   if (!ql.empty()) dnc(l, mid - 1, ql); n   if (!qr.empty()) dnc(mid + 1, r, qr); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , arr[i]); n   int q; n   scanf( %d , &q); n   vector<int> qid(q); n   for (int i = 0; i < q; i++) { n     int r1, c1, r2, c2; n     scanf( %d%d%d%d , &r1, &c1, &r2, &c2); n     queries[i] = {r1 - 1, c1 - 1, r2 - 1, c2 - 1}; n     qid[i] = i; n   } n   dnc(0, n - 1, qid); n   for (int i = 0; i < q; i++) printf(ans[i] ?  Yes n  :  No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int N = 1e5 + 7; n int pre[N], sz[N], cnt[N], n, lencnt, dx, one; n pair<int, int> a[N]; n void init() { memset(pre, -1, sizeof(pre)); } n int fd(int n) { return pre[n] == -1 ? n : pre[n] = fd(pre[n]); } n void mg(int a, int b) { n   a = fd(a), b = fd(b); n   if (a != b) { n     pre[b] = a; n     sz[a] += sz[b]; n     sz[b] = 0; n   } n } n void solve(int x) { n   int v = x + 1; n   if (v <= n && sz[fd(v)]) { n     if (--cnt[sz[fd(v)]] == 0) --lencnt; n     if (--cnt[sz[x]] == 0) --lencnt; n     mg(x, v); n     if (++cnt[sz[fd(v)]] == 1) ++lencnt; n   } n   v = x - 1; n   if (v >= 1 && sz[fd(v)]) { n     if (--cnt[sz[fd(v)]] == 0) --lencnt; n     if (--cnt[sz[fd(x)]] == 0) --lencnt; n     mg(x, v); n     if (++cnt[sz[fd(v)]] == 1) ++lencnt; n   } n   one = cnt[sz[fd(x)]]; n } n int main(void) { n   int i; n   scanf( %d , &n); n   init(); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + 1 + n); n   int ans = 1; n   for (i = 1; i <= n; ++i) { n     sz[a[i].second] = 1; n     if (++cnt[1] == 1) ++lencnt; n     solve(a[i].second); n     if (lencnt == 1 && one > dx) { n       ans = a[i].first + 1; n       dx = one; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int G = 3; n const int K = 40001; n const int inv2 = 499122177; n const int MaxN = 6e5 + 10; n const int mod = 998244353; n int n, k, lim, w[MaxN], rev[MaxN], _inv[MaxN]; n void Mod(int &x) { x += x >> 31 & mod; } n void __init() { n   _inv[1] = 1; n   for (int i = 2; i < MaxN; i++) n     _inv[i] = (unsigned long long)(mod - mod / i) * _inv[mod % i] % mod; n } n int fast_pow(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = (unsigned long long)ret * a % mod; n     a = (unsigned long long)a * a % mod, b >>= 1; n   } n   return ret; n } n void init(int n) { n   int l = 32 - __builtin_clz(n - 1); n   lim = (1 << l); n   for (int i = 0, ri = 0; i < lim; i++) { n     rev[i] = ri; n     for (int k = lim >> 1; (ri ^= k) < k;) k >>= 1; n   } n   int wn = fast_pow(G, (mod - 1) >> l); n   w[lim >> 1] = 1; n   for (int i = (lim >> 1) + 1; i < lim; i++) n     w[i] = (unsigned long long)w[i - 1] * wn % mod; n   for (int i = (lim >> 1) - 1; i; --i) w[i] = w[i << 1]; n   lim = l; n } n struct poly { n   std::vector<int> v; n   inline poly() {} n   inline poly(int n) : v(n) {} n   inline poly(const poly &r) : v(r.v) {} n   inline poly(const std::vector<int> &r) : v(r) {} n   inline size_t size() { return v.size(); } n   inline bool empty() { return v.empty(); } n   inline void resize(int n) { v.resize(n); } n   inline void clear() { v.clear(); } n   inline void shl() { v.insert(v.begin(), 0); } n   void shrink() { n     while (!v.empty() && !v.back()) v.pop_back(); n   } n   inline int &operator[](int i) { return v[i]; } n   inline static int len(int n) { return 1 << (32 - __builtin_clz(n - 1)); } n   void deriv() { n     for (int i = 1; i < v.size(); i++) n       v[i - 1] = (unsigned long long)i * v[i] % mod; n     v.pop_back(); n   } n   void intg() { n     shl(); n     for (int i = 1; i < v.size(); i++) n       v[i] = (unsigned long long)v[i] * _inv[i] % mod; n   } n   void dft(int n) { n     static unsigned long long tmp[MaxN]; n     int ofs = lim - __builtin_ctz(n); n     resize(n); n     if (n <= 1) return; n     for (int i = 0; i < n; i++) tmp[rev[i] >> ofs] = v[i]; n     for (int i = 0; i < n; i += 2) { n       int x = tmp[i], y = tmp[i + 1]; n       tmp[i] = x + y, tmp[i + 1] = x + mod - y; n     } n     for (int i = 2; i < n; i <<= 1) n       for (int j = 0; j < n; j += (i << 1)) n         for (int k = 0; k < i; ++k) { n           int y = (unsigned long long)tmp[i + j + k] * w[i + k] % mod; n           tmp[i + j + k] = tmp[j + k] + mod - y, tmp[j + k] += y; n         } n     for (int i = 0; i < n; i++) v[i] = tmp[i] % mod; n   } n   void idft(int n) { n     dft(n); n     if (n <= 1) return; n     std::reverse(v.begin() + 1, v.end()); n     int tmp = mod - (mod - 1) / n; n     for (int i = 0; i < n; i++) v[i] = (unsigned long long)v[i] * tmp % mod; n   } n   void mul(int k) { n     int n = size(); n     for (int i = 0; i < n; i++) n       v[i] = (unsigned long long)(v[i] * k % mod + mod) % mod; n   } n   void add(poly r) { n     int n = std::max(size(), r.size()); n     resize(n); n     for (int i = 0; i < n; i++) v[i] = (unsigned long long)(v[i] + r[i]) % mod; n     shrink(); n   } n   void mul(poly r) { n     int n = len(size() + r.size() - 1); n     dft(n), r.dft(n); n     for (int i = 0; i < n; i++) v[i] = (unsigned long long)v[i] * r[i] % mod; n     idft(n), shrink(); n   } n   void inv() { n     std::vector<int> va(1); n     int n = size(), m = len(n); n     va[0] = fast_pow(v[0], mod - 2); n     poly a; n     v.resize(m), v.swap(va); n     for (int i = 1; i <= m; i <<= 1) { n       a.resize(i); n       for (int j = 0; j < i; j++) a[j] = va[j]; n       a.dft(i << 1), dft(i << 1); n       for (int j = 0; j < (i << 1); j++) n         v[j] = v[j] * (2 + mod - (unsigned long long)v[j] * a[j] % mod) % mod; n       idft(i << 1), resize(i); n     } n     resize(n); n   } n   void ln() { n     poly f0 = *this; n     int n = size(); n     deriv(), f0.inv(), mul(f0); n     resize(n), intg(), resize(n); n   } n   void exp() { n     std::vector<int> va(1); n     va[0] = 1; n     poly a; n     int n = size(), m = len(n); n     v.resize(m), v.swap(va); n     for (int i = 1; i <= m; i *= 2) { n       a = *this, a.resize(i), a.ln(); n       for (int j = 0; j < i; j++) a[j] = (va[j] + !j + mod - a[j]) % mod; n       mul(a), resize(i); n     } n     resize(n); n   } n   void pw(int k) { n     ln(); n     int n = size(); n     for (int i = 0; i < n; i++) v[i] = (unsigned long long)v[i] * k % mod; n     exp(); n   } n   void sqrt() { n     std::vector<int> va(1); n     int n = size(), m = len(n); n     poly a, b; n     va[0] = ::sqrt(v[0]); n     v.resize(m), v.swap(va); n     for (int i = 1; i <= m; i <<= 1) { n       a.resize(i), b.resize(i); n       for (int j = 0; j < i; j++) a[j] = va[j], b[j] = v[j]; n       b.inv(), a.mul(b); n       for (int j = 0; j < i; j++) n         v[j] = (unsigned long long)(v[j] + a[j]) % mod * inv2 % mod; n       resize(i << 1); n     } n     resize(n); n   } n } a, b, c, f, g, h; n void extend() { n   f = g, g = h, h[0] = g[0]; n   f.resize(K), g.resize(K), h.resize(K); n   for (int i = 1; i < K; i++) n     h[i] = (((f[i - 1]) + ((((g[i - 1]) + (g[i])) % mod))) % mod); n } n void Double() { n   int len = poly::len(2 * K), A, B, C, w = 1, wn = fast_pow(3, (mod - 1) / len); n   a = f, a.dft(len), b = g, b.dft(len), c = h, c.dft(len); n   for (int i = 0; i < len; i++, w = (unsigned long long)w * wn % mod) { n     A = a[i], B = b[i], C = c[i]; n     a[i] = n         (unsigned long long)(1ll * B * B % mod + 1ll * A * A % mod * w % mod) % n         mod; n     b[i] = n         (unsigned long long)(1ll * B * C % mod + 1ll * A * B % mod * w % mod) % n         mod; n     c[i] = n         (unsigned long long)(1ll * C * C % mod + 1ll * B * B % mod * w % mod) % n         mod; n   } n   a.idft(len), f = a, b.idft(len), g = b, c.idft(len), h = c; n   f.resize(K), g.resize(K), h.resize(K); n } n inline int read() { n   unsigned long long x = 0; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) ch = getchar(); n   while (ch <=  9  && ch >=  0 ) x = ((x * 10) + (ch ^ 48)), ch = getchar(); n   return x; n } n signed main() { n   __init(), init(300000), n = read(), k = read(), f.resize(K); n   g.resize(K), h.resize(K), g[0] = 1, h[0] = 1, h[1] = 1; n   int lg = log2(n); n   for (int i = lg - 1; ~i; i--) { n     Double(); n     if (n & (1 << i)) extend(); n   } n   for (int i = 1; i <= k; i++) printf( %d  , h[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   int i, count = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i < n - 1; i++) { n     if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) { n       count++; n       i += 2; n     } n   } n   printf( %d , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 11; n const long long oo = 1e17 + 123; n int tcases = 1; n int n, m = 0; n long long a[N], b[N], s[N], res[N]; n pair<int, int> q[N]; n struct Point { n   double x, y; n   Point() { x = y = 0; } n   Point(double _x, double _y) : x(_x), y(y) {} n }; n struct line { n   double a, b; n   line() { n     a = 1; n     b = 0; n   } n   line(double _a, double _b) { n     a = _a; n     b = _b; n   } n   long long val(double x) { return (long long)a >= oo ? oo : x * a + b; } n   double getIntersection(line ll) { return (ll.b - b) / (a - ll.a); } n } l0[N]; n struct LiChaoTree { n   vector<int> coor; n   vector<int> queries; n   vector<line> f; n   int m; n   void addline(int k, int l, int r, line ll) { n     if (l == r) { n       if (f[k].val(coor[l - 1]) > ll.val(coor[l - 1])) f[k] = ll; n       return; n     } n     int mid = (l + r) >> 1; n     bool sl = ll.val(coor[l - 1]) < f[k].val(coor[l - 1]), n          smid = ll.val(coor[mid] - 1) < f[k].val(coor[mid] - 1); n     if (smid) swap(f[k], ll); n     if (sl == smid) { n       addline(k << 1 | 1, mid + 1, r, ll); n     } else { n       addline(k << 1, l, mid, ll); n     } n   } n   long long cal(int k, int l, int r, long long x) { n     if (coor[l - 1] > x || coor[r - 1] < x) return oo; n     if (l == r) return f[k].val(x); n     long long rr = f[k].val(x); n     int mid = (l + r) >> 1; n     return min(rr, min(cal(k << 1, l, mid, x), cal(k << 1 | 1, mid + 1, r, x))); n   } n   void init(int l, int r) { n     coor.push_back(-n); n     coor.push_back(0); n     for (int i : queries) { n       coor.push_back(q[i].first - q[i].second - 1); n     } n     sort((coor).begin(), (coor).end()); n     coor.erase(unique((coor).begin(), (coor).end()), coor.end()); n     m = coor.size() - 1; n     f.assign(m * 4 + 11, line(oo, oo)); n     for (int i = l; i <= r; ++i) { n       addline(1, 1, m, l0[i]); n     } n   } n   void getMin(int l, int r) { n     init(l, r); n     for (int i : queries) { n       int x = q[i].first, y = q[i].second; n       res[i] = min(res[i], cal(1, 1, m, x - y - 1) + s[y]); n     } n   } n }; n struct IT { n   LiChaoTree f[N * 4]; n   void insert(int k, int l, int r, int L, int R, int i) { n     if (L > r || l > R) return; n     if (L <= l && r <= R) { n       f[k].queries.push_back(i); n       return; n     } n     int mid = (l + r) >> 1; n     insert(k << 1, l, mid, L, R, i); n     insert(k << 1 | 1, mid + 1, r, L, R, i); n   } n   void getMin(int k, int l, int r) { n     f[k].getMin(l, r); n     if (l < r) { n       int mid = (l + r) >> 1; n       getMin(k << 1, l, mid); n       getMin(k << 1 | 1, mid + 1, r); n     } n   } n } segmentTree; n void testcase() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     s[i] = s[i - 1] + a[i]; n     l0[i] = line(a[i], a[i] * i - s[i - 1]); n   } n   cin >> m; n   for (int i = 1; i <= m; ++i) { n     int x, y; n     cin >> x >> y; n     segmentTree.insert(1, 1, n, y - x + 1, y, i); n     q[i] = make_pair(x, y); n   } n   for (int i = 1; i <= m; ++i) res[i] = oo; n   segmentTree.getMin(1, 1, n); n   for (int i = 1; i <= m; ++i) cout << res[i] <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   for (int i = 1; i <= tcases; ++i) testcase(); n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 34; n int n, m; n int f[MAXN][MAXN], s[MAXN][MAXN]; n int work2(int j) { n   s[m][m + 1 + j] = s[m][j] * s[m][m]; n   int tot = s[m][j] * f[m][j] + s[m][m + 1 + j] * f[m][m + 1 + j]; n   for (int i = 0; i < m; ++i) { n     int x = f[i][j] + s[i][m] * f[i][m + j + 1] + s[m][j] * f[m + i + 1][j] + n             s[i][m] * s[m][m + j + 1] * f[m + i + 1][m + j + 1]; n     tot += abs(x); n   } n   return tot; n } n int work1() { n   int tot = 0; n   for (int i = 0, x1, x2; i < m; ++i) { n     s[m][i] = 1, x1 = work2(i); n     s[m][i] = -1, x2 = work2(i); n     tot += max(x1, x2); n   } n   return tot; n } n int main() { n   scanf( %d , &n), m = n >> 1; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) scanf( %d , f[i] + j); n   int mx = 0; n   for (int mask = 0, lim = 1 << (m + 1); mask < lim; ++mask) { n     for (int i = 0; i <= m; ++i) s[i][m] = (mask & 1 << i) ? -1 : 1; n     for (int i = m + 1; i < n; ++i) s[i][m] = s[m][m] * s[i - m - 1][m]; n     int cc = 0; n     for (int i = 0; i < n; ++i) cc += s[i][m] * f[i][m]; n     mx = max(mx, work1() + cc); n   } n   printf( %d n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t) { n     t--; n     long long a, b; n     cin >> a >> b; n     long long n = a / b + (bool)(a % b); n     cout << b * n - a << endl; n   } n   return 0; n } n