#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int pre[N]; n int find(int x) { n   if (!pre[x]) return x; n   return pre[x] = find(pre[x]); n } n int d[510][510]; n struct point { n   int x, y, z; n   void read() { scanf( %d%d%d , &x, &y, &z); } n } a[N]; n int type[N]; n int main() { n   int n, m, r, x, y; n   scanf( %d%d%d , &n, &m, &r); n   int t = 0; n   for (int i = 1; i <= r; i++) { n     scanf( %d , &x); n     while (x--) type[++t] = i; n   } n   for (int i = 1; i <= m; i++) { n     a[i].read(); n     if (a[i].z == 0) { n       x = find(a[i].x); n       y = find(a[i].y); n       if (x != y) pre[x] = y; n     } n   } n   for (int i = 2; i <= n; i++) n     if (type[i] == type[i - 1] && find(i) != find(i - 1)) { n       cout <<  No  << endl; n       return 0; n     } n   memset(d, -1, sizeof(d)); n   for (int i = 1; i <= m; i++) { n     x = type[a[i].x], y = type[a[i].y]; n     if (d[x][y] == -1) n       d[y][x] = d[x][y] = a[i].z; n     else n       d[y][x] = d[x][y] = min(d[x][y], a[i].z); n   } n   for (int i = 1; i <= r; i++) d[i][i] = 0; n   for (int k = 1; k <= r; k++) n     for (int i = 1; i <= r; i++) n       for (int j = 1; j <= r; j++) n         if (d[i][k] != -1 && d[k][j] != -1) n           if (d[i][j] == -1) n             d[i][j] = d[i][k] + d[k][j]; n           else n             d[i][j] = min(d[i][j], d[i][k] + d[k][j]); n   cout <<  Yes  << endl; n   for (int i = 1; i <= r; i++) { n     for (int j = 1; j <= r; j++) { n       printf( %d  , d[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, n, x, y; n   cin >> a >> b >> c >> d >> n; n   if (c < d) swap(a, b), swap(c, d); n   cout << max(0, n - (c - 1) * a - (d - 1) * b) <<    ; n   if (b * d >= n) { n     cout << n / d; n     return 0; n   } n   cout << b + (n - b * d) / c; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int lim = sqrt(n) + 1; n   if (!(n & 1)) { n     cout << n / 2 << endl; n     return 0; n   } n   for (long long int i = 3; i <= lim + 1; i++) { n     if (n % i == 0) { n       cout << (n - i) / 2 + 1 << endl; n       return 0; n     } n   } n   puts( 1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char g[10][10]; n int ax[10 * 10], n; n bool use[10 * 10]; n int q[2][100], ct[2], vst[8][8]; n int dx[9] = {0, 0, 0, 1, -1, 1, 1, -1, -1}; n int dy[9] = {0, 1, -1, 0, 0, -1, 1, -1, 1}; n bool check(int x, int y) { n   if (x < 0 || y < 0 || x >= 8 || y >= 8) return 0; n   if (vst[x][y]) return 0; n   for (int i = 0; i < n; i++) n     if (ax[i] % 8 == y && (ax[i] / 8 == x || ax[i] / 8 == x - 1)) return 0; n   return 1; n } n int bfs() { n   q[0][0] = 7 * 8; n   ct[0] = 1; n   for (int i = 0; i < 10; i++) { n     int id = i & 1, nid = id ^ 1; n     ct[nid] = 0; n     memset(vst, 0, sizeof(vst)); n     for (int j = 0; j < ct[id]; j++) { n       int u = q[id][j]; n       int x = u / 8, y = u % 8; n       for (int d = 0; d < 9; d++) { n         int nx = x + dx[d], ny = y + dy[d]; n         if (check(nx, ny)) q[nid][ct[nid]++] = nx * 8 + ny, vst[nx][ny] = 1; n         if (nx == 0 && ny == 7) return 1; n       } n     } n     for (int j = 0; j < n; j++) ax[j] = (ax[j] / 8 + 1) * 8 + ax[j] % 8; n   } n   if (ct[0]) return 1; n   return 0; n } n int main() { n   n = 0; n   for (int i = 0; i < 8; i++) { n     scanf( %s , g[i]); n     for (int j = 0; j < 8; j++) n       if (g[i][j] ==  S ) ax[n++] = i * 8 + j; n   } n   if (bfs()) n     puts( WIN ); n   else n     puts( LOSE ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n bool conn[8][8]; n int a, b; n int v[8]; n int cur; n bool used[8][8]; n int best, cur2, cur3, cur4, ans; n int main() { n   cin >> N >> M; n   for (int i = 0; i < M; i++) { n     cin >> a >> b; n     conn[a][b] = conn[b][a] = true; n   } n   for (int i = 0; i < 279936; i++) { n     cur = i; n     for (int j = 0; j < 6; j++) { n       for (int k = 0; k < 6; k++) { n         used[j][k] = false; n       } n     } n     for (int j = 0; j < 7; j++) { n       v[j] = cur % 6; n       cur /= 6; n     } n     cur2 = 0; n     for (int j = 1; j <= N; j++) { n       for (int k = j + 1; k <= N; k++) { n         if (conn[j][k]) { n           cur3 = v[j]; n           cur4 = v[k]; n           if (cur3 > cur4) { n             swap(cur3, cur4); n           } n           if (!used[cur3][cur4]) { n             ++cur2; n             used[cur3][cur4] = true; n           } n         } n       } n     } n     ans = max(ans, cur2); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int lar(long long int a, long long int b) { n   return ((a >= b) ? a : b); n } n long long int les(long long int a, long long int b) { n   return ((a <= b) ? a : b); n } n int main() { n   string s; n   cin >> s; n   vector<string> v; n   long long int a = 0, b = 0; n   for (long long int i = 0; i < s.size(); i++) { n     if (s[i] ==  4 ) n       a++; n     else if (s[i] ==  7 ) n       b++; n   } n   if (a == 0 && b == 0) { n     cout << -1 << endl; n     return 0; n   } n   if (a >= b) n     cout <<  4  << endl; n   else n     cout <<  7  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[200005]; n bool can(long long val, long long n, long long k) { n   int m = (n + 1) / 2; n   long long sum = 0; n   for (int i = m; i <= n; i++) { n     if (val > arr[i]) n       sum += val - arr[i]; n     else n       break; n   } n   if (sum <= k) n     return true; n   else n     return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   sort(arr + 1, arr + n + 1); n   long long l = arr[(n + 1) / 2]; n   long long h = 3000000001; n   while (l < h - 1) { n     long long m = (l + h) / 2; n     if (can(m, n, k)) { n       l = m; n     } else { n       h = m; n     } n   } n   if (can(h, n, k)) l = h; n   cout << l <<   n ; n } n


#include <bits/stdc++.h> n const int oo = 0x3f3f3f3f; n template <typename T> n inline bool chkmax(T &a, T b) { n   return a < b ? a = b, true : false; n } n template <typename T> n inline bool chkmin(T &a, T b) { n   return a > b ? a = b, true : false; n } n template <typename T> n T read(T &first) { n   int f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (first = 0; isdigit(ch); ch = getchar()) first = 10 * first + ch -  0 ; n   return first *= f; n } n template <typename T> n void write(T first) { n   if (first < 0) { n     putchar( - ); n     first = -first; n   } n   static char s[20]; n   int top = 0; n   for (; first; first /= 10) s[++top] = first % 10 +  0 ; n   if (top == 0) n     putchar( 0 ); n   else n     while (top) putchar(s[top--]); n } n const int MAXN = 1e5 + 5, MAXK = 6; n struct Line { n   int a, b, c; n   int id; n   bool on(const std::pair<double, double> &p) { n     static double EPS = 1e-8; n     return std::fabs(a * p.first + b * p.second + c) < EPS; n   } n }; n int N, K; n Line A[MAXN]; n std::pair<int, int> ans[MAXN]; n int anssize; n int st[MAXN]; n inline bool intersection(const Line &a, const Line &b, n                          std::pair<double, double> &p) { n   long long d = (long long)a.b * b.a - (long long)b.b * a.a; n   if (d == 0) return false; n   p.first = (double)((long long)b.b * a.c - (long long)a.b * b.c) / d; n   p.second = (double)((long long)b.c * a.a - (long long)a.c * b.a) / d; n   return true; n } n bool dfs(int n, int k) { n   if (n <= k) { n     for (int i = 0; i < n; ++i) { n       ans[anssize++] = std::make_pair(A[i].id + 1, -1); n     } n     return true; n   } n   if (k == 0) return false; n   for (int i = n - k - 1; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       std::pair<double, double> p; n       if (intersection(A[i], A[j], p) == false) continue; n       ans[anssize++] = std::make_pair(A[i].id + 1, A[j].id + 1); n       int m = n, top = 0; n       for (int k = n - 1; k >= 0; --k) { n         if (A[k].on(p)) { n           st[++top] = k; n           std::swap(A[k], A[--m]); n         } n       } n       if (dfs(m, k - 1) == true) return true; n       --anssize; n       while (top > 0) { n         std::swap(A[st[top--]], A[m++]); n       } n     } n   } n   return false; n } n void input() { n   read(N); n   read(K); n   for (int i = 0; i < N; ++i) { n     read(A[i].a); n     read(A[i].b); n     read(A[i].c); n     A[i].id = i; n   } n } n void solve() { n   srand(time(NULL)); n   if (N >= K * K * K) { n     int times = 100; n     while (times-- && N > 0 && K > 0) { n       int i = rand() % N; n       int j = rand() % N; n       std::pair<double, double> p; n       if (i == j || intersection(A[i], A[j], p) == false) continue; n       int top = 0; n       for (int k = N - 1; k >= 0; --k) { n         if (A[k].on(p)) st[++top] = k; n       } n       if (top > K) { n         ans[anssize++] = std::make_pair(A[i].id + 1, A[j].id + 1); n         for (int k = 1; k <= top; ++k) { n           A[st[k]] = A[--N]; n         } n         --K; n       } n     } n   } n   bool haveAns = dfs(N, K); n   if (haveAns) { n     puts( YES ); n     write(anssize); n     putchar(  n ); n     for (int i = 0; i < anssize; ++i) { n       write(ans[i].first); n       putchar(   ); n       write(ans[i].second); n       putchar(  n ); n     } n   } else n     puts( NO ); n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int num = 0; n   int n; n   cin >> n; n   int d; n   int sh[n]; n   cin >> d; n   for (int i = 0; i < n; i++) cin >> sh[i]; n   for (int i = 0; i < n; i++) { n     for (int k = i + 1; k < n; k++) { n       if (abs(sh[i] - sh[k]) <= d) num += 2; n     } n   } n   cout << num; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> Map; n int n, m, i, j; n long long ans; n string s; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> s; n     m = s.size(); n     bool cnt[30]; n     for (j = 0; j < 26; j++) cnt[j] = 0; n     for (j = 0; j < m; j++) cnt[s[j] -  a ] ^= 1; n     int aux1 = 0; n     for (j = 0; j < 26; j++) { n       aux1 <<= 1; n       aux1 |= cnt[j]; n     } n     for (j = 0; j < 26; j++) ans += Map[aux1 ^ (1 << j)]; n     ans += Map[aux1]; n     Map[aux1]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int n; n   scanf( %lld , &n); n   printf( %lld n , n / 2 + 1); n   long long int count1 = 0; n   long long int count2 = 1; n   for (int i = 1; i <= n; i++) { n     if (i % 2 == 0) { n       count2++; n     } else { n       count1++; n     } n     printf( %lld %lld n , count1, count2); n   } n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-4, pi = acos(-1.0); n const int maxS = 1e3 + 13; n const int maxT = 20; n const long long MOD = 1e9 + 7; n using namespace std; n struct point { n   int x, y; n }; n struct rs { n   int x, y, z; n }; n int n, m, i, j, k, x, y, z, S, T, ii, s, t, l, r, pos, len; n int tot = 0, ans = 0, res = 0, kase = 0, cnt = 0; n bool chk[maxS][maxS], flag = true; n string s1, s2; n char ch[maxS][maxS]; n vector<rs> v; n point dr[5] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {0, 0}}; n bool fk(int x, int y, int len) { n   if (x - len < 0 || y - len < 0 || x + len >= n || y + len >= m) return false; n   int i; n   for (i = x - len; i <= x + len; i++) n     if (ch[i][y] ==  . ) return false; n   for (i = y - len; i <= y + len; i++) n     if (ch[x][i] ==  . ) return false; n   for (i = x - len; i <= x + len; i++) chk[i][y] = false; n   for (i = y - len; i <= y + len; i++) chk[x][i] = false; n   v.push_back({x, y, len}); n   return true; n } n bool ok(int x, int y) { n   int i, j, k, len; n   for (k = 0; k < 5; k++) { n     for (i = 1; i < max(n, m); i++) n       if (fk(x + dr[k].x * i, y + dr[k].y * i, i)) return true; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       cin >> ch[i][j]; n       if (ch[i][j] ==  * ) chk[i][j] = true; n     } n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) n       if (flag && chk[i][j] && !ok(i, j)) flag = false; n   if (!flag) n     cout << -1 << endl; n   else { n     cout << v.size() << endl; n     for (auto t : v) cout << t.x + 1 <<     << t.y + 1 <<     << t.z << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n  n #define int long long n using namespace std; n  n const int MAX = 505; n int n, MOD, fact[MAX], C[MAX][MAX], f[2][MAX * MAX]; n  n int getMaxInv(int x) { n     return (x - 1) * x / 2; n } n  n int32_t main() { n #ifdef ACM n     freopen( input ,  r , stdin); n #endif n     cin >> n >> MOD; n     fact[0] = 1; n     for (int i = 1; i <= n; i++) n         fact[i] = fact[i - 1] * i % MOD; n     C[0][0] = 1; n     for (int i = 1; i <= n; i++) { n         C[i][0] = C[i][i] = 1; n         for (int j = 1; j < n; j++) n             C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; n     } n     int rs = 0; n     for (int i = 0; i < n; i++) { n         for (int j = 1; j <= getMaxInv(n - 1); j++) n             f[i % 2][j] = 0; n         f[i % 2][0] = 1; n         for (int inv = 1; inv <= getMaxInv(i); inv++) { n             int l = inv - min(i, inv + 1) + 1, r = inv; n             f[i % 2][inv] = (f[1 - i % 2][r] - (l == 0 ? 0 : f[1 - i % 2][l - 1]) + MOD) % MOD; n         } n         for (int j = 1; j <= getMaxInv(n - 1); j++) n             f[i % 2][j] = (f[i % 2][j] + f[i % 2][j - 1]) % MOD; n         for (int inv = 2; inv <= getMaxInv(i); inv++) { n             int t1 = (f[i % 2][inv] - f[i % 2][inv - 1] + MOD) % MOD; n             for (int t = 1; t <= min(i, inv - 1); t++) { n                 int t2 = f[i % 2][inv - t - 1]; n                 int pickCnt = i - t + 1; n                 rs = (rs + t1 * t2 % MOD * pickCnt % MOD * fact[n - i - 1] % MOD * C[n][i + 1]) % MOD; n             } n         } n     } n     cout << rs; n }


#include <bits/stdc++.h> n using namespace std; n pair<int, int> s[300005]; n int main() { n   int n, a, b, c, d, start, len; n   scanf( %d %d %d %d %d %d %d , &n, &a, &b, &c, &d, &start, &len); n   for (int i = 0; i < n; i++) scanf( %d %d , &s[i].first, &s[i].second); n   long long sum = 0, res = 0, ans = start; n   int j = 0; n   for (int i = 0; i < n; i++) { n     while (j < n && s[j].first - s[i].first < len) { n       sum += (s[j].second ? c : -d); n       res = min(res, sum); n       j++; n     } n     if (ans + res >= 0) { n       if (!i) n         printf( %d n , 0); n       else n         printf( %d n , s[i - 1].first + 1); n       return 0; n     } n     ans += (s[i].second == 0 ? -b : a); n     sum -= (s[i].second == 0 ? -d : c); n     res -= (s[i].second == 0 ? -d : c); n     if (ans < 0) { n       printf( -1 n ); n       return 0; n     } n   } n   printf( %d n , s[n - 1].first + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int x1, y1, x2, y2; n   scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n   int res = abs(x1 - x2) + 1 + (x1 == x2); n   res += abs(y1 - y2) + 1 + (y1 == y2); n   printf( %d n , res * 2); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n long long int tc, cnt = 0, number, many, total = 0, query, require; n int rmove[8] = {1, 0, -1, 0, -1, 1, -1, 1}; n int cmove[8] = {0, 1, 0, -1, 1, -1, -1, 1}; n long long int modpow(long long int x, long long int y, long long int m) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % m; n     x = (x * x) % m; n     y >>= 1; n   } n   return res; n } n long long int dist[305][305], path[305][305]; n void FloydWarshall() { n   for (int i = 0; i < many; i++) { n     for (int j = 0; j < many; j++) { n       for (int k = 0; k < many; k++) { n         if (dist[j][i] + dist[i][k] < dist[j][k]) { n           dist[j][k] = dist[j][i] + dist[i][k]; n           path[j][k] = path[i][k]; n         } n       } n     } n   } n } n vector<int> answer; n void printPath(int a, int b) { n   if (path[a][b] == a) return; n   printPath(a, path[a][b]); n   answer.push_back(path[a][b]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cin >> many; n   for (int i = 0; i < many; i++) { n     for (int j = 0; j < many; j++) { n       dist[i][j] = 1e18; n       path[i][j] = i; n     } n     dist[i][i] = 0; n     path[i][i] = -1; n   } n   for (int i = 0; i < many - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     dist[a][b] = 1; n     dist[b][a] = 1; n   } n   for (int i = 1; i < many; i++) { n     int sum = 0; n     for (int j = 0; j < many; j++) { n       if (i == j) continue; n       if (dist[i][j] == 1) sum++; n     } n     if (sum == 1) number++; n   } n   vector<int> vec; n   vec.resize(number); n   for (int i = 0; i < number; i++) { n     cin >> vec[i]; n     vec[i]--; n   } n   FloydWarshall(); n   int now = 0; n   answer.push_back(now); n   for (int i = 0; i < number; i++) { n     printPath(now, vec[i]); n     answer.push_back(vec[i]); n     now = vec[i]; n   } n   printPath(now, 0); n   answer.push_back(0); n   if (answer.size() == 2 * many - 1) { n     for (int i = 0; i < answer.size(); i++) cout << answer[i] + 1 <<    ; n   } else n     cout << -1; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, k; n int arr[51]; n char color[51]; n int mem[51][2501]; n int solve(int i, char last, int num, int sum) { n   if (sum >= k) { n     return 0; n   } n   if (~mem[i][sum]) return mem[i][sum]; n   int ch1 = 1e9; n   for (int j = 0; j < n; j++) { n     if (arr[j] > num && last != color[j]) n       ch1 = min(ch1, solve(j, color[j], arr[j], sum + arr[j]) + abs(i - j)); n   } n   return mem[i][sum] = ch1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   srand(time(NULL)); n   cin >> n >> s >> k; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < n; i++) { n     cin >> color[i]; n   } n   memset(mem, -1, sizeof mem); n   int ans = 1e9; n   for (int i = 0; i < n; i++) { n     ans = min(solve(i, color[i], arr[i], arr[i]) + abs(s - 1 - i), ans); n   } n   if (ans != 1e9) n     cout << ans <<   n ; n   else n     cout << -1 <<   n ; n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n const long long N = 1e6 + 17, mo = 1e9 + 7; n long long f[N][2], n, h[N]; n inline long long min(long long x, long long y) { return x > y ? y : x; } n inline long long quickmod(long long x) { return x >= mo ? x - mo : x; } n signed main() { n   std::ios::sync_with_stdio(0); n   cin >> n; n   for (long long i = 1; i <= n; ++i) { n     cin >> h[i]; n     --h[i]; n   } n   f[1][0] = h[1]; n   f[1][1] = min(h[1], h[2]); n   long long ans = f[1][0]; n   for (long long i = 2; i <= n; ++i) { n     f[i][0] = quickmod(h[i] + min(h[i], h[i - 1]) * f[i - 1][1] % mo); n     f[i][1] = quickmod(min(h[i], h[i + 1]) + n                        min(min(h[i], h[i - 1]), h[i + 1]) * f[i - 1][1] % mo); n     ans = quickmod(ans + f[i][0]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n int head[444444], to[444444], cap[444444], dist[444444], nxt[444444], n     cur[444444], tnode[444444], mx, ans, tp, cnp, L[444444], R[444444]; n int n, m, s, t, cnt, x, y, z; n bool used[444444]; n void addedge(int s, int t, int cps, int cpt) { n   cnt++; n   nxt[cnt] = head[s]; n   head[s] = cnt; n   to[cnt] = t; n   cap[cnt] = cps; n   cnt++; n   nxt[cnt] = head[t]; n   head[t] = cnt; n   to[cnt] = s; n   cap[cnt] = cpt; n } n void bfs() { n   memset(dist, -1, sizeof(dist)); n   queue<int> q; n   q.push(s); n   dist[s] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = head[x]; i; i = nxt[i]) { n       if (!cap[i]) continue; n       if (!~dist[to[i]]) { n         dist[to[i]] = dist[x] + 1; n         q.push(to[i]); n       } n     } n   } n } n int dfs(int i, int f) { n   if (i == t) return f; n   int tmp = f; n   for (int &j = cur[i]; j; j = nxt[j]) { n     if (!cap[j] || dist[to[j]] != dist[i] + 1) continue; n     int d = dfs(to[j], min(cap[j], f)); n     if (d) { n       cap[j] -= d; n       cap[j ^ 1] += d; n       tmp -= d; n       if (!tmp) break; n     } else n       dist[to[j]] = 0; n   } n   return f - tmp; n } n int dinic() { n   int res = 0; n   while (1) { n     for (int i = 1; i <= t; i++) cur[i] = head[i]; n     bfs(); n     if (!~dist[t]) break; n     res += dfs(s, 1e9); n   } n   return res; n } n void build(int l, int r, int node) { n   mx = max(mx, node); n   L[node] = l; n   R[node] = r; n   if (l == r) { n     tnode[l] = node; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, node << 1); n   build(mid + 1, r, (node << 1) | 1); n   addedge(node, node << 1, inf, 0); n   addedge(node, (node << 1) | 1, inf, 0); n } n void update(int beg, int en, int l, int r, int node, int p) { n   if (l > en || r < beg) return; n   if (l >= beg && r <= en) { n     addedge(p, node, 1, 0); n     return; n   } n   int mid = (l + r) >> 1; n   update(beg, en, l, mid, node << 1, p); n   update(beg, en, mid + 1, r, (node << 1) | 1, p); n } n int Search(int l, int r, int node) { n   if (l == r) return l; n   int mid = (l + r) >> 1; n   for (int j = head[node]; j; j = nxt[j]) { n     if (cap[j ^ 1] && n         ((to[j] == (node << 1)) || (to[j] == ((node << 1) | 1)))) { n       cap[j ^ 1]--; n       if (to[j] == (node << 1)) return Search(l, mid, node << 1); n       return Search(mid + 1, r, (node << 1) | 1); n     } n   } n } n int main() { n   cnt = 1; n   scanf( %d%d , &n, &m); n   build(1, m, 1); n   s = n + mx + 1; n   t = s + 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &tp); n     if (!tp) { n       scanf( %d , &cnp); n       for (int j = 1; j <= cnp; j++) { n         scanf( %d , &x); n         addedge(mx + i, tnode[x], 1, 0); n       } n       addedge(s, mx + i, 1, 0); n     } else if (tp == 1) { n       scanf( %d%d , &x, &y); n       update(x, y, 1, m, 1, mx + i); n       addedge(s, mx + i, 1, 0); n     } else { n       scanf( %d%d%d , &x, &y, &z); n       used[x] = used[y] = 1; n       ans += 2; n       addedge(mx + i, tnode[x], 0, 1); n       addedge(mx + i, tnode[y], 0, 1); n       addedge(mx + i, tnode[z], 1, 0); n       addedge(s, mx + i, 0, 2); n     } n   } n   for (int i = 1; i <= m; i++) { n     if (!used[i]) n       addedge(tnode[i], t, 1, 0); n     else n       addedge(tnode[i], t, 0, 1); n   } n   ans += dinic(); n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) { n     for (int j = head[mx + i]; j; j = nxt[j]) { n       if (!cap[j] && to[j] != s) { n         printf( %d %d n , i, Search(L[to[j]], R[to[j]], to[j])); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<set<int> > arr; n long long res = 0; n void dfs(int vertex, int prev, int level) { n   for (int v : arr[vertex]) { n     if (v == prev) { n       continue; n     } n     if (level == 2) { n       res++; n       continue; n     } else { n       dfs(v, vertex, level + 1); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   arr = vector<set<int> >(n + 1); n   for (int i = 0; i < n; ++i) { n     int a, b; n     cin >> a >> b; n     arr[a].insert(b); n     arr[b].insert(a); n   } n   for (int i = 1; i <= n; ++i) { n     dfs(i, -1, 1); n   } n   cout << res / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int arr[1001] = {0}; n   int n, i, others, temp; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &temp); n     arr[temp]++; n   } n   for (i = 0; i < 1001; i++) { n     if (!arr[i]) continue; n     others = n - arr[i]; n     if (arr[i] - others > 1) { n       puts( NO ); n       getchar(); n       getchar(); n       return 0; n     } n   } n   puts( YES ); n   getchar(); n   getchar(); 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 n, k, ans = 1, p, temp; n   cin >> n >> k; n   if (n == k) { n     cout << 1; n     return 0; n   } n   if (k == 1) { n     cout << n; n     return 0; n   } n   long long s, e, m; n   s = 1, e = (n - 1) / 2; n   while (s <= e) { n     m = (s + e) / 2; n     p = log2((n + 1) / (2 * m + 2)) + 1; n     temp = 2 * ((1LL << p) - 1) + max(1 + n - (1LL << p) * 2 * m, 0LL); n     if (temp >= k) { n       s = m + 1; n       ans = max(ans, 2 * m); n     } else n       e = m - 1; n   } n   s = 0, e = (n - 3) / 4; n   while (s <= e) { n     m = (s + e) / 2; n     p = log2((n + 1) / (4 * m + 4)) + 1; n     temp = 1 + 2 * ((1LL << p) - 1) + n            max(1 + n - (1LL << (p + 1)) * (2 * m + 1), 0LL); n     if (temp >= k) { n       s = m + 1; n       ans = max(ans, 2 * m + 1); n     } else n       e = m - 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops, no-stack-protector ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const int INF = int(1e9) + 99; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cout.tie(0); n   int n; n   string s; n   cin >> n >> s; n   vector<int> max_0(n + 1, 0); n   vector<int> max_1(n + 1, 0); n   for (int i = n - 1; i >= 0; --i) { n     if (s[i] !=  0 ) max_1[i] = 1 + max_1[i + 1]; n     if (s[i] !=  1 ) max_0[i] = 1 + max_0[i + 1]; n   } n   unordered_map<int, vector<int>> pos_0; n   unordered_map<int, vector<int>> pos_1; n   int c = 0; n   while (c < n) { n     if (c == 0 || s[c - 1] ==  1 ) { n       for (int len = 1; len <= max_0[c]; ++len) { n         pos_0[len].push_back(c); n       } n       c += max(max_0[c], 1); n     } else { n       ++c; n     } n   } n   c = 0; n   while (c < n) { n     if (c == 0 || s[c - 1] ==  0 ) { n       for (int len = 1; len <= max_1[c]; ++len) { n         pos_1[len].push_back(c); n       } n       c += max(max_1[c], 1); n     } else { n       ++c; n     } n   } n   for (int len = 1; len <= n; ++len) { n     int pos = 0; n     int ans = 0; n     int index_0 = 0; n     int index_1 = 0; n     while (pos < n) { n       int npos = INF; n       if (max_0[pos] >= len) { n         npos = min(npos, pos + len); n       } n       if (max_1[pos] >= len) { n         npos = min(npos, pos + len); n       } n       while ((index_0 < (int)pos_0[len].size()) && (pos > pos_0[len][index_0])) n         ++index_0; n       while ((index_1 < (int)pos_1[len].size()) && (pos > pos_1[len][index_1])) n         ++index_1; n       if (index_0 < (int)pos_0[len].size()) { n         npos = min(npos, pos_0[len][index_0] + len); n       } n       if (index_1 < (int)pos_1[len].size()) { n         npos = min(npos, pos_1[len][index_1] + len); n       } n       if (npos != INF) { n         ++ans; n       } n       pos = npos; n     } n     cout << ans <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n int main() { n   int x, y, z, i, j, k, a, b, c, n, m, t, mat[210210]; n   scanf( %d , &t); n   long long w, sum; n   while (t--) { n     scanf( %d %lld , &n, &w); n     j = -1; n     sum = 0; n     c = 0; n     for (x = 0; x < n; x++) { n       scanf( %d , &mat[x]); n       if (mat[x] >= (w + 1) / 2 && mat[x] <= w) j = x; n       if (mat[x] <= w) sum += mat[x]; n     } n     if (j != -1) n       printf( 1 n%d n , j + 1); n     else if (sum < (w + 1) / 2) n       puts( -1 ); n     else { n       x = -1; n       if (sum > w) { n         for (x = 0; x < n; x++) { n           if (mat[x] > w) continue; n           sum -= mat[x]; n           if (sum >= (w + 1) / 2 && sum <= w) break; n         } n       } n       for (y = x + 1; y < n; y++) n         if (mat[y] <= w) c++; n       printf( %d n , c); n       for (y = x + 1; y < n; y++) n         if (mat[y] <= w) printf( %d  , y + 1); n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int num; n   std::cin >> num; n   int arr1[num], arr2[num], sum1 = 0, sum2 = 0; n   for (int i = 0; i < num; i++) { n     std::cin >> arr1[i]; n     sum1 |= arr1[i]; n   } n   for (int i = 0; i < num; i++) { n     std::cin >> arr2[i]; n     sum2 |= arr2[i]; n   } n   std::cout << sum1 + sum2 << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 37; n const long long INF = 1LL << 61; n int n, u, r; n long long sum; n int a[MAX_N], b[MAX_N], k[MAX_N], p[MAX_N]; n void dfs(int dep, bool f) { n   if ((u - dep) % 2 == 0) { n     long long s = 0; n     for (int i = 1; i <= n; ++i) { n       s += (long long)a[i] * k[i]; n     } n     sum = max(sum, s); n   } n   if (dep == u) return; n   int t[MAX_N] = {0}; n   for (int i = 1; i <= n; ++i) { n     t[i] = a[i]; n   } n   if (!f) { n     for (int i = 1; i <= n; ++i) { n       a[i] = t[i] ^ b[i]; n     } n     dfs(dep + 1, true); n   } n   for (int i = 1; i <= n; ++i) { n     a[i] = t[p[i]] + r; n   } n   dfs(dep + 1, false); n   for (int i = 1; i <= n; ++i) { n     a[i] = t[i]; n   } n } n int main() { n   scanf( %d%d%d , &n, &u, &r); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , b + i); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , k + i); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , p + i); n   } n   sum = -INF; n   dfs(0, 0); n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, A[100005], B[100005], C[100005], sum, val; n pair<long long, long long> E[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n     C[A[i]]++; n   } n   for (int i = 1; i <= k; i++) { n     if (C[i] == 0) val++; n   } n   for (int i = 1; i <= n; i++) { n     cin >> B[i]; n     E[i].first = B[i]; n     E[i].second = A[i]; n   } n   sort(E + 1, E + n + 1); n   k = 1; n   for (int i = 1; k <= val; i++) { n     if (C[E[i].second] > 1) { n       sum += E[i].first; n       C[E[i].second]--; n       k++; n     } n   } n   cout << sum <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long T, n, s, sum; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int main() { n   cin >> T; n   while (T--) { n     cin >> n; n     sum = 0; n     long long a[100010] = {0}; n     for (int i = 1; i <= n; i++) { n       cin >> s; n       a[i] += s; n       if (a[i] > 0) a[i + 1] += a[i]; n       if (a[i] < 0) sum -= a[i]; n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1000005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int a[n + 1]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   dp[0] = 1; n   for (int i = 0; i < n; i++) { n     set<int> s; n     for (int j = 1; j * j <= a[i]; j++) { n       if (a[i] % j == 0) { n         s.insert(j); n         s.insert(a[i] / j); n       } n     } n     for (set<int>::reverse_iterator it = s.rbegin(); it != s.rend(); it++) { n       dp[*it] = (dp[*it] + dp[*it - 1]) % 1000000007; n     } n   } n   long long int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = (ans + dp[i]) % 1000000007; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n long long p = 1e9 + 7, x; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> x; n   while (cin >> x) n     cout << ((((x + 4) / 4) % p) * (((x + 6) / 4) % p)) % p <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxn = 200005, mod = pow(10, 9) + 7; n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void create_array(vector<long long int>& a) { n   for (long long int i = 1;; i++) { n     if (i % 3 != 0 and i % 10 != 3) { n       a.push_back(i); n     } n     if (a.size() == 1000) break; n   } n } n void solve(vector<long long int>& a) { n   long long int k; n   cin >> k; n   cout << a[k - 1] << endl; n } n int main() { n   fast(); n   long long int t = 1; n   vector<long long int> a; n   create_array(a); n   cin >> t; n   while (t--) { n     solve(a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1000000007; n int main() { n   long long n, s, a, t1, t2; n   cin >> n; n   s = n * 2; n   vector<long long> v(s); n   for (int i = 0; i < s; i++) { n     cin >> a; n     v[i] = a; n   } n   sort(v.begin(), v.end(), greater<long long>()); n   t1 = 0; n   t2 = 0; n   for (int i = 0; i < n; i++) { n     t1 += v[i]; n   } n   for (int i = v.size() - 1; i >= n; i--) { n     t2 += v[i]; n   } n   if (t1 == t2) { n     cout << -1 <<   n ; n   } else { n     for (int i = 0; i < v.size(); i++) { n       cout << v[i] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void segmentedSieve(long long L, long long R) { n   long long lim = sqrt(R); n   vector<bool> mark(lim + 1, false); n   vector<long long> primes; n   for (long long i = 2; i <= lim; ++i) { n     if (!mark[i]) { n       primes.emplace_back(i); n       for (long long j = i * i; j <= lim; j += i) mark[j] = true; n     } n   } n   int buenos = 0; n   long long len = ceil(sqrt(R - L + 1)); n   for (long long bloques = 0; bloques < len; ++bloques) { n     long long l = L + len * bloques + 1; n     if (bloques == 0) l = L; n     long long r = min(R, L + len * (bloques + 1)); n     if (l > r) break; n     vector<bool> isPrime(r - l + 1, true); n     for (long long i : primes) n       for (long long j = max(i * i, (l + i - 1) / i * i); j <= r; j += i) n         isPrime[j - l] = false; n     if (l == 1) isPrime[0] = false; n     for (long long i = l; i <= r; ++i) { n       if (i == 2 || (isPrime[i - l] && i % 4 == 1)) { n         buenos++; n       } n     } n   } n   cout << buenos <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << setprecision(20) << fixed; n   long long l, r; n   cin >> l >> r; n   segmentedSieve(l, r); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   int n, k, i, o; n   cin >> n >> k; n   o = n - k; n   for (i = 1; i <= o - 1; i++) cout << i <<    ; n   for (i = n; i >= o; i--) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T By(T x, T y, T P) { n   T F1 = 0; n   while (y) { n     if (y & 1) { n       F1 += x; n       if (F1 < 0 || F1 >= P) F1 -= P; n     } n     x <<= 1; n     if (x < 0 || x >= P) x -= P; n     y >>= 1; n   } n   return F1; n } n template <class T> n T Mul(T x, T y, T P) { n   T F1 = 1; n   x %= P; n   while (y) { n     if (y & 1) { n       F1 = By(F1, x, P); n     } n     x = By(x, x, P); n     y >>= 1; n   } n   return F1; n } n template <class T> n T Gcd(T x, T y) { n   if (y == 0) return x; n   T z; n   while (z = x % y) { n     x = y, y = z; n   } n   return y; n } n struct EDGE { n   int T; n   EDGE *Nxt; n }; n template <class T> n void UpdataMin(T &x, T y) { n   if (y < x) { n     x = y; n   } n } n template <class T> n void UpdataMax(T &x, T y) { n   if (x < y) { n     x = y; n   } n } n int Dp[61][61][61]; n int Map[61][61][61]; n int NM[61][61]; n int N, M, R, I, J, K; n int main() { n   memset(Dp, 63, sizeof(Dp)); n   memset(NM, 63, sizeof(NM)); n   scanf( %d%d%d , &N, &M, &R); n   for (int i = 0; i < (M); ++i) { n     for (int j = 1; j <= (N); ++j) n       for (int k = 1; k <= (N); ++k) scanf( %d , &Map[i][j][k]); n     for (int l = 1; l <= (N); ++l) n       for (int j = 1; j <= (N); ++j) n         for (int k = 1; k <= (N); ++k) n           if (Map[i][j][l] + Map[i][l][k] < Map[i][j][k]) n             Map[i][j][k] = Map[i][j][l] + Map[i][l][k]; n     for (int j = 1; j <= (N); ++j) n       for (int k = 1; k <= (N); ++k) UpdataMin(NM[j][k], Map[i][j][k]); n   } n   for (int i = 1; i <= (N); ++i) n     for (int j = 1; j <= (N); ++j) Dp[0][i][j] = NM[i][j]; n   for (int i = 1; i <= (60); ++i) n     for (int j = 1; j <= (N); ++j) n       for (int k = 1; k <= (N); ++k) n         for (int l = 1; l <= (N); ++l) n           UpdataMin(Dp[i][j][l], Dp[i - 1][j][k] + NM[k][l]); n   while (R--) { n     scanf( %d%d%d , &I, &J, &K); n     if (K > 60) K = 60; n     printf( %d n , Dp[K][I][J]); n   } n   scanf( %d , &N); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int maxn = 2e5 + 10; n const int inf = 0x3f3f3f3f; n const double eps = 1e-6; n int n, a[maxn]; n int num[maxn]; n int pre[maxn]; n int lst[maxn]; n int up = 500; n int mx = 0; n int c0[maxn], c[maxn]; n void add(int x) { n   c0[c[x]]--; n   c[x]++; n   c0[c[x]]++; n   mx = max(mx, c[x]); n } n void sub(int x) { n   c0[c[x]]--; n   c[x]--; n   c0[c[x]]++; n   mx -= (c0[mx] == 0); n } n int solve1(int D) { n   int res = 0; n   for (int i = 1; i <= up; i++) { n     memset(c, 0, sizeof(c)), memset(c0, 0, sizeof(c0)); n     mx = 0; n     int k = 1, x = 0; n     for (int j = 1; j <= n; j++) { n       while (k <= n && x + (a[k] == D) <= i) add(a[k]), x += (a[k] == D), k++; n       if (c0[mx] >= 2) res = max(res, k - j); n       sub(a[j]), x -= (a[j] == D); n     } n   } n   return res; n } n int solve2(int D, int V) { n   int res = 0; n   memset(pre, 0, sizeof(pre)); n   memset(lst, -1, sizeof(lst)); n   for (int i = 1; i <= n; i++) { n     if (a[i] == D) n       pre[i] = pre[i - 1] + 1; n     else if (a[i] == V) n       pre[i] = pre[i - 1] - 1; n     else n       pre[i] = pre[i - 1]; n   } n   for (int i = n; i >= 1; i--) lst[pre[i]] = i; n   lst[0] = 0; n   for (int i = 1; i <= n; i++) res = max(res, i - lst[pre[i]]); n   return res; n } n int main() { n   std::ios::sync_with_stdio(false), cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i], num[a[i]]++; n   int mx = 0, mx_id = 0, cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (num[i] > mx) n       mx = num[i], mx_id = i, cnt = 1; n     else if (num[i] == mx) n       cnt++; n   } n   if (cnt > 1) { n     cout << n <<   n ; n     return 0; n   } n   int D = mx_id; n   int ans = 0; n   ans = max(ans, solve1(D)); n   for (int V = 1; V <= n; V++) { n     if (V == D) continue; n     if (num[V] > up) ans = max(ans, solve2(D, V)); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1000000007; n int n; n long long x[1 << 20]; n long long y[1 << 20]; n long long d[1 << 20]; n long long sx[1 << 20]; n long long sy[1 << 20]; n long long sd[1 << 20]; n long long ssd[1 << 20]; n long long cirsm(int s, int v, long long sa[] = sd) { n   long long ar = 0; n   if (s > 0 && v > s) { n     ar = sa[v - 1] - sa[s - 1]; n   } else if (s == 0) { n     ar = sa[v - 1]; n   } else { n     ar = sa[n + v - 1] - sa[s - 1]; n   } n   return ar; n } n long long area(int s, int v) { n   long long ar = cirsm(s, v); n   ar += (x[s] - x[v]) * (y[s] + y[v]); n   return ar; n } n long long sumsd2(int s, int v) { n   long long ar = 0; n   if (s == 0) { n     ar = ssd[v - 1]; n   } else if (s < v) { n     long long rm = (long long)(v - s) * (sd[s - 1] % mod); n     rm = rm % mod; n     if (rm < 0) { n       rm += mod; n     } n     ar = (mod + ssd[v - 1] - ssd[s - 1] + mod - rm) % mod; n   } else { n     v += n; n     long long rm = (long long)(v - s) * (sd[s - 1] % mod); n     rm = rm % mod; n     if (rm < 0) { n       rm += mod; n     } n     ar = (mod + ssd[v - 1] - ssd[s - 1] + mod - rm) % mod; n   } n   return ar; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     long long xx, yy; n     scanf( %lld %lld , &xx, &yy); n     x[i + n] = x[i] = xx; n     y[i + n] = y[i] = yy; n   } n   for (int i = 0; i < 2 * n; ++i) { n     if (i + 1 < 2 * n) { n       d[i] = (x[i + 1] - x[i]) * (y[i + 1] + y[i]); n     } else { n       d[i] = d[i - n]; n     } n     if (i == 0) { n       sx[i] = x[i]; n       sy[i] = y[i]; n       sd[i] = d[i]; n       ssd[i] = sd[i] % mod; n       if (ssd[i] < 0) { n         ssd[i] += mod; n       } n     } else { n       sx[i] = sx[i - 1] + x[i]; n       sy[i] = sy[i - 1] + y[i]; n       sd[i] = sd[i - 1] + d[i]; n       ssd[i] = ssd[i - 1] + sd[i]; n       ssd[i] %= mod; n       if (ssd[i] < 0) { n         ssd[i] += mod; n       } n     } n   } n   long long S = sd[n - 1]; n   long long ans = 0; n   int k = 1; n   int v = 0; n   long long dsm = 0; n   while (k < n && 2 * area(0, k + 1) < S) { n     ++k; n     dsm += (x[k] - x[0]) * (y[k] + y[0]); n     dsm %= mod; n     if (dsm < 0) { n       dsm += mod; n     } n   } n   for (int i = 0; i < n; ++i) { n     long long lt = cirsm(i, k) % mod; n     if (lt < 0) { n       lt += mod; n     } n     long long gt = cirsm(k, i) % mod; n     if (gt < 0) { n       gt += mod; n     } n     long long cnt = (k - 1 - i); n     if (cnt < 0) { n       cnt += n; n     } n     long long aa = 0; n     aa = cnt * (cirsm(i + 1, k) % mod) - sumsd2(i + 1, k); n     aa %= mod; n     if (aa < 0) { n       aa += mod; n     } n     if (cnt) { n       long long ch = cnt * (gt - lt + mod) + aa + aa + 2LL * dsm; n       ch %= mod; n       ans += ch; n       ans %= mod; n       if (ans < 0) { n         ans += mod; n       } n     } n     if (i + 1 >= n) { n       break; n     } n     if (k != (i + 1) % n) { n       int j = (i + 2) % n; n       dsm -= (x[j] - x[i]) * (y[j] + y[i]); n       dsm %= mod; n       if (dsm < 0) { n         dsm += mod; n       } n     } n     long long ndi = k - i - 2; n     if (ndi < 0) { n       ndi += n; n     } n     if (k != (i + 1) % n && k != (i + 2) % n) { n       long long c = (x[i + 1] * y[i + 1] - x[i] * y[i]) % mod; n       dsm = dsm - c * ndi; n       dsm %= mod; n       long long xsm = cirsm(i + 3, k + 1, sx) % mod; n       dsm += xsm * (y[i + 1] - y[i]); n       long long ysm = cirsm(i + 3, k + 1, sy) % mod; n       dsm -= ysm * (x[i + 1] - x[i]); n       dsm %= mod; n       if (dsm < 0) { n         dsm += mod; n       } n     } n     if ((i + 1) == k) { n       k = (i + 2) % n; n     } n     while ((unsigned long long)2LL * n                (unsigned long long)area(i + 1, (k + 1) % n) < n            S) { n       k = (k + 1) % n; n       dsm += (x[k] - x[i + 1]) * (y[k] + y[i + 1]); n       dsm %= mod; n       if (dsm < 0) { n         dsm += mod; n       } n     } n   } n   ans += mod; n   ans %= mod; n   printf( %lld n , (long long)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n long long a, b, c, d, e, f, k; n bool check(long long x, long long y) { n   if (k == 0) { n     return x == c && y == d; n   } n   return ((c - x) * e + (d - y) * f) % k == 0 && n          (e * (d - y) + f * (x - c)) % k == 0; n } n int main() { n   while (cin >> a >> b >> c >> d >> e >> f) { n     k = e * e + f * f; n     if (check(a, b) || check(-a, -b) || check(b, -a) || check(-b, a)) { n       cout <<  YES  << endl; n     } else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n const long double E = 1e-7; n unsigned char ccc; n bool _minus = false; n template <typename T> n inline T sqr(T t) { n   return (t * t); n } n inline void read(long long &n) { n   n = 0; n   _minus = false; n   while (true) { n     ccc = getchar(); n     if (ccc ==     || ccc ==   n ) break; n     if (ccc ==  - ) { n       _minus = true; n       continue; n     } n     n = n * 10 + ccc -  0 ; n   } n   if (_minus) n *= -1; n } n inline void read(int &n) { n   n = 0; n   _minus = false; n   while (true) { n     ccc = getchar(); n     if (ccc ==     || ccc ==   n ) break; n     if (ccc ==  - ) { n       _minus = true; n       continue; n     } n     n = n * 10 + ccc -  0 ; n   } n   if (_minus) n *= -1; n } n char wwww[19]; n int kkkk; n inline void write(long long y) { n   long long x = y; n   kkkk = 0; n   if (!x) n     ++kkkk, wwww[kkkk] =  0 ; n   else n     while (x) { n       ++kkkk; n       wwww[kkkk] = char(x % 10 +  0 ); n       x /= 10; n     } n   for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); n   putchar(   ); n } n long long d, k, a, b, t; n long long get(long long x) { n   long long ans = max(0LL, x - 1) * t + x * k * a + (d - x * k) * b; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   srand(time(NULL)); n   cout.precision(10); n   cout << fixed; n   cin >> d >> k >> a >> b >> t; n   long long ans = ((d - 1) / k) * t + d * a; n   long long l = 0, r = d / k; n   while (l + 10 < r) { n     long long l1 = (l + l + r) / 3, r1 = (l + r + r) / 3; n     if (get(l1) < get(r1)) { n       r = r1; n     } else { n       l = l1; n     } n   } n   for (long long i = l; i <= r; i++) ans = min(ans, get(i)); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c; n   cin >> r >> c; n   char mat[r + 5][c + 5]; n   for (int i = 1; i <= r; i++) { n     for (int j = 1; j <= c; j++) { n       cin >> mat[i][j]; n     } n   } n   for (int i = 1; i <= r; i++) { n     for (int j = 1; j <= c; j++) { n       if (mat[i][j] ==  B ) { n         int rw = 0, cl = 0; n         for (int k = j; k <= c; k++) { n           if (mat[i][k] ==  B ) n             cl++; n           else n             break; n         } n         for (int k = i; k <= r; k++) { n           if (mat[k][j] ==  B ) n             rw++; n           else n             break; n         } n         cout << i + rw / 2 <<     << j + cl / 2 << endl; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int t[100003] = {0}; n int main() { n   int L, b, f, n; n   scanf( %d%d%d%d , &L, &b, &f, &n); n   t[L] = 1000; n   for (int i = 1; i <= n; i++) { n     int req, len; n     scanf( %d%d , &req, &len); n     if (req == 1) { n       bool e = 0; n       for (int a = 0; a < L; a++) { n         if (t[a]) continue; n         int prev = a; n         for (int c = a + 1; c <= L; c++) { n           a = c; n           if (t[c]) { n             if (prev == 0 && c == L && L - prev >= len) { n               for (int j = 0; j < len; j++) t[j] = i; n               printf( 0 n ); n               e = 1; n             } else if (prev == 0 && c >= len + f) { n               for (int j = 0; j < len; j++) t[j] = i; n               printf( 0 n ); n               e = 1; n             } else if (c == L && L - prev >= len + b || n                        c - prev >= len + b + f) { n               for (int j = prev + b; j < prev + b + len; j++) t[j] = i; n               printf( %d n , prev + b); n               e = 1; n             } n             break; n           } n         } n         if (e) break; n       } n       if (!e) printf( -1 n ); n     } else { n       for (int j = 0; j < L; j++) n         if (t[j] == len) t[j] = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200009; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long a[n + 5]; n     long long na[n + 5]; n     for (long long i = 1; i <= n; i++) { n       cin >> a[i]; n       na[i] = a[i]; n     } n     sort(na + 1, na + n + 1); n     long long p[m + 5]; n     unordered_map<long long, long long> mp; n     for (long long i = 1; i <= m; i++) { n       cin >> p[i]; n       mp[p[i]] = 1; n     } n     for (long long i = 1; i <= n; i++) { n       for (long long i = 1; i < n; i++) { n         if (mp[i] == 1 && a[i] > a[i + 1]) { n           swap(a[i], a[i + 1]); n         } n       } n     } n     long long f = 1; n     for (long long i = 1; i <= n; i++) { n       if (a[i] != na[i]) { n         f = 0; n         cout <<  NO  n              <<   n ; n         break; n       } n     } n     if (f == 1) { n       cout <<  YES  n            <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int mod = 1e9 + 7; n int arr[500]; n int main() { n   int k, n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     if (a % 2) { n       cout <<  First  << endl; n       return 0; n     } n   } n   cout <<  Second  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[30010][210][2][2][2]; n int pd[30010][210][2][2][2]; n int v[30010]; n int n, K; n int solve(int ind, int cut, int s1, int s2, int tem) { n   if (cut == K) return 0LL; n   if (ind == n) return -0x3f3f3f3f; n   int &ans = pd[ind][cut][s1][s2][tem]; n   if (vis[ind][cut][s1][s2][tem]) return ans; n   int p1, p2; n   if (s1) n     p1 = 1; n   else n     p1 = -1; n   if (s2) n     p2 = 1; n   else n     p2 = -1; n   if (cut == 0) p1 = 0LL; n   if (cut == K - 1) p2 = 0LL; n   vis[ind][cut][s1][s2][tem] = true; n   ans = -0x3f3f3f3f; n   int x = v[ind] * (p1 + p2); n   if (tem) { n     ans = max(x + solve(ind + 1, cut + 1, s2 ^ 1, 0, 0), n               x + solve(ind + 1, cut + 1, s2 ^ 1, 1, 0)); n     ans = max(ans, x + solve(ind + 1, cut, s1, s2, tem)); n   } else { n     ans = max(ans, solve(ind + 1, cut, s1, s2, 0)); n     ans = max(ans, x + solve(ind + 1, cut, s1, s2, 1)); n     ans = max(ans, x + solve(ind + 1, cut + 1, s2 ^ 1, 0, 0)); n     ans = max(ans, x + solve(ind + 1, cut + 1, s2 ^ 1, 1, 0)); n   } n   return ans; n } n int main() { n   scanf(  %d %d , &n, &K); n   for (int i = 0; i < (n); ++i) scanf(  %d , &v[i]); n   int ans = max(solve(0, 0, 0, 0, 0), solve(0, 0, 0, 1, 0)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = (1 << 19); n string str; n vector<pair<int, char> > sorted; n int cnt[MX], done[MX], K; n int main() { n   cin >> str >> K; n   for (int j = 0; j < str.size(); j++) cnt[str[j] -  a ]++; n   for (int j = 0; j < 26; j++) { n     sorted.push_back(make_pair(cnt[j], j +  a )); n   } n   sort(sorted.begin(), sorted.end()); n   int ans = 26; n   for (auto pp : sorted) { n     if (pp.first > K) break; n     K -= pp.first; n     done[pp.second] = 1; n     --ans; n   } n   cout << ans << endl; n   for (auto ch : str) n     if (!done[ch]) cout << ch; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool isSubSequence(string str1, string str2, int m, int n) { n   if (m == 0) return true; n   if (n == 0) return false; n   if (str1[m - 1] == str2[n - 1]) n     return isSubSequence(str1, str2, m - 1, n - 1); n   return isSubSequence(str1, str2, m, n - 1); n } n int main() { n   string s, t; n   cin >> s >> t; n   int i, j; n   int arr1[26] = {0}, arr2[26] = {}; n   int m = s.length(); n   int n = t.length(); n   if (isSubSequence(t, s, n, m) == true) { n     cout <<  automaton  << endl; n     return 0; n   } n   for (i = 0; i < s.length(); i++) { n     arr1[int(s[i]) - 97]++; n   } n   for (i = 0; i < t.length(); i++) { n     arr2[int(t[i]) - 97]++; n   } n   int flag = 0; n   if (s.length() == t.length()) { n     for (i = 0; i < 26; i++) { n       if (arr1[i] != arr2[i]) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       cout <<  array  << endl; n       return 0; n     } else { n       cout <<  need tree  << endl; n       return 0; n     } n   } n   for (i = 0; i < 26; i++) { n     if (arr1[i] < arr2[i]) { n       cout <<  need tree  << endl; n       return 0; n     } n   } n   cout <<  both  << endl; 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   string s[n]; n   for (int i = 0; i < n; i++) cin >> s[i]; n   long long a[m]; n   for (int i = 0; i < m; i++) cin >> a[i]; n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     long long maxi = 0; n     for (char c =  A ; c <=  E ; c++) { n       long long temp = 0; n       for (int j = 0; j < n; j++) { n         if (s[j][i] == c) temp += a[i]; n       } n       maxi = max(temp, maxi); n     } n     ans += maxi; n   } n   cout << ans << endl; 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 int d4x[4] = {1, 0, -1, 0}; n int d4y[4] = {0, 1, 0, -1}; n int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1}; n const long long oo = 1e18; n const long long maxN = 1e6; n void maximize(int &a, int b) { a = max(a, b); } n void minimize(int &a, int b) { a = min(a, b); } n int cnt[15]; n void solve() { n   int n, ans = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     cnt[x]++; n     int g = 0, b, d = 0; n     b = n + 1; n     for (int j = 1; j <= 10; j++) { n       if (cnt[j] > 0) { n         cnt[j]--; n         int p = 0; n         bool ok = true; n         for (int k = 1; k <= 10; k++) { n           if (cnt[k]) { n             if (!p) n               p = cnt[k]; n             else if (p != cnt[k]) n               ok = false; n           } n         } n         cnt[j]++; n         if (ok) ans = i; n       } n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1, ddd = 0; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[20]; n int m; n long long dp[1 << 18][110]; n int cnt[20]; n int main() { n   ios::sync_with_stdio(false); n   cin >> s >> m; n   memset(dp, 0, sizeof(dp)); n   memset(cnt, 0, sizeof(cnt)); n   dp[0][0] = 1; n   int l = strlen(s); n   long long x = 1 << l, d = 1; n   for (int i = 0; i < l; i++) d *= ++cnt[s[i] -  0 ]; n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < l; j++) { n       if (!(i & (1 << j)) && (i || s[j] -  0 )) { n         for (int k = 0; k < m; k++) { n           if (dp[i][k]) n             dp[i | (1 << j)][(k * 10 + (s[j] -  0 )) % m] += dp[i][k]; n         } n       } n     } n   } n   cout << dp[x - 1][0] / d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, j, s, x; n   map<long long, long long> mp; n   map<long long, long long>::iterator it; n   cin >> n; n   s = 0; n   for (i = 0; i < n; i++) { n     cin >> x; n     s += x; n     mp[s]++; n   } n   i = -1; n   for (it = mp.begin(); it != mp.end(); it++) { n     i = max(i, it->second); n   } n   cout << n - i <<   n ; n } n


#include<bits/stdc++.h> n using namespace std; n  n #define FASTio ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n #define ll long long int  n #define all(v) (v).begin(), (v).end() n #define mp make_pair n #define pb push_back n #define pf push_front n #define ff first n #define ss second n #define lcm(a, b) ((a) * (b)) / __gcd(a, b) n #define umpit unordered_map<ll,ll>::iterator n #define mpit map<ll,ll>::iterator n #define setit set<ll>::iterator n #define mx(a) *max_element(all(a)) n #define mn(a) *min_element(all(a)) n #define yes cout<< YES n  n #define no cout<< NO n  n const ll INF = LLONG_MAX / 2; n #define PI 3.1415926535897932384626433832795 n #define mod 1000000007 n  n ll bin_exp_mod(ll a,ll n) n { n     ll res=1; n     a%=mod; n     if(a==0) n         return 0; n     while(n) n     { n         if(n&1) n             res=((res%mod)*(a%mod))%mod; n         n=n>>1; n         a=((a%mod)*(a%mod))%mod; n     } n     res%=mod; n     return res; n } n  n  n int main() n { n    FASTio; n     #ifndef ONLINE_JUDGE                    //file start n     freopen( input.txt , r ,stdin); n     freopen( output.txt , w ,stdout);    n     #endif                                  //file end n     ll t,i,j; n     cin>>t; n     while(t--) n     { n         ll n,k; n         cin>>n>>k; n         while(k>3) n         { n             cout<< 1  ; n             k--; n             n--; n         } n         vector<ll>arr(k); n         if(n%3 == 0) n         { n              n             arr[0] = n/3; n             arr[1] = n/3; n             arr[2] = n/3; n             for(auto x : arr) n                 cout<<x<<   ; n             cout<<  n ; n             continue; n         } n         if(n%4 == 0) n         { n            arr[0] = n/2; n             arr[1] = n/4; n             arr[2] = n/4; n             for(auto x : arr) n                 cout<<x<<   ; n             cout<<  n ; n             continue; n         } n         if(n%2 == 0) n         { n              arr[0] = n/2 -1; n             arr[1] = n/2 - 1; n             arr[2] = 2; n             for(auto x : arr) n                 cout<<x<<   ; n             cout<<  n ; n             continue; n         } n         if(n%2 == 1) n         { n              arr[0] = n/2; n             arr[1] = n/2; n             arr[2] = 1; n             for(auto x : arr) n                 cout<<x<<   ; n             cout<<  n ; n             continue; n         } n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n void start(vector<int> &arr, pair<char, int> &prefAns, pair<char, int> &sufAns, n            string &s) { n   int i; n   for (i = 0; i < s.size(); ++i) { n     int cnt = 0; n     while (i + 1 != s.size() && s[i] == s[i + 1]) { n       ++cnt; n       ++i; n     } n     arr[s[i]] = max(arr[s[i]], cnt + 1); n   } n   i = 0; n   while (i + 1 != s.size() && s[i] == s[i + 1]) { n     ++i; n   } n   prefAns = {s[0], i + 1}; n   i = s.size() - 1; n   while (i - 1 != -1 && s[i] == s[i - 1]) { n     --i; n   } n   sufAns = {s[s.size() - 1], s.size() - i}; n } n char same(string &s) { n   int res = s[0]; n   for (auto c : s) { n     if (res != c) { n       return -1; n     } n   } n   return res; n } n void relax(vector<int> &ans, pair<char, int> &prefAns, pair<char, int> &sufAns, n            string &s) { n   for (auto c : s) { n     ans[c] = max(ans[c], (sufAns.first == c) * sufAns.second + 1 + n                              (prefAns.first == c) * prefAns.second); n   } n } n int max(vector<int> &arr) { n   int res = 0; n   for (auto elem : arr) { n     res = max(res, elem); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   vector<string> arr(n); n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   vector<int> ans( z  + 1); n   pair<char, int> prefAns; n   pair<char, int> sufAns; n   start(ans, prefAns, sufAns, arr[n - 1]); n   bool flag = (same(arr[n - 1]) != -1); n   for (int i = n - 2; i >= 0; --i) { n     char c = same(arr[i]); n     if (flag && (c != arr[n - 1][0])) { n       c = arr[n - 1][0]; n       vector<int> tmpAns( z  + 1); n       pair<char, int> tmpPrefAns; n       pair<char, int> tmpSufAns; n       start(tmpAns, tmpPrefAns, tmpSufAns, arr[i]); n       ans[c] = ans[c] * (tmpAns[c] + 1) + tmpAns[c]; n       if (tmpPrefAns.first == c) { n         prefAns = { n             c, prefAns.second * (tmpPrefAns.second + 1) + tmpPrefAns.second}; n       } n       if (tmpSufAns.first == c) { n         sufAns = {c, sufAns.second * (tmpSufAns.second + 1) + tmpSufAns.second}; n       } n       flag = false; n       continue; n     } n     if (flag) { n       ans[c] = ans[c] * (arr[i].size() + 1) + arr[i].size(); n       prefAns = {c, ans[c]}; n       sufAns = {c, ans[c]}; n     } else { n       relax(ans, prefAns, sufAns, arr[i]); n     } n   } n   cout << max(ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 0x3f3f3f3f; n const long long llinf = 1e18; n const long long nax = 0; n long long n; n vector<long long> arr(5005); n vector<bool> ada(5005), masuk(5005); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   sort(arr.begin(), arr.begin() + n); n   for (int i = 0; i < n; ++i) { n     if (masuk[i]) { n       continue; n     } n     for (int j = i + 1; j < n; ++j) { n       if (arr[j] <= arr[i]) continue; n       if (!ada[j]) { n         masuk[i] = 1, ada[j] = 1; n         break; n       } n     } n   } n   int ans = n; n   for (int i = 0; i < n; ++i) { n     if (masuk[i]) ans--; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int l, r; n long long pw[500007], inv[500007], ans = 0; n pair<int, int> p[500007]; n const long long mod = 1e9 + 7; n long long pwr(long long x, long long y) { n   if (y == 0) return 1LL; n   if (y == 1) return x; n   long long ret = pwr(x, y / 2); n   if (y % 2 == 0) n     return (ret * ret) % mod; n   else n     return (((ret * ret) % mod) * x) % mod; n } n long long C(long long k, long long n) { n   if (k > n) n     return 0; n   else n     return (((pw[n] * inv[k]) % mod) * inv[n - k]) % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   inv[0] = 1; n   pw[0] = 1; n   for (long long i = 1; i <= 500000; i++) pw[i] = (pw[i - 1] * i) % mod; n   inv[500000] = pwr(pw[500000], mod - 2); n   for (long long i = 499999; i >= 1; i--) inv[i] = (inv[i + 1] * (i + 1)) % mod; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> l >> r; n     p[i * 2 - 1] = make_pair(l, 1); n     p[i * 2] = make_pair(r + 1, -1); n   } n   sort(p + 1, p + 1 + 2 * n); n   long long cur = 1; n   for (int i = 2; i <= 2 * n; i++) { n     long long val = p[i].first - p[i - 1].first; n     ans += (val * C(k, cur)) % mod; n     if (ans >= mod) ans -= mod; n     cur += p[i].second; n   } n   cout << ans; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5 * 100 * 1000 + 13; n queue<int> bfsq; n set<int> bfsS; n set<pair<int, int> > forbid; n vector<int> compV[MAXN], toDel; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     forbid.insert(make_pair(min(a, b), max(a, b))); n   } n   for (int i = 1; i <= n; i++) bfsS.insert(i); n   int comp = 0; n   while (!bfsS.empty()) { n     comp++; n     bfsq.push(*(bfsS.begin())); n     bfsS.erase(bfsS.begin()); n     while (!bfsq.empty()) { n       int u = bfsq.front(); n       compV[comp].push_back(u); n       bfsq.pop(); n       for (auto v : bfsS) n         if (forbid.find(make_pair(min(u, v), max(u, v))) == forbid.end()) { n           toDel.push_back(v); n           bfsq.push(v); n         } n       for (auto u : toDel) bfsS.erase(u); n       toDel.clear(); n     } n   } n   cout << comp << endl; n   for (int i = 1; i <= comp; i++) { n     cout << compV[i].size() <<    ; n     for (auto u : compV[i]) cout << u <<    ; n     cout << 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   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int N; n set<int> P; n void setP(long long int n, int x, int y) { n   if (n > N) return; n   if (n != 0) P.insert(n); n   if (n != 0 || x != 0) setP(n * 10 + x, x, y); n   if (n != 0 || y != 0) setP(n * 10 + y, x, y); n } n int main() { n   cin >> N; n   for (int i = 0; i < 10; i++) { n     for (int j = i + 1; j < 10; j++) { n       setP(0, i, j); n     } n   } n   cout << P.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int hit[12]; n   string notes[12] = { C ,   C# ,  D ,   D# ,  E ,  F , n                        F# ,  G ,   G# ,  A ,   B ,  H }; n   string x, y, z; n   cin >> x >> y >> z; n   for (int i = 0; i < 12; i++) { n     if (x == notes[i] || y == notes[i] || z == notes[i]) { n       hit[i] = 1; n     } n   } n   for (int i = 0; i < 12; i++) { n     if (hit[i] == 1) { n       if (hit[(i + 4) % 12] == 1 && hit[(i + 7) % 12] == 1) { n         cout <<  major  << endl; n         return 0; n       } else if (hit[(i + 3) % 12] == 1 && hit[(i + 7) % 12] == 1) { n         cout <<  minor  << endl; n         return 0; n       } n     } n   } n   cout <<  strange  << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, l, j; n   char t; n   char kb[100] =  qwertyuiopasdfghjkl;zxcvbnm,./ ; n   char s[1000]; n   scanf( %c , &t); n   scanf( %s , s); n   l = strlen(s); n   for (i = 0; i < l; i++) { n     for (j = 0; j < 30; j++) { n       if (kb[j] == s[i]) { n         if (t ==  R ) { n           printf( %c , kb[j - 1]); n         } else if (t ==  L ) { n           printf( %c , kb[j + 1]); n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   long long n, k, ans = 0; n   cin >> n >> k; n   if (k >= n / 2) n     ans = (n * (n - 1)) / 2; n   else { n     ans = (n * (n - 1)) / 2 - (((n - k) * (n - k - 1)) / 2) + k * (n - 2 * k) + n           (k * (k - 1)) / 2; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void runTest() { n   long long int n, a, b, k; n   cin >> n >> a >> b >> k; n   long long int ans = 0; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int val; n     cin >> val; n     long long int temp = n         ((val % (a + b) == 0) ? (val / (a + b)) - 1 : val / (a + b)); n     temp *= (a + b); n     temp = val - temp; n     temp = ((temp - 1) / a) + 1; n     temp--; n     if (temp <= 0) n       ans++; n     else n       v.push_back(temp); n   } n   sort(v.begin(), v.end()); n   for (auto i : v) { n     if (k >= i) { n       ans++; n       k -= i; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) runTest(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sum; n int main() { n   cin >> n; n   int ans = 0; n   for (int i = 1; i <= 1000000; i++) { n     sum += i; n     ans += (n + i) % 3 == 0 && (n + i) / 3 >= sum; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int ROOT = 0, MAXN = 100000; n const char WHITE = 0, GRAY = 1, BLACK = 2; n vector<int> graph[MAXN]; n int n, m, a, b, i; n char k[MAXN] = {WHITE}; n inline int dfs(const int& v = ROOT) { n   k[v] = GRAY; n   int i, t, p = -1; n   for (i = 0; i < (int)(graph[v].size()); i++) n     if (k[graph[v][i]] == WHITE) { n       t = dfs(graph[v][i]); n       if (t == -1) { n         if (p == -1) n           p = graph[v][i]; n         else { n           printf( %d %d %d n , p + 1, v + 1, graph[v][i] + 1); n           p = -1; n         } n       } else n         printf( %d %d %d n , v + 1, graph[v][i] + 1, t + 1); n     } else if (k[graph[v][i]] == BLACK) { n       if (p == -1) n         p = graph[v][i]; n       else { n         printf( %d %d %d n , p + 1, v + 1, graph[v][i] + 1); n         p = -1; n       } n     } n   k[v] = BLACK; n   return p; n } n int main() { n   scanf( %d%d , &n, &m); n   if (m & 1) { n     printf( No solution n ); n     return 0; n   } n   for (i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     a--; n     b--; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   assert(dfs() == -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r; n char s[110]; n int main() { n   scanf( %*d %s , s); n   for (int i = 1; s[i]; i++) r += s[i] == s[i - 1]; n   printf( %d , r); n } n


#include <bits/stdc++.h> n #pragma optimize( SEX_ON_THE_BEACH ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( fast-math ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n using ll = long long int; n using ull = unsigned long long int; n using dd = double; n using ldd = long double; n namespace someUsefull { n template <typename T1, typename T2> n inline void checkMin(T1& a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n inline void checkMax(T1& a, T2 b) { n   if (a < b) a = b; n } n const int _bitfunctions_size_ = 16; n const int _bitfunctions_mask_ = (1 << _bitfunctions_size_) - 1; n char* _bits_count_; n inline void _build_bits_count_() { n   _bits_count_ = new char[1 << _bitfunctions_size_]; n   for (int i = 1; i < (1 << _bitfunctions_size_); ++i) { n     _bits_count_[i] = _bits_count_[i >> 1]; n     if (i & 1) ++_bits_count_[i]; n   } n } n template <typename T1 = char, typename T2 = int> n inline T1 popcnt(T2 x) { n   T1 ans = 0; n   while (x) { n     ans += _bits_count_[x & _bitfunctions_mask_]; n     x >>= _bitfunctions_size_; n   } n   return ans; n } n }  // namespace someUsefull n namespace operators { n template <typename T1, typename T2> n std::istream& operator>>(std::istream& in, std::pair<T1, T2>& x) { n   in >> x.first >> x.second; n   return in; n } n template <typename T1, typename T2> n std::ostream& operator<<(std::ostream& out, std::pair<T1, T2> x) { n   out << x.first <<     << x.second; n   return out; n } n template <typename T1> n std::istream& operator>>(std::istream& in, std::vector<T1>& x) { n   for (auto& i : x) in >> i; n   return in; n } n template <typename T1> n std::ostream& operator<<(std::ostream& out, std::vector<T1>& x) { n   for (auto& i : x) out << i <<    ; n   return out; n } n }  // namespace operators n using namespace std; n using namespace operators; n using namespace someUsefull; n vector<vector<int>> gr; n vector<pair<int, int>> ed; n vector<int> d; n void dfs(int v, int _d = 0, int p = -1) { n   d[v] = _d; n   for (int to : gr[v]) { n     if (to != p) dfs(to, _d + 1, v); n   } n } n struct Tree { n   const int _size = 1 << 19; n   int n, t; n   vector<pair<int, int>> tree; n   vector<int> upd; n   vector<int> d; n   vector<int> pre; n   vector<int> tin, tout, tos; n   void dfs(int v, int _d = 0, int p = -1) { n     d[v] = _d; n     pre[v] = p; n     tin[v] = tos.size(); n     tos.push_back(v); n     for (int to : gr[v]) { n       if (to != p) dfs(to, _d + 1, v); n     } n     tout[v] = tos.size(); n   } n   pair<int, int> merge(pair<int, int> a, pair<int, int> b) { n     return {max(a.first, b.first), max(a.second, b.second)}; n   } n   Tree(int v) { n     tree.resize(_size << 1, {0, 0}); n     upd.resize(_size << 1, 0); n     n = gr.size(); n     t = 0; n     d.resize(n); n     tin.resize(n); n     tout.resize(n); n     pre.resize(n); n     0; n     ; n     dfs(v); n     0; n     ; n     for (int i = 0; i < n; ++i) tree[_size + i].first = d[tos[i]]; n     for (int i = _size - 1; i > 0; --i) n       tree[i] = merge(tree[i << 1], tree[i << 1 | 1]); n   } n   void change(int v) { n     upd[v] ^= 1; n     swap(tree[v].first, tree[v].second); n   } n   void push(int v) { n     if (!upd[v]) return; n     change(v << 1); n     change(v << 1 | 1); n     upd[v] = 0; n   } n   void update(int v, int l, int r, int fl, int fr) { n     if (r <= l || r <= fl || fr <= l) return; n     if (fl <= l && r <= fr) { n       change(v); n       return; n     } n     push(v); n     update(v << 1, l, (r + l) >> 1, fl, fr); n     update(v << 1 | 1, (r + l) >> 1, r, fl, fr); n     tree[v] = merge(tree[v << 1], tree[v << 1 | 1]); n   } n   int add(int a, int b) { n     if (pre[a] != b) swap(a, b); n     update(1, 0, _size, tin[a], tout[a]); n     return tree[1].first; n   } n }; n void solve(int test) { n   int n; n   cin >> n; n   gr.resize(n); n   vector<int> type(n - 1); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     cin >> a >> b >> type[i]; n     --a; n     --b; n     ed.push_back({a, b}); n     gr[a].push_back(b); n     gr[b].push_back(a); n   } n   d.resize(n); n   d.assign(n, -1); n   dfs(0); n   int v = 0; n   for (int i = 0; i < n; ++i) n     if (d[v] < d[i]) v = i; n   dfs(v); n   int L = v; n   int R = 0; n   for (int i = 0; i < n; ++i) n     if (d[R] < d[i]) R = i; n   0; n   ; n   0; n   ; n   Tree LT(L), RT(R); n   0; n   ; n   for (int i = 0; i < n - 1; ++i) { n     if (type[i]) n       LT.add(ed[i].first, ed[i].second), RT.add(ed[i].first, ed[i].second); n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     int x; n     cin >> x; n     --x; n     int ans = 0; n     checkMax(ans, LT.add(ed[x].first, ed[x].second)); n     checkMax(ans, RT.add(ed[x].first, ed[x].second)); n     cout << ans <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   _build_bits_count_(); n   cout << setprecision(12) << fixed; n   int t = 1; n   for (int i = 0; i < t; ++i) { n     0; n     ; n     solve(i + 1); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long cnt = 0; n   int H; n   scanf( %d  , &H); n   ; n   long long N; n   cin >> N; n   N--; n   long long n = 0; n   int d = 0; n   for (int h = (0); h < (H); h++) { n     long long msk = ((1LL) << (long long)(H - h - 1)); n     if (N & msk) { n       if (d & 1) n         cnt++; n       else { n         cnt += msk * 2LL; n         d++; n       } n     } else { n       if (d & 1) { n         cnt += msk * 2LL; n         d++; n       } else n         cnt++; n     } n     d++; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n template <class T> n void dbs(string str, T t) { n   cout << str <<   :   << t << endl; n } n template <class T, class... second> n void dbs(string str, T t, second... s) { n   int idx = str.find( , ); n   cout << str.substr(0, idx) <<   :   << t <<  , ; n   dbs(str.substr(idx + 1), s...); n } n template <class second, class T> n ostream& operator<<(ostream& os, const pair<second, T>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class second, class T> n ostream& operator<<(ostream& os, const map<second, T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n void prc(T a, T b) { n   cout <<  [ ; n   for (T i = a; i != b; ++i) { n     if (i != a) cout <<  ,  ; n     cout << *i; n   } n   cout <<  ] ; n   cout << endl; n } n struct pt { n   int x, y; n   pt(int x = 0, int y = 0) : x(x), y(y) {} n   pt operator-(const pt& b) { return pt(x - b.x, y - b.y); } n }; n int cross(pt x, pt y) { return y.y * x.x - y.x * x.y; } n bool inside(pt a, vector<pt> b) { n   int ar = abs(cross(b[1] - b[0], b[2] - b[0])) + n            abs(cross(b[2] - b[0], b[3] - b[0])); n   for (auto i = (0); i <= (3); ++i) n     ar -= abs(cross(b[i] - a, b[(i + 1) % 4] - a)); n   return ar == 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   vector<pt> a(4); n   for (auto i = (0); i <= (3); ++i) n     cin >> a[i].x >> a[i].y, a[i].x *= 4, a[i].y *= 4; n   vector<pt> b(4); n   for (auto i = (0); i <= (3); ++i) n     cin >> b[i].x >> b[i].y, b[i].x *= 4, b[i].y *= 4; n   pt A, B; n   for (auto i = (0); i <= (3); ++i) n     A.x += a[i].x, A.y += a[i].y, B.x += b[i].x, B.y += b[i].y; n   A.x /= 4, A.y /= 4, B.x /= 4, B.y /= 4; n   bool ans = inside(A, b) | inside(B, a); n   for (auto i = (0); i <= (3); ++i) ans |= inside(a[i], b) | inside(b[i], a); n   cout << (ans ?  YES n  :  NO n ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, t, b[100020], cnt, p, q; n char s[100020], s1[100020], s2[100020]; n char chose(char x, char y) { n   if (x !=  a  && y !=  a ) n     return  a ; n   else if (x !=  b  && y !=  b ) n     return  b ; n   else n     return  c ; n } n int main() { n   scanf( %d%d , &n, &t); n   scanf( %s , s1); n   scanf( %s , s2); n   cnt = 0; n   p = 0; n   q = 0; n   for (int i = 0; i < n; i++) n     if (s1[i] != s2[i]) cnt++; n   if (t < (cnt + 1) / 2) { n     printf( -1 n ); n     return 0; n   } n   if (t <= cnt) { n     for (int i = 0; i < n; i++) { n       if (s1[i] == s2[i]) n         s[i] = s1[i]; n       else { n         p++; n         if (p <= 2 * t - cnt) n           s[i] = chose(s1[i], s2[i]); n         else if (p % 2 == 1) n           s[i] = s1[i]; n         else n           s[i] = s2[i]; n       } n     } n   } else { n     for (int i = 0; i < n; i++) { n       if (s1[i] == s2[i]) { n         q++; n         if (q <= t - cnt) n           s[i] = chose(s1[i], s1[i]); n         else n           s[i] = s1[i]; n       } else { n         p++; n         s[i] = chose(s1[i], s2[i]); n       } n     } n   } n   printf( %s , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   cin >> test; n   while (test--) { n     int n, m; n     cin >> n >> m; n     vector<int> a(n), b(m); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < m; i++) { n       cin >> b[i]; n     } n     vector<int> idx(100001); n     for (int i = 0; i < n; i++) { n       idx[a[i]] = i; n     } n     long long ans = 0; n     int mxDepth = 0; n     for (int i = 0; i < m; i++) { n       if (idx[b[i]] < mxDepth) { n         ans++; n       } else { n         mxDepth = idx[b[i]]; n         ans += 2 * (idx[b[i]] - i) + 1; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 3; n const int mod = 998244353; n int n; n map<int, int> a; n char s[N]; n long long ans = 0; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     int x = 0, l = strlen(s + 1); n     for (int j = 1; j <= l; j++) x = x ^ (1 << (s[j] -  a )); n     for (int j = 0; j < 26; j++) ans += a[x ^ (1 << j)]; n     ans += a[x]; n     a[x]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> working; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   working[-1] = -1; n   working[2E9] = 2E9; n   working[1] = n; n   int answer = n; n   while (q--) { n     int l, r, k; n     cin >> l >> r >> k; n     int haveNow = 0; n     map<int, int>::iterator it = working.lower_bound(l); n     it--; n     if (it->second >= l) { n       working[l] = it->second; n       it->second = l - 1; n     } n     it++; n     while (it->first <= r) { n       if (it->second > r) { n         haveNow += r + 1 - it->first; n         working[r + 1] = it->second; n       } else n         haveNow += it->second - it->first + 1; n       map<int, int>::iterator it2 = it; n       it++; n       working.erase(it2); n     } n     if (k == 1) { n       answer -= haveNow; n     } else { n       answer -= haveNow; n       working[l] = r; n       answer += r - l + 1; n     } n     cout << answer <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n void itval(istream_iterator<string> it) {} n template <typename T, typename... Args> n void itval(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   itval(++it, args...); n } n const long long int MOD = 1e9 + 7; n template <typename T> n inline void print(T x) { n   cout << x <<   n ; n } n template <typename T> n inline void printvec(T x) { n   for (auto a : x) cout << a <<    ; n   cout <<   n ; n } n struct custom { n   bool operator()(const pair<int, pair<int, int> > &p1, n                   const pair<int, pair<int, int> > &p2) const { n     return p1.first < p2.first; n   } n }; n long long int get_pow(long long int a, long long int b, long long int M = MOD) { n   long long int res = 1; n   while (b) { n     if (b & 1) res = (res * a) % M; n     a = (a * a) % M; n     b >>= 1; n   } n   return res; n } n const long long int N = 2600 + 5, inf = 2e18; n int lookup[N][N]; n string longestPalindrome(string X, string Y, int m, int n) { n   if (m == 0 || n == 0) { n     return string(  ); n   } n   if (X[m - 1] == Y[n - 1]) { n     return longestPalindrome(X, Y, m - 1, n - 1) + X[m - 1]; n   } n   if (lookup[m - 1][n] > lookup[m][n - 1]) { n     return longestPalindrome(X, Y, m - 1, n); n   } n   return longestPalindrome(X, Y, m, n - 1); n } n int LCSLength(string X, string Y, int n) { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (X[i - 1] == Y[j - 1]) { n         lookup[i][j] = lookup[i - 1][j - 1] + 1; n       } else { n         lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]); n       } n     } n   } n   return lookup[n][n]; n } n void solve() { n   string s; n   cin >> s; n   int n = s.length(); n   if (n >= 2600) { n     vector<long long> v(26, 0); n     for (auto c : s) v[c -  a ]++; n     for (long long int i = (long long int)0; i < (long long int)(26); i++) { n       if (v[i] >= 100) { n         for (long long int z = (long long int)0; z < (long long int)(100); z++) n           cout << char( a  + i); n         return; n       } n     } n   } n   string r = s; n   reverse(r.begin(), r.end()); n   LCSLength(s, r, n); n   string res = longestPalindrome(s, r, n, n); n   if (res.length() <= 100) { n     cout << res; n     return; n   } n   string p =   ; n   for (long long int i = (long long int)0; i < (long long int)(50); i++) n     p.push_back(res[i]); n   cout << p; n   reverse(p.begin(), p.end()); n   cout << p; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int test = 1; n   clock_t z = clock(); n   for (long long int tes = (long long int)0; tes < (long long int)(test); n        tes++) { n     solve(); n   } n   fprintf(stderr,  Total Time:%.4f n , (double)(clock() - z) / CLOCKS_PER_SEC), n       fflush(stderr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 1e5 + 5; n const int mod = 1e9 + 7; n const double eps = 1e-8; n int t, n; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     if (n == 1) n       puts( 0 ); n     else if (n == 2) n       puts( 1 ); n     else { n       int cnr = ceil(sqrt(n)); n       int cnt = (cnr - 1) * 2; n       int a = cnr * cnr - n; n       cout << cnt - a / cnr << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n const int MAXM = 1e5 + 10; n const int INF = 1e9; n struct edge { n   int u, v; n   long long w; n   int next; n } e[MAXM * 2]; n int first[MAXN]; n int idx; n int n; n long long len, wei; n struct node { n   long long weight, dist; n   int id; n } p[MAXN]; n bool cmp(node x, node y) { n   if (x.weight == y.weight) return x.dist < y.dist; n   return x.weight < y.weight; n } n void add(int a, int b, long long c) { n   e[idx].u = a; n   e[idx].v = b; n   e[idx].w = c; n   e[idx].next = first[a]; n   first[a] = idx++; n } n void init() { n   fill(first, first + n + 1, -1); n   idx = 0; n } n namespace BIT { n long long c[MAXN]; n int n; n int lowbit(int x) { return x & (-x); } n void updata(int i, int k) { n   while (i <= n) { n     c[i] += k; n     i += lowbit(i); n   } n } n long long getsum(int i) { n   long long res = 0; n   while (i > 0) { n     res += c[i]; n     i -= lowbit(i); n   } n   return res; n } n void change(int x, int k) { updata(x, k); } n long long query(int l, int r) { n   if (l <= 0 || r <= 0) return 0; n   return getsum(r) - getsum(l - 1); n } n void init(int _n) { n   n = _n; n   fill(c, c + n + 1, 0); n } n };  // namespace BIT n int sz[MAXN], mson[MAXN], vis[MAXN]; n int tolsize, root, tol; n long long res; n void get_root(int x, int fa) { n   sz[x] = 1; n   mson[x] = 0; n   for (int i = first[x]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (vis[v] || v == fa) continue; n     get_root(v, x); n     sz[x] += sz[v]; n     if (sz[v] > mson[x]) mson[x] = sz[v]; n   } n   if (tolsize - sz[x] > mson[x]) mson[x] = tolsize - sz[x]; n   if (mson[x] < mson[root]) root = x; n } n void get_dis(int x, int fa, long long w, long long l) { n   p[++tol].weight = w; n   p[tol].dist = l; n   for (int i = first[x]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (v == fa || vis[v]) continue; n     get_dis(v, x, w + e[i].w, l + 1); n   } n } n int right_bound(int l, long long x) { n   int ans = 0, r = tol; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (p[mid].weight <= x) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   return ans; n } n long long cal(int x, long long w, long long le) { n   long long ans = 0; n   tol = 0; n   get_dis(x, 0, w, le); n   sort(p + 1, p + tol + 1, cmp); n   int l = 1; n   BIT::init(tol + 1); n   long long maxdist = 0; n   for (int i = 1; i <= tol; i++) { n     maxdist = max(maxdist, p[i].dist); n     BIT::change(p[i].dist + 1, 1); n   } n   int last = tol; n   while (l < tol && p[l].weight * 2 <= wei) { n     BIT::change(p[l].dist + 1, -1); n     while (p[l].weight + p[last].weight > wei && last > l) { n       BIT::change(p[last].dist + 1, -1); n       last--; n     } n     long long tmp = BIT::query(1, min(maxdist + 1, len - p[l].dist + 1)); n     ans += tmp; n     l++; n   } n   return ans; n } n void solve(int x) { n   vis[x] = 1; n   res += cal(x, 0, 0); n   for (int i = first[x]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (vis[v]) continue; n     res -= cal(v, e[i].w, 1); n     tolsize = sz[v]; n     root = 0; n     get_root(v, x); n     solve(root); n   } n } n int main() { n   scanf( %d%lld%lld , &n, &len, &wei); n   init(); n   for (int i = 2; i <= n; i++) { n     int v; n     long long w; n     scanf( %d%lld , &v, &w); n     add(i, v, w); n     add(v, i, w); n   } n   tolsize = n; n   root = 0; n   mson[0] = INF; n   get_root(1, 0); n   solve(root); n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nodes[2050], depth, score = 0; n int abs(int a) { n   if (a > 0) return a; n   return -a; n } n int max(int a, int b) { n   if (a > b) return a; n   return b; n } n int dfs(int d, int i, int total) { n   if (d == depth) return nodes[i]; n   int r = dfs(d + 1, i * 2, 0); n   int l = dfs(d + 1, i * 2 + 1, 0); n   score += abs(r - l); n   return max(r, l) + nodes[i]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> depth; n   for (size_t i = 2; i < pow(2, depth + 1); i++) cin >> nodes[i]; n   dfs(0, 1, 0); n   cout << score; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   bool split(vector<int>& A, vector<int>& inc, vector<int>& dec) { n     map<int, int> map; n     for (int e : A) map[e]++; n     for (const pair<int, int>& p : map) { n       if (p.second > 2) return false; n       inc.push_back(p.first); n       if (p.second == 2) dec.push_back(p.first); n     } n     sort(dec.begin(), dec.end(), greater<int>()); n     return true; n   } n }; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<int> inc, dec; n   if (Solution().split(a, inc, dec)) { n     cout <<  YES  << endl; n     cout << inc.size() << endl; n     for (int e : inc) cout << e <<    ; n     cout << endl; n     cout << dec.size() << endl; n     for (int e : dec) cout << e <<    ; n     cout << endl; n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned int n; n   cin >> n; n   const int N = n; n   unsigned int m[N], sum = 0; n   for (int i(0); i < n; i++) { n     cin >> m[i]; n     sum += m[i]; n   } n   sort(m, m + n); n   unsigned int myMonets = 0, count = 0; n   for (int i(n - 1); i >= 0; i--) { n     myMonets += m[i]; n     count++; n     if (myMonets > (sum - myMonets)) break; n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, mp[20][20], pre[20], cnt[100005][20]; n int ban[1 << 17], usd[1 << 17][20][20], can[1 << 17], sum[1 << 17]; n char s[100005]; n int main() { n   scanf( %d%d , &n, &p); n   scanf( %s , s); n   for (int i = 0; i < p; i++) n     for (int j = 0; j < p; j++) scanf( %d , &mp[i][j]); n   memset(pre, -1, sizeof pre); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < p; j++) n       cnt[i][j] = (s[i] -  a  == j) + (i ? cnt[i - 1][j] : 0); n     for (int j = 0; j < p; j++) n       if (!mp[s[i] -  a ][j] && pre[j] >= 0 && pre[s[i] -  a ] <= pre[j]) { n         int sta = 0; n         for (int k = 0; k < p; k++) n           if (cnt[i - 1][k] - cnt[pre[j]][k]) sta |= (1 << k); n         usd[sta][s[i] -  a ][j] |= 1; n       } n     pre[s[i] -  a ] = i; n   } n   for (int sta = 0; sta < (1 << p); sta++) n     for (int i = 0; i < p; i++) n       for (int j = 0; j < p; j++) n         if (usd[sta][i][j]) { n           int rsta = (1 << p) - 1 - sta - (1 << i) - (i != j ? (1 << j) : 0); n           for (int S = rsta;; S = (S - 1) & (rsta)) { n             ban[sta | S] = 1; n             if (!S) break; n           } n         } n   int ans = n; n   can[0] = 1; n   for (int sta = 0; sta < (1 << p); sta++) { n     if (can[sta] && !ban[sta]) { n       ans = min(ans, n - sum[sta]); n       for (int i = 0; i < p; i++) n         if (((1 << i) & sta) == 0) n           can[sta | (1 << i)] = 1, n                           sum[sta | (1 << i)] = sum[sta] + cnt[n - 1][i]; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long base; n vector<long> vec[2000]; n long can[200][200]; n long lcm[200]; n long long n, k; n vector<vector<long> > mult(vector<vector<long> > a, vector<vector<long> > b) { n   vector<vector<long> > v; n   v.resize(a.size()); n   for (int i = 0; i < v.size(); i++) v[i].resize(a[0].size()); n   for (int i = 0; i < v.size(); i++) n     for (int j = 0; j < v.size(); j++) v[i][j] = 0; n   for (int k = 0; k < v.size(); k++) n     for (int i = 0; i < v.size(); i++) n       for (int j = 0; j < v.size(); j++) n         v[i][j] += a[i][k] * b[k][j], v[i][j] %= base; n   return v; n } n vector<vector<long> > one(long sz) { n   vector<vector<long> > v; n   v.resize(sz); n   for (int i = 0; i < v.size(); i++) v[i].resize(sz); n   for (int i = 0; i < v.size(); i++) n     for (int j = 0; j < v.size(); j++) v[i][j] = (i == j); n   return v; n } n vector<vector<long> > pw(vector<vector<long> > a, long long b) { n   if (b == 0) return one(a.size()); n   if (b % 2) return mult(a, pw(a, b - 1)); n   return pw(mult(a, a), b / 2); n } n vector<long> decomp(long state) { n   vector<long> res; n   for (int i = 25; i >= 0; --i) n     if (vec[i].size()) n       res.push_back(state % lcm[i]), state /= lcm[i]; n     else n       res.push_back(1); n   reverse(res.begin(), res.end()); n   return res; n } n long mov(long a, long b) { n   vector<long> v1, v2; n   v1 = decomp(a); n   v2 = decomp(b); n   long free = 0; n   long found = 0; n   for (int i = 0; i < 26; i++) { n     if (vec[i].size() == 0) continue; n     if ((v1[i] + 1) % lcm[i] == v2[i]) { n       if (lcm[i] == 1) n         free++; n       else if (found == 0) n         found = 1; n       else n         return 0; n     } else if (v1[i] != v2[i]) n       return 0; n   } n   if (found) return 1; n   return free; n } n string st; n long val; n long total; n long gcd(long a, long b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n vector<vector<long> > res; n bool bad(long x, long ps) { n   for (int i = 0; i < vec[ps].size(); i++) n     if (x % vec[ps][i] == 0) return false; n   return true; n } n bool check(vector<long> v) { n   for (long i = 0; i < 26; i++) { n     if (vec[i].size() == 0) continue; n     if (bad(v[i], i)) return false; n   } n   return true; n } n void show(vector<vector<long> > v) { n   for (int i = 0; i < v.size(); i++) { n     for (int j = 0; j < v[i].size(); j++) cout << v[i][j] <<    ; n     cout << endl; n   } n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   base = 12345; n   cin >> n >> k; n   for (int i = 1; i <= k; i++) { n     cin >> st >> val; n     vec[st[0] -  A ].push_back(val); n     can[st[0] -  A ][val] = 1; n   } n   total = 1; n   for (int i = 0; i < 26; i++) n     if (vec[i].size()) { n       lcm[i] = 1; n       for (int j = 0; j < vec[i].size(); j++) { n         long q = vec[i][j]; n         lcm[i] = lcm[i] * q / gcd(lcm[i], q); n       } n       total *= lcm[i]; n     } n   res.resize(total); n   for (int i = 0; i < res.size(); i++) res[i].resize(total); n   for (int i = 0; i < total; i++) n     for (int j = 0; j < total; j++) res[i][j] = mov(i, j); n   res = pw(res, n); n   long ans = 0; n   for (int i = 0; i < total; i++) { n     vector<long> v = decomp(i); n     if (check(v)) ans += res[0][i], ans %= base; n   } n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int N = 2e5 + 4; n int n, m, p[N], idx[N]; n LL v1[N], v2[N]; n vector<int> vals[N]; n struct BIT { n   int s[N]; n   void init() { memset(s, 0, sizeof(s)); } n   void add(int i, int v) { n     for (; i <= n; i += i & (-i)) s[i] += v; n   } n   LL get(int i) { n     LL sum = 0; n     for (; i; i -= i & (-i)) sum += s[i]; n     return sum; n   } n } bit; n struct E { n   int id, wh, v; n   E() = default; n   E(int _id, int _wh, int _v) : id(_id), wh(_wh), v(_v) {} n   bool operator<(const E &rhs) const { return wh < rhs.wh; } n }; n struct Q { n   int l, r; n } qs[N]; n vector<E> events; n int main() { n   bit.init(); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , p + i); n     idx[p[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     if (!idx[i]) continue; n     int ii = idx[i]; n     for (int j = i; j <= n; j += i) { n       if (int ij = idx[j]) vals[ij].push_back(ii); n     } n   } n   events.reserve(2 * m); n   for (int i = 1; i <= m; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (l > 1) events.emplace_back(i, l - 1, 1); n     if (r <= n) events.emplace_back(i, r, 2); n     qs[i].l = l; n     qs[i].r = r; n   } n   sort(events.begin(), events.end()); n   for (int i = 1, j = 0, es = events.size(); i <= n && j < es; i++) { n     for (int v : vals[i]) bit.add(v, 1); n     for (; j < es && events[j].wh == i; j++) { n       if (events[j].v == 1) n         v1[events[j].id] = n             bit.get(qs[events[j].id].r) - bit.get(qs[events[j].id].l - 1); n       else n         v2[events[j].id] = n             bit.get(qs[events[j].id].r) - bit.get(qs[events[j].id].l - 1); n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %lld n , v2[i] - v1[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const long long inf = 4e18; n const long long N = 1e6; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m, k; n     cin >> n >> m >> k; n     char a[n][m]; n     bool pos[n][m]; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         cin >> a[i][j]; n         pos[i][j] = 0; n       } n     } n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         if (a[i][j] ==  . ) continue; n         long long d = 0; n         for (long long h = 1; h <= min({i, j, m - 1 - j}); h++) { n           if (a[i - h][j - h] ==  *  && a[i - h][j + h] ==  * ) n             d++; n           else n             break; n         } n         if (d < k) continue; n         pos[i][j] = 1; n         for (long long h = 1; h <= min({i, j, m - 1 - j}); h++) { n           if (a[i - h][j - h] ==  *  && a[i - h][j + h] ==  * ) n             pos[i - h][j - h] = 1, pos[i - h][j + h] = 1; n           else n             break; n         } n       } n     } n     bool boob = 1; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         if (a[i][j] ==  *  && !pos[i][j]) boob = 0; n       } n     } n     if (boob) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   int a = 0, b = 0; n   int i, j; n   int n = s.size(); n   for (i = 1; i < n; ++i) { n     if (s[i] ==  b  && s[i - 1] ==  a ) { n       ++a; n     } n     if (s[i] ==  a  && s[i - 1] ==  b ) { n       ++b; n     } n   } n   if (a != b) { n     if (s[0] ==  a ) n       s[0] =  b ; n     else n       s[0] =  a ; n   } n   cout << s <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int test_case = 1; n   cin >> test_case; n   while (test_case-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, i, ans; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &m), ans += (i + 1) * m; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int64_t dr8[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int64_t dc8[] = {0, 1, 1, 1, 0, -1, -1, -1}; n int64_t dr4[] = {1, 0, 0, -1}; n int64_t dc4[] = {0, 1, -1, 0}; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int64_t i, n, j, m, k, l, t; n   int64_t x; n   cin >> x; n   while (x--) { n     cin >> n; n     vector<int64_t> v(n); n     for (i = 0; i < n; i++) cin >> v[i]; n     for (i = 0; i < n / 2; i++) { n       k = v[i]; n       v[i] = v[n - 1 - i]; n       v[n - 1 - i] = k; n     } n     for (i = 0; i < n; i++) cout << v[i] <<    ; n     cout << endl; n   } n } n