#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30); n int dp[110][700][40], g[35][35]; n string s; n int solve(int p, int k, int q) { n   if (p < 0 && k >= 0) return 0; n   if (p < 0 || k < 0) return -inf; n   if (dp[p][k][q] != -1) return dp[p][k][q]; n   char ch = q +  a ; n   int ans = -inf; n   for (char curr =  a ; curr <=  z ; curr++) { n     int dk = 0; n     if (curr != s[p]) dk++; n     int x = solve(p - 1, k - dk, curr -  a ); n     if (x == -inf) continue; n     ans = max(ans, x + g[curr -  a ][q]); n   } n   return dp[p][k][q] = ans; n } n int main() { n   fill(&dp[0][0][0], &dp[0][0][0] + 110 * 700 * 40, -1); n   int k; n   cin >> s >> k; n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     string tmp; n     char c1, c2; n     int w; n     cin >> c1 >> c2 >> w; n     g[c1 -  a ][c2 -  a ] = w; n   } n   cout << solve(s.size() - 1, k, 30); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   vector<pair<int, int>> t(N), b(N); n   for (int i = 0; i < N; i++) n     cin >> b[i].first >> b[i].second >> t[i].first >> t[i].second; n   multiset<int> tm[2], bm[2]; n   for (int i = 0; i < N; i++) { n     tm[0].insert(t[i].first); n     tm[1].insert(t[i].second); n     bm[0].insert(-b[i].first); n     bm[1].insert(-b[i].second); n   } n   for (int i = 0; i < N; i++) { n     tm[0].erase(tm[0].find(t[i].first)); n     tm[1].erase(tm[1].find(t[i].second)); n     bm[0].erase(bm[0].find(-b[i].first)); n     bm[1].erase(bm[1].find(-b[i].second)); n     pair<int, int> tp = make_pair(*tm[0].begin(), *tm[1].begin()); n     pair<int, int> bp = make_pair(-*bm[0].begin(), -*bm[1].begin()); n     if (tp.first >= bp.first && tp.second >= bp.second) { n       cout << tp.first <<     << tp.second << endl; n       return 0; n     } n     tm[0].insert(t[i].first); n     tm[1].insert(t[i].second); n     bm[0].insert(-b[i].first); n     bm[1].insert(-b[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int count = 0; n   char k[4]; n   cin >> k; n   int a[5]; n   for (int i = 0; i < 4; i++) { n     a[i] = k[i] -  0 ; n   } n   int b[5]; n   for (int i = 0; i < 4; i++) { n     b[i] = a[i]; n   } n   while (1) { n     if (b[3] == 9) { n       b[3] = 0; n       if (b[2] == 9) { n         b[2] = 0; n         if (b[1] == 9) { n           b[1] = 0; n           b[0] = b[0] + 1; n         } else n           b[1] = b[1] + 1; n       } else n         b[2] = b[2] + 1; n     } else n       b[3] = b[3] + 1; n     int i = 0; n     for (int v = 0; v < 4; v++) n       for (int z = v + 1; z < 4; z++) { n         if (b[v] == b[z]) count++; n       } n     if (count == 0) n       break; n     else n       count = 0; n   } n   for (int m = 0; m < 4; m++) { n     cout << b[m]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long INF = 1e18; n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long nCr(long long n, long long r) { n   long long res = 1; n   if (n < r) return -1; n   r = min(r, n - r); n   for (long long i = 0; i < r; i++) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n long long nPr(long long n, long long r) { n   long long res = 1; n   if (n < r) return -1; n   for (long long i = 0; i < r; i++) res *= (n - i); n   return res; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> a(n), b(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n   } n   sort(b.begin(), b.end()); n   long long ans = INF; n   for (long long i = 0; i < n; i++) { n     long long x = (b[i] - a[0] + m) % m; n     vector<long long> v(n); n     for (long long j = 0; j < n; j++) v[j] = (a[j] + x) % m; n     sort(v.begin(), v.end()); n     if (v == b) ans = min(ans, x); n   } n   cout << ans; n } n signed 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   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 1e6 + 4; n long long fac[N], infac[N]; n long long me(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return res; n } n long long mi(long long n) { return me(n, mod - 2); } n void factorial() { n   fac[0] = 1; n   for (long long i = 1; i < N; i++) fac[i] = (i * fac[i - 1]) % mod; n   infac[N - 1] = mi(fac[N - 1]); n   for (long long i = N - 1; i > 0; i--) infac[i - 1] = (i * infac[i]) % mod; n } n long long ncr(long long n, long long r) { n   if (n < r) return 0; n   long long res = fac[n]; n   res = (res * infac[r]) % mod; n   res = (res * infac[n - r]) % mod; n   return res; n } n int main() { n   factorial(); n   long long n, m; n   scanf( %lld , &n); n   scanf( %lld , &m); n   long long ans = me(m, n); n   for (long long s = 1; s <= n; s++) { n     long long val = me(m, s), temp = me(m - 1, n - s); n     val = (val * temp) % mod; n     val = (val * ncr(n, s - 1)); n     ans = (ans + val) % mod; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const long long INFLL = 0x3f3f3f3f3f3f3f3f; n const int maxn = 1e6 + 10; n struct Edge { n   int to, nxt; n   long long cost; n   Edge() {} n   Edge(int to, int nxt, long long cost) : to(to), nxt(nxt), cost(cost) {} n } edge[maxn << 1]; n int head[maxn], tot; n long long dp[maxn]; n void Init(int n) { n   tot = 0; n   for (int i = 1; i <= n; ++i) head[i] = -1; n } n void addedge(int u, int v, long long cost) { n   edge[tot] = Edge(v, head[u], cost); n   head[u] = tot++; n   edge[tot] = Edge(u, head[v], cost); n   head[v] = tot++; n } n int n; n long long val[maxn]; n long long ans; n void DFS(int u, int pre) { n   dp[u] = val[u]; n   long long max1 = 0, max2 = 0; n   for (int i = head[u]; ~i; i = edge[i].nxt) { n     int v = edge[i].to; n     long long cost = edge[i].cost; n     if (v == pre) continue; n     DFS(v, u); n     long long tmp = dp[v] - cost; n     if (tmp > max1) swap(max1, tmp); n     max2 = max(tmp, max2); n     dp[u] = max(dp[u], dp[v] - cost + val[u]); n   } n   ans = max(ans, max(dp[u], max1 + max2 + val[u])); n } n void RUN() { n   while (~scanf( %d , &n)) { n     Init(n); n     for (int i = 1; i <= n; ++i) scanf( %lld , val + i); n     long long w; n     for (int i = 1, u, v; i < n; ++i) { n       scanf( %d %d %lld , &u, &v, &w); n       addedge(u, v, w); n     } n     ans = 0; n     DFS(1, -1); n     printf( %lld n , ans); n   } n } n int main() { n   RUN(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 400000 + 1000; n struct node { n   int l, r, dir; n   long long w; n } temp; n vector<node> v; n long long vis[N]; n bool cmp(node a, node b) { n   if (a.l == b.l) return a.dir < b.dir; n   return a.l < b.l; n } n int main() { n   int n, x, i, l, w, r, time; n   long long ans; n   memset(vis, 0, sizeof(vis)); n   ans = 1e11; n   scanf( %d%d , &n, &x); n   for (i = 0; i < n; i++) { n     scanf( %d%d%d , &l, &r, &w); n     temp.w = w; n     temp.l = l; n     temp.r = r; n     temp.dir = -1; n     v.push_back(temp); n     temp.l = r; n     temp.r = l; n     temp.dir = 1; n     v.push_back(temp); n   } n   sort(v.begin(), v.end(), cmp); n   for (i = 0; i < v.size(); i++) { n     time = abs(v[i].r - v[i].l) + 1; n     if (time >= x) continue; n     if (v[i].dir == -1) { n       if (vis[x - time] != 0) ans = min(ans, vis[x - time] + v[i].w); n     } else { n       if (!vis[time]) n         vis[time] = v[i].w; n       else n         vis[time] = min(vis[time], v[i].w); n     } n   } n   if (ans == 1e11) n     printf( -1 n ); n   else n     printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, m, k, u, v, c[200005] = {}, d[200005] = {}, z = 0; n   vector<int> g[200005] = {}; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 0; i < m; i++) n     cin >> u >> v, g[u].push_back(v), g[v].push_back(u); n   for (int i = 1; i <= n; i++) { n     if (d[i]) continue; n     d[i] = 1; n     queue<int> q; n     vector<int> w = {i}; n     q.push(i); n     while (!q.empty()) { n       int x = q.front(); n       q.pop(); n       for (int j = 0; j < g[x].size(); j++) { n         if (!d[g[x][j]]) d[g[x][j]] = 1, q.push(g[x][j]), w.push_back(g[x][j]); n       } n     } n     unordered_map<int, int> p; n     int o = 0; n     for (int j = 0; j < w.size(); j++) p[c[w[j]]]++, o = max(o, p[c[w[j]]]); n     z += w.size() - o; n   } n   cout << z; n } n


#include <bits/stdc++.h> n using namespace std; n int p = 0, cnt = 0, cplus, cminus, val[10005], Child[10005]; n pair<int, int> F[10005][105], FF[105]; n string s; n vector<int> g[10005]; n void DFS(int u) { n   while (p < s.size()) { n     p++; n     if (s[p - 1] ==  ( ) { n       g[u].push_back(++cnt); n       DFS(cnt); n     } else if ( 0  <= s[p - 1] && s[p - 1] <=  9 ) { n       val[++cnt] = s[p - 1] -  0 ; n       g[u].push_back(cnt); n     } else if (s[p - 1] ==  ) ) n       break; n   } n } n void FinalDFS(int u, int root) { n   for (int i = 0; i <= 100; i++) F[u][i] = pair<int, int>(1e9, -1e9); n   if (g[u].size() == 0) { n     F[u][0] = pair<int, int>(val[u], val[u]); n     return; n   } n   for (int i = 0; i < g[u].size(); i++) n     if (g[u][i] != root) FinalDFS(g[u][i], u); n   for (int i = 0; i <= 100; i++) F[u][i] = F[g[u][0]][i]; n   for (int i = 1; i < g[u].size(); i++) { n     int v = g[u][i]; n     for (int j = 0; j <= 100; j++) FF[j] = pair<int, int>(1e9, -1e9); n     for (int j = 0; j <= 100; j++) n       for (int jj = 0; jj <= j; jj++) { n         if (jj < j) { n           if (cplus <= 100) { n             FF[j].first = n                 min(FF[j].first, F[u][j - jj - 1].first + F[v][jj].first); n             FF[j].second = n                 max(FF[j].second, F[u][j - jj - 1].second + F[v][jj].second); n           } else { n             FF[j].first = n                 min(FF[j].first, F[u][j - jj - 1].first - F[v][jj].second); n             FF[j].second = n                 max(FF[j].second, F[u][j - jj - 1].second - F[v][jj].first); n           } n         } n         if (cplus <= 100) { n           FF[j].first = min(FF[j].first, F[u][j - jj].first - F[v][jj].second); n           FF[j].second = n               max(FF[j].second, F[u][j - jj].second - F[v][jj].first); n         } else { n           FF[j].first = min(FF[j].first, F[u][j - jj].first + F[v][jj].first); n           FF[j].second = n               max(FF[j].second, F[u][j - jj].second + F[v][jj].second); n         } n       } n     for (int j = 0; j <= 100; j++) F[u][j] = FF[j]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s >> cplus >> cminus; n   DFS(0); n   FinalDFS(1, 1); n   if (cplus <= 100) n     cout << F[1][cplus].second; n   else n     cout << F[1][cminus].second; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n vector<pair<char, int> > v; n map<int, int> mp1, mp2; n int arr[105][105]; n int main() { n   int x, y, z, n = 0, m = 0, c = 0, lo = 2, mn = 1e9, mx = 0; n   char ch; n   cin >> x >> y; n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < y; j++) { n       cin >> arr[i][j]; n     } n   } n   if (y > x) { n     for (int i = 0; i < x; i++) { n       for (int j = 0; j < y; j++) { n         mn = min(mn, arr[i][j]); n       } n       if (mn != 0) { n         for (int k = 0; k < y; k++) { n           arr[i][k] -= mn; n         } n         while (mn--) { n           v.push_back(make_pair( r , i + 1)); n         } n       } n       mn = 1e9; n     } n     mn = 1e9; n     for (int i = 0; i < y; i++) { n       mn = 1e9; n       for (int j = 0; j < x; j++) { n         mn = min(mn, arr[j][i]); n       } n       if (mn != 0) { n         for (int k = 0; k < x; k++) { n           arr[k][i] -= mn; n         } n         while (mn--) { n           v.push_back(make_pair( c , i + 1)); n         } n       } n       mn = 1e9; n     } n   } else { n     mn = 1e9; n     for (int i = 0; i < y; i++) { n       mn = 1e9; n       for (int j = 0; j < x; j++) { n         mn = min(mn, arr[j][i]); n       } n       if (mn != 0) { n         for (int k = 0; k < x; k++) { n           arr[k][i] -= mn; n         } n         while (mn--) { n           v.push_back(make_pair( c , i + 1)); n         } n       } n       mn = 1e9; n     } n     for (int i = 0; i < x; i++) { n       for (int j = 0; j < y; j++) { n         mn = min(mn, arr[i][j]); n       } n       if (mn != 0) { n         for (int k = 0; k < y; k++) { n           arr[i][k] -= mn; n         } n         while (mn--) { n           v.push_back(make_pair( r , i + 1)); n         } n       } n       mn = 1e9; n     } n   } n   bool f = 0; n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < y; j++) { n       if (arr[i][j] != 0) { n         f = 1; n         i = x; n         break; n       } n     } n   } n   if (f == 1) { n     cout << -1 << endl; n   } else { n     cout << v.size() << endl; n     for (int i = 0; i < v.size(); i++) { n       if (v[i].first ==  r ) { n         cout <<  row  ; n       } else { n         cout <<  col  ; n       } n       cout << v[i].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[400001], b[400001]; n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   unsigned n; n   cin >> n; n   for (unsigned i = 1; i <= n / 2; ++i) cin >> b[i]; n   a[1] = 0; n   a[n] = b[1]; n   for (unsigned i = 2; i <= n / 2; ++i) { n     if (b[i] - a[i - 1] <= a[n - i + 2]) { n       a[i] = a[i - 1]; n       a[n - i + 1] = b[i] - a[i]; n     } else { n       a[n - i + 1] = a[n - i + 2]; n       a[i] = b[i] - a[n - i + 1]; n     } n   } n   for (unsigned i = 1; i <= n; ++i) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int edge[1005]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> edge[i]; n   } n   int sum = 0; n   int tmp1, tmp2; n   for (int i = 0; i < m; i++) { n     cin >> tmp1 >> tmp2; n     sum += min(edge[tmp1], edge[tmp2]); n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   scanf( %lld%lld , &n, &k); n   if (k > (long long)1e8) { n     printf( -1 n ); n     return 0; n   } n   long long b = n / (k * (k + 1) / 2); n   if (b == 0) { n     printf( -1 n ); n     return 0; n   } n   long long r = 1; n   for (long long x = 1; x * x <= n; x++) { n     if (n % x != 0) continue; n     if (x <= b && x > r) r = x; n     if (n / x <= b && n / x > r) r = n / x; n   } n   for (int i = 1; i < k; i++) printf( %lld  , r * i); n   n -= r * k * (k - 1) / 2; n   printf( %lld n , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 123; n int n, p[MAXN], s[MAXN], a[MAXN]; n vector<int> c[MAXN]; n void inp(); n void dfs(int); n int main() { n   inp(); n   a[0] = s[0]; n   dfs(0); n   for (int i = 0; i < n; i++) n     if (a[i] < 0) return cout << -1, 0; n   cout << accumulate(a, a + n, 0ll); n   return 0; n } n void inp() { n   cin >> n; n   p[0] = -1; n   for (int i = 1; i < n; i++) cin >> p[i], c[--p[i]].push_back(i); n   for (int i = 0; i < n; i++) cin >> s[i]; n } n void dfs(int v) { n   if (c[v].empty()) return; n   if (~s[v]) n     for (int u : c[v]) dfs(u); n   else { n     int kam = INT_MAX; n     for (int u : c[v]) kam = min(kam, s[u]); n     a[v] = kam - s[p[v]]; n     s[v] = s[p[v]] + a[v]; n     for (int u : c[v]) { n       a[u] = s[u] - s[v]; n       dfs(u); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double c = 0, l = 0, i, j; n   string x; n   cin >> x; n   if (x.size() == 1) { n     cout << x[0] << x[0]; n     return 0; n   } n   if (x.size() % 2 == 0) n     c = x.size() / 2 + 1; n   else n     c = x.size() / 2; n   string a, b, m; n   for (i = x.size() / 2; i >= 0; i--) { n     a.clear(); n     b.clear(); n     m.clear(); n     for (j = l + c; j < x.size(); j++) { n       if (x[i] != x[j]) { n         a = x; n         m = x; n         a.insert(x.size() - i, 1, x[i]); n         m.insert(x.size() - j - 1, 1, x[j]); n       } n       break; n     } n     l++; n     b = a; n     reverse(b.begin(), b.end()); n     if (a == b and a.size() != 0 and b.size() != 0) { n       cout << a; n       return 0; n     } n     b.clear(); n     b = m; n     reverse(b.begin(), b.end()); n     if (m == b and m.size() != 0 and b.size() != 0) { n       cout << m; n       return 0; n     } n   } n   l = 0; n   for (i = x.size() - 1 / 2; i >= 0; i--) { n     a.clear(); n     b.clear(); n     m.clear(); n     for (j = l + c; j < x.size(); j++) { n       if (x[i] != x[j]) { n         a = x; n         m = x; n         a.insert(x.size() - i, 1, x[i]); n         m.insert(x.size() - j - 1, 1, x[j]); n       } n       break; n     } n     l++; n     b = a; n     reverse(b.begin(), b.end()); n     if (a == b and a.size() != 0 and b.size() != 0) { n       cout << a; n       return 0; n     } n     b.clear(); n     b = m; n     reverse(b.begin(), b.end()); n     if (m == b and m.size() != 0 and b.size() != 0) { n       cout << m; n       return 0; n     } n   } n   a.clear(); n   b.clear(); n   if (x.size() % 2 == 0) n     x += x[0]; n   else n     x = x[x.size() - 1] + x; n   a = x; n   reverse(a.begin(), a.end()); n   if (a == x) n     cout << a; n   else n     cout <<  NA ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   int arr[14] = {4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777}; n   int i; n   for (i = 0; i < 14; i++) { n     if (a % arr[i] == 0) break; n   } n   if (i < 14) cout <<  YES ; n   if (i == 14) cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const double eps = 1e-10; n const int INF = 0x3f3f3f3f; n const long long INFLL = 0x3f3f3f3f3f3f3f3fll; n const int MAXN = 2e5 + 5; n const int mod = 1e9 + 7; n int a[MAXN], b[MAXN]; n long long fastpow(long long a, int n) { n   long long base = a, ret = 1; n   while (n) { n     if (n & 1) ret = ret * base % MOD; n     base = base * base % MOD; n     n >>= 1; n   } n   return ret; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   long long prob = 1, ans = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0) { n       if (b[i] == 0) n         ans = (ans + prob * (1 - fastpow(m, MOD - 2) + MOD) % MOD * n                          fastpow(2, MOD - 2) % MOD) % n               MOD; n       else n         ans = (ans + prob * (m - b[i]) % MOD * fastpow(m, MOD - 2) % MOD) % MOD; n       prob = prob * fastpow(m, MOD - 2) % MOD; n     } else { n       if (b[i] == 0) { n         ans = (ans + prob * (a[i] - 1) % MOD * fastpow(m, MOD - 2) % MOD) % MOD; n         prob = prob * fastpow(m, MOD - 2) % MOD; n       } else { n         if (b[i] > a[i]) n           break; n         else if (b[i] < a[i]) { n           ans = (ans + prob) % MOD; n           break; n         } n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int temp[100012], arr[100012], t[100012]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     t[i] = arr[i]; n   } n   sort(t, t + n); n   int v = 0; n   for (int i = 0; i < n; i++) { n     if (t[i] != arr[i]) { n       v = 1; n       break; n     } n   } n   if (v == 0) { n     cout << n << endl; n     return 0; n   } n   int c = 0; n   for (int i = 0; i < n; i++) { n     if (arr[i] == i) n       c++; n     else { n       temp[i] = arr[i]; n     } n   } n   int f = 0; n   for (int i = 0; i < n; i++) { n     if (arr[i] != i) { n       if (i == temp[temp[i]]) { n         c += 2; n         f = 1; n         break; n       } n     } n   } n   if (f == 0) c += 1; n   if (c > n) c = n; n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[100005], tree[100005], used[100005], l[100005], r[100005], n     d[100005], b[100005], query[100005], x, y, n, m, k; n template <class T> n inline T lowbit(T n) { n   return (n & -n); n } n void update(long long s[], int index, long long val) { n   while (index <= 100005) { n     s[index] += val; n     index += (index & (-index)); n   } n } n long long getS(long long s[], int l, int r) { n   if (l > r) n     return 0; n   else if (l == 1) { n     long long res = 0; n     for (; r > 0; r -= lowbit(r)) res += s[r]; n     return res; n   } else { n     return getS(s, 1, r) - getS(s, 1, l - 1); n   } n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> l[i] >> r[i] >> d[i]; n   } n   for (int i = 0; i < k; i++) { n     cin >> x >> y; n     update(tree, x, 1); n     update(tree, y + 1, -1); n   } n   for (int i = 1; i <= m; i++) { n     used[i] = getS(tree, 1, i); n   } n   for (int i = 0; i < m; i++) { n     update(b, l[i], d[i] * used[i + 1]); n     update(b, r[i] + 1, -d[i] * used[i + 1]); n   } n   for (int i = 0; i < n; i++) { n     query[i + 1] = getS(b, 1, i + 1); n   } n   for (int i = 1; i <= n; i++) { n     cout << arr[i - 1] + query[i]; n     if (i != n) { n       cout <<    ; n     } else { n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j; n   char t, w; n   cin >> n; n   cin >> t >> w; n   bool ok = true; n   if (t == w) ok = false; n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) { n       if (i == 0 && j < 2) continue; n       char x; n       cin >> x; n       if ((i == j || i + j == n - 1) && x != t) ok = false; n       if (!(i == j || i + j == n - 1) && x != w) ok = false; n     } n   if (ok) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int prime(int n) { n   int i; n   for (i = 3; i * i <= n; i += 2) { n     if (n % i == 0) return 0; n   } n   return 1; n } n int m, i; n int main() { n   scanf( %d , &m); n   if (m < 9) { n     printf( 1 n%d , m); n     return 0; n   } n   printf( 3 n3  ); n   m -= 3; n   if (m == 4) { n     printf( 2 2 ); n     return 0; n   } n   for (i = 3;; i += 2) { n     if (!prime(i)) continue; n     if (!prime(m - i)) continue; n     printf( %d %d , i, m - i); n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Order { n   char d; n   int p; n   int q; n }; n int main() { n   int n, s; n   Order a[1001]; n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     cin >> a[i].d >> a[i].p >> a[i].q; n   } n   map<int, int> bmp, smp; n   for (int i = 0; i < n; i++) { n     if (a[i].d ==  B ) { n       bmp[a[i].p] += a[i].q; n     } else { n       smp[a[i].p] += a[i].q; n     } n   } n   vector<pair<int, int> > bv; n   for (map<int, int>::iterator it = bmp.begin(); it != bmp.end(); it++) { n     bv.push_back(make_pair(it->first, it->second)); n   } n   vector<pair<int, int> > sv; n   for (map<int, int>::iterator it = smp.begin(); it != smp.end(); it++) { n     sv.push_back(make_pair(it->first, it->second)); n   } n   sort(bv.begin(), bv.end()); n   sort(sv.begin(), sv.end()); n   reverse(bv.begin(), bv.end()); n   int n1 = min(s, (int)bv.size()); n   int n2 = min(s, (int)sv.size()); n   vector<pair<int, int> > resv; n   for (int i = 0; i < n2; i++) { n     resv.push_back(sv[i]); n   } n   reverse(resv.begin(), resv.end()); n   for (int i = 0; i < n2; i++) { n     cout <<  S   << resv[i].first <<     << resv[i].second <<   n ; n   } n   for (int i = 0; i < n1; i++) { n     cout <<  B   << bv[i].first <<     << bv[i].second <<   n ; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define ll long long n #define pii pair<ll,ll> n #define debug(a) cout<<a<<  n  n #define maxn 200009 /// I wanna be withma youlu n #define MOD 1000000007 n #define F first n #define S second n #define rep(i, a, b) for(ll i = a; i < (b); ++i) n #define per(i, b, a) for(ll i = b-1; i>=a ; i--) n #define trav(a, x) for(auto& a : x) n #define allin(a , x) for(auto a : x) n #define all(x) begin(x), end(x)  n #define sz(x) (ll)(x).size() n using namespace std; n const ll INF = 1e9 + 9; n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0); n     ll t; n     cin>>t; n     while(t--){ n         ll n; n         cin>>n; n         string s,s2; n         cin>>s>>s2; n         ll conta = 0,contb = 0; n         rep(i,0,n){ n             if(s[i] -  0  > s2[i] -  0 )conta++; n             else if(s[i] -  0  < s2[i] -  0 )contb++; n         } n         // cout<<conta<<   <<contb<<endl; n         if(conta > contb) cout<< RED n ; n         else if(conta == contb) cout<< EQUAL n ; n         else cout<< BLUE n ; n     } n     return 0;   n }


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (y > x) x = y; n } n const long long inf = (long long)1e18 + 17; n const long long iinf = 1500000000; n const long long dx[] = {1, 0, -1, 0}; n const long long dy[] = {0, 1, 0, -1}; n const long long mod = 1e9 + 7; n bool test_case = 1; n void solve() { n   string a, b; n   cin >> a >> b; n   long long next[a.size() + 10][26], n1 = a.size(); n   for (long long i = 0; i < 26; i++) next[n1][i] = inf; n   for (long long i = n1 - 1; i >= 0; i--) { n     for (long long j = 0; j < 26; j++) next[i][j] = next[i + 1][j]; n     next[i][a[i] -  a ] = i; n   } n   long long n = b.size(); n   long long dp[n + 10][n + 10]; n   dp[0][0] = 0; n   for (long long ii = 0; ii < b.size(); ii++) { n     long long l1 = ii + 1, l2 = b.size() - l1; n     for (long long i = 0; i <= l1; i++) { n       for (long long j = 0; j <= l2; j++) { n         if (i == 0 && j == 0) continue; n         dp[i][j] = inf; n         if (j != 0) { n           long long op1 = dp[i][j - 1]; n           if (op1 != inf && ii + j < b.size()) { n             long long nex = b[ii + j] -  a ; n             long long pos = next[op1][nex]; n             amin(dp[i][j], pos + 1); n           } n         } n         if (i != 0) { n           long long op1 = dp[i - 1][j]; n           if (op1 != inf) { n             long long nex = b[i - 1] -  a ; n             long long pos = next[op1][nex]; n             amin(dp[i][j], pos + 1); n           } n         } n       } n     } n     if (dp[l1][l2] != inf) { n       cout <<  YES n ; n       return; n     } n   } n   cout <<  NO n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   if (test_case == true) cin >> t; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5e5; n long long pr(long long a, long long b, long long c) { n   if (b == 0) return 1LL; n   long long ans = 1; n   while (b > 0) { n     if (b % 2) ans = ((ans % c) * (a % c)) % c; n     a = ((a % c) * (a % c)) % c; n     b /= 2; n     a %= c; n     ans %= c; n   } n   return ans; n } n void solve() { n   long long k, m, h, l, r, a, b, c, d, w, mini, maxi = INT_MAX, prev, res; n   double n; n   cin >> n; n   n *= 2.0; n   double y = (double)3.14159265358979323846 / n; n   double ans = tan((3.14159265358979323846 / 2.0) - y); n   cout << fixed << setprecision(10) << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n, b = 0, p = 0, m, i = 0, matches = 0; n   scanf( %d , &n); n   scanf( %d , &b); n   scanf( %d , &p); n   m = n; n   while (m != 1) { n     matches = matches + m / 2; n     m = m / 2 + m % 2; n   } n   printf( %d %d , b * (matches * 2) + matches, p * n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, arr[200002], cum[200002]; n   long long int res = 1000000000000LL, count = 0LL, i, j, m; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n   } n   for (i = 1; i <= n; i++) { n     cum[i] = arr[i]; n   } n   for (i = 2; i <= n; i++) { n     cum[i] += cum[i - 1]; n   } n   for (i = 1, j = n; i < n; i++) { n     if (arr[i] == 1) { n       m = (cum[j] - cum[i + 1]); n       count += j - i - m; n     } n   } n   res = min(res, count); n   count = 0; n   for (i = n; i > 1; i--) { n     if (arr[i] == 0) { n       count += (cum[i - 1]); n     } n   } n   res = min(res, count); n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD; n long long getFibb(long long n) { n   long long a11 = 1, a12 = 1, a21 = 1, a22 = 0; n   long long r11 = 1, r12 = 0; n   long long q11 = 0, q12 = 0, q21 = 0, q22 = 0; n   while (n > 0) { n     if (n & 1) { n       q11 = (r11 * a11 + r12 * a21) % MOD; n       q12 = (r11 * a12 + r12 * a22) % MOD; n       r11 = q11; n       r12 = q12; n     } n     q11 = (a11 * a11 + a12 * a21) % MOD; n     q12 = (a11 * a12 + a12 * a22) % MOD; n     q21 = (a21 * a11 + a22 * a21) % MOD; n     q22 = (a21 * a12 + a22 * a22) % MOD; n     a11 = q11; n     a12 = q12; n     a21 = q21; n     a22 = q22; n     n >>= 1; n   } n   return r12; n } n long long m, l, r, k, ans; n long long cnt(long long L, long long R, long long X) { n   return ((R / X) - ((L - 1) / X)); n } n int main() { n   cin >> m >> l >> r >> k; n   MOD = m; n   for (long long i = 1; i * i <= r; i++) { n     if (cnt(l, r, i) >= k) ans = max(ans, i); n     if (cnt(l, r, r / i) >= k) ans = max(ans, r / i); n   } n   cout << getFibb(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, res, s, a, b; n   cin >> n; n   res = n - 1; n   for (i = 1; i < n; i++) { n     s = 0; n     a = n; n     b = i; n     while (a != b) { n       if (a > b) n         a = a - b; n       else n         b = b - a; n       s++; n     } n     if (a == 1 && b == 1) n       if (s < res) res = s; n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n const int INF = 1e9 + 7; n long long int arr[MAX]; n signed main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios::sync_with_stdio(0); n   long long int n1, n2; n   cin >> n1 >> n2; n   string a, b; n   cin >> a >> b; n   long long int ans = INF; n   long long int pos; n   long long int cnt = 0; n   for (long long int i = 0; i < n2 - n1 + 1; ++i) { n     long long int cnt = 0; n     for (long long int j = 0; j < n1; ++j) { n       if (a[j] == b[i + j]) { n         cnt++; n       } n     } n     cnt = n1 - cnt; n     if (cnt < ans) { n       ans = cnt; n       pos = i; n     } n   } n   cout << ans << endl; n   for (long long int i = pos; i < pos + n1; ++i) { n     if (b[i] != a[i - pos]) { n       cout << i - pos + 1 <<    ; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int i, j, n, k, l, s = 0, m = 0, p = 0, r = 0; n     cin >> n; n     int a[n]; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       s += a[i]; n       if (a[i] == 0) p++; n       if (a[i] < -1) r++; n       if (a[i] > 0) m++; n     } n     s += p; n     if (s != 0) n       cout << p << endl; n     else { n       cout << p + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   scanf( %d%d , &n, &t); n   string s; n   cin >> s; n   string s1 =   , s2 =   ; n   bool ck = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  . ) n       ck = 1; n     else if (ck) n       s2 += s[i]; n     else n       s1 += s[i]; n   } n   if (ck == 0) { n     cout << s << endl; n     return 0; n   } n   int cnt = 0; n   ck = 0; n   for (int i = 0; i < s2.size(); i++) { n     if (s2[i] >=  5 ) { n       cnt = i; n       ck = 1; n       break; n     } n   } n   int cp = 0; n   if (ck) { n     while (cnt >= 0) { n       if (cp == t) break; n       cp++; n       cnt--; n       int val = (s2[cnt] -  0 ) + 1; n       s2[cnt] = (val +  0 ); n       if (val < 5) break; n     } n     if (cnt == -1) { n       int len = s1.size(); n       string sl =   ; n       for (int i = 0; i < len - 1; i++) sl +=  0 ; n       sl +=  1 ; n       int ca = 0; n       string s9 =   ; n       for (int i = len - 1; i >= 0; i--) { n         int val = (s1[i] -  0 ) + (sl[i] -  0 ) + ca; n         ca = val / 10; n         s9 += (val % 10 +  0 ); n       } n       if (ca) s9 += (ca +  0 ); n       reverse(s9.begin(), s9.end()); n       cout << s9 << endl; n     } else { n       cout << s1 <<  . ; n       if (ck) n         for (int i = 0; i <= cnt; i++) cout << s2[i]; n       else n         cout << s2 << endl; n       cout << endl; n     } n   } else { n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1100000; n long long arr[N]; n long long b[N]; n long long n, m, w; n bool ok(long long x) { n   memset(b, 0, sizeof(b)); n   long long remain = m; n   long long c = 0; n   for (int i = 0; i < n; i++) { n     c += b[i]; n     if (c + arr[i] < x) { n       long long need = x - (c + arr[i]); n       b[i + w] -= need; n       remain -= need; n       c += need; n     } n   } n   if (remain >= 0) n     return true; n   else n     return false; n } n int main() { n   while (cin >> n >> m >> w) { n     memset(arr, 0, sizeof(arr)); n     for (int i = 0; i < n; i++) cin >> arr[i]; n     long long l = 1; n     long long r = (long long)9e9; n     while (l < r) { n       long long mid = (l + r + 1) / 2; n       if (ok(mid)) { n         l = mid; n       } else { n         r = mid - 1; n       } n     } n     cout << l << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n const int MAXN = 510; n char grid[MAXN][MAXN]; n int n, k; n int component[MAXN][MAXN]; n int size[MAXN * MAXN]; n int orig_size[MAXN * MAXN]; n int last_taken[MAXN * MAXN]; n int dr[] = {1, 0, -1, 0}; n int dc[] = {0, 1, 0, -1}; n inline bool valid(int r, int c) { return 0 <= r && r < n && 0 <= c && c < n; } n void dfs(int r, int c, int cmp) { n   component[r][c] = cmp; n   size[cmp]++; n   for (int i = 0; i < 4; i++) { n     int nr = r + dr[i], nc = c + dc[i]; n     if (!valid(nr, nc)) continue; n     if (component[nr][nc] != -1 || grid[nr][nc] ==  X ) continue; n     dfs(nr, nc, cmp); n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int r = 0; r < n; r++) n     for (int c = 0; c < n; c++) scanf(  %c , &grid[r][c]); n   memset(component, -1, sizeof(component)); n   int cnt = 0; n   for (int r = 0; r < n; r++) { n     for (int c = 0; c < n; c++) { n       if (grid[r][c] ==  .  && component[r][c] == -1) { n         dfs(r, c, cnt); n         orig_size[cnt] = size[cnt]; n         cnt++; n       } n     } n   } n   int best = 0; n   int t = 1; n   for (int r = 0; r + k - 1 < n; r++) { n     for (int i = 0; i < cnt; i++) size[i] = orig_size[i]; n     for (int rr = 0; rr < k; rr++) n       for (int cc = 0; cc < k; cc++) n         if (grid[r + rr][cc] ==  . ) size[component[r + rr][cc]]--; n     for (int c = 0; c + k - 1 < n; c++) { n       int got = k * k; n       for (int i = 0; i < k; i++) { n         int rr, cc; n         rr = r - 1, cc = c + i; n         if (valid(rr, cc) && grid[rr][cc] ==  .  && n             last_taken[component[rr][cc]] != t) { n           got += size[component[rr][cc]]; n           last_taken[component[rr][cc]] = t; n         } n         rr = r + i, cc = c + k; n         if (valid(rr, cc) && grid[rr][cc] ==  .  && n             last_taken[component[rr][cc]] != t) { n           got += size[component[rr][cc]]; n           last_taken[component[rr][cc]] = t; n         } n         rr = r + k, cc = c + i; n         if (valid(rr, cc) && grid[rr][cc] ==  .  && n             last_taken[component[rr][cc]] != t) { n           got += size[component[rr][cc]]; n           last_taken[component[rr][cc]] = t; n         } n         rr = r + i, cc = c - 1; n         if (valid(rr, cc) && grid[rr][cc] ==  .  && n             last_taken[component[rr][cc]] != t) { n           got += size[component[rr][cc]]; n           last_taken[component[rr][cc]] = t; n         } n       } n       best = max(best, got); n       for (int rr = r; rr < r + k; rr++) { n         if (grid[rr][c] ==  . ) size[component[rr][c]]++; n         if (c + k < n && grid[rr][c + k] ==  . ) size[component[rr][c + k]]--; n       } n       t++; n     } n   } n   printf( %d n , best); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long par[200005], sz[200005]; n long long root(long long x) { n   return (x == par[x] ? x : (par[x] = root(par[x]))); n } n long long ans = 0; n void join(long long x, long long y) { n   ans -= sz[root(y)] * (sz[root(y)] - 1) / 2; n   ans -= sz[root(x)] * (sz[root(x)] - 1) / 2; n   sz[root(x)] += sz[root(y)]; n   par[root(y)] = root(x); n   ans += sz[root(x)] * (sz[root(x)] - 1) / 2; n } n long long res[200005]; n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, pair<long long, long long>>> sh; n   for (long long i = 1; i < n; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     sh.push_back({z, {x, y}}); n   } n   for (long long i = 1; i <= n; i++) sz[i] = 1, par[i] = i; n   sort(sh.begin(), sh.end()); n   long long l = 0; n   vector<pair<long long, long long>> qr; n   for (long long i = 0; i < m; i++) { n     long long x; n     cin >> x; n     qr.push_back({x, i}); n   } n   sort(qr.begin(), qr.end()); n   for (auto &cv : qr) { n     while (l < sh.size() && sh[l].first <= cv.first) { n       join(sh[l].second.first, sh[l].second.second); n       l++; n     } n     res[cv.second] = ans; n   } n   for (long long i = 0; i < m; i++) cout << res[i] << (i == m - 1 ?   n  :    ); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000007; n vector<int> rec[100008]; n int dp[2][100008], a[100008]; n int fun(int minn, int val) { n   int ll = 0, rr, ss = rec[val].size(); n   if (ss == 0) n     return maxn; n   else if (rec[val][0] >= minn) n     return rec[val][0] + 1; n   rr = ss; n   while (ll + 1 != rr) { n     int mid = (ll + rr) / 2; n     if (rec[val][mid] < minn) n       ll = mid; n     else n       rr = mid; n   } n   if (rr == ss) return maxn; n   return rec[val][rr] + 1; n } n int main() { n   int n, m, s, e, i, cc, ans; n   bool f; n   scanf( %d%d%d%d , &n, &m, &s, &e); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i <= 100000; i++) rec[i].clear(); n   for (i = 0; i < m; i++) { n     int tmp; n     scanf( %d , &tmp); n     rec[tmp].push_back(i); n   } n   memset(dp, 0, sizeof(dp)); n   cc = 1; n   ans = 0; n   f = true; n   while (f) { n     f = false; n     for (i = ans; i < n; i++) { n       if (i != 0 && dp[1 - cc][i - 1] != maxn) { n         dp[cc][i] = fun(dp[1 - cc][i - 1], a[i]); n         if (i != ans) dp[cc][i] = min(dp[cc][i], dp[cc][i - 1]); n         if (dp[cc][i] + i + 1 + (ans + 1) * e <= s) f = true; n       } else if (i == 0) { n         dp[cc][i] = fun(0, a[i]); n         if (i != ans) dp[cc][i] = min(dp[cc][i], dp[cc][i - 1]); n         if (dp[cc][i] + i + 1 + (ans + 1) * e <= s) f = true; n       } else n         dp[cc][i] = maxn; n     } n     if (f) ++ans; n     cc = 1 - cc; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int dp[3333]; n bool d[3333]; n int a[3333]; n bool f[3333]; n int qu[3333]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x; n     cin >> x; n     d[x] = true; n   } n   m = 0; n   for (int i = 0; i <= 1000; i++) n     if (d[i]) { n       a[++m] = i - n; n     } n   qu[1] = 0; n   f[1000] = true; n   int l = 1; n   int r = 1; n   while (l <= r) { n     int u = qu[l]; n     l++; n     for (int i = 1; i <= m; i++) { n       int v = u + a[i]; n       if (v >= -1000 && v <= 1000) { n         if (f[v + 1000] == false) { n           f[v + 1000] = true; n           qu[++r] = v; n           dp[v + 1000] = dp[u + 1000] + 1; n         } n       } n     } n   } n   int ret = 1000000001; n   for (int i = -1000; i <= 1000; i++) n     if (f[i + 1000]) { n       bool ok = false; n       for (int j = 1; j <= m; j++) n         if (i + a[j] == 0) { n           ok = true; n           break; n         } n       if (ok) ret = min(ret, dp[i + 1000] + 1); n     } n   if (ret == 1000000001) n     cout << -1; n   else n     cout << ret; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = (long long)(2e5), MOD = 1e9 + 7; n const long long INF = 1e17; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   int cnt = 0; n   string copy; n   copy += s[0]; n   for (int i = 1; i < s.size(); i++) { n     if (s[i] == copy[copy.size() - 1]) { n       copy.erase(copy.begin() + copy.size() - 1); n       cnt++; n     } else n       copy += s[i]; n   } n   if (cnt % 2 == 0) n     cout <<  No ; n   else n     cout <<  Yes ; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = acos((long double)-1.0); n const long double eps = 1e-9; n namespace io_impl { n inline bool maybe_digit(char c) { return c >=  0  && c <=  9 ; } n struct io_s { n  private: n   bool negative; n   bool ok = true; n   char ch = next_char(); n   int precious; n   long double epslion; n   inline char next_char() const { return getchar(); } n  n  public: n   template <typename T> n   inline bool rn(T& _v) { n     negative = false; n     _v = 0; n     while (!maybe_digit(ch) && ch != EOF) { n       negative = ch ==  - ; n       ch = next_char(); n     }; n     if (ch == EOF) return ok = false; n     do { n       _v = (_v << 1) + (_v << 3) + ch -  0 ; n     } while (maybe_digit(ch = next_char())); n     if (negative) _v = -_v; n     return true; n   } n   template <typename T> n   inline void rn_unsafe(T& _v) { n     negative = false; n     _v = 0; n     while (!maybe_digit(ch)) { n       negative = ch ==  - ; n       ch = next_char(); n     }; n     do { n       _v = (_v << 1) + (_v << 3) + ch -  0 ; n     } while (maybe_digit(ch = next_char())); n     if (negative) _v = -_v; n   } n   template <typename T> n   inline T rn() { n     T v = T(); n     rn_unsafe(v); n     return v; n   } n   template <typename T> n   inline bool run(T& _v) { n     _v = 0; n     while (!maybe_digit(ch) && ch != EOF) ch = next_char(); n     if (ch == EOF) return ok = false; n     do { n       _v = (_v << 1) + (_v << 3) + ch -  0 ; n     } while (maybe_digit(ch = next_char())); n     return true; n   } n   template <typename T> n   inline void run_unsafe(T& _v) { n     _v = 0; n     while (!maybe_digit(ch)) ch = next_char(); n     do { n       _v = (_v << 1) + (_v << 3) + ch -  0 ; n     } while (maybe_digit(ch = next_char())); n   } n   template <typename T> n   inline T run() { n     T v = T(); n     run_unsafe(v); n     return v; n   } n   template <typename T> n   inline bool rd(T& _v) { n     negative = false; n     _v = 0; n     while (!maybe_digit(ch) && ch != EOF) { n       negative = ch ==  - ; n       ch = next_char(); n     }; n     if (ch == EOF) return ok = false; n     do { n       _v = (_v * 10) + (ch -  0 ); n     } while (maybe_digit(ch = next_char())); n     static int stk[70], tp; n     if (ch ==  . ) { n       tp = 0; n       T _v2 = 0; n       while (maybe_digit(ch = next_char())) { n         stk[tp++] = ch -  0 ; n       } n       while (tp--) { n         _v2 = _v2 / 10 + stk[tp]; n       } n       _v += _v2 / 10; n     } n     if (ch ==  e  || ch ==  E ) { n       ch = next_char(); n       static bool _neg = false; n       if (ch ==  + ) n         ch = next_char(); n       else if (ch ==  - ) n         _neg = true, ch = next_char(); n       if (maybe_digit(ch)) { n         _v *= pow(10, run<long long>() * (_neg ? -1 : 1)); n       } n     } n     if (negative) _v = -_v; n     return true; n   } n   template <typename T> n   inline T rd() { n     T v = T(); n     rd(v); n     return v; n   } n   inline int gets(char* s) { n     char* c = s; n     while (ch ==   n  || ch ==   r  || ch ==    ) ch = next_char(); n     if (ch == EOF) return (ok = false), *c = 0; n     do { n       *(c++) = ch; n       ch = next_char(); n     } while (ch !=   n  && ch !=   r  && ch !=     && ch != EOF); n     *(c++) =   0 ; n     return static_cast<int>(c - s - 1); n   } n   inline int getline(char* s) { n     char* c = s; n     while (ch ==   n  || ch ==   r ) ch = next_char(); n     if (ch == EOF) return (ok = false), *c = 0; n     do { n       *(c++) = ch; n       ch = next_char(); n     } while (ch !=   n  && ch !=   r  && ch != EOF); n     *(c++) =   0 ; n     return static_cast<int>(c - s - 1); n   } n   inline char get_alpha() { n     while (!isalpha(ch)) ch = next_char(); n     const char ret = ch; n     ch = next_char(); n     return ret; n   } n   inline char get_nonblank() { n     while (iswspace(ch)) ch = next_char(); n     const char ret = ch; n     ch = next_char(); n     return ret; n   } n   inline char get_char() { n     const char ret = ch; n     ch = next_char(); n     return ret; n   } n   template <typename T> n   inline void o(T p) { n     static int stk[70], tp; n     if (p == 0) { n       putchar( 0 ); n       return; n     } n     if (p < 0) { n       p = -p; n       putchar( - ); n     } n     while (p) stk[++tp] = p % 10, p /= 10; n     while (tp) putchar(stk[tp--] +  0 ); n   } n   template <typename T> n   inline void od(T v) { n     static int stk[70], tp; n     tp = 0; n     if (fabs(v) < epslion / 10) { n       putchar( 0 ); n       if (precious > 0) { n         putchar( . ); n         for (int i = 0; i < precious; ++i) putchar( 0 ); n       } n       return; n     } else { n       if (v < 0) { n         v = -v; n         putchar( - ); n       } n       v += epslion / 2; n       T p = floor(v) + epslion / 10; n       while (fabs(p) > 1 - epslion) { n         stk[++tp] = fmod(p, 10), p = floor(p / 10) + epslion; n       } n       if (tp == 0) putchar( 0 ); n       while (tp) putchar(stk[tp--] +  0 ); n     } n     if (precious == 0) return; n     putchar( . ); n     v -= floor(v); n     for (int i = 0; i < precious; ++i) { n       v *= 10; n       putchar((int)floor(v) +  0 ); n       v = fmod(v, 1); n     } n   } n   typedef void io_operator(io_s& v); n   typedef char* charptr; n   template <typename T> n   inline io_s& operator>>(T& x) { n     if (numeric_limits<T>::is_signed) n       rn(x); n     else n       run(x); n     return *this; n   } n   template <typename T1, typename T2> n   inline io_s& operator>>(pair<T1, T2> pr); n   template <typename T> n   inline io_s& operator<<(const T& x); n   inline io_s& operator<<(io_operator* v) { n     v(*this); n     return *this; n   } n   operator bool() const { return ok; } n   void set_precious(int x) { n     precious = x; n     epslion = pow(10, -x); n   } n   inline int ri() { return rn<int>(); } n   inline long long rll() { return rn<long long>(); } n   inline static void new_line(io_s& v) { putchar(  n ); } n   inline static void flush(io_s& v) { fflush(stdout); } n }; n template <> n inline io_s& io_s::operator>>(char*& x) { n   gets(x); n   return *this; n } n template <> n inline io_s& io_s::operator>>(float& x) { n   rd(x); n   return *this; n } n template <> n inline io_s& io_s::operator>>(double& x) { n   rd(x); n   return *this; n } n template <> n inline io_s& io_s::operator>>(long double& x) { n   rd(x); n   return *this; n } n template <typename T1, typename T2> n inline io_s& io_s::operator>>(pair<T1, T2> pr) { n   rd(pr.frist); n   rd(pr.second); n   return *this; n } n template <> n inline void io_s::o(const char p) { n   putchar(p); n } n template <> n inline void io_s::o(const char* p) { n   printf(p); n } n template <> n inline void io_s::o(float p) { n   od(p); n } n template <> n inline void io_s::o(double p) { n   od(p); n } n template <> n inline void io_s::o(long double p) { n   od(p); n } n template <typename T> n inline io_s& io_s::operator<<(const T& x) { n   o(x); n   return *this; n } n io_s::io_operator* nl = io_s::new_line; n io_s::io_operator* fl = io_s::flush; n }  // namespace io_impl n using namespace io_impl; n io_s io; n typedef void InitFunction(); n typedef bool SolveFunction(int); n struct env_s { n  private: n   bool global_inited = false; n   bool case_inited = false; n  n  public: n   bool once() { return global_inited ? false : (global_inited = true); } n   bool every() { return case_inited ? false : (case_inited = true); } n   void fileInput() { freopen( data.in ,  r , stdin); } n   void fileOutput() { freopen( data.out ,  w , stdout); } n   InitFunction *_initOnce, *_init; n   template <InitFunction TInitOnce, InitFunction TInit> n   void configure() { n     _initOnce = TInitOnce; n     _init = TInit; n   } n   void init(bool global) { n     if (global) n       if (_initOnce != NULL) _initOnce(); n     if (_init != NULL) _init(); n   } n   void emptyInit() {} n   template <SolveFunction TSolve> n   void solveSingle() { n     global_inited = false; n     case_inited = false; n     init(true); n     TSolve(0); n   } n   template <SolveFunction TSolve> n   void solveMany() { n     global_inited = false; n     int T = io.ri(); n     for (int cs = 1; T--; ++cs) { n       case_inited = false; n       init(cs == 1); n       TSolve(cs); n     } n   } n   template <SolveFunction TSolve> n   void solveMuch() { n     global_inited = false; n     case_inited = false; n     for (int cs = 1; init(cs == 1), TSolve(cs); ++cs) case_inited = false; n   } n } env; n namespace solution { n void initOnce() { io.set_precious(12); } n void init() {} n bool solve(int _cas) { n   int n; n   io >> n; n   int x = 1e9 + 7, a, aa, b, bb; n   for (int i = 0; i < n; ++i) { n     io >> a >> b; n     aa = max(a, b); n     bb = min(a, b); n     if (aa <= x) n       x = aa; n     else if (bb <= x) n       x = bb; n     else { n       puts( NO ); n       return true; n     } n   } n   puts( YES ); n   return true; n } n }  // namespace solution n int main() { n   env.configure<solution::initOnce, solution::init>(); n   env.solveSingle<solution::solve>(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, q; n   cin >> n >> q; n   vector<int> a((size_t)n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<pair<int, int>> b((size_t)q); n   for (int i = 0; i < q; i++) { n     cin >> b[i].first >> b[i].second; n     b[i].first--, b[i].second--; n   } n   int m = 22; n   vector<vector<int>> c((size_t)m, vector<int>((size_t)m, n)); n   vector<vector<int>> d((size_t)m, vector<int>((size_t)m, n)); n   vector<vector<int>> e((size_t)n, vector<int>((size_t)m, n)); n   for (int i = n - 1; i >= 0; i--) { n     c = vector<vector<int>>((size_t)m, vector<int>((size_t)m, n)); n     vector<int> x((size_t)m, n); n     for (int j = 0; j < m; j++) { n       for (int k = 0; k < m; k++) { n         if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) { n           c[j][k] = i; n         } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) { n           if ((a[i] & (1 << j)) != 0) { n             c[j][k] = d[j][k]; n           } n           if ((a[i] & (1 << j)) != 0) { n             x[k] = min(x[k], c[j][k]); n           } n         } n       } n     } n     for (int j = 0; j < m; j++) { n       for (int k = 0; k < m; k++) { n         if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) == 0) { n           c[j][k] = min(c[j][k], x[k]); n         } n         e[i][j] = min(e[i][j], c[j][k]); n         e[i][k] = min(e[i][k], c[j][k]); n         d[j][k] = min(d[j][k], c[j][k]); n       } n     } n   } n   for (int i = 0; i < q; i++) { n     bool f = false; n     for (int j = 0; j < m; j++) { n       if ((a[b[i].second] & (1 << j)) != 0) { n         if (e[b[i].first][j] <= b[i].second) { n           f = true; n           break; n         } n       } n     } n     cout << (f ?  Shi  :  Fou ) << endl; n   } 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   long long n, k; n   while (cin >> n >> k) { n     vector<long long> a(n); n     long long total = 0; n     for (auto &i : a) { n       cin >> i; n       total += i; n     } n     sort(a.begin(), a.end()); n     long long lo = 0, hi = (total / n); n     while (lo < hi) { n       long long mid = (lo + hi + 1) >> 1; n       long long need = 0; n       for (int i = 0; i < n; ++i) { n         need += max(0LL, mid - a[i]); n       } n       if (need > k) n         hi = mid - 1; n       else n         lo = mid; n     } n     long long tmp1 = lo; n     lo = (total + n - 1) / n, hi = total; n     while (lo < hi) { n       long long mid = (lo + hi) >> 1; n       long long need = 0; n       for (int i = 0; i < n; ++i) { n         need += max(0LL, a[i] - mid); n       } n       if (need > k) n         lo = mid + 1; n       else n         hi = mid; n     } n     cout << llabs(lo - tmp1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, h; n   scanf( %d%d%d , &n, &m, &h); n   h--; n   n--; n   int cnto = 0, cntr = 0, tot; n   for (int i = 0; i < m; i++) { n     int t; n     scanf( %d , &t); n     if (i == h) n       cntr = t - 1; n     else n       cnto += t; n   } n   if (n > cnto + cntr) { n     printf( -1.0 n ); n     return 0; n   } n   tot = cnto + cntr; n   double prob = 1; n   for (int i = n; i > 0; i--, cnto--, tot--) { n     prob *= (double)cnto / tot; n   } n   printf( %.8lf n , 1 - prob); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sum; n long long int cnt; n long long int maxs; n long long int a[100001]; n map<long long int, long long int> jilu; n long long int maxa(long long int a, long long int b) { return a > b ? a : b; } n int main() { n   int i, n; n   while (scanf( %d , &n) != EOF) { n     cnt = 0; n     sum = 0; n     maxs = 0; n     jilu.clear(); n     for (i = 1; i < n + 1; i++) { n       scanf( %lld , &a[i]); n       sum += a[i]; n       maxs = maxa(maxs, ++jilu[sum]); n     } n     cnt = n - maxs; n     printf( %lld n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, n; n   scanf( %d%d%d , &x, &y, &n); n   for (int i = 0; i < n; i++) y += x ^= y ^= x ^= y; n   printf( %d n , x); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 15; n const int mod = 998244353; n char s[N]; n int len; n int depth[N]; n vector<int> adj[N]; n int dp[N][2]; n int add(int x, int y) { return ((x + y) % mod + mod) % mod; } n int mul(int x, int y) { return (long long)x * y % mod; } n int mypow(int x, int c) { n   int ret = 1; n   while (c > 0) { n     if (c & 1) { n       ret = mul(ret, x); n     } n     c /= 2; n     x = mul(x, x); n   } n   return ret; n } n bool vis[N]; n void dfs(int node, int par) { n   vis[node] = true; n   for (int ch : adj[node]) { n     if (ch != par && !vis[ch]) { n       depth[ch] = depth[node] + 1; n       dfs(ch, node); n     } n   } n } n int solveDp(int node, int bit) { n   int &ret = dp[node][bit]; n   if (ret != -1) { n     return ret; n   } n   ret = 0; n   bool isLeaf = true; n   int cc = 0; n   for (int ch : adj[node]) { n     if (depth[ch] > depth[node]) { n       isLeaf = false; n     } else { n       continue; n     } n     ++cc; n     int diff = abs(ch - node); n     if (diff >= len) { n       int ind = min(ch, node); n       if (s[ind] ==  ? ) { n         ret = add(ret, solveDp(ch, 0)); n         ret = add(ret, solveDp(ch, 1)); n       } else { n         int u = (s[ind] -  0 ) ^ bit; n         ret = solveDp(ch, u); n       } n     } else { n       ret = solveDp(ch, bit); n     } n   } n   if (isLeaf) { n     ret = 1; n   } n   return ret; n } n void solve() { n   scanf(  %s  , s + 1); n   len = strlen(s + 1); n   int ret = 0; n   for (int st = 2; st <= len; ++st) { n     for (int i = 1; i <= len * 2; ++i) adj[i].clear(); n     memset(dp, -1, sizeof(dp)); n     memset(depth, 0, sizeof(depth)); n     memset(vis, 0, sizeof(vis)); n     for (int i = 1; i <= len; ++i) { n       int j = len - i + 1; n       if (i != j) adj[i].push_back(j); n     } n     int g = len - st + 1; n     for (int j = st; j <= len; ++j) { n       int ind = j - st + 1; n       int nind = g - ind + 1 + (st - 1); n       if (j + len != nind + len) adj[j + len].push_back(nind + len); n     } n     for (int i = 1; i < st; ++i) { n       dp[i + len][1] = 0; n     } n     dp[1][0] = 0; n     dp[len][0] = 0; n     dp[st + len][0] = 0; n     dp[len * 2][0] = 0; n     for (int i = 1; i <= len; ++i) { n       adj[i].push_back(i + len); n       adj[i + len].push_back(i); n     } n     int tmp = 1; n     for (int i = 1; i <= len * 2; ++i) { n       assert(adj[i].size() <= 2); n       if (adj[i].size() == 1 && !vis[i]) { n         dfs(i, 0); n       } n     } n     for (int i = 1; i <= len * 2; ++i) { n       assert(vis[i]); n       if (depth[i] == 0) { n         assert(adj[i].size() == 1); n         tmp = mul(tmp, add(solveDp(i, 0), solveDp(i, 1))); n       } n     } n     ret = add(ret, tmp); n   } n   printf( %d n , ret); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 50; n long long T, n, m; n char s[maxn]; n int main() { n   cin >> n; n   cin >> s + 1; n   int sign = 0, x = 0, y = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  U ) n       y++; n     else if (s[i] ==  R ) n       x++; n     if (y > x) { n       if (sign == -1) ans++; n       sign = 1; n     } else if (y < x) { n       if (sign == 1) ans++; n       sign = -1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long llinf = 0x3f3f3f3f3f3f3f3f; n template <typename INint> n inline void IN(INint &first) { n   first = 0; n   int f = 1; n   char c; n   cin.get(c); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     cin.get(c); n   } n   while (c >=  0  && c <=  9 ) { n     first = first * 10 + c -  0 ; n     cin.get(c); n   } n   first *= f; n } n template <typename INint> n inline void OUT(INint first) { n   if (first > 9) OUT(first / 10); n   cout.put(first % 10 +  0 ); n } n const int maxn = int(4e5); n int mod = int(1e9 + 7); n int n; n int mn = -inf, mx = inf; n set<int> A; n int gtype(int first) { n   if (first < mn) return 0; n   if (first > mx) return 4; n   if (first == mn) return 1; n   if (first == mx) return 3; n   return 2; n } n void mul(long long &a, long long b) { n   a *= b; n   if (a > mod) a %= mod; n } n int main() { n   int T(1), times(0); n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   while (++times, T--) { n     cin >> n; n     A.insert(mx); n     A.insert(mn); n     long long res(1), ans(1); n     for (int i = 1; i <= int(n); i++) { n       int no; n       string s; n       cin >> s >> no; n       int p = gtype(no); n       if (s[1] ==  D ) { n         A.insert(no); n         if (p == 2) ++res; n       } else { n         if (p == 0 || p == 4) { n           cout << 0 << endl; n           return 0; n         } n         if (p == 2) mul(ans, 2); n         A.erase(no); n         res = 1; n         auto pos = A.lower_bound(no); n         mx = *pos; n         mn = *(--pos); n       } n     } n     ans *= res; n     ans %= mod; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, m; n vector<long> clauses[100000 + 1]; n vector<long> variables[100000 + 1]; n long long ans[2] = {0}; n bool check_clauses[100000 + 1] = {false}; n long long dp_path[100000 + 1][2][2]; n long long dp_cycle[100000 + 1][2][2][2]; n void path_component(long start_clause) { n   long cur_clause = 0, nxt_clause = 0, variable = 0; n   long long temp[2]; n   if (clauses[start_clause].size() == 1) { n     variable = abs(clauses[start_clause][0]); n     if (variables[variable].size() == 1) { n       if ((ans[0] == 0) && (ans[1] == 0)) { n         ans[1] = 1; n         ans[0] = 1; n       } else { n         temp[1] = (ans[1] + ans[0]) % 1000000007; n         temp[0] = (ans[1] + ans[0]) % 1000000007; n         ans[1] = temp[1]; n         ans[0] = temp[0]; n       } n       return; n     } n     nxt_clause = (abs(variables[variable][0]) != start_clause) n                      ? abs(variables[variable][0]) n                      : abs(variables[variable][1]); n     if ((long long)variables[variable][0] * (long long)variables[variable][1] > n         0) { n       dp_path[nxt_clause][1][1] = 0; n       dp_path[nxt_clause][1][0] = 1; n       dp_path[nxt_clause][0][1] = 0; n       dp_path[nxt_clause][0][0] = 1; n     } else { n       dp_path[nxt_clause][1][1] = 1; n       dp_path[nxt_clause][1][0] = 0; n       dp_path[nxt_clause][0][1] = 1; n       dp_path[nxt_clause][0][0] = 0; n     } n     cur_clause = nxt_clause; n   } else { n     if ((variables[abs(clauses[start_clause][0])].size() == 2) && n         (variables[abs(clauses[start_clause][1])].size() == 2)) n       return; n     variable = (variables[abs(clauses[start_clause][0])].size() == 1) n                    ? abs(clauses[start_clause][0]) n                    : abs(clauses[start_clause][1]); n     cur_clause = start_clause; n     dp_path[start_clause][1][1] = 1; n     dp_path[start_clause][1][0] = 0; n     dp_path[start_clause][0][1] = 0; n     dp_path[start_clause][0][0] = 1; n   } n   if (check_clauses[start_clause]) return; n   check_clauses[start_clause] = true; n   check_clauses[cur_clause] = true; n   while (true) { n     if (clauses[cur_clause].size() == 1) { n       if ((ans[0] == 0) && (ans[1] == 0)) { n         ans[0] = (dp_path[cur_clause][0][0] + dp_path[cur_clause][1][0]) % n                  1000000007; n         ans[1] = (dp_path[cur_clause][0][1] + dp_path[cur_clause][1][1]) % n                  1000000007; n       } else { n         temp[0] = ((ans[1] * n                     ((dp_path[cur_clause][0][1] + dp_path[cur_clause][1][1]) % n                      1000000007)) % n                        1000000007 + n                    (ans[0] * n                     ((dp_path[cur_clause][0][0] + dp_path[cur_clause][1][0]) % n                      1000000007)) % n                        1000000007) % n                   1000000007; n         temp[1] = ((ans[1] * n                     ((dp_path[cur_clause][0][0] + dp_path[cur_clause][1][0]) % n                      1000000007)) % n                        1000000007 + n                    (ans[0] * n                     ((dp_path[cur_clause][0][1] + dp_path[cur_clause][1][1]) % n                      1000000007)) % n                        1000000007) % n                   1000000007; n         ans[0] = temp[0]; n         ans[1] = temp[1]; n       } n       return; n     } n     variable = (abs(clauses[cur_clause][0]) != variable) n                    ? abs(clauses[cur_clause][0]) n                    : abs(clauses[cur_clause][1]); n     if (variables[variable].size() == 1) { n       long long dp_temp[2][2]; n       dp_temp[0][1] = dp_path[cur_clause][0][1]; n       dp_temp[0][0] = dp_path[cur_clause][0][0]; n       dp_temp[1][1] = n           (dp_path[cur_clause][0][0] + 2 * dp_path[cur_clause][1][1]) % n           1000000007; n       dp_temp[1][0] = n           (dp_path[cur_clause][0][1] + 2 * dp_path[cur_clause][1][0]) % n           1000000007; n       if ((ans[0] == 0) && (ans[1] == 0)) { n         ans[0] = (dp_temp[0][0] + dp_temp[1][0]) % 1000000007; n         ans[1] = (dp_temp[0][1] + dp_temp[1][1]) % 1000000007; n       } else { n         temp[0] = ((ans[1] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) % n                        1000000007 + n                    (ans[0] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) % n                        1000000007) % n                   1000000007; n         temp[1] = ((ans[1] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) % n                        1000000007 + n                    (ans[0] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) % n                        1000000007) % n                   1000000007; n         ans[0] = temp[0]; n         ans[1] = temp[1]; n       } n       return; n     } n     nxt_clause = (abs(variables[variable][0]) != cur_clause) n                      ? abs(variables[variable][0]) n                      : abs(variables[variable][1]); n     check_clauses[nxt_clause] = true; n     if ((long long)variables[variable][0] * (long long)variables[variable][1] > n         0) { n       dp_path[nxt_clause][0][1] = n           (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][1]) % 1000000007; n       dp_path[nxt_clause][0][0] = n           (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][0]) % 1000000007; n       dp_path[nxt_clause][1][1] = n           (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][1]) % 1000000007; n       dp_path[nxt_clause][1][0] = n           (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][0]) % 1000000007; n     } else { n       dp_path[nxt_clause][1][1] = n           (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][0]) % 1000000007; n       dp_path[nxt_clause][1][0] = n           (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][1]) % 1000000007; n       dp_path[nxt_clause][0][1] = n           (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][0]) % 1000000007; n       dp_path[nxt_clause][0][0] = n           (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][1]) % 1000000007; n     } n     cur_clause = nxt_clause; n   } n } n void cycle_component(long start_clause) { n   long cur_clause = 0, nxt_clause = 0, variable = 0; n   long long temp[2]; n   if (clauses[start_clause].size() == 1) return; n   if ((variables[abs(clauses[start_clause][0])].size() == 1) || n       (variables[abs(clauses[start_clause][1])].size() == 1)) n     return; n   if (check_clauses[start_clause]) return; n   check_clauses[start_clause] = true; n   variable = abs(clauses[start_clause][0]); n   nxt_clause = (abs(variables[variable][0]) != start_clause) n                    ? abs(variables[variable][0]) n                    : abs(variables[variable][1]); n   if (nxt_clause == start_clause) { n     if ((long long)variables[variable][0] * (long long)variables[variable][1] > n         0) { n       if ((ans[0] == 0) && (ans[1] == 0)) { n         ans[1] = 1; n         ans[0] = 1; n       } else { n         temp[1] = (ans[1] + ans[0]) % 1000000007; n         temp[0] = (ans[1] + ans[0]) % 1000000007; n         ans[1] = temp[1]; n         ans[0] = temp[0]; n       } n     } else { n       if ((ans[0] == 0) && (ans[1] == 0)) { n         ans[1] = 2; n         ans[0] = 0; n       } else { n         temp[1] = 2 * ans[0]; n         temp[0] = 2 * ans[1]; n         ans[1] = temp[1]; n         ans[0] = temp[0]; n       } n     } n     return; n   } n   if ((long long)variables[variable][0] * (long long)variables[variable][1] > n       0) { n     dp_cycle[nxt_clause][1][1][1] = 0; n     dp_cycle[nxt_clause][1][1][0] = 1; n     dp_cycle[nxt_clause][1][0][1] = 0; n     dp_cycle[nxt_clause][1][0][0] = 0; n     dp_cycle[nxt_clause][0][1][1] = 0; n     dp_cycle[nxt_clause][0][1][0] = 0; n     dp_cycle[nxt_clause][0][0][1] = 0; n     dp_cycle[nxt_clause][0][0][0] = 1; n   } else { n     dp_cycle[nxt_clause][1][1][1] = 0; n     dp_cycle[nxt_clause][1][1][0] = 0; n     dp_cycle[nxt_clause][1][0][1] = 1; n     dp_cycle[nxt_clause][1][0][0] = 0; n     dp_cycle[nxt_clause][0][1][1] = 1; n     dp_cycle[nxt_clause][0][1][0] = 0; n     dp_cycle[nxt_clause][0][0][1] = 0; n     dp_cycle[nxt_clause][0][0][0] = 0; n   } n   cur_clause = nxt_clause; n   while (true) { n     check_clauses[cur_clause] = true; n     variable = (abs(clauses[cur_clause][0]) != variable) n                    ? abs(clauses[cur_clause][0]) n                    : abs(clauses[cur_clause][1]); n     nxt_clause = (abs(variables[variable][0]) != cur_clause) n                      ? abs(variables[variable][0]) n                      : abs(variables[variable][1]); n     if (nxt_clause == start_clause) { n       long long dp_temp[2][2]; n       if ((long long)variables[variable][0] * n               (long long)variables[variable][1] > n           0) { n         dp_temp[0][1] = n             (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][1] + n              dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][1]) % n             1000000007; n         dp_temp[0][0] = n             (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][0] + n              dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][0]) % n             1000000007; n         dp_temp[1][1] = n             (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][0] + n              dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][1]) % n             1000000007; n         dp_temp[1][0] = n             (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][1] + n              dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][0]) % n             1000000007; n       } else { n         dp_temp[1][1] = n             (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][0] + n              dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][0]) % n             1000000007; n         dp_temp[1][0] = n             (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][1] + n              dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][1]) % n             1000000007; n         dp_temp[0][1] = n             (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][1] + n              dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][0]) % n             1000000007; n         dp_temp[0][0] = n             (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][0] + n              dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][1]) % n             1000000007; n       } n       if ((ans[0] == 0) && (ans[1] == 0)) { n         ans[0] = (dp_temp[0][0] + dp_temp[1][0]) % 1000000007; n         ans[1] = (dp_temp[0][1] + dp_temp[1][1]) % 1000000007; n       } else { n         temp[0] = ((ans[1] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) % n                        1000000007 + n                    (ans[0] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) % n                        1000000007) % n                   1000000007; n         temp[1] = ((ans[1] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) % n                        1000000007 + n                    (ans[0] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) % n                        1000000007) % n                   1000000007; n         ans[0] = temp[0]; n         ans[1] = temp[1]; n       } n       return; n     } n     if ((long long)variables[variable][0] * (long long)variables[variable][1] > n         0) { n       for (long i = 0; i <= 1; i++) { n         dp_cycle[nxt_clause][i][0][1] = n             (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][1]) % n             1000000007; n         dp_cycle[nxt_clause][i][0][0] = n             (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][0]) % n             1000000007; n         dp_cycle[nxt_clause][i][1][1] = n             (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][0]) % n             1000000007; n         dp_cycle[nxt_clause][i][1][0] = n             (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][1]) % n             1000000007; n       } n     } else { n       for (long i = 0; i <= 1; i++) { n         dp_cycle[nxt_clause][i][1][1] = n             (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][0]) % n             1000000007; n         dp_cycle[nxt_clause][i][1][0] = n             (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][1]) % n             1000000007; n         dp_cycle[nxt_clause][i][0][1] = n             (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][1]) % n             1000000007; n         dp_cycle[nxt_clause][i][0][0] = n             (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][0]) % n             1000000007; n       } n     } n     cur_clause = nxt_clause; n   } n } n int main() { n   cin >> n >> m; n   for (long i = 1; i <= n; i++) { n     long k; n     cin >> k; n     clauses[i].resize(k); n     for (long j = 0; j < k; j++) { n       cin >> clauses[i][j]; n       if (clauses[i][j] < 0) n         variables[-clauses[i][j]].push_back(-i); n       else n         variables[clauses[i][j]].push_back(i); n     } n   } n   for (long i = 1; i <= n; i++) { n     path_component(i); n   } n   for (long i = 1; i <= n; i++) { n     cycle_component(i); n   } n   for (long i = 1; i <= m; i++) { n     if (variables[i].size() == 0) { n       ans[0] = (ans[0] * 2) % 1000000007; n       ans[1] = (ans[1] * 2) % 1000000007; n     } n   } n   ans[0] = ans[0] % 1000000007; n   ans[1] = ans[1] % 1000000007; n   cout << ans[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int n, h; n int dynamics[1001][2][31][31][31]; n int progress(int a) { return (a == 0 || a == h - 1) ? 0 : a + 1; } n inline void add(int& a, int b, int mod) { a = (a + b) % mod; } n int main(int argc, char* argv[]) { n   std::cin >> n >> h; n   if (h == 1) n     dynamics[1][1][0][0][0] = 4; n   else n     dynamics[1][1][1][1][1] = 4; n   for (int i = 1; i < n; i++) { n     for (int alive = 0; alive < 2; alive++) { n       for (int left = 0; left < h; left++) { n         for (int right = 0; right < h; right++) { n           for (int front = 0; front < h; front++) { n             int current = dynamics[i][alive][left][right][front]; n             int next_left = progress(left); n             int next_right = progress(right); n             int next_front = progress(front); n             add(dynamics[i + 1][alive][next_left][next_right][next_front], n                 current, 1e9 + 9); n             add(dynamics[i + 1][left > 0][alive][next_right][next_front], n                 current, 1e9 + 9); n             add(dynamics[i + 1][right > 0][alive][next_left][next_front], n                 current, 1e9 + 9); n             add(dynamics[i + 1][front > 0][alive][next_left][next_right], n                 current, 1e9 + 9); n           } n         } n       } n     } n   } n   int result = 0; n   for (int alive = 0; alive < 2; alive++) { n     for (int left = 0; left < h; left++) { n       for (int right = 0; right < h; right++) { n         for (int front = 0; front < h; front++) { n           if (alive > 0 || left > 0 || right > 0 || front > 0) { n             add(result, dynamics[n][alive][left][right][front], 1e9 + 9); n           } n         } n       } n     } n   } n   std::cout << result << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 + 11; n int sdis(int x, int y, int xx, int yy) { n   int dx = x - xx; n   int dy = y - yy; n   return dx * dx + dy * dy; n } n int main() { n   int ax, ay, ar, bx, by, br; n   while (cin >> ax >> ay >> ar >> bx >> by >> br) { n     int ds = sdis(ax, ay, bx, by); n     if ((ar + br) * (ar + br) <= ds) { n       printf( %.10lf n , (sqrt(ds + 0.0) - ar - br) * 0.5); n     } else if (ds <= (ar - br) * (ar - br)) { n       printf( %.10lf n , (abs(ar - br) - sqrt(ds + 0.0)) * 0.5); n     } else { n       printf( %.10lf n , 0.0); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INFL = 1LL << 60; n const long long INF = INFL; n long long MOD = 1000000007; n vector<long long> dy = {0, -1, 0, 1, 1, 1, -1, -1, 0}; n vector<long long> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; n void pres(double A, long long x = 10) { n   cout << fixed << setprecision(x) << A << endl; n } n void BinarySay(long long x, long long y = 60) { n   for (long long i = 0; i < (y); i++) cout << (x >> (y - 1 - i) & 1); n   cout << endl; n } n long long get_bit(long long x) { return __builtin_popcountll(x); } n long long pow_long(long long x, long long k) { n   long long res = 1; n   while (k > 0) { n     if (k % 2) res *= x; n     x *= x; n     k /= 2; n   } n   return res; n } n long long pow_mod(long long x, long long k) { n   long long res = 1; n   while (k > 0) { n     if (k % 2) { n       res *= x; n       res %= MOD; n     } n     x *= x; n     x %= MOD; n     k /= 2; n   } n   return res; n } n long long inverse(long long x) { return pow_mod(x, MOD - 2); }; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }; n long long kai_mod(long long x) { n   if (x == 0) return 1; n   return x * kai_mod(x - 1) % MOD; n } n vector<long long> divisor(long long n) { n   vector<long long> res(0); n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       res.push_back(i); n       if (i != n / i) res.push_back(n / i); n     } n   } n   sort(res.begin(), res.end()); n   return res; n } n vector<long long> MakePrimeList(long long x) { n   vector<long long> res; n   for (long long k = 2; k * k <= x; k++) { n     if (x % k == 0) { n       res.push_back(k); n       while (x % k == 0) x /= k; n     } n   } n   if (x > 1) res.push_back(x); n   return res; n } n const int MAXcomb = 2000100; n long long fac[MAXcomb], finv[MAXcomb], inv[MAXcomb]; n void COMinit() { n   fac[0] = fac[1] = 1; n   finv[0] = finv[1] = 1; n   inv[1] = 1; n   for (int i = 2; i < MAXcomb; i++) { n     fac[i] = fac[i - 1] * i % MOD; n     inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; n     finv[i] = finv[i - 1] * inv[i] % MOD; n   } n } n long long comb(int n, int k) { n   if (n < k) return 0; n   if (n < 0 || k < 0) return 0; n   return fac[n] * finv[k] % MOD * finv[n - k] % MOD; n } n long long disit(long long x, long long d = 10) { n   long long res = 0; n   while (x) { n     res++; n     x /= d; n   } n   return res; n } n void compress(vector<long long> &S) { n   vector<long long> xs; n   for (long long i = 0; i < (S.size()); i++) xs.push_back(S[i]); n   sort(xs.begin(), xs.end()); n   xs.erase(unique(xs.begin(), xs.end()), xs.end()); n   for (long long i = 0; i < (S.size()); i++) { n     S[i] = lower_bound(xs.begin(), xs.end(), S[i]) - xs.begin(); n   } n } n void solve() { n   long long N; n   cin >> N; n   vector<long long> A(N); n   for (long long i = 0; i < (N); i++) cin >> A[i]; n   compress(A); n   long long M = A[0] + 1; n   vector<long long> cnt(M, 0); n   for (long long i = 0; i < (N); i++) cnt[A[i]]++; n   long long g = cnt[M - 1]; n   long long second = 0; n   long long at = M; n   for (long long i = (M)-1; i >= 0; i--) { n     if (i == M - 1) continue; n     second += cnt[i]; n     at = i; n     if (second > g) break; n   } n   long long B = 0; n   long long b = 0; n   for (long long i = at - 1; i >= 0; i--) { n     b += cnt[i]; n     if (b <= g) continue; n     if ((g + second + b) * 2 > N) break; n     B = b; n   } n   if (B == 0) g = second = 0; n   cout << g <<     << second <<     << B << endl; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long T; n   cin >> T; n   for (long long i = 0; i < (T); i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n int main(void) { n   int t; n   long long int a, b, k, p, s; n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     scanf( %lld  , &a); n     scanf( %lld  , &b); n     scanf( %lld  , &k); n     s = k / 2; n     if ((k % 2) == 0) { n       p = (a - b) * (s); n     } n     if ((k % 2) == 1) { n       p = ((a - b) * (s) + a); n     } n     printf( %lld  n , p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int a[maxn]; n int t[maxn], l[maxn], r[maxn]; n int n, m, ans; n void solve(int k) { n   int tot = 0; n   if (k == 1) { n     for (int i = 2; i <= n; i++) n       if (a[i] < a[i - 1]) tot++; n   } else { n     for (int i = 2; i <= n; i++) { n       int p = i / k; n       if (i % k > 1) p++; n       if (a[i] < a[p]) tot++; n     } n   } n   printf( %d  , tot); n } n void prepare() { n   l[1] = 2, r[1] = m + 1; n   for (int i = l[1]; i <= r[1]; i++) n     if (a[i] < a[1]) ans++; n   for (int i = l[1]; i <= r[1]; i++) { n     l[i] = m * (i - 1) + 2; n     if (l[i] > n) l[i] = n + 1; n     r[i] = m * i + 1; n     if (r[i] > n) r[i] = n; n     for (int j = l[i]; j <= r[i]; j++) n       if (a[j] < a[i]) ans++; n   } n   printf( %d  , ans); n } n void deal(int k) { n   for (int i = 1; i <= m + 1; i++) { n     if (l[i] > r[i]) break; n     for (int j = 1; j < i; j++) n       if (l[i] <= r[i] && a[l[i]++] < a[i]) ans--; n     for (int j = 1; j <= i; j++) n       if (r[i] < n && a[++r[i]] < a[i]) ans++; n   } n   printf( %d  , ans); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   m = sqrt(n) + 1; n   for (int i = 1; i < m; i++) solve(i); n   if (m > n - 1) return 0; n   prepare(); n   for (int i = m + 1; i < n; i++) deal(i); n   return 0; n } n


#include <bits/stdc++.h> n int scan() { n   int noRead = 0; n   char p = getchar(); n   for (; p < 33;) { n     p = getchar(); n   } n   while (p > 32) { n     noRead = (noRead << 3) + (noRead << 1) + (p -  0 ); n     p = getchar(); n   } n   return noRead; n } n long long int lscan() { n   long long int noRead = 0; n   char p = getchar(); n   for (; p < 33;) { n     p = getchar(); n   } n   while (p > 32) { n     noRead = (noRead << 3) + (noRead << 1) + (p -  0 ); n     p = getchar(); n   } n   return noRead; n } n using namespace std; n int dp[501][501]; n int vec[501]; n int f(int l, int r) { n   if (l > r) return 0; n   if (l == r) return 1; n   if (dp[l][r] != -1) return dp[l][r]; n   int &ans = dp[l][r]; n   ans = 100001; n   ans = min(ans, 1 + f(l + 1, r)); n   for (int i = l + 2; i <= r; ++i) { n     if (vec[l] == vec[i]) { n       ans = min(ans, f(l + 1, i - 1) + f(i + 1, r)); n     } n   } n   if (vec[l] == vec[l + 1]) ans = min(ans, 1 + f(l + 2, r)); n   return ans; n } n int main(void) { n   int n = scan(); n   for (int i = 0; i < n; ++i) vec[i] = scan(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) dp[i][j] = -1; n   printf( %d , f(0, n - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = (int)2E5 + 5; n int n, x, y; n vector<int> Adj[maxN]; n int deg[maxN]; n bool avail[maxN]; n bool marked[maxN]; n int Count[maxN]; n int DFS(int u) { n   marked[u] = true; n   Count[u] = 0; n   int cnt = 0; n   for (int i = 0, sz = Adj[u].size(); i < sz; ++i) { n     int v = Adj[u][i]; n     if (marked[v]) continue; n     DFS(v); n     Count[u] += Count[v]; n     if (avail[v]) ++cnt; n   } n   if (cnt == 0) n     avail[u] = true; n   else if (cnt == 1) n     avail[u] = true, ++Count[u]; n   else n     avail[u] = false, Count[u] += 2; n   return Count[u]; n } n int main() { n   scanf( %d %d %d , &n, &x, &y); n   for (int u = 1; u <= n; ++u) Adj[u].clear(); n   memset(deg, 0, sizeof(deg)); n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     Adj[u].push_back(v); n     Adj[v].push_back(u); n     ++deg[u]; n     ++deg[v]; n   } n   if (x >= y) { n     for (int u = 1; u <= n; ++u) n       if (deg[u] == n - 1) { n         cout << (long long)(n - 2) * y + x << endl; n         return 0; n       } n     cout << (long long)(n - 1) * y << endl; n     return 0; n   } n   DFS(1); n   memset(marked, false, sizeof(marked)); n   cout << (long long)Count[1] * x + (long long)(n - 1 - Count[1]) * y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l, r, deg[100005], num; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &l, &r); n     deg[l]++; n     deg[r]++; n   } n   for (int i = 1; i <= n; i++) n     if (deg[i] == 1) num++; n   printf( %.7f n , (double)2.0 * m / num); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 500005; n const long long inf = 0x3f3f3f3f3f3f3f3f; n const long long MOD = 100000007; n const double eps = 1e-10; n long long qpow(long long a, long long b) { n   long long tmp = a % MOD, ans = 1; n   while (b) { n     if (b & 1) { n       ans *= tmp, ans %= MOD; n     } n     tmp *= tmp, tmp %= MOD, b >>= 1; n   } n   return ans; n } n long long lowbit(long long x) { return x & -x; } n long long max(long long a, long long b) { return a > b ? a : b; } n long long min(long long a, long long b) { return a < b ? a : b; } n long long mmax(long long a, long long b, long long c) { n   return max(a, max(b, c)); n } n long long mmin(long long a, long long b, long long c) { n   return min(a, min(b, c)); n } n void mod(long long &a) { n   a += MOD; n   a %= MOD; n } n long long ll(long long p) { return p << 1; } n long long rr(long long p) { return p << 1 | 1; } n long long mm(long long l, long long r) { return (l + r) / 2; } n long long lg(long long x) { n   if (x == 0) return 1; n   return (long long)log2(x) + 1; n } n bool smleql(double a, double b) { n   if (a < b || fabs(a - b) <= eps) return true; n   return false; n } n double len(double a, double b, double c, double d) { n   return sqrt((a - c) * (a - c) + (b - d) * (b - d)); n } n bool isp(long long x) { n   if (x == 1) return false; n   if (x == 2) return true; n   for (long long i = 2; i * i <= x; ++i) n     if (x % i == 0) return false; n   return true; n } n inline long long read() { n   char ch = getchar(); n   long long s = 0, w = 1; n   while (ch < 48 || ch > 57) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >= 48 && ch <= 57) { n     s = (s << 1) + (s << 3) + ch - 48; n     ch = getchar(); n   } n   return s * w; n } n inline void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 + 48); n } n long long n, k, a[maxn], sum = 0; n deque<long long> q; n long long chk(long long now) { n   long long res = 0; n   for (long long i = 1; i <= n; ++i) res += min(now, a[i]); n   return res; n } n long long half(long long l, long long r) { n   while (l <= r) { n     long long m = (l + r) / 2; n     if (chk(m) <= k) n       l = m + 1; n     else n       r = m - 1; n   } n   return r; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n >> k; n   long long mx = -inf; n   for (long long i = 1; i <= n; ++i) n     cin >> a[i], mx = max(mx, a[i]), sum += a[i]; n   if (sum < k) n     cout << -1; n   else if (sum == k) n     ; n   else { n     long long t = half(1, mx); n     k -= chk(t); n     for (long long i = 1; i <= n; ++i) { n       a[i] -= t; n       if (a[i] > 0) q.push_back(i); n     } n     while (k--) { n       long long f = q.front(); n       q.pop_front(); n       a[f]--; n       if (a[f] > 0) q.push_back(f); n     } n     for (long long i = 0; i < q.size(); ++i) cout << q[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005], dp[100005][5]; n bool solve(int i, int j) { n   if (i == n - 1) return true; n   if (dp[i][j] == -1) { n     bool ans = false; n     if (a[i] < a[i + 1]) n       for (int k = j + 1; k < 5; k++) ans |= solve(i + 1, k); n     else if (a[i] > a[i + 1]) n       for (int k = 0; k < j; k++) ans |= solve(i + 1, k); n     else n       for (int k = 0; k < 5; k++) n         if (j != k) ans |= solve(i + 1, k); n     dp[i][j] = ans; n   } n   return dp[i][j]; n } n void print(int i, int j) { n   cout << j + 1 <<    ; n   if (i == n - 1) { n     cout << endl; n     return; n   } n   if (a[i] < a[i + 1]) { n     for (int k = j + 1; k < 5; k++) { n       if (solve(i + 1, k)) { n         print(i + 1, k); n         return; n       } n     } n   } else if (a[i] > a[i + 1]) { n     for (int k = 0; k < j; k++) { n       if (solve(i + 1, k)) { n         print(i + 1, k); n         return; n       } n     } n   } else { n     for (int k = 0; k < 5; k++) { n       if (j != k && solve(i + 1, k)) { n         print(i + 1, k); n         return; n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   memset(dp, -1, sizeof(dp)); n   for (int j = 0; j < 5; j++) { n     if (solve(0, j)) { n       print(0, j); n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k, b, n; n   long long z = 1, t; n   cin >> k >> b >> n >> t; n   for (int i = 0; i < n; i++) { n     if (z > t) { n       cout << n - i + 1 << endl; n       return 0; n     } n     z = z * k + b; n   } n   if (z <= t) n     cout << 0 << endl; n   else n     cout << 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, r, r2; n   cin >> k; n   if (k % 2 == 0) n     cout << 4 <<     << k - 4; n   else n     cout << k - 9 <<     << 9; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)(1e9 + 17); n const long long mod = (long long)(1e9 + 7); n int n, k; n string s, t; n int dp[201][201][201]; n int recurse(int idx, int tot, int ct) { n   if (tot > k) { n     return -inf; n   } n   if (idx == n) { n     if (tot > k) return -inf; n     return 0; n   } n   int &ans = dp[idx][tot][ct]; n   if (ans != -1) { n     return ans; n   } n   ans = -inf; n   if (s[idx] != t[1] or s[idx] != t[0]) n     ans = max(ans, recurse(idx + 1, tot, ct)); n   ans = max(ans, recurse(idx + 1, tot + (s[idx] != t[0]), ct + 1)); n   ans = max(ans, n             ct + recurse(idx + 1, tot + (s[idx] != t[1]), ct + (t[0] == t[1]))); n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> k; n   cin >> s >> t; n   memset(dp, -1, sizeof dp); n   int ans = recurse(0, 0, 0); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long limit = 1e5 + 1; n long long res = 0, ans = 1e17, sum = 0; n long long a[limit], cost[limit]; n long long n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i] >> cost[i]; n   for (long long i = 1; i <= n; i++) { n     ans = min(cost[i], ans); n     cost[i] = ans; n   } n   for (long long i = 1; i <= n; i++) res += a[i] * cost[i]; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int get() { n   char ch; n   while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - ) n     ; n   if (ch ==  - ) { n     int s = 0; n     while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ; n     return -s; n   } n   int s = ch -  0 ; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ; n   return s; n } n const int N = 3205; n const int INF = 1e9; n int n; n char rig[N], lef[N]; n int lenr, lenl; n int f[2005][N]; n pair<int, int> lst[2005][N]; n int sa[N], y[N]; n int rk[N]; n int a[N], b[N], cnt[N]; n int all[N]; n int to[N][10]; n int nxt[N]; n int main() { n   scanf( %s , lef + 1); n   scanf( %s , rig + 1); n   n = get(); n   lenl = strlen(lef + 1); n   lenr = strlen(rig + 1); n   int w = lenl; n   for (; w && lef[lenl] ==  0 ;) lef[w--] =  9 ; n   lef[w]--; n   if (lef[1] ==  0 ) { n     for (int i = 1; i <= lenl - 1; i++) lef[i] = lef[i + 1]; n     lenl--; n   } n   if (lenl == 0) lenl = 1, lef[1] =  0 ; n   int m = 0; n   for (int i = 1; i <= lenl; i++) all[y[++m] = i] = lef[i] -  0 ; n   for (int i = 1; i <= lenr; i++) all[y[++m] = i + lenl + lenr] = rig[i] -  0 ; n   for (int i = lenl + 1; i <= lenl + lenr; i++) all[i] = 10, a[i] = b[i] = INF; n   for (int i = 1; i <= lenr; i++) n     all[i + lenl + lenr * 2] = 10, n                           a[i + lenl + lenr * 2] = b[i + lenl + lenr * 2] = INF; n   for (int i = 1; i <= m; i++) cnt[all[y[i]]]++; n   for (int i = 1; i <= 9; i++) cnt[i] += cnt[i - 1]; n   for (int i = 1; i <= m; i++) sa[cnt[all[y[i]]]--] = y[i]; n   int k = 1; n   a[sa[1]] = 1; n   for (int i = 2; i <= m; i++) { n     if (all[sa[i]] > all[sa[i - 1]]) k++; n     a[sa[i]] = k; n   } n   for (int j = 1; j <= max(lenl, lenr); j <<= 1) { n     int u = 0; n     for (int i = 1; i <= m; i++) n       if (sa[i] > j && all[sa[i] - j] < 10) y[++u] = sa[i] - j; n     for (int i = 1; i <= m; i++) n       if (all[sa[i] + j] == 10) y[++u] = sa[i]; n     for (int i = 0; i <= m; i++) cnt[i] = 0; n     for (int i = 1; i <= m; i++) cnt[a[y[i]]]++; n     for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; n     for (int i = m; i >= 1; i--) sa[cnt[a[y[i]]]--] = y[i]; n     for (int i = 1; i <= m; i++) b[sa[i]] = a[sa[i]]; n     a[sa[1]] = k = 1; n     for (int i = 2; i <= m; i++) { n       if (b[sa[i]] > b[sa[i - 1]] || n           (b[sa[i]] == b[sa[i - 1]] && b[sa[i] + j] > b[sa[i - 1] + j])) n         k++; n       a[sa[i]] = k; n     } n   } n   int wl, wr; n   for (int i = 1; i <= m; i++) { n     nxt[sa[i]] = sa[i] + 1; n     if (sa[i] == 1) wl = i; n     if (sa[i] == lenl + lenr + 1) wr = i; n     if (sa[i] == lenl) nxt[sa[i]] = 0; n     if (sa[i] == lenl + lenr + lenr) nxt[sa[i]] = 0; n     rk[sa[i]] = i; n   } n   rk[0] = m + 1; n   for (int i = 1; i <= m + 1; i++) { n     int x = sa[i]; n     for (int c = 0; c <= 9; c++) { n       int y = m + 1; n       int l = 1, r = m; n       for (; l <= r;) { n         int mid = (l + r) / 2; n         int u = sa[mid]; n         if (all[u] > c || (all[u] == c && rk[nxt[u]] >= i)) n           y = mid, r = mid - 1; n         else n           l = mid + 1; n       } n       to[i][c] = y; n     } n   } n   for (int i = 0; i <= n + 1; i++) n     for (int j = 1; j <= m + 1; j++) f[i][j] = -INF; n   f[n + 1][m + 1] = 0; n   for (int i = n + 1; i >= 2; i--) n     for (int j = 1; j <= m + 1; j++) n       if (f[i][j] != -INF) { n         for (int c = 0; c <= 9; c++) { n           int p = to[j][c]; n           int len = n - i + 2; n           int ad = max(0, min(len + 1, lenr) - lenl - 1); n           if (c != 0) { n             if (lenl == lenr) { n               if (len >= lenl && p > wl && p <= wr) ad = 1; n             } else { n               if (len >= lenl && p > wl) ad++; n               if (len >= lenr && p <= wr) ad++; n             } n           } else n             ad = 0; n           if (f[i][j] + ad > f[i - 1][p] || n               (f[i][j] + ad == f[i - 1][p] && n                (c < lst[i - 1][p].first || n                 (c == lst[i - 1][p].first && j < lst[i - 1][p].second)))) { n             f[i - 1][p] = f[i][j] + ad; n             lst[i - 1][p] = make_pair(c, j); n           } n         } n       } n   w = 1; n   for (int i = 2; i <= m + 1; i++) n     if (f[1][w] < f[1][i]) w = i; n   printf( %d n , f[1][w]); n   int pw = w; n   w = pw; n   for (int i = 1; i <= n; i++) { n     putchar( 0  + lst[i][w].first); n     w = lst[i][w].second; n   } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s1, s2; n int x[100001]; n bool C(int v) { n   if (abs(s1 - s2) > v) return false; n   set<int> st; n   st.insert(s1); n   int pos = s2; n   for (int i = 0; i < n; i++) { n     if (st.size()) { n       st.insert(pos); n     } n     while (st.size() && abs(*st.begin() - x[i]) > v) { n       st.erase(*st.begin()); n     } n     while (st.size() && abs(*st.rbegin() - x[i]) > v) { n       st.erase(*st.rbegin()); n     } n     pos = x[i]; n   } n   if (st.empty()) return false; n   return true; n } n int main(void) { n   scanf( %d%d%d , &n, &s1, &s2); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x[i]); n   } n   int l = -1, r = 1000000007; n   while (l + 1 < r) { n     int mid = (l + r) / 2; n     if (C(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %d n , r); n   return 0; n } n