#include <bits/stdc++.h> n const int mod = 200003; n const int gmod = 2; n const int inf = 1039074182; n const long long llinf = 1LL * inf * inf; n template <typename T1, typename T2> n inline void chmin(T1 &x, T2 b) { n   if (b < x) x = b; n } n template <typename T1, typename T2> n inline void chmax(T1 &x, T2 b) { n   if (b > x) x = b; n } n template <typename T1, typename T2> n inline void chadd(T1 &x, T2 b) { n   x += b; n   if (x >= mod) x -= mod; n } n template <typename T1, typename T2> n inline void chmul(T1 &x, T2 b) { n   x = 1LL * x * b % mod; n } n template <typename T1, typename T2> n inline void chmod(T1 &x, T2 b) { n   x %= b, x += b; n   if (x >= b) x -= b; n } n using namespace std; n template <typename T> n ostream &operator<<(ostream &cout, vector<T> vec) { n   cout <<  { ; n   for (int i = 0; i < vec.size(); i++) { n     cout << vec[i]; n     if (i != (int)vec.size() - 1) cout <<  , ; n   } n   cout <<  } ; n   return cout; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &cout, pair<T1, T2> p) { n   cout <<  (  << p.first <<  ,  << p.second <<  ) ; n   return cout; n } n template <typename T> n ostream &operator<<(ostream &cout, set<T> s) { n   vector<T> t; n   for (auto x : s) t.push_back(x); n   cout << t; n   return cout; n } n template <typename T> n ostream &operator<<(ostream &cout, queue<T> q) { n   vector<T> t; n   while (q.size()) { n     t.push_back(q.front()); n     q.pop(); n   } n   cout << t; n   return cout; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &cout, map<T1, T2> m) { n   for (auto &x : m) { n     cout <<  Key:   << x.first <<     <<  Value:   << x.second << endl; n   } n   return cout; n } n template <typename T> n T operator*(vector<T> v1, vector<T> v2) { n   assert(v1.size() == v2.size()); n   int n = v1.size(); n   T res = 0; n   for (int i = 0; i < n; i++) { n     res += v1[i] * v2[i]; n   } n   return res; n } n template <typename T1, typename T2> n pair<T1, T2> operator+(pair<T1, T2> x, pair<T1, T2> y) { n   return make_pair(x.first + y.first, x.second + y.second); n } n template <typename T1, typename T2> n pair<T1, T2> operator-(pair<T1, T2> x) { n   return make_pair(-x.first, -x.second); n } n template <typename T> n vector<vector<T> > operator~(vector<vector<T> > vec) { n   vector<vector<T> > v; n   int n = vec.size(), m = vec[0].size(); n   v.resize(m); n   for (int i = 0; i < m; i++) { n     v[i].resize(n); n   } n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       v[i][j] = vec[j][i]; n     } n   } n   return v; n } n void print0x(int x) { n   std::vector<int> vec; n   while (x) { n     vec.push_back(x & 1); n     x >>= 1; n   } n   std::reverse(vec.begin(), vec.end()); n   for (int i = 0; i < vec.size(); i++) { n     std::cout << vec[i]; n   } n   std::cout <<    ; n } n template <typename T> n void print0x(T x, int len) { n   std::vector<int> vec; n   while (x) { n     vec.push_back(x & 1); n     x >>= 1; n   } n   reverse(vec.begin(), vec.end()); n   for (int i = vec.size(); i < len; i++) { n     putchar( 0 ); n   } n   for (int i = 0; i < vec.size(); i++) { n     std::cout << vec[i]; n   } n   std::cout <<    ; n } n using namespace std; n const int basic = 500000; n int n; n int a[300005], b[300005]; n char buf[500005]; n vector<int> sum[basic * 2 + 5]; n int m1, m2, m3, m4; n int b1, b2; n int besta, bestb; n int best; n inline void calc(int k) { n   int res = 0; n   chmax(res, b1); n   chmax(res, b2); n   int zx = max(m2, m3); n   int nx = max(m1, m4); n   int tx = (nx - zx) / 2; n   chmax(tx, 0); n   chmax(res, -tx + nx); n   if (res < best) { n     best = res; n     besta = tx; n     if (besta == 0 && k == 0) besta++; n     bestb = besta + k; n   } n } n inline void read(int &a, int &b) { n   a = 0; n   b = 0; n   char c = 0; n   while (c !=  N  && c !=  B ) c = getchar(); n   while (c ==  N  || c ==  B ) { n     if (c ==  B ) n       b++; n     else n       a++; n     c = getchar(); n   } n } n int main() { n   cin >> n; n   m1 = -inf, m2 = -inf, m3 = -inf, m4 = -inf; n   b1 = -inf, b2 = -inf; n   int k = basic; n   for (int i = 0; i < n; i++) { n     read(a[i], b[i]); n     sum[b[i] - a[i] + basic].push_back(i); n     chmax(b2, a[i] + k - b[i]); n     chmax(m3, k - b[i]); n     chmax(m4, a[i]); n   } n   best = inf; n   calc(basic); n   for (k = basic - 1; k >= -basic; k--) { n     m1++; n     b1++; n     m3--; n     b2--; n     for (auto id : sum[k + basic]) { n       int x0 = a[id]; n       int y0 = b[id]; n       chmax(m1, y0 - k); n       chmax(m2, -x0); n       chmax(b1, y0 - (x0 + k)); n     } n     calc(k); n   } n   cout << best << endl; n   for (int i = 0; i < bestb; i++) { n     putchar( B ); n   } n   for (int i = 0; i < besta; i++) { n     putchar( N ); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n set<long long> ns; n long long p2[100]; n int main() { n   int n, i, j; n   int cnt = 0; n   long long t = 1; n   while (t < 2000000000) { n     cnt++; n     p2[cnt] = t; n     t *= 2; n   } n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     ns.insert(a[i]); n   } n   sort(a + 1, a + 1 + n); n   for (i = 2; i < n; i++) { n     for (j = 1; j <= cnt; j++) { n       long long t1 = a[i] - p2[j], t2 = a[i] + p2[j]; n       if (ns.find(t1) != ns.end() && ns.find(t2) != ns.end()) { n         puts( 3 ); n         cout << t1 <<     << a[i] <<     << t2 << endl; n         return 0; n       } n     } n   } n   for (i = 2; i <= n; i++) { n     for (j = 1; j <= cnt; j++) { n       long long t1 = a[i] - p2[j]; n       if (ns.find(t1) != ns.end()) { n         puts( 2 ); n         cout << t1 <<     << a[i] << endl; n         return 0; n       } n     } n   } n   puts( 1 ); n   cout << a[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n constexpr int N = 1e2 + 5; n int n, m, k, g[N][N], ans = -1, id; n unordered_set<int> dp[N][N]; n char c; n bool check(int i, int j, int val) { n   for (auto el : dp[i][j]) { n     if (ans == el + val) { n       ans -= val; n       return true; n     } n   } n   return false; n } n void Solve() { n   cin >> n >> m >> k; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) { n       dp[i][j].reserve(1024); n       dp[i][j].max_load_factor(0.25); n     } n   for (int i = 0; i < N; i++) dp[0][i] = {0}; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> c; n       g[i][j] = c -  0 ; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       for (auto el : dp[i - 1][j - 1]) dp[i][j].insert(el + g[i][j]); n       for (auto el : dp[i - 1][j + 1]) dp[i][j].insert(el + g[i][j]); n     } n   for (int i = 1; i <= m; i++) n     for (auto el : dp[n][i]) { n       if (!(el % (k + 1))) { n         if (ans < el) { n           ans = el; n           id = i; n         } n       } n     } n   cout << ans <<   n ; n   if (ans == -1) return; n   cout << id <<   n ; n   for (int i = n; i > 1; i--) { n     if (check(i - 1, id - 1, g[i][id])) { n       id = id - 1; n       cout <<  L ; n     } else if (check(i - 1, id + 1, g[i][id])) { n       id = id + 1; n       cout <<  R ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int SG(double x) { return x > -1E-8 ? x < 1E-8 ? 0 : 1 : -1; } n int c[1000000]; n int main() { n   int n, m, r, i, j, k, d; n   long long md; n   scanf( %d %d %d , &n, &m, &r); n   for (i = 0; i < (n); i++) { n     scanf( %d , &c[i]); n   } n   if (n % 2 == 0) { n     puts( 0 ); n     return 0; n   } n   d = (n + 1) / 2; n   md = (long long)(m / d) * (long long)r; n   for (i = 0; i < n; i += 2) { n     if (c[i] < md) md = c[i]; n   } n   printf( %I64d n , md); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   double general_sum, sum; n   general_sum = sum = 0; n   int i = 0; n   int j = 0; n   cin >> n >> k; n   vector<int> a(n); n   double w = n - k + 1; n   for (i; i < w; i++) { n     if (i == 0) { n       for (j; j < k; j++) { n         cin >> a[j]; n         sum += a[j]; n       } n     } else { n       cin >> a[j]; n       sum += a[j]; n       sum -= a[i - 1]; n       j++; n     } n     general_sum += sum; n   } n   cout.precision(6); n   cout << fixed << general_sum / w << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n const int inf = 0x3f3f3f3f; n int n, a[N], ma[N][N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (a[i] > a[j]) cnt++; n   int ans = cnt, tot = 1; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) ma[i][j] = ma[i][j - 1] + (a[i] < a[j]); n   for (int i = n; i >= 1; i--) n     for (int j = i - 1; j >= 1; j--) ma[i][j] = ma[i][j + 1] + (a[i] > a[j]); n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int aa = ma[i][j - 1], bb = ma[j][i + 1]; n       int tmp = cnt + aa - (j - i - 1 - aa) + bb - (j - i - 1 - bb); n       tmp += (a[i] < a[j] ? 1 : -1); n       if (ans > tmp) { n         ans = tmp; n         tot = 1; n       } else if (ans == tmp) n         tot++; n     } n   } n   printf( %d %d n , ans, tot); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, K, f[110][110][30], ans = -1e9 - 7, a[30][30], c; n char s[110], x, y; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   scanf( %d%d , &K, &m); n   for (int i = 1; i <= m; i++) { n     scanf(  %c %c %d , &x, &y, &c); n     a[x -  a ][y -  a ] = c; n   } n   memset(f, 0xcf, sizeof(f)); n   for (int i = 0; i < 26; i++) f[1][s[1] -  a  == i ? 0 : 1][i] = 0; n   for (int i = 2; i <= n; i++) n     for (int j = 0; j <= min(i, K); j++) n       for (int k = 0; k < 26; k++) n         for (int l = 0; l < 26; l++) n           if (s[i] -  a  == l) n             f[i][j][l] = max(f[i][j][l], f[i - 1][j][k] + a[k][l]); n           else if (j) n             f[i][j][l] = max(f[i][j][l], f[i - 1][j - 1][k] + a[k][l]); n   for (int j = 0; j <= K; j++) n     for (int k = 0; k < 26; k++) ans = max(ans, f[n][j][k]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   int cur = 1; n   for (int i = 0; i < n; i++) { n     if (cur <= arr[i]) { n       cur++; n     } n   } n   cout << cur; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n & 1) { n     cout << (n + 1) / 2; n   } else { n     cout << n / 2 + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, x, y, z, q, w, e; n int main() { n   cin >> a >> b >> c >> x >> y >> z; n   q = a - x; n   if (q > 0) q /= 2; n   w = b - y; n   if (w > 0) w /= 2; n   e = c - z; n   if (e > 0) e /= 2; n   if (q + w + e >= 0) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 0.000000001; n double x[200000]; n int n; n void solve() { n   if (abs(x[1]) < eps) { n     int p = 0; n     for (int i = 1; i < n; i++) n       if (abs(x[i]) > eps) p++; n     if (p == 0) { n       printf( 1 n ); n       exit(0); n     } n     return; n   } n   double q = x[2] / x[1]; n   for (int i = 3; i < n; i++) { n     if (abs(x[i]) < eps) { n       if (abs(q) > eps) return; n     } else if (abs(x[i] / x[i - 1] - q) > eps) n       return; n   } n   printf( 1 n ); n   exit(0); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %lf , &x[i]); n   if (abs(x[0]) < eps) { n     int p = 0; n     for (int i = 0; i < n; i++) n       if (abs(x[i]) > eps) p++; n     if (p < 2) { n       printf( %d n , p); n       return 0; n     } n     solve(); n     printf( 2 n ); n     return 0; n   } n   if (n < 3) { n     printf( 0 n ); n     return 0; n   } n   double q = x[1] / x[0]; n   int p = 0; n   for (int i = 2; i < n; i++) { n     if (abs(x[i]) < eps) { n       if (abs(q) > eps) { n         if (p == 1) { n           p = 2; n           break; n         } n         p = 1; n         x[i] = x[i - 1]; n       } n     } else if (abs(x[i] / x[i - 1] - q) > eps) { n       if (p == 1) { n         p = 2; n         break; n       } n       p = 1; n       if (abs(x[i] - x[i - 1]) > eps && i != n - 1) { n         if (abs(x[i + 1] / x[i - 1] - q) < eps) n           x[i] = x[i - 1]; n         else if (abs(x[i + 1] / x[i] - q) > eps) { n           p = 2; n           break; n         } n       } n     } n   } n   if (p < 2) { n     printf( %d n , p); n     return 0; n   } n   solve(); n   swap(x[0], x[1]); n   solve(); n   printf( 2 n ); n   return 0; n } n


#pragma GCC optimize ( O3 ) n #include <bits/stdc++.h> n  n using namespace std; n typedef long long ll; n #define endl   n  n const ll DIM = 607; n const ll INF = 1E18; n vector<pair<int,ll> > G[DIM]; n ll dist[DIM],mod[DIM*4]; n bool vis[DIM]; n struct node{ n     ll dist; n     int x,y; n }; n const bool operator > (const node &a,const node &b){ n     return a.dist>b.dist; n } n int main() { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n  n     ll n, m; n     cin >> n >> m; n     for(ll i = 0;i<DIM*4;++i) n         mod[i] = i%n; n     for (int i = 1; i <= m; ++i) { n         ll a, b, c; n         cin >> a >> b >> c; n         G[a].push_back({b, c}); n     } n     for (ll i = 0; i < n; ++i) { n         sort(G[i].begin(), G[i].end()); n         vector<pair<int, ll> > V; n         int mi = 0; n         for (int j = 1; j < G[i].size(); ++j) { n             if (G[i][mi].second > G[i][j].second) n                 mi = j; n         } n         for (int j = mi; j < G[i].size(); ++j) n             V.push_back(G[i][j]); n         for (int j = 0; j < mi; ++j) n             V.push_back(G[i][j]); n         G[i] = V; n     } n  n     for (ll x = 0; x < n; ++x) { n         for(ll j = 0;j<n;++j){ n             dist[j] = INF; n             vis[j] = 0; n         } n         dist[x] = 0; n  n         while(1){ n             int y = -1; n             for(int j = 0;j<n;++j){ n                 if (vis[j]) n                     continue; n                 if (y==-1 || dist[y]>dist[j]) n                     y = j; n             } n             if (y==-1) n                 break; n             vis[y] = 1; n             ll dd = dist[y]; n             ll dm = dd%n; n             pair<int, ll> cur = G[y][0]; n             int start = mod[G[y][0].first + dm]; n             int ptr = 1; n             ll ss = G[y][0].second - 1; n             for (int i = start; i < n; ++i) { n                 ++ss; n                 while (ptr < G[y].size() && mod[G[y][ptr].first + dm] == i) { n                     if (G[y][ptr].second < ss) { n                         cur = G[y][ptr]; n                         ss = G[y][ptr].second; n                     } n                     ++ptr; n                 } n                 ll ds = ss + dd; n                 if (dist[i] > ds) { n                     dist[i] = ds; n  n                 } n             } n             for (int i = 0; i < start; ++i) { n                 ++ss; n                 while (ptr < G[y].size() && mod[G[y][ptr].first + dm] == i) { n                     if (G[y][ptr].second < ss) { n                         cur = G[y][ptr]; n                         ss = G[y][ptr].second; n                     } n                     ++ptr; n                 } n                 ll ds = ss + dd; n                 if (dist[i] > ds) { n                     dist[i] = ds; n  n                 } n             } n         } n         for(ll i = 0;i<n;++i) n             cout<<dist[i]<<   ; n         cout<<endl; n     } n  n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const int N = 5001; n int dp[N][26][26]; n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   int n = s.length(); n   for (int i = (int)(0); i < (int)(n); i++) { n     int ch = s[i] -  a ; n     for (int j = (int)(0); j < (int)(n); j++) { n       int dh = s[(i + j) % n] -  a ; n       dp[j][ch][dh] += 1; n     } n   } n   int cnt = 0; n   for (int j = (int)(0); j < (int)(26); j++) { n     int ma = 0; n     for (int i = (int)(1); i < (int)(n); i++) { n       int uq = 0; n       for (int k = (int)(0); k < (int)(26); k++) { n         if (dp[i][j][k] == 1) { n           uq++; n         } n       } n       ma = max(ma, uq); n     } n     cnt += ma; n   } n   cout << setprecision(15) << ((double)cnt / (double)n) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 123; n int n, c; n int made[N], preparedToSell[N]; n long long goods[N], limit[N]; n long long toSell[N], toSend[N]; n void readData() { n   cin >> n >> c; n   for (int i = 1; i <= n; ++i) { n     cin >> made[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> preparedToSell[i]; n   } n } n void generateData() { n   n = rand() % 6; n   c = rand() % 4; n   for (int i = 1; i <= n; ++i) { n     made[i] = rand() % 6; n   } n   for (int i = 1; i <= n; ++i) { n     preparedToSell[i] = rand() % 6; n   } n } n void writeLog() { n   cout << n <<     << c <<   n ; n   for (int i = 1; i <= n; ++i) { n     cout << made[i] <<    ; n   } n   cout <<   n ; n   for (int i = 1; i <= n; ++i) { n     cout << preparedToSell[i] <<    ; n   } n   cout <<   n ; n } n long long capacity[102][102], flow[102][102], excess[102]; n int height[102]; n set<int> overfilled, lowerChildren[102], higherChildren[102]; n void push(int u, int v) { n   long long additionalFlow = min(excess[u], capacity[u][v] - flow[u][v]); n   if (!excess[v] && additionalFlow && v != n + 1) { n     overfilled.insert(v); n   } n   if (excess[u] == additionalFlow) { n     overfilled.erase(u); n   } n   flow[u][v] += additionalFlow; n   flow[v][u] -= additionalFlow; n   excess[u] -= additionalFlow; n   excess[v] += additionalFlow; n   if (flow[u][v] == capacity[u][v]) { n     capacity[u][v] = 0; n     lowerChildren[u].erase(v); n     higherChildren[v].insert(u); n   } n } n void relabel(int u) { n   if (u == 0) { n     overfilled.erase(0); n     return; n   } n   int minHeight = height[0]; n   for (int v : higherChildren[u]) { n     minHeight = min(minHeight, height[v]); n   } n   height[u] = minHeight + 1; n   for (int i = 0; i <= n + 1; ++i) { n     if (height[i] >= height[u]) continue; n     auto it = higherChildren[u].find(i); n     if (it != higherChildren[u].end()) { n       higherChildren[u].erase(it); n       lowerChildren[u].insert(i); n     } n   } n   for (int i = 0; i <= n + 1; ++i) { n     auto it = lowerChildren[i].find(u); n     if (it != lowerChildren[i].end()) { n       lowerChildren[i].erase(it); n       higherChildren[i].insert(u); n     } n   } n } n long long flowSolve() { n   memset(capacity, 0, sizeof(capacity)); n   memset(flow, 0, sizeof(flow)); n   memset(excess, 0, sizeof(excess)); n   overfilled.clear(); n   for (auto& s : higherChildren) { n     s.clear(); n   } n   for (auto& s : lowerChildren) { n     s.clear(); n   } n   for (int i = 1; i <= n; ++i) { n     capacity[0][i] = made[i]; n     lowerChildren[0].insert(i); n     for (int j = i + 1; j <= n; ++j) { n       capacity[i][j] = c; n       higherChildren[i].insert(j); n     } n     capacity[i][n + 1] = preparedToSell[i]; n     higherChildren[i].insert(n + 1); n   } n   excess[0] = 1e18; n   height[0] = n + 2; n   overfilled.insert(0); n   while (!overfilled.empty()) { n     int u = *(overfilled.begin()); n     if (!lowerChildren[u].empty()) { n       int v = *(lowerChildren[u].begin()); n       push(u, v); n     } else { n       relabel(u); n     } n   } n   return excess[n + 1]; n } n long long table[2][N]; n long long solve() { n   long long cll = c; n   for (int k = 1; k <= n; ++k) { n     long long* oldRow = table[k & 1]; n     long long* newRow = table[!(k & 1)]; n     newRow[0] = oldRow[0] + made[k]; n     for (int i = 1; i < k; ++i) { n       newRow[i] = n           min(oldRow[i] + made[k] + i * cll, oldRow[i - 1] + preparedToSell[k]); n     } n     newRow[k] = oldRow[k - 1] + preparedToSell[k]; n   } n   long long* lastRow = table[!(n & 1)]; n   long long result = lastRow[0]; n   for (int i = 1; i <= n; ++i) { n     result = min(result, lastRow[i]); n   } n   return result; n } n int main() { n   ios_base::sync_with_stdio(false); n   srand(time(0)); n   readData(); n   cout << solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[1010][1010], v1[1010][1010], tip[100], n, m; n char sir[2010][1010], sir1[2010][1010]; n void transpune() { n   for (int i = 1; i < 2 * m; i++) n     if (i % 2) n       for (int j = 1; j < n; j++) sir1[i][j] = sir[2 * j][i / 2 + 1]; n     else n       for (int j = 1; j <= n; j++) sir1[i][j] = sir[2 * j - 1][i / 2]; n   swap(n, m); n   for (int i = 1; i < 2 * n; i++) n     for (int j = 1; j <= m - i % 2; j++) sir[i][j] = sir1[i][j]; n } n int solve1() { n   int sol = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (j < m) sol += sir[2 * i - 1][j] ==  E ; n       if (i < n) sol += sir[2 * i][j] ==  E ; n     } n   return 4 * sol >= 3 * (2 * n * m - n - m); n } n int solve2() { n   int transpus = 0; n   if (n > m) { n     transpune(); n     transpus = 1; n   } n   for (int i = 1; i <= n; i++) { n     v[i][1] = 0; n     for (int j = 2; j <= m; j++) n       v[i][j] = v[i][j - 1] ^ tip[sir[2 * i - 1][j - 1]]; n     if (i > 1) { n       int nr = 0; n       for (int j = 1; j <= m; j++) n         nr += (v[i][j] == v[i - 1][j]) ^ tip[sir[2 * i - 2][j]]; n       if (2 * nr < m) n         for (int j = 1; j <= m; j++) v[i][j] ^= 1; n     } n   } n   if (transpus) { n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) v1[j][i] = v[i][j]; n     swap(n, m); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) v[i][j] = v1[i][j]; n   } n   return 1; n } n int main() { n   int k, ok; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i < 2 * n; i++) scanf(  n%s , sir[i] + 1); n   tip[ E ] = 0; n   tip[ N ] = 1; n   if (k == 1) n     ok = solve1(); n   else n     ok = solve2(); n   if (ok) { n     printf( YES n ); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) printf( %d  , v[i][j] + 1); n       printf(  n ); n     } n   } else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void Read(T &cn) { n   char c; n   int sig = 1; n   while (!isdigit(c = getchar())) n     if (c ==  - ) sig = -1; n   cn = c - 48; n   while (isdigit(c = getchar())) cn = cn * 10 + c - 48; n   cn *= sig; n } n template <typename T> n void Write(T cn) { n   if (cn < 0) { n     putchar( - ); n     cn = 0 - cn; n   } n   int wei = 0; n   T cm = 0; n   int cx = cn % 10; n   cn /= 10; n   while (cn) cm = cm * 10 + cn % 10, cn /= 10, wei++; n   while (wei--) putchar(cm % 10 + 48), cm /= 10; n   putchar(cx + 48); n } n int f[500 / 2 + 1][500 + 1][500 + 1]; n int n, m, q; n int a[500 + 1][500 + 1]; n int b[500 + 1][500 + 1]; n int jian_c(int cn, int cm, int cx) { n   if (a[cn - cx + 1][cm - cx + 1] !=  R  || b[cn - cx + 1][cm - cx + 1] != cx) n     return 0; n   if (a[cn - cx + 1][cm + 1] !=  G  || b[cn - cx + 1][cm + 1] != cx) return 0; n   if (a[cn + 1][cm - cx + 1] !=  Y  || b[cn + 1][cm - cx + 1] != cx) return 0; n   if (a[cn + 1][cm + 1] !=  B  || b[cn + 1][cm + 1] < cx) return 0; n   return 1; n } n void jian(int cn, int cm) { n   int lin = min(min(cn, cm), min(n - cn, m - cm)); n   for (int i = 1; i <= lin; i++) { n     if (!jian_c(cn, cm, i)) return; n     f[i][cn - i + 1][cm - i + 1] = 1; n   } n } n void yuchu() { n   for (int i = 1; i <= n; i++) b[i][m] = 1; n   for (int i = 1; i <= m; i++) b[n][i] = 1; n   for (int i = n - 1; i >= 1; i--) n     for (int j = m - 1; j >= 1; j--) { n       if (a[i][j] == a[i + 1][j + 1] && a[i][j] == a[i + 1][j] && n           a[i][j] == a[i][j + 1]) n         b[i][j] = 1 + min(min(b[i + 1][j], b[i][j + 1]), b[i + 1][j + 1]); n       else n         b[i][j] = 1; n     } n } n int suan(int cn, int cx1, int cy1, int cx2, int cy2) { n   return f[cn][cx2][cy2] - f[cn][cx2][cy1] - f[cn][cx1][cy2] + f[cn][cx1][cy1]; n } n int main() { n   Read(n); n   Read(m); n   Read(q); n   for (int i = 1; i <= n; i++) { n     while (!isalpha(a[i][1] = getchar())) n       ; n     for (int j = 2; j <= m; j++) a[i][j] = getchar(); n   } n   yuchu(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) jian(i, j); n   for (int ij = 1; ij <= n / 2; ij++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) n         f[ij][i][j] = f[ij][i - 1][j] + f[ij][i][j - 1] - f[ij][i - 1][j - 1] + n                       f[ij][i][j]; n   for (int i = 1; i <= q; i++) { n     int bx1, by1, bx2, by2; n     Read(bx1); n     Read(by1); n     Read(bx2); n     Read(by2); n     int ans = 0; n     int lin = (min(bx2 - bx1, by2 - by1) + 1) / 2; n     for (int j = lin; j >= 1; j--) n       if (suan(j, bx1 - 1, by1 - 1, bx2 - j * 2 + 1, by2 - j * 2 + 1)) { n         ans = 4 * j * j; n         break; n       } n     Write(ans); n     puts(  ); 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 long long pow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * x); n     x = (x * x); n     n >>= 1; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long x = n / 2; n     if (x % 2) { n       cout <<  NO  <<   n ; n       continue; n     } n     cout <<  YES  <<   n ; n     vector<long long> a; n     for (long long i = 0; i < n; i += 2) { n       a.push_back(i + 2); n     } n     long long c = a.size() / 2; n     for (long long i = 0; i < c; i++) { n       a.push_back(a[i] - 1); n       a.push_back(a[x - i - 1] + 1); n     } n     for (auto x : a) cout << x <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[15][15]; n vector<long long int> dp[1 << 14][14]; n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     dp[1 << i][i].push_back(1LL); n   } n   for (int i = 1; i < (1 << n); i++) { n     int p = __builtin_popcount(i); n     for (int j = 0; j < n; j++) { n       if (((i >> j) & 1) == 0) continue; n       for (int k = 0; k < n; k++) { n         if ((i >> k) & 1) continue; n         if (dp[i + (1 << k)][k].size() == 0) { n           dp[i + (1 << k)][k].resize((1 << p)); n         } n         int t = 0; n         if (a[j][k] ==  1 ) t = 1; n         for (int l = 0; l < dp[i][j].size(); l++) { n           dp[i + (1 << k)][k][t * (1 << (p - 1)) + l] += dp[i][j][l]; n         } n       } n     } n   } n   for (int i = 0; i < (1 << (n - 1)); i++) { n     long long int res = 0; n     for (int j = 0; j < n; j++) { n       res += dp[(1 << n) - 1][j][i]; n     } n     printf( %lld  , res); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n static const int maxn = 1000 + 10; n int n, m, f[maxn][maxn]; n char s[maxn], t[maxn]; n int main() { n   scanf( %d %d , &n, &m); n   scanf( %s , s); n   scanf( %s , t); n   int mn = n + 1; n   vector<int> ans; n   for (int i = 0; i + n <= m; i++) { n     int count = 0; n     vector<int> now; n     for (int j = 0; j < n; j++) n       if (s[j] != t[i + j]) { n         count++; n         now.emplace_back(j + 1); n       } n     if (count < mn) { n       mn = count; n       ans = now; n     } n   } n   printf( %d n , mn); n   for (int i = 0; i < ans.size(); i++) n     printf( %d%c , ans[i],    n [i == mn - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void swapll(long long *a, long long *b) { n   long long tmp = *a; n   *a = *b; n   *b = tmp; n } n void swapc(char *a, char *b) { n   char tmp = *a; n   *a = *b; n   *b = tmp; n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n, 0); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   ; n   long long count = 0; n   long long idx = -1; n   for (long long i = 0; i < n; i++) { n     if (v[i] == 1 && idx == -1) { n       count++; n       idx = i; n     } else if (v[i] == 1 && i - idx == 1) { n       count++; n       idx = i; n     } else if (v[i] == 1 && i - idx != 1) { n       count += 2; n       idx = i; n     } n   } n   cout << count << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, ans; n   cin >> n >> a >> b; n   if (n == 1) n     cout <<  1  << endl; n   else { n     ans = (a + b + abs(b) * n) % n; n     if (ans == 0) ans = n; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n struct Node { n   int l, r, w, s; n } t[N << 2]; n vector<int> a[N], c[N]; n int p[N], q[N], L[N], R[N], s[N], z[N]; n int cnt, n, m, x, y; n void update(int x) { n   if (t[x].w) n     t[x].s = t[x].r - t[x].l + 1; n   else if (t[x].r != t[x].l) n     t[x].s = t[x << 1].s + t[x << 1 | 1].s; n   else n     t[x].s = 0; n } n void build(int x, int l, int r) { n   t[x].l = l, t[x].r = r; n   if (l == r) return; n   int m = (l + r) >> 1; n   build(x << 1, l, m); n   build(x << 1 | 1, m + 1, r); n   update(x); n } n void change(int x, int l, int r, int d) { n   if (t[x].l > r || t[x].r < l) n     return; n   else if (l <= t[x].l && t[x].r <= r) n     t[x].w += d; n   else n     change(x << 1, l, r, d), change(x << 1 | 1, l, r, d); n   update(x); n } n void dfs(int x, int y) { n   L[x] = ++cnt; n   for (__typeof(a[x].begin()) i = a[x].begin(); i != a[x].end(); i++) n     if (*i != y) dfs(*i, x); n   R[x] = cnt; n } n inline void tcg(int k, int d) { change(1, L[k], R[k], d); } n void DFS(int x, int y) { n   for (__typeof(c[x].begin()) i = c[x].begin(); i != c[x].end(); i++) n     tcg(p[*i], 1), tcg(q[*i], 1); n   z[x] = t[1].s; n   for (__typeof(a[x].begin()) i = a[x].begin(); i != a[x].end(); i++) n     if (*i != y) DFS(*i, x); n   for (__typeof(c[x].begin()) i = c[x].begin(); i != c[x].end(); i++) n     tcg(p[*i], -1), tcg(q[*i], -1); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     a[x].push_back(y); n     a[y].push_back(x); n   } n   dfs(1, 0); n   build(1, 1, n); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , p + i, q + i); n     c[p[i]].push_back(i); n     c[q[i]].push_back(i); n   } n   DFS(1, 0); n   for (int i = 1; i <= n; i++) printf( %d  , max(z[i] - 1, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans = 0; n long long f[1000001]; n int main(int argc, char **argv) { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     long long b; n     cin >> b; n     f[b - i + 400000] += b; n     ans = max(ans, f[b - i + 400000]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1 << 18; n int a[MAXN]; n long long s[MAXN << 2]; n int h[MAXN << 2]; n void build(int id, int l, int r) { n   if (l == r) { n     s[id] = a[l]; n     h[id] = 0; n     return; n   } n   int mid = (l + r) >> 1; n   build(id << 1, l, mid); n   build(id << 1 | 1, mid + 1, r); n   s[id] = s[id << 1] + s[id << 1 | 1]; n   h[id] = h[id << 1] + 1; n } n long long query(int id, int l, int r, int ll, int rr) { n   if (ll <= l && r <= rr) return s[id]; n   int mid = (l + r) >> 1; n   long long ret = 0; n   if (ll <= mid) ret += query(id << 1, l, mid, ll, rr); n   if (rr > mid) ret += query(id << 1 | 1, mid + 1, r, ll, rr); n   return ret; n } n void modify(int id, int l, int r, int pos, int v) { n   if (l == r) { n     s[id] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     modify(id << 1, l, mid, pos, v); n   else n     modify(id << 1 | 1, mid + 1, r, pos, v); n   s[id] = s[id << 1] + s[id << 1 | 1]; n } n int S; n int v = 0; n long long query1(int id, int l, int r, int ll, int rr) { n   if (ll <= l && r <= rr) { n     int LLL = ((v & (S - (1 << h[id]))) ^ l), n         RRR = ((v & (S - (1 << h[id]))) ^ r); n     if (LLL > RRR) swap(LLL, RRR); n     return query(1, 0, S - 1, LLL, RRR); n   } n   int mid = (l + r) >> 1; n   long long ret = 0; n   if (ll <= mid) ret += query1(id << 1, l, mid, ll, rr); n   if (rr > mid) ret += query1(id << 1 | 1, mid + 1, r, ll, rr); n   return ret; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < (1 << n); ++i) scanf( %d , &a[i]); n   build(1, 0, (1 << n) - 1); n   S = (1 << n); n   for (int i = 1; i <= q; ++i) { n     int tp; n     scanf( %d , &tp); n     if (tp == 1) { n       int x, k; n       scanf( %d%d , &x, &k); n       x--; n       x ^= v; n       modify(1, 0, (1 << n) - 1, x, k); n     } else if (tp == 2) { n       int k; n       scanf( %d , &k); n       v ^= (1 << k) - 1; n     } else if (tp == 3) { n       int k; n       scanf( %d , &k); n       v ^= (1 << k); n     } else { n       int l, r; n       scanf( %d%d , &l, &r); n       l--; n       r--; n       printf( %lld n , query1(1, 0, (1 << n) - 1, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int z[N], t1[N], t2[N]; n string s; n vector<pair<int, int> > ans; n int n; n void BuildZArray() { n   int L = 0, R = 0; n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   n = s.size(); n   BuildZArray(); n   for (int i = 0; i < n; i++) { n     t1[z[i]]++; n   } n   t1[n]++; n   for (int i = N - 1; i >= 0; i--) { n     t2[i] = t1[i] + t2[i + 1]; n   } n   for (int i = 0; i < n; i++) n     if (z[i] >= n - i) ans.push_back(pair<int, int>(z[i], t2[z[i]])); n   ans.push_back(pair<int, int>(n, 1)); n   sort(ans.begin(), ans.end()); n   cout << ans.size() <<   n ; n   for (auto it : ans) cout << it.first <<     << it.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n inline int read() { n   int s = 0, w = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) w |= (ch ==  - ), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return w ? -s : s; n } n struct rr { n   int nt, to; n } bl[100005 << 1]; n int hd[100005], itot = 0; n inline void adde(int x, int y) { n   bl[++itot] = (rr){hd[x], y}; n   hd[x] = itot; n   bl[++itot] = (rr){hd[y], x}; n   hd[y] = itot; n   return; n } n vector<int> to[100005]; n int belong[100005], cnt = 0, sz[100005]; n int dfn[100005], low[100005], num = 0, stk[100005], top = 0; n int vis[100005]; n void tarjan(int u, int fa) { n   dfn[u] = low[u] = ++num; n   stk[++top] = u; n   vis[u] = 1; n   for (int i = hd[u], y; i; i = bl[i].nt) n     if ((y = bl[i].to) != fa && vis[y] != 2) { n       if (!dfn[y]) { n         tarjan(y, u); n         low[u] = min(low[u], low[y]); n       } else n         low[u] = min(low[u], dfn[y]); n     } n   if (dfn[u] == low[u]) { n     int z; n     ++cnt; n     do { n       z = stk[top--]; n       belong[z] = cnt; n       sz[cnt]++; n       vis[z] = 2; n     } while (z != u); n   } n   return; n } n int n, m; n int dist[100005], sd[100005]; n int f[100005][20], LOG[100005]; n inline int LCA(int x, int y) { n   if (sd[x] < sd[y]) swap(x, y); n   for (int j = LOG[sd[x]]; j >= 0; --j) n     if (f[x][j] && sd[f[x][j]] >= sd[y]) x = f[x][j]; n   if (x == y) return x; n   for (int j = LOG[sd[x]]; j >= 0; --j) n     if (f[x][j] && f[y][j] && f[x][j] != f[y][j]) x = f[x][j], y = f[y][j]; n   return f[x][0]; n } n void dfs(int u, int fa) { n   dist[u] = dist[fa] + (sz[u] != 1); n   sd[u] = sd[fa] + 1; n   f[u][0] = fa; n   for (int j = 1; j <= LOG[sd[u]]; ++j) f[u][j] = f[f[u][j - 1]][j - 1]; n   for (int i = 0; i < to[u].size(); ++i) n     if (to[u][i] != fa) dfs(to[u][i], u); n   return; n } n int bin[100005]; n int main() { n   n = (read()), m = (read()); n   bin[0] = 1, bin[1] = 2; n   for (int i = 2; i <= n; ++i) { n     LOG[i] = LOG[i >> 1] + 1; n     bin[i] = bin[i - 1] + bin[i - 1]; n     if (bin[i] >= mod) bin[i] -= mod; n   } n   for (int i = 1, x, y; i <= m; ++i) x = (read()), y = (read()), adde(x, y); n   tarjan(1, 0); n   for (int i = 1; i <= n; ++i) n     for (int j = hd[i], y; j; j = bl[j].nt) n       if (belong[i] != belong[y = bl[j].to]) to[belong[i]].push_back(belong[y]); n   dfs(1, 0); n   int q = (read()), x, y; n   while (q--) { n     x = belong[(read())], y = belong[(read())]; n     int lca = LCA(x, y); n     printf( %d n , bin[dist[x] + dist[y] - dist[lca] - dist[f[lca][0]]]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const int MAXE = 200005; n const int mod = 1e9 + 7; n struct Edge { n   int v; n   Edge* next; n } E[MAXE], *H[MAXN], *cur; n int color[MAXN]; n long long dp[MAXN][2]; n void clear() { n   cur = E; n   memset(H, 0, sizeof H); n   memset(color, 0, sizeof color); n   memset(dp, 0, sizeof dp); n } n void addedge(int u, int v) { n   cur->v = v; n   cur->next = H[u]; n   H[u] = cur++; n } n void scanf(int& x, char c = 0) { n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n } n void dfs(int u, int f = 0) { n   dp[u][color[u]] = 1; n   for (Edge* e = H[u]; e; e = e->next) { n     int v = e->v; n     if (v == f) continue; n     dfs(v, u); n     dp[u][1] = n         (dp[u][1] * (dp[v][0] + dp[v][1]) % mod + dp[u][0] * dp[v][1]) % mod; n     dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % mod; n   } n } n void solve() { n   int n, x; n   while (~scanf( %d , &n)) { n     clear(); n     for (int i = (1); i < (n); ++i) { n       scanf(x); n       addedge(x, i); n       addedge(i, x); n     } n     for (int i = (0); i < (n); ++i) scanf(color[i]); n     dfs(0); n     printf( %I64d n , dp[0][1]); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l, x, y; n   cin >> n >> l >> x >> y; n   set<int> d; n   for (int i = 0; i < n; ++i) { n     int t; n     cin >> t; n     d.insert(t); n   } n   bool issetX = false, issetY = false; n   for (set<int>::iterator i = d.begin(); i != d.end(); ++i) { n     if (d.find(*i + x) != d.end()) { n       issetX = true; n     } n     if (d.find(*i + y) != d.end()) { n       issetY = true; n     } n   } n   if (issetX && issetY) { n     cout << 0; n     return 0; n   } else if (issetX) { n     cout <<  1   << endl << y; n   } else if (issetY) { n     cout <<  1   << endl << x; n   } else { n     int point = -1; n     for (set<int>::iterator i = d.begin(); i != d.end(); ++i) { n       if (d.find(*i + (y - x)) != d.end()) { n         if (*i - x >= 0) { n           point = *i - x; n           break; n         } else if (*i + (y - x) + x <= l) { n           point = *i + (y - x) + x; n           break; n         } n       } n       if (d.find(*i + (y + x)) != d.end()) { n         point = *i + x; n         break; n       } n     } n     if (point < 0) { n       cout <<  2   << endl << x <<     << y; n     } else { n       cout <<  1   << endl << point; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, flag; n   cin >> n >> m; n   int l[m], ans[n], check[n]; n   memset(ans, 0, sizeof(ans)); n   memset(check, 0, sizeof(check)); n   for (int i = 0; i < m; i++) cin >> l[i]; n   for (int i = 0; i < m - 1; i++) { n     if (l[i + 1] > l[i]) { n       flag = 0; n       if (ans[l[i] - 1] != 0) { n         if (ans[l[i] - 1] != l[i + 1] - l[i]) { n           cout <<  -1 ; n           return 0; n         } else n           flag = 1; n       } n       ans[l[i] - 1] = l[i + 1] - l[i]; n       if (check[ans[l[i] - 1] - 1] == 0) n         check[ans[l[i] - 1] - 1] = 1; n       else if (flag == 0) { n         cout <<  -1 ; n         return 0; n       } n     } else { n       flag = 0; n       if (ans[l[i] - 1] != 0) { n         if (ans[l[i] - 1] != l[i + 1] - l[i] + n) { n           cout <<  -1 ; n           return 0; n         } else n           flag = 1; n       } n       ans[l[i] - 1] = l[i + 1] - l[i] + n; n       if (check[ans[l[i] - 1] - 1] == 0) n         check[ans[l[i] - 1] - 1] = 1; n       else if (flag == 0) { n         cout <<  -1 ; n         return 0; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (ans[i]) n       cout << ans[i] <<    ; n     else { n       for (int j = 0; j < n; j++) n         if (check[j] == 0) { n           cout << j + 1 <<    ; n           check[j] = 1; n           break; n         } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, id; n } s[100101]; n bool cmp(node a, node b) { return a.x < b.x; } n int main() { n   int n, i, j, x; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> s[i].x; n     s[i].id = i + 1; n   } n   sort(s, s + n, cmp); n   if (n % 2) n     x = n / 2 + 1; n   else n     x = n / 2; n   cout << x << endl; n   for (i = 0; i < n; i += 2) cout << s[i].id <<    ; n   cout << endl << n - x << endl; n   for (i = 1; i < n; i += 2) cout << s[i].id <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 9, MAX2 = 3e5 + 9; n const long long MOD = 1e9 + 7; n vector<pair<int, int> > g[MAX]; n pair<int, int> e[MAX2]; n int h[MAX], mark[MAX2], dp[MAX], par[MAX]; n bool vis[MAX]; n void dfs(int v, int height) { n   vis[v] = true; n   h[v] = height; n   for (auto x : g[v]) { n     int u = x.first; n     if (!vis[u]) { n       mark[x.second] = true; n       par[u] = v; n       dfs(u, height + 1); n     } n   } n } n void dfs2(int v) { n   dp[v] = h[v]; n   bool f = false; n   if (v == 2) f = true; n   for (auto x : g[v]) { n     int u = x.first; n     if (mark[x.second]) { n       if (par[u] == v) { n         dfs2(u); n         dp[v] = min(dp[v], dp[u]); n       } n       continue; n     } n     dp[v] = min(dp[v], h[u]); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(par, -1, sizeof(par)); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int v, u; n     cin >> v >> u; n     v--, u--; n     g[v].push_back({u, i}); n     g[u].push_back({v, i}); n     e[i] = {v, u}; n   } n   memset(vis, 0, sizeof(vis)); n   dfs(0, 0); n   dfs2(0); n   for (int i = 0; i < m; i++) { n     if (!mark[i]) continue; n     int v = e[i].first, u = e[i].second; n     if (v != par[u]) swap(v, u); n     if (dp[u] > h[v]) return cout <<  0 n , 0; n   } n   for (int i = 0; i < m; i++) { n     int v = e[i].first, u = e[i].second; n     if (mark[i]) { n       if (v != par[u]) swap(v, u); n     } else { n       if (h[v] < h[u]) swap(v, u); n     } n     cout << v + 1 <<     << u + 1 <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int dist[2 * 100000 + 2 + 5], N, K; n vector<int> G[2 * 100000 + 2 + 5]; n void addedge(const string &wall, const string &wall2, int i, int offs) { n   int here = i + offs, there = i + N - offs; n   if (wall[i] ==  X ) return; n   if (i != 0 && wall[i - 1] !=  X ) G[here].push_back(here - 1); n   if (i == N - 1 || wall[i + 1] !=  X ) G[here].push_back(here + 1); n   if (i + K >= N) { n     G[here].push_back(2 * N); n   } else if (wall2[i + K] !=  X ) { n     G[here].push_back(there + K); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string L, R; n   cin >> N >> K >> L >> R; n   for (int i = 0; i < N; i++) { n     addedge(L, R, i, 0); n     addedge(R, L, i, N); n   } n   memset(dist, -1, sizeof(dist)); n   queue<int> q; n   dist[0] = 0; n   q.push(0); n   while (!q.empty()) { n     int fr = q.front(), d = dist[fr]; n     q.pop(); n     for (int to : G[fr]) { n       if (dist[to] == -1 && (to % N > d || to >= 2 * N)) { n         dist[to] = d + 1; n         q.push(to); n       } n     } n   } n   if (dist[2 * N] != -1 || dist[2 * N + 1] != -1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 5; n const int INF = (int)1e9 + 9; n const int MOD = (int)1e9 + 7; n void go(int x) { n   cout << x; n   exit(0); n } n vector<int> g[N]; n set<int> st[N]; n int c[N]; n bool used[N]; n int main() { n   int n, m, cnt = -1, ans = -1; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n     used[c[i]] = 1; n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n     used[c[u]] = 1; n     used[c[v]] = 1; n     if (c[u] == c[v]) { n       continue; n     } n     st[c[u]].insert(c[v]); n     st[c[v]].insert(c[u]); n   } n   for (int i = 1; i <= 1e5; i++) { n     if (used[i] && (int)st[i].size() > cnt) { n       cnt = (int)st[i].size(); n       ans = i; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int h[100005], ans[100005]; n int l[100005], r[100005], w[100005]; n vector<int> mid2[100005]; n int lo2[100005], hi2[100005]; n vector<int> cur[100005]; n struct st { n   int le, ri, len, all; n }; n st seg[400005]; n st update_seg(st a, st b) { n   st res; n   res.len = a.len + b.len; n   res.all = max(max(a.all, b.all), a.ri + b.le); n   if (a.len == a.le) n     res.le = a.len + b.le; n   else n     res.le = a.le; n   if (b.len == b.le) n     res.ri = a.ri + b.len; n   else n     res.ri = b.ri; n   return res; n } n void update(int n, int s, int e, int idx, int v) { n   if (s == e) { n     seg[n].le = max(0, v); n     seg[n].ri = max(0, v); n     seg[n].all = max(0, v); n     seg[n].len = 1; n   } else { n     int mid = (s + e) / 2; n     if (idx <= mid) n       update(n + n, s, mid, idx, v); n     else n       update(n + n + 1, mid + 1, e, idx, v); n     seg[n] = update_seg(seg[n + n], seg[n + n + 1]); n   } n } n st query(int n, int s, int e, int l, int r) { n   if (s > r || l > e) { n     st res; n     res.le = res.ri = res.len = res.all = 0; n     return res; n   } n   if (l <= s && e <= r) return seg[n]; n   int mid = (s + e) / 2; n   st q1 = query(n + n, s, mid, l, r); n   st q2 = query(n + n + 1, mid + 1, e, l, r); n   return update_seg(q1, q2); n } n int main() { n   int n, q; n   scanf( %d , &n); n   vector<int> v; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n     v.push_back(h[i]); n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   for (int i = 1; i <= n; i++) { n     int idx = lower_bound(v.begin(), v.end(), h[i]) - v.begin(); n     cur[idx].push_back(i); n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d%d , &l[i], &r[i], &w[i]); n     lo2[i] = 0, hi2[i] = v.size() - 1; n   } n   while (1) { n     for (int i = 0; i < v.size(); i++) { n       mid2[i].clear(); n       int h = v[i]; n       int idx = lower_bound(v.begin(), v.end(), h) - v.begin(); n       for (int j = 0; j < cur[idx].size(); j++) { n         int k = cur[idx][j]; n         update(1, 1, n, k, 0); n       } n     } n     bool ok = 1; n     for (int i = 1; i <= q; i++) { n       if (lo2[i] <= hi2[i]) { n         int mid = (lo2[i] + hi2[i]) / 2; n         mid2[mid].push_back(i); n         ok = 0; n       } n     } n     if (ok) break; n     for (int i = v.size() - 1; i >= 0; i--) { n       int h = v[i]; n       int idx = lower_bound(v.begin(), v.end(), h) - v.begin(); n       for (int j = 0; j < cur[idx].size(); j++) { n         int k = cur[idx][j]; n         update(1, 1, n, k, 1); n       } n       for (int j = 0; j < mid2[i].size(); j++) { n         int idx = mid2[i][j]; n         st q = query(1, 1, n, l[idx], r[idx]); n         if (q.all >= w[idx]) { n           lo2[idx] = i + 1; n           ans[idx] = i; n         } else { n           hi2[idx] = i - 1; n         } n       } n     } n   } n   for (int i = 1; i <= q; i++) printf( %d n , v[ans[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n bool lucky(int x) { n   while (x) { n     if (x % 10 != 4 && x % 10 != 7) { n       return false; n     } n     x /= 10; n   } n   return true; n } n int id[maxn], a[maxn], pos[maxn], n; n bool f[maxn]; n bool check() { n   for (int i = 0; i + 1 < n; ++i) { n     if (a[i + 1] < a[i]) { n       return false; n     } n   } n   return true; n } n bool comp(int x, int y) { return a[x] < a[y]; } n int main() { n   while (scanf( %d , &n) != EOF) { n     int c = -1; n     for (int i = 0; i < n; ++i) { n       scanf( %d , a + i); n       if (lucky(a[i])) c = i; n       id[i] = i; n     } n     if (c == -1) { n       printf( %d n , check() ? 0 : -1); n       continue; n     } n     sort(id, id + n, comp); n     for (int i = 0; i < n; ++i) { n       pos[id[i]] = i; n       f[i] = false; n     } n     int posc = c; n     vector<pair<int, int> > ans; n     while (posc != pos[c]) { n       ans.push_back(pair<int, int>(posc, id[posc])); n       f[posc] = true; n       posc = id[posc]; n     } n     f[posc] = true; n     for (int i = 0; i < n; ++i) { n       if (pos[i] != i && !f[i]) { n         ans.push_back(pair<int, int>(posc, i)); n         int tmp = posc; n         posc = i; n         while (pos[i] != posc) { n           ans.push_back(pair<int, int>(posc, id[posc])); n           f[posc] = true; n           posc = id[posc]; n         } n         f[posc] = true; n         ans.push_back(pair<int, int>(posc, tmp)); n         posc = tmp; n       } n     } n     printf( %d n , ((int)((ans).size()))); n     for (int i = 0; i < ((int)((ans).size())); ++i) { n       printf( %d %d n , ans[i].first + 1, ans[i].second + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> mp; n map<string, int> stn; n void Print(string cur) { n   string res =  / ; n   while (cur !=  / ) { n     string tmp = cur; n     while (int(tmp.size()) && n            ( 0  <= tmp[tmp.size() - 1] && tmp[tmp.size() - 1] <=  9 )) n       tmp.erase(tmp.begin() + tmp.size() - 1); n     res =  /  + tmp + res; n     cur = mp[cur]; n   } n   cout << res << endl; n } n int main() { n   int n; n   string s, cur =  / ; n   cin >> n; n   for (int cn = 0; cn < int(n); cn++) { n     cin >> s; n     if (s ==  pwd ) { n       Print(cur); n     } else { n       cin >> s; n       int ssize = s.size(); n       string acc =   ; n       int st = 0; n       if (s[0] ==  . ) { n         cur = mp[cur]; n         st = 3; n       } n       if (s[0] ==  / ) { n         cur =  / ; n         st = 1; n       } n       acc =   ; n       for (int i = st; i < ssize; i++) { n         if (s[i] ==  / ) { n           if (acc ==  .. ) { n             cur = mp[cur]; n           } else { n             stn[acc]++; n             stringstream ss; n             string nw; n             ss << stn[acc]; n             ss >> nw; n             acc += nw; n             mp[acc] = cur; n             cur = acc; n           } n           acc =   ; n         } else { n           acc += s[i]; n         } n       } n       if (acc.size()) { n         if (acc ==  .. ) { n           cur = mp[cur]; n         } else { n           stn[acc]++; n           stringstream ss; n           string nw; n           ss << stn[acc]; n           ss >> nw; n           acc += nw; n           mp[acc] = cur; n           cur = acc; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct str { n   int x1; n   int y1; n   int x2; n   int y2; n }; n bool compare(str &a, str &b) { n   if (a.x1 < b.x1) n     return true; n   else if (a.x1 == b.x1) { n     if (a.x2 < b.x2) n       return true; n     else if (a.x2 == b.x2) { n       if (a.y1 < b.y1) n         return true; n       else if (a.y1 <= b.y1) { n         return (a.y2 < b.y2); n       } n     } n   } n   return false; n } n int main() { n   int n; n   cin >> n; n   vector<str> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i].x1 >> v[i].y1 >> v[i].x2 >> v[i].y2; n   } n   sort(v.begin(), v.end(), compare); n   int x11 = v[0].x1; n   int y11 = v[0].y1; n   int x22 = v[0].x2; n   int y22 = v[0].y2; n   int flag = 0, ind = 0, counter = 1; n   for (int i = 1; i < n; i++) { n     int x1 = max(x11, v[i].x1); n     int x2 = min(x22, v[i].x2); n     int y1 = max(y11, v[i].y1); n     int y2 = min(y22, v[i].y2); n     int x1r = x2 - x1; n     int y1r = y2 - y1; n     if ((x1r < 0 || y1r < 0) && !flag) { n       ind = i; n     } else { n       x11 = x1, y11 = y1, x22 = x2, y22 = y2; n       counter++; n     } n   } n   if (counter == n - 1) n     cout << x11 <<     << y11 << endl; n   else { n     x11 = v[ind].x1, y11 = v[ind].y1, x22 = v[ind].x2, y22 = v[ind].y2; n     for (int i = 1; i < n; i++) { n       int x1 = max(x11, v[i].x1); n       int x2 = min(x22, v[i].x2); n       int y1 = max(y11, v[i].y1); n       int y2 = min(y22, v[i].y2); n       int x1r = x2 - x1; n       int y1r = y2 - y1; n       if ((x1r < 0 || y1r < 0) && !flag) { n         ind = i; n       } else { n         x11 = x1, y11 = y1, x22 = x2, y22 = y2; n         counter++; n       } n     } n     cout << x11 <<     << y11 << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int m = 1e9 + 7; n int bin_pow(int x, int k) { n   if (k == 0) return 1; n   int g = bin_pow(x, k / 2); n   g = (1ll * g * g) % m; n   if (k % 2 > 0) g = (1ll * g * x) % m; n   return g; n } n signed main() { n   iostream::sync_with_stdio(0); n   cin.tie(0); n   setlocale(LC_ALL,  Russian ); n   int tt, n, p, i, j, r, second, ans, add; n   vector<int> read, k, cnt, st; n   cin >> tt; n   for (; tt > 0; tt--) { n     cin >> n >> p; n     read.assign(n, 0); n     k.clear(); n     cnt.clear(); n     st.clear(); n     for (i = 0; i < n; i++) cin >> read[i]; n     if (p == 1) { n       if (n % 2 > 0) n         cout <<  1 n ; n       else n         cout <<  0 n ; n       continue; n     } n     sort(read.begin(), read.end()); n     for (i = 0; i < n; i++) { n       j = i + 1; n       while (j < n && read[i] == read[j]) j++; n       k.push_back(read[i]); n       cnt.push_back(j - i); n       i = j - 1; n     } n     st.push_back(bin_pow(p, k[0])); n     for (i = 1; i < k.size(); i++) n       st.push_back((1ll * st[i - 1] * bin_pow(p, k[i] - k[i - 1])) % m); n     ans = 0; n     for (i = k.size() - 1; i >= 0; i--) { n       if (cnt[i] % 2 == 0) continue; n       j = i - 1; n       second = 1; n       while (j >= 0) { n         for (r = 0; r < k[j + 1] - k[j] && second <= n; r++) second *= p; n         if (r < k[j + 1] - k[j] || second > n) break; n         add = min(second, cnt[j]); n         second -= add; n         ans = (ans - 1ll * cnt[j] * st[j] + 2ll * m * m) % m; n         cnt[j] -= add; n         if (second == 0) break; n         j--; n       } n       if (second > 0) { n         while (j >= 0) { n           ans = (ans - 1ll * cnt[j] * st[j] + 2ll * m * m) % m; n           j--; n         } n         ans = (ans + st[i]) % m; n         break; n       } n       ans = 0; n       i = j + 1; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void add_edge(vector<long long> adj[], long long a, long long b) { n   adj[a].push_back(b); n   adj[b].push_back(a); n } n class myComparator { n  public: n   int operator()(int p1, int p2) { return p1 > p2; } n }; n long long fun(long long x, long long n) { n   long long mid, beg = 1, last = x / 2; n   while (beg < last) { n     mid = (beg + last) / 2; n     long long t = x - mid; n     if (t <= n) { n       last = mid; n     } else { n       beg = mid + 1; n     } n   } n   return (x / 2) - last + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long n; n   cin >> n; n   long long t = (2 * n) - 1; n   string v = to_string(t); n   string s; n   long long i; n   for (i = 0; i < v.size(); i++) s +=  9 ; n   long long num = stoll(s); n   if (num > t) { n     num /= 10; n     s.pop_back(); n   } n   long long ans = 0; n   for (i = 0; i < 9; i++) { n     string temp = to_string(i); n     string ma = temp + s; n     long long num = stoll(ma); n     if (num > t) break; n     ans += fun(num, n); n   } n   if (n > 4) n     cout << ans << endl; n   else { n     long long k; n     long long ans = 1; n     for (k = 3; k <= n; k++) ans *= k; n     if (n == 4) ans = ans / 2; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[111]; n int main() { n   int n; n   scanf( %d , &n); n   arr[0] = 0; n   for (int i = (1); i < (n + 1); ++i) scanf( %d , &arr[i]); n   int ans = 0; n   if (n % 2 == 0 || n == 1) { n     printf( -1 n ); n     return 0; n   } n   for (int i = n; i >= 1; i -= 2) { n     int tmp = max(arr[i], arr[i - 1]); n     ans += tmp; n     arr[i] = arr[i - 1] = 0; n     arr[(i - 1) / 2] = max(0, arr[(i - 1) / 2] - tmp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2010; n int n, m, k; n char a[MAXN][MAXN]; n int pref[MAXN][MAXN]; n int getSumLine(int i, int j) { n   return pref[i][j + k - 1] - pref[i][j - 1] - pref[i - 1][j + k - 1] + n          pref[i - 1][j - 1]; n } n int getSumRow(int i, int j) { n   return pref[i + k - 1][j] - pref[i - 1][j] - pref[i + k - 1][j - 1] + n          pref[i - 1][j - 1]; n } n int main() { n   scanf( %d%d%d n , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %s , a[i] + 1); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i][j] ==  * ) { n         pref[i][j]++; n       } n       pref[i][j] += pref[i - 1][j]; n       pref[i][j] += pref[i][j - 1]; n       pref[i][j] -= pref[i - 1][j - 1]; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (i + k - 1 <= n) { n         if (getSumRow(i, j) == 0) ans++; n       } n       if (j + k - 1 <= m && k != 1) { n         if (getSumLine(i, j) == 0) ans++; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n long long int a, b, n, S; n int main() { n   cin >> t; n   while (t--) { n     cin >> a >> b >> n >> S; n     if (b >= S) { n       cout <<  YES  << endl; n       continue; n     } n     if (a * n + b < S) { n       cout <<  NO  << endl; n       continue; n     } n     long long int x = S / n; n     x = min(x, a); n     long long int res = (S - x * n); n     if (res <= b) { n       cout <<  YES  << endl; n       continue; n     } n     if (res > b) { n       cout <<  NO  << endl; n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n int main() { n   int n, k; n   cin >> n; n   cin >> k; n   int c[n]; n   for (int i = 0; i < n; ++i) cin >> c[i]; n   ; n   map<int, int> table; n   vector<long long> capitals; n   long long csum = 0; n   for (int i = 0; i < k; ++i) { n     int id; n     cin >> id; n     id -= 1; n     capitals.push_back(c[id]); n     ; n     ; n     csum += c[id]; n     table[id] = 1; n   }; n   long long ans = 0; n   long long ncsum = 0; n   for (int i = 0; i < n; ++i) { n     if (table[i] != 1) ncsum += c[i]; n   }; n   long long dncsum = 0; n   for (int i = 0; i < n; ++i) { n     int right = (i + 1 == n ? 0 : i + 1); n     if (table[i] != 1) { n       if (table[right] != 1) { n         ; n         ; n         dncsum += c[i] * c[right]; n       } n     } n   }; n   vector<long long> prefix(k); n   for (int i = 0; i < k; ++i) { n     if (i == 0) n       prefix[i] = capitals[i]; n     else n       prefix[i] = prefix[i - 1] + capitals[i]; n   } n   long long fcsum = 0; n   for (int i = 0; i < k; ++i) { n     fcsum += capitals[i] * (csum - prefix[i]); n     fcsum += capitals[i] * ncsum; n   } n   ans = fcsum + dncsum; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:36777216 ) n template <class T> n inline T &RD(T &); n template <class T> n inline void OT(const T &); n inline int RD() { n   int x; n   return RD(x); n } n inline double &RF(double &); n inline double RF() { n   double x; n   return RF(x); n } n inline char *RS(char *s); n inline char &RC(char &c); n inline char RC(); n inline char &RC(char &c) { n   scanf(  %c , &c); n   return c; n } n inline char RC() { n   char c; n   return RC(c); n } n template <class T> n inline T &RDD(T &x) { n   char c; n   for (c = getchar(); c <  - ; c = getchar()) n     ; n   if (c ==  - ) { n     x =  0  - getchar(); n     for (c = getchar();  0  <= c && c <=  9 ; c = getchar()) n       x = x * 10 +  0  - c; n   } else { n     x = c -  0 ; n     for (c = getchar();  0  <= c && c <=  9 ; c = getchar()) n       x = x * 10 + c -  0 ; n   } n   return x; n } n inline int RDD() { n   int x; n   return RDD(x); n } n template <class T0, class T1> n inline T0 &RD(T0 &x0, T1 &x1) { n   RD(x0), RD(x1); n   return x0; n } n template <class T0, class T1, class T2> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) { n   RD(x0), RD(x1), RD(x2); n   return x0; n } n template <class T0, class T1, class T2, class T3> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { n   RD(x0), RD(x1), RD(x2), RD(x3); n   return x0; n } n template <class T0, class T1, class T2, class T3, class T4> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); n   return x0; n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); n   return x0; n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); n   return x0; n } n template <class T0, class T1> n inline void OT(const T0 &x0, const T1 &x1) { n   OT(x0), OT(x1); n } n template <class T0, class T1, class T2> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) { n   OT(x0), OT(x1), OT(x2); n } n template <class T0, class T1, class T2, class T3> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) { n   OT(x0), OT(x1), OT(x2), OT(x3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, n                const T4 &x4) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, n                const T4 &x4, const T5 &x5) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, n                const T4 &x4, const T5 &x5, const T6 &x6) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); n } n inline char &RC(char &a, char &b) { n   RC(a), RC(b); n   return a; n } n inline char &RC(char &a, char &b, char &c) { n   RC(a), RC(b), RC(c); n   return a; n } n inline char &RC(char &a, char &b, char &c, char &d) { n   RC(a), RC(b), RC(c), RC(d); n   return a; n } n inline char &RC(char &a, char &b, char &c, char &d, char &e) { n   RC(a), RC(b), RC(c), RC(d), RC(e); n   return a; n } n inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) { n   RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); n   return a; n } n inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) { n   RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); n   return a; n } n inline double &RF(double &a, double &b) { n   RF(a), RF(b); n   return a; n } n inline double &RF(double &a, double &b, double &c) { n   RF(a), RF(b), RF(c); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d) { n   RF(a), RF(b), RF(c), RF(d); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d, double &e) { n   RF(a), RF(b), RF(c), RF(d), RF(e); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d, double &e, n                   double &f) { n   RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d, double &e, n                   double &f, double &g) { n   RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); n   return a; n } n inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } n inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } n template <class T0, class T1> n inline void RDD(const T0 &a, const T1 &b) { n   RDD(a), RDD(b); n } n template <class T0, class T1, class T2> n inline void RDD(const T0 &a, const T1 &b, const T2 &c) { n   RDD(a), RDD(b), RDD(c); n } n template <class T> n inline void RST(T &A) { n   memset(A, 0, sizeof(A)); n } n template <class T> n inline void FLC(T &A, int x) { n   memset(A, x, sizeof(A)); n } n template <class T> n inline void CLR(T &A) { n   A.clear(); n } n template <class T0, class T1> n inline void RST(T0 &A0, T1 &A1) { n   RST(A0), RST(A1); n } n template <class T0, class T1, class T2> n inline void RST(T0 &A0, T1 &A1, T2 &A2) { n   RST(A0), RST(A1), RST(A2); n } n template <class T0, class T1, class T2, class T3> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   RST(A0), RST(A1), RST(A2), RST(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); n } n template <class T0, class T1> n inline void FLC(T0 &A0, T1 &A1, int x) { n   FLC(A0, x), FLC(A1, x); n } n template <class T0, class T1, class T2> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) { n   FLC(A0, x), FLC(A1, x), FLC(A2, x); n } n template <class T0, class T1, class T2, class T3> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { n   FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { n   FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { n   FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { n   FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), n       FLC(A6, x); n } n template <class T> n inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) { n   while (!Q.empty()) Q.pop(); n } n template <class T> n inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) { n   while (!Q.empty()) Q.pop(); n } n template <class T0, class T1> n inline void CLR(T0 &A0, T1 &A1) { n   CLR(A0), CLR(A1); n } n template <class T0, class T1, class T2> n inline void CLR(T0 &A0, T1 &A1, T2 &A2) { n   CLR(A0), CLR(A1), CLR(A2); n } n template <class T0, class T1, class T2, class T3> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); n } n template <class T> n inline void CLR(T &A, int n) { n   for (int i = 0; i < int(n); ++i) CLR(A[i]); n } n template <class T> n inline T &SRT(T &A) { n   sort(A.begin(), A.end()); n   return A; n } n template <class T, class C> n inline T &SRT(T &A, C B) { n   sort(A.begin(), A.end(), B); n   return A; n } n template <class T> n inline T &UNQ(T &A) { n   A.resize(unique(SRT(A).begin(), SRT(A).end()) - A.begin()); n   return A; n } n const int dx4[] = {-1, 0, 1, 0}; n const int dy4[] = {0, 1, 0, -1}; n const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1}; n const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1}; n const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; n const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; n const int MOD = 1000000007; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-9; n const double OO = 1e15; n const double PI = acos(-1.0); n template <class T> n inline void checkMin(T &a, const T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkMax(T &a, const T b) { n   if (a < b) a = b; n } n template <class T> n inline void checkMin(T &a, T &b, const T x) { n   checkMin(a, x), checkMin(b, x); n } n template <class T> n inline void checkMax(T &a, T &b, const T x) { n   checkMax(a, x), checkMax(b, x); n } n template <class T, class C> n inline void checkMin(T &a, const T b, C c) { n   if (c(b, a)) a = b; n } n template <class T, class C> n inline void checkMax(T &a, const T b, C c) { n   if (c(a, b)) a = b; n } n template <class T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T min(T a, T b, T c, T d) { n   return min(min(a, b), min(c, d)); n } n template <class T> n inline T max(T a, T b, T c, T d) { n   return max(max(a, b), max(c, d)); n } n template <class T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n inline T cub(T a) { n   return a * a * a; n } n inline int ceil(int x, int y) { return (x - 1) / y + 1; } n inline int sgn(double x) { return x < -EPS ? -1 : x > EPS; } n inline int sgn(double x, double y) { return sgn(x - y); } n template <class T> n inline T &RD(T &x) { n   char c; n   for (c = getchar(); c <  0 ; c = getchar()) n     ; n   x = c -  0 ; n   for (c = getchar();  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   return x; n } n inline double &RF(double &x) { n   scanf( %lf , &x); n   return x; n } n inline char *RS(char *s) { n   scanf( %s , s); n   return s; n } n int Case; n template <class T> n inline void OT(const T &x) { n   cout << x << endl; n } n const int N = 309; n int a[N][N]; n int t, tt; n int tp, tu, td; n int n, m; n int U[N][N], D[N][N], L[N][N], R[N][N]; n struct ANS { n   int T; n   int lx, ly, rx, ry; n } ans; n void upd(int lx, int rx, int ly, int ry, int T) { n   if (ans.T == -1 || abs(T - t) < abs(ans.T - t)) { n     ans.T = T; n     ans.lx = lx; n     ans.ly = ly; n     ans.rx = rx; n     ans.ry = ry; n   } n } n set<pair<int, int> > hh; n int f(int pre, int now) { n   if (pre < now) return tu; n   if (pre > now) return td; n   return tp; n } n int main() { n   RD(n, m); n   RD(t, tp, tu, td); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) RD(a[i][j]); n   RST(U, D, L, R); n   ans.T = -1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 2; j <= m; ++j) { n       L[i][j] = L[i][j - 1]; n       if (a[i][j] > a[i][j - 1]) n         L[i][j] += tu; n       else if (a[i][j] == a[i][j - 1]) n         L[i][j] += tp; n       else n         L[i][j] += td; n     } n   } n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       U[i][j] = U[i - 1][j]; n       if (a[i][j] < a[i - 1][j]) n         U[i][j] += tu; n       else if (a[i][j] == a[i - 1][j]) n         U[i][j] += tp; n       else n         U[i][j] += td; n     } n   } n   for (int i = n - 1; i >= 1; --i) { n     for (int j = 1; j <= m; ++j) { n       D[i][j] = D[i + 1][j]; n       if (a[i][j] < a[i + 1][j]) n         D[i][j] += tu; n       else if (a[i][j] == a[i + 1][j]) n         D[i][j] += tp; n       else n         D[i][j] += td; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = m - 1; j >= 1; --j) { n       R[i][j] = R[i][j + 1]; n       if (a[i][j] > a[i][j + 1]) n         R[i][j] += tu; n       else if (a[i][j] == a[i][j + 1]) n         R[i][j] += tp; n       else n         R[i][j] += td; n     } n   } n   for (int lx = 1; lx <= n; ++lx) n     for (int rx = lx + 3 - 1; rx <= n; ++rx) { n       hh.clear(); n       int head = 1; n       int tot = 0, now; n       for (int ry = 1; ry <= m; ++ry) { n         if (ry != 1) { n           tot += f(a[lx][ry - 1], a[lx][ry]); n           tot += f(a[rx][ry], a[rx][ry - 1]); n         } n         now = tot + D[lx][ry] - D[rx][ry]; n         if (!hh.empty()) { n           static set<pair<int, int> >::iterator iter, liter, riter; n           iter = hh.lower_bound(make_pair(t - now, 0)); n           liter = iter, riter = iter; n           if (liter != hh.begin()) liter--; n           if (riter != hh.end()) riter++; n           if (iter != hh.end()) { n             pair<int, int> me = *iter; n             upd(lx, rx, me.second, ry, now + me.first); n           } n           if (liter != hh.end()) { n             pair<int, int> me = *liter; n             upd(lx, rx, me.second, ry, now + me.first); n           } n           if (riter != hh.end()) { n             pair<int, int> me = *riter; n             upd(lx, rx, me.second, ry, now + me.first); n           } n         } n         while (ry + 1 - head + 1 >= 3) { n           pair<int, int> me; n           me.first = 0; n           me.second = head; n           me.first += U[rx][head] - U[lx][head]; n           me.first -= L[lx][head]; n           me.first -= R[rx][1] - R[rx][head]; n           hh.insert(me); n           ++head; n         } n       } n     } n   cout << ans.lx <<     << ans.ly <<     << ans.rx <<     << ans.ry << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n struct Node { n   int t[26], len, f, tf, d, s; n } a[1000500]; n int tn, las; n void linkd(int u) { n   int fa = a[u].f; n   a[u].d = a[u].len - a[fa].len; n   a[u].tf = (a[u].d == a[fa].d && a[u].d * 2 <= a[u].len) ? a[fa].tf : u; n } n void ins(int k, int c, char *str) { n   int p = las; n   while (str[k - a[p].len - 1] != c) p = a[p].f; n   if (!a[p].t[c]) { n     int np = las = ++tn, v; n     for (v = a[p].f; str[k - a[v].len - 1] != c; v = a[v].f) n       ; n     if (!a[v].t[c]) n       a[np].f = 2; n     else n       a[np].f = a[v].t[c]; n     a[a[p].t[c] = np].len = a[p].len + 2; n     linkd(np); n   } else n     las = a[p].t[c]; n } n int f[1000500]; n void dp(int k) { n   for (int p = las; p > 2; p = a[a[p].tf].f) { n     int tl = a[a[p].tf].len; n     a[p].s = f[k - tl]; n     if (a[p].tf != p) a[p].s = (a[a[p].f].s + a[p].s) % mod; n     if (!(k & 1)) f[k] = (f[k] + a[p].s) % mod; n   } n } n void Init() { n   a[1].f = a[2].f = 1; n   a[1].len = -1; n   las = tn = 2; n } n int n; n char s[1000500], s2[1000500]; n int main() { n   scanf( %s , s2 + 1); n   n = strlen(s2 + 1); n   if (n & 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 1; i + i <= n; i++) { n     s[i * 2 - 1] = s2[n - i + 1]; n     s[i * 2] = s2[i]; n   } n   s[0] = -1; n   f[0] = 1; n   Init(); n   for (int i = 1; i <= n; i++) { n     ins(i, s[i] -=  a , s); n     dp(i); n   } n   printf( %d , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1003], d, u, e; n vector<int> v1, v2, v3; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] < 0) v1.push_back(a[i]); n     if (a[i] > 0) v2.push_back(a[i]); n     if (a[i] == 0) v3.push_back(a[i]); n   } n   if (v2.size() == 0) { n     v2.push_back(v1[0]); n     v2.push_back(v1[1]); n     v1[0] = -1003; n     v1[1] = -1003; n     d += 2; n   } n   if (v1.size() % 2 == 0) { n     v3.push_back(v1[v1.size() - 1]); n     v1[v1.size() - 1] = -1003; n     d++; n   } n   cout << v1.size() - d <<    ; n   for (int i = 0; i < v1.size(); i++) n     if (v1[i] != -1003) cout << v1[i] <<    ; n   cout << endl; n   cout << v2.size() <<    ; n   for (int i = 0; i < v2.size(); i++) n     if (v2[i] != -1003) cout << v2[i] <<    ; n   cout << endl; n   cout << v3.size() <<    ; n   for (int i = 0; i < v3.size(); i++) n     if (v3[i] != -1003) cout << v3[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n const int sz = 1 << 15; n char inbuf[sz], outbuf[sz]; n char *pinbuf = inbuf + sz; n char *poutbuf = outbuf; n inline char _getchar() { n   if (pinbuf == inbuf + sz) fread(inbuf, 1, sz, stdin), pinbuf = inbuf; n   return *(pinbuf++); n } n inline void _putchar(char x) { n   if (poutbuf == outbuf + sz) fwrite(outbuf, 1, sz, stdout), poutbuf = outbuf; n   *(poutbuf++) = x; n } n inline void flush() { n   if (poutbuf != outbuf) n     fwrite(outbuf, 1, poutbuf - outbuf, stdout), poutbuf = outbuf; n } n }  // namespace IO n inline int read() { n   int x = 0, p = 1; n   char c = IO::_getchar(); n   while (c <= 32) c = IO::_getchar(); n   if (c == 45) p = -p, c = IO::_getchar(); n   while (c > 32) x = x * 10 + c - 48, c = IO::_getchar(); n   return x * p; n } n namespace Mymath { n long long qp(long long x, long long p, long long mod) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = ans * x % mod; n     x = x * x % mod; n     p >>= 1; n   } n   return ans; n } n long long inv(long long x, long long mod) { return qp(x, mod - 2, mod); } n long long C(long long N, long long K, long long fact[], long long mod) { n   return fact[N] * inv(fact[K], mod) % mod * inv(fact[N - K], mod) % mod; n } n template <typename Tp> n Tp gcd(Tp A, Tp B) { n   if (B == 0) return A; n   return gcd(B, A % B); n } n template <typename Tp> n Tp lcm(Tp A, Tp B) { n   return A * B / gcd(A, B); n } n };  // namespace Mymath n namespace fwt { n using namespace Mymath; n void FWT(int a[], int n, long long mod) { n   for (int d = 1; d < n; d <<= 1) n     for (int m = d << 1, i = 0; i < n; i += m) n       for (int j = 0; j < d; j++) { n         int x = a[i + j], y = a[i + j + d]; n         a[i + j] = (x + y) % mod, a[i + j + d] = (x - y + mod) % mod; n       } n } n void UFWT(int a[], int n, long long mod) { n   long long rev = inv(2, mod); n   for (int d = 1; d < n; d <<= 1) n     for (int m = d << 1, i = 0; i < n; i += m) n       for (int j = 0; j < d; j++) { n         int x = a[i + j], y = a[i + j + d]; n         a[i + j] = 1LL * (x + y) * rev % mod, n               a[i + j + d] = (1LL * (x - y) * rev % mod + mod) % mod; n       } n } n void solve(int a[], int b[], int n, long long mod) { n   FWT(a, n, mod); n   FWT(b, n, mod); n   for (int i = 0; i < n; i++) a[i] = 1LL * a[i] * b[i] % mod; n   UFWT(a, n, mod); n } n };  // namespace fwt n namespace Maxflow { n const int Maxn = 1005; n struct edge { n   int to, rev, cap; n   edge(int _to, int _rev, int _cap) { to = _to, rev = _rev, cap = _cap; } n }; n int src = 1002, snk = 1003; n vector<edge> G[Maxn]; n int dist[Maxn], seen[Maxn]; n int n; n void Clear() { n   for (int i = 0; i < Maxn; i++) G[i].clear(); n } n void bfs(int s) { n   for (int i = 0; i < Maxn; i++) dist[i] = -1; n   dist[s] = 0; n   queue<int> Q; n   Q.push(s); n   while (!Q.empty()) { n     int x = Q.front(); n     Q.pop(); n     for (int i = 0; i < G[x].size(); i++) { n       edge &e = G[x][i]; n       if (e.cap && dist[e.to] == -1) { n         dist[e.to] = dist[x] + 1; n         Q.push(e.to); n       } n     } n   } n } n int dfs(int now, int dest, int fl) { n   if (now == dest) return fl; n   for (int &i = seen[now]; i < G[now].size(); i++) { n     edge &ed = G[now][i]; n     if (ed.cap && dist[ed.to] == dist[now] + 1) { n       int f = dfs(ed.to, dest, min(fl, ed.cap)); n       if (f) { n         ed.cap -= f; n         G[ed.to][ed.rev].cap += f; n         return f; n       } n     } n   } n   return 0; n } n int dinic(int s = src, int t = snk) { n   int ret = 0; n   while (1) { n     memset(seen, 0, sizeof(seen)); n     bfs(s); n     if (dist[t] == -1) return ret; n     int f = dfs(s, t, 2147483647); n     while (f) { n       ret += f; n       f = dfs(s, t, 2147483647); n     } n   } n } n };  // namespace Maxflow n namespace Geometry { n struct iP { n   long long x, y; n   iP(long long _x, long long _y) { x = _x, y = _y; } n   iP() { x = y = 0; } n   iP operator+(iP p) { return iP(x + p.x, y + p.y); } n   iP operator-(iP p) { return iP(x - p.x, y - p.y); } n   iP operator*(long long k) { return iP(x * k, y * k); } n   long long det(iP p) { return x * p.y - y * p.x; } n   long long dot(iP p) { return x * p.x + y * p.y; } n   long long dist() { return x * x + y * y; } n   bool operator<(const iP &p) const { n     if (x != p.x) return x < p.x; n     return y < p.y; n   } n }; n struct dP { n   double x, y; n   dP(double _x, double _y) { x = _x, y = _y; } n   dP() { x = y = 0.0; } n   dP operator+(dP p) { return dP(x + p.x, y + p.y); } n   dP operator-(dP p) { return dP(x - p.x, y - p.y); } n   dP operator*(double k) { return dP(x * k, y * k); } n   double det(dP p) { return x * p.y - y * p.x; } n   double dot(dP p) { return x * p.x + y * p.y; } n   double dist() { return x * x + y * y; } n   bool operator<(const dP &p) const { n     if (x != p.x) return x < p.x; n     return y < p.y; n   } n }; n vector<iP> convex_hull(iP X[], int l) { n   vector<iP> ret; n   sort(X + 1, X + 1 + l); n   ret.push_back(X[1]); n   for (int i = 2; i <= l; i++) { n     while (ret.size() >= 2 && n            (ret[ret.size() - 2] - X[i]).det(ret[ret.size() - 1] - X[i]) >= 0) { n       ret.pop_back(); n     } n     ret.push_back(X[i]); n   } n   int rs = ret.size() + 1; n   for (int i = l - 1; i >= 1; i--) { n     while (ret.size() >= rs && n            (ret[ret.size() - 2] - X[i]).det(ret[ret.size() - 1] - X[i]) >= 0) { n       ret.pop_back(); n     } n     ret.push_back(X[i]); n   } n   return ret; n } n };  // namespace Geometry n int main() { n   int n, m; n   n = read(); n   m = read(); n   if (n > m) swap(n, m); n   long long tot = 1ll * n * m; n   if (n == 1) { n     m = m % 6; n     if (m == 0) { n       cout << tot / 2 * 2 << endl; n     } else if (m == 1) { n       cout << tot / 2 * 2 << endl; n     } else if (m == 2) { n       cout << (tot / 2 - 1) * 2 << endl; n     } else if (m == 3) { n       cout << (tot / 2 - 1) * 2 << endl; n     } else if (m == 4) { n       cout << (tot / 2 - 1) * 2 << endl; n     } else if (m == 5) { n       cout << (tot / 2) * 2 << endl; n     } n   } else if (n == 2) { n     if (m == 2) { n       cout << 0 << endl; n       return 0; n     } n     if (m == 3) { n       cout << 4 << endl; n       return 0; n     } n     if (m == 7) { n       cout << 12 << endl; n       return 0; n     } n     cout << tot << endl; n     return 0; n   } else { n     cout << tot / 2 * 2 << endl; n   } n   return ~~(0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, i, j, k, a, b; n   scanf( %d , &a); n   if (a == 1) n     printf( 1 n ); n   else { n     printf( %d n , (a - 1) * a / 2 * 12 + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n void solve() { n   long long n; n   cin >> n; n   map<long long, long long> m; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     m[x]++; n   } n   long long mx = 0; n   long long ans = 0; n   for (auto it : m) { n     ans += min(mx, it.second); n     mx = max(mx, it.second); n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(pair<long long int, char> p1, pair<long long int, char> p2) { n   return p1.first > p2.first; n } n vector<vector<long long int> > graph; n vector<bool> vis; n vector<vector<long long int> > level; n vector<long long int> d, childp; n void dfs(long long int node, long long int prev) { n   vis[node] = true; n   d[node] = prev + 1; n   long long int temp = 1, i; n   long long int x = 0, k = 1; n   for (i = 0; i < graph[node].size(); i++) { n     if (vis[graph[node][i]] == false) { n       dfs(graph[node][i], d[node]); n       x += childp[graph[node][i]]; n     } n   } n   childp[node] = x + 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, i, p; n   cin >> n; n   long double ans[n + 1]; n   graph.resize(n + 1); n   level.resize(n + 1); n   vis.resize(n + 1); n   d.resize(n + 1); n   childp.resize(n + 1); n   for (i = 1; i <= n; i++) { n     vis[i] = false; n     d[i] = 0; n     ans[i] = 0.0; n     childp[i] = 0; n   } n   for (i = 2; i <= n; i++) { n     cin >> p; n     graph[p].push_back(i); n     graph[i].push_back(p); n   } n   dfs(1, 0); n   for (i = 1; i <= n; i++) n     printf( %0.8lf  , ((n - childp[i] - d[i] + 1) / 2.0) + d[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x7fffffff; n long long a[510][510], n; n int main() { n   while (~scanf( %I64d , &n)) { n     if (n == 1) { n       scanf( %I64d , &n); n       printf( 1 n ); n       continue; n     } n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) scanf( %I64d , &a[i][j]); n     } n     int zr = 0, zc; n     for (int i = 1; i <= n && !zr; ++i) { n       for (int j = 1; j <= n && !zr; ++j) { n         if (a[i][j] == 0) { n           zr = i; n           zc = j; n         } n       } n     } n     long long zv, sum = 0, zsum = 0; n     for (int i = 1; i <= n; ++i) zsum += a[zr][i]; n     if (zr == 1) { n       for (int i = 1; i <= n; ++i) sum += a[n][i]; n       zv = sum - zsum; n     } else { n       for (int i = 1; i <= n; ++i) sum += a[1][i]; n       zv = sum - zsum; n     } n     if (zv <= 0) { n       printf( -1 n ); n       continue; n     } n     a[zr][zc] = zv; n     bool ok = true; n     for (int i = 1; i <= n; ++i) { n       long long tmpsum = 0; n       for (int j = 1; j <= n; ++j) tmpsum += a[i][j]; n       if (tmpsum != sum) { n         ok = false; n         break; n       } n       tmpsum = 0; n       for (int j = 1; j <= n; ++j) tmpsum += a[j][i]; n       if (tmpsum != sum) { n         ok = false; n         break; n       } n     } n     long long tmpsum = 0; n     for (int i = 1; i <= n; ++i) tmpsum += a[i][i]; n     if (tmpsum != sum) ok = false; n     tmpsum = 0; n     for (int i = n; i >= 1; --i) tmpsum += a[i][n + 1 - i]; n     if (tmpsum != sum) ok = false; n     if (ok) { n       printf( %I64d n , zv); n     } else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 105; n int n, m, i, j, k, s[N], h[N], id[N], a[N], l[N], r[N]; n int fr[N], fl[N]; n bool cmp(int x, int y) { return s[x] < s[y]; } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; ++i) scanf( %d%d , s + i, h + i), id[i] = i; n   if (m == 1) { n     printf( %d n1 n , s[1]); n     return 0; n   } n   std::sort(id + 1, id + m + 1, cmp); n   for (i = 1; i <= n; ++i) scanf( %d , a + i); n   for (i = 1; i <= m; ++i) { n     long long ss; n     ss = h[i]; n     for (l[i] = s[i]; l[i] && ss >= 0; ss += a[--l[i]]) n       ; n     ++l[i]; n     ss = h[i]; n     for (r[i] = s[i]; r[i] <= n && ss >= 0; ss += a[++r[i]]) n       ; n     --r[i]; n   } n   fr[m] = m; n   for (i = m - 1; i; --i) { n     for (j = i + 1; j <= m; ++j) n       if (fr[j] && l[id[j]] <= s[id[i]]) fr[i] = j; n   } n   fl[1] = 1; n   for (i = 2; i <= m; ++i) { n     for (j = 1; j < i; ++j) n       if (fl[j] && r[id[j]] >= s[id[i]]) fl[i] = j; n   } n   if (fr[1]) { n     static bool bb[N]; n     static int st[N]; n     int w = 0; n     printf( %d n , s[id[1]]); n     for (j = 1; j < m; j = fr[j]) st[++w] = id[j]; n     st[++w] = id[m]; n     std::reverse(st + 1, st + w + 1); n     for (; w; --w) bb[st[w]] = 1, printf( %d  , st[w]); n     for (j = 1; j <= m; ++j) n       if (!bb[j]) printf( %d  , j); n     exit(0); n   } n   if (fl[m]) { n     static bool bb[N]; n     static int st[N]; n     int w = 0; n     printf( %d n , s[id[m]]); n     for (j = m; j > 1; j = fl[j]) st[++w] = id[j]; n     st[++w] = id[1]; n     std::reverse(st + 1, st + w + 1); n     for (; w; --w) bb[st[w]] = 1, printf( %d  , st[w]); n     for (j = 1; j <= m; ++j) n       if (!bb[j]) printf( %d  , j); n     exit(0); n   } n   for (i = 1; i < m; ++i) n     for (k = i + 1; k <= m; ++k) n       if (fl[i] && fr[k] && r[id[i]] + 1 >= l[id[k]]) { n         printf( %d n , l[id[k]]); n         static int st[N]; n         int w = 0; n         static bool bb[N]; n         for (j = k; j < m; j = fr[j]) st[++w] = id[j]; n         st[++w] = id[m]; n         std::reverse(st + 1, st + w + 1); n         for (; w; --w) bb[st[w]] = 1, printf( %d  , st[w]); n         w = 0; n         for (j = i; j > 1; j = fl[j]) st[++w] = id[j]; n         st[++w] = id[1]; n         std::reverse(st + 1, st + w + 1); n         for (; w; --w) bb[st[w]] = 1, printf( %d  , st[w]); n         for (j = 1; j <= m; ++j) n           if (!bb[j]) printf( %d  , j); n         exit(0); n       } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[111]; n int in[111]; n long long n, s, now, t; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     s += ar[i]; n     in[i] = i; n   } n   if (s < n - 1 || ar[0] == 0) { n     cout << -1; n     return 0; n   } n   for (int ii = 1; ii < n - 1; ii++) n     for (int i = 1; i < n - 1; i++) n       if (ar[in[i]] < ar[in[i + 1]]) swap(in[i], in[i + 1]); n   cout << n - 1; n   s = 0; n   now = 1; n   while (now < n) { n     while (ar[in[s]] > 0 && now < n) { n       cout << endl << in[s] + 1 <<     << in[now++] + 1; n       ar[in[s]]--; n     } n     s++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 6; n const long long M = 1e9 + 1; n char mat[105][105]; n void solve() { n   long long n; n   cin >> n; n   long long row[n]; n   vector<pair<long long, long long> > vecr; n   vector<pair<long long, long long> > vecc; n   long long col[n]; n   memset(row, 0, sizeof(row)); n   memset(col, 0, sizeof(col)); n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       cin >> mat[i][j]; n       if (mat[i][j] ==  . ) { n         if (row[i] != 1) vecr.push_back({i, j}); n         row[i] = 1; n         if (col[j] != 1) vecc.push_back({i, j}); n         col[j] = 1; n       } n     } n   } n   bool rowok = true; n   bool colok = true; n   for (long long i = 0; i < n; i++) { n     if (row[i] == 0) rowok = false; n     if (col[i] == 0) colok = false; n   } n   if (!rowok && !colok) n     cout << -1; n   else if (rowok) { n     for (long long i = 0; i < vecr.size(); i++) n       cout << vecr[i].first + 1 <<     << vecr[i].second + 1 <<   n ; n   } else if (colok) { n     for (long long i = 0; i < vecc.size(); i++) n       cout << vecc[i].first + 1 <<     << vecc[i].second + 1 <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1000000007; n bool check(int i, int j, int k) { n   return (i + j > k && j + k > i && j + i > j); n } n int main() { n   int n, ct = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       int k = i ^ j; n       if (k <= n && k >= j && check(i, j, k)) ct++; n     } n   } n   cout << ct << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int number_of_usb; n int file_size; n int usb_required = 0; n int summing = 0; n int temp; n int main() { n   cin >> number_of_usb; n   cin >> file_size; n   int usb_sizes[number_of_usb]; n   for (int i = 0; i < number_of_usb; i++) { n     cin >> usb_sizes[i]; n   } n   for (int i = 0; i < number_of_usb; i++) { n     for (int j = 0; j < number_of_usb; j++) { n       if (usb_sizes[i] > usb_sizes[j]) { n         temp = usb_sizes[j]; n         usb_sizes[j] = usb_sizes[i]; n         usb_sizes[i] = temp; n       } n     } n   } n   for (int i = 0; i < number_of_usb; i++) { n     if (summing < file_size) { n       usb_required++; n     } n     summing += usb_sizes[i]; n   } n   cout << usb_required; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n const int MOD = 1e4 + 7; n int n, m, q; n int main() { n   scanf( %d , &q); n   while (q--) { n     int a, b; n     scanf( %d%d , &a, &b); n     int delt = abs(a - b); n     int ans = delt / 10 + (delt % 10 != 0); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, tmp; n bool flag = false; n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     scanf( %d , &tmp); n     if (tmp == 1) flag = true; n   } n   if (flag) n     printf( %d , -1); n   else n     printf( %d , 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n const long long int p = 27; n string s; n long long int hashnum(int lw, int up) { n   if ((up - lw) % 2 == 0) n     return (((hashnum(lw, (lw + up) / 2) * p) % mod + 1) * n             ((hashnum((lw + up) / 2, up) * p) % mod + 1)) % n            mod; n   long long int res = 0; n   for (int i = lw; i < up; i++) { n     res *= p; n     res %= mod; n     res += (s[i] -  a ); n     res %= mod; n   } n   return res; n } n int main() { n   cin >> s; n   long long int tmp = hashnum(0, s.length()); n   cin >> s; n   long long int tmp1 = hashnum(0, s.length()); n   if (tmp == tmp1) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n } a[200005]; n bool cmp(struct point A, struct point B) { n   if (A.x != B.x) return A.x < B.x; n   return A.y < B.y; n } n int m; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i].x); n     a[i].y = i + 1; n   } n   int num = 0; n   for (int i = n; i < 2 * n; i++) { n     scanf( %d , &a[i].x); n     a[i].y = i + 1 - n; n     if (a[i].x == a[i - n].x) num++; n   } n   n *= 2; n   a[n].x = 1e9 + 1; n   n++; n   sort(a, a + n, cmp); n   cin >> m; n   long long ans = 1; n   int count = 1; n   for (int i = 1; i < n; i++) { n     if (a[i].x == a[i - 1].x) { n       count++; n       ans = (ans * count); n       while (num && ans % 2 == 0) { n         ans /= 2; n         num--; n       } n       ans %= m; n     } else n       count = 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 2; n int tree[N * 32][2], cnt[N * 32]; n long long int curr = 1, n, ans; n vector<long long int> arr(N); n void clean() { n   for (long long int i = 0; i <= N * 32 - 1; i++) tree[i][0] = tree[i][1] = -1; n } n void add(long long int x) { n   long long int v = 0; n   cnt[v]++; n   for (long long int i = 32; i >= 0; i--) { n     long long int b = (x >> i) & 1; n     if (tree[v][b] == -1) tree[v][b] = curr++; n     v = tree[v][b]; n     cnt[v]++; n   } n } n long long int calc(int v) { n   if (v == -1) return 0; n   int s0 = 0, s1 = 0; n   if (tree[v][0] != -1) s0 = cnt[tree[v][0]]; n   if (tree[v][1] != -1) s1 = cnt[tree[v][1]]; n   if (s0 == 0 && s1 == 0) return cnt[v]; n   if (s0 == 0) return calc(tree[v][1]); n   if (s1 == 0) return calc(tree[v][0]); n   return 1 + max(calc(tree[v][0]), calc(tree[v][1])); n } n int main() { n   clean(); n   cin >> n; n   for (long long int i = 0; i <= n - 1; i++) cin >> arr[i], add(arr[i]); n   cout << n - calc(0); n } n