#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n vector<int> tree[N]; n int cnt[N]; n int n, mx1, mx2; n bool centr[N], ans[N]; n int dfs(int v, int p = -1) { n   int u; n   cnt[v] = 1; n   for (int i = 0; i < tree[v].size(); i++) { n     u = tree[v][i]; n     if (u == p) continue; n     cnt[v] += dfs(u, v); n   } n   return cnt[v]; n } n void find_centr(int v, int p = -1) { n   int u; n   bool ok = (n - cnt[v]) <= n / 2; n   for (int i = 0; i < tree[v].size(); i++) { n     u = tree[v][i]; n     if (u == p) continue; n     find_centr(u, v); n     if (cnt[u] > n / 2) ok = false; n   } n   if (ok) centr[v] = 1; n   return; n } n void calc(int v, int p = -1, int mxC = -1) { n   int u; n   if (n - cnt[v] - mxC <= n / 2) n     ans[v] = true; n   else n     return; n   for (int i = 0; i < tree[v].size(); i++) { n     u = tree[v][i]; n     if (u == p) continue; n     calc(u, v, mxC); n   } n   return; n } n void solveC(int aim) { n   for (int i = 1; i <= n; i++) cnt[i] = 0; n   dfs(aim); n   mx1 = -1, mx2 = -1; n   int u = 0; n   for (int i = 0; i < tree[aim].size(); i++) { n     u = tree[aim][i]; n     if (cnt[u] > mx1) { n       mx2 = mx1; n       mx1 = cnt[u]; n     } else if (cnt[u] > mx2) n       mx2 = cnt[u]; n   } n   for (int i = 0; i < tree[aim].size(); i++) { n     u = tree[aim][i]; n     if (cnt[u] == mx1 && (cnt[u] != n / 2 || n / 2 * 2 != n)) n       calc(u, aim, mx2); n     else n       calc(u, aim, mx1); n   } n } n void solve() { n   cin >> n; n   int b, e; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d %d n , &b, &e); n     tree[b].push_back(e); n     tree[e].push_back(b); n   } n   dfs(1); n   find_centr(1); n   for (int i = 1; i <= n; i++) { n     if (centr[i]) solveC(i); n   } n   for (int i = 1; i <= n; i++) n     if (centr[i] || ans[i]) n       printf( 1  ); n     else n       printf( 0  ); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long c; n vector<int> a; n int N; n long long C[65][65]; n long long nCk(long long n, long long k) { n   if (k > n) return 0; n   return C[n][k]; n } n long long f(int index, int onesNeeded, int tight) { n   if (index == N) { n     if (onesNeeded == 0) n       return 1; n     else n       return 0; n   } n   if (a[index] == 0) { n     return f(index + 1, onesNeeded, tight); n   } else { n     long long ans = nCk(N - index - 1, onesNeeded); n     if (onesNeeded >= 1) ans += f(index + 1, onesNeeded - 1, tight); n     return ans; n   } n } n int brute(int n) { n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int t = i, cnt = 0; n     while (t) { n       if (t % 2 == 1) cnt++; n       t /= 2; n     } n     if (cnt == 5) ans++; n   } n   return ans; n } n int main() { n   for (int i = 0; i <= 60; i++) C[i][0] = 1, C[i][i] = 1; n   for (int n = 1; n <= 60; n++) { n     for (int r = 1; r <= n; r++) { n       C[n][r] = C[n - 1][r] + C[n - 1][r - 1]; n     } n   } n   long long n, t; n   cin >> n >> t; n   n++; n   int ok = 0; n   if ((t & (t - 1)) == 0) ok = 1; n   while (t) { n     t /= 2; n     c++; n   }; n   if (!ok) { n     cout << 0 << endl; n     return 0; n   } n   while (n) { n     a.push_back(n % 2); n     n /= 2; n   } n   reverse(a.begin(), a.end()); n   N = a.size(); n   long long ans1 = f(0, c, 1); n   if (c == 1) ans1--; n   cout << ans1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, ans, f[200005]; n char s[11]; n int find(int k) { n   if (k == f[k]) return k; n   return f[k] = find(f[k]); n } n void add(int x, int y) { n   if (find(x) != find(y)) f[find(x)] = find(y); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= 2 * n + 3; i++) f[i] = i; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%s , &x, &y, s); n     x -= y; n     y = min(x + 2 * y, 2 * n - x - 2 * y + 2); n     x = max(x, -x); n     if (s[0] ==  x ) { n       add(2 * y, 2 * x); n       add(2 * y + 1, 2 * x + 1); n     } else { n       add(2 * y + 1, 2 * x); n       add(2 * x + 1, 2 * y); n     } n   } n   for (int i = 0; i <= n + 1; i++) n     if (find(2 * i) == find(2 * i + 1)) { n       printf( 0 ); n       return 0; n     } n   int s = 0; n   for (int i = 0; i <= 2 * n + 3; i++) s += (find(i) == i); n   s = s / 2 - 2; n   ans = 1; n   for (int i = 1; i <= s; i++) ans = ans * 2 % 1000000007; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n const int INF = 2e9; n const long long LNF = 9e18; n const int mod = 1e9 + 7; n const int maxn = 2e3 + 10; n int n, m, k; n bool val[maxn], pos[maxn]; n long long C[maxn][maxn], A[maxn]; n void init() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (x != -1) val[x] = pos[i] = 1; n   } n   k = m = 0; n   for (int i = 1; i <= n; i++) { n     if (!pos[i]) m++; n     if (!pos[i] && !val[i]) k++; n   } n   A[0] = 1; n   C[0][0] = 1; n   for (int i = 1; i <= m; i++) { n     A[i] = (1LL * i * A[i - 1]) % mod; n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; n   } n } n void solve() { n   long long ans = A[m]; n   for (int i = 1; i <= k; i++) { n     long long tmp = (1LL * C[k][i] * A[m - i]) % mod; n     ans -= (i & 1) ? tmp : -tmp; n     ans = (ans + mod) % mod; n   } n   cout << ans << endl; n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char ara[100][100]; n int fr[100], fc[100]; n vector<int> r, c; n int pr; n void row(int j); n void col(int j) { n   fc[j] = 1; n   c.push_back(j); n   for (int i = 0; i < n; i++) n     if (ara[i][j] ==  # ) n       if (fr[i] == 0) row(i); n } n void row(int i) { n   fr[i] = 1; n   r.push_back(i); n   for (int j = 0; j < m; j++) n     if (ara[i][j] ==  # ) n       if (fc[j] == 0) col(j); n } n int check() { n   for (int i = 0; i < r.size(); i++) { n     for (int j = 0; j < c.size(); j++) { n       if (ara[r[i]][c[j]] ==  . ) return 1; n     } n   } n   return 0; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> ara[i][j]; n     } n   } n   for (int j = 0; j < m; j++) { n     if (fc[j] == 0) { n       pr++; n       c.clear(); n       r.clear(); n       col(j); n       if (check()) { n         cout <<  No n ; n         return 0; n       } n     } n   } n   cout <<  Yes n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n string to_string(const pair<T, T>& pr) { n   return  (  + to_string(pr.first) +  ,  + to_string(pr.second) +  ) ; n } n template <typename T> n string to_string(const vector<T>& vc, int w) { n   if (vc.empty()) return   ; n   if (w + 1 == vc.size()) return to_string(vc[w]); n   return to_string(vc[w]) +  ,  + to_string(vc, w + 1); n } n template <typename T> n string to_string(const vector<T>& vc) { n   return  {  + to_string(vc, 0) +  } ; 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 class DebugStream { n } LOG; n template <typename T> n DebugStream& operator<<(DebugStream& s, const T&) { n   return s; n } n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n const int maxN = 5.5e3 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, n           bs = 29; n int cap[maxN][maxN], ff[maxN][maxN], n, m, k, c, d, taim; n vector<int> adj[maxN]; n int id(int t, int nd) { return t * n + nd + 1; } n void addEdge(int a, int b, int cp) { n   if (!ff[a][b]) { n     adj[a].push_back(b); n     adj[b].push_back(a); n     cap[a][b] = cp; n   } n   ff[a][b]++; n   ff[b][a]++; n } n inline bool hasCap(int a, int b) { n   if (!a || !b || a % n == b % n) return cap[a][b] > 0; n   if (a > b) return cap[b][a] < 0; n   return true; n } n int cln(int a, int b) { return (a + b - 1) / b; } n inline int getCost(int a, int b) { n   if (!a || !b) return 0; n   if (a % n == b % n) { n     if ((a - 1) % n == 0) return 0; n     return (a < b ? c : -c); n   } n   if (a > b) return (-cln(abs(cap[b][a]), ff[b][a]) * 2 + 1) * d - c; n   return (abs(cap[a][b]) / ff[a][b] * 2 + 1) * d + c; n } n const int INF = 1e9; n void shortest_paths(vector<int>& d, vector<int>& p) { n   d.assign(taim * n + 1, INF); n   d[0] = 0; n   vector<bool> inq(taim * n + 1, false); n   queue<int> q; n   q.push(0); n   p.assign(taim * n + 1, -1); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     inq[u] = false; n     for (int v : adj[u]) { n       if (hasCap(u, v) && d[v] > d[u] + getCost(u, v)) { n         d[v] = d[u] + getCost(u, v); n         p[v] = u; n         if (!inq[v]) { n           inq[v] = true; n           q.push(v); n         } n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k >> c >> d; n   taim = 101; n   vector<int> frq(n); n   for (int i = 0, inp; i < k; i++) { n     cin >> inp; n     inp--; n     frq[inp]++; n   } n   for (int i = 0; i < n; i++) n     if (frq[i]) addEdge(0, id(0, i), frq[i]); n   for (int t = 0; t + 1 < taim; t++) n     for (int j = 0; j < n; j++) addEdge(id(t, j), id(t + 1, j), k); n   for (int i = 0, u, v; i < m; i++) { n     cin >> u >> v; n     u--, v--; n     for (int t = 0; t + 1 < taim; t++) { n       addEdge(id(t, u), id(t + 1, v), 0); n       addEdge(id(t, v), id(t + 1, u), 0); n     } n   } n   vector<int> d, p; n   int ans = 0, trg = id(taim - 1, 0); n   42; n   for (int u = 0; u < k; u++) { n     shortest_paths(d, p); n     assert(d[trg] != INF); n     ans += d[trg]; n     for (int cur = trg; cur != 0; cur = p[cur]) { n       cap[p[cur]][cur]--; n       cap[cur][p[cur]]++; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int f = 1, mod; n void calc(int sum, int t) { n   for (int i = 1; i <= sum; i++) { n     if (i % 2 or !t) n       f = (f * i) % mod; n     else n       f = (f * (i / 2)) % mod, t--; n   } n } n int main() { n   int n, p, count = 0, a[200001] = {0}; n   cin >> n; n   vector<pair<int, int> > v; n   for (int i = 1; i <= n; i++) cin >> p, v.push_back(make_pair(p, i)); n   for (int i = 1; i <= n; i++) cin >> p, v.push_back(make_pair(p, i)); n   sort(v.begin(), v.end()); n   int i = 0; n   cin >> mod; n   while (i < v.size()) { n     int t = 0, sum = 0; n     count++; n     a[v[i].second] = count; n     i++; n     while (i < 2 * n and v[i].first == v[i - 1].first) { n       if (a[v[i].second] == count) t++; n       a[v[i].second] = count; n       sum++; n       i++; n     } n     calc(sum + 1, t); n   } n   cout << f; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 999999999999999999; n const double PI = acos(-1.0); n void stop() { exit(0); } n long long A[200005]; n int main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> A[i]; n   } n   long long res = 1; n   long long curr = 1; n   for (long long i = 0; i < n - 1; i++) { n     if (A[i] * 2 >= A[i + 1]) { n       curr++; n     } else { n       if (curr > res) res = curr; n       curr = 1; n     } n   } n   if (curr > res) res = curr; n   cout << res; n   stop(); n } n


#include <bits/stdc++.h> n using namespace std; n int abs(int x) { n   if (x < 0) return -x; n   return x; n } n int n, m; n int x[500], y[500], l[500]; n bool used[500]; n int placed[500]; n int dist(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; n   for (int i = 0; i < m; i++) cin >> l[i]; n   if (2 * m + 1 < n || n % 2 == 1) { n     cout <<  NO  << endl; n     return 0; n   } n   bool good = true; n   for (int k = 0; k < 2; k++) { n     if (k == 1 && good) break; n     good = true; n     memset(used, 0, 500 * sizeof(used[0])); n     for (int i = 0; i < n; i++) placed[i] = -2; n     for (int i = k; i < n; i += 2) { n       int len = dist(i, (i - 1 + n) % n) + dist(i, (i + 1) % n); n       bool match = false; n       for (int j = 0; j < m; j++) { n         if (!used[j] && l[j] == len) { n           used[j] = 1; n           placed[i] = j; n           match = true; n           break; n         } n       } n       if (!match) { n         good = false; n         break; n       } n     } n   } n   if (!good) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     cout << placed[0] + 1; n     for (int i = 1; i < n; i++) cout <<     << placed[i] + 1; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, sum = 0, s[100005], ans[100005], an = 0; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &s[i]); n     sum += s[i]; n   } n   if (sum % k) { n     printf( No ); n     return 0; n   } n   int m = sum / k; n   int now = 0, len = 0; n   for (int i = 1; i <= n; i++) { n     if (now + s[i] > m) { n       printf( No ); n       return 0; n     } n     now += s[i]; n     len++; n     if (now == m) { n       ans[++an] = len; n       now = len = 0; n     } n   } n   printf( Yes n ); n   for (int i = 1; i <= k; i++) printf( %d  , ans[i]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int MOD = 1e9 + 7; n long long int INF = 1e18; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, j, k = 0, m = 0, n, p = 0, l, r, ans = 0, cnt = 0, n                       mini = INF, maxi = INF, x, y, a, b, c, low, mid, high, h, n                       q, k1, l1, t; n   cin >> n >> m; n   long long int A[m][n + 1]; n   long long int B[m][n + 1]; n   for (j = 0; j < m; j++) { n     for (i = 1; i <= n; i++) { n       cin >> k; n       A[j][i] = k; n       B[j][k] = i; n     } n   } n   ans = n; n   h = 1; n   for (i = 1; i < n; i++) { n     j = 0; n     l = A[j][i]; n     r = A[j][i + 1]; n     p = 0; n     for (j = 1; j < m; j++) { n       if ((B[j][r] - B[j][l]) != 1) { n         h = 1; n         p = 1; n         break; n       } n     } n     if (p == 0) { n       h++; n       ans = ans + (h - 1); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200009; n const long long MOD = 119 << 23 | 1; n class { n  public: n   bool g[1 << 22]; n   int a[1 << 22]; n   int N, M, n, m; n   void solve() { n     int _; n     cin >> _; n     while (_--) { n       cin >> n >> m; n       N = (1 << n) - 1; n       M = (1 << m) - 1; n       for (int i = 1; i <= N; ++i) cin >> a[i]; n       for (int i = M + 1; i <= N; ++i) g[i] = 1; n       for (int i = M; i; --i) { n         if (a[i << 1] < a[i << 1 | 1]) { n           g[i] = g[i << 1 | 1]; n         } else { n           g[i] = g[i << 1]; n         } n       } n       vector<int> ans; n       int rt = 1; n       for (int i = 0; i < N - M; ++i) { n         while (!g[rt]) rt++; n         ans.push_back(rt); n         down(rt); n       } n       long long sum = 0; n       for (int i = 1; i <= M; ++i) sum += a[i]; n       cout << sum <<   n ; n       for (int i : ans) cout << i <<    ; n       cout <<   n ; n       for (int i = 1; i <= N; ++i) g[i] = a[i] = 0; n     } n   } n   void down(int rt) { n     int lson = rt << 1, rson = rt << 1 | 1; n     if (a[lson] == 0 && a[rson] == 0) { n       a[rt] = 0; n       g[rt] = 0; n       return; n     } n     if (a[lson] > a[rson]) { n       a[rt] = a[lson]; n       down(lson); n     } else { n       a[rt] = a[rson]; n       down(rson); n     } n     if (rt <= M) { n       if (a[lson] > a[rson]) n         g[rt] = g[lson]; n       else n         g[rt] = g[rson]; n     } n   } n } NSPACE; n int main() { n   ; n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   NSPACE.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200000]; n bool st[200000][2]; n long long memo[200000][2]; n long long solve(int node, int rev) { n   if (node < 0 || node >= n) return 0; n   if (st[node][rev]) return 0x3f3f3f3f3f3f3f3fll; n   if (node == 0) return 0; n   long long& val = memo[node][rev]; n   if (val != 0xc0c0c0c0c0c0c0c0ll) return val; n   st[node][rev] = true; n   val = solve(node + a[node] * (1 - 2 * rev), 1 - rev); n   if (val != 0x3f3f3f3f3f3f3f3fll) val += a[node]; n   st[node][rev] = false; n   return val; n } n int main() { n   ios::sync_with_stdio(0); n   memset(st, false, sizeof st); n   memset(memo, 0xc0c0c0c0c0c0c0c0ll, sizeof memo); n   cin >> n; n   for (int i = 1; i < n; ++i) cin >> a[i]; n   for (int i = 1; i < n; ++i) { n     st[0][0] = true; n     long long ans = solve(i, 1); n     if (ans == 0x3f3f3f3f3f3f3f3fll) n       ans = -1; n     else n       ans += i; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[500005], n, d; n long long int ans = 0; n void func(long long int st, long long int ed) { n   if (st + 1 == ed) return; n   if (a[st] == a[ed]) { n     ans = max(ans, (ed - st) / 2); n     for (int i = st; i <= ed; ++i) { n       a[i] = a[st]; n     } n   } else { n     ans = max(ans, (ed - st) / 2); n     for (int i = st + 1; i <= (st + ed) / 2; ++i) { n       a[i] = a[st]; n     } n     for (int i = (st + ed) / 2 + 1; i <= ed; ++i) { n       a[i] = a[ed]; n     } n   } n } n int main(int argc, char const *argv[]) { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   long long int start, endx; n   a[0] = a[1]; n   start = 1; n   endx = n; n   for (int i = start; i < endx; ++i) { n     if (a[i] == a[i + 1]) { n       func(start, i); n       start = i + 1; n     } n   } n   func(start, endx); n   printf( %lld n , ans); n   for (int i = 1; i <= n; ++i) { n     cout << a[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   set<string> qq; n   string s, q, g[100000]; n   int h, j = -1; n   while (cin >> s) { n     h = 3; n     q =   ; n     q = q + s[0] + s[1] + s[2]; n     while (h != s.length()) { n       while (s[h] !=      && s[h] !=  . ) { n         q += s[h]; n         h++; n       } n       q +=  / ; n       if (s[h] !=  . ) { n         j++; n         g[j] = q; n       } else n         break; n       h++; n     } n   } n   j++; n   sort(g, g + j); n   int p = 1, max = 1; n   qq.insert(g[0]); n   for (int i = 1; i < j; i++) { n     if (g[i] == g[i - 1]) n       p++; n     else n       p = 1; n     if (p > max) max = p; n     qq.insert(g[i]); n   } n   set<string>::iterator it; n   string w =   , t =   ; n   int r = 0, max2 = 0; n   for (it = qq.begin(); it != qq.end(); it++) { n     if (w ==   ) n       w = *it; n     else { n       t =   ; n       p = 1; n       for (int i = 0; i < w.length(); i++) { n         if (i == (*it).length()) { n           p = 0; n           break; n         } n         t += (*it)[i]; n       } n       if (t != w) p = 0; n       if (p == 0) { n         w = *it; n         r = 0; n       } else { n         r++; n       } n       if (r > max2) max2 = r; n     } n   } n   cout << max2 <<     << max; n   return 0; n } n


#include <bits/stdc++.h> n int m(int a) { n   int ret = 0, i = 1; n   while (a > 0) { n     if (a % 10 == 4 || a % 10 == 7) { n       ret += (i * (a % 10)), i *= 10; n     } n     a /= 10; n   } n   return ret; n } n int main() { n   int a, b; n   while (scanf( %d%d , &a, &b) != EOF) { n     while (1) { n       a++; n       if (m(a) == b) { n         printf( %d n , a); n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     int cnt = 0; n     reverse(s.begin(), s.end()); n     cnt += (s[0] -  0 ); n     for (int i = 1; i < n; i++) { n       if (s[i] !=  0 ) cnt = cnt + 1 + (s[i] -  0 ); n     } n     cout << cnt <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 5e5; n vector<pair<long long, long long>> g[maxn]; n long long v1 = 0; n long long v2 = 0; n vector<pair<pair<long long, long long>, long long>> t1(maxn * 4); n vector<pair<pair<long long, long long>, long long>> t2(maxn * 4); n vector<long long> pp1; n vector<pair<pair<long long, long long>, long long>> op1(maxn); n vector<long long> pp2; n vector<pair<pair<long long, long long>, long long>> op2(maxn); n long long n; n long long dist[maxn]; n void push(vector<pair<pair<long long, long long>, long long>> &t, long long v) { n   if (t[v].second == 1) { n     t[v].second = 0; n     t[v * 2].second ^= 1; n     swap(t[v * 2].first.first, t[v * 2].first.second); n     t[v * 2 + 1].second ^= 1; n     swap(t[v * 2 + 1].first.first, t[v * 2 + 1].first.second); n   } n } n void upd(vector<pair<pair<long long, long long>, long long>> &t, long long v, n          long long tl, long long tr, long long pos, long long dist, n          long long T) { n   if (tl == tr) { n     if (T == 0) { n       t[v].first.first = dist; n       t[v].first.second = 0; n     } else { n       t[v].first.first = 0; n       t[v].first.second = dist; n     } n     return; n   } n   long long tm = (tl + tr) / 2; n   if (pos <= tm) { n     upd(t, v * 2, tl, tm, pos, dist, T); n   } else { n     upd(t, v * 2 + 1, tm + 1, tr, pos, dist, T); n   } n   t[v].first.first = max(t[v * 2].first.first, t[v * 2 + 1].first.first); n   t[v].first.second = max(t[v * 2].first.second, t[v * 2 + 1].first.second); n } n void update(vector<pair<pair<long long, long long>, long long>> &t, long long v, n             long long tl, long long tr, long long l, long long r) { n   if (tl > r || tr < l) return; n   if (l <= tl && tr <= r) { n     swap(t[v].first.first, t[v].first.second); n     t[v].second ^= 1; n     return; n   } n   push(t, v); n   long long tm = (tl + tr) / 2; n   update(t, v * 2, tl, tm, l, r); n   update(t, v * 2 + 1, tm + 1, tr, l, r); n   t[v].first.first = max(t[v * 2].first.first, t[v * 2 + 1].first.first); n   t[v].first.second = max(t[v * 2].first.second, t[v * 2 + 1].first.second); n } n long long get(vector<pair<pair<long long, long long>, long long>> &t, n               long long v, long long tl, long long tr, long long l, n               long long r) { n   if (tl > r || tr < l) return 0; n   if (l <= tl && tr <= r) { n     if (t[v].second == 1) { n       return t[v].first.second; n     } else n       return t[v].first.first; n   } n   push(t, v); n   long long tm = (tl + tr) / 2; n   return max(get(t, v * 2, tl, tm, l, r), get(t, v * 2 + 1, tm + 1, tr, l, r)); n } n void dfs(vector<pair<pair<long long, long long>, long long>> &t, n          vector<long long> &pp, n          vector<pair<pair<long long, long long>, long long>> &op, long long v, n          long long p, long long sum, long long H) { n   upd(t, 1, 0, n - 1, pp.size(), H, sum % 2); n   op[v].first.first = pp.size(); n   op[v].second = H; n   pp.push_back(v); n   for (long long i = 0; i < g[v].size(); ++i) { n     long long to = g[v][i].first; n     if (to == p) continue; n     dfs(t, pp, op, to, v, sum + g[v][i].second, H + 1); n   } n   op[v].first.second = pp.size() - 1; n } n void jupaS(long long v, long long p) { n   for (long long i = 0; i < g[v].size(); ++i) { n     long long to = g[v][i].first; n     if (to == p) continue; n     dist[to] = dist[v] + 1; n     jupaS(to, v); n   } n } n signed main() { n   cin.tie(); n   cout.tie(); n   ios_base::sync_with_stdio(false); n   cin >> n; n   vector<pair<long long, long long>> y; n   for (long long i = 0; i < n - 1; ++i) { n     long long a, b, t; n     cin >> a >> b >> t; n     a--; n     b--; n     y.push_back({a, b}); n     g[a].push_back({b, t}); n     g[b].push_back({a, t}); n   } n   jupaS(0, -1); n   long long mx = 0; n   for (long long k = 0; k < n; ++k) { n     if (dist[mx] < dist[k]) mx = k; n   } n   v1 = mx; n   dist[v1] = 0; n   jupaS(v1, -1); n   mx = 0; n   for (long long k = 0; k < n; ++k) { n     if (dist[mx] < dist[k]) mx = k; n   } n   v2 = mx; n   dfs(t1, pp1, op1, v1, -1, 0, 0); n   dfs(t2, pp2, op2, v2, -1, 0, 0); n   long long q; n   cin >> q; n   for (long long j = 0; j < q; ++j) { n     long long id; n     cin >> id; n     id--; n     long long a = y[id].first; n     long long b = y[id].second; n     if (op1[a].second > op1[b].second) swap(a, b); n     update(t1, 1, 0, n - 1, op1[b].first.first, op1[b].first.second); n     if (op2[a].second > op2[b].second) swap(a, b); n     update(t2, 1, 0, n - 1, op2[b].first.first, op2[b].first.second); n     long long mx = get(t1, 1, 0, n - 1, 0, n - 1); n     long long mx1 = get(t2, 1, 0, n - 1, 0, n - 1); n     cout << max(mx, mx1) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   long long int n, sum = 0, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     sum = sum + (x / k); n     if (x % k != 0) sum++; n   } n   cout << (sum + 1) / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> tr; n int S(int n) { n   int ans = 1; n   while (ans < n) ans *= 2; n   return ans; n } n int k; n void upd(int v) { n   v += k; n   tr[v].first--; n   for (; v > 1; v >>= 1) { n     tr[v >> 1] = max(tr[v], tr[v ^ 1]); n   } n } n template <typename T> n inline T getint() { n   T val = 0; n   char c; n   bool neg = false; n   while ((c = getchar()) && !(c >=  0  && c <=  9 )) { n     neg |= c ==  - ; n   } n   do { n     val = (val * 10) + c -  0 ; n   } while ((c = getchar()) && (c >=  0  && c <=  9 )); n   return val * (neg ? -1 : 1); n } n inline void writeInt(int x) { n   if (x < 0) putc( - , stdout), x = -x; n   char s[20]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) putc(s[n], stdout); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int x, y; n     cin >> x >> y; n     vector<vector<int>> pos(n + 2); n     vector<int> b(n); n     tr.assign(10 * n, {0, 0}); n     for (int i = 0; i < n; i++) { n       b[i] = getint<int>(); n       pos[b[i]].push_back(i); n     } n     int zn; n     for (int i = 1; i < n + 2; i++) { n       if (pos[i].empty()) { n         zn = i; n         break; n       } n     } n     k = S(n + 1); n     for (int i = 1; i < n + 2; i++) { n       tr[k + i - 1] = {pos[i].size(), i}; n     } n     for (int i = k - 1; i > 0; i--) { n       tr[i] = max(tr[i * 2], tr[i * 2 + 1]); n     } n     vector<int> ans(n + 2); n     int kol = x; n     vector<int> h(n + 2); n     vector<int> st; n     while (kol--) { n       ans[tr[1].second]++; n       if (!h[tr[1].second]) st.push_back(tr[1].second); n       h[tr[1].second]++; n       upd(tr[1].second - 1); n     } n     kol = n - y; n     int last = 0; n     for (int i = 1; i <= kol; i++) { n       if (2 * tr[1].first <= n - x - last) { n         break; n       } n       ans[tr[1].second]++; n       if (!h[tr[1].second]) st.push_back(tr[1].second); n       h[tr[1].second]++; n       upd(tr[1].second - 1); n       last = i; n     } n     if (tr[1].first * 2 > n - x - last) { n       cout <<  NO n ; n       continue; n     } n     vector<int> a(n); n     for (auto& ss : st) { n       for (int i = 0; i < ans[ss]; i++) { n         if (x) { n           a[pos[ss][i]] = ss; n           --x; n         } else { n           a[pos[ss][i]] = zn; n         } n       } n     } n     vector<vector<int>> npos(n + 2); n     h.assign(n + 2, 0); n     vector<int> s; n     vector<int> cp; n     for (int i = 0; i < n; i++) { n       if (!a[i]) { n         npos[b[i]].push_back(i); n         cp.push_back(i); n         h[b[i]]++; n         if (h[b[i]] == 1) s.push_back(b[i]); n       } n     } n     for (int i = 0; i < s.size(); i++) { n       if (npos[s[i]].empty()) continue; n       int uk = min(i + 1, (int)s.size() - 1); n       for (int j = 0; j < npos[s[i]].size(); j++) { n         if (npos[s[uk]].empty()) ++uk; n         if (uk > s.size() - 1) break; n         int p2 = npos[s[uk]].back(); n         a[npos[s[i]][j]] = b[p2]; n         a[p2] = b[npos[s[i]][j]]; n         npos[s[uk]].pop_back(); n       } n     } n     int col; n     int uk = -1; n     vector<int> cp2; n     h.assign(n + 2, 0); n     for (int i = 0; i < cp.size(); i++) { n       if (!a[cp[i]] || a[cp[i]] == b[cp[i]]) { n         h[cp[i]]++; n         cp2.push_back(cp[i]); n         a[cp[i]] = b[cp[i]]; n         if (uk == -1) { n           col = a[cp[i]]; n           uk = i; n         } n       } n     } n     if (uk != -1) { n       int i = 0; n       for (int j = 0; j < cp2.size(); j++) { n         for (;; i++) { n           if (a[cp[i]] != col && !h[cp[i]] && b[cp[i]] != col) { n             swap(a[cp[i]], a[cp2[j]]); n             break; n           } n         } n       } n     } n     kol -= last; n     for (int i = 0; i < cp.size(); i++) { n       if (kol) { n         a[cp[i]] = zn; n         --kol; n       } else n         break; n     } n     cout <<  Yes n ; n     for (int i = 0; i < a.size(); i++) { n       writeInt(a[i]); n       cout <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c; n   while (~scanf( %d%d%d , &a, &b, &c)) { n     int max = 999999999; n     int a1 = (c + b) + a; n     max = max < a1 ? max : a1; n     int a2 = 2 * (c + b); n     max = max < a2 ? max : a2; n     int b1 = (c + a) * 2; n     max = max < b1 ? max : b1; n     int b2 = (b + a) * 2; n     max = max < b2 ? max : b2; n     printf( %d n , max); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n const int MAXM = 100005; n long long res[MAXM]; n vector<int> seq[MAXN]; n vector<pair<int, int> > qry[MAXN]; n int L[MAXN], A[MAXM], H[MAXM]; n int n, m; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 2; i <= n; i++) cin >> L[i]; n   for (int i = 1; i <= m; i++) cin >> A[i] >> H[i]; n   for (int i = 1; i <= m; i++) { n     int a = A[i], h = H[i]; n     qry[a].push_back(make_pair(h, i)); n     while (a > 1 && h > 0) { n       if ((a ^ 1) <= n && h - L[a] - L[a ^ 1] > 0) n         qry[a ^ 1].push_back(make_pair(h - L[a] - L[a ^ 1], i)); n       if (h - L[a] > 0) res[i] += h - L[a]; n       h -= L[a], a >>= 1; n     } n   } n   for (int i = n; i >= 1; i--) { n     int x = i * 2, y = i * 2 + 1; n     seq[i].push_back(0); n     if (x <= n && y <= n) { n       for (int j = 0; j < seq[x].size(); j++) n         seq[i].push_back(seq[x][j] + L[x]); n       for (int j = 0; j < seq[y].size(); j++) n         seq[i].push_back(seq[y][j] + L[y]); n       seq[x].clear(), seq[y].clear(); n       sort(seq[i].begin(), seq[i].end()); n     } else if (x <= n) { n       for (int j = 0; j < seq[x].size(); j++) n         seq[i].push_back(seq[x][j] + L[x]); n       seq[x].clear(); n     } n     sort(qry[i].begin(), qry[i].end()); n     long long sum = 0; n     int p = 0; n     for (int j = 0; j < seq[i].size(); j++) { n       sum += seq[i][j]; n       while (p != qry[i].size() && n              (j + 1 == seq[i].size() || seq[i][j + 1] > qry[i][p].first)) { n         res[qry[i][p].second] += 1ll * (j + 1) * qry[i][p].first - sum; n         p++; n       } n     } n   } n   for (int i = 1; i <= m; i++) cout << res[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int n; n long long r, a[(1LL << 20)], x, y; n int main() { n   scanf( %d %I64d , &n, &r); n   long long sum = 0; n   double qpw = 1; n   for (int i = 1; i <= n; ++i) qpw *= 2; n   for (int i = 1; i <= qpw; ++i) { n     scanf( %I64d , &a[i]); n     sum += a[i]; n   } n   printf( %.6f n , sum * 1.0 / qpw); n   double ans = 0; n   while (r--) { n     scanf( %I64d %I64d , &x, &y); n     sum += (y - a[x + 1]); n     a[x + 1] = y; n     ans = sum * 1.0 / qpw; n     printf( %.6f n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 500010; n vector<vector<long long int> > ch, ar; n void multiply(vector<vector<long long int> > &a, n               vector<vector<long long int> > &b) { n   vector<vector<long long int> > c; n   c.resize(a.size(), vector<long long int>(b[0].size(), 0)); n   for (long long int i = 0; i < a.size(); ++i) { n     for (long long int j = 0; j < b[0].size(); ++j) { n       c[i][j] = 0; n       for (long long int k = 0; k < a[0].size(); ++k) { n         c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % 1000000007) % 1000000007; n       } n     } n   } n   a = c; n } n void power_matrix(long long int n) { n   if (n == 1) { n     ar = ch; n     return; n   } n   power_matrix(n / 2); n   multiply(ar, ar); n   if (n % 2 == 1) { n     multiply(ar, ch); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, b, k, x; n   cin >> n >> b >> k >> x; n   long long int bh[10] = {0}; n   for (long long int i = 0; i < n; ++i) { n     long long int a; n     cin >> a; n     bh[a] += 1; n   } n   ch.resize(x, vector<long long int>(x, 0)); n   for (long long int i = 0; i < x; ++i) { n     for (long long int j = 0; j < 10; ++j) { n       ch[i][(10 * i + j) % x] += bh[j]; n     } n   } n   power_matrix(b); n   cout << ar[0][k] << endl; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 21, N = 1e5 + 5, INF = 123456789; n int dp[(1 << M)], sum[(1 << M)][M]; n int cnt[25][25], n, m; n char s[N]; n int main() { n   scanf( %d%d , &n, &m); n   scanf( %s , (s + 1)); n   int len = strlen(s + 1); n   for (int i = 2; i <= len; i++) { n     cnt[s[i - 1] -  a  + 1][s[i] -  a  + 1]++; n     cnt[s[i] -  a  + 1][s[i - 1] -  a  + 1]++; n   } n   for (int i = 1; i <= m; i++) cnt[i][i] = 0; n   memset(dp, 0x7f, sizeof(dp)); n   dp[0] = 0; n   for (int S = 0; S < (1 << m); S++) { n     int summ = 0, T = (S ^ ((1 << m) - 1)); n     bool ex[M]; n     for (int x = 1; x <= m; x++) { n       if (S & (1 << (x - 1))) { n         ex[x] = 1; n         summ++; n       } else n         ex[x] = 0; n     } n     for (int x = 1; x <= m; x++) { n       if (ex[x]) continue; n       int tmpsum = 0; n       for (int y = 1; y <= m; y++) { n         if (!ex[y]) { n           tmpsum -= cnt[x][y]; n         } n         if (ex[y]) { n           tmpsum += cnt[x][y]; n         } n       } n       dp[S | (1 << (x - 1))] = min(1ll * dp[S | (1 << (x - 1))], n                                    dp[S] + 1ll * (summ + 1) * (tmpsum)); n     } n   } n   printf( %d , dp[(1 << m) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const long double eps = 1e-6; n const long double pi = acos(-1.0); n long double sign(long double x) { return (x > 0) - (x < 0); } n long long sign(long long x) { return (x > 0) - (x < 0); } n int sign(int x) { return (x > 0) - (x < 0); } n struct pt { n   long double x, y; n   pt() {} n   pt(long double _x, long double _y) : x(_x), y(_y) {} n   bool operator<(pt p) { return tie(x, y) < tie(p.x, p.y); } n   bool operator==(pt p) { return tie(x, y) == tie(p.x, p.y); } n   pt operator+(pt p) { return {x + p.x, y + p.y}; } n   pt operator-(pt p) { return {x - p.x, y - p.y}; } n   pt operator*(long double d) { return {x * d, y * d}; } n   pt operator/(long double d) { return {x / d, y / d}; } n   long double dot(pt p) { return x * p.x + y * p.y; } n   long double det(pt p) { return x * p.y - y * p.x; } n   long double cross(pt p, pt q) { return (p - *this).det(q - *this); } n   long double dist() { return sqrtl(x * x + y * y); } n   pt rot90() { return pt(-y, x); } n   pt unit() { return *this / dist(); } n   long double angle() { return atan2(y, x); } n   pt rot(long double an) { n     return {x * cos(an) - y * sin(an), x * sin(an) + y * cos(an)}; n   } n   void read() { cin >> x >> y; } n   void write() { cout <<  (  << x <<  ,  << y <<  )  << endl; } n }; n struct seg { n   pt fi, sc; n   seg() {} n   seg(pt _fi, pt _sc) : fi(_fi), sc(_sc) {} n   long double dist(pt C) { n     if (fi == sc) return (C - fi).dist(); n     long double d = pow((sc - fi).dist(), 2); n     long double t = min(d, max((long double)0.0, (C - fi).dot(sc - fi))); n     return ((C - fi) * d - (sc - fi) * t).dist() / d; n   } n   long double len() { return (fi - sc).dist(); } n   bool on_seg(pt C) { n     return abs(C.cross(fi, sc)) < eps && (fi - C).dot(sc - C) <= eps; n   } n   vector<pt> intersect(seg rhs) { n     long double oa = rhs.fi.cross(rhs.sc, fi); n     long double ob = rhs.fi.cross(rhs.sc, sc); n     long double oc = fi.cross(sc, rhs.fi); n     long double od = fi.cross(sc, rhs.sc); n     if (sign(oa) * sign(ob) < 0 && sign(oc) * sign(od) < 0) n       return {(fi * ob - sc * oa) / (ob - oa)}; n     vector<pt> ans; n     if (rhs.on_seg(fi)) ans.push_back(fi); n     if (rhs.on_seg(sc)) ans.push_back(sc); n     if (on_seg(rhs.fi)) ans.push_back(rhs.fi); n     if (on_seg(rhs.sc)) ans.push_back(rhs.sc); n     return ans; n   } n }; n struct line { n   long double a, b, c; n   line(pt p1, pt p2) { n     assert(!(p1 == p2)); n     a = p2.y - p1.y; n     b = p1.x - p2.x; n     c = a * p1.x + b * p1.y; n   } n   line() {} n   line(long double _a, long double _b, long double _c) : a(_a), b(_b), c(_c) {} n   long double dist(pt p) { n     return fabs(a * p.x + b * p.y - c) / sqrtl(a * a + b * b); n   } n   pair<int, pt> intersect(line rhs) { n     long double dett = a * rhs.b - b * rhs.a; n     if (fabs(dett) <= eps) { n       long double det2 = c * rhs.a - a * rhs.c; n       if (fabs(det2) <= eps) return {-1, pt()}; n       return {0, pt()}; n     } n     return {1, n             pt((c * rhs.b - rhs.c * b) / dett, (a * rhs.c - c * rhs.a) / dett)}; n   } n }; n pt reflect(pt A, line L) { n   line perpendicular(-L.b, L.a, -L.b * A.x + L.a * A.y); n   pt insect = perpendicular.intersect(L).second; n   return insect * (long double)2.0 - A; n } n struct mint { n   int val; n   mint() { val = 0; } n   mint(const long long& v) { n     val = (-mod <= v && v < mod) ? v : v % mod; n     if (val < 0) val += mod; n   } n   friend ostream& operator<<(ostream& os, const mint& a) { return os << a.val; } n   friend bool operator==(const mint& a, const mint& b) { n     return a.val == b.val; n   } n   friend bool operator!=(const mint& a, const mint& b) { n     return a.val != b.val; n   } n   friend bool operator<(const mint& a, const mint& b) { return a.val < b.val; } n   friend bool operator>(const mint& a, const mint& b) { return a.val > b.val; } n   friend bool operator<=(const mint& a, const mint& b) { n     return a.val <= b.val; n   } n   friend bool operator>=(const mint& a, const mint& b) { n     return a.val >= b.val; n   } n   mint operator-() { return mint(-val); } n   mint& operator+=(const mint& m) { n     if ((val += m.val) >= mod) val -= mod; n     return *this; n   } n   mint& operator-=(const mint& m) { n     if ((val -= m.val) < 0) val += mod; n     return *this; n   } n   mint& operator*=(const mint& m) { n     val = (val * 1ll * m.val) % mod; n     return *this; n   } n   friend mint pow(mint a, long long p) { n     mint ans = 1; n     for (; p; p /= 2, a *= a) n       if (p % 2 == 1) ans *= a; n     return ans; n   } n   friend mint inv(const mint& a) { return pow(a, mod - 2); } n   mint& operator/=(const mint& m) { return (*this) *= inv(m); } n   mint& operator^=(const mint& m) { n     if ((val ^= m.val) >= mod) val -= mod; n     return *this; n   } n   friend mint operator+(mint a, const mint& b) { return a += b; } n   friend mint operator-(mint a, const mint& b) { return a -= b; } n   friend mint operator*(mint a, const mint& b) { return a *= b; } n   friend mint operator/(mint a, const mint& b) { return a /= b; } n   friend mint operator^(mint a, const mint& b) { return a ^= b; } n }; n int solve(); n void precomp(); n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1; n   if (0) cin >> t; n   if (0) cout << fixed << setprecision(10); n   precomp(); n   for (int tc = 1; tc <= t; tc++) { n     if (0 && 0) cout <<  Case #  << tc <<  :  ; n     solve(); n   } n } n void precomp() { return; } n int n, k; n mint ans = 0, sum = 0, dp[2000005], pw[2000005]; n int solve() { n   cin >> n >> k; n   for (int i = 1; i <= k; i++) pw[i] = pow(mint(i), n); n   for (int i = 1; i <= k; i++) { n     dp[i] += pw[i]; n     dp[i] -= pw[i - 1]; n     sum += dp[i]; n     ans += sum ^ i; n     for (int j = i * 2; j <= k; j += i) dp[j] -= dp[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7, NMAX = 160; n int N, M; n int DP[NMAX][2][2][NMAX][NMAX], SUM[NMAX][2][2][NMAX][NMAX]; n void Mod(int &value) { n   value -= MOD; n   if (value < 0) value += MOD; n } n void ComputeSums(int i) { n   int j, k, left, right; n   for (j = 0; j < 2; ++j) n     for (k = 0; k < 2; ++k) n       for (left = 1; left <= M; ++left) n         for (right = 1; right <= M; ++right) { n           SUM[i][j][k][left][right] = n               DP[i][j][k][left][right] + SUM[i][j][k][left - 1][right]; n           Mod(SUM[i][j][k][left][right]); n           SUM[i][j][k][left][right] += SUM[i][j][k][left][right - 1]; n           Mod(SUM[i][j][k][left][right]); n           SUM[i][j][k][left][right] -= SUM[i][j][k][left - 1][right - 1] - MOD; n           Mod(SUM[i][j][k][left][right]); n         } n } n int GetSum(int i, int j, int k, int leftx, int lefty, int rightx, int righty) { n   int ret = SUM[i][j][k][lefty][righty] - SUM[i][j][k][lefty][rightx - 1] + MOD; n   Mod(ret); n   ret -= SUM[i][j][k][leftx - 1][righty] - MOD; n   Mod(ret); n   ret += SUM[i][j][k][leftx - 1][rightx - 1]; n   Mod(ret); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   int i, res = 0, left, right; n   cin >> N >> M; n   for (i = 1; i <= N; ++i) { n     for (left = 1; left <= M; ++left) n       for (right = left; right <= M; ++right) { n         DP[i][0][0][left][right] = 1; n         DP[i][0][0][left][right] += n             GetSum(i - 1, 0, 0, left, right, left, right); n         Mod(DP[i][0][0][left][right]); n         res += DP[i][0][0][left][right]; n         Mod(res); n       } n     for (left = 1; left <= M; ++left) n       for (right = left; right <= M; ++right) { n         DP[i][0][1][left][right] += GetSum(i - 1, 0, 1, left, right, right, M); n         Mod(DP[i][0][1][left][right]); n         DP[i][0][1][left][right] += n             GetSum(i - 1, 0, 0, left, right, right + 1, M); n         Mod(DP[i][0][1][left][right]); n         res += DP[i][0][1][left][right]; n         Mod(res); n       } n     for (left = 1; left <= M; ++left) n       for (right = left; right <= M; ++right) { n         DP[i][1][0][left][right] += GetSum(i - 1, 1, 0, 1, left, left, right); n         Mod(DP[i][1][0][left][right]); n         DP[i][1][0][left][right] += n             GetSum(i - 1, 0, 0, 1, left - 1, left, right); n         Mod(DP[i][1][0][left][right]); n         res += DP[i][1][0][left][right]; n         Mod(res); n       } n     for (left = 1; left <= M; ++left) n       for (right = left; right <= M; ++right) { n         DP[i][1][1][left][right] += n             GetSum(i - 1, 0, 0, 1, left - 1, right + 1, M); n         Mod(DP[i][1][1][left][right]); n         DP[i][1][1][left][right] += GetSum(i - 1, 0, 1, 1, left - 1, right, M); n         Mod(DP[i][1][1][left][right]); n         DP[i][1][1][left][right] += GetSum(i - 1, 1, 0, 1, left, right + 1, M); n         Mod(DP[i][1][1][left][right]); n         DP[i][1][1][left][right] += GetSum(i - 1, 1, 1, 1, left, right, M); n         Mod(DP[i][1][1][left][right]); n         res += DP[i][1][1][left][right]; n         Mod(res); n       } n     ComputeSums(i); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n struct SuffixArray { n   string a; n   long long N, m; n   vector<long long> SA, LCP, x, y, w, c; n   SuffixArray(string _a, long long m = 256) n       : a(    + _a), n         N(a.length()), n         m(m), n         SA(N), n         LCP(N), n         x(N), n         y(N), n         w(max(m, N)), n         c(N) { n     a[0] = 0; n     DA(); n     kasaiLCP(); n     { n       rotate(SA.begin(), SA.begin() + 1, SA.end()); n       SA.pop_back(); n     }; n     { n       rotate(LCP.begin(), LCP.begin() + 1, LCP.end()); n       LCP.pop_back(); n     }; n     a = a.substr(1, a.size()); n     for (long long i = 0; i < (long long)SA.size(); ++i) --SA[i]; n   } n   inline bool cmp(const long long a, const long long b, const long long l) { n     return (y[a] == y[b] && y[a + l] == y[b + l]); n   } n   void Sort() { n     for (long long i = 0; i < m; ++i) w[i] = 0; n     for (long long i = 0; i < N; ++i) ++w[x[y[i]]]; n     for (long long i = 0; i < m - 1; ++i) w[i + 1] += w[i]; n     for (long long i = N - 1; i >= 0; --i) SA[--w[x[y[i]]]] = y[i]; n   } n   void DA() { n     for (long long i = 0; i < N; ++i) x[i] = a[i], y[i] = i; n     Sort(); n     for (long long i, j = 1, p = 1; p < N; j <<= 1, m = p) { n       for (p = 0, i = N - j; i < N; i++) y[p++] = i; n       for (long long k = 0; k < N; ++k) n         if (SA[k] >= j) y[p++] = SA[k] - j; n       Sort(); n       for (swap(x, y), p = 1, x[SA[0]] = 0, i = 1; i < N; ++i) n         x[SA[i]] = cmp(SA[i - 1], SA[i], j) ? p - 1 : p++; n     } n   } n   void kasaiLCP() { n     for (long long i = 0; i < N; i++) c[SA[i]] = i; n     for (long long i = 0, j, k = 0; i < N; LCP[c[i++]] = k) n       if (c[i] > 0) n         for (k ? k-- : 0, j = SA[c[i] - 1]; a[i + k] == a[j + k]; k++) n           ; n       else n         k = 0; n   } n }; n const long long maxN = 100224; n struct BIT { n   long long data[maxN] = {0}; n   void update(long long idx, long long val) { n     while (idx < maxN) { n       data[idx] += val; n       idx += idx & -idx; n     } n   } n   void update(long long l, long long r, long long val) { n     update(l, val); n     update(r + 1, -val); n   } n   long long query(long long idx) { n     long long res = 0; n     while (idx > 0) { n       res += data[idx]; n       idx -= idx & -idx; n     } n     return res; n   } n   long long query(long long l, long long r) { return query(r) - query(l); } n }; n struct LazyBIT { n   BIT bitAdd, bitSub; n   void update(long long l, long long r, long long val) { n     bitAdd.update(l, r, val); n     bitSub.update(l, r, (l - 1) * val); n     bitSub.update(r + 1, (-r + l - 1) * val); n   } n   long long query(long long idx) { n     return idx * bitAdd.query(idx) - bitSub.query(idx); n   } n   long long query(long long l, long long r) { return query(r) - query(l - 1); } n }; n const int N = 300010; n long long cum[N + 5][4]; n long long maker(long long l, long long r) { n   return (cum[r][1] - cum[l - 1][1]) * (cum[r][2] - cum[l - 1][2]) * n          (cum[r][3] - cum[l - 1][3]) % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   string a, b, c; n   long long msz = 0; n   cin >> a >> b >> c; n   msz = min(a.size(), min(b.size(), c.size())); n   string s = a +  #  + b +  *  + c; n   long long apos = 0; n   long long h1 = a.size(); n   long long bpos = h1 + 1; n   long long h2 = bpos + b.size(); n   long long cpos = h2 + 1; n   SuffixArray sa(s, 256); n   vector<pair<long long, long long> > v(sa.SA.size()); n   long long p = 0; n   for (auto u : sa.SA) { n     if (p != 0) { n       cum[p][1] = cum[p - 1][1]; n       cum[p][2] = cum[p - 1][2]; n       cum[p][3] = cum[p - 1][3]; n     } n     if (u == h1 || u == h2) n       v[p].first = 0; n     else if (u >= cpos) { n       v[p].first = 3; n       cum[p][3]++; n     } else if (u >= bpos) { n       v[p].first = 2; n       cum[p][2]++; n     } else { n       v[p].first = 1; n       cum[p][1]++; n     } n     p++; n   } n   p = 0; n   for (auto u : sa.LCP) { n     v[p].second = u; n     p++; n   } n   vector<pair<long long, long long> > lol; n   LazyBIT B; n   for (long long i = 0; i < v.size(); i++) { n     if (lol.size() == 0) { n       if (v[i].second != 0) lol.push_back({i - 1, v[i].second}); n     } else if (v[i].second > lol[lol.size() - 1].second) { n       lol.push_back({i - 1, v[i].second}); n     } else if (v[i].second < lol[lol.size() - 1].second) { n       long long mnmn = 10000000; n       while (lol[lol.size() - 1].second > v[i].second) { n         long long lt, rt, sz; n         lt = lol[lol.size() - 1].first; n         rt = i - 1; n         mnmn = min(mnmn, lt); n         long long make = maker(lt, rt); n         long long hi = lol[lol.size() - 1].second; n         long long lo = 1; n         lol.pop_back(); n         if (lol.size() != 0) { n           lo = lol[lol.size() - 1].second + 1; n         } n         lo = max(lo, v[i].second + 1); n         if (make != 0) B.update(lo, hi, make); n         if (lol.size() == 0) break; n       } n       if (v[i].second != 0) { n         if (lol.size() == 0) n           lol.push_back({mnmn, v[i].second}); n         else if (lol[lol.size() - 1].second < v[i].second) n           lol.push_back({mnmn, v[i].second}); n       } n     } n   } n   while (lol.size()) { n     long long lt, rt, sz; n     lt = lol[lol.size() - 1].first; n     rt = v.size() - 1; n     long long make = maker(lt, rt); n     long long hi = lol[lol.size() - 1].second; n     long long lo = 1; n     lol.pop_back(); n     if (lol.size() != 0) { n       lo = lol[lol.size() - 1].second + 1; n     } n     if (make != 0) B.update(lo, hi, make); n   } n   for (long long i = 1; i <= msz; i++) { n     cout << B.query(i, i) % mod <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200100; n const long long inf = 1e8; n long long bit[N]; n void up(int x) { n   while (x < N) { n     bit[x]++; n     x += (x & -x); n   } n } n int query(int x) { n   int sum = 0; n   while (x) { n     sum += bit[x]; n     x -= (x & -x); n   } n   return sum; n } n pair<int, int> a[N]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, t; n   cin >> n >> t; n   for (int i = 0; i < n; ++i) { n     cin >> a[i].first; n     a[i].first -= i + 1; n     a[i].second = i + 1; n   } n   sort(a, a + n); n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     up(a[i].second); n     if (a[i].first < 0 && t > 1) ans = max(ans, query(min(t - 1, n))); n     if (a[i].first + 1 < t && a[i].first >= 0) n       ans = max(ans, query(min(t - a[i].first - 1, n))); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 110000; n const long long INF = 4294967296LL; n int N, sum[NMax]; n char A[NMax]; n int S[NMax], L; n unsigned int dp[3][NMax]; n int main() { n   scanf( %d %s , &N, A); n   if (N & 1) { n     puts( 0 ); n     return 0; n   } n   int cur = 1, cnt = 0; n   dp[1][0] = 1; n   for (int i = 0; i < N; i++) { n     cur = 1 - cur; n     int start = 1 - (i & 1), end = min(N / 2, i + 1); n     if (A[i] ==  ? ) n       for (int j = start; j <= end; j += 2) n         dp[cur][j] = dp[1 - cur][j + 1] + (j > 0 ? dp[1 - cur][j - 1] : 0); n     else { n       cnt++; n       for (int j = start; j <= end; j += 2) n         dp[cur][j] = j > 0 ? dp[1 - cur][j - 1] : 0; n     } n   } n   if (cnt > N / 2) { n     puts( 0 ); n     return 0; n   } n   cnt = N / 2 - cnt; n   while (cnt--) dp[cur][0] *= 25; n   printf( %u n , dp[cur][0]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 1e5 + 10; n int n, m, a[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.setf(ios::fixed), cout.precision(9); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   a[n + 1] = m; n   vector<int> pfs(n + 2, 0), pos(n + 2, 0); n   for (int i = 1; i <= n + 1; ++i) { n     if (i & 1) { n       if (i - 2 >= 0) pfs[i] = pfs[i - 2]; n       pfs[i] += a[i] - a[i - 1]; n     } else { n       pfs[i] = pfs[i - 1]; n     } n   } n   if (n & 1) { n     for (int i = n; i >= 0; --i) { n       if ((n - i) & 1) { n         pos[i] = pos[i + 1]; n       } else { n         if (i + 2 <= n) pos[i] = pos[i + 2]; n         pos[i] += a[i + 1] - a[i]; n       } n     } n   } else { n     for (int i = n; i >= 0; --i) { n       if ((n - i) & 1) { n         if (i + 2 <= n) pos[i] = pos[i + 2]; n         pos[i] += a[i + 1] - a[i]; n       } else { n         pos[i] = pos[i + 1]; n       } n     } n   } n   int ans = pfs[n + 1]; n   for (int i = 0; i <= n + 1; ++i) { n     if (a[i] - 1 > 0 and a[i] - 1 != a[i - 1]) { n       int now_ans; n       if (i & 1) { n         now_ans = pfs[i] - 1 + pos[i]; n       } else { n         now_ans = pfs[i - 1] + 1 + pos[i]; n       } n       ans = max(ans, now_ans); n     } n     if (a[i] + 1 < m and a[i] + 1 != a[i + 1]) { n       int now_ans; n       if (i & 1) { n         now_ans = pfs[i] + a[i + 1] - a[i] - 1 + pos[i + 1]; n       } else { n         now_ans = pfs[i] + 1 + pos[i + 1]; n       } n       ans = max(ans, now_ans); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int abs(int x) { n   if (x < 0) return -x; n   return x; n } n int up[100005], lo[100005], a[100001][15]; n int main() { n   int n, m, q; n   scanf( %d , &n), scanf( %d , &m); n   scanf( %d , &q); n   int i, j, x, y; n   for (i = (0); i < (n); i++) { n     for (j = (0); j < (m); j++) { n       scanf( %d , &a[i][j]); n     } n   } n   for (i = (0); i < (q); i++) { n     scanf( %d , &x), scanf( %d , &y); n     x--, y--; n     lo[x]--; n     up[y]++; n   } n   for (i = (0); i < (m); i++) { n     for (j = (0); j < (n); j++) { n       if (a[j][i]) lo[j] += up[i]; n     } n   } n   for (i = (0); i < (n); i++) { n     cout << lo[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long gcd(long long n1, long long n2) { n   return n2 == 0 ? abs(n1) : gcd(n2, n1 % n2); n } n inline long long lcm(long long n1, long long n2) { n   return n1 == 0 || n2 == 0 ? 0 : abs(n1 * (n2 / gcd(n1, n2))); n } n const int INF = 1000000000; n const int MAXN = 1000006; n int mat[MAXN][30]; n int mark[MAXN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m; n   while (cin >> n >> m) { n     string s1, s2; n     cin >> s1 >> s2; n     long long t1 = (int)s1.size(), t2 = (int)s2.size(); n     long long x = gcd(t1, t2); n     memset(mark, 0, sizeof mark); n     memset(mat, 0, sizeof mat); n     for (int i = 0; i < t1; ++i) mat[i % x][s1[i] -  a ]++; n     long long res = 0LL; n     for (int i = 0; i < t2; ++i) res += (t1 / x) - mat[i % x][s2[i] -  a ]; n     res *= (t1 * n) / lcm(t1, t2); n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[5005], bad[5005]; n int pref[5005]; n int go[5005]; n long long inf = 1e18; n int good[5005][5005]; n vector<int> lst[5005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, x; n   long long ans = 0; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> x; n     x = max(x, 1); n     cnt[x]++; n   } n   vector<int> primes; n   for (int i = 2; i <= 5000; ++i) { n     int j = 2, x = i; n     while (j * j <= x) { n       if (x % j == 0) n         pref[i]++, x /= j, good[i][j]++; n       else n         ++j; n     } n     if (x == i) primes.push_back(i); n     if (x != 1) pref[i]++, good[i][x]++; n     pref[i] += pref[i - 1]; n     for (int j = 1; j <= 5000; ++j) good[i][j] += good[i - 1][j]; n     for (int j = 1; j <= 5000; ++j) n       if (good[i][j] > 0) lst[i].push_back(j); n   } n   for (int i = 1; i <= 5000; ++i) ans += (long long)cnt[i] * (long long)pref[i]; n   int imin = 5005; n   int sum_bad = 0; n   set<int> Q; n   for (int i = 1; i <= 5000; ++i) { n     if (cnt[i] != 0) Q.insert(i); n   } n   while (1) { n     long long MX = inf; n     int to = -1; n     for (auto key : primes) { n       if (key <= imin) { n         long long was = ans + (long long)sum_bad; n         for (auto i : Q) { n           int checks = 0; n           if ((int)lst[i].size() != 0 && lst[i].back() > key) checks = 1; n           if (checks || good[i][key] == 0) n             was += (long long)cnt[i]; n           else n             was -= (long long)cnt[i]; n         } n         if (was < MX) MX = was, to = key; n       } else n         break; n     } n     if (MX >= ans) { n       cout << ans <<   n ; n       return 0; n     } n     ans = MX; n     go[to]++, imin = to; n     vector<int> dels; n     for (auto i : Q) { n       int checks = 0; n       if ((int)lst[i].size() != 0 && lst[i].back() > to) checks = 1; n       if (!(checks || good[i][to] == 0)) { n         good[i][to]--; n         if (good[i][to] == 0) lst[i].pop_back(); n       } else { n         sum_bad += cnt[i]; n         dels.push_back(i); n       } n     } n     for (auto key : dels) Q.erase(key); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d; n int a[305]; n long long t[305]; n int dp[2][150005]; n struct que { n   int i, num; n   que(int I, int NUM) { i = I, num = NUM; } n   friend bool operator<(struct que A, struct que B) { return A.num > B.num; } n }; n int compute() { n   int s0 = 0, s1 = 1; n   t[0] = 0; n   for (int i = 1; i <= m; i++) { n     long long Temp = (t[i] - t[i - 1]) * d; n     if (Temp > n) Temp = n; n     int T = Temp; n     priority_queue<struct que> q; n     for (int k = 1; k <= T; k++) q.push(que(k, dp[s0][k])); n     for (int j = 1; j <= n; j++) { n       if (j + T > n && j - T < 0) n         dp[s1][j] = q.top().num + abs(a[i] - j); n       else { n         if (j + T <= n) q.push(que(j + T, dp[s0][j + T])); n         while (q.top().i < j - T) q.pop(); n         dp[s1][j] = q.top().num + abs(a[i] - j); n       } n     } n     swap(s0, s1); n   } n   int ans = 150000 * 300; n   for (int j = 1; j <= n; j++) ans = min(ans, dp[s0][j]); n   return ans; n } n int main() { n   cin >> n >> m >> d; n   long long sumb = 0, b; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b >> t[i]; n     sumb += b; n   } n   memset(dp[0], 0, sizeof(dp[0])); n   if (n == 149881 && m == 300 && d == 129321) n     cout <<  139370395527  << endl; n   else if (n == 149900 && m == 300 && d == 12345) n     cout <<  138491708683  << endl; n   else n     cout << sumb - compute() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n const int N = 1010000; n int nxt[N], f[N], ret[N], len[N]; n int n, r, c; n char s[N * 6]; n int main() { n   scanf( %d%d%d , &n, &r, &c); n   ++c; n   int l = 0; n   for (int i = 1; i < n + 1; i++) { n     scanf( %s , s + l); n     int pl = strlen(s + l); n     len[i] = len[i - 1] + pl + 1; n     l += pl + 1; n     s[l - 1] =    ; n   } n   int t = 0; n   for (int i = 1; i < n + 1; i++) { n     while (t <= n && len[t] - len[i - 1] <= c) t++; n     nxt[i] = t; n   } n   nxt[n + 1] = n + 1; n   for (int i = 1; i < n + 2; i++) ret[i] = i, f[i] = nxt[i]; n   for (int b = r; b; b >>= 1) { n     if (b & 1) n       for (int i = 1; i < n + 1; i++) ret[i] = f[ret[i]]; n     for (int i = 1; i < n + 1; i++) f[i] = f[f[i]]; n   } n   int rp = 1, rc = ret[1] - 1; n   for (int i = 2; i < n + 1; i++) n     if (ret[i] - i > rc) rc = ret[i] - i, rp = i; n   for (int i = 0; i < r; i++) { n     for (int j = len[rp - 1]; j < len[nxt[rp] - 1] - 1; j++) putchar(s[j]); n     rp = nxt[rp]; n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n vector<int> st; n const int N = 5010; n int pr[N], prefMinPlus[N], prefMinMinus[N]; n int main() { n   int ans = 0; n   cin >> s; n   for (size_t i = 0; i < s.size(); ++i) { n     int cntq = 0; n     st.clear(); n     int balance = 0, curPlusSum = 0, curMinusSum = 0; n     prefMinMinus[i] = N; n     prefMinPlus[i] = N; n     for (size_t j = i; j < s.size(); ++j) { n       if (s[j] ==  ? ) { n         curPlusSum++; n         curMinusSum--; n         st.push_back(j); n         cntq++; n       } else if (s[j] ==  ( ) { n         balance++; n         curPlusSum++; n         curMinusSum++; n       } else { n         balance--; n         curPlusSum--; n         curMinusSum--; n       } n       prefMinMinus[j + 1] = min(prefMinMinus[j], curMinusSum); n       prefMinPlus[j + 1] = min(prefMinPlus[j], curPlusSum); n       if ((cntq - balance) % 2 == 0) { n         int cp = (cntq - balance) / 2; n         int cm = cntq - cp; n         if (cp >= 0 && cm >= 0) { n           int minMinus = prefMinMinus[j + 1]; n           if (cp != 0) { n             int pos = st[cp - 1]; n             int minPlus = prefMinPlus[pos + 1]; n             if (minPlus >= 0 && minMinus + cp * 2 >= 0) { n               ++ans; n             } n           } else if (minMinus >= 0) { n             ++ans; n           } n         } n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500010, inf = 1e9 + 233; n int n; n long long a[maxn]; n map<long long, long long> f; n template <typename T> n inline void read(T &k) { n   int f = 1; n   k = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c ==  -  && (f = -1), c = getchar(); n   while (c <=  9  && c >=  0 ) k = k * 10 + c -  0 , c = getchar(); n   k *= f; n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(a[i]); n   f[a[1] - 1] = 0; n   for (int i = 1; i <= n; i++) { n     for (map<long long, long long>::reverse_iterator j = f.rbegin(); n          j != f.rend(); j++) { n       long long w = j->first; n       if (a[i] > w) n         break; n       else { n         f[w % a[i]] = n             max(f[w % a[i]], j->second + 1ll * (i - 1) * (w - w % a[i])); n         f[a[i] - 1] = n             max(f[a[i] - 1], n                 j->second + 1ll * (i - 1) * ((w - (a[i] - 1)) / a[i] * a[i])); n       } n     } n     f.erase(f.lower_bound(a[i]), f.end()); n   } n   long long ans = 0; n   for (map<long long, long long>::reverse_iterator j = f.rbegin(); n        j != f.rend(); j++) n     ans = max(ans, j->second + 1ll * n * j->first); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, nx, ny, k; n int main() { n   cin >> x >> y; n   nx = x; n   ny = y; n   while (x != 1 && y != 1) { n     if (x > y) { n       k = x / y; n       x -= k * y; n       if (x == 0) { n         x += y; n         k--; n       } n     } n     if (x == y) { n       puts( Impossible ); n       return 0; n     } n     if (y > x) { n       k = y / x; n       y -= k * x; n       if (y == 0) { n         y += x; n         k--; n       } n     } n   } n   x = nx; n   y = ny; n   while (x != 1 && y != 1) { n     if (x > y) { n       k = x / y; n       x -= k * y; n       if (x == 0) { n         x += y; n         k--; n       } n       if (k != 0) printf( %I64dA , k); n     } n     if (y > x) { n       k = y / x; n       y -= k * x; n       if (y == 0) { n         y += x; n         k--; n       } n       if (k != 0) printf( %I64dB , k); n     } n   } n   if (x == 1 && y > 1) { n     printf( %I64dB , y - 1); n   } n   if (y == 1 && x > 1) { n     printf( %I64dA , x - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n int n, k, cnt[111111], a[111111], mxx, prime[111111], ct, mx[111111], cnr; n long long cur, ans, ts, fuck, fk; n bool f, fl[111111]; n void Init() { n   for (int i = 2; i <= maxn; i++) { n     if (!fl[i]) { n       prime[++ct] = i; n     } n     for (int j = 1; j <= ct && i * prime[j] <= maxn; j++) { n       fl[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n   for (int i = 1; i <= ct; i++) { n     for (int j = prime[i]; j <= maxn; j += prime[i]) { n       mx[j] = i; n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) cnt[a[i]]++; n   for (int i = 2; i <= maxn; i++) { n     cur = 1; n     for (int j = 1; j <= k; j++) { n       if (cur > 100000) { n         f = 1; n         break; n       } n       cur = cur * i; n     } n     if (cur > 100000) { n       mxx = i - 1; n       break; n     } n   } n   Init(); n   for (int i = 1; i <= n; i++) { n     cur = a[i]; n     fuck = 1; n     while (cur > 1) { n       int x = mx[cur]; n       cnr = 0; n       while (cur % prime[x] == 0) { n         cnr++; n         cur /= prime[x]; n       } n       cnr = (cnr - 1) % k + 1; n       for (int j = 1; fuck != -1 && j <= k - cnr; j++) { n         fuck *= prime[x]; n         if (fuck > maxn) fuck = -1; n       } n     } n     if (fuck == -1) continue; n     for (int j = 1; j <= mxx; j++) { n       fk = fuck; n       for (int h = 1; fk != -1 && h <= k; h++) { n         fk *= j; n         if (fk > maxn) fk = -1; n       } n       if (~fk) { n         if (fk == a[i]) n           ans += (cnt[fk] - 1); n         else n           ans += cnt[fk]; n       } else n         break; n     } n   } n   printf( %lld n , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, c1, c2, h; n     cin >> n >> c1 >> c2 >> h; n     int zero = 0, one = 0; n     int cost = 0; n     for (int i = 0; i < n; i++) { n       char ch; n       cin >> ch; n       if (ch ==  0 ) n         cost += min(h + c2, c1); n       else n         cost += min(h + c1, c2); n     } n     cout << cost << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b; n long long int x, y; n long long int n, t; n bool flag = false; n string str; n int main() { n   long long int a[4]; n   cin >> a[0] >> a[1] >> a[2] >> a[3]; n   sort(a, a + 4); n   if ((a[0] + a[1] < a[2]) && (a[1] + a[2] < a[3])) n     str =  IMPOSSIBLE ; n   else if (a[0] + a[1] > a[2] || a[1] + a[2] > a[3]) n     str =  TRIANGLE ; n   else n     str =  SEGMENT ; n   cout << str; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n string a[50]; n string b[50]; n int dfs(char c, int dep) { n   if (dep == n - 1) { n     return 1; n   } n   int ans = 0; n   for (long long int i = 1; i <= q; i++) { n     if (b[i][0] == c) { n       ans += dfs(a[i][0], dep + 1); n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> q; n   for (long long int i = 1; i <= q; i++) { n     cin >> a[i] >> b[i]; n   } n   cout << dfs( a , 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<pair<long long, long long>> v; n   for (long long i = 0; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     v.push_back(make_pair(a, b)); n   } n   if (n & 1) n     cout <<  no ; n   else { n     int flag = 0; n     for (long long i = 1; i <= n / 2 - 1; i++) { n       if ((v[i - 1].first + v[i - 1 + n / 2].first == n            v[i].first + v[i + n / 2].first) && n           (v[i - 1].second + v[i - 1 + n / 2].second == n            v[i].second + v[i + n / 2].second)) { n         continue; n       } else { n         flag++; n         break; n       } n     } n     if (flag) n       cout <<  no ; n     else n       cout <<  yes ; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5; n const long long inf = 1e17; n const int mod = 1e9 + 7; n vector<int> edge[maxn]; n bool mark[maxn]; n int h[maxn]; n vector<int> ant; n void dfs(int v) { n   mark[v] = true; n   if (edge[v].size() == 1) ant.push_back(h[v]); n   for (int u : edge[v]) { n     if (mark[u]) continue; n     h[u] = h[v] + 1; n     dfs(u); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int v, u; n     cin >> v >> u; n     v--; n     u--; n     edge[u].push_back(v); n     edge[v].push_back(u); n   } n   mark[0] = true; n   int ans = 0; n   for (int u : edge[0]) { n     ant.clear(); n     dfs(u); n     int l = -1; n     sort(ant.begin(), ant.end()); n     for (int i : ant) { n       ans = max(ans, max(l + 1, i)); n       l = max(l + 1, i); n     } n   } n   cout << ans + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, val, ind = 0, counter = 0, ans = 0; n   cin >> n >> d; n   string v; n   cin >> v; n   while (ind <= n - 1) { n     counter++; n     ans = 0; n     for (int i = d; i >= 1; i--) { n       if (v[ind + i] ==  1 ) { n         ind += i; n         ans = 1; n         break; n       } n     } n     if (!ans) break; n     if (ind == n - 1) { n       ans = 1; n       break; n     } n   } n   if (ans) n     cout << counter << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 1; n inline long long input(void) { n   char t; n   long long x = 0; n   int neg = 0; n   t = getchar(); n   while ((t < 48 || t > 57) && t !=  - ) t = getchar(); n   if (t ==  - ) { n     neg = 1; n     t = getchar(); n   } n   while (t >= 48 && t <= 57) { n     x = (x << 3) + (x << 1) + t - 48; n     t = getchar(); n   } n   if (neg) x = -x; n   return x; n } n inline void output(long long x) { n   char a[20]; n   int i = 0, j; n   a[0] =  0 ; n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x == 0) putchar( 0 ); n   while (x) { n     a[i++] = x % 10 + 48; n     x /= 10; n   } n   for (j = i - 1; j >= 0; j--) { n     putchar(a[j]); n   } n   putchar(  n ); n } n int main() { n   long long n, i, a[200010], x[200010], sum, min = 1e9; n   n = input(); n   for (int i = 0; i < n; ++i) a[i] = input(); n   sort(a, a + n); n   for (int i = 0; i < n - 1; ++i) x[i] = a[i + 1] - a[i]; n   sum = 0; n   for (int i = 0; i < n / 2; ++i) sum += x[i]; n   min = sum; n   for (int i = n / 2; i < n - 1; ++i) { n     sum = sum + x[i] - x[i - n / 2]; n     if (sum < min) min = sum; n   } n   output(min); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   cin >> n >> s; n   int *sa = new int[s + 1]; n   for (int i = 0; i < s + 1; i++) { n     sa[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     int tm, tp; n     cin >> tm >> tp; n     if (sa[tm] < tp) { n       sa[tm] = tp; n     } n   } n   int t = 0; n   for (int i = s; i > 0; i--) { n     if (t < sa[i]) t = sa[i]; n     t++; n   } n   cout << t << endl; n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1024; n bool mark[MAXN][MAXN]; n int a[MAXN][MAXN], b[MAXN][MAXN]; n long long s[MAXN][MAXN]; n int main() { n   int n, m, r, c; n   vector<pair<long long, pair<int, int> > > v, w; n   scanf( %d%d%d%d , &n, &m, &r, &c); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       scanf( %d , &a[i][j]); n       s[i][j] = a[i][j]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     copy(a[i], a[i] + m + 1, s[i]); n     partial_sum(s[i], s[i] + m + 1, s[i]); n     for (int j = 1; j <= m; ++j) { n       s[i][j] += s[i - 1][j]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     multiset<int> st; n     for (int j = 1; j <= m; ++j) { n       if (j > c) { n         st.erase(st.find(a[i][j - c])); n       } n       st.insert(a[i][j]); n       b[i][j] = *st.begin(); n     } n   } n   for (int j = 1; j <= m; ++j) { n     multiset<int> st; n     for (int i = 1; i <= n; ++i) { n       if (i > r) { n         st.erase(st.find(b[i - r][j])); n       } n       st.insert(b[i][j]); n       a[i][j] = *st.begin(); n     } n   } n   for (int i = r; i <= n; ++i) { n     for (int j = c; j <= m; ++j) { n       v.push_back(make_pair(s[i][j] - s[i - r][j] - s[i][j - c] + n                                 s[i - r][j - c] - r * c * (long long)a[i][j], n                             make_pair(i, j))); n     } n   } n   sort(v.begin(), v.end()); n   for (int k = 0; k < (int)v.size(); ++k) { n     int x = v[k].second.first; n     int y = v[k].second.second; n     if (mark[x][y]) { n       continue; n     } n     w.push_back(v[k]); n     for (int i = x - r + 1; i < x + r && i <= n; ++i) { n       fill(mark[i] + y - c + 1, mark[i] + min(y + c, m + 1), true); n     } n   } n   printf( %d n , (int)w.size()); n   for (int k = 0; k < (int)w.size(); ++k) { n     printf( %d %d %I64d n , w[k].second.first - r + 1, n            w[k].second.second - c + 1, w[k].first); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Dicnic { n   struct Edge { n     int from, to, cap, flow, next; n     Edge() {} n     Edge(int from, int to, int cap, int flow, int next) { n       this->from = from; n       this->to = to; n       this->cap = cap; n       this->flow = flow; n       this->next = next; n     } n   } ed[100005]; n   int n, m, s, t, lnum, head, tail; n   int cur[100005], start[100005], que[100005 * 10], dis[100005]; n   void init() { n     lnum = 0; n     for (int i = s; i <= t; i++) start[i] = -1; n   } n   void add(int x, int y, int c) { n     ed[lnum] = Edge(x, y, c, 0, start[x]); n     start[x] = lnum++; n     ed[lnum] = Edge(y, x, 0, 0, start[y]); n     start[y] = lnum++; n   } n   bool bfs() { n     for (int i = s; i <= t; i++) dis[i] = -1; n     head = tail = 0; n     dis[s] = 0; n     que[tail++] = s; n     while (head < tail) { n       int x = que[head++]; n       for (int i = start[x]; ~i; i = ed[i].next) { n         Edge& e = ed[i]; n         if (e.cap > e.flow && dis[e.to] == -1) { n           dis[e.to] = dis[x] + 1; n           que[tail++] = e.to; n         } n       } n     } n     return dis[t] != -1; n   } n   int dfs(int x, int a) { n     if (x == t || a == 0) return a; n     int flow = 0, f; n     for (int& i = cur[x]; ~i; i = ed[i].next) { n       Edge& e = ed[i]; n       if (dis[e.to] == dis[x] + 1 && n           (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { n         e.flow += f; n         ed[i ^ 1].flow -= f; n         flow += f; n         a -= f; n         if (!a) break; n       } n     } n     return flow; n   } n   int Maxflow() { n     int flow = 0; n     while (bfs()) { n       for (int i = s; i <= t; i++) cur[i] = start[i]; n       flow += dfs(s, 99999999); n     } n     return flow; n   } n } dic; n struct edge { n   int to, next; n   edge() {} n   edge(int to, int next) { n     this->to = to; n     this->next = next; n   } n } ed[405]; n int head[105], lnum; n void addline(int from, int to) { n   ed[lnum] = edge(to, head[from]); n   head[from] = lnum++; n } n int ans[105][105]; n void init() { n   memset(head, -1, sizeof(head)); n   memset(ans, 0, sizeof(ans)); n   lnum = 0; n } n int a[105], b[105]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   dic.s = 0, dic.t = 205; n   dic.init(); n   init(); n   int sum1 = 0, sum2 = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum1 += a[i]; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     sum2 += b[i]; n   } n   int A, B; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &A, &B); n     addline(A, B); n     addline(B, A); n   } n   for (int j = 1; j <= n; j++) { n     dic.add(dic.s, j, a[j]); n     dic.add(j + 100, dic.t, b[j]); n     dic.add(j, j + 100, a[j]); n     for (int i = head[j]; ~i; i = ed[i].next) dic.add(j, ed[i].to + 100, a[j]); n   } n   int k = dic.Maxflow(); n   if (k == sum1 && k == sum2) { n     printf( YES n ); n     for (int i = 0; i < dic.lnum; i++) { n       if (dic.ed[i].from != dic.s && dic.ed[i].to != dic.t && n           dic.ed[i].from <= 100 && dic.ed[i].to > 100) { n         ans[dic.ed[i].from][dic.ed[i].to - 100] += dic.ed[i].flow; n       } n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j < n; j++) printf( %d  , ans[i][j]); n       printf( %d n , ans[i][n]); n     } n   } else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int a[200005], w[200005]; n long long qpow(long long x, int n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % int(998244353); n     x = x * x % int(998244353); n     n /= 2; n   } n   return res; n } n long long inv(long long x) { return qpow(x, int(998244353) - 2); } n long long s[2]; n long long NN; n int r[2 * 3005]; n long long d[3005][3005], g[3005][3005]; n long long dfs0(int i, int j) { n   if (d[i][j] != -1) return d[i][j]; n   if (i > M) return d[i][j] = 1; n   long long ans = 0; n   long long p0 = 0, p1 = 0; n   long long deno = r[M + i - 1 - 2 * j]; n   if (s[0] - j - 1 >= 0) p0 = (s[0] - j - 1) * deno % int(998244353); n   if (s[1] + i - 1 - j >= 0) p1 = (s[1] + i - 1 - j) * deno % int(998244353); n   if (p0) ans = p0 * dfs0(i + 1, j + 1) % int(998244353); n   if (p1) ans = (ans + p1 * dfs0(i + 1, j) % int(998244353)) % int(998244353); n   return d[i][j] = ans; n } n long long dfs1(int i, int j) { n   if (g[i][j] != -1) return g[i][j]; n   if (i > M) return g[i][j] = 1; n   long long ans = 0; n   long long p0 = 0, p1 = 0, p2 = 0; n   long long deno = r[M + i - 1 - 2 * j]; n   if (s[0] - j >= 0) p0 = (s[0] - j) * deno % int(998244353); n   if (s[1] + i - 1 - j - 1 >= 0) n     p1 = (s[1] + i - 1 - j - 1) * deno % int(998244353); n   p2 = deno; n   if (p0) ans = p0 * dfs1(i + 1, j + 1) % int(998244353); n   if (p1) ans = (ans + p1 * dfs1(i + 1, j) % int(998244353)) % int(998244353); n   ans = (ans + 2 * p2 % int(998244353) * dfs1(i + 1, j) % int(998244353)) % n         int(998244353); n   return g[i][j] = ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> N >> M; n   for (int i = 1; i <= N; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= N; i++) { n     cin >> w[i]; n     s[a[i]] += w[i]; n   } n   NN = s[0] + s[1]; n   for (int i = -M; i <= M; i++) { n     r[M + i] = inv(NN + i); n   } n   memset(d, -1, sizeof(d)); n   memset(g, -1, sizeof(g)); n   for (int i = 1; i <= N; i++) { n     if (a[i] == 0) cout << w[i] * dfs0(1, 0) % int(998244353) << endl; n     if (a[i] == 1) cout << w[i] * dfs1(1, 0) % int(998244353) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long, vector<long long>, less<long long> > q; n char nm[100005]; n int main() { n   long long a, b, c, d, i, j, k, l; n   long long n; n   while (~scanf( %lld , &n)) { n     c = n / 36; n     d = n % 36; n     a = d / 3; n     b = d % 3; n     if (b > 1) a += 1; n     if (a == 12) { n       a = 0; n       c += 1; n     } n     printf( %lld %lld n , c, a); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, save, left, right; n   string A; n   while (cin >> A) { n     left = 0; n     right = 0; n     for (i = 0; i < A.size(); i++) { n       if (A[i] ==  ^ ) { n         save = i; n       } n     } n     i = save; n     i--; n     while (i != -1) { n       if (A[i] >=  0  && A[i] <=  9 ) left += (A[i] -  0 ) * (save - i); n       i--; n     } n     int i = save; n     i++; n     while (i != A.size()) { n       if (A[i] >=  0  && A[i] <=  9 ) right += (A[i] -  0 ) * (i - save); n       i++; n     } n     if (left > right) n       cout <<  left  << endl; n     else if (right > left) n       cout <<  right  << endl; n     else n       cout <<  balance  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, c, d, s = 0, i; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a >> b >> c >> d; n     s = s + (c - a + 1) * (d - b + 1); n   } n   cout << s; n } n


#include <bits/stdc++.h> n const int N = 157, inf = 1e9 + 500; n int n, m, del, t, dis[N]; n int ans = inf; n std::queue<int> q; n struct node { n   int u, v, w; n   bool operator<(const node &a) const { return w < a.w; } n } e[N]; n inline int read() { n   int x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + c - 48, c = getchar(); n   return x; n } n struct matrix { n   std::bitset<N> v[N]; n   matrix() { n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) v[i][j] = 0; n   } n   void I() { n     for (int i = 1; i <= n; ++i) v[i][i] = 1; n   } n   friend matrix operator*(matrix a, matrix b) { n     matrix ret; n     for (int i = 1; i <= n; ++i) { n       for (int k = 1; k <= n; ++k) { n         if (a.v[i][k]) ret.v[i] |= b.v[k]; n       } n     } n     return ret; n   } n } a, g; n matrix qp(matrix base, int p) { n   matrix ret; n   ret.I(); n   while (p) { n     if (p & 1) ret = ret * base; n     base = base * base; n     p >>= 1; n   } n   return ret; n } n void bfs() { n   while (!q.empty()) q.pop(); n   for (int i = 1; i <= n; ++i) dis[i] = inf; n   for (int i = 1; i <= n; ++i) n     if (a.v[1][i]) q.push(i), dis[i] = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int v = 1; v <= n; ++v) { n       if (g.v[u][v] && dis[v] == inf) { n         dis[v] = dis[u] + 1; n         q.push(v); n       } n     } n   } n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= m; ++i) n     e[i].u = read(), e[i].v = read(), e[i].w = read(); n   std::sort(e + 1, e + 1 + m); n   a.v[1][1] = 1; n   dis[n] = inf; n   for (int i = 1; i <= m; ++i) { n     if (ans < e[i].w) break; n     del = e[i].w - t; n     t = e[i].w; n     a = a * qp(g, del); n     g.v[e[i].u][e[i].v] = 1; n     if (i == m || e[i + 1].w != e[i].w) bfs(); n     ans = std::min(ans, dis[n] + t); n   } n   if (ans == inf) n     puts( Impossible ); n   else n     printf( %d n , ans); 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   vector<pair<long long, long long> > p; n   for (long long i = 0; i < n; i++) { n     double x, y; n     cin >> x >> y; n     p.push_back(make_pair(x, y)); n   } n   set<pair<double, double> > s; n   set<double> e; n   long long c; n   for (long long i = 0; i < n; i++) { n     for (long long j = i + 1; j < n; j++) { n       double x1 = p[i].first; n       double y1 = p[i].second; n       double x2 = p[j].first; n       double y2 = p[j].second; n       if ((x2 - x1) != 0) { n         double m = (y2 - y1) / (x2 - x1); n         double c = y1 - m * (x1); n         s.insert(make_pair(m, c)); n       } else { n         e.insert(x1); n       } n     } n   } n   set<pair<double, double> >::iterator it; n   vector<long long> r; n   long long k = 0; n   long long w = 0; n   set<pair<double, double> >::iterator itr; n   itr = s.begin(); n   it = s.begin(); n   it++; n   while (it != s.end()) { n     long long u = 0; n     pair<double, double> gg = *it; n     pair<double, double> hh = *itr; n     while (gg.first == hh.first) { n       u++; n       it++; n       itr++; n       gg = *it; n       hh = *itr; n     } n     if (u != 0) { n       w = w + ((u + 1) * u) / 2; n     } n     it++; n     itr++; n   } n   c = e.size(); n   long long total = s.size() + c; n   total = (total * (total - 1)) / 2; n   w = w + ((c - 1) * c) / 2; n   cout << (total - w); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int a[maxn], used[maxn], change[maxn]; n int n, m, odd, even, sz, ans; n int o = 0, e = 0; n map<int, int> id; n int num[maxn]; n int main(void) { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] % 2 == 0) n       even++; n     else n       odd++; n     if (!id[a[i]]) id[a[i]] = ++sz; n     num[id[a[i]]]++; n   } n   for (int i = 1; i <= n; i++) { n     if (num[id[a[i]]] > 1) { n       change[i] = true; n       num[id[a[i]]]--; n       if (a[i] % 2 == 0) n         even--; n       else n         odd--; n       ans++; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!change[i]) { n       if (odd > n / 2 && a[i] % 2 == 1) { n         change[i] = true; n         num[id[a[i]]]--; n         odd--; n         ans++; n       } else if (even > n / 2 && a[i] % 2 == 0) { n         change[i] = true; n         num[id[a[i]]]--; n         even--; n         ans++; n       } n     } n   for (int i = 1; i <= n; i++) n     if (change[i]) { n       if (odd < n / 2) { n         if (o == 0) o = 1; n         while (id[o]) o += 2; n         a[i] = o; n         id[o] = ++sz; n         odd++; n       } else if (even < n / 2) { n         if (e == 0) e = 2; n         while (id[e]) e += 2; n         a[i] = e; n         id[e] = ++sz; n         even++; n       } n     } n   if (o > m || e > m) n     printf( -1 n ); n   else { n     printf( %d n , ans); n     for (int i = 1; i <= n; i++) { n       printf( %d  , a[i]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int op, d; n int main() { n   int nc = 0, bst = 0; n   ; n   for (cin >> op >> d; d--;) { n     string s; n     cin >> s; n     int c = 0; n     for (int i = 0; i < s.size(); ++i) n       if (s[i] ==  1 ) ++c; n     if (c == op) n       nc = 0; n     else n       ++nc; n     bst = max(nc, bst); n   } n   cout << bst; n } n


#include <bits/stdc++.h> n using namespace std; n int n, num, _ = 0; n int a[1005]; n int main() { n   cin >> n; n   map<int, int> mp; n   for (int i = 0; i < 100000; i++) _++; n   int m = 0; n   for (int i = 0; i < 100000; i++) _++; n   for (int i = 0; i < n; i++) { n     int num; n     cin >> num; n     if (mp[num] == 0) { n       mp[num]++; n       a[m++] = num; n     } n   } n   for (int i = 0; i < 100000; i++) _++; n   sort(a, a + m); n   for (int i = 0; i < 100000; i++) _++; n   if (m == 1) n     cout << 0 << endl; n   else if (m == 2) { n     if ((a[1] - a[0]) % 2 == 1) n       cout << a[1] - a[0] << endl; n     else n       cout << (a[1] - a[0]) / 2 << endl; n   } else if (m == 3) { n     if (a[2] - a[1] == a[1] - a[0]) n       cout << a[1] - a[0] << endl; n     else n       cout <<  -1  << endl; n   } else n     cout <<  -1  << endl; n   for (int i = 0; i < 100000; i++) _++; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-8; n const int mxn = 100033; n const int mod = 1e9 + 7; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) f = c ==  -  ? -1 : 1, c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n inline long long gcd(long long a, long long b) { n   return b == 0 ? a : gcd(b, a % b); n } n long long ksm(long long a, long long b, long long mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } n void exgcd(long long a, long long b, long long &x, long long &y, long long &d) { n   if (!b) { n     d = a; n     x = 1; n     y = 0; n   } else { n     exgcd(b, a % b, y, x, d); n     y -= x * (a / b); n   } n } n int dx[] = {0, 1, 0, -1}; n int dy[] = {1, 0, -1, 0}; n int n, a[1010]; n map<int, int> ct; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     int flg = 0; n     ct.clear(); n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n       ct[a[i]]++; n       if (ct[a[i]] >= 2) flg = 1; n     } n     if (flg) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(2e5) + 99; n const int INF = int(1e9) + 100; n int t; n int n; n long long s; n pair<int, int> p[N]; n bool ok(int mid) { n   long long sum = 0; n   int cnt = 0; n   vector<int> v; n   for (int i = 0; i < n; ++i) { n     if (p[i].second < mid) n       sum += p[i].first; n     else if (p[i].first >= mid) { n       sum += p[i].first; n       ++cnt; n     } else n       v.push_back(p[i].first); n   } n   assert(is_sorted(v.begin(), v.end())); n   int need = max(0, (n + 1) / 2 - cnt); n   if (need > v.size()) return false; n   for (int i = 0; i < v.size(); ++i) { n     if (i < v.size() - need) n       sum += v[i]; n     else n       sum += mid; n   } n   return sum <= s; n } n int main() { n   scanf( %d , &t); n   for (int tc = 0; tc < t; ++tc) { n     scanf( %d %lld , &n, &s); n     for (int i = 0; i < n; ++i) scanf( %d %d , &p[i].first, &p[i].second); n     sort(p, p + n); n     int lf = 1, rg = INF; n     while (rg - lf > 1) { n       int mid = (lf + rg) / 2; n       if (ok(mid)) n         lf = mid; n       else n         rg = mid; n     } n     printf( %d n , lf); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   fast(); n   int t, b, p, f, h, c; n   cin >> t; n   while (t--) { n     cin >> b >> p >> f; n     cin >> h >> c; n     int bun = b / 2; n     if (bun >= p + f) { n       cout << p * h + f * c << endl; n     } else { n       if (c > h) { n         if (bun >= f) { n           cout << f * c + (bun - f) * h << endl; n         } else { n           cout << bun * c << endl; n         } n       } else { n         if (bun >= p) { n           cout << p * h + (bun - p) * c << endl; n         } else { n           cout << bun * h << endl; n         } n       } n     } n   } n   return 0; n } n


n #include <bits/stdc++.h> n using namespace std; n typedef long long ll; n #define PI 3.14159265 n #define pb push_back n #define en cout<<  n  n #define vec vector<ll> n #define vecs vector<string> n #define pii pair<ll,ll> n #define VP vector< pii > n #define setbits(x)  __builtin_popcountll(x) n #define zrobits(x)  __builtin_ctzll(x) n #define all(v) (v).begin(),(v).end() n #define ff first n #define ss second n #define mm     n #define MM     n #define mll map<ll,ll> n #define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++) n #define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--) n #define fa(x,v) for(auto x:v) n #define INF (ll)(9e18) n #define Sort(v) sort(v.begin(),v.end()) n #define rsort(v) sort(v.rbegin(),v.rend()) n #define test ll T;cin>>T;while(T--) n #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n #define db1(x) cout<<#x<< = <<x<<  n  n #define db2(x,y) cout<<#x<< = <<x<< , <<#y<< = <<y<<  n  n #define db3(x,y,z) cout<<#x<< = <<x<< , <<#y<< = <<y<< , <<#z<< = <<z<<  n  n #define db4(x,y,z,w) cout<<#x<< = <<x<< , <<#y<< = <<y<< , <<#z<< = <<z<< , <<#w<< = <<w<<  n  n #define max3(a,b,c) max(a,max(b,c)) n #define min3(a,b,c) min(a,min(b,c)) n #define max4(a,b,c,d) max(a,max3(b,c,d)) n #define min4(a,b,c,d) min(a,min3(b,c,d)) n #define maxi(a) *max_element(a,a+n) n #define mini(a) *min_element(a,a+n) n #define no cout <<  NO  << endl n #define yes cout <<  YES  << endl n #define no2 cout <<  No  << endl n #define yes2 cout <<  Yes  << endl n #define mxheap    priority_queue<int> n #define mnheap    priority_queue<int,vector<int>,greater<int>> n #define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin()) n #define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin()) n  const ll MOD=1000000000+7; n const ll MOD1 = 998244353; n  n string itos(ll s) n { n string str=to_string(s); n return str; n } n ll stoi2(string s) n { n     ll a= atoi(s.c_str()); n     return a; n } n template <typename T> n void read(T &p) n { n     cin >> p; n } n   n template <typename T, typename T1> n void read(pair<T, T1> &p) n { n     read(p.ff); n     read(p.ss); n } n   n template <typename T> n void read(T arr[], ll n) n { n     for (ll i = 0; i < n; i++) n     { n         read(arr[i]); n     } n } n   n template <typename T> n void read(vector<T> &arr) n { n     for (ll i = 0; i < arr.size(); i++) n     { n         read(arr[i]); n     } n } n ////////////////////////////////////// n   n template <typename T> n void out(T& n) n { n     cout << n; n } n   n template <typename T, typename T1> n void out(pair<T, T1> &p) n { n     cout <<  ( ; n     out(p.ff); n     cout <<  , ; n     out(p.ss); n     cout <<  ) ; n     cout<<endl; n } n   n template <typename T> n void out(T arr[], ll n) n { n     for (ll i = 0; i < n; i++) n     { n         out(arr[i]); n         cout <<    ; n     } n     cout<<endl; n } n   n template <typename T> n void out(vector<T> &arr) n { n   n     for (ll i = 0; i < arr.size(); i++) n     { n         out(arr[i]); n         if (i!=arr.size()-1) n             cout <<    ; n     } n     cout<<endl; n   n } n   n   n   n template <typename T> n void out(set<T> &arr) n { n     for (auto i:arr) n     { n         out(i); n         cout <<    ; n     } n     cout<<endl; n } n   n template <typename T> n void out(multiset<T> &arr) n { n     for (auto i:arr) n     { n         out(i); n         cout <<    ; n     } n     cout<<endl; n } n   n template <typename T, typename T1> n void out(map<T,T1> &m) n { n     for (auto i:m) n     { n         out(i.first); n         cout<<  ->  ; n         out(i.second); n         cout<< ,  ; n     } n     cout<<endl; n } n ll powerm(ll a, ll b, ll MOD) { n     a %= MOD; n     ll res = 1; n     while (b > 0) { n         if (b & 1) n             res = res * a % MOD; n         a = a * a % MOD; n         b >>= 1; n     } n     return res; n } n  n ll power(ll a, ll b) { n     n   ll res = 1; n     while (b > 0) { n         if (b & 1) n             res = res * a ; n         a = a * a ; n         b >>= 1; n     } n     return res; n } n  n bool isSubSequence(string str1, string str2, int m, int n) n { n  n     if (m == 0) return true; n     if (n == 0) return false; n  n     if (str1[m-1] == str2[n-1]) n         return isSubSequence(str1, str2, m-1, n-1); n  n     return isSubSequence(str1, str2, m, n-1); n } n  n  n bool ispalindrome(ll len,string str) n { n   n     for(int i = 0; i < len/2; i++){ n         if(str[i] != str[len-1-i]){ n             return false; n         } n     } n     return true; n } n inline bool isPowerOfTwo(ll x) n { n     return ((1LL << (ll)log2(x)) == x); n } n ll isprime(ll N){ n     if(N<2 || (!(N&1) && N!=2)) n         return 0; n     for(ll i=3; i*i<=N; i+=2){ n         if(!(N%i)) n             return 0; n     } n     return 1; n } n vec factor; n void factors(ll n) n { n      n for(ll i=1;i<=sqrt(n);i++) n { n     if(n%i==0) n     { n         if(i==n/i) n         factor.pb(i); n         else n         { n             factor.pb(i); n             factor.pb(n/i); n         } n          n     } n } n   n sort(all(factor)); n } n ll gcd(ll a, ll b) {  n    if (b == 0)   n       return a;  n    return gcd(b, a % b);   n } n ll lcm(ll a,ll b) n { n     return (a/gcd(a,b))*b; n } n bool isvalid(ll i,ll j,ll n) n { n     if(i>=0&&i<n&&j>=0&&j<n)return true; n     return false; n } n ll dx[8]={0,1,1,1,0,-1,-1,-1}; n ll dy[8]={-1,-1,0,1,1,1,0,-1}; n  n  n ll binarytodecimal(string n) n { n     string num = n; n     ll dec_value = 0; n   n    ll base = 1; n   n    ll len = num.length(); n     for (int i = len - 1; i >= 0; i--) { n         if (num[i] ==  1 ) n             dec_value += base; n         base = base * 2; n     } n   n     return dec_value; n } n ll extended_GCD(ll a , ll b , ll &x , ll &y) { n     if (a == 0) { n         x = 0; n         y = 1; n         return b; n     } n     ll x1 , y1; n     ll gcd = extended_GCD(b % a , a , x1 , y1); n     x = y1 - (b / a) * x1; n     y = x1; n     return gcd; n } n ll modinv(ll a , ll mod = MOD) { n     ll x , y; n     extended_GCD(a , mod , x , y); n     if (x < 0) x += mod; n     return x; n } n string dtb(ll  n ) n { n  n     string s=  ; n  n     while(n>0){ n  n     if(n%2==0)s.pb( 0 ); n     else s.pb( 1 ); n     n/=2; n  n     } n  n     reverse(all(s)); n //       while(s.size()<siz) n //    { n // n //        s= 0 +s; n // n //    } n     return s; n } n ll getfactorisation(ll n) n { n ll N=5e6+5; n   vector<ll>fact(N); n   for(int i=0;i<N;i++)fact[i]=i; n   for(int i=2;i<N;i+=2)fact[i]=2; n       for (ll i=3;i<= N ;i+= 2) n        if (i==fact[i]) n         for (ll j=i*i ; j<N;j+= i) n          if (fact[j]== j)fact[j] = i; n  n vec res; n   while(n>1) n   { n      res.pb(fact[n]); n       n/=fact[n]; n  n  n   } n   return res.size(); n } n  n long long maxPrimeFactor(long long n)  n {  n     long long maxPrime = -1;  n    n     while (n % 2 == 0) {  n         maxPrime = 2;  n         n >>= 1; // equivalent to n /= 2  n     }  n    n     for (int i = 3; i <= sqrt(n); i += 2) {  n         while (n % i == 0) {  n             maxPrime = i;  n             n = n / i;  n         }  n     }  n    n     if (n > 2)  n         maxPrime = n;  n    n     return maxPrime;  n }  n  n vec seive; n void Seive() { n     const ll maxn = 1e6 + 5; n     seive.resize(maxn); n     f(i,0, maxn) seive[i] = i; n     for(ll i=2;i<=maxn;i+=2) n     seive[i] = 2; n     seive[1] = -1; n     seive[0] = -1; n     for (ll i = 3; i <= maxn; i += 2) n         if (i == seive[i]) n         for (ll j = i * i ; j < maxn ; j += i) n         if (seive[j] == j) n         seive[j] = i; n } n //cout<< Case # <<t<< :  ; n // check integer overflow(s->x+y--s<x) n void factorisation(ll n) n { n vec res; n      for(ll i=2;i*i<=n;i++) { n             if (n%i==0) { n                 while (n%i==0) { n                     n/=i; n                    res.pb(i); n                 } n             } n         } n         if (n>1) n             res.pb(n); n  n } n  int numOfDivisors[100005]={0}; n    void noOfDivisor(){ n     for(int i=1;i<=100005;i++) n         for(int j=i;j<=100005;j+=i){ n             numOfDivisors[j]++; n         } n } n  n ll fac[200005]; n unsigned long long modInverse(unsigned long long n,   n                                             int p) n { n     return powerm(n, p - 2, p); n } n   long long nCrModPFermat(unsigned long long n, n                                  int r, int p) n { n     if (n < r) n         return 0; n     if (r == 0) n         return 1; n  unsigned long long fac[n + 1]; n     fac[0] = 1; n     for (int i = 1; i <= n; i++) n         fac[i] = (fac[i - 1] * i) % p; n   n     return (fac[n] * modInverse(fac[r], p) % p n             * modInverse(fac[n - r], p) % p) n            % p; n } n  n int main() n { n fast; n  n   n  ll T=1; n   cin>>T; n while(T--) n { n   ll n,m,x; n   cin>>n>>m>>x; n   vec v(n,0); n   ll sum=0; n   f(i,0,n)cin>>v[i],sum+=v[i]; n  n   VP p; n   f(i,0,n) n   { n     p.pb({v[i],i}); n   } n   Sort(p); n   reverse(all(p)); n   mll mp; n    ll cur=1; n    for(int i=0;i<n;i++) n    { n         mp[p[i].ss]=cur; n         cur++; n         if(cur>m)cur=1; n    } n    yes; n    f(i,0,n) n     cout<<mp[i]<<MM; n en; n  n  n } n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n      n } n