#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const double pi = acos(-1.0); n int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n struct point_i { n   int x, y; n   point_i() { x = y = 0; }; n   point_i(int x_, int y_) : x(x_), y(y_) {} n }; n struct line { n   double a, b, c; n }; n struct point { n   double x, y; n   point() { x = y = 0.0; }; n   point(double x_, double y_) : x(x_), y(y_) {} n   bool operator<(point other) const { n     if (fabs(x - other.x) < EPS) return y < other.y; n     return x < other.x; n   } n   bool operator==(point other) const { n     return fabs(x - other.x) < EPS && fabs(y - other.y) < EPS; n   } n }; n double dist(point p1, point p2) { return hypot(p1.x - p2.x, p1.y - p2.y); } n void pointsToLine(point p1, point p2, line &l) { n   if (fabs(p1.x - p2.x) < EPS) { n     l.a = 1.0, l.b = 0.0, l.c = -p1.x; n   } else { n     l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x); n     l.b = 1.0; n     l.c = -(double)(l.a * p1.x) - p1.y; n   } n } n bool areParallel(line l1, line l2) { n   return (fabs(l1.a - l2.a) < EPS && fabs(l1.b - l2.b) < EPS); n } n bool areIntersect(line l1, line l2, point &p) { n   if (areParallel(l1, l2)) return false; n   p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b); n   if (fabs(l1.b) > EPS) n     p.y = -(l1.a * p.x + l1.c); n   else n     p.y = -(l2.a * p.x + l2.c); n   return true; n } n bool between(point p1, point p2, point p) { n   return (p.x + EPS >= min(p1.x, p2.x) && p.x <= max(p1.x, p2.x) + EPS && n           p.y + EPS >= min(p1.y, p2.y) && p.y <= max(p1.y, p2.y) + EPS); n } n int main() { n   point p1, p2; n   cin >> p1.x >> p1.y >> p2.x >> p2.y; n   line l1; n   pointsToLine(p1, p2, l1); n   int n; n   cin >> n; n   line a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i].a >> a[i].b >> a[i].c; n     point f, s; n     if (!a[i].a) { n       f.y = (-a[i].a * 1 - a[i].c) / a[i].b; n       f.x = 1; n       s.y = (-a[i].a * 2 - a[i].c) / a[i].b; n       s.x = 2; n     } else { n       f.x = (-a[i].b * 1 - a[i].c) / a[i].a; n       f.y = 1; n       s.x = (-a[i].b * 2 - a[i].c) / a[i].a; n       s.y = 2; n     } n     pointsToLine(f, s, a[i]); n   } n   int ans = 0; n   point p; n   for (int i = 0; i < n; ++i) { n     if (areIntersect(l1, a[i], p) && between(p1, p2, p)) { n       ++ans; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); n mt19937_64 rngll(rng()); n const int mn = 1e6 + 10; n const long long mod = 1e9 + 7; n long long fact[mn]; n long long po(long long a, long long b = mod - 2) { n   if (b < 0) b += mod - 1; n   long long ans = 1; n   for (; b; b >>= 1, a *= a, a %= mod) n     if (b & 1) ans *= a, ans %= mod; n   return ans; n } n long long ch(long long n, long long k) { n   if (k < 0 || k > n) return 0; n   return fact[n] * po(fact[k] * fact[n - k] % mod) % mod; n } n int main() { n   int n, m, a, b; n   scanf( %d%d%d%d , &n, &m, &a, &b); n   fact[0] = 1; n   for (int i = 1; i < mn; i++) fact[i] = fact[i - 1] * i % mod; n   long long ans = 0; n   for (int i = 1; i <= n - 1; i++) { n     long long num = ch(m - 1, i - 1); n     num *= ch(n - 2, i - 1) * fact[i - 1] % mod, num %= mod; n     num *= po(m, n - i - 1), num %= mod; n     num *= (i + 1) * po(n, n - 1 - i - 1) % mod, num %= mod; n     ans += num, ans %= mod; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n struct P { n   long long x; n   long long y; n   bool operator<(const P &a) const { return y > a.y; } n }; n vector<long long> v[3]; n int a, c, i, b, n, m, k, d; n long long o[1333331]; n int l[1111111]; n int j[1111115]; n int e[1111111]; n int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1}, n     dz[10] = {0, 0, 0, 0, 1, -1}; n long long x, y, mod = 1000000007, mod2 = 1000000009, mod3 = 2017; n long long z; n double pi = 3.14159265; n P u[1]; n queue<int> q; n bool as(P a, P b) { n   if (a.x != b.x) return a.x < b.x; n   return a.y < b.y; n } n long long f(long long a, long long b) { n   for (; b;) { n     long long h = a; n     a = b; n     b = h % b; n   } n   return a; n } n int main() { n   scanf( %d , &a); n   for (int t = 1; t <= a; t++) scanf( %lld , &o[t]); n   for (int t = 2; t <= a; t++) scanf( %d , &l[t]); n   for (int t = a; t > 0; t--) o[l[t]] += o[t]; n   for (int t = a; t > 0; t--) n     if (o[1] / f(o[1], o[t]) <= a) { n       j[o[1] / f(o[1], o[t])]++; n     } n   for (int t = a; t > 0; t--) { n     for (int h = t * 2; h <= a; h += t) j[h] += j[t]; n   } n   for (int t = 1; t <= a; t++) n     if (j[t] >= t) e[t] = 1; n   for (int t = a; t > 0; t--) n     if (e[t]) { n       for (int h = t * 2; h <= a; e[t] %= mod, h += t) e[t] += e[h]; n     } n   printf( %d  , e[1]); n } n


#include <bits/stdc++.h> n using namespace std; n char moves[4] = { L ,  U ,  R ,  D }; n long long dx[4] = {-1, 0, 1, 0}; n long long dy[4] = {0, -1, 0, 1}; n const long long N = 1e3 + 1; n bool visited[N]; n vector<long long> adj[N]; n long long n, k; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (long long i = 1; i < k + 1; i++) { n     cout << 2 * i <<     << 2 * i - 1 <<    ; n   } n   for (long long i = 2 * k + 1; i < 2 * n + 1; i++) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n struct Node { n   int cnt, l, r, z_0, z_1; n   Node() { n     cnt = 0; n     l = r = -1; n     z_0 = z_1 = 0; n   } n }; n vector<Node> Trie; n void update(int x) { n   int cur = 0; n   for (int i = 30; i >= 0; i--) { n     int c = (((1 << i) & x) > 0); n     if (Trie[cur].l == -1) { n       Trie.push_back(Node()); n       Trie[cur].l = Trie.size() - 1; n     } n     if (Trie[cur].r == -1) { n       Trie.push_back(Node()); n       Trie[cur].r = Trie.size() - 1; n     } n     if (!c) n       cur = Trie[cur].l; n     else n       cur = Trie[cur].r; n     Trie[cur].cnt++; n   } n } n int query(int x, int k) { n   int ans = 0, cur = 0; n   for (int i = 30; i >= 0; i--) { n     int c_x = (((1 << i) & x) > 0); n     int c_k = (((1 << i) & k) > 0); n     if (Trie[cur].l == -1) { n       Trie.push_back(Node()); n       Trie[cur].l = Trie.size() - 1; n     } n     if (Trie[cur].r == -1) { n       Trie.push_back(Node()); n       Trie[cur].r = Trie.size() - 1; n     } n     if (!c_x) { n       if (!c_k) { n         ans += Trie[Trie[cur].r].cnt; n         cur = Trie[cur].l; n       } else n         cur = Trie[cur].r; n     } else { n       if (!c_k) { n         ans += Trie[Trie[cur].l].cnt; n         cur = Trie[cur].r; n       } else n         cur = Trie[cur].l; n     } n   } n   ans += Trie[cur].cnt; n   return ans; n } n void update2(int x, int col) { n   int cur = 0; n   for (int i = 30; i >= 0; i--) { n     int c = (((1 << i) & x) > 0); n     if (Trie[cur].l == -1) { n       Trie.push_back(Node()); n       Trie[cur].l = Trie.size() - 1; n     } n     if (Trie[cur].r == -1) { n       Trie.push_back(Node()); n       Trie[cur].r = Trie.size() - 1; n     } n     if (!c) n       cur = Trie[cur].l; n     else n       cur = Trie[cur].r; n     if (col == 1) n       Trie[cur].z_1++; n     else n       Trie[cur].z_0++; n   } n } n long long query2(int x, int k, int col) { n   long long ans = 0; n   int cur = 0; n   for (int i = 30; i >= 0; i--) { n     int c_x = (((1 << i) & x) > 0); n     int c_k = (((1 << i) & k) > 0); n     if (Trie[cur].l == -1) { n       Trie.push_back(Node()); n       Trie[cur].l = Trie.size() - 1; n     } n     if (Trie[cur].r == -1) { n       Trie.push_back(Node()); n       Trie[cur].r = Trie.size() - 1; n     } n     if (!c_x) { n       if (!c_k) { n         if (col == 1) n           ans += Trie[Trie[cur].r].z_1; n         else n           ans += Trie[Trie[cur].r].z_0; n         cur = Trie[cur].l; n       } else n         cur = Trie[cur].r; n     } else { n       if (!c_k) { n         if (col == 1) n           ans += Trie[Trie[cur].l].z_1; n         else n           ans += Trie[Trie[cur].l].z_0; n         cur = Trie[cur].r; n       } else n         cur = Trie[cur].l; n     } n   } n   if (col == 1) n     ans += Trie[cur].z_1; n   else n     ans += Trie[cur].z_0; n   return ans; n } n int n, m; n int a[50001]; n int check(int x) { n   Trie.clear(); n   Trie.push_back(Node()); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     cnt += query(a[i], x); n     update(a[i]); n   } n   return cnt; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   if (m == 0) { n     cout << 0; n     return 0; n   } n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long l = 0, r = 2e9; n   int ans = -1; n   while (l <= r) { n     long long mi = (l + r) / 2; n     if (check(mi) >= m) { n       ans = mi; n       l = mi + 1; n     } else n       r = mi - 1; n   } n   int lft = check(ans) - m; n   long long res = 0; n   for (int j = 0; j <= 30; j++) { n     Trie.clear(); n     Trie.push_back(Node()); n     for (int i = 1; i <= n; i++) { n       int c = (((1 << j) & a[i]) > 0); n       res = (res + (query2(a[i], ans, 1 - c) * (1LL << j)) % mod) % mod; n       update2(a[i], c); n     } n   } n   cout << ((res - lft * ans) % mod + mod) % mod; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int l = s.length(); n   int f[l + 2]; n   int a[26] = {0}; n   int l1[1000005], l2[1000005]; n   for (int i = 0; i <= l; i++) { n     f[i] = 0, l1[i] = 0, l2[i] = 0; n   } n   for (int i = 0; i < 26; i++) a[i] = 0; n   int flag = 0; n   for (int i = 0; i < l; i++) { n     if (a[s[i] -  A ] == 0) { n       a[s[i] -  A ] = 1; n       l1[i] = 1; n     } n   } n   for (int i = 0; i < 26; i++) a[i] = 0; n   for (int i = l - 1; i >= 0; i--) { n     if (a[s[i] -  A ] == 0) { n       a[s[i] -  A ] = 1; n       l2[i + 1] = -1; n     } n   } n   f[0] = l1[0]; n   for (int i = 1; i <= l; i++) { n     f[i] = f[i - 1] + l1[i] + l2[i]; n   } n   for (int i = 0; i <= l; i++) { n     if (f[i] > k) { n       flag = 1; n       break; n     } n   } n   if (flag == 1) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int i, k, j, l, a, b, len; n   scanf( %ld%ld , &len, &k); n   if (k > len) { n     printf( -1 n ); n     return 0; n   } n   if ((len > 1) && (k == 1)) { n     printf( -1 n ); n     return 0; n   } n   if ((len == 1) && (k == 1)) { n     printf( a n ); n     return 0; n   } n   if ((len == 2) && (k == 2)) { n     printf( ab n ); n     return 0; n   } else if (k == 2) { n     for (i = 1; i <= len; i++) { n       if (i % 2 == 1) n         printf( a ); n       else if (i % 2 == 0) n         printf( b ); n     } n     printf(  n ); n   } else { n     for (i = 1; i <= (len + 2 - k); i++) { n       if (i % 2 == 1) n         printf( a ); n       else if (i % 2 == 0) n         printf( b ); n     } n     b = k - 1; n     for (i = 1; i <= (k - 2); i++) { n       a = i % b; n       printf( %c , (98 + a)); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n int main(void) { n   int n, a, b; n   scanf( %d%d%d%s , &n, &a, &b, s); n   --a; n   --b; n   puts(s[a] != s[b] ?  1  :  0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   ; n   int precision = numeric_limits<double>::digits10; n   ; n   int n; n   cin >> n; n   int j = sqrt(n), k = sqrt(n); n   for (int i = j; i > 0; i--) { n     for (int p = k; p < n + 1; p++) { n       if (i * p == n) { n         cout << i <<     << p << endl; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n const long long MOD = 1e9 + 7; n using pii = std::pair<long long, long long>; n using namespace std; n long long n; n string s; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> s; n   long long good = 1; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < i; j++) n       for (long long k = 0; k < j; k++) n         if (s[k] > s[j] && s[j] > s[i]) { n           good = 0; n           break; n         } n   long long ans[205] = {0}; n   for (long long i = 0; i < n; i++) n     for (long long j = i + 1; j < n; j++) n       if (s[j] < s[i]) ans[j] = !ans[i]; n   if (!good) n     cout <<  NO n ; n   else { n     cout <<  YES n ; n     for (long long i = 0; i < n; i++) cout << ans[i]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50005; n int n; n int id[N]; n vector<pair<int, int> > val; n vector<int> sol; n int main() { n   ios ::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> id[i]; n   } n   id[n] = id[0]; n   for (int i = 0; i < n; i++) { n     val.push_back(make_pair((n - id[i]) - id[i + 1], i)); n   } n   sort(val.begin(), val.end()); n   sol.resize(n); n   for (int i = 0; i < n; i++) { n     sol[val[i].second] = n - i - 1; n   } n   for (int i = 0; i < n; i++) { n     cout << sol[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ch[3000005][2]; n int v[3000005]; n map<int, int> mp; n int sz; n void inser(int x) { n   int u = 0; n   for (int i = 20; i >= 0; i--) { n     int c = x >> i & 1; n     if (!ch[u][c]) { n       memset(ch[sz], 0, sizeof(ch[sz])); n       v[sz] = 0; n       ch[u][c] = sz++; n     } n     u = ch[u][c]; n   } n   v[u] = x; n } n int query(int y) { n   int u = 0; n   for (int i = 20; i >= 0; i--) { n     int c = y >> i & 1; n     if (ch[u][c]) n       u = ch[u][c]; n     else n       u = ch[u][c ^ 1]; n   } n   return v[u] ^ y; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   sz = 1; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     mp[x] = 1; n   } n   for (int i = 0; i <= 600005; i++) { n     if (mp[i] == 0) inser(i); n   } n   int yi = 0; n   for (int i = 1; i <= m; i++) { n     int y; n     scanf( %d , &y); n     yi ^= y; n     printf( %d n , query(yi)); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 205; n int n, m, A[N][N], a[N][N], h[N], l[N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &A[i][j]); n   for (int i = 0; i <= m; i++) { n     int f = 1; n     memcpy(a, A, sizeof(A)); n     memset(h, 0, sizeof(h)); n     memset(l, 0, sizeof(l)); n     for (int j = 1; j <= m; j++) n       if ((j <= i && a[1][j]) || (j > i && a[1][j] == 0)) { n         for (int k = 1; k <= n; k++) a[k][j] ^= 1; n         l[j] ^= 1; n       } n     for (int j = 2; j <= n; j++) { n       int f0 = 1, f1 = 1; n       for (int k = 1; k <= m; k++) n         if (a[j][k]) n           f0 = 0; n         else n           f1 = 0; n       if (f0) n         h[j] ^= 1; n       else if (!f1) { n         f = 0; n         break; n       } n     } n     if (f) { n       puts( YES ); n       for (int j = 1; j <= n; j++) printf( %d , h[j]); n       puts(  ); n       for (int j = 1; j <= m; j++) printf( %d , l[j]); n       puts(  ); n       return 0; n     } n   } n   memcpy(a, A, sizeof(A)); n   memset(h, 0, sizeof(h)); n   memset(l, 0, sizeof(l)); n   for (int i = 1; i <= m; i++) n     if (a[1][i]) { n       for (int k = 1; k <= n; k++) a[k][i] ^= 1; n       l[i] ^= 1; n     } n   int f = 2; n   for (int j = 2; j <= n; j++) { n     int cnt = 0, c = a[j][1]; n     for (int k = 2; k <= m; k++) n       if (a[j][k] != a[j][k - 1]) cnt++; n     if (cnt > 1) { n       puts( NO ); n       return 0; n     } n     if (cnt == 0 && f == 2) { n       if (c == 1) h[j] ^= 1; n       continue; n     } n     if (cnt == 0 && f == 1) { n       if (c == 0) h[j] ^= 1; n       continue; n     } n     if (cnt == 1 && f == 2) { n       f--; n       if (c) h[j] ^= 1; n       continue; n     } n     puts( NO ); n   } n   puts( YES ); n   for (int j = 1; j <= n; j++) printf( %d , h[j]); n   puts(  ); n   for (int j = 1; j <= m; j++) printf( %d , l[j]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long maxn = 60; n const char spec1[5] = { T ,  J ,  Q ,  K ,  A }; n const char spec2[4] = { S ,  D ,  H ,  C }; n long start[maxn]; n bool pos[maxn][maxn][maxn][maxn]; n long n; n bool ispair[maxn][maxn]; n int main() { n   scanf( %ld , &n); n   long q; n   long maxx = -1; n   for (q = 1; q <= n; q++) { n     long tmp1 = -1; n     long tmp2 = -1; n     string s; n     cin >> s; n     if ((s[0] >=  2 ) && (s[0] <=  9 )) n       tmp1 = s[0] -  2 ; n     else { n       for (long j = 0; j <= 4; j++) n         if (spec1[j] == s[0]) { n           tmp1 = j + 8; n           break; n         } n     } n     for (long j = 0; j <= 3; j++) n       if (spec2[j] == s[1]) { n         tmp2 = j; n         break; n       } n     assert(tmp1 != -1); n     assert(tmp2 != -1); n     start[q] = tmp2 * 13 + tmp1 + 1; n     maxx = max(maxx, start[q]); n   } n   for (long first = 1; first <= 52; first++) n     for (long second = 1; second <= 52; second++) { n       long t1 = (first - 1) % 13; n       long t2 = (first - 1) / 13; n       long t3 = (second - 1) % 13; n       long t4 = (second - 1) / 13; n       ispair[first][second] = ((t1 == t3) || (t2 == t4)); n     } n   long t1, t2, t3; n   if (n >= 3) n     t1 = start[n - 2]; n   else n     t1 = 0; n   if (n >= 2) n     t2 = start[n - 1]; n   else n     t2 = 0; n   t3 = start[n]; n   pos[n][t1][t2][t3] = true; n   for (long it = n; it > 1; it--) n     for (long first = 0; first <= maxx; first++) n       for (long second = 0; second <= maxx; second++) n         for (long third = 0; third <= maxx; third++) n           if (pos[it][first][second][third]) { n             if (ispair[second][third]) { n               long tmp; n               if (it - 3 > 0) n                 tmp = start[it - 3]; n               else n                 tmp = 0; n               pos[it - 1][tmp][first][third] = true; n             } n             if ((it - 3 > 0) && (ispair[start[it - 3]][third])) n               pos[it - 1][third][first][second] = true; n           } n   for (long j = 0; j <= maxx; j++) n     if (pos[1][0][0][j]) { n       printf( YES n ); n       exit(0); n     } n   printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n template <class I> n inline void ckMax(I& p, I q) { n   p = (p > q ? p : q); n } n template <class I> n inline void ckMin(I& p, I q) { n   p = (p < q ? p : q); n } n template <class I> n inline I Max(I& p, I q) { n   return p > q ? p : q; n } n template <class I> n inline I Min(I& p, I q) { n   return p < q ? p : q; n } n template <class I> n inline void sp(I& p, I& q) { n   I x = p; n   p = q, q = x; n } n int n, m, k, a[N]; n long long f[N][N], ans; n deque<int> q[N]; n int main() { n   scanf( %d%d%d , &n, &k, &m); n   register int i, j; n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   q[0].push_back(0); n   for (i = 1; i <= n; ++i) n     for (j = m; j; --j) { n       while ((!q[j - 1].empty()) && q[j - 1].front() < i - k) n         q[j - 1].pop_front(); n       if (q[j - 1].empty()) continue; n       f[i][j] = f[q[j - 1].front()][j - 1] + a[i]; n       while ((!q[j].empty()) && f[q[j].back()][j] <= f[i][j]) q[j].pop_back(); n       q[j].push_back(i); n     } n   for (i = n - k + 1; i <= n; ++i) ckMax(ans, f[i][m]); n   printf( %lld , ans > 0 ? ans : -1ll); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int matrix[505][505]; n int m, n; n int col[505]; n int row[505]; n int prime[110000]; n int pc; n bool visit[110000]; n void init() { n   for (int i = 2; i < 110000; i++) { n     if (visit[i]) continue; n     for (int j = i * 2; j < 110000; j += i) visit[j] = true; n   } n   for (int i = 2; i < 110000; i++) n     if (!visit[i]) prime[pc++] = i; n } n int next(int num) { n   int l, r, mid; n   l = 0; n   r = pc - 1; n   while (l < r) { n     mid = (l + r) / 2; n     if (prime[mid] < num) n       l = mid + 1; n     else n       r = mid; n   } n   return prime[r] - num; n } n int main() { n   int i, j, v, tmp, min; n   cin >> n >> m; n   init(); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) cin >> matrix[i][j]; n   for (i = 0; i < n; i++) { n     v = 0; n     for (j = 0; j < m; j++) { n       tmp = next(matrix[i][j]); n       col[j] += tmp; n       v += tmp; n     } n     row[i] = v; n   } n   min = INT_MAX; n   for (i = 0; i < n; i++) n     if (min > row[i]) min = row[i]; n   for (j = 0; j < m; j++) n     if (min > col[j]) min = col[j]; n   cout << min << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n const double eps = 1e-9; n const double pi = acos(-1.0); n int Sng(double x) { n   if (x < -eps) return -1; n   if (x > eps) return 1; n   return 0; n } n class Point { n  public: n   double x, y; n   Point(double x = 0, double y = 0) : x(x), y(y) {} n   void Read() { scanf( %lf%lf , &x, &y); } n   double Angle() { return atan2(y, x); } n   friend double Dis(const Point &A, const Point &B); n }; n Point operator-(const Point &A, const Point &B) { n   return Point(A.x - B.x, A.y - B.y); n } n double Dis(const Point &A, const Point &B) { n   return sqrt(((A.x - B.x) * (A.x - B.x)) + ((A.y - B.y) * (A.y - B.y))); n } n struct Data { n   double x; n   int io; n   Data(double x = 0, int io = 0) : x(x), io(io) {} n   bool operator<(const Data &O) const { n     if (Sng(x - O.x)) return x < O.x; n     return io > O.io; n   } n }; n void Good(double &x) { n   while (x < -pi) x += 2.0 * pi; n   while (x > pi) x -= 2.0 * pi; n } n Point p[N]; n Data sk[N * 20]; n double ang[N][N], dis[N][N]; n int w, h, n, top; n int Add(double l, double r) { n   Good(l); n   Good(r); n   sk[top++] = (Data(l, 1)); n   sk[top++] = (Data(r, -1)); n   return l > r; n } n bool Check(int id, double m) { n   if (Sng(m) == 0) return true; n   int cnt = 0; n   top = 0; n   if (p[id].x < m) { n     double agl = acos(p[id].x / m); n     cnt += Add(pi - agl, pi + agl); n     cnt += Add(pi - agl, pi + agl); n   } n   if (p[id].x + m > w) { n     double agl = acos((w - p[id].x) / m); n     cnt += Add(-agl, agl); n     cnt += Add(-agl, agl); n   } n   if (p[id].y < m) { n     double agl = acos(p[id].y / m); n     cnt += Add(1.5 * pi - agl, 1.5 * pi + agl); n     cnt += Add(1.5 * pi - agl, 1.5 * pi + agl); n   } n   if (p[id].y + m > h) { n     double agl = acos((h - p[id].y) / m); n     cnt += Add(0.5 * pi - agl, 0.5 * pi + agl); n     cnt += Add(0.5 * pi - agl, 0.5 * pi + agl); n   } n   for (int i = 0; i < n; i++) { n     if (i == id || Sng(dis[id][i]) == 0 || dis[id][i] >= 2 * m) continue; n     double agl = acos((dis[id][i] / 2 / m)); n     cnt += Add(ang[i][id] - agl, ang[i][id] + agl); n   } n   if (cnt <= 1) return true; n   sort(sk, sk + top); n   for (int i = 0; i < top; i++) { n     cnt += sk[i].io; n     if (cnt <= 1) return true; n   } n   return false; n } n int main() { n   scanf( %d%d%d , &w, &h, &n); n   for (int i = 0; i < n; i++) { n     p[i].Read(); n   } n   random_shuffle(p, p + n); n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) { n       ang[i][j] = (p[i] - p[j]).Angle(); n       ang[j][i] = (p[j] - p[i]).Angle(); n       dis[j][i] = dis[i][j] = Dis(p[i], p[j]); n     } n   double ans = 0.0; n   for (int i = 0; i < n; i++) { n     if (Check(i, ans + eps) == false) continue; n     double l = ans, r = sqrt(1.0 * w * w + 1.0 * h * h); n     for (int t = 0; r - l > 1e-10; t++) { n       double mid = (l + r) / 2.0; n       if (Check(i, mid)) n         l = mid + eps, ans = mid; n       else n         r = mid - eps; n     } n   } n   printf( %.15lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class _T> n inline _T sqr(const _T &first) { n   return first * first; n } n template <class _T> n inline string tostr(const _T &a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795L; n const long double EPS = 1e-12; n char TEMPORARY_CHAR; n const int INF = 1e9; n inline void fft(vector<complex<long double> > &a, bool invert) { n   int n = (int)a.size(); n   for (int i = 1, j = 0; i < n; ++i) { n     int bit = n >> 1; n     for (; j >= bit; bit >>= 1) j -= bit; n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     long double ang = 2 * PI / len * (invert ? -1 : 1); n     complex<long double> wlen(cos(ang), sin(ang)); n     for (int i = 0; i < n; i += len) { n       complex<long double> w(1); n       for (int j = 0; j < len / 2; ++j) { n         complex<long double> u = a[i + j], v = a[i + j + len / 2] * w; n         a[i + j] = u + v; n         a[i + j + len / 2] = u - v; n         w *= wlen; n       } n     } n   } n   if (invert) n     for (int i = 0; i < n; ++i) a[i] /= n; n } n inline void input(int &a) { n   a = 0; n   while (((TEMPORARY_CHAR = getchar()) >  9  || TEMPORARY_CHAR <  0 ) && n          (TEMPORARY_CHAR !=  - )) { n   } n   char neg = 0; n   if (TEMPORARY_CHAR ==  - ) { n     neg = 1; n     TEMPORARY_CHAR = getchar(); n   } n   while (TEMPORARY_CHAR <=  9  && TEMPORARY_CHAR >=  0 ) { n     a = (a << 3) + (a << 1) + TEMPORARY_CHAR -  0 ; n     TEMPORARY_CHAR = getchar(); n   } n   if (neg) a = -a; n } n inline void out(long long a) { n   if (!a) putchar( 0 ); n   if (a < 0) { n     putchar( - ); n     a = -a; n   } n   char s[20]; n   int i; n   for (i = 0; a; ++i) { n     s[i] =  0  + a % 10; n     a /= 10; n   } n   for (int j = (i)-1; j >= 0; j--) putchar(s[j]); n } n inline int nxt() { n   int(ret); n   input((ret)); n   ; n   return ret; n } n struct lnum { n   vector<int> a; n   int base; n   lnum(int num = 0, int base = 1000000000) : base(base) { n     if (!num) a.resize(1); n     while (num) { n       a.push_back(num % base); n       num /= base; n     } n   } n   inline int len() const { return a.size(); } n   lnum &operator=(const lnum &l) { n     if (this != &l) { n       a = l.a; n       base = l.base; n     } n     return *this; n   } n   inline friend lnum operator+(const lnum &l, const lnum &r) { n     lnum ret(0, l.base); n     int base = l.base; n     int ln = l.len(), rn = r.len(); n     int n = max(ln, rn); n     ret.a.resize(n); n     int o = 0; n     for (int i = 0; i < n; ++i) { n       int s = o; n       if (i < ln) s += l.a[i]; n       if (i < rn) s += r.a[i]; n       o = s >= base; n       if (o) s -= base; n       ret.a[i] = s; n     } n     if (o) ret.a.push_back(1); n     return ret; n   } n   inline friend lnum operator-(const lnum &l, const lnum &r) { n     lnum ret(0, l.base); n     int base = l.base; n     int n = l.len(); n     int rn = r.len(); n     ret.a.resize(n); n     int o = 0; n     for (int i = 0; i < n; ++i) { n       int s = l.a[i] - o; n       if (i < rn) s -= r.a[i]; n       o = s < 0; n       if (o) s += base; n       ret.a[i] = s; n     } n     if (ret.len() > 1 && !ret.a.back()) ret.a.pop_back(); n     return ret; n   } n   inline friend lnum operator*(const lnum &l, const lnum &r) { n     lnum ret(0, l.base); n     int base = l.base; n     if (l.len() * r.len() > 1000000) { n       vector<complex<long double> > fa(l.a.begin(), l.a.end()), n           fb(r.a.begin(), r.a.end()); n       int n = 1; n       while (n < max(l.len(), r.len())) n <<= 1; n       n <<= 1; n       fa.resize(n), fb.resize(n); n       fft(fa, false), fft(fb, false); n       for (int i = 0; i < n; ++i) fa[i] *= fb[i]; n       fft(fa, true); n       ret.a.resize(n); n       for (int i = 0; i < n; ++i) ret.a[i] = int(fa[i].real() + 0.5); n       int carry = 0; n       for (int i = 0; i < n; ++i) { n         ret.a[i] += carry; n         carry = ret.a[i] / base; n         ret.a[i] %= base; n       } n     } else { n       ret.a.resize(l.len() + r.len()); n       for (int i = 0; i < l.len(); ++i) n         for (int j = 0, carry = 0; j < r.len() || carry; ++j) { n           long long cur = ret.a[i + j] + n                           (long long)l.a[i] * (j < r.len() ? r.a[j] : 0) + n                           carry; n           ret.a[i + j] = cur % base; n           carry = cur / base; n         } n     } n     while (ret.len() > 1 && !ret.a.back()) ret.a.pop_back(); n     return ret; n   } n   inline friend lnum operator/(const lnum &l, const int &r) { n     lnum ret(0, l.base); n     ret.a.resize(l.len()); n     int carry = 0; n     for (int i = l.len() - 1; i >= 0; --i) { n       long long cur = l.a[i] + (long long)carry * l.base; n       ret.a[i] = cur / r; n       carry = cur % r; n     } n     while (ret.len() > 1 && ret.a.back() == 0) ret.a.pop_back(); n     return ret; n   } n   inline friend bool operator<(const lnum &l, const lnum &r) { n     if (l.len() < r.len()) return true; n     if (l.len() > r.len()) return false; n     int n = l.len(); n     for (int i = n - 1; i >= 0; --i) { n       if (l.a[i] < r.a[i]) return true; n       if (l.a[i] > r.a[i]) return false; n     } n     return false; n   } n   inline friend bool operator>(const lnum &l, const lnum &r) { return r < l; } n   inline friend bool operator==(const lnum &l, const lnum &r) { n     if (l.len() != r.len()) return false; n     int n = l.len(); n     for (int i = n - 1; i; --i) { n       if (l.a[i] != r.a[i]) return false; n     } n     return true; n   } n   inline friend bool operator!=(const lnum &l, const lnum &r) { n     return !(l == r); n   } n   inline void print() { n     if (base == 1000000000) { n       printf( %d , a.back()); n       for (int i = a.size() - 2; i >= 0; --i) printf( %09d , a[i]); n     } else { n       for (int i = a.size() - 1; i >= 0; --i) printf( %d , a[i]); n     } n   } n }; n vector<int> get(int n, int len) { n   vector<int> ret; n   ret.push_back(n); n   ret.push_back(-n); n   for (int i = 1, p = 10; i < len; ++i, p *= 10) { n     vector<int> a = get(n % p, i); n     vector<int> b = get(n / p, len - i); n     for (int i = 0; i < (int)(a.size()); i++) { n       for (int j = 0; j < (int)(b.size()); j++) { n         ret.push_back(a[i] * b[j]); n         ret.push_back(a[i] + b[j]); n         ret.push_back(a[i] - b[j]); n       } n     } n   } n   sort((ret).begin(), (ret).end()); n   ret.resize(unique((ret).begin(), (ret).end()) - ret.begin()); n   return ret; n } n int main() { n   int(k); n   input((k)); n   ; n   int(m); n   input((m)); n   ; n   set<string> s; n   char buf[10]; n   for (int i = 0; i < (int)(10000); i++) { n     vector<int> cur = get(i, 4); n     for (int j = 0; j < (int)(cur.size()); j++) { n       if (abs(k - cur[j]) < 10000) { n         sprintf(buf,  %04d%04d , abs(k - cur[j]), i); n         sprintf(buf,  %04d%04d , i, abs(k - cur[j])); n         s.insert(string(buf)); n         if ((int)s.size() >= m) break; n       } n     } n     if ((int)s.size() >= m) break; n   } n   for (__typeof((s).begin()) i = (s).begin(); i != (s).end(); i++) { n     cout << *i << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n struct el { n   int x, y, z; n }; n using namespace std; n int cmp(el a, el b) { return a.x < b.x; } n int cmp1(el a, el b) { return a.y < b.y; } n int main(int argc, const char* argv[]) { n   int n, w; n   cin >> n >> w; n   vector<el> a; n   el b; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> b.x; n     ans += b.x / 2 + b.x % 2; n     b.z = b.x / 2 + b.x % 2; n     b.y = i; n     a.push_back(b); n   } n   if (ans > w) { n     cout << -1; n     return 0; n   } n   int diff = w - ans; n   sort(a.begin(), a.end(), cmp); n   for (int i = (int)a.size() - 1; (i > -1) && (diff > 0); i--) { n     int tmp = min(diff, a[i].x - (a[i].x / 2 + a[i].x % 2)); n     a[i].z += tmp; n     diff -= tmp; n   } n   sort(a.begin(), a.end(), cmp1); n   for (auto i = a.begin(); i != a.end(); i++) { n     cout << (*i).z <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   int n; n   cin >> n; n   vector<pair<int, int> > arr[1001]; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     arr[x / 1000].push_back(make_pair(y, i)); n   } n   for (int i = 0; i < 1001; i++) n     if (arr[i].size() > 1) sort(arr[i].begin(), arr[i].end()); n   bool flag = 0; n   for (int i = 0; i < 1001; i++) { n     if (arr[i].size()) { n       flag = (!flag); n       if (flag) n         for (int j = 0; j < arr[i].size(); j++) cout << arr[i][j].second <<    ; n       else n         for (int j = arr[i].size() - 1; j >= 0; j--) n           cout << arr[i][j].second <<    ; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 200010, LOG = 20; n struct DSU { n   int par[MAXN]; n   int rank[MAXN]; n   stack<pair<int*, int>> stk; n   DSU() { n     for (int i = 1; i < MAXN; i++) par[i] = i; n   } n   int get(int x) { n     if (par[x] == x) return x; n     return get(par[x]); n   } n   int join(int x, int y) { n     x = get(x); n     y = get(y); n     if (x == y) return 0; n     if (rank[x] > rank[y]) swap(x, y); n     stk.push({&par[x], par[x]}); n     stk.push({&rank[y], rank[y]}); n     par[x] = y; n     rank[y] += (rank[x] == rank[y]); n     return 1; n   } n   void undo(int cnt = 1) { n     cnt <<= 1; n     while (cnt--) { n       auto it = stk.top(); n       stk.pop(); n       *it.first = it.second; n     } n   } n } dsu; n int n, m, k, u, v, x, y, a, b, last; n int T[MAXN], X[MAXN], Y[MAXN]; n map<pair<int, int>, vector<int>> mp; n vector<pair<int, int>> seg[MAXN << 2]; n set<pair<int, int>> edge; n string out; n int f(int x) { n   if (x == n) return 1; n   return x + 1; n } n pair<int, int> ff(int x, int y) { n   if (x > y) swap(x, y); n   return {x, y}; n } n void Add(int id, int tl, int tr, int l, int r, pair<int, int> p) { n   if (r <= tl || tr <= l) return; n   if (l <= tl && tr <= r) { n     seg[id].push_back(p); n     return; n   } n   int mid = (tl + tr) >> 1; n   Add(id << 1, tl, mid, l, r, p); n   Add(id << 1 | 1, mid, tr, l, r, p); n } n void handle(int i) { n   if (T[i] == 2) return; n   if (last) X[i] = f(X[i]), Y[i] = f(Y[i]); n   pair<int, int> p = ff(X[i], Y[i]); n   if (edge.count(p)) n     edge.erase(p); n   else n     edge.insert(p); n } n void dfs(int id, int tl, int tr) { n   int cnt = 0; n   for (pair<int, int> p : seg[id]) n     if (edge.count(p)) cnt += dsu.join(p.first, p.second); n   if (tr - tl == 1) { n     if (T[tl] == 1) n       ; n     else { n       int i = tl; n       if (last) X[i] = f(X[i]), Y[i] = f(Y[i]); n       out +=  0  + (last = (dsu.get(X[i]) == dsu.get(Y[i]))); n     } n   } else { n     int mid = (tl + tr) >> 1; n     dfs(id << 1, tl, mid); n     handle(mid); n     dfs(id << 1 | 1, mid, tr); n   } n   dsu.undo(cnt); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> T[i] >> X[i] >> Y[i]; n     mp[ff(X[i], Y[i])].push_back(i); n     mp[ff(f(X[i]), f(Y[i]))].push_back(i); n   } n   for (auto it : mp) { n     it.second.push_back(m + 1); n     for (int i = 0; i < it.second.size() - 1; i++) n       Add(1, 1, m + 1, it.second[i], it.second[i + 1], it.first); n   } n   handle(1); n   dfs(1, 1, m + 1); n   cout << out <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dat[1500][1500], vst[1500][1500], lx[1500][1500], ly[1500][1500], n     step[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n char str[1600]; n int n, m, sx, sy; n bool dfs(int x, int y) { n   int nx = ((x % n) + n) % n; n   int ny = ((y % m) + m) % m; n   if (!dat[nx][ny]) return false; n   if (!vst[nx][ny]) { n     vst[nx][ny] = 1; n     lx[nx][ny] = x; n     ly[nx][ny] = y; n   } else if ((lx[nx][ny] != x) || (ly[nx][ny] != y)) n     return true; n   else n     return false; n   for (int i = 0; i < 4; ++i) n     if (dfs(x + step[i][0], y + step[i][1])) return true; n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %s , str); n     for (int j = 0; j < m; ++j) { n       dat[i][j] = (str[j] !=  # ); n       if (str[j] ==  S ) { n         sx = i; n         sy = j; n       } n     } n   } n   memset(lx, 0, sizeof(lx)); n   memset(ly, 0, sizeof(ly)); n   if (dfs(sx, sy)) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long int mod = 1e9 + 7; n long double dp[111][11111], ans; n long long int a[111], n, k, x; n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int i, j, k; n   cin >> n >> x; n   dp[0][0] = 1; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) n     for (j = i - 1; j >= 0; j--) n       for (k = 0; k <= 10000; k++) n         dp[j + 1][k + a[i]] += dp[j][k] * (j + 1) / (n - j); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= 10000; j++) n       ans += min((1.0 * n / i + 1) * x / 2, 1.0 * j / i) * dp[i][j]; n   cout << fixed << setprecision(15) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 3e1 + 17, N = 5e1 + 17, inf = 1e9 + 7, MOD = 1e9 + 7, n                 Mod = 1e9 + 6; n long long n, m, y, t, d[maxN][maxN][N], help, mini, x; n int main() { n   for (long long i = 0; i <= 30; i++) { n     for (long long j = 0; j <= 30; j++) { n       for (long long k = 0; k <= 50; k++) d[i][j][k] = inf; n       if (i && j) d[i][j][0] = 0; n     } n   } n   for (long long i = 1; i <= 30; i++) { n     for (long long j = 1; j <= 30; j++) { n       mini = min((i * j), (long long)50); n       for (long long k = 1; k <= mini; k++) { n         if (i < 2 || j < 2) d[i][j][k] = 1; n         if (k == i * j) d[i][j][k] = 0; n         for (long long z = 0; z <= i; z++) { n           for (long long e = 0; e <= k; e++) n             d[i][j][k] = n                 min(d[i][j][k], d[i - z][j][e] + d[z][j][k - e] + (j * j)); n         } n         for (long long z = 0; z <= j; z++) { n           for (long long e = 0; e <= k; e++) n             d[i][j][k] = n                 min(d[i][j][k], d[i][j - z][e] + d[i][z][k - e] + (i * i)); n         } n       } n     } n   } n   cin >> t; n   for (long long i = 1; i <= t; i++) { n     cin >> n >> m >> y; n     cout << d[n][m][y] <<   n ; n   } n   return cout <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename t> n void read(t &x) { n   char ch = getchar(); n   x = 0; n   t f = 1; n   while (ch <  0  || ch >  9 ) f = (ch ==  -  ? -1 : f), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n long long dp[1000][100]; n int main() { n   int n, m; n   read(n), read(m); n   m *= 2; n   ; n   for (int i = 1; i <= n; i++) dp[1][i] = 1; n   for (int i = 2; i <= m; i++) n     for (int j = n; j > 0; j--) n       dp[i][j] = (dp[i][j + 1] + dp[i - 1][j]) % 1000000007; n   long long ans = 0; n   for (int i = 1; i <= n; i++) ans = (ans + dp[m][i]) % 1000000007; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n int w[N]; n vector<pair<int, int> > v[N], g[N]; n int a[N]; n int rnk(int x) { n   while (a[x] != x) a[x] = a[a[x]], x = a[x]; n   return x; n } n struct eg { n   int x, y, id; n } e[N]; n bool cmp(eg a, eg b) { return w[a.id] < w[b.id]; } n bool vis[N], taken[N]; n int ans = 0; n int dp[N][20], mx[N][20], l[N]; n int lca(int x, int y) { n   int res = 0; n   if (l[x] > l[y]) swap(x, y); n   for (int i = 19; i >= 0; i--) n     if ((l[y] - l[x]) & (1 << i)) res = max(res, mx[y][i]), y = dp[y][i]; n   if (y == x) return res; n   for (int i = 19; i >= 0; i--) n     if (dp[x][i] != dp[y][i]) n       res = max(res, max(mx[x][i], mx[y][i])), x = dp[x][i], y = dp[y][i]; n   return max(res, max(mx[x][0], mx[y][0])); n } n void dfs(int x, int val = 0, int p = 0) { n   if (vis[x]) return; n   vis[x] = 1; n   l[x] = l[p] + 1; n   dp[x][0] = p; n   mx[x][0] = val; n   for (int i = 1; i < 20; i++) n     dp[x][i] = dp[dp[x][i - 1]][i - 1], n     mx[x][i] = max(mx[x][i - 1], mx[dp[x][i - 1]][i - 1]); n   for (auto it : v[x]) { n     int y = it.first, z = it.second; n     if (!taken[z] && vis[y] && y != p) { n       int lc = lca(x, y); n       assert(lc <= w[z]); n       if (lc == w[z]) ans++; n     } n   } n   for (auto it : g[x]) { n     if (!vis[it.first]) dfs(it.first, w[it.second], x); n   } n } n int main() { n   int n, m, x, y, z; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d , &x, &y, &z); n     v[x].push_back({y, i}); n     v[y].push_back({x, i}); n     w[i] = z; n     e[i] = {x, y, i}; n   } n   sort(e + 1, e + m + 1, cmp); n   for (int i = 1; i <= n; i++) a[i] = i; n   for (int i = 1; i <= m; i++) { n     if (rnk(e[i].x) != rnk(e[i].y)) { n       a[rnk(e[i].x)] = rnk(e[i].y); n       g[e[i].x].push_back({e[i].y, e[i].id}); n       g[e[i].y].push_back({e[i].x, e[i].id}); n       taken[e[i].id] = 1; n     } n   } n   dfs(1, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1e5 + 6; n int n, t[MX]; n int ID(int x, int jump) { n   int st = 1, en = x, ret = 1e6; n   while (st <= en) { n     int mid = (st + en) / 2; n     if (t[x] - jump <= t[mid]) { n       ret = min(ret, mid); n       en = mid - 1; n     } else n       st = mid + 1; n   } n   return ret; n } n int dp[MX]; n int solve(int x) { n   if (x < 1) return 0; n   if (dp[x] != -1) return dp[x]; n   int ans = 1e8; n   int id = ID(x, 1439); n   if (id < x) ans = min(ans, 120 + solve(id - 1)); n   id = ID(x, 89); n   if (id < x) ans = min(ans, 50 + solve(id - 1)); n   ans = min(ans, 20 + solve(x - 1)); n   return dp[x] = ans; n } n int main() { n   cin >> n; n   memset(dp, -1, sizeof dp); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , t + i); n     cout << solve(i) - ans << endl; n     ans = solve(i); n   } 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(NULL); n   long long int n, m, x1, y1, x2, y2, count = 0; n   cin >> n >> m; n   vector<char> v[n]; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       char x; n       cin >> x; n       v[i].push_back(x); n       if (count == 0 && x ==  B ) { n         x1 = i + 1, y1 = j + 1; n         count++; n       } n       if (count > 0 && x ==  B ) { n         x2 = i + 1, y2 = j + 1; n       } n     } n   } n   cout << (x1 + x2) / 2 <<     << (y1 + y2) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long n = s.size(); n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  r ) { n       cout << i + 1 <<   n ; n     } n   } n   for (long long i = n - 1; i >= 0; i--) { n     if (s[i] ==  l ) { n       cout << i + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n const long long int MOD = 998244353; n class Combination { n   long long int ppow(long long int i, long long int j) { n     long long int res = 1LL; n     while (j) { n       if ((j & 1LL)) { n         res *= i; n         if (res >= MOD) { n           res %= MOD; n         } n       } n       j >>= 1; n       i *= i; n       if (i >= MOD) { n         i %= MOD; n       } n     } n     return res; n   } n  n  public: n   vector<long long int> k; n   vector<long long int> r; n   void resize(int N) { n     k.resize(N + 2); n     r.resize(N + 2); n     k[0] = 1; n     for (int i = 1; i < N + 2; i++) { n       k[i] = k[i - 1]; n       k[i] *= i; n       if (k[i] >= MOD) k[i] %= MOD; n     } n     long long int al = k[k.size() - 1]; n     long long int iv = ppow(k[k.size() - 1], MOD - 2); n     r[k.size() - 1] = iv; n     for (int i = (int)(r.size()) - 2; i >= 0; i--) { n       r[i] = r[i + 1] * (i + 1); n       if (r[i] >= MOD) { n         r[i] %= MOD; n       } n     } n   } n   long long int C(int a, int b) { n     if (a < b) return 0; n     long long int up = k[a]; n     long long int dw = r[b] * r[a - b]; n     dw %= MOD; n     up *= dw; n     up %= MOD; n     return up; n   } n   long long int H(int a, int b) { return C(a + b - 1, b); } n   long long int catalan_number(int n) { n     return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD; n   } n } c; n int main() { n   cin >> n; n   c.resize(2000000); n   long long int subt = 0; n   for (int i = 1; i < n; i++) { n     long long int way = c.C(n, i); n     way *= c.k[n - i]; n     way %= MOD; n     subt += way; n     if (subt >= MOD) subt %= MOD; n   } n   long long int whole = c.k[n]; n   whole *= n; n   whole %= MOD; n   whole += MOD; n   whole %= MOD; n   whole += MOD - subt; n   whole %= MOD; n   printf( %lld n , whole); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n int t,n;  n  n char a[300005],b[300005],c[300005]; n  n void solve(char *x,char *y,int tmp) { n // for(int i=1;i<=n*2;i++) cout<<x[i]; n // for(int j=1;j<=n*2;j++) cout<<y[j]; n // cout<< ! <<endl; n // cout<<tmp<<endl; n  int l1=1,l2=1; n  for(int i=1;i<=n*3;i++) { n   if(l1>n*2 && l2>n*2) cout<< 0 ; n   else if(l1>n*2) cout<<y[l2],l2++; n   else if(l2>n*2) cout<<x[l1],l1++; n   else if(x[l1]==y[l2]) cout<<x[l1],l1++,l2++; n   else { n //   cout<<x[l1]<<   <<y[l2]<<endl; n    if(x[l1]==tmp+ 0 ) cout<<y[l2],l2++; n    else cout<<x[l1],l1++; n   } n  } n  cout<<endl; n // exit(0); n } n  n int main(){ n  cin>>t; n  while(t--) { n   cin>>n>>a+1>>b+1>>c+1; n   int a1=0,a2=0,a3=0,b1=0,b2=0,b3=0; n   for(int i=1;i<=n*2;i++) { n    if(a[i]== 1 ) a1++; n    else b1++; n    if(b[i]== 1 ) a2++; n    else b2++; n    if(c[i]== 1 ) a3++; n    else b3++;    n   } n   if(a1>=n && a2>=n) solve(a,b,1); n   else if(a1>=n && a3>=n) solve(a,c,1); n   else if(a2>=n && a3>=n) solve(b,c,1); n   else if(b1>=n && b2>=n) solve(a,b,0); n   else if(b1>=n && b3>=n) solve(a,c,0); n   else if(b2>=n && b3>=n) solve(b,c,0); n  } n   n   n   n }


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char now[1 << 16], *S, *T; n   if (T == S) { n     T = (S = now) + fread(now, 1, 1 << 16, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline long long read() { n   register long long x = 0, f = 1; n   char ch = gc(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = gc(); n   } n   return (f == 1) ? x : -x; n } n struct node { n   long long l, r, sz, rd, w; n } g[1200001]; n inline void pushup(long long rt) { n   g[rt].sz = g[g[rt].l].sz + g[g[rt].r].sz + 1; n } n long long tot; n inline long long news(long long w) { n   g[++tot].sz = 1; n   g[tot].w = w; n   g[tot].rd = rand(); n   g[tot].l = g[tot].r = 0; n   return tot; n } n void split(long long rt, long long &a, long long &b, long long w) { n   if (!rt) return (void)(a = b = 0); n   if (g[rt].w <= w) { n     a = rt; n     split(g[a].r, g[a].r, b, w); n     pushup(a); n   } else { n     b = rt; n     split(g[b].l, a, g[b].l, w); n     pushup(b); n   } n } n long long merge(long long a, long long b) { n   if (a * b == 0) return a + b; n   if (g[a].rd <= g[b].rd) { n     g[a].r = merge(g[a].r, b); n     pushup(a); n     return a; n   } else { n     g[b].l = merge(a, g[b].l); n     pushup(b); n     return b; n   } n } n long long root; n inline void insert(long long w) { n   long long a = 0, b = 0, c = 0; n   split(root, a, b, w); n   root = merge(merge(a, news(w)), b); n } n inline long long rank_x(long long rt, long long ra) { n   while (rt) { n     if (g[g[rt].l].sz + 1 == ra) n       return g[rt].w; n     else if (g[g[rt].l].sz + 1 > ra) n       rt = g[rt].l; n     else n       ra -= g[g[rt].l].sz + 1, rt = g[rt].r; n   } n   return -1; n } n struct QUE { n   long long id, pos, opt, wht; n } q[2000001]; n long long ans[1000001], n, m, Q, qcnt; n inline bool cmp(QUE a, QUE b) { n   if (a.pos == b.pos) return a.opt < b.opt; n   return a.pos < b.pos; n } n long long sum[1000001]; n signed main() { n   n = read(), m = read(), Q = read(); n   for (long long i = 1; i <= n; i++) { n     sum[read()]++; n   } n   for (long long i = 1; i <= m; i++) { n     q[++qcnt].opt = 2; n     q[qcnt].pos = sum[i]; n     q[qcnt].id = i; n   } n   sort(q + 1, q + 1 + qcnt, cmp); n   long long SUM = 0; n   for (long long i = 1; i <= m; i++) { n     SUM += sum[q[i].id]; n     q[i].pos = sum[q[i].id] * i - SUM; n   } n   sort(q + 1, q + 1 + qcnt, cmp); n   for (long long i = 1; i <= Q; i++) { n     long long sb = read() - n; n     q[++qcnt].id = i; n     q[qcnt].pos = sb; n     q[qcnt].opt = 1; n     q[qcnt].wht = sb; n   } n   sort(q + m + 1, q + qcnt + 1, cmp); n   long long tmp = 0; n   for (long long i = m + 1; i <= qcnt; i++) { n     long long sb = q[i].wht; n     while (tmp < m && q[tmp + 1].pos < sb) tmp++; n     q[i].wht = ((sb - q[tmp].pos) % tmp ? (sb - q[tmp].pos) % tmp : tmp); n   } n   sort(q + 1, q + 1 + qcnt, cmp); n   for (long long i = 1; i <= qcnt; i++) { n     if (q[i].opt == 1) { n       ans[q[i].id] = rank_x(root, q[i].wht); n     } else { n       insert(q[i].id); n     } n   } n   for (long long i = 1; i <= Q; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int v[100000], c[100000], l[100000], r[100000]; n vector<int> g[300001]; n int d[200001] = {}; n vector<int> p[200001], pp[200001], px[200001]; n vector<int> t; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < (int)(n); ++i) cin >> v[i] >> c[i] >> l[i] >> r[i]; n   for (int i = 0; i < (int)(n); ++i) g[c[i] + l[i] + r[i]].push_back(i); n   int ans = 0; n   for (int i = 0; i < (int)(200001); ++i) n     if (!g[i].empty()) { n       for (int x : g[i]) n         if ((!l[x] || d[l[x]]) && d[l[x] + c[x]] < d[l[x]] + v[x]) { n           d[l[x] + c[x]] = d[l[x]] + v[x]; n           p[l[x] + c[x]].push_back(l[x]); n           pp[l[x] + c[x]].push_back((int)p[l[x]].size() - 1); n           px[l[x] + c[x]].push_back(x); n         } n       int ind = i; n       if (d[ind] > ans) { n         int pind = (int)p[ind].size() - 1; n         ans = d[ind]; n         t.clear(); n         while (ind) { n           t.push_back(px[ind][pind]); n           int nind = p[ind][pind]; n           pind = pp[ind][pind]; n           ind = nind; n         } n       } n       for (int x : g[i]) { n         int pos = l[x] + c[x]; n         d[pos] = 0; n         p[pos].clear(); n         pp[pos].clear(); n         px[pos].clear(); n       } n     } n   cout << (int)t.size() <<   n ; n   for (int i = (int)t.size() - 1; i >= 0; --i) cout << t[i] + 1 <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n, t, x = 1.000000011; n   scanf( %lf%lf , &n, &t); n   double res = n * pow(x, t); n   printf( %.12lf , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long> mod; n int main() { n   long long n, k; n   scanf( %I64d%I64d , &n, &k); n   bool result = true; n   for (long long i = 1LL; i <= k; i++) { n     long long x = n % i; n     if (mod.find(x) != mod.end()) { n       result = false; n       break; n     } n     mod.insert(x); n   } n   printf( %s n , result ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 100; n char ch[maxn]; n namespace Suffix_Array { n int cntA[maxn], cntB[maxn], tsa[maxn], A[maxn], B[maxn]; n int sa[maxn], rkrk[maxn], height[maxn]; n void GetSa(char *ch, int n) { n   for (int i = 0; i < maxn; i++) cntA[i] = 0; n   for (int i = 1; i <= n; i++) cntA[ch[i]]++; n   for (int i = 1; i <= maxn; i++) cntA[i] += cntA[i - 1]; n   for (int i = n; i; i--) sa[cntA[ch[i]]--] = i; n   rkrk[sa[1]] = 1; n   for (int i = 2; i <= n; i++) { n     rkrk[sa[i]] = rkrk[sa[i - 1]]; n     if (ch[sa[i]] != ch[sa[i - 1]]) rkrk[sa[i]]++; n   } n   for (int l = 1; rkrk[sa[n]] < n; l <<= 1) { n     for (int i = 0; i < maxn; i++) cntA[i] = 0; n     for (int i = 0; i < maxn; i++) cntB[i] = 0; n     for (int i = 1; i <= n; i++) { n       cntA[A[i] = rkrk[i]]++; n       cntB[B[i] = (i + l <= n) ? rkrk[i + l] : 0]++; n     } n     for (int i = 1; i < maxn; i++) cntB[i] += cntB[i - 1]; n     for (int i = n; i; i--) tsa[cntB[B[i]]--] = i; n     for (int i = 1; i < maxn; i++) cntA[i] += cntA[i - 1]; n     for (int i = n; i; i--) sa[cntA[A[tsa[i]]]--] = tsa[i]; n     rkrk[sa[1]] = 1; n     for (int i = 2; i <= n; i++) { n       rkrk[sa[i]] = rkrk[sa[i - 1]]; n       if (A[sa[i]] != A[sa[i - 1]] || B[sa[i]] != B[sa[i - 1]]) rkrk[sa[i]]++; n     } n   } n } n };  // namespace Suffix_Array n int sum[maxn]; n struct Segment_Tree { n   int Min[maxn * 8]; n   void up(int x) { Min[x] = min(Min[x << 1], Min[x << 1 | 1]); } n   void build(int x, int l, int r) { n     if (l == r) { n       Min[x] = sum[l]; n       return; n     } n     int mid = l + r >> 1; n     build(x << 1, l, mid); n     build(x << 1 | 1, mid + 1, r); n     up(x); n   } n   int query(int x, int l, int r, int L, int R) { n     if (l > R || L > r) return 0x3f3f3f3f; n     if (L <= l && r <= R) return Min[x]; n     int mid = l + r >> 1; n     return min(query(x << 1, l, mid, L, R), n                query(x << 1 | 1, mid + 1, r, L, R)); n   } n } seg; n int SUM; n int n, N; n bool check(int x) { n   int Min = seg.query(1, 1, N, x, x + n - 1); n   int begin = sum[x - 1]; n   if (SUM < 0) { n     begin += SUM; n   } n   if (Min >= begin) n     return 1; n   else n     return 0; n } n int main() { n   scanf( %s , ch + 1); n   n = strlen(ch + 1); n   for (int i = n + 1; i <= 2 * n; i++) { n     if (ch[i - n] ==  ( ) n       ch[i - n] =  0 ; n     else n       ch[i - n] =  1 ; n     ch[i] = ch[i - n]; n   } n   N = n << 1; n   Suffix_Array::GetSa(ch, N); n   for (int i = 1; i <= N; i++) { n     sum[i] = sum[i - 1]; n     if (ch[i] ==  0 ) n       sum[i]++; n     else n       sum[i]--; n   } n   SUM = sum[n]; n   seg.build(1, 1, N); n   for (int i = 1; i <= N; i++) { n     int x = Suffix_Array::sa[i]; n     if (x > n) continue; n     if (check(x)) { n       if (SUM < 0) { n         for (int j = 0; j > SUM; j--) { n           printf( ( ); n         } n       } n       for (int j = x; j < x + n; j++) { n         if (ch[j] ==  0 ) n           printf( ( ); n         else n           printf( ) ); n       } n       if (SUM > 0) { n         for (int j = 0; j < SUM; j++) { n           printf( ) ); n         } n       } n       puts(  ); n       return 0; n     } n   } n   assert(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long lim; n long long n, mod; n int m; n inline long long F(long long a, long long d, long long want, long long n) { n   if (want == 0 or lim < a) { n     return 0; n   } n   if (n == want) { n     n = min(n, (lim - a) / d + 1); n     long long z = 2 * a + (n - 1) * d; n     ((z & 1) ? n : z) >>= 1; n     return (n % mod) * (z % mod) % mod; n   } n   long long mid = (n + 1) >> 1; n   if (want <= mid) { n     return F(a, d << 1, want, mid); n   } n   return (F(a, d << 1, mid, mid) + F(a + d, d << 1, want - mid, n - mid)) % mod; n } n inline long long cal(long long r, long long v) { n   lim = min(v, n); n   return F(1, 1, r, n); n } n int main() { n   scanf( %I64d %d %I64d , &n, &m, &mod); n   for (; m-- > 0;) { n     long long l, r, u, v; n     scanf( %I64d %I64d %I64d %I64d , &l, &r, &u, &v); n     l--, u--; n     printf( %I64d n , n            (((cal(r, v) - cal(l, v)) - (cal(r, u) - cal(l, u))) % mod + mod) % n                mod); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 100; n int p[maxn << 2]; n void pushdown(int L, int R, int u) { n   if (L != R) { n     if (p[u] != -1) { n       p[u << 1] = p[u]; n       p[u << 1 | 1] = p[u]; n       p[u] = -1; n     } n   } n } n void update(int cL, int cR, int val, int L, int R, int u) { n   pushdown(L, R, u); n   if (cL <= L && R <= cR) { n     p[u] = val; n   } else { n     int m = (L + R) / 2; n     if (cR <= m) n       update(cL, cR, val, L, m, u << 1); n     else if (cL > m) n       update(cL, cR, val, m + 1, R, u << 1 | 1); n     else { n       update(cL, cR, val, L, m, u << 1); n       update(cL, cR, val, m + 1, R, u << 1 | 1); n       update(cL, cR, val, m + 1, R, u << 1 | 1); n     } n   } n } n int query(int x, int L, int R, int u) { n   pushdown(L, R, u); n   if (L == R) n     return p[u]; n   else { n     int m = (L + R) / 2; n     if (x <= m) n       return query(x, L, m, u << 1); n     else n       return query(x, m + 1, R, u << 1 | 1); n   } n } n vector<int> G[maxn]; n int res[maxn]; n int sum[maxn]; n int n; n void dfs(int u) { n   sum[u]++; n   int Max = 0; n   for (int i = 0; i < G[u].size(); ++i) { n     int v = G[u][i]; n     dfs(v); n     sum[u] += sum[v]; n     Max = max(Max, sum[v]); n   } n   update(Max, sum[u], u, 0, n, 1); n   res[u] = query(sum[u] / 2, 0, n, 1); n } n int main() { n   int q; n   scanf( %d %d , &n, &q); n   for (int i = 2; i <= n; ++i) { n     int t; n     scanf( %d , &t); n     G[t].push_back(i); n   } n   memset(p, -1, sizeof(p)); n   memset(sum, 0, sizeof(sum)); n   dfs(1); n   while (q--) { n     int t; n     scanf( %d , &t); n     printf( %d n , res[t]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main() n { n     ios_base::sync_with_stdio(0); cin.tie(0); n     long long cases, no_points, xcoord, ycoord; n     cin >> cases; n     for (int i = 0; i < cases; i++) n     { n         vector <long long> xpoints; n         vector <long long> ypoints; n          n         long long choicesx,choicesy = 1; n         n         cin >> no_points; n         for (int x = 0; x < no_points; x++) n         { n             cin >> xcoord >> ycoord; n             xpoints.push_back(xcoord); n             ypoints.push_back(ycoord); n         } n         if(no_points % 2 == 1){ n             cout << 1 <<   n ; n         } n  n         else{ n             sort(xpoints.begin(), xpoints.end()); n             sort(ypoints.begin(), ypoints.end()); n             choicesx = xpoints[(no_points/2)] - xpoints[(no_points/2) - 1] + 1; n             choicesy = ypoints[(no_points/2)] - ypoints[(no_points/2) - 1] + 1; n             cout << choicesx * choicesy <<   n ; n         } n          n         n           n     } n      n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n; n int a[4]; n int check(int a, int b, int c, int d) { n   int avg = a + b + c + d; n   int med = b + c; n   if (avg % 4 != 0 || med % 2 != 0) return 0; n   if (avg / 4 == d - a && med / 2 == d - a) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i <= n - 1; i++) cin >> a[i]; n   sort(a, a + n); n   if (n == 0) { n     cout <<  YES  << endl; n     cout << 1 << endl << 1 << endl << 3 << endl << 3; n     return 0; n   } n   if (n == 1) { n     cout <<  YES  << endl; n     cout << 3 * a[0] << endl; n     cout << 2 * a[0] << endl; n     cout << 2 * a[0]; n     return 0; n   } n   if (n <= 2) { n     if (a[1] <= 3 * a[0]) { n       cout <<  YES  << endl; n       cout << 4 * a[0] - a[1] << endl; n       cout << 3 * a[0]; n       return 0; n     } n     cout <<  NO ; n     return 0; n   } n   if (n == 3) { n     for (int i = 1; i <= 1000000; i++) { n       int b[4] = {a[0], a[1], a[2], i}; n       sort(b, b + 4); n       if (check(b[0], b[1], b[2], b[3]) == 1) { n         cout <<  YES  << endl; n         cout << i; n         return 0; n       } n     } n     cout <<  NO ; n     return 0; n   } n   if (check(a[0], a[1], a[2], a[3]) == 0) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n inline long long Input() { n   long long ret = 0; n   bool isN = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) isN = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     ret = ret * 10 + c -  0 ; n     c = getchar(); n   } n   return isN ? -ret : ret; n } n inline void Output(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   int len = 0, data[20]; n   while (x) { n     data[len++] = x % 10; n     x /= 10; n   } n   if (!len) data[len++] = 0; n   while (len--) putchar(data[len] + 48); n   putchar(  n ); n } n #pragma comment(linker,  /STACK:124000000,124000000 ) n const long double PI = acos(-1.0); n using namespace std; n int num[100005]; n int shit[100005]; n int a[100005], n; n long long work1(int id) { n   memset(shit, 0, sizeof(shit)); n   if (num[a[(n + 1) / 2]] & 1) { n     int i; n     for (i = n / 2; i; i--) { n       if (a[i] != a[n + 1 - i]) break; n     } n     for (int j = id; j <= i; j++) { n       shit[a[j]]++; n       shit[a[n - j + 1]]--; n     } n     int j, cnt = 0; n     bool flag = 1; n     for (j = 0; j <= n; j++) { n       if (shit[j] != 0) { n         flag = 0; n         if (shit[j] < 0) cnt++; n       } n     } n     if (cnt == 0) return 1LL * id * (n - id + 1 - i); n     for (j = i + 1; j < n - i + 1; j++) { n       shit[a[j]] += 1; n       if (shit[a[j]] >= 0 && shit[a[j]] - 2 < 0) cnt--; n       if (cnt == 0) { n         return 1LL * id * (n - id + 1 - j); n       } n     } n     for (j = n - i + 1; j <= n; j++) { n       shit[a[j]] += 2; n       if (shit[a[j]] >= 0 && shit[a[j]] - 2 < 0) cnt--; n       if (cnt == 0) { n         return 1LL * id * (n - id + 1 - j); n       } n     } n   } else { n     for (int j = id; j <= (n + 1) / 2; j++) { n       shit[a[j]]++; n       shit[a[n - j + 1]]--; n     } n     shit[a[(n + 1) / 2]]++; n     int j, cnt = 0; n     bool flag = 1; n     for (j = 0; j <= n; j++) { n       if (shit[j] != 0) { n         if (shit[j] < 0) cnt++; n       } n     } n     if (cnt == 0) return 1LL * id * (n - id + 1 - (n + 1) / 2); n     for (j = n + 2 - (n + 1) / 2; j <= n; j++) { n       shit[a[j]] += 2; n       if (shit[a[j]] >= 0 && shit[a[j]] - 2 < 0) cnt--; n       if (cnt == 0) { n         return 1LL * id * (n - id + 1 - j); n       } n     } n   } n } n long long work2(int id) { n   memset(shit, 0, sizeof(shit)); n   int i; n   for (i = n / 2; i; i--) { n     if (a[i] != a[n + 1 - i]) break; n   } n   for (int j = id; j <= i; j++) { n     shit[a[j]]++; n     shit[a[n - j + 1]]--; n   } n   int j, cnt = 0; n   bool flag = 1; n   for (j = 0; j <= n; j++) { n     if (shit[j] != 0) { n       flag = 0; n       if (shit[j] < 0) cnt++; n     } n   } n   if (cnt == 0) return 1LL * id * (n - id + 1 - i); n   for (j = i + 1; j < n - i + 1; j++) { n     shit[a[j]] += 1; n     if (shit[a[j]] >= 0 && shit[a[j]] - 2 < 0) cnt--; n     if (cnt == 0) { n       return 1LL * id * (n - id + 1 - j); n     } n   } n   for (j = n - i + 1; j <= n; j++) { n     shit[a[j]] += 2; n     if (shit[a[j]] >= 0 && shit[a[j]] - 2 < 0) cnt--; n     if (cnt == 0) { n       return 1LL * id * (n - id + 1 - j); n     } n   } n } n long long solve() { n   long long ans = 0; n   int i, id; n   for (i = 1; i <= n; i++) { n     if (a[i] != a[n + 1 - i]) { n       ans = ans + 1LL * i * i; n       id = i; n       break; n     } n   } n   if (i == n + 1) return 1LL * n * (n + 1) / 2; n   if (n & 1) { n     ans = ans + work1(id); n     for (int i = 1; i <= n / 2; i++) swap(a[i], a[n + 1 - i]); n     ans = ans + work1(id); n   } else { n     ans = ans + work2(id); n     for (int i = 1; i <= n / 2; i++) swap(a[i], a[n + 1 - i]); n     ans = ans + work2(id); n   } n   return ans; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     memset(num, 0, sizeof(num)); n     for (int i = 1; i <= n; i++) { n       scanf( %d , a + i); n       num[a[i]]++; n     } n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       if (num[i] & 1) cnt++; n     } n     if (cnt >= 2) { n       printf( 0 n ); n       continue; n     } n     printf( %I64d n , solve()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3fffffff; n const int maxn = 110; n char a[20]; n int k; n int main() { n   scanf( %s , a); n   cin >> k; n   int len = strlen(a); n   for (int i = 0; i < len; ++i) { n     int p = i; n     for (int j = 1; j <= k && j + i < len; ++j) n       if (a[i + j] > a[p]) p = i + j; n     if (a[p] == a[i]) continue; n     for (int j = p; j > i; --j) swap(a[j], a[j - 1]); n     k -= p - i; n     if (k <= 0) break; n   } n   printf( %s n , a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, tt[40], qq[10]; n long long int m[10][41]; n vector<int> ss[10]; n int check(int c, long long int s) { n   if (c == k) return (s == 0) || ((s + (s & (-s))) == (1LL << n)); n   int i, z = ss[c].size() - qq[c] - __builtin_popcountll(s & m[c][0]); n   s &= ~m[c][0], s |= m[c][z]; n   for (i = 0; i <= qq[c]; i++) { n     if (!check(c + 1, s)) return 0; n     if (i < qq[c]) s ^= (1LL << ss[c][z + i]); n   } n   return 1; n } n int main() { n   int i, j; n   int q; n   cin >> n >> k; n   for (i = 0; i < n; i++) tt[i] = -1; n   for (i = 0; i < k; i++) { n     cin >> q; n     ss[i].resize(q); n     for (j = 0; j < q; j++) { n       cin >> ss[i][j], ss[i][j]--; n       if (tt[ss[i][j]] == -1) tt[ss[i][j]] = i; n     } n   } n   if (n == 1) { n     cout <<  ACCEPTED  << endl; n     return 0; n   } n   for (i = 0; i < n; i++) { n     if (tt[i] == -1) { n       cout <<  REJECTED  << endl; n       return 0; n     } else n       qq[tt[i]]++; n   } n   for (i = 0; i < k; i++) { n     long long int a = 0; n     for (j = 0; j < ss[i].size(); j++) a |= (1LL << ss[i][j]); n     for (j = 0; j <= ss[i].size(); j++) { n       m[i][j] = a; n       if (j < ss[i].size()) a ^= (1LL << ss[i][j]); n     } n   } n   if (check(0, 0)) n     cout <<  ACCEPTED  << endl; n   else n     cout <<  REJECTED  << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e2 + 5; n string grid[N]; n bool found, vis[N][N]; n ; n int n, m, k, cnt, ans; n vector<pair<int, pair<int, int> > > p; n int dr[] = {-1, 1, 0, 0}; n int dc[] = {0, 0, -1, 1}; n bool check(int r, int c) { n   return r == 0 || r == n - 1 || c == 0 || c == m - 1; n } n bool valid(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } n void dfs(int r, int c) { n   vis[r][c] = 1; n   if (check(r, c)) found = 1; n   cnt++; n   for (int i = 0; i < 4; i++) { n     int rr = r + dr[i], cc = c + dc[i]; n     if (valid(rr, cc) && !vis[rr][cc] && grid[rr][cc] ==  . ) dfs(rr, cc); n   } n } n void build(int r, int c) { n   grid[r][c] =  * ; n   for (int i = 0; i < 4; i++) { n     int rr = r + dr[i], cc = c + dc[i]; n     if (valid(rr, cc)) { n       if (grid[rr][cc] ==  . ) { n         build(rr, cc); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> grid[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (!vis[i][j] && grid[i][j] ==  . ) { n         found = 0; n         cnt = 0; n         dfs(i, j); n         if (found) continue; n         p.push_back({cnt, {i, j}}); n       } n     } n   } n   sort(p.begin(), p.end()); n   for (int i = 0; i < (int)p.size() - k; i++) { n     ans += p[i].first; n     build(p[i].second.first, p[i].second.second); n   } n   cout << ans <<   n ; n   for (int i = 0; i < n; i++) { n     cout << grid[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string elimina(string s) { n   for (int i = 0; i < int(s.size()); i++) { n     if (s[i] ==  )  or s[i] ==  (  or s[i] ==  ,  or s[i] ==  . ) s[i] =    ; n   } n   return s; n } n int main() { n   string ss; n   int n; n   cin >> n; n   getline(cin, ss); n   map<string, vector<string> > g; n   map<string, vector<string> > pare; n   map<string, string> tipus; n   map<string, long long int> border; n   map<string, long long int> spacing; n   map<string, long long int> width; n   map<string, long long int> height; n   map<string, long long int> aritat; n   for (int pas = 0; pas < n; pas++) { n     getline(cin, ss); n     ss = elimina(ss); n     istringstream ci(ss); n     string name; n     ci >> name; n     if (name ==  VBox ) { n       ci >> name; n       tipus[name] =  VBox ; n     } else if (name ==  HBox ) { n       ci >> name; n       tipus[name] =  HBox ; n     } else if (name ==  Widget ) { n       ci >> name; n       tipus[name] =  Widget ; n       long long int w, h; n       ci >> w >> h; n       width[name] = w; n       height[name] = h; n     } else { n       string inst; n       ci >> inst; n       if (inst ==  pack ) { n         string name2; n         ci >> name2; n         g[name].push_back(name2); n         aritat[name]++; n         pare[name2].push_back(name); n       } else if (inst ==  set_border ) { n         long long int b; n         ci >> b; n         border[name] = b; n       } else if (inst ==  set_spacing ) { n         long long int spa; n         ci >> spa; n         spacing[name] = spa; n       } n     } n   } n   stack<string> s; n   for (map<string, string>::iterator it = tipus.begin(); it != tipus.end(); n        it++) { n     string name = it->first; n     if (aritat[name] == 0) s.push(name); n   } n   while (not s.empty()) { n     string name = s.top(); n     s.pop(); n     if (tipus[name] ==  HBox ) { n       long long int ample = 0; n       long long int alt = 0; n       vector<string> &fill = g[name]; n       for (int i = 0; i < int(fill.size()); i++) { n         string f = fill[i]; n         alt = max(alt, height[f]); n         ample += width[f]; n       } n       if (int(fill.size()) == 0) { n         ample = 0; n         alt = 0; n       } else if (int(fill.size()) == 1) { n         alt += 2 * border[name]; n         ample += 2 * border[name]; n       } else { n         alt += 2 * border[name]; n         ample += 2 * border[name] + (int(fill.size()) - 1) * spacing[name]; n       } n       height[name] = alt; n       width[name] = ample; n     } else if (tipus[name] ==  VBox ) { n       long long int ample = 0; n       long long int alt = 0; n       vector<string> &fill = g[name]; n       for (int i = 0; i < int(fill.size()); i++) { n         string f = fill[i]; n         alt += height[f]; n         ample = max(ample, width[f]); n       } n       if (int(fill.size()) == 0) { n         ample = 0; n         alt = 0; n       } else if (int(fill.size()) == 1) { n         alt += 2 * border[name]; n         ample += 2 * border[name]; n       } else { n         alt += 2 * border[name] + (int(fill.size()) - 1) * spacing[name]; n         ample += 2 * border[name]; n       } n       height[name] = alt; n       width[name] = ample; n     } n     vector<string> &p = pare[name]; n     for (int i = 0; i < int(p.size()); i++) { n       string nam = p[i]; n       aritat[nam]--; n       if (aritat[nam] == 0) { n         s.push(nam); n       } n     } n   } n   for (map<string, string>::iterator it = tipus.begin(); it != tipus.end(); n        it++) { n     string name = it->first; n     cout << name <<     << width[name] <<     << height[name] << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n constexpr int64_t P0 = 2000003; n constexpr int64_t P1 = 2000017; n constexpr int64_t M0 = 3000000000013; n constexpr int64_t M1 = 2000000000003; n class TaskC { n  public: n   struct Class { n     int id; n     int cnt; n   }; n   struct Hash { n     int64_t a, b; n     Hash() : a(0), b(0) {} n     void update(int addition) { n       a = (a * P0 + addition) % M0; n       b = (b * P1 + addition) % M1; n     } n     bool operator<(const Hash& other) const { n       return std::tie(a, b) < std::tie(other.a, other.b); n     } n   }; n   void solve(std::istream& in, std::ostream& out) { n     int n, m; n     in >> n >> m; n     vector<vector<int>> schools(n); n     vector<Class> classes; n     int timer = 0; n     vector<Hash> hashes(m); n     for (int i = 0; i < n; i++) { n       int k; n       in >> k; n       auto& school = schools[i]; n       for (int j = 0; j < k; j++) { n         int id; n         in >> id; n         --id; n         school.push_back(id); n       } n       sort(school.begin(), school.end()); n       classes.clear(); n       int l = 0; n       while (l < k) { n         int id = school[l], cnt = 0; n         while (l < k && school[l] == id) { n           ++l; n           ++cnt; n         } n         classes.push_back(Class{id, cnt}); n       } n       sort(classes.begin(), classes.end(), n            [](Class lhs, Class rhs) { return lhs.cnt < rhs.cnt; }); n       l = 0; n       while (l < classes.size()) { n         ++timer; n         int cnt = classes[l].cnt; n         while (l < classes.size() && classes[l].cnt == cnt) { n           int id = classes[l].id; n           ++l; n           hashes[id].update(timer); n         } n       } n     } n     map<Hash, int> was; n     int64_t result = 1; n     int64_t MOD = (1000000000 + 7); n     for (auto hash : hashes) { n       ++was[hash]; n       result = (result * was[hash]) % MOD; n     } n     out << result <<   n ; n   } n }; n int main() { n   std::ios_base::sync_with_stdio(false); n   TaskC solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   in.tie(0); n   out << std::fixed; n   out.precision(20); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n struct Node { n   string str; n   int pos; n   friend bool operator<(Node const& n1, Node const& n2) { n     return n2.str < n1.str; n   } n }; n priority_queue<Node> que; n int main() { n   int k; n   string str; n   cin >> str; n   cin >> k; n   que.push({string{ z  + 1}, INF}); n   for (int i = 0; i < str.size(); ++i) { n     que.push({string{str[i]}, i}); n   } n   Node i; n   while (k-- && !que.empty()) { n     i = que.top(); n     que.pop(); n     if (i.pos == str.size() - 1 || i.pos == INF) continue; n     que.push({i.str + str[i.pos + 1], i.pos + 1}); n   } n   if (k && que.empty()) n     cout <<  No such line.  << endl; n   else n     cout << i.str << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[1000010], b[1000010]; n   while (scanf( %s%s , &a, &b) != EOF) { n     int len1 = strlen(a); n     int len2 = strlen(b); n     int ans = 0; n     if (len1 != len2) { n       ans = 1; n     } else { n       if (len1 == 1) { n         if (a[0] != b[0]) n           ans = 1; n         else n           ans = 0; n       } else { n         if (strcmp(a, b) == 0) n           ans = 0; n         else { n           int j1 = 0, j2 = 0; n           for (int i = 0; i < len1; i++) n             if (a[i] ==  1 ) j1 = 1; n           for (int i = 0; i < len1; i++) n             if (b[i] ==  1 ) j2 = 1; n           if (j1 && j2) n             ans = 0; n           else n             ans = 1; n         } n       } n     } n     if (!ans) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr <<     << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void inp_out() {} n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   inp_out(); n   long long int n, m; n   cin >> n >> m; n   vector<pair<long long int, long long int>> a(n); n   long long int cnt = 0; n   multiset<pair<long long int, long long int>> st; n   vector<multiset<long long int>> adj(m + 1); n   for (long long int i = 0; i < n; ++i) { n     cin >> a[i].first >> a[i].second; n     adj[a[i].first].insert(a[i].second); n     if (a[i].first == 1) n       ++cnt; n     else n       st.insert({a[i].second, a[i].first}); n   } n   vector<long long int> ans(n + 1, 3e18); n   for (long long int i = max(1ll, cnt); i <= n; ++i) { n     vector<multiset<long long int>> temp_adj = adj; n     multiset<pair<long long int, long long int>> temp_st = st; n     long long int val = cnt, cost = 0; n     for (long long int j = 2; j <= m; ++j) { n       while (temp_adj[j].size() >= i) { n         cost += *temp_adj[j].begin(); n         temp_st.erase(temp_st.find({*temp_adj[j].begin(), j})); n         temp_adj[j].erase(temp_adj[j].begin()); n         ++val; n       } n     } n     if (val > i) continue; n     while (val < i) { n       cost += (*temp_st.begin()).first; n       temp_st.erase(temp_st.begin()); n       ++val; n     } n     ans[i] = cost; n   } n   long long int fa = *min_element(ans.begin(), ans.end()); n   cout << fa; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n void fastIO() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   fastIO(); n   int n, a; n   cin >> n; n   int tot = 0; n   int mx = 0; n   for (int i = 0; i < n; i++) { n     cin >> a; n     tot += a; n     mx = max(mx, a); n   } n   double cond = (2.0 * tot) / n; n   if (cond < mx) { n     cout << mx << endl; n   } else { n     cout << ceil(cond + .00001) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int arr[n]; n   bool fl = false; n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (arr[i] == 1) { n       fl = true; n     } n   } n   if (fl == false) n     printf( EASY ); n   else n     printf( HARD ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n char tr[100005]; n map<char, int> p; n char num[12] = { AHIMOTUVWXY }; n char num1[27] = { ABCDEFGHIJKLMNOPQRSTUVWXYZ }; n void init() { n   int i; n   for (i = 0; i < 26; i++) { n     p[num1[i]] = 0; n   } n   for (i = 0; i < 11; i++) { n     p[num[i]] = 1; n   } n } n int main() { n   init(); n   int n, i, j; n   while (scanf( %s , tr) != EOF) { n     n = strlen(tr); n     for (i = 0, j = n - 1; i < n / 2; i++, j--) { n       if (tr[i] != tr[j]) break; n     } n     if (i < n / 2) { n       printf( NO n ); n       continue; n     } n     for (i = 0; i < n; i++) { n       if (!p[tr[i]]) break; n     } n     if (i < n) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[100]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int time = 10; n   int solvedCount = 0; n   int penalty = 0; n   for (int i = 0; i < n; i++) { n     time = time + a[i]; n     if (time <= 720) { n       solvedCount++; n       if (time > 360) penalty = penalty + (time - 360); n     } n   } n   cout << solvedCount <<     << penalty << endl; 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(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     cout << n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n void solve() { n     int n; cin >> n; n     map<int,int> m; n     for(int i=0,x; i < n; ++i) { n         cin >> x; n         m[x]++; n     } n     map<int,int> m2; n     for(auto p : m) { n         m2[p.second]++; n     } n     int cnt,mn=n; n     for(auto p1 : m2) { n         cnt = 0; n         for(auto p2 : m2) { n             if(p1.first > p2.first) { n                 cnt += p2.first * p2.second; n             } else { n                 cnt += (p2.first -p1.first) * p2.second; n             } n         } n         mn = min(mn, cnt); n     } n     cout << mn << endl; n } n // 1,3,2,1,4,2 n // 1 1 2 2 n // 1:2 n // 2:2 n  n // 1 2 3 3 3 3 2 6 6 n // 1 2 2 4 n // 1:1 n // 2:2 n // 4:1 n int main() { n     int t; cin >> t; while(t--) n     solve(); n }


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:167772160 ) n using namespace std; n const int inf = 1000000000; n long long k[1001], p[1001], f[1001], c[1001]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> k[i] >> c[i]; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (c[i] > c[j]) { n         swap(c[i], c[j]); n         swap(k[i], k[j]); n       } n   int t; n   cin >> t; n   for (int i = 1; i <= t; i++) cin >> p[i]; n   p[0] = 0; n   for (int i = 1; i <= t; i++) f[i] = p[i] - p[i - 1]; n   f[t + 1] = 1000000000000LL; n   int j = 1; n   long long num = 0; n   long long ans = 0; n   for (int i = 1; i <= t + 1; i++) { n     while (j <= n) { n       long long mn = min(k[j] - num, f[i]); n       ans += (long long)c[j] * mn * (long long)i; n       if (mn == k[j] - num && mn == f[i]) { n         ++j; n         num = 0; n         break; n       } else if (mn == k[j] - num) { n         ++j; n         f[i] -= mn; n         num = 0; n       } else { n         num += mn; n         break; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long b[150005], up[150005], a[150005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   bool fl = false; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     up[i] = max(1ll, b[i]); n     if (b[i] != 0) fl = true; n   } n   if (!fl) { n     cout <<  YES n ; n     for (int i = 1; i <= n; i++) cout << 1 <<    ; n     exit(0); n   } n   for (int i = 1; i <= n; i++) { n     if (i == 1) n       up[i] = max(up[i], b[n] + 1); n     else n       up[i] = max(up[i], b[i - 1] + 1); n   } n   for (int i = 1; i <= n; i++) { n     if (b[i] < up[i]) continue; n     cout <<  YES n ; n     a[i] = b[i]; n     int uk; n     if (i == 1) n       uk = n; n     else n       uk = i - 1; n     while (uk != i) { n       long long t = up[uk] / a[uk % n + 1]; n       if (t * a[uk % n + 1] + b[uk] >= up[uk]) n         a[uk] = t * a[uk % n + 1] + b[uk]; n       else n         a[uk] = (t + 1) * a[uk % n + 1] + b[uk]; n       if (uk == 1) n         uk = n; n       else n         uk--; n     } n     for (int j = 1; j <= n; j++) cout << a[j] <<    ; n     exit(0); n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n template <typename T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n char s[N]; n int len, n, m[N], mx, A[N], B[N][N], cost[10]; n int cnt[10], rnk[N], rk2[N], dp[N][N]; n void init() { n   scanf( %s , s + 1); n   len = strlen(s + 1); n   mx = len; n   for (int i = 1; i <= len; ++i) { n     A[i] = s[len - i + 1] -  0 ; n   } n   read(n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , s + 1); n     m[i] = strlen(s + 1); n     cmax(mx, m[i]); n     for (int j = 1; j <= m[i]; ++j) { n       B[i][j] = s[m[i] - j + 1] -  0 ; n     } n   } n   ++mx; n   for (int i = 0; i <= 9; ++i) { n     read(cost[i]); n   } n } n void solve() { n   for (int i = 1; i <= n; ++i) { n     rnk[i] = i; n   } n   memset(dp, 0x80, sizeof(dp)); n   dp[0][0] = 0; n   for (int d = 1, l, r; d <= mx; ++d) { n     if (A[d] <= 9) { n       l = r = A[d]; n     } else { n       l = (d == len ? 1 : 0); n       r = 9; n     } n     for (int k = l; k <= r; ++k) { n       int sum = 0, jin = 0; n       for (int i = 1; i <= n; ++i) { n         int cur = k + B[i][d]; n         if (!cur) { n           if (d <= m[i] || d <= len) { n             sum += cost[0]; n           } n         } else if (cur > 9) { n           sum += cost[cur - 10]; n           ++jin; n         } else { n           sum += cost[cur]; n         } n       } n       for (int i = 0; i <= n; ++i) { n         if (i) { n           int cur = k + B[rnk[n - i + 1]][d]; n           if (cur == 9) { n             sum += cost[0] - cost[9]; n             ++jin; n           } else { n             if (cur == 0 && d > m[rnk[n - i + 1]] && d > len) { n               sum += cost[1]; n             } else { n               sum += cost[cur % 10 + 1] - cost[cur % 10]; n             } n           } n         } n         cmax(dp[d][jin], dp[d - 1][i] + sum); n       } n     } n     for (int i = 0; i <= 9; ++i) { n       cnt[i] = 0; n     } n     for (int i = 1; i <= n; ++i) { n       ++cnt[B[i][d]]; n     } n     for (int i = 1; i <= 9; ++i) { n       cnt[i] += cnt[i - 1]; n     } n     for (int i = n; i >= 1; --i) { n       rk2[cnt[B[rnk[i]][d]]--] = rnk[i]; n     } n     memcpy(rnk, rk2, sizeof(rnk)); n   } n   printf( %d n , dp[mx][0]); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const long long maxn = 1e4 + 10; n inline long long Max(long long a, long long b) { return a > b ? a : b; } n inline long long Min(long long a, long long b) { return a > b ? b : a; } n namespace FastIO { n inline long long read() { n   long long f = 1, r = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) { n       f = -1; n     } n     c = getchar(); n   } n   while (isdigit(c)) { n     r = 10 * r + c -  0 ; n     c = getchar(); n   } n   return f * r; 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 +  0 ); n } n inline void writesp(long long x) { write(x), putchar(   ); } n inline void writeln(long long x) { write(x), puts(  ); } n };  // namespace FastIO n using namespace FastIO; n long long N; n inline bool ask(long long x1, long long y1, long long x2, long long y2) { n   putchar( ? ), putchar(   ); n   writesp(x1), writesp(y1), writesp(x2), writeln(y2); n   fflush(stdout); n   std::string s; n   std::cin >> s; n   return s ==  YES ; n } n signed main() { n   std::cin >> N; n   long long nowx = 1, nowy = 1; n   std::string s1 =   ; n   while (true) { n     if (nowx + nowy == N + 1) { n       break; n     } n     if (nowx == N) { n       nowy++, s1.push_back( R ); n     } else if (nowy == N) { n       nowx++, s1.push_back( D ); n     } else { n       if (ask(nowx + 1, nowy, N, N)) { n         nowx++, s1.push_back( D ); n       } else { n         nowy++, s1.push_back( R ); n       } n     } n   } n   nowx = N, nowy = N; n   std::string s2 =   ; n   while (true) { n     if (nowx + nowy == N + 1) { n       break; n     } n     if (nowx == 1) { n       nowy--, s2.push_back( R ); n     } else if (nowy == 1) { n       nowx--, s2.push_back( D ); n     } else { n       if (ask(1, 1, nowx, nowy - 1)) { n         nowy--, s2.push_back( R ); n       } else { n         nowx--, s2.push_back( D ); n       } n     } n   } n   std::reverse(s2.begin(), s2.end()); n   putchar( ! ), putchar(   ); n   std::string res = s1 + s2; n   std::cout << res << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long u, long long v) { n   while (v != 0) { n     long long r = u % v; n     u = v; n     v = r; n   } n   return u; n } n typedef struct State { n   char cnt[21]; n   char top[3]; n } State; n inline bool operator<(const State &a, const State &b) { n   return memcmp(&a, &b, sizeof(State)) < 0; n } n int nways[3]; n int need[3][16], gain[3][16]; n char color[3] = { B ,  G ,  R }; n char name[21][4] = { B ,    G ,    R ,    BBB ,  BBG ,  BBR ,  BGB , n                      BGG ,  BGR ,  BRB ,  BRG ,  BRR ,  GBG ,  GBR , n                      GGG ,  GGR ,  GRG ,  GRR ,  RBR ,  RGR ,  RRR }; n void init() { n   memset(nways, 0, sizeof(nways)); n   for (int i = (0); i < (3); ++i) { n     int x = name[i][0] ==  B  ? 0 : name[i][0] ==  G  ? 1 : 2; n     need[x][nways[x]] = i; n     gain[x][nways[x]] = -1; n     ++nways[x]; n   } n   for (int i = (3); i < (21); ++i) n     for (int j = (0); j < (3); ++j) { n       if (j == 2 && name[i][0] == name[i][2]) continue; n       int x = name[i][j] ==  B  ? 0 : name[i][j] ==  G  ? 1 : 2; n       int y = name[i][2 - j] ==  B  ? 0 : name[i][2 - j] ==  G  ? 1 : 2; n       need[x][nways[x]] = i; n       gain[x][nways[x]] = j == 1 ? -1 : y; n       ++nways[x]; n     } n   for (int i = (0); i < (3); ++i) assert(nways[i] == 16); n } n int encode(char s[3]) { n   for (int i = (3); i < (21); ++i) n     if (s[0] == name[i][0] && s[1] == name[i][1] && s[2] == name[i][2]) n       return i; n   assert(false); n   return -1; n } n map<State, double> mem; n double go(State &s) { n   map<State, double>::iterator retit = mem.find(s); n   if (retit != mem.end()) return retit->second; n   char oldtop[3]; n   int gaintop = -1; n   bool fulltop = true; n   for (int k = (0); k < (3); ++k) n     if (s.top[k] ==  ? ) fulltop = false; n   if (fulltop) { n     for (int k = (0); k < (3); ++k) oldtop[k] = s.top[k]; n     gaintop = encode(s.top); n     for (int k = (0); k < (3); ++k) s.top[k] =  ? ; n   } n   int nwait = 1; n   double sum = 0; n   for (int i = (0); i < (3); ++i) { n     int times = i == 2 ? 1 : 2; n     bool any = false; n     double best = -1; n     for (int j = (0); j < (nways[i]); ++j) n       if (s.cnt[need[i][j]] > 0) n         for (int k = (0); k < (3); ++k) { n           if (s.top[k] !=  ?  || k == 2 && s.top[0] ==  ? ) continue; n           s.top[k] = color[i]; n           bool swaptop = false; n           if (k == 2 && s.top[0] > s.top[2]) { n             swaptop = true; n             swap(s.top[0], s.top[2]); n           } n           --s.cnt[need[i][j]]; n           if (gain[i][j] != -1) ++s.cnt[gain[i][j]]; n           if (gaintop != -1) ++s.cnt[gaintop]; n           double cur = go(s); n           if (!any || cur < best) any = true, best = cur; n           if (gaintop != -1) --s.cnt[gaintop]; n           if (gain[i][j] != -1) --s.cnt[gain[i][j]]; n           ++s.cnt[need[i][j]]; n           if (swaptop) swap(s.top[0], s.top[2]); n           s.top[k] =  ? ; n         } n     if (!any) n       nwait += times; n     else n       sum += times * best; n   } n   if (fulltop) { n     for (int k = (0); k < (3); ++k) s.top[k] = oldtop[k]; n   } n   double ret = nwait == 6 ? 0.0 : (6 + sum) / (6 - nwait); n   return mem[s] = ret; n } n char sinit[6][3]; n void run() { n   int n; n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (3); ++j) scanf(  %c , &sinit[i][j]); n   for (int i = (0); i < (n); ++i) n     if (sinit[i][0] > sinit[i][2]) swap(sinit[i][0], sinit[i][2]); n   State init; n   for (int j = (0); j < (21); ++j) init.cnt[j] = 0; n   for (int i = (0); i < (n - 1); ++i) init.cnt[encode(sinit[i])]++; n   for (int k = (0); k < (3); ++k) init.top[k] = sinit[n - 1][k]; n   double ret = go(init); n   printf( %.9lf n , ret); n } n int main() { n   init(); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n long long gcd(long long x, long long y) { n   if (!y) return x; n   return gcd(y, x % y); n } n int main() { n   scanf( %I64d%I64d%I64d , &n, &m, &k); n   if (n * m * 2 % k) { n     printf( NO ); n     return 0; n   } n   long long x = gcd(n * m * 2 / k, n), y = n * m * 2 / k / x; n   if (y > m) x *= 2, y /= 2; n   printf( YES n ); n   printf( %I64d 0 n0 %I64d n0 0 , x, y); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const long long MAX = 1000004; n const long long INF = 1e18L + 5; n template <class T, class U> n void chmin(T &t, const U &u) { n   if (t > u) t = u; n } n template <class T, class U> n void chmax(T &t, const U &u) { n   if (t < u) t = u; n } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << H; n   debug_out(T...); n } n template <typename Arg1> n void pn(Arg1 &&arg1) { n   cout << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void pn(Arg1 &&arg1, Args &&...args) { n   cout << arg1 <<   n ; n   pn(args...); n } n template <typename Arg1> n void ps(Arg1 &&arg1) { n   cout << arg1 <<    ; n } n template <typename Arg1, typename... Args> n void ps(Arg1 &&arg1, Args &&...args) { n   cout << arg1 <<    ; n   ps(args...); n } n template <typename Arg1> n void read(Arg1 &&arg1) { n   cin >> arg1; n } n template <typename Arg1, typename... Args> n void read(Arg1 &&arg1, Args &&...args) { n   cin >> arg1; n   read(args...); n } n long long n, m; n char mat[51][51]; n long long dx[] = {1, 0, -1, 0}; n long long dy[] = {0, 1, 0, -1}; n bool valid(long long x, long long y) { n   if (x >= 0 && y >= 0 && x < n && y < m) return true; n   return false; n } n vector<vector<long long>> vis(51, vector<long long>(51, 0)); n void dfs(long long x, long long y) { n   if (mat[x][y] ==  # ) return; n   if (mat[x][y] ==  G ) { n     mat[x][y] =  . ; n   } n   if (mat[x][y] ==  B ) { n     return; n   } n   vis[x][y] = 1; n   for (long long i = 0; i < 4; ++i) { n     long long tx = x + dx[i]; n     long long ty = y + dy[i]; n     if (valid(tx, ty)) { n       if (!vis[tx][ty]) dfs(tx, ty); n     } n   } n } n void solve() { n   read(n, m); n   bool pos = true; n   for (long long i = 0; i < n; ++i) n     for (long long j = 0; j < m; ++j) read(mat[i][j]); n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < m; ++j) { n       if (mat[i][j] ==  B ) { n         for (long long k = 0; k < 4; ++k) { n           long long tx = i + dx[k]; n           long long ty = j + dy[k]; n           if (valid(tx, ty)) { n             if (mat[tx][ty] ==  G ) { n               pos = false; n             } else { n               if (mat[tx][ty] !=  B ) mat[tx][ty] =  # ; n             } n           } n         } n       } n     } n   } n   if (pos) { n     for (long long i = 0; i < n; ++i) { n       for (long long j = 0; j < m; ++j) { n         if (mat[i][j] ==  G ) { n           for (long long k = 0; k < n; ++k) n             for (long long l = 0; l < m; ++l) vis[k][l] = 0; n           dfs(i, j); n           if (vis[n - 1][m - 1] == 0) { n             pos = false; n           } n         } n       } n     } n     pn(pos ?  Yes  :  No ); n   } else n     pn( No ); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long tc = 1; n   cin >> tc; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 5; n int n, m, fa[N], b[N], ans; n struct edge { n   int u, v, w; n } e[N]; n bool cmp(edge x, edge y) { return x.w > y.w; } n int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int main() { n   scanf( %d%d , &n, &m); n   for (register int i = 1; i <= n + m; ++i) fa[i] = i; n   for (register int i = 1, x, y, w; i <= m; ++i) { n     scanf( %d%d%d , &x, &y, &w); n     e[i] = (edge){x, y, w}; n   } n   sort(e + 1, e + m + 1, cmp); n   for (register int i = 1, x, y; i <= m; ++i) { n     x = find(e[i].u), y = find(e[i].v); n     if (x == y && !b[x]) { n       b[x] = 1; n       ans += e[i].w; n     } else if (x != y && !(b[x] && b[y])) { n       if (b[x] || b[y]) b[y] = 1; n       fa[x] = y; n       ans += e[i].w; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   long long x, y; n   int type; n   void get(int t) { n     cin >> x >> y; n     type = t; n   } n }; n int N, M; n Point R[500], B[500], P[1000], pivot; n bool used[500]; n Point operator-(Point p1, Point p2) { return Point{p1.x - p2.x, p1.y - p2.y}; } n int quadrant(Point p) { n   if (p.x >= 0 && p.y >= 0) return 1; n   if (p.x <= 0 && p.y >= 0) return 2; n   if (p.x <= 0 && p.y <= 0) return 3; n   return 4; n } n long long cross(Point p1, Point p2) { return p1.x * p2.y - p1.y * p2.x; } n bool operator<(Point p1, Point p2) { n   if (quadrant(p1 - pivot) != quadrant(p2 - pivot)) n     return quadrant(p1 - pivot) < quadrant(p2 - pivot); n   return cross(p1 - pivot, p2 - pivot) > 0; n } n int main() { n   while (cin >> N >> M) { n     for (int i = 0; i < N; i++) R[i].get(0); n     for (int i = 0; i < M; i++) B[i].get(1); n     memset(used, 0, sizeof(used)); n     int x = 0; n     for (int i = 0; i < N; i++) { n       used[i] = 1; n       pivot = R[i]; n       int cnt = 0; n       for (int j = 0; j < N; j++) n         if (!used[j]) P[cnt++] = R[j]; n       for (int j = 0; j < M; j++) P[cnt++] = B[j]; n       sort(P, P + cnt); n       for (int j = 0; j < cnt; j++) { n         if (P[j].type == 0) { n           int last = -1; n           for (int k = (j + 1) % cnt; k != j; k = (k + 1) % cnt) { n             bool ok = (last == -1 || cross(P[last] - P[j], P[k] - P[j]) > 0); n             if (P[k].type == 0) { n               if (cross(P[j] - R[i], P[k] - R[i]) < 0) break; n               if (ok) x++; n             } else if (ok) n               last = k; n           } n         } n       } n     } n     cout << x << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   if (n > m) swap(n, m); n   switch (n) { n     case 1: n       cout << m / 6 * 6 + 2 * max(m % 6 - 3, 0); n       break; n     case 2: n       switch (m) { n         case 2: n           cout << 0; n           break; n         case 3: n           cout << 4; n           break; n         case 7: n           cout << 12; n           break; n         default: n           cout << 2 * m; n           break; n       } n       break; n     default: n       cout << 1ll * n * m / 2 * 2; n       break; n   } n   return 0; n } n