#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k, z; n   cin >> n >> k >> z; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int ans = 0; n   for (int i = 0; i <= z; i++) { n     int kol = k - 2 * i; n     if (kol < 0) { n       continue; n     } n     int tren = 0; n     int maxa = 0; n     for (int j = 0; j < kol + 1; j++) { n       tren += a[j]; n       if (j < n - 1) { n         maxa = max(a[j] + a[j + 1], maxa); n       } n     } n     tren += i * maxa; n     ans = max(ans, tren); n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void remax(T& A, T B) { n   if (A < B) A = B; n } n template <class T> n inline void remin(T& A, T B) { n   if (A > B) A = B; n } n string ToString(long long num) { n   string ret; n   do { n     ret += ((num % 10) +  0 ); n     num /= 10; n   } while (num); n   reverse(ret.begin(), ret.end()); n   return ret; n } n long long ToNumber(string s) { n   long long r = 0, p = 1; n   for (int i = s.size() - 1; i >= 0; --i) r += (s[i] -  0 ) * p, p *= 10; n   return r; n } n long long Gcd(long long a, long long b) { n   while (a %= b ^= a ^= b ^= a) n     ; n   return b; n } n long long Power(long long base, long long power) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret *= base; n     power >>= 1; n     base *= base; n   } n   return ret; n } n long long PowerMod(long long base, long long power, long long mod) { n   if (!power) return 1; n   if (power & 1) return (base * PowerMod(base, power - 1, mod)) % mod; n   return PowerMod((base * base) % mod, power >> 1, mod); n } n int Log(long long num, long long base) { n   int ret = 0; n   while (num) { n     ++ret; n     num /= base; n   } n   return ret; n } n int Count(long long mask) { n   int ret = 0; n   while (mask) { n     if (mask & 1) ++ret; n     mask >>= 1; n   } n   return ret; n } n inline void run() { n   unsigned long long ans; n   long long n; n   scanf( %I64d , &n); n   ans = n * 3; n   ans *= (n + 1); n   cout << ++ans <<   n ; n } n int main() { n   FILE* input = stdin; n   FILE* output = stdout; n   while (!feof(input)) { n     run(); n     break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, n; n void write(int X) { n   if (X < 0) putchar( - ), X = -X; n   if (X > 9) write(X / 10); n   putchar(X % 10 | 48); n } n void print(int k) { n   int h; n   for (h = 1; h <= (4); h++) { n     write(((i - 1) << 2) + h); n     putchar(   ); n     write(((j - 1) << 2) + (!((h + k) % 4) ? 4 : (h + k) % 4)); n     putchar(  n ); n   } n } n int main() { n   cin >> n; n   if (n % 4 == 2 || n % 4 == 3) puts( NO ), exit(0); n   puts( YES ); n   for (i = 1; i <= (n >> 2); i++) { n     if (n % 4 == 1) { n       write(((i - 1) << 2) + 1); n       putchar(   ); n       write(n); n       putchar(  n ); n     } n     write(((i - 1) << 2) + 1); n     putchar(   ); n     write(((i - 1) << 2) + 3); n     putchar(  n ); n     if (n % 4 == 1) { n       write(((i - 1) << 2) + 3); n       putchar(   ); n       write(n); n       putchar(  n ); n     } n     if (n % 4 == 1) { n       write(((i - 1) << 2) + 2); n       putchar(   ); n       write(n); n       putchar(  n ); n     } n     write(((i - 1) << 2) + 2); n     putchar(   ); n     write(((i - 1) << 2) + 4); n     putchar(  n ); n     if (n % 4 == 1) { n       write(((i - 1) << 2) + 4); n       putchar(   ); n       write(n); n       putchar(  n ); n     } n     write(((i - 1) << 2) + 1); n     putchar(   ); n     write(((i - 1) << 2) + 2); n     putchar(  n ); n     write(((i - 1) << 2) + 3); n     putchar(   ); n     write(((i - 1) << 2) + 4); n     putchar(  n ); n     write(((i - 1) << 2) + 2); n     putchar(   ); n     write(((i - 1) << 2) + 3); n     putchar(  n ); n     write(((i - 1) << 2) + 1); n     putchar(   ); n     write(((i - 1) << 2) + 4); n     putchar(  n ); n   } n   for (i = 1; i <= ((n >> 2) - 1); i++) n     for (j = i + 1; j <= (n >> 2); j++) { n       print(2), print(1); n       print(3), print(0); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long arr[n]; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     long long b[n]; n     if (n % 2 == 0) { n       for (long long i = 0; i < n - 1; i = i + 2) { n         b[i] = arr[i + 1]; n         b[i + 1] = -1 * arr[i]; n       } n       for (long long i = 0; i < n; i++) { n         cout << b[i] <<    ; n       } n       cout <<   n ; n     } else { n       for (long long i = 3; i < n - 1; i = i + 2) { n         b[i] = arr[i + 1]; n         b[i + 1] = -1 * arr[i]; n       } n       if (arr[0] + arr[1] != 0) { n         cout << -arr[2] <<     << -arr[2] <<     << arr[0] + arr[1] <<    ; n       } else if (arr[1] + arr[2] != 0) { n         cout << arr[1] + arr[2] <<     << -arr[0] <<     << -arr[0] <<    ; n       } else { n         cout << -arr[1] <<     << arr[0] + arr[2] <<     << -arr[1] <<    ; n       } n       for (long long i = 3; i < n; i++) { n         cout << b[i] <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1}; n const int dc[]{0, 1, 1, 1, 0, -1, -1, -1}; n void run() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void solve() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> v(m); n   vector<int> id; n   for (auto &it : v) { n     cin >> it.first >> it.second; n     id.push_back(it.first); n   } n   sort(id.rbegin(), id.rend()); n   vector<long long> sum(1, 0); n   for (auto &it : id) sum.push_back(it + sum.back()); n   long long mx = sum[min(n, m)]; n   for (auto &it : v) { n     int tmp_n = n; n     int j = upper_bound(id.begin(), id.end(), it.second, greater<int>()) - n             id.begin(); n     long long cur = 0; n     if (it.first < it.second) { n       cur += it.first; n       tmp_n--; n     } n     j = min(j, tmp_n); n     cur += sum[j]; n     tmp_n -= j; n     cur += 1LL * tmp_n * it.second; n     mx = max(mx, cur); n   } n   cout << mx <<   n ; n } n int main() { n   run(); n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio; n   cin.tie(NULL); n   long long i, j, n, t, k, m; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     vector<long long> s(n); n     for (long long i = 0; i < n; i++) { n       cin >> s[i]; n     } n     map<long long, queue<long long>> m; n     for (long long i = 0; i < n; i++) { n       m[s[i]].push(i); n     } n     vector<long long> res(n + 1, 0); n     long long ans = 0; n     long long count = 0; n     long long col = 1; n     vector<long long> v; n     for (auto i : m) { n       if (i.second.size() >= k) { n         long long kk = k; n         col = 1; n         while (!i.second.empty() && kk > 0) { n           res[i.second.front()] = col; n           col++; n           kk--; n           i.second.pop(); n         } n       } else { n         while (!i.second.empty()) { n           v.push_back(i.second.front()); n           i.second.pop(); n         } n       } n     } n     if (v.size() >= k) { n       long long sz = v.size(); n       long long st = sz / k; n       st = st * k; n       long long col = 1; n       for (long long i = 0; i < st; i++) { n         res[v[i]] = col; n         col++; n         if (col == k + 1) { n           col = 1; n         } n       } n     } n     for (long long i = 0; i < n; i++) { n       cout << res[i] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     char s[n + 1]; n     scanf( %s , s + 1); n     int a[100] = {0}, b[100] = {0}; n     int aa = 0, bb = 0; n     for (int i = 1; i <= n; i++) { n       if (s[i] ==  a ) { n         aa++; n         b[i] = b[i - 1]; n         a[i] = a[i - 1] + 1; n       } else { n         bb++; n         a[i] = a[i - 1]; n         b[i] = b[i - 1] + 1; n       } n     } n     int flag = 1; n     if (aa == 0 || bb == 0) { n       printf( -1 -1 n ); n       continue; n     } else { n       for (int i = 1; i <= n; i++) { n         if (flag == 1) n           for (int j = n; j >= i; j--) { n             if (a[j] - a[i - 1] == b[j] - b[i - 1]) { n               printf( %d %d n , i, j); n               flag = 0; n               break; n             } n           } n         else n           break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, u, r, i; n bool flag = false; n long long max_score = 0; n vector<long long> a, b, k, p; n inline long long score(vector<long long> a) { n   long long s = 0; n   for (i = (0); i < (n); ++i) { n     s += a[i] * k[i]; n   } n   return s; n } n vector<long long> permute(const vector<long long> &a) { n   vector<long long> temp(a.size()); n   for (i = (0); i < (n); ++i) { n     temp[i] = a[p[i] - 1] + r; n   } n   return temp; n } n void recurse(vector<long long> a, long long remain) { n   if (remain % 2 == 0) { n     long long sc = score(a); n     if (!flag || max_score < sc) { n       max_score = sc; n       flag = true; n     } n   } n   if (remain == 0) return; n   recurse(permute(a), remain - 1); n   remain--; n   for (i = (0); i < (n); ++i) { n     a[i] = a[i] ^ b[i]; n   } n   if (remain % 2 == 0) { n     long long sc = score(a); n     if (!flag || max_score < sc) { n       max_score = sc; n       flag = true; n     } n   } n   if (remain == 0) return; n   recurse(permute(a), remain - 1); n } n int main(int argc, char *argv[]) { n   cin >> n >> u >> r; n   a = vector<long long>(n); n   b = vector<long long>(n); n   p = vector<long long>(n); n   k = vector<long long>(n); n   for (i = (0); i < (n); ++i) { n     cin >> a[i]; n   } n   for (i = (0); i < (n); ++i) { n     cin >> b[i]; n   } n   for (i = (0); i < (n); ++i) { n     cin >> k[i]; n   } n   for (i = (0); i < (n); ++i) { n     cin >> p[i]; n   } n   recurse(a, u); n   cout << max_score << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s, d; n bool f(char x) { n   if (x ==  a  || x ==  e  || x ==  i  || x ==  o  || x ==  u ) return true; n   if (x >=  0  && x <=  9 ) { n     int p = x -  0 ; n     if (p % 2 == 1) return true; n   } n   return false; n } n int main() { n   string s; n   cin >> s; n   int d = 0; n   for (int i = 0; i < s.length(); i++) { n     if (f(s[i])) d++; n   } n   cout << d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n const long long maxn = 1e5 + 10; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 10; n vector<long long> g[maxn]; n long long deg[maxn]; n long long a[maxn]; n bool mark[maxn]; n long long rem; n vector<long long> path, V; n inline void dfs(long long node) { n   mark[node] = true; n   rem--; n   path.push_back(node); n   for (auto u : g[node]) { n     if (mark[u]) { n       cout << 0 << endl; n       exit(0); n     } n     dfs(u); n   } n } n long long dp[maxn]; n int32_t main() { n   long long n = in(), q = in(), t = in(); n   rem = n; n   for (long long i = 0; i < n; i++) a[i] = in(); n   for (long long i = 0; i < q; i++) { n     long long from = in() - 1, to = in() - 1; n     g[from].push_back(to); n     deg[to]++; n   } n   V.push_back(0); n   dp[0] = 1; n   for (long long i = 0; i < n; i++) { n     if (!mark[i] && !deg[i]) { n       path.clear(); n       dfs(i); n       long long prefsum = 0; n       for (auto v : path) { n         prefsum += a[v]; n         V.push_back(prefsum); n         if (v - path.back()) t -= prefsum; n       } n     } n   } n   if (rem) return cout << 0 << endl, 0; n   if (t < 0) return cout << 0 << endl, 0; n   for (long long i = 1; i < V.size(); i++) { n     for (long long pre = 0; pre + V[i] < maxn; pre++) n       (dp[pre + V[i]] += dp[pre]) %= mod; n   } n   cout << dp[t] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void er() { n   cout <<  NO  << endl; n   exit(0); n } n int main() { n   int n; n   cin >> n; n   int a[n + 1]; n   int o = 0; n   int um = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] != 0) o++; n     um = um + a[i]; n   } n   if (o < 1) er(); n   if (um != 0) { n     cout <<  YES  << endl; n     cout << 1 << endl; n     cout << 1 <<     << n << endl; n     return 0; n   } n   cout <<  YES  << endl; n   cout << 2 << endl; n   for (int i = 1; i <= n; i++) { n     if (a[i] != 0) { n       cout << 1 <<     << i << endl; n       cout << i + 1 <<     << n << endl; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double e = 1e-2; n int k; n long double R, r, l; n long double x, y, xl, yl, xr, yr; n long double a, b, c, S; n long double COS[12], SIN[12]; n bool check(long double x, long double y) { n   long double s = 0; n   for (int i = 0; i < 12; i += 2) n     s = max(s, ((x + COS[i]) * (x + COS[i])) + ((y + SIN[i]) * (y + SIN[i]))); n   return s <= l + r; n } n bool end(long double x, long double y) { n   long double s = 1e+60; n   for (int i = 0; i < 12; i += 2) n     s = min(s, ((x + COS[i]) * (x + COS[i])) + ((y + SIN[i]) * (y + SIN[i]))); n   return s > S; n } n int main() { n   R = 1; n   r = sqrt(((R) * (R)) - ((0.5) * (0.5))); n   for (int i = 0; i < 12; i++) { n     COS[i] = cos(((i * 30) * M_PI / 180)); n     SIN[i] = sin(((i * 30) * M_PI / 180)); n   } n   scanf( %d , &k); n   l = k; n   l *= l; n   S = k + 3; n   S *= S; n   x = (k + 2) / 3 * 3 * COS[2]; n   y = (k + 2) / 3 * 3 * SIN[2]; n   xl = xr = x; n   yl = yr = y; n   long long ans = 0; n   do { n     long double s; n     s = y * xr - x * yr; n     while (s > 0 && abs(s) > e && !check(xr, yr)) { n       xr += 2 * r * COS[5]; n       yr += 2 * r * SIN[5]; n       s = y * xr - x * yr; n     } n     while (check(xr, yr)) { n       xr -= 2 * r * COS[5]; n       yr -= 2 * r * SIN[5]; n     } n     s = y * xl - x * yl; n     while (s < 0 && abs(s) > e && !check(xl, yl)) { n       xl += 2 * r * COS[11]; n       yl += 2 * r * SIN[11]; n       s = y * xl - x * yl; n     } n     while (check(xl, yl)) { n       xl -= 2 * r * COS[11]; n       yl -= 2 * r * SIN[11]; n     } n     long double L = sqrt(((xl - xr) * (xl - xr)) + ((yl - yr) * (yl - yr))); n     ans += max((long long)((L + r) / (2 * r)) - 1, 0LL); n     xl += 2 * r * COS[7]; n     yl += 2 * r * SIN[7]; n     xr += 2 * r * COS[9]; n     yr += 2 * r * SIN[9]; n   } while (!end(xr, yr) || !end(xl, yl)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   string s[n]; n   long long flg = 1; n   long long ar[n]; n   for (long long i = 0; i < n; i++) { n     cin >> s[i]; n     long long flag = 0; n     long long pos = -1; n     for (long long j = 0; j < n; j++) { n       if (s[i][j] ==  . ) { n         flag = 1; n         pos = j; n         break; n       } n     } n     if (flag == 0) { n       flg = 0; n     } n     ar[i] = pos; n   } n   if (!flg) { n     flg = 1; n     long long ar[n]; n     for (long long i = 0; i < n; i++) { n       long long flag = 0; n       long long pos = -1; n       for (long long j = 0; j < n; j++) { n         if (s[j][i] ==  . ) { n           flag = 1; n           pos = j; n           break; n         } n       } n       if (flag == 0) { n         flg = 0; n         break; n       } n       ar[i] = pos; n     } n     if (!flg) n       cout << -1; n     else { n       for (long long i = 0; i < n; i++) { n         cout << ar[i] + 1 <<     << i + 1 <<   n ; n       } n     } n     return 0; n   } else { n     for (long long i = 0; i < n; i++) { n       cout << i + 1 <<     << ar[i] + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   getline(cin, s); n   long long int a[s.size()]; n   vector<long long int> v; n   for (long long int i = 0; i < s.size(); i++) { n     if (i == 0) { n       vector<long long int> tp(8, 0); n       long long int j = 0; n       long long int k = s[i]; n       while (k) { n         tp[j] = (k >> 0) & 1; n         k = k >> 1; n         j++; n       } n       long long int ans = 0; n       reverse(tp.begin(), tp.end()); n       for (long long int l = 0; l < 8; l++) { n         ans = ans + tp[l] * pow(2, l); n       } n       v.push_back(ans); n       a[i] = 256 - ans; n       continue; n     } n     vector<long long int> tp(8, 0); n     long long int j = 0; n     long long int k = s[i]; n     while (k) { n       tp[j] = (k >> 0) & 1; n       k = k >> 1; n       j++; n     } n     long long int ans = 0; n     reverse(tp.begin(), tp.end()); n     for (long long int l = 0; l < 8; l++) { n       ans = ans + tp[l] * pow(2, l); n     } n     a[i] = (v[i - 1] - ans + 256) % 256; n     v.push_back(ans); n   } n   for (long long int i = 0; i < s.size(); i++) cout << a[i] <<   n  ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 400005; n template <typename T> n void cmin(T &x, const T &y) { n   if (y < x) x = y; n } n template <typename T> n void cmax(T &x, const T &y) { n   if (y > x) x = y; n } n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   bool f = 0; n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) f = 1, c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   if (f) x = -x; n } n int fst[MX], nxt[MX], v[MX], lnum; n int ida[MX], idb[MX], on_path[MX]; n int n; n void addeg(int nu, int nv) { n   nxt[++lnum] = fst[nu]; n   fst[nu] = lnum; n   v[lnum] = nv; n } n void input() { n   read(n); n   for (int i = 1; i <= n; i++) read(ida[i]); n   for (int i = 1; i <= n; i++) read(idb[i]); n   for (int i = 1; i < n; i++) { n     int a, b; n     read(a), read(b); n     addeg(a, b); n     addeg(b, a); n   } n } n int dep[MX], fa[MX], usd_mov; n void dfs1(int x, int f, int tar) { n   dep[x] = dep[f] + 1, fa[x] = f; n   if (x == tar) { n     int tmp = x; n     on_path[x] = 1; n     while (fa[tmp]) n       swap(ida[tmp], ida[fa[tmp]]), tmp = fa[tmp], on_path[tmp] = 1, usd_mov++; n   } n   for (int i = fst[x]; i; i = nxt[i]) { n     int y = v[i]; n     if (v[i] != f) dfs1(v[i], x, tar); n   } n } n void fuck_out(char *s) { n   cerr << s << endl; n   puts( -1 ); n   exit(0); n } n int posa[MX], posb[MX], on_cyc[MX]; n void work() { n   int frm = 0, tar = 0; n   for (int i = 1; i <= n; i++) n     if (!ida[i]) frm = i; n   for (int i = 1; i <= n; i++) n     if (!idb[i]) tar = i; n   dfs1(tar, 0, frm); n   for (int i = 1; i <= n; i++) posa[ida[i]] = i, posb[idb[i]] = i; n   for (int i = 1; i <= n; i++) n     if (ida[i] != idb[i]) on_cyc[i] = 1; n   int bot1 = 0, bot2 = 0; n   static int vis[MX]; n   vector<int> cyc; n   for (int i = 1; i <= n; i++) n     if (on_cyc[i] && dep[i] > dep[bot1]) bot1 = i; n   for (int x = bot1; on_cyc[x]; x = fa[x]) cyc.push_back(x), vis[x] = 1; n   reverse(cyc.begin(), cyc.end()); n   if (!bot1) { n     printf( %d %d n , 0, usd_mov); n     return; n   } n   for (int i = 1; i <= n; i++) n     if (on_cyc[i] && dep[i] > dep[bot2] && !vis[i]) bot2 = i; n   for (int x = bot2; on_cyc[x]; x = fa[x]) cyc.push_back(x), vis[x] = -1; n   int top1 = *cyc.begin(), top2 = *cyc.rbegin(), entry = fa[top1]; n   if (bot2 && fa[top1] != fa[top2]) fuck_out( unconnected loop! ); n   for (int i = 1; i <= n; i++) n     if (on_cyc[i] && !vis[i]) fuck_out( not a single loop! ); n   int step = 0, revs = 0; n   for (int i = 0; i < cyc.size(); i++) n     if (idb[cyc[i]] == ida[cyc[0]]) step = i; n   for (int i = 0; i < cyc.size(); i++) n     if (idb[cyc[(i + step) % cyc.size()]] != ida[cyc[i]]) n       fuck_out( can t rearrange loop! ); n   for (int x = frm; x; x = fa[x]) revs += vis[x]; n   long long way1 = usd_mov + 1ll * step * (cyc.size() + 1); n   long long way2 = usd_mov + 1ll * (cyc.size() - step) * (cyc.size() + 1); n   if (revs > 0) n     way2 -= revs * 2; n   else n     way1 += revs * 2; n   long long ans = min(way1, way2); n   int adda = 0, addb = 0; n   if (bot1 && bot2) n     adda = bot1, addb = bot2; n   else n     adda = bot1, addb = entry; n   for (int x = entry; !on_path[x]; x = fa[x]) ans += 2; n   if (adda > addb) swap(adda, addb); n   printf( %d %d %lld n , adda, addb, ans); n } n int main() { n   input(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   long long m_min = 1ll << 62; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     ans += x; n     if (x % 2 == 1) m_min = min(m_min, x); n   } n   if (ans % 2 == 1) ans -= m_min; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1e6 + 5; n long long int power(long long int base, long long int exp) { n   long long int res = 1; n   while (exp) { n     if (exp % 2) res *= base; n     base *= base; n     res %= mod; n     base %= mod; n     exp /= 2; n   } n   return res; n } n long long int n, m, k, l[maxn], r[maxn], dp[350][350], adj[350][350], c[maxn]; n long long int go(long long int idx, long long int k) { n   if (k <= 0) return 0; n   if (idx >= n + 1) return 1e16; n   long long int &ret = dp[idx][k]; n   if (ret != -1) return ret; n   ret = go(idx + 1, k); n   for (long long int i = idx; i <= min(idx + k, n); i++) { n     ret = min(ret, go(i + 1, k - (i - idx + 1)) + adj[idx][i]); n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> k; n   for (long long int i = 1; i <= n; i++) n     for (long long int j = i; j <= n; j++) adj[i][j] = 1e15; n   for (long long int i = 1; i <= m; i++) { n     cin >> l[i] >> r[i] >> c[i]; n     adj[l[i]][r[i]] = min(adj[l[i]][r[i]], c[i]); n   } n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= n; j++) { n       if (i < j) break; n       for (long long int k = j; k <= n; k++) { n         adj[j][k] = min(adj[j][k], adj[j][i] + adj[i][k]); n       } n     } n   } n   memset(dp, -1, sizeof(dp)); n   long long int ans = go(1, k); n   if (ans > 1e12) ans = -1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, A; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> A; n     if (i == n) { n       while (A--) cout <<  PLR ; n       return 0; n     } n     while (A--) { n       cout <<  PRL ; n     } n     cout <<  R ; n   } n } n


#include <bits/stdc++.h> n const int inf = int(1e9); n const int mod = inf + 7; n const double PI = acos(-1.0); n using namespace std; n const int N = 1e5 + 155; n void solve() { n   int n, k, s = 0; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     s += x; n   } n   for (int ans = 0;; ans++) { n     int a = 2 * (s + ans * k); n     int b = (2 * k - 1) * (ans + n); n     if (a >= b) { n       cout << ans; n       return; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   int ans = 0; n   while ((a >= 1 && b > 1) || (a > 1 && b >= 1)) { n     if (a < b) { n       ans++; n       a--; n       b -= 2; n     } else { n       ans++; n       a -= 2; n       b--; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int L[7], R[7]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> L[i] >> R[i]; n   } n   long double cases = 0; n   long double sum = 0; n   for (int p = 1; p <= 10000; ++p) { n     for (int mask = 3; mask < 1 << n; ++mask) { n       bool ok = true; n       long double cnt = 1; n       for (int j = 1; j <= n; ++j) n         if (!((mask >> (j - 1)) & 1)) { n           if (L[j] < p) { n             cnt *= min(p - 1, R[j]) - L[j] + 1; n           } else { n             ok = false; n           } n         } n       if (!ok) continue; n       vector<pair<bool, int>> ranges; n       for (int j = 1; j <= n; ++j) n         if ((mask >> (j - 1)) & 1) { n           if (p <= R[j]) { n             ranges.push_back(make_pair(L[j] <= p, R[j] - max(p + 1, L[j]) + 1)); n           } else { n             ok = false; n           } n         } n       if (!ok) continue; n       long double cnt2 = 0; n       for (int j = 0; j < int(ranges.size()); ++j) { n         bool ok = true; n         for (int k = 0; k < int(ranges.size()); ++k) n           if (k != j) n             if (!ranges[k].first) ok = false; n         if (ok) { n           cnt2 += ranges[j].second; n         } n       } n       int b = 0; n       for (int tmp = mask; tmp; tmp &= tmp - 1) ++b; n       if (b < 2) continue; n       bool add = true; n       for (int j = 0; j < int(ranges.size()); ++j) { n         add &= ranges[j].first; n       } n       cnt *= (cnt2 + add); n       cases += cnt; n       sum += cnt * p; n     } n   } n   cout << fixed << setprecision(15) << 1.0 * sum / cases; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sub; n   int grp; n   cin >> n >> sub; n   if (n == sub) n     grp = 0; n   else if (sub == 0 || sub == 1) n     grp = 1; n   else if (sub >= n - n / 2) n     grp = n - sub; n   else n     grp = sub; n   cout << grp << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Bp, typename Ep, typename Mp> n inline Mp __powMod(Bp a, Ep b, const Mp &m) { n   a %= m; n   Mp res = 1; n   while (b > 0) { n     if (b & 1) { n       res = ((long long)res * a) % m; n     } n     a = ((long long)a * a) % m; n     b >>= 1; n   } n   return res % m; n } n const int MOD = 1e9 + 7; n template <typename Tp> n inline Tp inv(const Tp &x) { n   return __powMod(x, MOD - 2, MOD); n } n const int N = 100000 + 5; n int m; n int dp[N]; n vector<int> prime; n int mu[N]; n bool notPrime[N]; n inline void init() { n   mu[1] = 1; n   for (int i = 2; i < N; ++i) { n     if (!notPrime[i]) { n       mu[i] = -1; n       prime.emplace_back(i); n     } n     for (const auto &p : prime) { n       if (i * p >= N) { n         break; n       } n       notPrime[i * p] = true; n       if (i % p == 0) { n         mu[i * p] = 0; n         break; n       } else { n         mu[i * p] = -mu[i]; n       } n     } n   } n } n int main() { n   init(); n   scanf( %d , &m); n   int ans = 1; n   for (int i = 2, t; i <= m; ++i) { n     if (mu[i] != 0) { n       t = m / i; n       ans = (ans - (long long)mu[i] * t * inv(m - t) % MOD + MOD) % MOD; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n double pd[40][40][300]; n int n, K; n int r[40]; n int go(int a, int i, int j) { n   if (a < i || a > j) return a; n   return j - (a - i); n } n double solve(int a, int b, int k) { n   if (k == 0) return b < a; n   double& ret = pd[a][b][k]; n   if (ret > -0.5) return ret; n   ret = 0; n   double mul = 1. / ((double)((n * (n + 1)) / 2)); n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) { n       int aa = go(a, i, j); n       int bb = go(b, i, j); n       ret += mul * solve(aa, bb, k - 1); n     } n   return ret; n } n int main() { n   cin >> n >> K; n   memset(pd, -1, sizeof pd); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     r[x] = i; n   } n   double ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       ans += solve(r[i], r[j], K); n     } n   printf( %.15f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 998244353; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   long long n, k; n   cin >> n >> k; n   long long m = 0; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   for (long long i = n; i > n - k; i--) { n     m += i; n   } n   vector<long long> ind; n   for (long long i = 0; i < n; i++) { n     if (v[i] > n - k) ind.push_back(i); n   } n   long long x = 1; n   for (long long i = 1; i < ind.size(); i++) { n     x *= ind[i] - ind[i - 1]; n     x %= MOD; n   } n   cout << m <<     << x; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int nxt[100001]; n char s[100001], t[100001]; n int temp[100001]; n int l, r; n bool check(int u) { n   for (int i = 0; i < m; ++i) { n     if (s[u + i] != t[i]) return 0; n   } n   return 1; n } n void pre_do() { n   for (int i = 1; i <= n - m + 1; i++) { n     temp[i] += check(i); n   } n } n void putin() { n   cin >> n >> m >> q; n   scanf( %s%s , s + 1, t); n } n int main() { n   putin(); n   pre_do(); n   for (int i = 1; i <= n; i++) { n     temp[i] += temp[i - 1]; n   } n   for (int i = 1; i <= q; i++) { n     cin >> l >> r; n     if (r - l + 1 < m) n       cout << 0 << endl; n     else n       cout << temp[r - m + 1] - temp[l - 1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e4 + 5; n const int SQRN = 210; n const int BSZ = 256; n vector<int> g[MAXN]; n int v[MAXN], lvl[MAXN], pai[MAXN]; n int jump[MAXN], best[MAXN][SQRN]; n int trie[BSZ * 20 + 5][2]; n int n, q, cnt_trie = 1; n void predfs(int cur, int p) { n   for (auto viz : g[cur]) { n     if (viz == p) continue; n     lvl[viz] = lvl[cur] + 1; n     pai[viz] = cur; n     predfs(viz, cur); n   } n } n void build(int cur, int k, int root) { n   if (k >= BSZ) return; n   jump[root] = cur; n   int val = v[cur] ^ k; n   int tcur = 1; n   for (int i = 16; i >= 0; i--) { n     bool viz = val & (1 << i); n     if (!trie[tcur][viz]) trie[tcur][viz] = ++cnt_trie; n     tcur = trie[tcur][viz]; n   } n   if (pai[cur] == cur) return; n   build(pai[cur], k + 1, root); n } n int find_best(int mask) { n   int cur = 1; n   int resp = 0; n   for (int i = 16; i >= 0; i--) { n     bool want = 1 ^ ((bool)(mask & (1 << i))); n     if (trie[cur][want]) n       cur = trie[cur][want], resp += (1 << i); n     else n       cur = trie[cur][!want]; n   } n   return resp; n } n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n   for (int i = 1; i <= n - 1; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   pai[1] = 1; n   predfs(1, 1); n   for (int i = 1; i <= n; i++) { n     build(i, 0, i); n     for (int k = 0; k * BSZ <= n; k++) best[i][k] = find_best(k * BSZ); n     memset(trie, 0, sizeof(trie)); n     cnt_trie = 1; n   } n   while (q--) { n     int a, b; n     scanf( %d %d , &a, &b); n     int w = 0; n     int respf = 0; n     while (lvl[jump[b]] > lvl[a]) { n       respf = max(respf, best[b][w]); n       b = pai[jump[b]]; n       w++; n     } n     int dist = w * BSZ; n     while (b != a) { n       respf = max(respf, v[b] ^ dist); n       b = pai[b]; n       dist++; n     } n     respf = max(respf, v[a] ^ dist); n     printf( %d n , respf); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t = 0, c = 0; n   char ch[105][105]; n   cin >> n; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> ch[i][j]; n   for (int i = 1; i <= n; i++) { n     c = 0; n     for (int j = 1; j <= n; j++) { n       if (ch[i][j] ==  C ) c++; n     } n     t += (c * (c - 1)) / 2; n   } n   for (int j = 1; j <= n; j++) { n     c = 0; n     for (int i = 1; i <= n; i++) { n       if (ch[i][j] ==  C ) c++; n     } n     t += (c * (c - 1)) / 2; n   } n   cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n namespace std { n bool operator<(const complex<double> &lhs, const complex<double> &rhs) { n   if (real(lhs) == real(rhs)) { n     return imag(lhs) < imag(rhs); n   } else { n     return real(lhs) < real(rhs); n   } n } n }  // namespace std n ostream &operator<<(ostream &os, const complex<double> &p) { n   return os <<  (  << p.real() <<  ,  << p.imag() <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &vec) { n   os <<  [ ; n   for (const auto &v : vec) { n     os << v <<  , ; n   } n   os <<  ] ; n   return os; n } n complex<double> counter_clock_wise(complex<double> p, double rad) { n   double a = abs(p); n   return polar(a, arg(p) + rad); n } n const double EPS = 1e-7; n const double PI = 3.141592653589793; n double dot(complex<double> a, complex<double> b) { return real(conj(a) * b); } n double point_distance(complex<double> a, complex<double> b) { n   return abs(a - b); n } n bool is_square(vector<complex<double> > ps) { n   sort(ps.begin(), ps.end()); n   vector<double> dist; n   for (long long i = 0; i < 4; i++) { n     for (long long j = i + 1; j < 4; j++) { n       if (abs(ps[i] - ps[j]) < EPS) return false; n       dist.push_back(abs(ps[i] - ps[j])); n     } n   } n   sort(dist.begin(), dist.end()); n   for (long long i = 0; i < 4; i++) { n     if (abs(dist[0] - dist[i]) > EPS) return false; n   } n   for (long long i = 4; i < 6; i++) { n     if (abs(sqrt(2) * dist[1] - dist[i]) > EPS) return false; n   } n   return true; n } n const long long INF = 100000000; n long long solve(vector<long long> y, vector<long long> x, vector<long long> b, n                 vector<long long> a) { n   long long ret = INF; n   ; n   ; n   for (long long i = 0; i < static_cast<long long>(4); i++) { n     for (long long j = 0; j < static_cast<long long>(4); j++) { n       for (long long k = 0; k < static_cast<long long>(4); k++) { n         for (long long l = 0; l < static_cast<long long>(4); l++) { n           vector<long long> cur = {i, j, k, l}; n           vector<complex<double> > ps; n           for (long long m = 0; m < static_cast<long long>(4); m++) { n             complex<double> p(x[m], y[m]); n             p -= complex<double>(a[m], b[m]); n             p = counter_clock_wise(p, (PI / 2) * cur[m]); n             p += complex<double>(a[m], b[m]); n             ps.push_back(p); n           } n           sort(ps.begin(), ps.end()); n           if (is_square(ps)) { n             ; n             ; n             ret = min(ret, i + j + k + l); n           } n         } n       } n     } n   } n   if (ret == INF) return -1; n   return ret; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     vector<long long> y(4), x(4), b(4), a(4); n     for (long long j = 0; j < 4; j++) { n       cin >> x[j] >> y[j] >> a[j] >> b[j]; n     } n     long long ret = solve(y, x, b, a); n     cout << ret << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace IO { n char gc() { return getchar(); } n template <typename Tp> n bool get1(Tp &x) { n   bool neg = 0; n   char c = gc(); n   while (c != EOF && (c <  0  || c >  9 ) && c !=  - ) c = gc(); n   if (c ==  - ) c = gc(), neg = 1; n   if (c == EOF) return false; n   x = 0; n   for (; c >=  0  && c <=  9 ; c = gc()) x = x * 10 + c -  0 ; n   if (neg) x = -x; n   return true; n } n template <typename Tp> n void printendl(Tp x) { n   if (x < 0) putchar( - ), x = -x; n   static short a[40], sz; n   sz = 0; n   while (x > 0) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n   puts(  ); n } n }  // namespace IO n using IO::get1; n using IO::printendl; n const int inf = 0x3f3f3f3f; n const long long Linf = 1ll << 61; n const int maxn = 111; n int n, q1, q2, a[maxn], b[maxn], c[maxn], ans[maxn]; n int d[maxn]; n void getv(int id) { n   for (int i = 1; i <= n; i++) a[id] += d[i] * (d[i] - 1) * (d[i] - 2) / 6; n   for (int i = 1; i <= n - 2; i++) b[id] += d[i] * d[i + 1] * d[i + 2]; n } n int main() { n   get1(n); n   get1(a[0]) && get1(b[0]); n   for (int i = 1; i <= n; i++) { n     c[i] = (i < n ? i : 1); n     printf( + %d n , c[i]); n     fflush(stdout); n     get1(a[i]) && get1(b[i]); n   } n   for (int i = n; i >= 1; i--) { n     a[i] -= a[i - 1]; n     b[i] -= b[i - 1]; n   } n   memset(ans, -1, sizeof(ans)); n   ans[1] = (a[n] - a[1]); n   for (int i = 2; i < n; i++) n     if (a[i]) { n       ans[i] = 0; n       while (ans[i] * (ans[i] - 1) / 2 < a[i]) ans[i]++; n     } n   if (ans[3] == -1) ans[3] = (b[2] > 0); n   if (ans[2] == -1) ans[2] = b[n] / (ans[3] + 1) - 1; n   for (int i = 3; i < n - 1; i++) { n     b[i] -= (ans[i - 1] + 1) * (ans[i - 2] + 1); n     if (ans[i + 1] == -1) ans[i + 1] = (b[i] > 0); n   } n   ans[n] = (b[n - 1] - (ans[n - 2] + 1) * (ans[n - 3] + 1)) / (ans[n - 2] + 1); n   printf( ! ); n   for (int i = 1; i <= n; i++) printf(  %d , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T x, T y) { n   if (x < y) swap(x, y); n   while (y > 0) { n     T f = x % y; n     x = y; n     y = f; n   } n   return x; n } n const int maxn = 200005; n int n; n int a[maxn]; n int dp[maxn]; n vector<int> adj[maxn]; n void dfs(int u, int p, set<int> gcd0, set<int> gcd1) { n   dp[u] = *gcd0.rbegin(); n   if (!gcd1.empty()) { n     dp[u] = max(dp[u], *gcd1.rbegin()); n   } n   for (int v : adj[u]) { n     if (v != p) { n       set<int> g0; n       set<int> g1 = gcd0; n       for (auto x : gcd0) g0.insert(gcd(x, a[v])); n       for (auto x : gcd1) g1.insert(gcd(x, a[v])); n       dfs(v, u, g0, g1); n     } n   } n } n int main(int argc, char *argv[]) { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   cin >> n; n   { n     for (int i = 1; i <= n; i++) cin >> a[i], dp[i] = i; n     for (int i = 1; i < n; i++) { n       int x, y; n       cin >> x >> y; n       adj[x].push_back(y); n       adj[y].push_back(x); n     } n     set<int> gcd0; n     set<int> gcd1; n     gcd0.insert(a[1]); n     gcd1.insert(0); n     dfs(1, 0, gcd0, gcd1); n     for (int i = 1; i <= n; i++) { n       cout << dp[i] << (i == n ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n int v[N], md[N]; n vector<int> freq[N]; n int lm[N], rm[N]; n int cs[N]; n int val(int x, int l, int r) { n   if (l > r || l < 0) return 0; n   return upper_bound(freq[x].begin(), freq[x].end(), r) - n          lower_bound(freq[x].begin(), freq[x].end(), l); n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   freq[0].push_back(0); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &v[i]); n     md[i] = v[i] % k; n     cs[i] += v[i] % k; n     cs[i] += cs[i - 1]; n     cs[i] %= k; n     freq[cs[i]].push_back(i); n   } n   stack<pair<int, int> > s; n   for (int i = n; i > 0; i--) { n     while (s.size() && s.top().first < v[i]) { n       s.pop(); n     } n     if (s.size()) { n       rm[i] = s.top().second - 1; n     } else n       rm[i] = n; n     s.push(make_pair(v[i], i)); n   } n   while (!s.empty()) { n     s.pop(); n   } n   for (int i = 1; i <= n; i++) { n     while (s.size() && s.top().first <= v[i]) { n       s.pop(); n     } n     if (s.size()) { n       lm[i] = s.top().second + 1; n     } else n       lm[i] = 1; n     s.push(make_pair(v[i], i)); n   } n   long long ans = 0; n   int l, r, al, ar; n   int x, d; n   for (int i = 1; i <= n; i++) { n     if (lm[i] == rm[i]) continue; n     if (abs(lm[i] - i) + 1 < abs(i - rm[i]) + 1) { n       l = lm[i] - 1, r = i - 2; n       al = i + 1, ar = rm[i]; n       d = md[i]; n     } else { n       d = -md[i]; n       l = i + 1, r = rm[i]; n       al = lm[i] - 1, ar = i - 2; n     } n     for (int j = l; j <= r; j++) { n       x = cs[j] + d + k; n       x %= k; n       ans += val(x, al, ar); n     } n     ans += val(cs[i - 1], lm[i] - 1, i - 2); n     ans += val(cs[i], i + 1, rm[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int u[1005]; n int v[1005]; n int main() { n   int n, k, i; n   cin >> n >> k; n   int a, b, c, d; n   int t; n   cin >> a >> b >> c >> d; n   if (n == 4) { n     cout <<  -1  << endl; n   } else if (k < n + 1) { n     cout <<  -1  << endl; n   } else { n     cout << a <<    ; n     cout << c <<    ; n     for (i = 1; i <= n; i++) { n       if (i == a || i == b || i == c || i == d) { n         continue; n       } n       cout << i <<    ; n     } n     cout << d <<    ; n     cout << b << endl; n     cout << c <<    ; n     cout << a <<    ; n     for (i = 1; i <= n; i++) { n       if (i == a || i == b || i == c || i == d) { n         continue; n       } n       cout << i <<    ; n     } n     cout << b <<    ; n     cout << d << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NR = 4e6 + 5; n int n; n long long P = 51123987; n char s[NR]; n int d[NR]; n long long f[NR], g[NR]; n int main() { n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = n; i >= 1; i--) { n     s[i * 2] = s[i]; n     s[i * 2 - 1] =  # ; n   } n   s[n * 2 + 1] =  # ; n   n = n * 2 + 1; n   int p = 0, maxr = 0; n   for (int i = 1; i <= n; i++) { n     if (i <= maxr) d[i] = min(d[p + maxr - i], maxr - i + 1); n     while (i - d[i] >= 1 && i + d[i] <= n && s[i - d[i]] == s[i + d[i]]) d[i]++; n     if (i + d[i] - 1 > maxr) { n       p = i - d[i] + 1; n       maxr = i + d[i] - 1; n     } n     if (s[i] ==  #  && d[i] == 1) continue; n     f[(i - d[i] + 2) / 2]++; n     f[i / 2 + 1]--; n     if (s[i] ==  # ) n       g[i / 2 + 1]++; n     else n       g[i / 2]++; n     g[(i + d[i] - 1) / 2 + 1]--; n   } n   n = (n - 1) / 2; n   for (int i = 1; i <= n; i++) { n     f[i] += f[i - 1]; n     f[i] %= P; n     g[i] += g[i - 1]; n     g[i] %= P; n   } n   long long sum1 = 0, sum2 = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     sum1 += f[i]; n     sum1 %= P; n     ans += f[i] * sum2; n     ans %= P; n     sum2 += g[i]; n     sum2 %= P; n   } n   sum1 = sum1 * (sum1 - 1) / 2 % P; n   printf( %lld n , (sum1 - ans + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 1; n const long long M = 1e9 + 7; n const long long H = 1e5 + 7; n const long long B = 1e4 + 7; n const long long A = 1e6 + 7; n set<long long> o; n map<long long, long long> mp, pm; n map<char, long long> hs; n vector<long long> v, vc, oreder1, inoreder, posoreder, ve, vv, vd; n deque<char> p, pe; n deque<string> ts, ss, stoi, rs; n deque<double> sd, ds; n string g, s, w, h, s1; n double l; n char z, u =  a ; n bool re, rt; n deque<int> e; n map<pair<long long, long long>, long long> aw, wa; n long long cnt, ans, TT, n, a, b, c, q, m, t, d, temp, mx = 0, mn = 0, rem, sum, n                                                       tt = 1e32; n long long used[N]; n int main() { n   ios_base::sync_with_stdio(0); n   srand(time(NULL)); n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a >> b; n     cnt = 0; n     for (long long i = 0; i < a; i++) { n       cin >> l; n       cnt += l; n     } n     if (cnt == b) cout <<  YES  << endl; n     if (cnt != b) cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[225][1 << 16]; n int dx[4] = {0, -1, 0, 1}; n int dy[4] = {1, 0, -1, 0}; n int tran[4] = {2, 3, 0, 1}; n string s[15]; n bool visit[15][15][1 << 16]; n int n, m, length, backx, backy, startbody, status; n struct snake { n   int x, y, body; n } start; n bool in(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); } n bool collision(int x, int y, snake sn) { n   int body = sn.body; n   int xx = sn.x; n   int yy = sn.y; n   for (int i = 1; i < length - 1; i++) { n     xx += dx[tran[body & 3]]; n     yy += dy[tran[body & 3]]; n     body >>= 2; n     if (x == xx && y == yy) return true; n   } n   return false; n } n int BFS() { n   queue<pair<snake, int>> Q; n   Q.push({start, 0}); n   visit[start.x][start.y][start.body] = 1; n   while (!Q.empty()) { n     snake nowsnake = Q.front().first; n     int nowstep = Q.front().second; n     if (s[nowsnake.x][nowsnake.y] ==  @ ) return nowstep; n     Q.pop(); n     for (int i = 0; i < 4; i++) { n       int nextx = nowsnake.x + dx[i]; n       int nexty = nowsnake.y + dy[i]; n       if (in(nextx, nexty) && s[nextx][nexty] !=  #  && n           !collision(nextx, nexty, nowsnake)) { n         snake nextsnake; n         nextsnake.x = nextx; n         nextsnake.y = nexty; n         nextsnake.body = ((nowsnake.body << 2) & startbody) | i; n         if (!visit[nextsnake.x][nextsnake.y][nextsnake.body]) { n           visit[nextsnake.x][nextsnake.y][nextsnake.body] = true; n           Q.push({nextsnake, nowstep + 1}); n         } n       } n     } n   } n   return -1; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   length = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] !=  #  && s[i][j] !=  @  && s[i][j] !=  . ) { n         if (s[i][j] ==  1 ) { n           start.x = i; n           start.y = j; n         } n         if (s[i][j] -  0  > length) { n           backx = i; n           backy = j; n           length = s[i][j] -  0 ; n         } n       } n     } n   } n   startbody = (1 << ((length - 1) * 2)) - 1; n   status = 0; n   while (s[backx][backy] !=  1 ) { n     for (int i = 0; i < 4; i++) { n       int newbackx = backx + dx[i]; n       int newbacky = backy + dy[i]; n       if (in(newbackx, newbacky) && n           s[newbackx][newbacky] + 1 == s[backx][backy]) { n         status <<= 2; n         status |= i; n         backx = newbackx; n         backy = newbacky; n         break; n       } n     } n   } n   start.body = status; n   cout << BFS() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1e5 + 10, mod = 1e9 + 7, inf = 2e9; n struct re { n   int to, va; n   bool co; n }; n re nel; n int n, anch = 1, anzn = 1, cuq, si[sz], sq, cu, lg, rg, add; n vector<re> sv[sz]; n vector<pair<int, pair<int, int> > > pa[sz]; n pair<int, int> dot[2][sz * 8]; n pair<int, int> recalc(pair<int, int> l, pair<int, int> r) { n   pair<int, int> re; n   re.first = (1ll * l.first * r.first) % mod; n   re.second = l.second + r.second; n   return re; n } n void build(int nu, int l, int r) { n   dot[cu][nu].first = 1, dot[cu][nu].second = 0; n   if (l < r) { n     int mid = (l + r) / 2; n     build(nu * 2, l, mid); n     build(nu * 2 + 1, mid + 1, r); n   } n } n void cha(int nu, int l, int r) { n   if (l == r) { n     dot[cu][nu].first = (1ll * dot[cu][nu].first * add) % mod, n     dot[cu][nu].second++; n   } else { n     int mid = (l + r) / 2; n     if (lg <= mid) n       cha(nu * 2, l, mid); n     else n       cha(nu * 2 + 1, mid + 1, r); n     dot[cu][nu] = recalc(dot[cu][nu * 2], dot[cu][nu * 2 + 1]); n   } n } n pair<int, int> que(int nu, int l, int r) { n   if (l >= lg and r <= rg) n     return dot[cu][nu]; n   else if (l > rg or r < lg) n     return make_pair(1, 0); n   else { n     int mid = (l + r) / 2; n     return recalc(que(nu * 2, l, mid), que(nu * 2 + 1, mid + 1, r)); n   } n } n int pow(int x, int y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 1) n     return (1ll * pow(x, y - 1) * x) % mod; n   else { n     int t = pow(x, y / 2); n     return (1ll * t * t) % mod; n   } n } n void siz(int v, int pr) { n   si[v] = 1; n   for (int a = 0; a < sv[v].size(); a++) { n     int ne = sv[v][a].to; n     if (ne != pr) { n       siz(ne, v); n       si[v] += si[ne]; n     } n   } n } n int find_ce(int v, int pr) { n   for (int a = 0; a < sv[v].size(); a++) { n     int ne = sv[v][a].to; n     if (ne != pr and si[ne] > cuq / 2) return find_ce(ne, v); n   } n   if (pr != -1) si[pr] = cuq - si[v]; n   return v; n } n void dfs2(int v, int pr, int rq, int bq, int le) { n   pa[sq].push_back(make_pair(le, make_pair(rq, bq))); n   for (int a = 0; a < sv[v].size(); a++) { n     int ne = sv[v][a].to, r = 0, b = 0, nle = sv[v][a].va; n     if (sv[v][a].co == 0) n       r++; n     else n       b++; n     if (ne != pr) dfs2(ne, v, rq + r, bq + b, (1ll * le * nle) % mod); n   } n } n void dfs(int v, int q) { n   cuq = q; n   siz(v, -1); n   int ce = find_ce(v, -1); n   sq = 1; n   pa[0].push_back(make_pair(1, make_pair(0, 0))); n   for (int a = 0; a < sv[ce].size(); a++) { n     int ne = sv[ce][a].to, rq = 0, bq = 0, le = sv[ce][a].va; n     if (sv[ce][a].co == 0) n       rq = 1; n     else n       bq = 1; n     dfs2(ne, ce, rq, bq, le), sq++; n   } n   q++; n   cu = 0, build(1, 0, q * 2); n   cu = 1, build(1, 0, q * 2); n   for (int a = 0; a < sq; a++) { n     for (int b = 0; b < pa[a].size(); b++) { n       int le = pa[a][b].first, rq = pa[a][b].second.first, n           bq = pa[a][b].second.second; n       cu = 0, lg = 0, rg = -(bq - rq * 2) + q; n       int qq; n       pair<int, int> ans = que(1, 0, q * 2); n       qq = ans.second, anch = (1ll * anch * ans.first) % mod; n       cu = 1, lg = 0, rg = -(bq * 2 - rq) - 1 + q; n       ans = que(1, 0, q * 2); n       qq -= ans.second, anzn = (1ll * anzn * ans.first) % mod; n       anch = (1ll * anch * pow(le, qq)) % mod; n     } n     for (int b = 0; b < pa[a].size(); b++) { n       int le = pa[a][b].first, rq = pa[a][b].second.first, n           bq = pa[a][b].second.second; n       cu = 0, lg = (bq - rq * 2) + q, add = le; n       cha(1, 0, q * 2); n       cu = 1, lg = (bq * 2 - rq) + q, add = le; n       cha(1, 0, q * 2); n     } n     pa[a].clear(); n   } n   while (sv[ce].size() > 0) { n     int ne = sv[ce].back().to; n     for (int b = 0; b < sv[ne].size(); b++) n       if (sv[ne][b].to == ce) sv[ne].erase(sv[ne].begin() + b); n     sv[ce].pop_back(); n     dfs(ne, si[ne]); n   } n } n int main() { n   cin >> n; n   for (int a = 0; a < n - 1; a++) { n     int u, v, va, co; n     scanf( %d%d%d%d , &u, &v, &va, &co); n     u--, v--; n     nel.to = v, nel.va = va, nel.co = co; n     sv[u].push_back(nel); n     nel.to = u, sv[v].push_back(nel); n   } n   dfs(0, n); n   int ans = (1ll * anch * pow(anzn, mod - 2)) % mod; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int a[N], n, i, k, j, c, p, b[N], m, r, t, check, mr; n struct tree { n   int data = 0; n   int rank = 1; n } * ROOT[N], *f[N]; n vector<int> leafs; n void push(tree*& node, int number) { n   tree* child = new tree(); n   child->data = number; n   child->rank = node->rank + 1; n   f[number] = child; n   if (leafs.size()) { n     for (int it = leafs.size() - 1; it >= 0; it--) { n       if (leafs[it] == node->data) { n         leafs.erase(leafs.begin() + it); n         break; n       } n     } n   } n   leafs.push_back(number); n   c = max(c, child->rank); n }; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n; i++) { n     if (a[i] > m) { n       j++; n       ROOT[j] = new tree(); n       ROOT[j]->data = a[i]; n       m = a[i]; n       if (leafs.size()) leafs.clear(); n       continue; n     } n     if (a[i] > a[i - 1]) { n       if (!leafs.size()) { n         push(ROOT[j], a[i]); n         continue; n       } else { n         r = 0; n         mr = 0; n         check = 0; n         for (int it = leafs.size() - 1; it >= 0; it--) { n           if (f[leafs[it]]->rank < r) leafs.erase(leafs.begin() + it); n           if (mr <= f[leafs[it]]->rank) { n             if (leafs[it] < a[i]) { n               if (f[leafs[it]]->rank > r) { n                 r = f[leafs[it]]->rank; n                 t = leafs[it]; n                 check = 1; n               } n             } n           } n           mr = max(mr, f[leafs[it]]->rank); n         } n         if (check) n           push(f[t], a[i]); n         else n           push(ROOT[j], a[i]); n       } n     } else { n       c = max(c, 1); n     } n   } n   if (c) n     cout << c; n   else n     cout << 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = (long long)1e15; n const double EPS = 1e-11; n vector<vector<pair<long long, int> > > adj; n vector<vector<long long> > ar; n vector<vector<long long> > dep; n long long nxt(int node, long long time) { n   int idx = n       lower_bound(ar[node].begin(), ar[node].end(), time) - ar[node].begin(); n   if (idx >= (int)ar[node].size() || ar[node][idx] != time) return 0; n   return dep[node][idx] - time; n } n long long dij(int src, int d, int len) { n   priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                  greater<pair<long long, int> > > n       q; n   long long cst = nxt(src, 0); n   q.push(make_pair((cst), (src))); n   vector<long long> dest(len, OO); n   dest[src] = q.top().first; n   while (!q.empty()) { n     int n = q.top().second; n     long long c = q.top().first; n     q.pop(); n     if (c != dest[n]) continue; n     if (n == d) return c; n     for (int i = 0; i < (int)adj[n].size(); ++i) { n       int tn = adj[n][i].second; n       long long tc = adj[n][i].first; n       long long nc = c + tc + ((tn == d) ? 0 : nxt(tn, c + tc)); n       if (nc < dest[tn]) { n         dest[tn] = nc; n         q.push(make_pair((dest[tn]), (tn))); n       } n     } n   } n   return -1; n } n int main() { n   int n, m, a, b, k; n   cin >> n >> m; n   adj.clear(); n   adj.resize(n); n   long long c; n   for (int i = 0; i < m; ++i) { n     cin >> a >> b >> c; n     a--, b--; n     adj[a].push_back(make_pair((c), (b))); n     swap(a, b); n     adj[a].push_back(make_pair((c), (b))); n   } n   ar.clear(); n   dep.clear(); n   ar.resize(n); n   dep.resize(n); n   for (int i = 0; i < n; ++i) { n     cin >> k; n     if (k == 0) continue; n     ar[i].resize(k); n     dep[i].resize(k); n     for (int j = 0; j < k; ++j) { n       cin >> ar[i][j]; n     } n     dep[i][k - 1] = ar[i][k - 1] + 1; n     for (int j = k - 2; j >= 0; --j) { n       if (ar[i][j] + 1 != ar[i][j + 1]) n         dep[i][j] = ar[i][j] + 1; n       else n         dep[i][j] = dep[i][j + 1]; n     } n   } n   cout << dij(0, n - 1, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M = 1e9 + 7; n const long long N = 1e6 + 7; n long long a[N]; n long long n; n long long res = 0; n long long par[N]; n long long sz[N]; n long long findpar(long long v) { n   if (par[v] == v) return v; n   return par[v] = findpar(par[v]); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     par[i] = i; n     sz[i] = 1; n   } n   vector<pair<long long, pair<long long, long long> > > vec; n   for (long long i = 1; i < n; i++) { n     long long v, u; n     cin >> v >> u; n     vec.push_back({max(a[v], a[u]), {v, u}}); n   } n   sort(vec.begin(), vec.end()); n   for (auto pr : vec) { n     long long x = pr.first; n     long long v = findpar(pr.second.first); n     long long u = findpar(pr.second.second); n     res += sz[v] * sz[u] * x; n     par[v] = u; n     sz[u] += sz[v]; n   } n   for (long long i = 0; i < vec.size(); i++) n     vec[i].first = -1 * (min(a[vec[i].second.first], a[vec[i].second.second])); n   sort(vec.begin(), vec.end()); n   for (long long i = 1; i <= n; i++) { n     par[i] = i; n     sz[i] = 1; n   } n   for (auto pr : vec) { n     long long x = pr.first; n     long long v = findpar(pr.second.first); n     long long u = findpar(pr.second.second); n     res += sz[v] * sz[u] * x; n     par[v] = u; n     sz[u] += sz[v]; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n const int mod = 1e9 + 7; n const int inf = 1 << 30; n const int maxn = 505; n int n, m, k; n int cuantas[maxn], tomar[maxn][maxn], dp[maxn][maxn]; n vector<int> pos[maxn]; n int solve(int i, int f) { n   if (f > k) return inf; n   if (i > n) return 0; n   int& res = dp[i][f]; n   if (res != -1) return res; n   res = inf; n   for (int j = 0; j <= cuantas[i]; j++) { n     int faltar = cuantas[i] - j; n     if (f + faltar <= k) { n       res = min(res, solve(i + 1, f + faltar) + tomar[i][j]); n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < m; j++) { n       if (s[j] ==  0 ) continue; n       pos[i].push_back(j); n     } n     cuantas[i] = (int)pos[i].size(); n   } n   for (int i = 1; i <= n; i++) { n     for (int tam = 1; tam <= cuantas[i]; tam++) { n       tomar[i][tam] = inf; n       for (int j = 0; j < cuantas[i]; j++) { n         if (j + tam - 1 >= cuantas[i]) break; n         tomar[i][tam] = min(tomar[i][tam], pos[i][j + tam - 1] - pos[i][j] + 1); n       } n     } n   } n   memset(dp, -1, sizeof(dp)); n   cout << solve(1, 0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int i, j, k, l, n; n   cout.precision(10); n   { n     cin >> n >> k; n     if (n <= k / 2) n       cout <<  YES  n            <<   n ; n     else { n       cout <<  NO  n            <<   n ; n       return 0; n     } n     for (i = 0; i < n - 1; i++) { n       cout <<  1  ; n     } n     cout << k - (n - 1) <<   n ; n     cout << k / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int C[200005], A[200005], n, k, i, j, mx; n int main() { n   scanf( %d%d%d , &n, &i, &k); n   for (i = 1; i <= n; i++) scanf( %d , &A[i]); n   for (i = j = 1; i <= n; i++) { n     while (j - i + 1 - C[A[i]] - (A[j] == A[i]) <= k && j <= n) { n       C[A[j]]++; n       j++; n     } n     if (j - i - C[A[i]]-- <= n) mx = max(mx, C[A[i]] + 1); n   } n   printf( %d , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long n, a = 1, b = 1, mod = 1000000007, pp = 1000000005; n unsigned long long fp(unsigned long long n, unsigned long long p) { n   if (p == 0) return 1; n   if (p == 1) return n; n   unsigned long long res; n   res = fp(n, p / 2) % mod; n   res = (res * res) % mod; n   if (p % 2) res = (res * n) % mod; n   return res % mod; n } n long long solve(long long n, long long m) { n   unsigned long long a = 1, b = 1, res; n   for (int i = 1; i <= m; i++) { n     a *= n; n     a %= mod; n     n--; n     b *= i; n     b %= mod; n   } n   res = (a * fp(b, pp) % mod) % mod; n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   ios_base::sync_with_stdio(0); n   ; n   cin >> n; n   unsigned long long ans = solve(2 * n, n); n   cout << (ans % mod - n) % mod <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isrange(int second, int first, int n, int m) { n   if (0 <= second && second < n && 0 <= first && first < m) return true; n   return false; n } n int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1}, n     ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1}; n const int MAX = 101010; n long long int a[6], arr[MAX]; n vector<pair<long long int, int> > v; n vector<int> tmp; n int num[MAX], start = 1; n long long int seg[MAX * 4]; n multiset<long long int> s[MAX]; n long long int getn(int l, int r) { n   l += start; n   r += start; n   long long int ans = 0; n   while (l <= r) { n     if (l % 2) { n       ans = max(ans, seg[l]); n       l++; n     } n     if ((r % 2) == 0) { n       ans = max(ans, seg[r]); n       r--; n     } n     l /= 2; n     r /= 2; n   } n   return ans; n } n void upd(int first, long long int second) { n   first += start; n   seg[first] = second; n   for (int e = (first / 2); e >= 1; e /= 2) n     seg[e] = max(seg[e * 2], seg[e * 2 + 1]); n } n void del(int first) { n   long long int second = *s[first].begin(); n   s[first].erase(s[first].lower_bound(second)); n   second = *s[first].begin(); n   upd(first, second); n } n int main(void) { n   while (start < MAX) start *= 2; n   for (int e = 0; e < 6; e++) scanf( %lld , &a[e]); n   int n; n   scanf( %d , &n); n   for (int e = 0; e < n; e++) { n     long long int r; n     scanf( %lld , &r); n     for (int p = 0; p < 6; p++) { n       v.push_back(make_pair(r - a[p], e)); n       s[e].insert(r - a[p]); n     } n   } n   if (n == 1) return !printf( 0 ); n   for (int e = 0; e < n; e++) { n     long long int first = *s[e].begin(); n     upd(e, first); n   } n   sort(v.begin(), v.end()); n   memset(num, 0, sizeof(num)); n   long long int ans = 1e18, lx = 0; n   for (int e = 0; e < (int)v.size(); e++) { n     if (lx != v[e].first) { n       bool suc = false; n       for (int p = 0; p < (int)tmp.size(); p++) { n         num[tmp[p]]++; n         if (num[tmp[p]] == 6) { n           suc = true; n           break; n         } n         del(tmp[p]); n       } n       if (suc) break; n       tmp.clear(); n     } n     long long int vv = 0; n     if (0 <= v[e].second - 1) vv = max(vv, getn(0, v[e].second - 1)); n     if (v[e].second + 1 < n) vv = max(vv, getn(v[e].second + 1, n - 1)); n     ans = min(ans, vv - v[e].first); n     tmp.push_back(v[e].second); n     lx = v[e].first; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int len = 0, x, pre; n   long long ans = 0LL; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     if (i != 0) { n       if (pre == x) { n         len++; n       } else { n         ans += 1LL * (len + 1) * len / 2; n         len = 1; n       } n     } else n       len = 1; n     pre = x; n   } n   ans += 1LL * (len + 1) * len / 2; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(int u, int p[], bool mark[]) { n   mark[u] = true; n   if (mark[p[u]]) return; n   dfs(p[u], p, mark); n } n int main() { n   int result = 0, cycle = 0; n   int n; n   cin >> n; n   int p[n + 1], b[n + 1], f = 0; n   bool mark[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     mark[i] = false; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     f += b[i]; n   } n   if (f % 2 == 0) result++; n   for (int i = 1; i <= n; i++) { n     if (!mark[i]) { n       dfs(i, p, mark); n       cycle++; n     } n   } n   if (cycle > 1) result += cycle; n   cout << result << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool upmin(T &x, T y) { n   return y < x ? x = y, 1 : 0; n } n template <typename T> n inline bool upmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n const long double eps = 1e-15; n const long double pi = acos(-1); n const long long oo = 1 << 30; n const long long loo = 1ll << 62; n const long long mods = 1e9 + 7; n const long long MAXN = 100005; n const long long INF = 0x7fffffff; n inline long long read() { n   long long f = 1, x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n double mx[MAXN], mn[MAXN], smx[MAXN], smn[MAXN], sp[MAXN], sq[MAXN]; n signed main() { n   long long n = read(); n   for (long long i = 1; i <= n; i++) scanf( %lf , &mx[i]); n   for (long long i = 1; i <= n; i++) scanf( %lf , &mn[i]); n   for (long long i = 1; i <= n; i++) smx[i] = smx[i - 1] + mx[i]; n   for (long long i = n; i >= 1; i--) smn[i] = smn[i + 1] + mn[i]; n   for (long long i = 1; i <= n; i++) { n     double pl = 1 + smx[i] - smn[i + 1], mul = smx[i], n            mi = sqrt(pl * pl - mul * 4 + eps); n     sp[i] = (pl + mi) * 0.5, sq[i] = (pl - mi) * 0.5; n   } n   for (long long i = 1; i <= n; i++) printf( %.10lf  , sp[i] - sp[i - 1]); n   puts(  ); n   for (long long i = 1; i <= n; i++) printf( %.10lf  , sq[i] - sq[i - 1]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first == b.first) { n     return a.second < b.second; n   } n   return a.first > b.first; n } n int main() { n   string s; n   int n, m; n   cin >> n >> m; n   cin >> s; n   char ara[m + 1], ara1[m + 1]; n   for (int i = 0; i < m; i++) { n     char x, y; n     cin >> x >> y; n     ara[i] = x; n     ara1[i] = y; n   } n   char str[27], str1[27]; n   int j = 0; n   for (char ch =  a ; ch <=  z ; ch++) { n     char ch1; n     ch1 = ch; n     int f = 0; n     for (int i = 0; i < m; i++) { n       if (ara[i] == ch1) { n         str[j] = ara1[i]; n         ch1 = ara1[i]; n         f = 1; n       } else if (ara1[i] == ch1) { n         str[j] = ara[i]; n         ch1 = ara[i]; n         f = 1; n       } n     } n     if (f == 1) { n       str1[j] = ch; n       j++; n     } n     f = 0; n   } n   str[j] =   0 ; n   str1[j] =   0 ; n   string st = (string)str; n   string st1 = (string)str1; n   int l1 = st1.size(); n   for (int j = 0; j < n; j++) { n     for (int i = 0; i < l1; i++) { n       if (s[j] == st1[i]) { n         s[j] = st[i]; n         break; n       } n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int x[100000], y[100000], xx[100000], yy[100000]; n int ord[100000]; n vector<int> inds; n inline bool cmpx(const int &lhs, const int &rhs) { return x[lhs] < x[rhs]; } n inline bool cmpy(const int &lhs, const int &rhs) { return y[lhs] < y[rhs]; } n vector<int> xs, ys; n void sortUnique(vector<int> &inds) { n   sort(inds.begin(), inds.end()); n   inds.erase(unique(inds.begin(), inds.end()), inds.end()); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < (int)(n); ++i) n     scanf( %d%d%d%d , x + i, y + i, xx + i, yy + i), x[i] += xx[i], n         y[i] += yy[i]; n   for (int i = 0; i < (int)(n); ++i) ord[i] = i; n   sort(ord, ord + n, cmpx); n   for (int i = 0; i < (int)(k + 1); ++i) inds.push_back(ord[i]); n   for (int i = 0; i < (int)(k + 1); ++i) inds.push_back(ord[n - 1 - i]); n   for (int i = 0; i < (int)(n); ++i) ord[i] = i; n   sort(ord, ord + n, cmpy); n   for (int i = 0; i < (int)(k + 1); ++i) inds.push_back(ord[i]); n   for (int i = 0; i < (int)(k + 1); ++i) inds.push_back(ord[n - 1 - i]); n   sortUnique(inds); n   for (int ind : inds) xs.push_back(x[ind]), ys.push_back(y[ind]); n   sortUnique(xs); n   sortUnique(ys); n   int xsz = (int)xs.size(); n   int ysz = (int)ys.size(); n   long long ans = 2e18; n   for (int i = 0; i < (int)(xsz); ++i) n     for (int j = i; j < xsz; ++j) { n       int cnt = j - i + 1; n       if (xsz - cnt > k) continue; n       int xbeg = xs[i], xed = xs[j]; n       for (int ii = 0; ii < (int)(ysz); ++ii) n         for (int jj = ii; jj < ysz; ++jj) { n           cnt = jj - ii + 1; n           if (ysz - cnt > k) continue; n           int ybeg = ys[ii], yed = ys[jj]; n           int bad = 0; n           for (int ind : inds) n             if (x[ind] < xbeg || x[ind] > xed || y[ind] < ybeg || n                 y[ind] > yed) { n               ++bad; n             } n           if (bad <= k) { n             long long dx = ((long long)xed - xbeg + 1) / 2; n             long long dy = ((long long)yed - ybeg + 1) / 2; n             if (dx <= 0) dx = 1; n             if (dy <= 0) dy = 1; n             ans = min(ans, (long long)dx * dy); n           } n         } n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int main() { n   int n, k; n   cin >> n >> k; n   cout << k * (6 * (n - 1) + 5) << endl; n   for (int i = 1; i <= n; i++) { n     printf( %d %d %d %d n , k * (6 * (i - 1) + 1), k * (6 * (i - 1) + 2), n            k * (6 * (i - 1) + 3), k * (6 * (i - 1) + 5)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9; n const long long MOD = 1e9 + 7; n const int MAXN = 705; n const int bignumlen = 17; n const int Blen = 8; n const long long base = 100000000; n struct bignum { n   int len; n   long long data[bignumlen]; n   long long &operator[](int x) { return (data[x]); } n   const long long &operator[](int x) const { return (data[x]); } n   bignum() { n     memset(data, 0, sizeof(data)); n     len = 0; n   } n   void clear() { n     for (int i = len; i >= 1; --i) data[i] = 0; n     len = 0; n   } n   int check(const bignum &a, const bignum &b) { n     if (a.len > b.len) return (0); n     if (b.len > a.len) return (1); n     for (int i = a.len; i >= 1; --i) { n       if (a.data[i] < b.data[i]) return (1); n       if (b.data[i] < a.data[i]) return (0); n     } n     return 2; n   } n   bool operator<(const bignum &b) { return (check(*this, b) == 1); } n   bool operator>(const bignum &b) { return (check(*this, b) == 0); } n   bool operator<=(const bignum &b) { return (check(*this, b) >= 1); } n   bool operator>=(const bignum &b) { return (check(*this, b) % 2 == 0); } n   bool operator!=(const bignum &b) { return (check(*this, b) != 2); } n   bool operator==(const bignum &b) { return (check(*this, b) == 2); } n   bignum operator=(const bignum &x) { n     for (int i = x.len + 1; i <= len; ++i) data[i] = 0; n     for (int i = 1; i <= x.len; ++i) data[i] = x.data[i]; n     len = x.len; n     return *this; n   } n   bignum operator=(long long x) { n     for (int i = len; i >= 0; --i) data[i] = 0; n     len = 0; n     while (x) { n       data[++len] = x % base; n       x /= base; n     } n     return *this; n   } n   bignum(long long x) { n     memset(data, 0, sizeof(data)); n     len = 0; n     (*this) = x; n   } n   bignum operator*(const bignum &b) { n     int i, j; n     bignum tmp; n     for (i = 1; i <= len; ++i) n       if (data[i] != 0) n         for (j = 1; j <= b.len; ++j) n           if (b.data[j] != 0) { n             tmp.data[i + j - 1] += data[i] * b.data[j]; n             tmp.data[i + j] += tmp.data[i + j - 1] / base; n             tmp.data[i + j - 1] %= base; n           } n     tmp.len = len + b.len - 1; n     while (tmp.data[tmp.len + 1]) tmp.len++; n     return tmp; n   } n   bignum operator*(long long x) { n     int i; n     bignum tmp; n     for (i = 1; i <= len; ++i) tmp[i] = data[i] * x; n     tmp.len = len; n     for (i = 1; i <= len; ++i) { n       tmp[i + 1] += tmp[i] / base, tmp[i] %= base; n       if (tmp[i + 1] && i + 1 > tmp.len) tmp.len++; n     } n     return tmp; n   } n   bignum operator/(long long x) { n     int i; n     bignum tmp; n     long long y = 0; n     for (i = len; i >= 1; --i) { n       y = y * base + data[i]; n       tmp[i] = y / x; n       y %= x; n     } n     tmp.len = len; n     while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--; n     return tmp; n   } n   bignum operator/(const bignum &b) { n     if (b.len <= 1 && b[1] == 0) { n       printf( error! ?0?! ); n       for (;;) n         ; n     } n     int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen; n     long long x = data[len], y = b[b.len]; n     while (x) x /= 10, l1++; n     while (y) y /= 10, l2++; n     bignum tmp, chu, B; n     chu = *this; n     B = b; n     for (i = 1; i * Blen <= l1 - l2; ++i) B *= base; n     for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10; n     for (i = l1 - l2; i >= 0; --i) { n       x = 0; n       while (chu >= B) chu -= B, x++; n       tmp[i / Blen + 1] = tmp[i / Blen + 1] * 10 + x; n       B /= 10; n     } n     tmp.len = (l1 - l2) / Blen + 1; n     while (tmp.len >= 1 && !tmp[tmp.len]) tmp.len--; n     return tmp; n   } n   bignum operator+(const bignum &b) { n     bignum tmp; n     int i, l = max(len, b.len); n     for (i = 1; i <= l; ++i) tmp[i] = data[i] + b[i]; n     for (i = 1; i <= l; ++i) tmp[i + 1] += tmp[i] / base, tmp[i] %= base; n     tmp.len = l; n     if (tmp[tmp.len + 1]) tmp.len++; n     return tmp; n   } n   bignum operator+(long long x) { n     bignum tmp; n     tmp = *this; n     tmp[1] += x; n     for (int i = 1; i <= len && tmp[i] >= base; ++i) n       tmp[i + 1] += tmp[i] / base, tmp[i] %= base; n     while (tmp[tmp.len + 1]) tmp.len++; n     return tmp; n   } n   bignum operator-(const bignum &b) { n     int i; n     bignum tmp; n     for (i = 1; i <= len; ++i) tmp.data[i] = data[i] - b.data[i]; n     for (i = 1; i <= len; ++i) { n       if (tmp[i] < 0) tmp.data[i] += base, tmp.data[i + 1]--; n     } n     tmp.len = len; n     while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--; n     return tmp; n   } n   bignum operator-(long long x) { n     bignum tmp; n     tmp = *this; n     tmp[1] -= x; n     for (int i = 1; i <= len && tmp[i] < 0; ++i) { n       tmp[i + 1] += (tmp[i] + 1) / base - 1; n       tmp[i] = (tmp[i] + 1) % base + base - 1; n     } n     while (!tmp[tmp.len] && tmp.len > 1) tmp.len--; n     return tmp; n   } n   long long operator%(long long x) { n     int i; n     long long y = 0; n     for (i = len; i >= 1; --i) y = (y * base + data[i]) % x; n     return y; n   } n   bignum operator%(const bignum &b) { n     if (b.len <= 1 && b[1] == 0) { n       printf( error! ?0 mod! ); n       for (;;) n         ; n     } n     int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen; n     long long x = data[len], y = b[b.len]; n     while (x) x /= 10, l1++; n     while (y) y /= 10, l2++; n     bignum chu, B; n     chu = *this; n     B = b; n     for (i = 1; i * Blen <= l1 - l2; ++i) B *= base; n     for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10; n     for (i = l1 - l2; i >= 0; --i) { n       while (chu >= B) chu -= B; n       B /= 10; n     } n     return chu; n   } n   bignum operator+=(const bignum &b) { return *this = (*this + b); } n   bignum operator*=(const bignum &b) { return *this = (*this * b); } n   bignum operator-=(const bignum &b) { return *this = (*this - b); } n   bignum operator/=(const bignum &b) { return *this = (*this / b); } n   bignum operator%=(const bignum &b) { return *this = (*this % b); } n   bignum operator*=(long long x) { return (*this = (*this * x)); } n   bignum operator+=(long long x) { return (*this = (*this + x)); } n   bignum operator-=(long long x) { return (*this = (*this - x)); } n   bignum operator/=(long long x) { return (*this = (*this / x)); } n   void read() { n     char c[bignumlen * Blen + 10]; n     scanf( %s , c + 1); n     int l = strlen(c + 1); n     (*this).clear(); n     long long x; n     for (int i = 1; i <= (l - 1) / Blen + 1; ++i) { n       x = 0; n       for (int j = l - Blen * i + 1; j <= l - Blen * i + Blen; ++j) n         if (j >= 1) x = x * 10 + c[j] - 48; n       data[++len] = x; n     } n   } n   void write() { n     printf( %I64d , data[len]); n     for (int i = len - 1; i >= 1; --i) printf( %0*I64d , Blen, data[i]); n   } n }; n int n; n vector<bignum> DP[MAXN]; n vector<int> v[MAXN]; n int sz[MAXN]; n int dfs(int now, int p) { n   DP[now].push_back(0); n   DP[now].push_back(1); n   sz[now] = 1; n   for (int ea = 0; ea < v[now].size(); ea++) { n     int sek = v[now][ea]; n     if (sek == p) continue; n     sz[now] += dfs(sek, now); n     vector<bignum> ans(MAXN); n     for (int j = 0; j < MAXN; j++) ans[j] = -1; n     for (int i = 1; i <= DP[now].size() - 1; i++) { n       for (int j = 1; j <= DP[sek].size() - 1; j++) { n         bignum tmp = DP[now][i] / i * DP[sek][j] / j * (i + j); n         if (tmp > ans[i + j]) ans[i + j] = tmp; n         tmp = DP[now][i] * DP[sek][j]; n         if (tmp > ans[i]) ans[i] = tmp; n       } n     } n     ans[0] = 0; n     DP[now].clear(); n     for (int j = 0; j < MAXN; j++) { n       if (ans[j] == -1) break; n       DP[now].push_back(ans[j]); n     } n   } n   return sz[now]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   dfs(0, -1); n   bignum res = DP[0][1]; n   for (int i = 2; i <= DP[0].size() - 1; i++) n     if (DP[0][i] > res) res = DP[0][i]; n   res.write(); n } n


#include <bits/stdc++.h> n using namespace std; n class fenwickTree { n  public: n   int* tree; n   int size; n   int prefixSum(int index) { n     index++; n     int ret = 0; n     while (index > 0) { n       ret += tree[index]; n       index -= (index & -index); n     } n     return ret; n   } n   void update(int index, int delta) { n     index++; n     while (index <= size) { n       tree[index] += delta; n       index += (index & -index); n     } n   } n   int rangeSum(int left, int right) { n     return prefixSum(right) - prefixSum(left - 1); n   } n   fenwickTree(int n) { n     size = n; n     tree = new int[size + 1]; n     memset(tree, 0, sizeof(int) * (size + 1)); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     fenwickTree tree(n + 1); n     int cts[n + 1]; n     memset(cts, 0, sizeof cts); n     long long invert = 0; n     bool ok = false; n     for (int i = 0; i < n; i++) { n       int w; n       cin >> w; n       cts[w]++; n       if (cts[w] == 2) ok = true; n       tree.update(w, 1); n       invert += tree.rangeSum(w + 1, n); n     } n     if (invert & 1 && !ok) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n template <class p, class q> n void umin(p& a, const q& b) { n   if (a > b) a = b; n } n template <class p, class q> n void umax(p& a, const q& b) { n   if (a < b) a = b; n } n using ll = long long; n using VI = std::vector<int>; n using VLL = std::vector<ll>; n using namespace std; n using namespace std::chrono; n int n; n int const N = 1e5 + 3; n ll const P = 1e9 + 7; n int a[N]; n int l[N]; n int prime[N]; n int phi[N]; n int mult[N]; n int ppower[N]; n ll ans; n void sieve() { n   int p = 2; n   prime[1] = 0; n   phi[1] = 1; n   while (p < N) { n     ppower[p] = 1; n     if (prime[p]) { n       int cp = p; n       while (cp % prime[p] == 0) { n         cp /= prime[p]; n         mult[p]++; n         ppower[p] *= prime[p]; n       } n       phi[p] = (mult[p] >= 2) ? prime[p] * phi[p / prime[p]] n                               : (prime[p] - 1) * phi[p / prime[p]]; n       p++; n       continue; n     } n     prime[p] = p; n     ppower[p] = p; n     mult[p] = 1; n     phi[p] = p - 1; n     for (ll i = 1LL * p * p; i < N; i += p) { n       if (!prime[i]) prime[i] = p; n     } n     p++; n   } n } n void write_divisors(int r, VI& output) { n   if (r == 1) n     output = {1}; n   else { n     VI partial; n     write_divisors(r / ppower[r], partial); n     int prw = 1; n     for (int i = (0); i <= (mult[r]); i++) { n       for (const auto& u : partial) { n         output.push_back(prw * u); n       } n       prw *= prime[r]; n     } n   } n } n VI divisors[N]; n void uzup() { n   for (int i = (1); i <= (N - 1); i++) write_divisors(i, divisors[i]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = (1); i <= (n); i++) cin >> a[i]; n   sieve(); n   uzup(); n   for (int j = (1); j <= (n); j++) { n     VI monitor; n     for (int k = j; k <= n; k += j) { n       for (const auto& u : divisors[a[k]]) { n         monitor.push_back(u); n         ans = (ans + 1LL * phi[u] * phi[j] * (2 * l[u] + 1)) % P; n         l[u]++; n       } n     } n     for (int i = (((int)((monitor).size())) - 1); i >= (0); i--) { n       l[monitor[i]]--; n       monitor.pop_back(); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   long long fh = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= fh; n } n template <typename T> n void write(T x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void writeln(T x) { n   write(x); n   puts(  ); n } n long long qpow(long long a, long long b) { n   if (b == -1) return 0; n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % 998244353; n     a = a * a % 998244353; n     b >>= 1; n   } n   return res; n } n long long n; n long long a[1000005], ans; n signed main() { n   read(n); n   for (long long i = 1; i <= n; ++i) read(a[i]); n   for (long long i = 1; i <= n; ++i) { n     ans += a[i] * qpow(2, n - i) % 998244353; n     ans %= 998244353; n     ans += a[i] * qpow(2, n - i - 1) % 998244353 * (n - i) % 998244353; n     ans %= 998244353; n   } n   writeln(ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int input, max = 0, temp1, temp2; n   scanf( %d , &input); n   for (int i = 1; i <= input; i++) { n     if (input % i == 0) { n       temp1 = input / i; n       temp2 = i; n       if (temp1 <= temp2) { n         break; n       } n     } n   } n   printf( %d %d n , temp1, temp2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxSize = 1 << 16; n map<string, int> t; n int a[maxSize]; n vector<string> s; n vector<vector<int> > v; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   v.resize(n); n   s.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     t[s[i]] = i; n   } n   for (int i = 0; i < k; i++) { n     string s1, s2; n     cin >> s1 >> s2; n     int x, y; n     x = t[s1], y = t[s2]; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   int res = 0, ans = 0; n   for (int i = 0; i < maxSize; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) n       if (i & (1 << j)) cnt++; n     for (int j = 0; j < n; j++) n       if (i & (1 << j)) n         for (int x = 0; x < v[j].size(); x++) n           if (i & (1 << v[j][x])) { n             cnt = 0; n             break; n           } n     if (cnt > ans) res = i, ans = cnt; n   } n   printf( %d n , ans); n   set<string> st; n   for (int i = 0; i < n; i++) n     if (res & (1 << i)) st.insert(s[i]); n   for (auto e : st) cout << e << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200500; n int n; n pair<int, int> a[MAXN]; n int b[MAXN]; n int s[MAXN]; n int FindMax(int j) { n   int ret = 0; n   for (; j >= 0; (j &= (j + 1))--) { n     ret = max(ret, s[j]); n   } n   return ret; n } n void Update(int j, int x) { n   for (; j < n; j |= (j + 1)) { n     s[j] = max(s[j], x); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &a[i].first, &a[i].second); n     b[i] = a[i].first + a[i].second; n   } n   sort(a, a + n); n   sort(b, b + n); n   int res = 1; n   for (int i = 0; i < n; ++i) { n     int p = lower_bound(b, b + n, a[i].first - a[i].second + 1) - b - 1; n     int curVal = FindMax(p) + 1; n     Update(lower_bound(b, b + n, a[i].first + a[i].second) - b, curVal); n     res = max(res, curVal); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int toInt(string s) { n   int v; n   istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n int n; n vector<pair<int, int> > v; n long long DP[3001][3001]; n long long solve(int idx, int lst) { n   if (idx == n) return 0; n   long long &ret = DP[idx][lst]; n   if (ret != -1) return ret; n   ret = (1LL << 59); n   ret = min(ret, solve(idx + 1, lst) + v[idx].first - v[lst].first); n   ret = min(ret, solve(idx + 1, idx) + v[idx].second); n   return ret; n } n int main() { n   scanf( %d , &n); n   int a, b; n   for (int i = 0; i < n; ++i) { n     scanf( %d %d , &a, &b); n     v.push_back(pair<int, int>(a, b)); n   } n   memset(DP, -1, sizeof(DP)); n   sort(v.begin(), v.end()); n   cout << solve(1, 0) + 1LL * v[0].second; n } n


#include <bits/stdc++.h> n using namespace std; n int row[1010]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   memset(row, 0x3f3f3f3f, sizeof row); n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     int r, c; n     scanf( %d %d , &r, &c); n     row[r] = min(c, row[r]); n   } n   for (int i = 1; i <= m; ++i) ans += ((row[i] == 0x3f3f3f3f) ? 0 : row[i]); n   printf( %lld n , min(ans, (long long)k)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 2e5 + 5; n int dist[N]; n vector<int> open[N], close[N]; n int n, k; n bool done[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (auto i = 0; i < (long long)(n); i++) { n     int l, r; n     cin >> l >> r; n     open[l].push_back(i); n     close[r + 1].push_back(i); n     dist[i] = r + 1; n   } n   set<int> pool; n   set<pair<int, int> > dist_idx; n   vector<int> res; n   for (auto i = 1; i < (long long)(N); i++) { n     for (auto j : close[i]) { n       if (done[j]) continue; n       pool.erase(j); n       dist_idx.erase({dist[j], j}); n     } n     for (auto j : open[i]) { n       pool.insert(j); n       dist_idx.insert({dist[j], j}); n     } n     while (((int)((pool).size())) > k) { n       auto it = dist_idx.end(); n       it--; n       int idx = (*it).second; n       res.push_back(idx + 1); n       dist_idx.erase(it); n       pool.erase(idx); n       done[idx] = true; n     } n   } n   cout << ((int)((res).size())) <<   n ; n   for (auto i : res) cout << i <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[50][50], n, m; n bool can(int k) { n   int total = 0; n   for (int i = 0; i < m; i++) n     if (a[k][i] != i + 1) total++; n   if (total <= 2) return 1; n   return 0; n } n bool ok() { n   for (int i = 1; i <= n; i++) n     if (!can(i)) return 0; n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n   if (ok()) { n     printf( YES n ); n     return 0; n   } n   for (int i = 0; i < m - 1; i++) n     for (int j = i + 1; j < m; j++) { n       for (int k = 1; k <= n; k++) swap(a[k][i], a[k][j]); n       if (ok()) { n         printf( YES n ); n         return 0; n       } n       for (int k = 1; k <= n; k++) swap(a[k][i], a[k][j]); n     } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x, y, i; n   cin >> n >> m >> x >> y; n   cout << x <<     << y << endl; n   int a = x; n   for (i = 1; i < m + 1; i++) { n     if (i != y) { n       cout << x <<     << i << endl; n     } n   } n   n++; n   int cnt = 0; n   while (n-- && n > 0) { n     if (n != x) { n       cnt++; n       if (cnt % 2) { n         for (int i = m; i > 0; i--) { n           cout << n <<     << i << endl; n         } n       } else { n         for (i = 1; i < m + 1; i++) { n           cout << n <<     << i << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n ostream& operator<<(std::ostream& os, const std::map<T1, T2>& v) { n   os <<  [ ; n   for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) n     os << ii->first <<  :  << ii->second <<    ; n   return os <<  ] ; n } n template <class Cntnr> n ostream& c_out(std::ostream& os, const Cntnr& v) { n   os <<  [ ; n   for (typename Cntnr::const_iterator ii = v.begin(); ii != v.end(); ++ii) n     os << *ii <<    ; n   os <<   ] ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& o, const vector<T>& c) { n   return c_out(o, c); n } n template <typename T> n ostream& operator<<(ostream& o, const list<T>& c) { n   return c_out(o, c); n } n template <typename T> n ostream& operator<<(ostream& o, const set<T>& c) { n   return c_out(o, c); n } n int main() { n   int n, a, b; n   scanf( %d , &n); n   scanf( %d , &a); n   scanf( %d , &b); n   for (int i = (0); i < (int)(n); i++) { n     long long w; n     cin >> w; n     cout << w - ((((w * a) / b) * b + a - 1) / a) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int com[] = {1, 4, 6, 8, 9}; n   long long int t; n   cin >> t; n   while (t--) { n     int64_t k; n     cin >> k; n     string n; n     cin >> n; n     map<int, int> m; n     for (int i = 0; i < n.size(); i++) { n       m[n[i] -  0 ]++; n     } n     int flag = 1; n     for (auto it : m) { n       if (it.first == 1 || it.first == 4 || it.first == 6 || it.first == 8 || n           it.first == 9) { n         flag = 0; n         cout << 1 << endl << it.first << endl; n         break; n       } n     } n     if (flag) { n       for (auto it : m) { n         if (it.second > 1) { n           cout << 2 << endl << it.first << it.first << endl; n           flag = 0; n           break; n         } n       } n       if (n ==  32  || n ==  35 ) { n         flag = 0; n         cout << 2 << endl << n << endl; n       } n     } n     if (flag) { n       stack<int> s; n       string temp =   ; n       for (int i = 0; i < n.size(); i++) { n         if (n[i] !=  3 ) { n           temp += n[i]; n         } n         if (temp.size() == 2) { n           break; n         } n       } n       cout << 2 << endl << temp << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n double DP[2][2510][2510]; n double X[2510], Y[2510]; n double dis(int a, int b) { n   return sqrt((X[a] - X[b]) * (X[a] - X[b]) + (Y[a] - Y[b]) * (Y[a] - Y[b])); n } n int main() { n   ios::sync_with_stdio(false); n   memset(DP, 0, sizeof(DP)); n   int N; n   cin >> N; n   for (int i = 0; i < N; i++) cin >> X[i] >> Y[i]; n   for (int i = 2; i <= N; i++) { n     for (int s = 0; s < N; s++) { n       int e = s + i - 2; n       e %= N; n       int t; n       t = (s - 1 + N) % N; n       DP[0][t][e] = max(DP[0][t][e], DP[0][s][e] + dis(s, t)); n       t = (e + 1) % N; n       DP[1][s][t] = max(DP[1][s][t], DP[0][s][e] + dis(s, t)); n       t = (s - 1 + N) % N; n       DP[0][t][e] = max(DP[0][t][e], DP[1][s][e] + dis(e, t)); n       t = (e + 1) % N; n       DP[1][s][t] = max(DP[1][s][t], DP[1][s][e] + dis(e, t)); n     } n   } n   double res = 0; n   for (int i = 0; i < N; i++) { n     int j = (i - 1 + N) % N; n     res = max(res, DP[0][i][j]); n     res = max(res, DP[1][i][j]); n   } n   cout.precision(20); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class X, class Y> n void minimize(X &x, const Y &y) { n   if (x > y) x = y; n } n template <class X, class Y> n void maximize(X &x, const Y &y) { n   if (x < y) x = y; n } n template <class T> n T Abs(const T &x) { n   return (x < 0 ? -x : x); n } n const int mod = (int)1e9 + 7; n int cntWay[500500 * 2], sumWay[500500 * 2]; n int frac[500500], finv[500500]; n int inverse(int x) { n   int res = 1; n   int mul = x; n   int k = mod - 2; n   while (k > 0) { n     if (k & 1) res = 1LL * res * mul % mod; n     mul = 1LL * mul * mul % mod; n     k >>= 1; n   } n   return (res); n } n void precalc(void) { n   frac[0] = finv[0] = 1; n   for (int i = (1), _b = (500500 - 1); i <= _b; i = i + 1) n     frac[i] = 1LL * frac[i - 1] * i % mod; n   for (int i = (1), _b = (500500 - 1); i <= _b; i = i + 1) n     finv[i] = inverse(frac[i]); n } n int comb(int k, int n) { n   if (k > n) return (0); n   return (1LL * frac[n] * finv[k] % mod * finv[n - k] % mod); n } n int countWay(int number, int sum, int absLim, int numBig) { n   sum -= numBig * absLim; n   sum += (number - numBig) * (absLim + 1); n   return (comb(number - 1, sum - 1)); n } n int countWay(int number, int sum, int absLim) { n   int res = 0; n   for (int numBig = 0, _n = (number + 1); numBig < _n; numBig = numBig + 1) { n     int numSet = comb(numBig, number); n     int numWay = countWay(number, sum, absLim, numBig); n     if (numBig % 2 == 0) n       res = (res + 1LL * numSet * numWay) % mod; n     else n       res = (res - 1LL * numSet * numWay % mod + mod) % mod; n   } n   return (res); n } n void process(void) { n   int a, b, range, n; n   cin >> a >> b >> range >> n; n   for (int i = (-range * n), _b = (range * n); i <= _b; i = i + 1) n     cntWay[i + 500500] = countWay(n, i, range); n   for (int i = (range * n), _a = (-range * n); i >= _a; i = i - 1) n     sumWay[i + 500500] = (sumWay[i + 1 + 500500] + cntWay[i + 500500]) % mod; n   int res = 0; n   for (int i = (-range * n), _b = (range * n); i <= _b; i = i + 1) { n     int reqScore = b + i + 1 - a; n     if (reqScore > range * n) continue; n     maximize(reqScore, -range * n); n     int numWayB = cntWay[i + 500500]; n     int numWayA = sumWay[reqScore + 500500]; n     res = (res + 1LL * numWayA * numWayB) % mod; n   } n   printf( %d n , res); n } n int main(void) { n   precalc(); n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x7f7f7f7f7f7f7f7f; n struct sta { n   int s; n   long long k, val; n } p[105]; n int n, m; n long long b, f[(1 << 20) + 5]; n bool cmp(sta a, sta b) { return a.k < b.k; } n void init() { n   memset(f, -1, sizeof f); n   scanf( %d%d , &n, &m); n   cin >> b; n   int t, a; n   for (int i = 0; i < n; i++) { n     scanf( %lld%lld%d , &p[i].val, &p[i].k, &t); n     p[i].s = 0; n     for (int j = 0; j < t; j++) { n       scanf( %d , &a); n       p[i].s |= (1 << (a - 1)); n     } n   } n   sort(p, p + n, cmp); n } n long long solve() { n   f[0] = 0; n   int t = (1 << m) - 1; n   long long ans = inf; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= t; j++) { n       if (f[j] == -1) continue; n       int u = p[i].s | j; n       if (f[u] == -1) n         f[u] = p[i].val + f[j]; n       else n         f[u] = min(f[u], p[i].val + f[j]); n     } n     if (f[t] != -1) ans = min(ans, f[t] + p[i].k * b); n   } n   return ans == inf ? -1 : ans; n } n int main() { n   init(); n   long long res = solve(); n   cout << res << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool bo[1005][1005]; n double ans[1005][1005]; n double f(int n, int m) { n   if (m == 0) return 1; n   if (bo[n][m]) return ans[n][m]; n   if (n == 0) { n     return 1.0 / (m + 1); n   } n   double a = 1.0, b = 1 - f(m, n - 1); n   double c = 1.0 * m / (m + 1) * (1 - f(m - 1, n)), d = 1.0 / (m + 1) + c; n   double p = (d - c) / (a - c + d - b); n   bo[n][m] = true; n   return ans[n][m] = p * a + (1 - p) * c; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   double x = f(n, m); n   printf( %.10f %.10f , x, 1 - x); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, w, a[1123], d, mx, mn; n int main() { n   cin >> n >> w; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     d += a[i]; n     mx = max(d, mx); n     mn = min(d, mn); n   } n   cout << max(w + 1 - mx + mn, 0ll); n } n


#include <bits/stdc++.h> n char str[100005]; n int main(void) { n   int n, m; n   int i; n   scanf( %d%d , &n, &m); n   scanf( %s , str); n   for (i = 0; i < n && m; i++) { n     if ((!(i & 1)) && str[i] ==  4 ) { n       if ((str[i + 1] ==  4  && str[i + 2] ==  7 )) n         break; n       else if (str[i + 1] ==  7  && str[i + 2] ==  7 ) n         break; n       else if (str[i + 1] ==  7 ) { n         str[i + 1] =  4 ; n         m--; n       } n     } else if ((i & 1) && str[i] ==  4  && str[i + 1] ==  7 ) { n       str[i] =  7 ; n       m--; n     } n   } n   if (m & 1) { n     if (str[i + 1] ==  4 ) n       str[i + 1] =  7 ; n     else n       str[i + 1] =  4 ; n   } n   printf( %s n , str); n   return 0; n } n