#include <bits/stdc++.h> n using namespace std; n const long long maxn = 500000 + 5; n long long a[maxn], tmp[maxn]; n multiset<long long> save; n vector<pair<long long, long long> > b; n int main() { n   long long total = 0; n   long long n; n   scanf( %lld , &n); n   for (long long i = 0; i < n; i++) scanf( %lld , a + i), total += a[i]; n   sort(a, a + n, greater<long long>()); n   for (long long i = 0, cnt = 1; i < n; i++, cnt++) n     if (i == n - 1 || a[i] != a[i + 1]) n       b.push_back(make_pair(a[i], cnt)), cnt = 0; n   for (long long i = 0, sizesum = 0; i < (long long)b.size(); i++) { n     long long num = b[i].first, nowsize = b[i].second; n     long long save_size = min(sizesum, sizesum + nowsize >> 1); n     long long cnt = max(0LL, save_size - nowsize); n     for (long long j = save_size - 1; j >= cnt; j--) n       if (j < (long long)save.size()) n         tmp[j] = *save.begin(), save.erase(save.begin()); n       else n         tmp[j] = 0; n     for (long long j = cnt, k = sizesum - cnt; j < k && j < save_size; j++) n       if (tmp[j] < num) n         tmp[j] = num; n       else if (--k < save_size) n         tmp[k] = max(0LL, 2LL * num - tmp[j]); n     save.insert(tmp + cnt, tmp + save_size); n     sizesum += nowsize; n   } n   for (auto it = save.begin(); it != save.end(); it++) total -= (*it); n   printf( %lld n , total); n } n


#include <bits/stdc++.h> n using namespace std; n long long int b[101]; n int main() { n   long long int n; n   cin >> n; n   long long int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[a[i]]++; n   } n   long long int ans = 0; n   for (int i = 1; i < 101; i++) { n     if (b[i] >= ans) { n       ans = b[i]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <stdio.h> n #include <vector> n #include <algorithm> n #include <set> n #define pii pair<int,int> n #define ll long long n using namespace std; n vector<int> edge[200001]; n vector<pii > pt[200001]; n int depth[200001]; n ll d[200001]; n int a[200001],v[200001]; n void dfs(int x) { n  for (auto nxt : edge[x]) { n   depth[nxt] = depth[x] + 1; n   dfs(nxt); n  } n } n int main() { n  int t; n  for (scanf( %d , &t); t--;) { n   int i, n; n   for (scanf( %d , &n), i = 1; i <= n; ++i) { n    edge[i].clear(); n    pt[i].clear(); n    d[i] = 0; n   } n   for (i = 2; i <= n; ++i) { n    scanf( %d ,v+i); n    edge[v[i]].push_back(i); n   } n   depth[1] = 1; n   dfs(1); n   for (i = 2; i <= n; ++i) { n    scanf( %d ,a+i); n    pt[depth[i]].push_back({a[i],i}); n   } n   pt[1].push_back({0,1}); n   for (i = 1; i <= n; ++i) n    sort(pt[i].begin(), pt[i].end()); n   int lev = 2; n   while (pt[lev].size()) { n    multiset < ll > l, r; n    for (auto p : pt[lev]) { n     int x = p.second; n     r.insert(d[v[x]]+a[x]); n    } n    for (auto p : pt[lev]) { n     int x = p.second; n     auto it = r.lower_bound(d[v[x]]+a[x]); n     r.erase(it); n  n     if (r.size()) { n      auto mx = prev(r.end()); n      d[x] = max(d[x], *mx - a[x]); n      int rightmost = pt[lev].back().first; n      d[x] = max(d[x], d[v[x]] + rightmost - a[x]); n     } n     if (l.size()) { n      auto mx = prev(l.end()); n      d[x] = max(d[x], a[x] + *mx); n      int leftmost = pt[lev][0].first; n      d[x] = max(d[x], d[v[x]] + a[x] - leftmost); n     } n     l.insert({d[v[x]]-a[x]}); n    } n    ++lev; n   } n  n   ll ans = 0; n   for (i = 1; i <= n; ++i) { n    if (ans < d[i]) ans = d[i]; n   } n   printf( %I64d n ,ans); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, res1, res2; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   res1 = a[0] ^ a[1]; n   vector<int> st; n   for (int i = 0; i < n; i++) { n     while (!st.empty() && st.back() < a[i]) st.pop_back(); n     st.push_back(a[i]); n     if (st.size() >= 2) res1 = max(res1, st[st.size() - 1] ^ st[st.size() - 2]); n   } n   res2 = a[n - 1] ^ a[n - 2]; n   vector<int> st1; n   for (int i = n - 1; i >= 0; i--) { n     while (!st1.empty() && st1.back() < a[i]) st1.pop_back(); n     st1.push_back(a[i]); n     if (st1.size() >= 2) n       res2 = max(res2, st1[st1.size() - 1] ^ st1[st1.size() - 2]); n   } n   cout << max(res1, res2); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse2 ) n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void solution() { n   int n; n   cin >> n; n   vector<long long> arr(n); n   map<long long, long long> cnt; n   map<long long, long long> freq; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   long long ans = 1; n   for (int i = 1; i < n + 1; i++) { n     cnt[arr[i - 1]]++; n     freq[cnt[arr[i - 1]]]++; n     long long x = cnt[arr[i - 1]]; n     long long y = freq[cnt[arr[i - 1]]]; n     if (x * y == i && i < n) n       ans = i + 1; n     else if (x * y == i - 1) n       ans = i; n   } n   cout << ans <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   for (int i = 1; i < t + 1; i++) { n     solution(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int N, K; n   cin >> N >> K; n   set<int> s; n   for (int i = 0; i < N; i++) { n     int a; n     cin >> a; n     s.insert(a); n   } n   if (s.size() > K) { n     cout << -1 << endl; n     return; n   } n   cout << N * K << endl; n   for (int i = 0; i < N; i++) { n     for (int b : s) cout << b <<    ; n     for (int j = 0; j < K - (int)s.size(); j++) cout << 1 <<    ; n   } n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int a[100005]; n int l[1000], r[1000]; n vector<int> v[100000]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) { n     int s, t; n     scanf( %d%d , &s, &t); n     l[i] = s, r[i] = t; n   } n   int ans = -100000000, anss = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       int mi = a[i], ma = a[j]; n       for (int k = 1; k <= m; k++) { n         if (l[k] <= i && i <= r[k] && (l[k] > j || r[k] < j)) n           mi--, v[(i - 1) * 300 + j].push_back(k); n       } n       if (ans < ma - mi) { n         ans = ma - mi; n         anss = (i - 1) * 300 + j; n       } n     } n   } n   printf( %d n , ans); n   printf( %d n , v[anss].size()); n   for (int i = 0; i < v[anss].size(); i++) printf( %d  , v[anss][i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long b, long long e, long long m) { n   long long p = 1; n   while (e > 0) { n     if (e & 1) p = (p * b) % m; n     e = e >> 1; n     b = (b * b) % m; n   } n   return p; n } n long long cnt[16] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0}; n long long getAns(long long n) { n   long long ans = 0; n   if (n == 0) return cnt[0]; n   while (n) { n     ans += cnt[n % 16]; n     n /= 16; n   } n   return ans; n } n int main() { n   long long n, i = 0, j, m, h, h1, h2, q, k, l, r, ans; n   scanf( %lld , &n); n   printf( %lld n , getAns(n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum[705][705], a[500100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q, t, x, y; n   memset(sum, 0, sizeof(sum)); n   cin >> q; n   while (q--) { n     cin >> t >> x >> y; n     if (t == 1) { n       a[x] += y; n       for (int i = 1; i < 705; ++i) { n         sum[i][x % i] += y; n       } n     } else { n       if (x < 705) { n         cout << sum[x][y] << endl; n       } else { n         int ans = 0; n         for (int i = y; i < 500100; i += x) ans += a[i]; n         cout << ans << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int N = 300001; n int dp[82][82][82][82], n, k, m, u, v, c; n vector<pair<int, int> > g[83]; n int calc(int idx, int l, int r, int rem) { n   if (rem == 0) return 0; n   if (l > r) return 1e9; n   int &ret = dp[idx][l][r][rem]; n   if (ret != -1) return ret; n   ret = 1e9; n   for (int i = 0; i < g[idx].size(); ++i) { n     int v = g[idx][i].first; n     int cost = g[idx][i].second; n     if (v < l) continue; n     if (v > r) continue; n     ret = min(ret, cost + calc(v, v + 1, r, rem - 1)); n     ret = min(ret, cost + calc(v, l, v - 1, rem - 1)); n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof dp); n   scanf( %d%d , &n, &k); n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d%d , &u, &v, &c); n     if (u == v) continue; n     g[u].push_back(make_pair(v, c)); n   } n   int ret = 1e9; n   for (int i = 1; i <= n; ++i) { n     ret = min(ret, calc(i, i + 1, n, k - 1)); n     ret = min(ret, calc(i, 1, i - 1, k - 1)); n   } n   if (ret == 1e9) ret = -1; n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<string, int>> v(n); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     v[i].first = s; n     v[i].second = i + 1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (j % 2 == 1) { n         v[i].first[j] = (char)( Z  - v[i].first[j]); n       } n     } n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     cout << v[i].second <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long FastExp(long long a, long long b, long long c) { n   if (b == 1) return a; n   long long ans = FastExp(a, b / 2, c); n   if (b % 2 == 0) return ans % c * ans % c; n   if (b % 2 != 0) return ans % c * ans % c * a % c; n } n int main() { n   int n; n   cin >> n; n   int a[n + 5]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (i == a[a[a[i]]]) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int mas[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; n   int m, n; n   bool ans = false; n   cin >> n >> m; n   for (int i = 0; i < 15; i++) { n     if (mas[i] == n) { n       if (mas[i + 1] == m) { n         ans = true; n         break; n       } n     } n   } n   if (ans) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std::chrono; n using namespace std; n const int mod = 1e9 + 7; n string s; n int op[501], cl[501]; n int sop[501], scl[501]; n int n; n void f() { n   memset(op, 0, sizeof(op)); n   memset(cl, 0, sizeof(cl)); n   if (s[0] ==  ( ) n     op[0]++; n   else n     cl[0]++; n   memset(sop, 0, sizeof(sop)); n   memset(scl, 0, sizeof(scl)); n   if (s[n - 1] ==  ( ) n     sop[n - 1]++; n   else n     scl[n - 1]++; n   for (int i = n - 2; i >= 0; --i) { n     if (s[i] ==  ) ) { n       sop[i] = sop[i + 1]; n       scl[i] = scl[i + 1] + 1; n     } else { n       if (scl[i + 1]) { n         sop[i] = sop[i + 1]; n         scl[i] = scl[i + 1] - 1; n       } else { n         sop[i] = sop[i + 1] + 1; n         scl[i] = scl[i + 1]; n       } n     } n   } n   for (int i = 1; i < n; ++i) { n     if (s[i] ==  ( ) { n       op[i] = op[i - 1] + 1; n       cl[i] = cl[i - 1]; n     } else { n       if (op[i - 1]) { n         op[i] = op[i - 1] - 1; n         cl[i] = cl[i - 1]; n       } else { n         op[i] = op[i - 1]; n         cl[i] = cl[i - 1] + 1; n       } n     } n   } n } n void solve() { n   cin >> n; n   cin >> s; n   int x1 = 1, x2 = 1, mx = 0, c = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = i; j < n; ++j) { n       int c = 0; n       swap(s[i], s[j]); n       f(); n       for (int k = 0; k < n; ++k) { n         int open; n         open = 0; n         if (k + 1 < n) open = sop[k + 1]; n         if (cl[k] == open && op[k] == 0) { n           if (k + 1 < n) { n             if (scl[k + 1] == 0) c++; n           } else n             c++; n         } n       } n       swap(s[i], s[j]); n       if (mx < c) { n         mx = c; n         x1 = i + 1; n         x2 = j + 1; n       } n     } n   } n   cout << mx << endl; n   cout << x1 <<     << x2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100100]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   set<int> s; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) s.insert(a[i] - a[i - 1]); n   if (s.size() == 1) n     cout << a[n - 1] + *s.begin() << endl; n   else n     cout << a[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[500000]; n bool visited[500000]; n int n; n unordered_map<int, int> m, p; n vector<pair<int, int> > v; n void check(int u) { n   visited[u] = true; n   for (auto v : g[u]) { n     if (!visited[v]) check(v); n   } n } n int main() { n   cin >> n; n   set<int> s; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     m[a] = b; n     v.push_back({a, 0}); n     v.push_back({b, 1}); n     p[a] = i; n     p[b] = i; n   } n   sort(v.begin(), v.end()); n   int count = 0; n   for (int i = 0; i < v.size(); i++) { n     if (v[i].second == 0) { n       for (auto ele : s) { n         if (ele < m[v[i].first]) { n           count++; n           g[p[v[i].first]].push_back(p[ele]); n           g[p[ele]].push_back(p[v[i].first]); n         } else n           break; n       } n       if (count > n - 1) { n         break; n       } n       s.insert(m[v[i].first]); n     } else { n       s.erase(v[i].first); n     } n   } n   if (count == n - 1) { n     check(0); n     int i; n     for (i = 0; i < n; i++) { n       if (!visited[i]) break; n     } n     if (i == n) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n struct ppi { n   long long max; n   int id; n   int w; n } pp1; n struct pi { n   int le; n   int ri; n   int max; n   int id; n } pp[maxn << 2]; n void build(int tot, int le, int ri) { n   pp[tot].le = le; n   pp[tot].ri = ri; n   pp[tot].max = -1000000001; n   if (le == ri) return; n   build(2 * tot, le, (le + ri) / 2); n   build(2 * tot + 1, (le + ri) / 2 + 1, ri); n } n void merg(int tot, int p, int k, int id) { n   if (pp[tot].le == pp[tot].ri) { n     if (pp[tot].max < k) { n       pp[tot].max = k; n       pp[tot].id = id; n     } n     return; n   } n   int mid = (pp[tot].le + pp[tot].ri) / 2; n   if (p <= mid) { n     merg(2 * tot, p, k, id); n   } else n     merg(2 * tot + 1, p, k, id); n   if (pp[2 * tot].max > pp[2 * tot + 1].max) { n     pp[tot].max = pp[2 * tot].max; n     pp[tot].id = pp[2 * tot].id; n   } else { n     pp[tot].max = pp[2 * tot + 1].max; n     pp[tot].id = pp[2 * tot + 1].id; n   } n } n ppi query(int tot, int l, int r) { n   if (pp[tot].le >= l && pp[tot].ri <= r) { n     pp1.max = pp[tot].max; n     pp1.id = pp[tot].id; n     return pp1; n   } n   int mid; n   mid = (pp[tot].le + pp[tot].ri) / 2; n   ppi pp2; n   pp2.max = -1000000001; n   if (l <= mid) { n     pp1 = query(2 * tot, l, r); n     if (pp1.max > pp2.max) { n       pp2.max = pp1.max; n       pp2.id = pp1.id; n     } n   } n   if (r > mid) { n     pp1 = query(2 * tot + 1, l, r); n     if (pp1.max > pp2.max) { n       pp2.max = pp1.max; n       pp2.id = pp1.id; n     } n   } n   return pp2; n } n struct pppi { n   int l; n   int r; n   int c; n   int id; n   int ii; n } pp3[maxn], pp4[maxn], pp5[maxn << 1]; n int a[maxn]; n int cmp(pppi a, pppi b) { n   if (a.l != b.l) return a.l < b.l; n   return a.id > b.id; n } n int cmp1(pppi a, pppi b) { n   if (a.r != b.r) return a.r < b.r; n   return a.id < b.id; n } n int cmp2(pppi a, pppi b) { n   if (a.r != b.r) return a.r < b.r; n   return a.id > b.id; n } n int main() { n   int i, n, m; n   cin >> n >> m; n   int tot = 1; n   ppi pp6; n   pp6.max = -10000; n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &pp3[i].l, &pp3[i].r); n     a[i] = pp3[i].r; n     pp3[i].id = 1; n     pp3[i].ii = i; n     pp5[tot] = pp3[i]; n     tot++; n   } n   sort(a + 1, a + 1 + n); n   for (i = 1; i <= m; i++) { n     scanf( %d%d%d , &pp4[i].l, &pp4[i].r, &pp4[i].c); n     pp4[i].id = -1; n     pp4[i].ii = i; n     pp5[tot] = pp4[i]; n     tot++; n   } n   tot--; n   build(1, 1, n); n   sort(pp5 + 1, pp5 + 1 + tot, cmp); n   for (i = 1; i <= tot; i++) { n     if (pp5[i].id == 1) { n       int p = lower_bound(a + 1, a + 1 + n, pp5[i].r) - a; n       merg(1, p, pp5[i].r, pp5[i].ii); n     } else { n       int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; n       ppi pp7; n       if (p >= 1 && p <= n) n         pp7 = query(1, p, n); n       else n         pp7.max = 0; n       if ((min(pp7.max, (long long)pp5[i].r) - pp5[i].l) * pp5[i].c > pp6.max) { n         pp6.max = (min(pp7.max, (long long)pp5[i].r) - pp5[i].l) * pp5[i].c; n         pp6.id = pp7.id; n         pp6.w = pp5[i].ii; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     a[i] = pp3[i].l; n   } n   sort(a + 1, a + 1 + n); n   build(1, 1, n); n   sort(pp5 + 1, pp5 + tot + 1, cmp1); n   for (i = tot; i >= 1; i--) { n     if (pp5[i].id == 1) { n       int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; n       merg(1, p, -pp5[i].l, pp5[i].ii); n     } else { n       int p = lower_bound(a + 1, a + 1 + n, pp5[i].r) - a; n       if (p > n) p--; n       if (p >= 1 && p <= n && a[p] > pp5[i].r) p--; n       ppi pp7; n       if (p >= 1 && p <= n) { n         pp7 = query(1, 1, p); n         pp7.max = -pp7.max; n         if ((pp5[i].r - max((long long)pp5[i].l, pp7.max)) * pp5[i].c > n             pp6.max) { n           pp6.max = (pp5[i].r - max((long long)pp5[i].l, pp7.max)) * pp5[i].c; n           pp6.id = pp7.id; n           pp6.w = pp5[i].ii; n         } n       } n     } n   } n   sort(pp5 + 1, pp5 + 1 + n, cmp2); n   build(1, 1, n); n   for (i = 1; i <= tot; i++) { n     if (pp5[i].id == 1) { n       int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; n       merg(1, p, pp5[i].r - pp5[i].l, pp5[i].ii); n     } else { n       int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; n       if (p >= 1 && p <= n) { n         ppi pp7 = query(1, p, n); n         if (pp7.max * pp5[i].c > pp6.max) { n           pp6.max = pp7.max * pp5[i].c; n           pp6.id = pp7.id; n           pp6.w = pp5[i].ii; n         } n       } n     } n   } n   if (pp6.max >= 0) n     cout << pp6.max << endl; n   else n     cout << 0 << endl; n   if (pp6.max > 0) cout << pp6.id <<     << pp6.w; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100010, TOUR = 1 << 17; n struct tour { n   int val, broj, prop; n   tour(int val, int broj, int prop) : val(val), broj(broj), prop(prop) {} n   tour() {} n } t[2 * TOUR]; n struct event { n   int t, l, r, val; n   event(int t, int l, int r, int val) : t(t), l(l), r(r), val(val) {} n   event() {} n }; n bool operator<(const event &a, const event &b) { return a.t < b.t; } n vector<int> V[MAX]; n vector<event> E; n int disc[MAX], fin[MAX]; n int rje[MAX]; n int vr = 0; n void Propagiraj(int x) { n   for (int i = 2 * x; i <= 2 * x + 1; i++) { n     t[i].prop += t[x].prop; n     t[i].val += t[x].prop; n   } n   t[x].prop = 0; n } n tour Merge(tour l, tour r) { n   if (l.val < r.val) return tour(l.val, l.broj, 0); n   if (r.val < l.val) return tour(r.val, r.broj, 0); n   return tour(l.val, l.broj + r.broj, 0); n } n void Stavi(int pos, int lo, int hi, int begin, int end, int val) { n   if (lo >= end || hi <= begin) return; n   if (lo >= begin && hi <= end) { n     t[pos].val += val; n     t[pos].prop += val; n     return; n   } n   Propagiraj(pos); n   Stavi(2 * pos + 0, lo, (lo + hi) / 2, begin, end, val); n   Stavi(2 * pos + 1, (lo + hi) / 2, hi, begin, end, val); n   t[pos] = Merge(t[2 * pos], t[2 * pos + 1]); n } n void Dfs(int node, int prosli) { n   disc[node] = vr++; n   for (int i = 0; i < (int)V[node].size(); i++) { n     int nn = V[node][i]; n     if (nn != prosli) Dfs(nn, node); n   } n   fin[node] = vr; n } n void Init(int n) { n   for (int i = 0; i < n; i++) t[i + TOUR] = tour(0, 1, 0); n   for (int i = n; i < TOUR; i++) t[i + TOUR] = tour(1e9, 0, 0); n   for (int i = TOUR - 1; i; i--) t[i] = Merge(t[2 * i], t[2 * i + 1]); n } n int main() { n   int n, brq; n   scanf( %d%d , &n, &brq); n   Init(n); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     V[a].push_back(b); n     V[b].push_back(a); n   } n   Dfs(0, -1); n   for (int i = 0; i < brq; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     E.push_back(event(disc[a], disc[a], fin[a], 1)); n     E.push_back(event(fin[a], disc[a], fin[a], -1)); n     E.push_back(event(disc[b], disc[a], fin[a], 1)); n     E.push_back(event(fin[b], disc[a], fin[a], -1)); n     E.push_back(event(disc[a], disc[b], fin[b], 1)); n     E.push_back(event(fin[a], disc[b], fin[b], -1)); n     E.push_back(event(disc[b], disc[b], fin[b], 1)); n     E.push_back(event(fin[b], disc[b], fin[b], -1)); n   } n   sort(E.begin(), E.end()); n   for (int i = 0, ind = 0; i < n; i++) { n     for (; ind < (int)E.size() && E[ind].t == i; ind++) { n       event ev = E[ind]; n       Stavi(1, 0, TOUR, ev.l, ev.r, ev.val); n     } n     if (t[1].val) n       rje[i] = n - 1; n     else n       rje[i] = max(0, n - t[1].broj - 1); n   } n   for (int i = 0; i < n; i++) printf( %d  , rje[disc[i]]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long mod = 1000000007; n const long long INF = mod * mod; n const long double eps = 1e-12; n const long double pi = acosl(-1.0); n long long mod_pow(long long x, long long n, long long m = mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % m; n     x = x * x % m; n     n >>= 1; n   } n   return res; n } n struct modint { n   long long n; n   modint() : n(0) { ; } n   modint(long long m) : n(m) { n     if (n >= mod) n       n %= mod; n     else if (n < 0) n       n = (n % mod + mod) % mod; n   } n   operator int() { return n; } n }; n bool operator==(modint a, modint b) { return a.n == b.n; } n modint operator+=(modint& a, modint b) { n   a.n += b.n; n   if (a.n >= mod) a.n -= mod; n   return a; n } n modint operator-=(modint& a, modint b) { n   a.n -= b.n; n   if (a.n < 0) a.n += mod; n   return a; n } n modint operator*=(modint& a, modint b) { n   a.n = ((long long)a.n * b.n) % mod; n   return a; n } n modint operator+(modint a, modint b) { return a += b; } n modint operator-(modint a, modint b) { return a -= b; } n modint operator*(modint a, modint b) { return a *= b; } n modint operator^(modint a, long long n) { n   if (n == 0) return modint(1); n   modint res = (a * a) ^ (n / 2); n   if (n % 2) res = res * a; n   return res; n } n long long inv(long long a, long long p) { n   return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); n } n modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } n int max_n; n const int mn = 100000; n struct edge { n   int to, cap; n   long long cost; n   int rev; n }; n vector<edge> G[mn]; n pair<int, int> par[mn]; n long long dist[mn]; n void add_edge(int from, int to, int cap, long long cost) { n   G[from].push_back({to, cap, cost, (int)G[to].size()}); n   G[to].push_back({from, 0, -cost, (int)G[from].size() - 1}); n   max_n = max({max_n, from + 1, to + 1}); n } n void add_edge2(int from, int to, int cap, long long cost) { n   G[from].push_back({to, cap, cost, -1}); n   max_n = max({max_n, from + 1, to + 1}); n } n pair<long long, long long> minimum_road(int s, int t) { n   fill(par, par + max_n, pair<int, int>{-1, -1}); n   fill(dist, dist + max_n, INF); n   dist[s] = 0; n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       q; n   q.push({0, s}); n   while (!q.empty()) { n     pair<long long, long long> p = q.top(); n     q.pop(); n     int id = p.second; n     if (id == t) continue; n     if (p.first > dist[id]) continue; n     for (int j = 0; j < G[id].size(); j++) { n       if (G[id][j].cap > 0) { n         int to = G[id][j].to; n         long long nd = p.first + G[id][j].cost; n         if (nd < dist[to]) { n           dist[to] = nd; n           par[to] = {id, j}; n           q.push({dist[to], to}); n         } n       } n     } n   } n   int cur = t; n   int f = mod; n   while (cur != s) { n     int p = par[cur].first, j = par[cur].second; n     if (p < 0) return {-1, -1}; n     f = min(f, G[p][j].cap); n     cur = p; n   } n   cur = t; n   while (cur != s) { n     int p = par[cur].first, j = par[cur].second; n     if (p < 0) return {-1, -1}; n     G[p][j].cap -= f; n     if (G[p][j].rev >= 0) { n       G[cur][G[p][j].rev].cap += f; n     } n     cur = p; n   } n   return {dist[t], f}; n } n long long minimum_cost_flow(int s, int t, int k = mod) { n   long long ret = 0; n   for (int i = 0; i < k; i++) { n     pair<long long, long long> z = minimum_road(s, t); n     if (z.first < 0) return ret; n     if (k - i <= z.second) { n       ret += z.first * (k - i); n       break; n     } n     i += z.second - 1; n     ret += z.first * z.second; n   } n   return ret; n } n void solve() { n   int n, m; n   cin >> n >> m; n   vector<int> d(n + 2 * m); n   for (int i = 0; i < m; i++) { n     int a, b, c, f; n     cin >> a >> b >> c >> f; n     a--; n     b--; n     int le = n + 2 * i; n     int ri = n + 2 * i + 1; n     add_edge(a, le, mod, 1); n     add_edge(le, a, f, 1); n     add_edge(ri, b, mod, 1); n     add_edge(b, ri, f, 1); n     add_edge(le, ri, c, 0); n     add_edge(le, ri, mod, 2); n     d[a] += -f; n     d[le] += f; n     d[ri] += -f; n     d[b] += f; n   } n   int sum = 0; n   int s = n + 2 * m; n   int t = s + 1; n   int sum2 = 0; n   int S = t + 1; n   int T = S + 1; n   for (int i = 0; i < n + 2 * m; i++) { n     if (d[i] > 0) { n       sum += abs(d[i]); n       add_edge(s, i, d[i], 0); n     } else if (d[i] < 0) { n       sum2 += abs(d[i]); n       add_edge(i, t, -d[i], 0); n     } n   } n   max_n = T + 1; n   add_edge(n - 1, 0, mod, 0); n   int ans = 0; n   ans += minimum_cost_flow(s, t, sum); n   cout << ans / 2 <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000 + 10; n int n, m, k; n int a[N], b[N]; n char s[N], t[N]; n int p[22]; n int mn[1 << 20], mx[1 << 20]; n int getint(char *str) { n   char news[22]; n   for (int i = 1; i <= k; i++) { n     news[p[i]] = str[i]; n   } n   int ans = 0; n   for (int i = 1; i <= k; i++) { n     ans = ans << 1 | (news[i] ==  1 ); n   } n   return ans; n } n void updmx(int &x, int y) { x = max(x, y); } n void updmn(int &x, int y) { x = min(x, y); } n int bitcount(int x) { n   int ans = 0; n   for (int i = 0; i < k; i++) n     if (x >> i & 1) ans++; n   return ans; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   scanf( %s%s , s + 1, t + 1); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i], &b[i]); n   } n   for (int i = 0; i < 1 << k; i++) mn[i] = 1e9, mx[i] = -1e9; n   int all = (1 << k) - 1; n   for (int i = 1; i <= k; i++) p[i] = i; n   updmx(mx[getint(t) ^ all], 0); n   for (int i = 1; i <= n; i++) { n     swap(p[a[i]], p[b[i]]); n     updmx(mx[getint(t) ^ all], i); n   } n   for (int i = 1; i <= k; i++) p[i] = i; n   updmn(mn[getint(s) ^ all], 0); n   for (int i = 1; i <= n; i++) { n     swap(p[a[i]], p[b[i]]); n     updmn(mn[getint(s) ^ all], i); n   } n   for (int bit = 0; bit < k; bit++) { n     for (int i = 1; i < (1 << k); i++) { n       if (((i >> bit) & 1) == 1) { n         mx[i] = max(mx[i], mx[i ^ (1 << bit)]); n         mn[i] = min(mn[i], mn[i ^ (1 << bit)]); n       } n     } n   } n   int bstval = -1, bstL, bstR; n   for (int i = 0; i < 1 << k; i++) { n     if (mx[all ^ i] - mn[all ^ i] >= m) { n       if (bitcount(i) >= bstval) { n         bstval = bitcount(i); n         bstR = mx[all ^ i]; n         bstL = mn[all ^ i] + 1; n       } n     } n   } n   int s1 = 0, t1 = 0; n   for (int i = 1; s[i]; i++) s1 += (s[i] ==  1  ? 1 : 0); n   for (int i = 1; t[i]; i++) t1 += (t[i] ==  1  ? 1 : 0); n   printf( %d n , k + 2 * bstval - s1 - t1); n   printf( %d %d n , bstL, bstR); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n int n, f[5010][5010][2], out[5010], siz[5010], size, rt = 1; n struct node { n   int u, v, nxt; n } e[5010 << 1]; n int head[5010], js; n void add(int u, int v) { n   e[++js] = (node){u, v, head[u]}; n   head[u] = js; n } n void dfs1(int u, int pre) { n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     out[u]++; n     if (v == pre) continue; n     dfs1(v, u); n   } n   size += out[u] == 1; n } n void dfs2(int u, int pre) { n   if (out[u] == 1) { n     siz[u] = 1; n     f[u][0][0] = f[u][1][1] = 0; n     return; n   } else n     f[u][0][0] = f[u][0][1] = 0; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == pre) continue; n     dfs2(v, u); n     siz[u] += siz[v]; n     for (int j = siz[u]; j >= 0; j--) { n       int tmp0 = 0x7fffffff, tmp1 = 0x7fffffff; n       for (int k = 0; k <= siz[v]; k++) n         if (k <= j) { n           tmp0 = min(tmp0, f[u][j - k][0] + min(f[v][k][0], f[v][k][1] + 1)); n           tmp1 = min(tmp1, f[u][j - k][1] + min(f[v][k][1], f[v][k][0] + 1)); n         } n       f[u][j][0] = tmp0; n       f[u][j][1] = tmp1; n     } n   } n } n int main() { n   n = read(); n   memset(f, 63, sizeof f); n   for (int i = 1; i < n; i++) { n     int x = read(), y = read(); n     add(x, y), add(y, x); n   } n   dfs1(1, 0); n   while (out[rt] == 1) rt++; n   dfs2(rt, 0); n   printf( %d , min(f[rt][size >> 1][0], f[rt][size >> 1][1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, a[110]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     for (int i = 0; i < n; i++) a[i] %= 2; n     int sum = 0; n     for (int i = 0; i < n; i++) sum += a[i]; n     if (sum == 0 || sum == n) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8, pi = acos(-1); n const int SZ = 100050, INF = 0x7FFFFFFF; n vector<int> mp[SZ]; n int res, pos; n int in[SZ]; n void dfs(int u, int par) { n   int cur = 0; n   for (int i = 0; i < mp[u].size(); ++i) { n     int to = mp[u][i]; n     if (to != par) { n       ++cur; n       dfs(to, u); n     } n   } n   if (par == -1 && cur == 2) --cur; n   if (cur > 1) { n     pos = u; n     ++res; n   } n } n int main() { n   std::ios::sync_with_stdio(0); n   int casenum; n   { n     int n; n     cin >> n; n     for (int i = 0; i < n - 1; ++i) { n       int u, v; n       cin >> u >> v; n       mp[u].push_back(v); n       mp[v].push_back(u); n       ++in[u]; n       ++in[v]; n     } n     dfs(1, -1); n     if (res > 1) n       cout <<  No  << endl; n     else if (res == 0) { n       cout <<  Yes n1  << endl; n       vector<int> tmp; n       for (int i = 1; i <= n; ++i) { n         if (in[i] == 1) { n           tmp.push_back(i); n         } n       } n       cout << tmp[0] <<     << tmp[1] << endl; n     } else { n       cout <<  Yes  << endl; n       int num = count(in + 1, in + 1 + n, 1); n       cout << num << endl; n       for (int i = 1; i <= n; ++i) { n         if (in[i] == 1) cout << i <<     << pos << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int len, rt, n, m; n vector<int> g[210]; n int a[210], b[210]; n void dfs(int cur, int pre, int dep) { n   int i; n   if (dep > len) { n     len = dep; n     rt = cur; n   } n   for (i = 0; i < g[cur].size(); i++) { n     int u = g[cur][i]; n     if (u == pre) continue; n     dfs(u, cur, dep + 1); n   } n } n int solve(int root) { n   rt = root; n   len = 0; n   dfs(root, root, 0); n   len = 0; n   dfs(rt, rt, 0); n   return len; n } n void Init() { n   for (int i = 1; i <= n; ++i) g[i].clear(); n } n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 1; i < n; i++) scanf( %d%d , &a[i], &b[i]); n   int ans = 0; n   for (i = 1; i < n; i++) { n     Init(); n     int x = a[i], y = b[i]; n     for (j = 1; j < n; j++) n       if (i != j) { n         g[a[j]].push_back(b[j]); n         g[b[j]].push_back(a[j]); n       } n     ans = max(ans, solve(x) * solve(y)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int n; n int fa[1000005], huan[1000005], zihuan[1000005], siz[1000005]; n int find(int now) { return fa[now] == now ? now : fa[now] = find(fa[now]); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 2 * n; i++) { n     fa[i] = i; n     siz[i] = 1; n   } n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     int x1 = find(x), y1 = find(y); n     if (x == y) zihuan[x1] = 1; n     if (x1 == y1) huan[y1] = 1; n     fa[x1] = y1; n   } n   for (int i = 1; i <= 2 * n; i++) { n     int x = find(i); n     if (x != i) { n       siz[x] += siz[i]; n       huan[x] |= huan[i]; n       zihuan[x] |= zihuan[i]; n     } n   } n   long long ans = 1; n   for (int i = 1; i <= 2 * n; i++) { n     if (find(i) == i) { n       if (zihuan[i]) n         continue; n       else if (huan[i]) n         ans = (ans * 2) % mod; n       else n         ans = (ans * siz[i]) % mod; n     } n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void amin(T &x, const T &y) { n   if (y < x) x = y; n } n template <class T> n inline void amax(T &x, const T &y) { n   if (x < y) x = y; n } n template <class Iter> n void rprintf(const char *fmt, Iter begin, Iter end) { n   for (bool sp = 0; begin != end; ++begin) { n     if (sp) n       putchar(   ); n     else n       sp = true; n     printf(fmt, *begin); n   } n   putchar(  n ); n } n int N; n int A[200011]; n int B[200011]; n using Tuple = tuple<int, int, int>; n Tuple X[100011]; n void MAIN() { n   scanf( %d , &N); n   for (int i = 0, i_len = (N); i < i_len; ++i) scanf( %d , A + i); n   int Q; n   scanf( %d , &Q); n   vector<Tuple> sortedX; n   sortedX.reserve(Q); n   for (int i = 0, i_len = (Q); i < i_len; ++i) { n     int s, t, u; n     scanf( %d%d%d , &s, &t, &u); n     s--; n     u--; n     X[i] = Tuple(s, t, u); n     sortedX.emplace_back(s, t, -1); n   } n   sort(sortedX.begin(), sortedX.end()); n   sortedX.erase(unique(sortedX.begin(), sortedX.end()), sortedX.end()); n   long long sum = 0; n   for (int i = 0, i_len = (N); i < i_len; ++i) sum += A[i]; n   for (int i = 0, i_len = (Q); i < i_len; ++i) { n     int s, t, u; n     tie(s, t, u) = X[i]; n     auto it = lower_bound(sortedX.begin(), sortedX.end(), Tuple(s, t, -1)); n     int s0, t0, u0; n     tie(s0, t0, u0) = *it; n     if (0 <= u0) { n       sum -= max(0, A[u0] - B[u0]); n       B[u0]--; n       sum += max(0, A[u0] - B[u0]); n     } n     if (0 <= u) { n       sum -= max(0, A[u] - B[u]); n       B[u]++; n       sum += max(0, A[u] - B[u]); n     } n     *it = Tuple(s, t, u); n     printf( %lld n , sum); n   } n } n int main() { n   int TC = 1; n   for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int q = 0; q < t; ++q) { n     int n, k; n     cin >> n >> k; n     vector<int> x(n); n     for (int i = 0; i < n; ++i) { n       cin >> x[i]; n     } n     vector<int> p(n, 0); n     for (int i = 1; i < n - 1; ++i) { n       if (x[i] > x[i + 1] && x[i] > x[i - 1]) { n         p[i] = p[i - 1] + 1; n       } else { n         p[i] = p[i - 1]; n       } n     } n     p[n - 1] = p[n - 2]; n     int ans = 0, left = 0; n     for (int l = 0; l + k - 1 < n; ++l) { n       if (p[l + k - 2] - p[l] > ans) { n         ans = p[l + k - 2] - p[l]; n         left = l; n       } n     } n     cout << ans + 1 <<     << left + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } n void solve() { n   long long n, m; n   cin >> n >> m; n   if (n == 1 || m == 1) { n     cout <<  YES  <<   n ; n     return; n   } n   if (n != m) { n     cout <<  NO  <<   n ; n     return; n   } n   if (n == 2) { n     cout <<  YES  <<   n ; n     return; n   } n   cout <<  NO  <<   n ; n } n signed main() { n   cin.tie(NULL), ios_base::sync_with_stdio(false), cout.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   cin >> s; n   vector<string> a, b; n   string t =   ; n   bool if_num = true, if_first = true; n   for (int i = 0; i < (int)s.size(); i++) { n     if (s[i] ==  ,  or s[i] ==  ; ) { n       if (if_num and t !=   ) { n         a.push_back(t); n       } else { n         b.push_back(t); n       } n       t =   ; n       if_num = true; n       if_first = true; n       continue; n     } n     if (s[i] <  0  or s[i] >  9  or n         (s[i] ==  0  and if_first and n          (i != (int)s.size() - 1 and s[i + 1] !=  ;  and s[i + 1] !=  , ))) { n       if_num = false; n     } n     if_first = false; n     t += s[i]; n   } n   if (if_num and t !=   ) { n     a.push_back(t); n   } else { n     b.push_back(t); n   } n   if (a.empty()) { n     cout <<  -  << endl; n   } else { n     cout <<    ; n     for (int i = 0; i < (int)a.size(); i++) { n       cout << a[i]; n       if (i != (int)a.size() - 1) { n         cout <<  , ; n       } n     } n     cout <<     << endl; n   } n   if (b.empty()) { n     cout <<  -  << endl; n   } else { n     cout <<    ; n     for (int i = 0; i < (int)b.size(); i++) { n       cout << b[i]; n       if (i != (int)b.size() - 1) { n         cout <<  , ; n       } n     } n     cout <<     << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a, b; n   int count = 0; n   for (int a = 0; a <= n + m; a++) n     for (int b = 0; b <= n + m; b++) { n       if (a * a + b == n && b * b + a == m) count++; n     } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const int MAX = 505; n int n, grid[MAX][MAX]; n char s[5]; n bool valid(int x, int y) { n   if (x < 1) return false; n   if (y < 1) return false; n   if (x > n) return false; n   if (y > n) return false; n   return true; n } n bool dist(int x1, int y1, int x2, int y2) { n   return ((abs(x1 - x2) + abs(y1 - y2)) >= n - 1); n } n bool cmd(int x1, int y1, int x2, int y2) { n   printf( ? %d %d %d %d n , x1, y1, x2, y2); n   fflush(stdout); n   scanf( %s , s); n   return (s[0] ==  Y ); n } n int main() { n   scanf( %d , &n); n   grid[1][1] = 1; n   grid[n][n] = 1; n   int x = 1, y = 1; n   while (true) { n     int ok = 0; n     if (valid(x, y + 1)) { n       if (dist(x, y + 1, n, n)) { n         if (cmd(x, y + 1, n, n)) { n           grid[x][y + 1] = 1; n           ok = 1; n           y++; n         } n       } n     } n     if (ok) continue; n     if (valid(x + 1, y)) { n       if (dist(x + 1, y, n, n)) { n         if (cmd(x + 1, y, n, n)) { n           grid[x + 1][y] = 1; n           ok = 1; n           x++; n         } n       } n     } n     if (ok) continue; n     break; n   } n   x = n, y = n; n   while (true) { n     int ok = 0; n     if (valid(x - 1, y)) { n       if (dist(x - 1, y, 1, 1)) { n         if (cmd(1, 1, x - 1, y)) { n           grid[x - 1][y] = 1; n           ok = 1; n           x--; n         } n       } n     } n     if (ok) continue; n     if (valid(x, y - 1)) { n       if (dist(x, y - 1, 1, 1)) { n         if (cmd(1, 1, x, y - 1)) { n           grid[x][y - 1] = 1; n           ok = 1; n           y--; n         } n       } n     } n     if (ok) continue; n     break; n   } n   string ans =   ; n   x = 1, y = 1; n   while (true) { n     if (x == n and y == n) break; n     if (grid[x][y + 1]) n       ans +=  R , y++; n     else n       ans +=  D , x++; n   } n   printf( !  ); n   for (int i = 0; i < ans.size(); i++) printf( %c , ans[i]); n   printf(  n ); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int Log[N], bin[21], ls[N], rs[N], root, a[N], n, i, j; n set<int> s[N]; n pair<int, int> mx[N][20]; n long long ans; n void merge(int x, int y) { n   if (s[x].size() < s[y].size()) swap(s[x], s[y]); n   for (auto& z : s[y]) s[x].insert(z); n   s[y].clear(); n } n void dfs(int x) { n   if (!x) return; n   dfs(ls[x]); n   dfs(rs[x]); n   if (s[ls[x]].size() > s[rs[x]].size()) swap(ls[x], rs[x]); n   for (auto& z : s[ls[x]]) n     if (s[rs[x]].count(a[x] - z)) ans++; n   s[x].insert(a[x]); n   merge(x, ls[x]); n   merge(x, rs[x]); n } n int query(int l, int r) { n   int t = Log[r - l + 1]; n   return max(mx[l][t], mx[r - bin[t] + 1][t]).second; n } n int build(int l, int r) { n   if (l > r) return 0; n   int x = query(l, r); n   ls[x] = build(l, x - 1); n   rs[x] = build(x + 1, r); n   return x; n } n int main() { n   for (scanf( %d , &n), i = 1; i <= n; ++i) scanf( %d , a + i); n   for (Log[0] = -1, i = 1; i <= n; ++i) Log[i] = Log[i >> 1] + 1; n   for (bin[0] = 1, i = 1; i < 20; ++i) bin[i] = bin[i - 1] << 1; n   for (i = 1; i <= n; ++i) mx[i][0] = pair<int, int>(a[i], i); n   for (j = 1; j <= Log[n]; ++j) n     for (i = 1; i + bin[j - 1] <= n; ++i) n       mx[i][j] = max(mx[i][j - 1], mx[i + bin[j - 1]][j - 1]); n   root = build(1, n); n   dfs(root); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 510; n long long sum[maxn]; n int main() { n   long long n, k; n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     if (n >= 32) { n       cout <<  YES  n            <<     << n - 1 << endl; n     } else { n       memset(sum, 0, sizeof(sum)); n       int ans = -1; n       long long total = (pow(4, n) - 1) / 3; n       if (k > total) { n         cout <<  NO  << endl; n         continue; n       } n       long long temp = pow(2, n - 1) * 2 - 1, cnt = 0; n       while (temp) { n         sum[++cnt] = 2 * (temp / 2) + 1; n         temp /= 2; n       } n       for (int i = cnt; i >= 1; i--) sum[i] += sum[i + 1]; n       for (int i = 1; i <= cnt; i++) n         if (sum[i] <= k && total - sum[1] >= k - sum[i]) { n           ans = i; n           break; n         } n       if (ans == -1) n         cout <<  NO  << endl; n       else n         cout <<  YES  n              <<     << ans - 1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { solve(); } n void solve() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     priority_queue<int> q; n     int k = s.length(); n     int cur = (s[0] ==  1 ) ? 1 : 0; n     for (int i = 1; i < k; i++) { n       if (s[i] ==  1  && s[i - 1] ==  0 ) { n         cur = 1; n       } n       if (s[i] ==  1  && s[i - 1] ==  1 ) { n         cur++; n       } n       if (s[i] ==  0 ) { n         if (cur != 0) { n           q.push(cur); n           cur = 0; n         } n       } n     } n     if (s[k - 1] ==  1 ) { n       if (cur != 0) { n         q.push(cur); n         cur = 0; n       } n     } n     int ans = 0; n     int qs = q.size(); n     for (int i = 0; i < qs; i++) { n       if ((i & 0x1) == 0) { n         ans += q.top(); n       } else { n       } n       q.pop(); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   float a1, b1, c1, a2, b2, c2; n   cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2; n   if (!b1 && !b2) { n     if (!a1 && !a2 && c1 && c2) { n       cout << 0 << endl; n       return 0; n     } n     if ((!a1 && c1) || (!a1 && c2)) { n       cout << 0 << endl; n       return 0; n     } n     if ((!c1 && !c2) || (!a1 && !a2) || c1 / a1 == c2 / a2) { n       cout << -1 << endl; n       return 0; n     } else { n       cout << 0 << endl; n       return 0; n     } n   } else if ((!b1 && !a1 && !c1) || (!b2 && !a2 && !c2)) { n     cout << -1 << endl; n     return 0; n   } else if ((!b1 && !a1) || (!b2 && !a2)) { n     cout << 0 << endl; n     return 0; n   } else if (!b1 || !b2) { n     cout << 1 << endl; n     return 0; n   } n   if (a1 / b1 == a2 / b2 && c1 / b1 == c2 / b2) n     cout << -1 << endl; n   else if (a1 / b1 == a2 / b2) n     cout << 0 << endl; n   else n     cout << 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s, curr[1000]; n   long long int size = 0, j; n   cin >> j; n   while (j--) { n     cin >> s; n     if (s.compare( pwd ) == 0) { n       for (long long int i = 0; i < size; ++i) { n         cout <<  /  << curr[i]; n       } n       cout <<  /  n            <<   n ; n     } else if (s.compare( cd ) == 0) { n       string temp; n       cin >> s; n       if (s[0] ==  / ) { n         size = 0; n       } n       for (long long int i = 0; i < s.size(); ++i) { n         temp =   ; n         for (; i < s.size(); ++i) { n           if (s[i] ==  / ) break; n           temp += s[i]; n         } n         if (temp.compare( .. ) == 0) n           --size; n         else if (temp.size()) { n           curr[size] = temp; n           ++size; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n const int M = N * 30; n int tree[M][2]; n vector<int> val[M]; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n + 1); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int idx = 0; n   auto insert = [&](int n, int id) { n     int tmp = 0; n     bitset<30> st(n); n     for (int i = 29; i >= 0; i--) { n       if (!tree[tmp][st[i]]) tree[tmp][st[i]] = ++idx; n       tmp = tree[tmp][st[i]]; n       val[tmp].emplace_back(id); n     } n   }; n   for (int i = 1; i <= n; i++) insert(a[i], i); n   auto get_cal = [&](vector<int> &a, vector<int> &b) -> long long { n     long long ans = 0; n     int i = 0, j = 0; n     while (i < a.size() && j < b.size()) { n       if (a[i] < b[j]) n         i++; n       else n         ans += (int)a.size() - i, j++; n     } n     return ans; n   }; n   vector<vector<long long>> ans(30, vector<long long>(2, 0)); n   function<void(int, int)> query = [&](int root, int deep) { n     if (!tree[root][0] && !tree[root][1]) return; n     long long cnt = get_cal(val[tree[root][0]], val[tree[root][1]]); n     ans[deep][0] += cnt; n     ans[deep][1] += n         1LL * val[tree[root][0]].size() * val[tree[root][1]].size() - cnt; n     if (tree[root][0]) query(tree[root][0], deep - 1); n     if (tree[root][1]) query(tree[root][1], deep - 1); n   }; n   query(0, 29); n   int res = 0; n   long long cnt = 0; n   for (int i = 29; i >= 0; i--) { n     if (ans[i][0] > ans[i][1]) n       res += 1 << i, cnt += ans[i][1]; n     else n       cnt += ans[i][0]; n   } n   printf( %lld %d n , cnt, res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n using vpii = vector<pii>; n using vvpii = vector<vpii>; n using vpll = vector<pll>; n using vvpll = vector<vpll>; n using vpdd = vector<pdd>; n using vvpdd = vector<vpdd>; n template <typename T> n void ckmin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void ckmax(T& a, const T& b) { n   a = max(a, b); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n namespace __input { n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n template <class Arg, class... Args> n void re(Arg& first, Args&... rest) { n   re(first); n   re(rest...); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (int((a).size())); i++) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); i++) re(a[i]); n } n }  // namespace __input n using namespace __input; n namespace __output { n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const deque<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T, bool pretty = true> n void prContain(const T& x) { n   if (pretty) pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0; n   if (pretty) pr( } ); n } n template <class T> n void pc(const T& x) { n   prContain<T, false>(x); n   pr(  n ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const deque<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg> n void ps(const Arg& first) { n   pr(first); n   ps(); n } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n }  // namespace __output n using namespace __output; n namespace __numeric { n template <typename T> n typename enable_if<is_integral<T>::value, T>::type floor(T n, T d) { n   assert(d != 0); n   if (d < 0) tie(n, d) = make_pair(-n, -d); n   return n / d - ((n < 0) && (n % d)); n } n template <typename T> n typename enable_if<is_integral<T>::value, T>::type ceil(T n, T d) { n   assert(d != 0); n   if (d < 0) tie(n, d) = make_pair(-n, -d); n   return n / d + ((n > 0) && (n % d)); n } n };  // namespace __numeric n using namespace __numeric; n namespace __algorithm { n template <typename T> n void dedup(vector<T>& v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n } n template <typename T> n typename vector<T>::iterator find(vector<T>& v, const T& x) { n   auto it = lower_bound((v).begin(), (v).end(), x); n   return it != v.end() && *it == x ? it : v.end(); n } n template <typename T> n size_t index(vector<T>& v, const T& x) { n   auto it = find(v, x); n   assert(it != v.end() && *it == x); n   return it - v.begin(); n } n template <typename C, typename T, typename OP> n vector<T> prefixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); n   return r; n } n template <typename C, typename T, typename OP> n vector<T> suffixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); n   return r; n } n }  // namespace __algorithm n using namespace __algorithm; n #pragma GCC diagnostic push n #pragma GCC diagnostic ignored  -Wunused-parameter  n struct monostate { n   friend istream& operator>>(istream& is, const monostate& ms) { return is; } n   friend ostream& operator<<(ostream& os, const monostate& ms) { return os; } n   friend monostate operator+(const monostate& a, const monostate& b) { n     return a; n   } n } ms; n #pragma GCC diagnostic pop n namespace __io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(15); n   if (int((second).size())) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace __io n using namespace __io; n struct union_find { n   struct node { n     int par, rnk, size; n     node(int id = 0) : par(id), rnk(0), size(1) {} n   }; n   vector<node> uf; n   union_find(int N = 0) : uf(N) { n     for (int i = 0; i < N; i++) uf[i] = node(i); n   } n   int rep(int i) { n     if (i != uf[i].par) uf[i].par = rep(uf[i].par); n     return uf[i].par; n   } n   bool unio(int a, int b) { n     a = rep(a), b = rep(b); n     if (a == b) return false; n     if (uf[a].rnk < uf[b].rnk) swap(a, b); n     uf[b].par = a; n     uf[a].size += uf[b].size; n     if (uf[a].rnk == uf[b].rnk) uf[a].rnk++; n     return true; n   } n   node& operator[](int i) { return uf[rep(i)]; } n   friend void pr(const union_find& u) { n     pr( { ); n     bool first = 1; n     for (int i = 0; i < int((u.uf).size()); i++) n       if (u.uf[i].par == i) { n         if (!first) n           pr( ,  ); n         else n           first = 0; n         pr( [  , i,   | rank= , u.uf[i].rnk,   size= , u.uf[i].size,   ] ); n       } n     pr( } ); n   } n }; n const int MAXB = 18; n const int MAXV = 1 << MAXB; n int ct[MAXV]; n int main() { n   setIO(); n   int N; n   re(N); n   for (int i = 0; i < (N); i++) { n     int v; n     re(v); n     ct[v]++; n   } n   ll ans = 0; n   for (int v = 0; v < (MAXV); v++) { n     ans -= ll(v) * ct[v]; n   } n   union_find uf(MAXV + 1); n   for (int sum = (MAXV)-1; sum >= 0; sum--) { n     for (int v1 = sum, v2 = 0; v1 > v2; v1 = (v1 - 1) & sum, v2 = sum ^ v1) { n       if (ct[v1] && ct[v2]) { n         if (uf.unio(v1, v2)) { n           ans += ll(ct[v1] + ct[v2] - 1) * sum; n           ct[v1] = 1; n           ct[v2] = 1; n         } n       } n     } n     if (ct[sum] && uf.unio(MAXV, sum)) ans += ll(ct[sum]) * sum; n   } n   ps(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long qpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a; n     b >>= 1; n     a = a * a; n   } n   return res; n } n int a[500005]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   k = 8 * k / n; n   if (k >= ceil(log(n) / log(2))) { n     cout << 0; n     return 0; n   } n   long long q = pow(2, k); n   int min1 = n; n   int sum = 0; n   for (int l = 1, r = 1; r <= n && l <= n;) { n     while (r < n && a[r] == a[r + 1]) r++; n     sum++; n     if (sum == q) { n       min1 = min(min1, n - (r - l + 1)), sum--; n       while (l < n && a[l] == a[l + 1]) l++; n       l++; n     } n     r++; n   } n   if (min1 == n) n     cout << 0; n   else n     cout << min1; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ) n using namespace std; n const int inf = numeric_limits<int>::max() / 2; n const long double eps = 1e-9; n const long double pi = acos(-1); n const int MAXN = (3e8 + 10) / 3; n int n; n unsigned int a, b, c, d; n bitset<MAXN> primes; n unsigned int f(unsigned int x) { return d + x * (c + x * (b + x * a)); } n int fict(int i) { return i - i / 2 - i / 3 + i / 6 - 1; } n void sieve() { n   primes.set(); n   for (int i = 5; i * i <= n; i++) { n     if (i % 2 == 0 || i % 3 == 0) { n       continue; n     } n     if (primes[fict(i)]) { n       for (int j = i * i; j <= n; j += i) { n         if (j % 2 == 0 || j % 3 == 0) { n           continue; n         } n         primes[fict(j)] = 0; n       } n     } n   } n } n inline void solve() { n   sieve(); n   unsigned int ans = 0; n   for (int p = 2; p <= n; p++) { n     if (p != 2 && p % 2 == 0 || p != 3 && p % 3 == 0) { n       continue; n     } n     if (p != 2 && p != 3 && !primes[fict(p)]) { n       continue; n     } n     unsigned int s = 0; n     unsigned int q = n / p; n     while (q >= 1) { n       s += q; n       q /= p; n     } n     ans += f(p) * s; n   } n   cout << ans <<   n ; n } n inline void read() { cin >> n >> a >> b >> c >> d; } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max = 2e5 + 10; n vector<int> A; n int ok[Max]; n int len; n int seg[Max * 4]; n int lazy[Max * 4]; n void build(int tl = 0, int tr = len + 1, int idx = 1) { n   if (tr - tl == 1) { n     seg[idx] = len - tl; n     return; n   } n   int mid = (tl + tr) / 2; n   build(tl, mid, idx * 2); n   build(mid, tr, idx * 2 + 1); n   seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]); n } n void shift(int idx) { n   if (!lazy[idx]) return; n   lazy[idx * 2] += lazy[idx]; n   lazy[idx * 2 + 1] += lazy[idx]; n   seg[idx * 2] += lazy[idx]; n   seg[idx * 2 + 1] += lazy[idx]; n   lazy[idx] = 0; n } n void upd(int i, int val, int tl = 0, int tr = len + 1, int idx = 1) { n   if (tr - 1 <= i) { n     lazy[idx] += val; n     seg[idx] += val; n     return; n   } n   if (tl > i) return; n   shift(idx); n   int mid = (tl + tr) / 2; n   upd(i, val, tl, mid, idx * 2); n   upd(i, val, mid, tr, idx * 2 + 1); n   seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]); n } n int check() { return seg[1]; } n int main() { n   int n, h; n   cin >> n >> len >> h; n   for (int i = 0; i < len; i++) { n     int a; n     cin >> a; n     A.push_back(a); n   } n   sort(A.begin(), A.end()); n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> a; n     int l = -1, r = len; n     while (r - l > 1) { n       int idx = (l + r) / 2; n       if (a + A[idx] >= h) n         r = idx; n       else n         l = idx; n     } n     ok[i] = r; n   } n   int ans = 0; n   build(); n   for (int i = 0; i < len; i++) upd(ok[i], -1); n   if (check() >= 0) ans++; n   for (int i = len; i < n; i++) { n     upd(ok[i], -1); n     upd(ok[i - len], 1); n     if (check() >= 0) ans++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 120000 + 10; n const double PI = acos(-1); n pair<double, double> operator+(pair<double, double> a, pair<double, double> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<double, double> operator-(pair<double, double> a, pair<double, double> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n pair<double, double> operator*(pair<double, double> a, pair<double, double> b) { n   return make_pair(a.first * b.first - a.second * b.second, n                    a.first * b.second + a.second * b.first); n } n pair<double, double> operator/(pair<double, double> a, double t) { n   return make_pair(a.first / t, a.second / t); n } n int rev[N], lim; n pair<double, double> w[N]; n void initrever(int n, int &lim) { n   lim = 1; n   for (; lim < n; lim <<= 1) n     ; n   for (int i = 1; i < lim; i++) { n     rev[i] = (rev[i >> 1] >> 1); n     if (i & 1) rev[i] |= (lim >> 1); n   } n   for (int i = 0; i < lim; i++) { n     double t = 2 * PI * i / lim; n     w[i] = make_pair(cos(t), sin(t)); n   } n   return; n } n void FFT(vector<pair<double, double> > &a, int t) { n   int lim = a.size(); n   if (t < 0) { n     for (int i = 1; i < lim; i++) { n       if (i < (lim - i)) swap(a[i], a[lim - i]); n     } n   } n   for (int i = 0; i < lim; i++) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int i = 2; i <= lim; i <<= 1) { n     for (int j = 0; j < lim; j += i) { n       for (int k = 0; k < (i >> 1); k++) { n         pair<double, double> first = a[j + k], n                              second = a[j + k + (i >> 1)] * w[lim / i * k]; n         a[j + k] = (first + second); n         a[j + k + (i >> 1)] = (first - second); n       } n     } n   } n   if (t < 0) { n     for (int i = 0; i < lim; i++) a[i] = a[i] / lim; n   } n   return; n } n vector<pair<double, double> > f[2]; n int n, a, p; n double reduce(double first) { n   long long tmp = llround(first); n   tmp %= p; n   return (double)tmp; n } n void add1() { n   swap(f[0], f[1]); n   for (int i = 0; i <= 1; i++) { n     for (int j = n; j >= 1; j--) { n       f[i][j].first = reduce(f[i][j].first + f[i][j - 1].first); n     } n   } n   f[1][1].first = reduce(f[1][1].first + 1); n   return; n } n void put(vector<pair<double, double> > v) { n   for (int i = 0; i < (int)v.size(); i++) printf( %lf  , v[i].first); n   printf(  n ); n   return; n } n void pro2(int v) { n   FFT(f[0], 1), FFT(f[1], 1); n   for (int i = 0; i < lim; i++) { n     pair<double, double> first = n         f[v & 1][i] * (f[0][i] + f[1][i]) + f[0][i] + f[v & 1][i]; n     pair<double, double> second = n         f[(v & 1) ^ 1][i] * (f[0][i] + f[1][i]) + f[1][i] + f[(v & 1) ^ 1][i]; n     f[0][i] = first, f[1][i] = second; n   } n   FFT(f[0], -1), FFT(f[1], -1); n   for (int i = 0; i <= 1; i++) { n     for (int j = 0; j <= n; j++) { n       f[i][j].first = reduce(f[i][j].first); n       f[i][j].second = 0; n     } n     for (int j = n + 1; j < lim; j++) f[i][j] = make_pair(0, 0); n   } n   return; n } n vector<int> num; n int main() { n   scanf( %d%d%d , &n, &a, &p); n   initrever(n * 2 + 1, lim); n   f[0].resize(lim), f[1].resize(lim); n   for (int no = a; no > 1; no /= 2) { n     num.push_back(no); n   } n   num.push_back(1); n   reverse(num.begin(), num.end()); n   f[1][1].first = 1; n   int ans = 0; n   for (int i = 0; i < (int)num.size() - 1; i++) { n     for (int j = 1; j <= n; j += 2) { n       ans = (ans + llround(f[1][j].first)) % p; n     } n     pro2(num[i]); n     if (num[i] * 2 < num[i + 1]) add1(); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i <= n - 1; i++) cin >> arr[i]; n   vector<pair<int, int> > G[n]; n   int cnt = 0; n   for (int i = 0; i <= n - 1; i++) { n     for (int j = i + 1; j <= n - 1; j++) { n       if (arr[i] > arr[j]) { n         G[i].push_back(make_pair(arr[j], j)); n         cnt++; n       } n     } n   } n   cout << cnt <<   n ; n   for (int i = 0; i <= n - 1; i++) { n     sort(G[i].begin(), G[i].end()); n     int s = G[i].size(); n     for (int j = s - 1; j >= 0; j--) { n       cout << i + 1 <<     << G[i][j].second + 1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[300022]; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     int n, i; n     scanf( %d , &n); n     for (i = 0; i < n; i++) scanf( %d , &arr[i]); n     map<int, int> mf, ml; n     map<int, int>::iterator it; n     for (i = 0; i < n; i++) { n       if (mf.find(arr[i]) == mf.end()) mf[arr[i]] = i; n       ml[arr[i]] = i; n     } n     int ans = 0, in = -1, len = 0; n     int cou = 0; n     for (it = mf.begin(); it != mf.end(); it++) { n       cou++; n       int ele = it->first; n       int focc = it->second, locc = ml[ele]; n       if (focc > in) n         len++; n       else n         len = 1; n       in = locc; n       ans = max(ans, len); n     } n     cout << (cou - ans) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace mainns { n template <typename T> n void relaxMax(T& relaxWhat, const T& relaxBy) { n   relaxWhat = max(relaxWhat, relaxBy); n } n template <typename T> n void relaxMin(T& relaxWhat, const T& relaxBy) { n   relaxWhat = min(relaxWhat, relaxBy); n } n namespace { n const vector<pair<int, int>> offsets = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n const unordered_map<char, char> nextLetter = { n     { D ,  I }, { I ,  M }, { M ,  A }, { A ,  D }}; n }  // namespace n class Solver374C { n  public: n   void run(); n   int height, width; n   vector<string> board; n   vector<vector<int>> maxValues; n   int getMaxValue(int x, int y) { n     if (maxValues[x][y] > 0) return maxValues[x][y]; n     if (maxValues[x][y] == -2) return numeric_limits<int>::max() / 10; n     maxValues[x][y] = -2; n     int maxNeighbourValue = 0; n     for (auto& offs : offsets) { n       auto neigbour = make_pair(x + offs.first, y + offs.second); n       if (neigbour.first >= 0 && neigbour.first < width && n           neigbour.second >= 0 && neigbour.second < height) n         if (nextLetter.at(board[y][x]) == n             board[neigbour.second][neigbour.first]) n           relaxMax(maxNeighbourValue, n                    getMaxValue(neigbour.first, neigbour.second)); n     } n     return maxValues[x][y] = maxNeighbourValue + 1; n   } n }; n void Solver374C::run() { n   cin >> height >> width; n   board = vector<string>(height); n   for (auto& s : board) cin >> s; n   maxValues = vector<vector<int>>(width, vector<int>(height, -1)); n   int maxValue = 0; n   for (int x = 0; x < width; ++x) n     for (int y = 0; y < height; ++y) { n       auto v = getMaxValue(x, y); n       if (board[y][x] ==  D ) relaxMax(maxValue, v); n     } n   if (maxValue < 4) n     cout <<  Poor Dima! ; n   else if (maxValue >= numeric_limits<int>::max() / 10) n     cout <<  Poor Inna! ; n   else n     cout << (maxValue / 4); n } n }  // namespace mainns n using CurrentSolver = mainns::Solver374C; n int main() { n   ios::sync_with_stdio(false); n   CurrentSolver().run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool allCharactersSame(string s) { n   int n = s.length(); n   for (int i = 1; i < n; i++) n     if (s[i] != s[0]) return false; n   return true; n } n signed main() { n   int n; n   const int letters = 26; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string str; n     cin >> str; n     int n = str.size(); n     vector<int> cnt(letters); n     for (int i = 0; i < n; ++i) cnt[str[i] -  a ]++; n     int ans = 0, k = 0; n     for (int i = 0; i < letters; ++i) { n       if (cnt[i] > 1) n         ans++; n       else if (cnt[i] == 1) n         k++; n     } n     ans += k / 2; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n int hp[N], dmg[N], p[N]; n bool cmp(int a, int b) { return hp[a] - dmg[a] > hp[b] - dmg[b]; } n bool vis[N]; n int main() { n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &hp[i], &dmg[i]); n     p[i] = i; n   } n   sort(p + 1, p + 1 + n, cmp); n   long long sum = 0, ans, la = 0, cb = 0; n   bool flag = false; n   for (int i = 1; i <= n; i++) { n     if (i > b || hp[p[i]] <= dmg[p[i]]) { n       if (!flag) flag = true, la = hp[p[i - 1]] - dmg[p[i - 1]], cb = i - 1; n     } n     if (!flag) n       sum += hp[p[i]], vis[p[i]] = true; n     else n       sum += dmg[p[i]]; n   } n   ans = sum; n   if (a && b) { n     for (int i = 1; i <= n; i++) { n       long long tmp = hp[i]; n       for (int j = 1; j <= a; j++) tmp = tmp * 2; n       tmp += sum; n       if (vis[i]) { n         tmp -= hp[i]; n       } else { n         tmp -= dmg[i]; n         if (cb == b) tmp -= la; n       } n       ans = max(ans, tmp); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Maxn = 10000; n const long double pi = acos(-1); n const long long mod = 1000000007; n template <class T> n ostream &operator<<(ostream &out, vector<T> &A) { n   for (auto x : A) out << x <<    ; n   return out; n } n template <class T> n ostream &operator<<(ostream &out, set<T> &A) { n   for (auto x : A) out << x <<    ; n   return out; n } n template <class T1, class T2> n T1 powr(T1 a, T2 b) { n   T1 res = 1; n   for (long long i = 1; i < b + 1; i++) res = res * a; n   return res; n } n int32_t main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, l, r, x; n   while (cin >> n >> l >> r >> x) { n     vector<long long> k(n); n     for (long long i = 0; i < n; i++) { n       cin >> k[i]; n     } n     long long cnt = 0; n     for (long long i = 0; i < (1 << n); i++) { n       long long mi = 1000000; n       long long ma = -1; n       long long sum = 0; n       for (long long j = 0; j < n; j++) { n         if (i & (1 << j)) { n           sum = sum + k[j]; n           ma = max(ma, k[j]); n           mi = min(mi, k[j]); n         } n       } n       if (l <= sum && sum <= r && (ma - mi) >= x) { n         cnt++; n       } n     } n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 + 5; n int N, P[MAXN]; n long long D[MAXN]; n vector<pair<long long, int>> v, f[2]; n int main() { n   scanf( %d , &N); n   P[1] = 1, P[2] = 2; n   for (int i = 3, x; i <= N; ++i) { n     printf( 2 1 %d %d n , P[2], i); n     fflush(stdout); n     scanf( %d , &x); n     if (x == -1) P[2] = i; n   } n   for (int i = 2; i <= N; ++i) { n     if (i == P[2]) continue; n     printf( 1 1 %d %d n , P[2], i); n     fflush(stdout); n     scanf( %lld , &D[i]); n     v.push_back(make_pair(D[i], i)); n   } n   sort(v.begin(), v.end()); n   int p = v.back().second; n   for (int i = 2, x; i <= N; ++i) { n     if (i == p || i == P[2]) continue; n     printf( 2 1 %d %d n , p, i); n     fflush(stdout); n     scanf( %d , &x); n     f[x == -1].push_back(make_pair(D[i], i)); n   } n   sort(f[0].begin(), f[0].end()); n   sort(f[1].begin(), f[1].end()); n   printf( 0 %d %d  , P[1], P[2]); n   for (int i = 0; i < f[1].size(); ++i) printf( %d  , f[1][i].second); n   printf( %d  , p); n   for (int i = f[0].size() - 1; i >= 0; --i) printf( %d  , f[0][i].second); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, m, n, u, v; n long long a[15], mod, lose[14][16389], f[16389], g[16389][15]; n long long sz[16389], inv[16389]; n long long getinv(long long a, long long p) { n   if (a == 1) return 1; n   long long k = a - getinv(p % a, a); n   return (k * p + 1) / a; n } n long long group_lose(int v, int u) { n   int i, j; n   long long ret; n   ret = f[v] * f[u - v] % mod * g[u - v][sz[v]] % mod * inv[u] % mod; n   return ret; n } n int main() { n   mod = 1000000007; n   scanf( %d , &n); n   for (i = 0; i <= n - 1; i++) { n     scanf( %lld , &a[i]); n   } n   for (u = 0; u <= (1 << n) - 1; u++) { n     sz[u] = 0; n     for (i = 0; i <= n - 1; i++) n       if (u & (1 << i)) sz[u]++; n   } n   for (u = 0; u <= (1 << n) - 1; u++) { n     f[u] = 1; n     for (i = 0; i <= n - 1; i++) n       if (u & (1 << i)) { n         for (j = i + 1; j <= n - 1; j++) n           if (u & (1 << j)) { n             f[u] = f[u] * (a[i] + a[j]) % mod; n           } n       } n     inv[u] = getinv(f[u], mod); n   } n   for (u = 0; u <= (1 << n) - 1; u++) { n     g[u][0] = 1; n     g[u][1] = 1; n     for (i = 0; i <= n - 1; i++) n       if (u & (1 << i)) { n         g[u][1] = g[u][1] * a[i] % mod; n       } n     for (i = 2; i <= n; i++) { n       g[u][i] = g[u][i - 1] * g[u][1] % mod; n     } n   } n   for (u = 0; u <= (1 << n) - 1; u++) { n     for (i = 0; i <= n - 1; i++) { n       if (u & (1 << i)) { n         lose[i][u] = 0; n         for (v = u; v; v = (v - 1) & u) n           if ((v != u) && (v & (1 << i))) { n             long long temp = group_lose(v, u); n             lose[i][u] += (1 - lose[i][v] + mod) * temp % mod; n           } n         lose[i][u] %= mod; n       } n     } n   } n   long long ans = 0; n   for (i = 0; i <= n - 1; i++) { n     ans += (1 - lose[i][(1 << n) - 1] + mod); n   } n   ans %= mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const long double eps = 1e-9; n const long long mod = 1e9 + 7, ll_max = (long long)1e18; n const int MX = 2e5 + 10, int_max = 0x3f3f3f3f; n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long n, k; n long long arr[MX]; n int solve() { n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     arr[i] += i + 1; n     n -= i + 1; n   } n   if (n < 0) return 0; n   for (int i = 0; i < k; i++) { n     arr[i] += (n / k); n   } n   n %= k; n   for (int i = k - 1; i > 0; i--) { n     int del = min(2 * arr[i - 1] - arr[i], n); n     n -= del; n     arr[i] += del; n   } n   return (n == 0); n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   if (solve()) { n     cout <<  YES n ; n     for (int i = 0; i < k; i++) cout << arr[i] <<    ; n     cout <<   n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n long long x[300001]; n long long p[300001] = {1}; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> x[i]; n     p[i + 1] = p[i] * 2 % MOD; n   } n   sort(x, x + n); n   long long ret = 0; n   for (int i = 1; i <= n; ++i) { n     ret = (ret + x[i - 1] * (p[i - 1] - p[n - i])) % MOD; n   } n   cout << (ret + MOD) % MOD; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> v, v1, v2; n multiset<long long int> mss; n multiset<long long int>::iterator it; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int a, b, c, d, i, j, z = 0, k, w = 1, q = 0, cnt = 0, x = 0, p = 0; n   cin >> a >> c; n   for (i = 0; i < a; i++) { n     cin >> b; n     v.push_back(b); n   } n   sort(v.begin(), v.end()); n   for (i = 0; i < a / 2; i++) { n     v1.push_back(v[i]); n   } n   for (j = i; j < a; j++) { n     mss.insert(v[j]); n   } n   for (i = 0; i < v1.size(); i++) { n     d = v1[i] + c; n     it = mss.lower_bound(d); n     if (it == mss.end()) { n       break; n     } n     cnt++; n     mss.erase(it); n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int x, int y) { return x > y; } n const int maxn = 3e5 + 100; n vector<int> v[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 2; i < maxn; i++) n     for (int j = i; j < maxn; j += i) v[j].push_back(i); n   v[1].push_back(1); n   int t; n   cin >> t; n   while (t--) { n     int n; n     char c; n     cin >> n >> c; n     string s; n     cin >> s; n     int check[n + 1]; n     memset(check, 0, sizeof(check)); n     int cnt = 0; n     for (int i = 0; i < n; i++) n       if (s[i] != c) { n         cnt++; n         for (auto j : v[i + 1]) check[j] = 1; n       } n     if (!cnt) { n       cout << 0 << endl; n       continue; n     } n     int cc = 0; n     for (int i = 2; i <= n; i++) n       if (!check[i] && s[i - 1] == c) cc = i; n     if (!cc) n       cout << 2 << endl << n <<     << n - 1 << endl; n     else n       cout << 1 << endl << cc << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n void test_case() { n   int n, k; n   cin >> n >> k; n   vector<pair<int, int>> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a.begin(), a.end(), less<pair<int, int>>()); n   int start = 0, tot = 0; n   vector<int> res; n   while (start < n && k - a[start].first >= 0) { n     debug() <<   [  n             <<  k  n                 :   n             << (k) <<  ]  ; n     tot++; n     k = k - a[start].first; n     res.push_back(a[start].second + 1); n     start++; n   } n   cout << tot << endl; n   for (int x : res) { n     cout << x <<    ; n   } n   cout << endl; n } n int main() { n   int t = 1; n   for (int i = 0; i < t; i++) test_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, c; n   cin >> a >> c; n   vector<long long> aa; n   while (a) { n     aa.push_back(a % 3); n     a /= 3; n   } n   vector<long long> cc; n   while (c) { n     cc.push_back(c % 3); n     c /= 3; n   } n   long long i = 0, j = 0; n   vector<long long> res; n   while (i < aa.size() && j < cc.size()) { n     res.push_back((cc[j] - aa[i] + 3) % 3); n     i++; n     j++; n   } n   while (i < aa.size()) { n     res.push_back((3 - aa[i]) % 3); n     i++; n   } n   while (j < cc.size()) { n     res.push_back(cc[j] % 3); n     j++; n   } n   long long p = 1; n   long long ans = 0; n   for (int i = 0; i < res.size(); i++) { n     ans += (res[i] * (p)); n     p *= 3; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sett(int n, int pos) { return n = n | (1 << pos); } n int reset(int n, int pos) { return n = n & ~(1 << pos); } n bool check(int n, int pos) { return (bool)(n & (1 << pos)); } n template <typename T> n T bigmod(T x, T p) { n   if (p == 0) return 1; n   if (p & 1) { n     return x * bigmod(x, p - 1) % 1000000007; n   } else { n     T tmp = bigmod(x, p >> 1) % 1000000007; n     return tmp * tmp % 1000000007; n   } n } n template <typename T> n void print(const T& v) { n   cerr << v <<    ; n } n template <typename T1, typename... T2> n void print(const T1& first, const T2&... rest) { n   print(first); n   print(rest...); n } n const int inf = (1 << 30); n int a[2000123 + 4]; n struct dat { n   long long val, id; n   dat(long long a, long long b) { n     val = a; n     id = b; n   }; n   bool operator<(dat x) const { return val == x.val ? id < x.id : val < x.val; } n }; n set<dat> st; n vector<dat> v; n bool compare(dat x, dat y) { return x.id < y.id; } n void func(dat x) { cout << x.val <<     << x.id <<   n ; } n void solve() { n   set<dat>::iterator it, it2, it3; n   while (1) { n     if (st.size() < 2) break; n     it = st.begin(); n     it2 = st.begin(); n     it2++; n     if (it->val != it2->val) { n       v.push_back(*it); n       st.erase(it); n     } else { n       dat x = dat(it->val + it2->val, it2->id); n       st.erase(it); n       it = st.begin(); n       st.erase(it); n       st.insert(x); n     } n   } n   if (st.size() == 1) { n     v.push_back(*(st.begin())); n   } n   sort(v.begin(), v.end(), compare); n   cout << v.size() <<   n ; n   for (int i = 0; i < v.size(); i++) cout << v[i].val <<    ; n } n int main() { n   long long cnt = 1, n, qr, k; n   scanf( %I64d , &n); n   for (int i = 1; i <= (int)n; i++) { n     scanf( %I64d , &k); n     st.insert(dat(k, (long long)i)); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, vector<int> > mx; n map<int, vector<int> > my; n int pow(int x) { n   long long i = 2; n   long long r = 1; n   while (x) { n     if (x & 1) r = (r * i) % 1000000007; n     i = (i * i) % 1000000007; n     x = x >> 1; n   } n   return r; n } n int main() { n   ios::sync_with_stdio(false); n   int i, j, n, t; n   cin >> n; n   int x[n]; n   int y[n]; n   long long an = 1; n   for (i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     mx[x[i]].push_back(i); n     my[y[i]].push_back(i); n   } n   bool visit[i]; n   memset(visit, 0, i); n   set<int> sx; n   set<int> sy; n   int c; n   queue<int> q; n   for (i = 0; i < n; i++) n     if (!visit[i]) { n       c = 0; n       sx.clear(); n       sy.clear(); n       for (j = 0; j < mx[x[i]].size(); j++) { n         c++; n         q.push(mx[x[i]][j]); n         visit[mx[x[i]][j]] = 1; n       } n       sx.insert(x[i]); n       for (j = 0; j < my[y[i]].size(); j++) { n         c++; n         q.push(my[y[i]][j]); n         visit[my[y[i]][j]] = 1; n       } n       sy.insert(y[i]); n       t = i; n       while (!q.empty()) { n         i = q.front(); n         q.pop(); n         if (sx.find(x[i]) == sx.end()) n           for (j = 0; j < mx[x[i]].size(); j++) { n             c++; n             q.push(mx[x[i]][j]); n             visit[mx[x[i]][j]] = 1; n           } n         sx.insert(x[i]); n         if (sy.find(y[i]) == sy.end()) n           for (j = 0; j < my[y[i]].size(); j++) { n             c++; n             q.push(my[y[i]][j]); n             visit[my[y[i]][j]] = 1; n           } n         sy.insert(y[i]); n       } n       i = t; n       if (c / 2 < sx.size() + sy.size()) n         an = (an * (pow(sx.size() + sy.size()) + 1000000007 - 1)) % 1000000007; n       else n         an = (an * pow(sx.size() + sy.size())) % 1000000007; n     } n   cout << an; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Pow(long long a, long long b) { n   long long ans = 1LL, base = a % 1000000007; n   while (b != 0) { n     if (b & 1LL != 0) ans = ans * base % 1000000007; n     base = base * base % 1000000007; n     b >>= 1LL; n   } n   return ans; n } n long long A, B, n, x; n long long ans; n int main() { n   scanf( %lld %lld %lld %lld , &A, &B, &n, &x); n   long long ppow = Pow(A, n) % 1000000007; n   if (A == 1LL) n     ans = (x % 1000000007 + n % 1000000007 * B % 1000000007) % 1000000007; n   else n     ans = (ppow * x % 1000000007 + B % 1000000007 * (ppow - 1LL) % 1000000007 * n                                        Pow(A - 1LL, 1000000007 - 2) % n                                        1000000007) % n           1000000007; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, d, m; n   cin >> n >> m >> d; n   int c[m], sum = 0; n   for (int i = 0; i < m; i++) { n     cin >> c[i]; n     sum += c[i]; n   } n   d--; n   if (sum + (m + 1) * d >= n) { n     cout <<  YES  << endl; n     int k = (d > 0 ? (n - sum) / d : 0), res = (d > 0 ? (n - sum) % d : 0), n         cnt = 0, id = 0; n     bool flag = 1; n     for (int i = 1; i <= n; i++) { n       int j; n       if (cnt == k && res) { n         k++, d = res; n         res = 0; n       } n       if (cnt < k && flag) { n         for (j = i; j < i + d && j <= n; j++) { n           cout << 0 <<    ; n         } n         cnt++; n       } else { n         for (j = i; j < i + c[id]; j++) { n           cout << id + 1 <<    ; n         } n         id++; n       } n       flag = !flag; n       i = j - 1; n     } n     cout << endl; n   } else n     cout <<  NO  << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e6 + 7; n const int ALPHA = 40; n const long long INF = 1e16 + 7; n const int MOD = 1e9 + 7; n const int LOG = 22; n const int BASE[2] = {313, 239}; n long long n, d; n bool ns[MAXN], seen[MAXN]; n vector<long long> group[MAXN], plast[MAXN]; n void solve() { n   cin >> n >> d; n   for (int i = 0; i < n; i++) { n     group[i].clear(); n     plast[i].clear(); n   } n   for (int i = 0; i < n; i++) cin >> ns[i]; n   fill(seen, seen + n, 0); n   long long ptr = 0, cnt = 0; n   for (int i = 0; i < n; i++) { n     ptr = i; n     if (seen[ptr]) continue; n     while (!seen[ptr]) { n       seen[ptr] = 1; n       group[cnt].push_back(ns[ptr]); n       ptr = (ptr + d) % n; n     } n     cnt++; n   } n   for (int i = 0; i < cnt; i++) { n     long long k = group[i].size(); n     for (int j = 0; j < k; j++) group[i].push_back(group[i][j]); n     long long last = -INF; n     for (int j = 0; j < group[i].size(); j++) { n       if (!group[i][j]) last = j; n       plast[i].push_back(j - last); n     } n   } n   long long maxim = 0; n   for (int i = 0; i < cnt; i++) { n     for (int j = 0; j < group[i].size() / 2; j++) { n       maxim = max(maxim, min(plast[i][j], plast[i][j + (group[i].size() / 2)])); n     } n   } n   if (maxim >= INF) n     cout << -1 <<   n ; n   else n     cout << maxim <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3000 + 50; n vector<int> edge[maxn]; n int sub[maxn][maxn], fa[maxn][maxn]; n long long n, ans, dp[maxn][maxn]; n void dfs(int root, int k, int dad) { n   sub[root][k]++; n   fa[root][k] = dad; n   for (auto t : edge[k]) { n     if (t != dad) { n       dfs(root, t, k); n       sub[root][k] += sub[root][t]; n     } n   } n } n long long DP(int a, int b) { n   if (a == b) return 0; n   if (dp[a][b] != -1) return dp[a][b]; n   return dp[a][b] = n              sub[a][b] * sub[b][a] + max(DP(fa[a][b], a), DP(fa[b][a], b)); n } n int main() { n   cin >> n; n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     edge[u].push_back(v); n     edge[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) dfs(i, i, -1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i != j) ans = max(ans, DP(i, j)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n; n   long long a, sum, ans; n   priority_queue<long long, vector<long long>, greater<long long> > Q; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %lld , &a); n     Q.push(a); n   } n   if (n % 2 == 0) Q.push(0); n   ans = 0; n   while (Q.size() > 2) { n     sum = Q.top(); n     Q.pop(); n     sum += Q.top(); n     Q.pop(); n     sum += Q.top(); n     Q.pop(); n     ans += sum; n     Q.push(sum); n   } n   printf( %lld n , ans); n   return 0; n } n