malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long l[5001], dp[5001][5001];   void solve() {     int n, k, ans = 0;     cin >> n >> k;     vector<int> v(n, 0);     for (int i = 0; i < (int)(n); ++i) cin >> v[i];     sort(v.begin(), v.end());     for (int i = 0, j = 0; i < n; ++i) {       while (v[i] - v[j] > 5) {         j++;       }       l[i] = j;     }     for (int i = 1; i <= k; i++) {       for (int j = 1; j <= n; j++) {         dp[i][j] = max(dp[i][j - 1], dp[i - 1][l[j - 1]] + j - l[j - 1]);       }     }     cout << dp[k][n] <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, data, prev = 0, energy = 0;     cin >> n;     vector<long long int> v1(n);     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       cin >> data;       if (energy < (data - prev)) {         ans += ((data - prev) - energy);         energy = 0;         prev = data;       } else {         energy += (prev - data);         prev = data;       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 10, M = N, INF = 0x3f3f3f3f, mod = 1e9 + 7,                   base = 131;   long long n, t, ans;   long long a[N], s[N];   long long phi(long long n) {     long long res, a;     res = a = n;     for (long long i = 2; i <= a / i; i++) {       if (a % i) continue;       res = res / i * (i - 1);       while (a % i == 0) a /= i;     }     if (a > 1) res = res / a * (a - 1);     return res;   }   signed main() {     long long p;     cin >> p;     cout << phi(p - 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, y, t, l[205], qd[205 + 205], qn[205 + 205], h[205], b1, b2, d1, d2,       ret;   void build(int x, int y) { qd[++t] = y, qn[t] = l[x], l[x] = t; }   void dfs(int x, int y, int *d) {     h[x] = 0;     int mx1 = 0, mx2 = 0;     for (int i = l[x]; i; i = qn[i]) {       if (i == b1 || i == b2 || qd[i] == y) continue;       dfs(qd[i], x, d);       h[x] = max(h[x], h[qd[i]] + 1);       if (h[qd[i]] + 1 >= mx1)         mx2 = mx1, mx1 = h[qd[i]] + 1;       else if (h[qd[i]] + 1 > mx2)         mx2 = h[qd[i]] + 1;     }     *d = max(*d, mx1 + mx2);   }   int main() {     scanf( %d , &n);     t = 1;     for (int i = 1; i < n; i++) scanf( %d%d , &x, &y), build(x, y), build(y, x);     ret = 0;     for (int i = 1; i <= n; i++) {       for (int j = l[i]; j; j = qn[j]) {         b1 = j;         b2 = j ^ 1;         d1 = d2 = 0;         dfs(i, -1, &d1);         dfs(qd[j], -1, &d2);         ret = max(ret, d1 * d2);       }     }     printf( %d n , ret);     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const int maxn = 510005;   int n, m;   vector<int> g[maxn];   char s[maxn];   vector<int> dep[maxn];   vector<int> op[maxn];   int id[maxn][2];   int deep[maxn];   int mx[maxn];   int dfs_clock = 1;   void dfs(int u, int d) {     deep[u] = mx[u] = d;     id[u][0] = dfs_clock++;     dep[d].push_back(1 << (s[u] -  a ));     op[d].push_back(id[u][0]);     for (int i = 0; i < g[u].size(); i++) {       dfs(g[u][i], d + 1);       mx[u] = max(mx[u], mx[g[u][i]]);     }     id[u][1] = dfs_clock++;   }   int main() {     int p;     scanf( %d%d , &n, &m);     for (int i = 2; i <= n; i++) {       scanf( %d , &p);       g[p].push_back(i);     }     scanf( %s , s + 1);     dfs(1, 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j < dep[i].size(); j++) {         dep[i][j] ^= dep[i][j - 1];       }     }     int u, h;     for (int i = 0; i < m; i++) {       scanf( %d%d , &u, &h);       if (h <= deep[u] || h > mx[u]) {         printf( Yes n );       } else {         int i = upper_bound(op[h].begin(), op[h].end(), id[u][0]) - op[h].begin();         int j =             upper_bound(op[h].begin(), op[h].end(), id[u][1]) - op[h].begin() - 1;         int a;         if (i - 1 >= 0)           a = dep[h][j] ^ dep[h][i - 1];         else           a = dep[h][j];         int num = 0;         for (int i = 0; i < 26; i++) {           if (a & (1 << i)) num++;         }         if (num <= 1)           printf( Yes n );         else           printf( No n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000 * 1000 * 1000 + 7;   const int MAXN = 155;   int i, j, n, m, k, it1, it2, dp[MAXN][MAXN][MAXN][2][2], ans;   int partSum[2][2][MAXN][MAXN];   void add(int &a, int b) {     a += b;     if (a >= MOD) a -= MOD;   }   int getSum(int mon1, int mon2, int leftBound1, int rightBound1, int leftBound2,              int rightBound2) {     int ans = 0;     ans = partSum[mon1][mon2][rightBound1][rightBound2] -           partSum[mon1][mon2][rightBound1][leftBound2 - 1] -           partSum[mon1][mon2][leftBound1 - 1][rightBound2] +           partSum[mon1][mon2][leftBound1 - 1][leftBound2 - 1];     while (ans >= MOD) ans -= MOD;     while (ans < 0) ans += MOD;     return ans;   }   int main() {     scanf( %d %d , &n, &m);     for (i = 1; i <= m; ++i)       for (j = i; j <= m; ++j) {         dp[1][i][j][0][0] = 1;         ans += n;       }     for (i = 1; i <= m; ++i)       for (j = 1; j <= m; ++j)         partSum[0][0][i][j] = dp[1][i][j][0][0] + partSum[0][0][i - 1][j] +                               partSum[0][0][i][j - 1] -                               partSum[0][0][i - 1][j - 1];     for (i = 2; i <= n; ++i) {       int cnt = 0;       for (j = 1; j <= m; ++j)         for (k = j; k <= m; ++k) {           add(dp[i][j][k][0][0], getSum(0, 0, j, m, 1, k));           add(dp[i][j][k][1][1], getSum(1, 1, 1, j, k, m));           add(dp[i][j][k][1][1], getSum(0, 0, 1, j - 1, k + 1, m));           add(dp[i][j][k][1][1], getSum(0, 1, 1, j - 1, k, m));           add(dp[i][j][k][1][1], getSum(1, 0, 1, j, k + 1, m));           add(dp[i][j][k][0][1], getSum(0, 1, j, k, k, m));           add(dp[i][j][k][0][1], getSum(0, 0, j, k, k + 1, m));           add(dp[i][j][k][1][0], getSum(1, 0, 1, j, j, k));           add(dp[i][j][k][1][0], getSum(0, 0, 1, j - 1, j, k));         }       for (j = 1; j <= m; ++j)         for (k = 1; k <= m; ++k)           for (it1 = 0; it1 <= 1; ++it1)             for (it2 = 0; it2 <= 1; ++it2) {               partSum[it1][it2][j][k] = dp[i][j][k][it1][it2];               add(cnt, dp[i][j][k][it1][it2]);             }       add(ans, 1LL * cnt * (n - i + 1) % MOD);       for (it1 = 0; it1 <= 1; ++it1)         for (it2 = 0; it2 <= 1; ++it2)           for (j = 1; j <= m; ++j)             for (k = 1; k <= m; ++k) {               add(partSum[it1][it2][j][k], partSum[it1][it2][j - 1][k]);               add(partSum[it1][it2][j][k], partSum[it1][it2][j][k - 1]);               partSum[it1][it2][j][k] -= partSum[it1][it2][j - 1][k - 1];               if (partSum[it1][it2][j][k] < 0) partSum[it1][it2][j][k] += MOD;             }     }     for (i = 1; i <= n; ++i)       for (j = i; j <= n; ++j)         for (it1 = 0; it1 <= 1; ++it1)           for (it2 = 0; it2 <= 1; ++it2) {           }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = (1e5 + 5) * 2;   void Max(int &x, int y) {     if (x < y) x = y;   }   int n, m;   int a[MAXN];   int dp[MAXN];   bool chk1(int s, int x) {     memset(dp, 0, sizeof(dp));     for (int i = s, iend = s + n - 1; i <= iend; ++i) {       int d = a[i + 1] - a[i] - 1;       if (dp[i] + x < d) return false;       if (dp[i] >= d) Max(dp[i + 1], x);       if (dp[i] + x >= a[i + 2] - a[i] - 1)         Max(dp[i + 2], x - (a[i + 2] - a[i + 1]));     }     int t = s + n - 1, d = a[s] - a[s - 1] - 1;     if (dp[t] + x < d)       return false;     else       return true;   }   bool chk2(int s, int x) {     memset(dp, 0, sizeof(dp));     if (x < a[s + 1] - a[s] - 1) return false;     Max(dp[s + 1], x - (a[s + 1] - a[s]));     for (int i = s + 1, iend = s + n - 1; i <= iend; ++i) {       int d = a[i + 1] - a[i] - 1;       if (dp[i] + x < d) return false;       if (dp[i] >= d) Max(dp[i + 1], x);       if (dp[i] + x >= a[i + 2] - a[i] - 1)         Max(dp[i + 2], x - (a[i + 2] - a[i + 1]));     }     int t = s + n - 1, d = a[s + 1] - a[s - 1] - 1;     if (dp[t] + x < d)       return false;     else       return true;   }   bool check(int x) {     int s = -1;     for (int i = 1, iend = n; i <= iend; ++i) {       if (a[i + 1] - a[i] > x + 1) s = i + 1;     }     if (s == -1)       return true;     else       return chk1(s, x) || chk2(s, x);   }   int main() {     cin >> m >> n;     for (int i = 1, iend = n; i <= iend; ++i) scanf( %d , &a[i]);     for (int i = 1, iend = n; i <= iend; ++i) a[i + n] = m + a[i];     if (n == 1) {       cout << m - 1 << endl;       return 0;     }     int l = 0, r = m, res;     while (l <= r) {       int mid = (l + r) >> 1;       if (check(mid))         res = mid, r = mid - 1;       else         l = mid + 1;     }     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     char a[n][m];     int c = 0;     int column_no;     int row_number;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] ==  B  && c == 0) {           column_no = j + 1;           row_number = i + 1;         }         if (a[i][j] ==  B ) c++;       }     double s = sqrt(c);     if (c == 1)       cout << row_number <<     << column_no;     else {       int r = s / 2;       cout << row_number + r <<     << column_no + r;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXn = 3000;   vector<pair<int, bool> > adj[MAXn + 2];   int d[MAXn + 2][MAXn + 2], c[MAXn + 2][MAXn + 2], root;   bool mark[MAXn + 5];   void DFS(int v) {     int sum = 0, Min = INT_MAX;     mark[v] = true;     for (int i = 0; i < (int)adj[v].size(); i++)       if (!mark[adj[v][i].first]) {         DFS(adj[v][i].first);         sum += d[root][adj[v][i].first] + adj[v][i].second;       }     for (int i = 0; i < (int)adj[v].size(); i++)       if (!mark[adj[v][i].first])         Min = min(Min, c[root][adj[v][i].first] + !adj[v][i].second +                            (sum - d[root][adj[v][i].first] - adj[v][i].second));     d[root][v] = sum;     Min = min(Min, d[root][v]);     c[root][v] = Min;     mark[v] = false;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       u--;       v--;       adj[u].push_back(make_pair(v, 0));       adj[v].push_back(make_pair(u, 1));     }     for (int i = 0; i < n; i++) {       root = i;       DFS(i);     }     int Min = INT_MAX;     for (int i = 0; i < n; i++) Min = min(d[i][i], Min);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (i != j) Min = min(Min, d[i][i] - d[i][j] + c[i][j]);     cout << Min << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1010, M = 200200;   int i, j, k, n, m, ch, now, q;   int a[N][N], ans[M];   struct cc {     int x, y;   } A[M];   struct dd {     int l, r, s, t, q;   } Q[M];   void R(int &x) {     x = 0;     ch = getchar();     while (ch <  0  ||  9  < ch) ch = getchar();     while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();   }   bool cmp(const dd &a, const dd &b) { return a.r < b.r; }   void work() {     now++;     int x = A[now].x, y = A[now].y;     a[x][y] = a[y][x] = now;     for (int i = 1; i <= n; i++) a[x][i] = a[y][i] = max(a[x][i], a[y][i]);   }   int main() {     R(n);     R(m);     R(q);     for (i = 1; i <= m; i++) R(A[i].x), R(A[i].y);     for (i = 1; i <= q; i++) {       R(Q[i].l);       R(Q[i].r);       R(Q[i].s);       R(Q[i].t);       Q[i].q = i;     }     sort(Q + 1, Q + q + 1, cmp);     now = 0;     for (i = 1; i <= q; i++) {       while (now < Q[i].r) work();       if (a[Q[i].t][Q[i].s] >= Q[i].l) ans[Q[i].q] = 1;     }     for (i = 1; i <= q; i++) {       if (ans[i])         puts( Yes );       else         puts( No );     }   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar();     bool flag = false;     while (!isdigit(c)) {       if (c ==  - ) flag = true;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     if (flag) x = -x;   }   int n, m, k, lim = 1, ans, root, tot, cnt;   int ls[3000010], rs[3000010], tag[3000010], sum[3000010], val[3000010];   struct node {     int x, l, r, v;   } p[100010];   bool cmp(const node &a, const node &b) { return a.x < b.x; }   int get(int l, int r) {     int v = 0;     for (int i = 1; i <= k; i <<= 1)       v |= (((r / i - (l - 1) / i) -              (i * 2 <= k ? r / i / 2 - (l - 1) / i / 2 : 0)) &             1) *            i;     return v;   }   void pushup(int cur, int l, int r) {     if (tag[cur])       sum[cur] = val[cur];     else       sum[cur] = sum[ls[cur]] ^ sum[rs[cur]];   }   void modify(int L, int R, int l, int r, int v, int &cur) {     if (!cur) cur = ++tot;     val[cur] = get(l, r);     if (L <= l && R >= r) {       tag[cur] += v, pushup(cur, l, r);       return;     }     if (L <= ((l + r) >> 1)) modify(L, R, l, ((l + r) >> 1), v, ls[cur]);     if (R > ((l + r) >> 1)) modify(L, R, ((l + r) >> 1) + 1, r, v, rs[cur]);     pushup(cur, l, r);   }   int main() {     read(n), read(m), read(k);     while (lim <= k) lim <<= 1;     for (int i = 1; i <= m; ++i) {       int u, d, l, r;       read(l), read(u), read(r), read(d);       p[++cnt] = {l, u, d, 1}, p[++cnt] = {r + 1, u, d, -1};     }     sort(p + 1, p + cnt + 1, cmp);     for (int i = 1; i <= cnt; ++i) {       if (p[i - 1].x != p[i].x) {         int v1 = sum[root], v2 = get(p[i - 1].x, p[i].x - 1), s1 = 0, s2 = 0;         for (int j = lim; j; j >>= 1) {           s1 += ((v1 & j) != 0), s2 += ((v2 & j) != 0);           if ((s1 * s2) & 1) ans ^= j;         }       }       modify(p[i].l, p[i].r, 1, n, p[i].v, root);     }     if (ans)       puts( Hamed );     else       puts( Malek );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool Check(int X1, int Y1, int X2, int Y2, int a, int b) {     bool answ = false;     if (X1 <= a && X2 <= a && Y1 <= b && Y2 <= b)       if (!(X1 + X2 > a) || !(Y1 + Y2 > b)) answ = true;     if (Y1 <= a && X2 <= a && X1 <= b && Y2 <= b)       if (!(Y1 + X2 > a) || !(X1 + Y2 > b)) answ = true;     if (X1 <= a && Y2 <= a && Y1 <= b && X2 <= b)       if (!(X1 + Y2 > a) || !(Y1 + X2 > b)) answ = true;     if (Y1 <= a && Y2 <= a && X1 <= b && X2 <= b)       if (!(Y1 + Y2 > a) || !(X1 + X2 > b)) answ = true;     return answ;   }   int main() {     int n, a, b;     cin >> n >> a >> b;     char XY[n][2];     for (int i = 0; i < n; i++) {       int Xi, Yi;       cin >> Xi >> Yi;       XY[i][0] = Xi;       XY[i][1] = Yi;     }     int maxS = 0;     for (int i = 0; i < n - 1; i++) {       int X1 = XY[i][0];       int Y1 = XY[i][1];       for (int j = i + 1; j < n; j++) {         int X2 = XY[j][0];         int Y2 = XY[j][1];         if (Check(X1, Y1, X2, Y2, a, b) == true) {           maxS = max(maxS, X1 * Y1 + X2 * Y2);         }       }     }     cout << maxS;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());   const int N = 2e5 + 10;   const int mod = 998244353;   int a[N], b[N], p[N], l[N];   int getMin(int l, int r) {     int x = a[l];     for (int i = l; i <= r; i++) x = min(x, a[i]);     return x;   }   void zero() {     cout << 0;     exit(0);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     int ptr = m - 1;     for (int i = n - 1; i >= 0; i--) {       if (a[i] == b[ptr]) {         p[ptr--] = i;       }       if (ptr < 0) break;     }     if (ptr >= 0) {       zero();     }     l[0] = -1;     for (int i = m - 1; i >= 1; i--) {       int j = p[i];       while (b[i] <= a[j]) j--;       l[i] = j;     }     if (getMin(0, p[0]) != b[0] || getMin(p[m - 1], n - 1) != b[m - 1]) zero();     for (int i = 0; i < m - 1; i++) {       if (getMin(p[i], p[i + 1]) != b[i]) zero();     }     long long ans = 1;     for (int i = 1; i < m; i++) {       ans *= (p[i] - l[i]);       ans %= mod;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[100100][102];   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     cin.tie(0);     long long n;     cin >> n;     int ans = 0;     while (n) {       ans += (n % 10 == 7 || n % 10 == 4);       n /= 10;     }     cout << (ans == 4 || ans == 7 ?  YES  :  NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   namespace fgraph {   const int MAXV = 50;   const int MAXE = 10 * MAXV * MAXV;   const int INF = (1 << 30);   struct edge {     int to, cap, flow, cost;     edge *nxt, *rev;   } edges[MAXE + 5], *adj[MAXV + 5], *cur[MAXV + 5], *ecnt = edges;   void addedge(int u, int v, int c, int w) {     edge *p = (++ecnt), *q = (++ecnt);     (*p) = (edge){v, c, 0, w, adj[u], q}, adj[u] = p;     (*q) = (edge){u, 0, 0, -w, adj[v], p}, adj[v] = q;   }   int d[MAXV + 5], h[MAXV + 5], s, t;   bool relabel() {     for (int i = s; i <= t; i++) h[i] += d[i], d[i] = INF, cur[i] = adj[i];     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         que;     que.push(make_pair(d[t] = 0, t));     while (!que.empty()) {       int k = que.top().first, x = que.top().second;       que.pop();       if (k != d[x]) continue;       for (edge *p = adj[x]; p; p = p->nxt) {         if (p->rev->cap > p->rev->flow) {           int dis = k + p->rev->cost + (h[x] - h[p->to]);           if (dis < d[p->to]) que.push(make_pair(d[p->to] = dis, p->to));         }       }     }     return d[s] != INF;   }   bool vis[MAXV + 5];   int aug(int x, int tot) {     if (x == t) return tot;     vis[x] = true;     int sum = 0;     for (edge *&p = cur[x]; p; p = p->nxt) {       int dis = d[p->to] + p->cost + (h[p->to] - h[x]);       if (d[x] == dis && !vis[p->to] && p->cap > p->flow) {         int del = aug(p->to, min(tot - sum, p->cap - p->flow));         sum += del, p->flow += del, p->rev->flow -= del;         if (sum == tot) break;       }     }     vis[x] = false;     return sum;   }   }  // namespace fgraph   int f[55];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       fgraph::addedge(u, v, 1, w);     }     int cnt = 0;     fgraph::s = 1, fgraph::t = n;     for (cnt = 0; fgraph::relabel(); cnt++)       f[cnt + 1] = f[cnt] + fgraph::d[1] + fgraph::h[1], fgraph::aug(1, 1);     int q;     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       int x;       scanf( %d , &x);       double ans = 1E12;       for (int j = 1; j <= cnt; j++) ans = min(ans, 1.0 * (x + f[j]) / j);       printf( %.9f n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 10;   const int INF = 1e9 + 9;   const int B = 1e9 + 7;   long long p[N], s[N];   int fp(int first) {     return first == p[first] ? first : p[first] = fp(p[first]);   }   void u(int a, int b, int d) {     s[fp(a)] += 4 * d;     if (fp(a) == fp(b)) return;     s[fp(b)] += s[fp(a)];     p[fp(a)] = fp(b);   }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<long long> a(n + 1);     long long ans = 0;     for (int i = int(2); i <= int(n); ++i) p[i] = i, s[i] = 0;     for (int i = int(n); i >= int(2); --i)       for (int j = 2; i * j <= n; ++j) u(i, i * j, j);     for (int i = int(2); i <= int(n); ++i) ans = max(ans, s[fp(i)]);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 1e8;   int main() {     ios::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     int tcase;     cin >> tcase;     while (tcase--) {       int n, k;       cin >> n >> k;       vector<int> a(n + 1), b(n + 1), id(n + 1);       for (int i = 1; i <= n; i++) cin >> a[i] >> b[i], id[i] = i;       sort(id.begin() + 1, id.begin() + n + 1,            [&](int u, int v) { return b[u] < b[v]; });       vector<vector<int>> dp(n + 1, vector<int>(k + 1, -INF)),           prv(n + 1, vector<int>(k + 1, 0));       dp[0][0] = 0;       for (int i = 1; i <= n; i++) {         for (int j = 0; j <= k; j++) {           dp[i][j] = dp[i - 1][j];           if (j) {             if (dp[i][j] < dp[i - 1][j - 1] + a[id[i]] - b[id[i]] * (k - j)) {               dp[i][j] = dp[i - 1][j - 1] + a[id[i]] - b[id[i]] * (k - j);               prv[i][j] = 1;             }           }         }       }       vector<int> chk(n + 1, 0);       int cur_i = n, cur_j = k;       while (cur_i) {         if (prv[cur_i][cur_j])           chk[cur_i] = 1, cur_i--, cur_j--;         else           cur_i--;       }       vector<int> ans1, ans2;       for (int i = 1; i <= n; i++)         if (chk[i]) ans1.push_back(id[i]);       for (int i = 1; i <= n; i++)         if (!chk[i]) ans2.push_back(id[i]);       vector<int> rlt;       for (int i = 0; i < ans1.size() - 1; i++) rlt.push_back(ans1[i]);       for (auto it : ans2) rlt.push_back(it), rlt.push_back(-it);       rlt.push_back(ans1[ans1.size() - 1]);       cout << rlt.size() <<   n ;       for (int i = 0; i < rlt.size(); i++)         cout << rlt[i] <<    n [i == rlt.size() - 1];     }     return 0;   }
#include <bits/stdc++.h>      using namespace std;      const int N = 2e5+5;   const long long inf = 0x3f3f3f3f3f3f3f3f;   typedef long long ll;   #define pb push_back      int n, m;   int a[N];   int g[N], v[N<<1], nxt[N<<1], tot, fa[N];   vector<int> c[N];   ll f[N], ans;      inline void add(int x, int y) {    v[++tot] = y, nxt[tot] = g[x], g[x] = tot;   }      void dfs(int x, int ff, int dep) {    fa[x] = ff, c[dep].pb(x), m = max(m, dep);    for (int i = g[x]; i; i = nxt[i]) {     int y = v[i];     if (y == ff) continue;     dfs(y, x, dep + 1);    }   }      inline void solve() {    scanf( %d , &n);    for (int i = 2; i <= n; i++) {     int x;     scanf( %d , &x);     add(i, x), add(x, i);    }    for (int i = 2; i <= n; i++) scanf( %d , &a[i]);    dfs(1, 0, 0);    for (int r = 1; r <= m; r++) {     ll max0 = -inf, min0 = inf, max1 = -inf, max2 = -inf;     for (int i = 0; i < (int)c[r].size(); i++) {      int x = c[r][i];      max0 = max(max0, (ll)a[x]), min0 = min(min0, (ll)a[x]);      max1 = max(max1, f[fa[x]] + a[x]);      max2 = max(max2, f[fa[x]] - a[x]);     }     for (int i = 0; i < (int)c[r].size(); i++) {      int x = c[r][i];      f[x] = max(f[fa[x]] + max(max0 - a[x], a[x] - min0), max(max1 - a[x], max2 + a[x]));      if (r == m) ans = max(ans, f[x]);     }    }    printf( %lld n , ans);   }      inline void init() {    ans = m = tot = 0;    for (int i = 1; i <= n; i++) c[i].clear(), g[i] = 0, f[i] = 0;   }      int main()   {    int T;    scanf( %d , &T);    while (T--) {     init();     solve();    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[200005];   int v[200005];   int c[200005], lower;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     int sol = 123123123;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) {       cin >> v[i];       if (c[i] < v[i]) lower++;     }     int j = 0;     for (int i = 1; i <= n; i++) {       while (j < n && lower > 0) {         j++;         c[a[j]]++;         if (c[a[j]] == v[a[j]]) lower--;       }       if (!lower) sol = min(sol, j - i + 1);       c[a[i]]--;       if (c[a[i]] == v[a[i]] - 1) lower++;     }     if (sol == 123123123)       sol = -1;     else       sol = sol - accumulate(v + 1, v + m + 1, 0);     cout << sol <<   n ;   }