malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     string c;     cin >> c;     string ans = regex_replace(c, regex( (/)+ ),  / );     if (ans[ans.size() - 1] ==  /  and ans.size() > 1) {       ans[ans.size() - 1] =    ;     }     cout << ans << endl;     ;   }
#include <bits/stdc++.h>   using namespace std;   const long long OO = 1e18;   vector<int> prs = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};   long long solve(long long num, int in, int lst) {     if (in == prs.size() || lst == 0) return num == 1 ? 1 : -1;     long long res = -1;     long long cur = 1;     long long cp = num;     for (int i = 0; i <= lst; i++) {       if (num < (i + 1) || cur > OO || (i && cur > OO / prs[in])) break;       if (i) cur *= prs[in];       if (num % (i + 1) != 0) continue;       long long can = solve(num / (i + 1), in + 1, i);       if ((res == -1 && can != -1) || (can != -1 && can < (res + cur - 1) / cur))         res = cur * can;     }     return res;   }   int main() {     long long n;     cin >> n;     cout << solve(n, 0, 100);   }
#include <bits/stdc++.h>   uint16_t C[10][10 + 1];   void flow(int i, int j, uint16_t c) {     if (i >= 10) {       return;     }     if (C[i][j] == (1 << 10)) {       flow(i + 1, j, c >> 1);       flow(i + 1, j + 1, c >> 1);     } else {       C[i][j] += c;     }   }   int main() {     int n, t;     std::cin >> n >> t;     for (int i = 0; i < n; i++) {       for (int j = 0; j <= i; j++) {         C[i][j] = 0;       }     }     while (t--) {       flow(0, 0, (1 << 10));     }     int r = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j <= n; j++) {         if (C[i][j] == (1 << 10)) {           r++;         }       }     }     std::cout << r <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10, Q = 1e5 + 10;   int n, q;   struct edg {     int x, y, t;     bool operator==(edg a) { return x == a.x && y == a.y; }   } e[Q];   bool cmp(edg a, edg b) {     return a.x < b.x || (a.x == b.x && a.y < b.y) ||            (a.x == b.x && a.y == b.y && a.t < b.t);   }   class stk {    private:     int tp;     struct elm {       int x, y, rnk;     } e[Q << 2];       public:     void push(int x, int y, int rnk) {       e[++tp] = (elm){x, y, rnk};       return;     }     elm top() { return e[tp]; }     int size() { return tp; }     void pop() {       if (tp) tp--;       return;     }   } s;   class ufs {    private:     struct elm {       int fth, rnk;     } d[N << 1];       public:     void init() {       for (int i = 1; i <= (n << 1); i++) d[i].fth = i, d[i].rnk = 0;       return;     }     inline int find(int x) {       while (x != d[x].fth) x = d[x].fth;       return x;     }     bool check(int x, int y) { return find(x) == find(y); }     void unite(int x, int y) {       x = find(x), y = find(y);       if (x == y) return;       if (d[x].rnk < d[y].rnk) swap(x, y);       s.push(x, y, d[x].rnk == d[y].rnk);       d[y].fth = x;       d[x].rnk += d[x].rnk == d[y].rnk;       return;     }     void divide() {       int x = s.top().x, y = s.top().y;       d[y].fth = y;       d[x].rnk -= s.top().rnk;       s.pop();       return;     }   } u;   class sgt {    private:     struct elm {       int x, y;     };     vector<elm> v[Q << 2];       public:     void insert(int i, int l, int r, int lq, int rq, int x, int y) {       if (l > rq || r < lq) return;       if (l >= lq && r <= rq) {         v[i].push_back((elm){x, y});         return;       }       int mid = l + r >> 1;       insert(i << 1, l, mid, lq, rq, x, y);       insert(i << 1 | 1, mid + 1, r, lq, rq, x, y);       return;     }     void query(int i, int l, int r) {       int sz = s.size();       bool flag = true;       for (int j = 0; j < v[i].size() && flag; j++) {         int x = v[i][j].x, y = v[i][j].y;         if (u.check(x, y))           flag = false;         else           u.unite(x, y + n), u.unite(x + n, y);       }       if (flag) {         if (l == r)           puts( YES );         else {           int mid = l + r >> 1;           query(i << 1, l, mid);           query(i << 1 | 1, mid + 1, r);         }       } else         for (int i = l; i <= r; i++) puts( NO );       while (s.size() > sz) u.divide();       return;     }   } t;   inline int rd() {     int f = 1, x = 0;     char c = getchar();     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -1;     for (; c >=  0  && c <=  9 ; c = getchar()) x = (x << 3) + (x << 1) + c -  0 ;     return f * x;   }   int main() {     n = rd(), q = rd();     u.init();     for (int i = 1; i <= q; i++) e[i].x = rd(), e[i].y = rd(), e[i].t = i;     sort(e + 1, e + 1 + q, cmp);     for (int i = 1; i <= q; i++) {       if (e[i] == e[i + 1])         t.insert(1, 1, q, e[i].t, e[i + 1].t - 1, e[i].x, e[i].y), i++;       else         t.insert(1, 1, q, e[i].t, q, e[i].x, e[i].y);     }     t.query(1, 1, q);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const long long mo = 1e9 + 7;   const long long inf = 1e9;   const long long Inf = 1e18;   long long w[N], a[N];   int x[N], y[N];   int main() {     int n, m, k;     scanf( %d%d%d , &n, &k, &m);     for (int i = (1); i <= (n); ++i) scanf( %lld , &w[i]);     sort(w + 1, w + n + 1);     for (int i = (1); i <= (m); ++i) w[i] += w[i - 1], a[i] = w[i];     for (int i = (1); i <= (k); ++i) scanf( %d%d , &x[i], &y[i]);     for (int i = (1); i <= (m); ++i)       for (int j = (1); j <= (k); ++j)         if (x[j] <= i) a[i] = min(a[i - x[j]] + w[i] - w[i - x[j] + y[j]], a[i]);     printf( %lld n , a[m]);   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll LINF = 1001002003004005006ll;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }   template <class T>   bool chmax(T &a, const T &b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool chmin(T &a, const T &b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   void solve() {     ll n, m;     cin >> n >> m;     vector<tuple<ll, ll, ll>> v(n);     for (int i = 0; i < n; i++) {       ll t, l, h;       cin >> t >> l >> h;       v[i] = make_tuple(t, l, h);     }     sort(v.begin(), v.end());     ll L = m, H = m, T = 0;     for (int i = 0; i < n; i++) {       ll now = get<0>(v[i]), l = get<1>(v[i]), h = get<2>(v[i]);       L = L - (now - T);       H = H + (now - T);       if (h < L or H < l) {         cout <<  NO  << endl;         return;       }       chmax(L, l);       chmin(H, h);       T = now;     }     cout <<  YES  << endl;     return;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(0);     int q;     cin >> q;     while (q--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int solve(int x) {     int sum = 0;     while (x >= 5) {       sum += x / 5;       x /= 5;     }     return sum;   }   int main() {     int m;     cin >> m;     int k = 0;     vector<long long> vec;     for (int i = 1; i <= 1000000; i++) {       long long sum = solve(i);       if (sum == m) {         k++;         vec.push_back(i);       } else if (sum > m)         break;     }     cout << k << endl;     for (int i = 0; i < k; i++) cout << vec[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   const int INF = (int)1E9;   const long long INF64 = (long long)1E18;   const long double EPS = 1E-8;   const long double PI = 3.1415926535897932384626433832795;   const int MAXN = 5200;   const int MAXF = 1001000;   long long d1[MAXN], d[MAXN], D[MAXN][MAXN], p;   long long f[MAXF], a[MAXF];   vector<long long> l;   int n, m;   long long get(int n, int c) {     if (n == 0 && c == 0) return 1;     if (n == 0 || c == 0) return 0;     long long &ans = D[n][c];     if (ans != -1) return ans;     ans = (get(n - 1, c - 1) + get(n - 1, c) * (c - 1)) % p;     return ans;   }   int main() {     cin >> n >> m >> p;     f[0] = a[0] = 1;     for (int i = 1; i < MAXF; i++) {       f[i] = (f[i - 1] * i) % p;       a[i] = (a[i - 1] * (m - i + 1)) % p;     }     int prev = -1;     long long sum = 0;     memset(D, 255, sizeof D);     for (int step = 0; step < (int)(n); step++) {       int l;       scanf( %d , &l);       for (int i = 1; i <= l; i++)         if (prev == -1) {           long long cur = get(l, i) % p;           cur = (cur * a[i]) % p;           d1[i] = cur;         } else {           long long cur = get(l, i) % p;           cur = (cur * a[i]) % p;           cur = (cur * sum) % p;           long long ppp = 0;           if (i <= prev) {             ppp = get(l, i) % p;             ppp = (ppp * f[i]) % p;             ppp = (ppp * d[i]) % p;           }           d1[i] = (cur - ppp + p) % p;         }       sum = 0;       for (int i = 0; i < (int)(l + 1); i++) {         sum = (sum + d1[i]) % p;         d[i] = d1[i];       }       prev = l;     }     long long ans = 0;     for (int i = 0; i < (int)(prev + 1); i++) ans = (ans + d[i]) % p;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string op;     int x = 0;     for (int k = 0; k < n; k++) {       cin >> op;       if (op ==  ++X  || op ==  X++ ) {         x++;       } else if (op ==  --X  || op ==  X-- ) {         x--;       }     }     cout << x << endl;   }
#include <bits/stdc++.h>   using namespace std;   bool isprime(long long k) {     for (long long i = 2; i <= sqrt(k); i++)       if (k % i == 0) return false;     return true;   }   long long bm(long long a, long long b, long long mod) {     if (b == 0) return 1;     long long t = bm(a, b / 2, mod);     t = t * t % mod;     return (b % 2 == 1 ? t * a % mod : t);   }   long long inv(long long a, long long mod) { return bm(a, mod - 2, mod); }   void gay(long long TC) { cout <<  Case #  << TC <<  :  ; }   void solve(long long TC) {     string s;     cin >> s;     long long n = s.length();     if (n % 2) {       cout <<  NO n ;       return;     }     if (s.substr(0, n / 2) != s.substr(n / 2, n / 2)) {       cout <<  NO n ;     } else       cout <<  YES n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     for (long long i = 1; i <= t; i++) solve(i);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, i, a[1000021], j;   int main() {     scanf( %d%d , &n, &k);     if (n / k <= 2) {       cout << -1;       exit(0);     }     for (i = 1; i <= k * ((n / k) - 1); i++) a[i] = (i - 1) / ((n / k) - 1) + 1;     j = ((n / k) - 1) * k;     while (j <= n)       for (i = 1; i <= k; i++)         if (j <= n) a[++j] = i;     for (i = 1; i <= n; i++) printf( %d  , a[i]);     return 0;   }
#include <bits/stdc++.h>   int aa[100005];   int main(void) {     int n;     while (~scanf( %d , &n)) {       for (int i = 0; i < n; i++) {         scanf( %d , &aa[i]);       }       for (int i = 0; i < n - 1; i++) {         printf( %d  , aa[i] + aa[i + 1]);       }       printf( %d n , aa[n - 1]);     }   }
#include <bits/stdc++.h>   using namespace std;   static const int maxn = 3e5 + 5;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n;     cin >> n;     multiset<int> ms;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       ms.insert(x);     }     int ans = 0;     while (ms.empty() == false) {       int mini = *ms.begin();       ++ans;       vector<int> del;       for (int x : ms) {         if (x % mini == 0) del.push_back(x);       }       for (int x : del) ms.erase(ms.find(x));     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   int vis[200005];   int comp_size;   int cycle_length;   void dfs(int cur, int prev, int t, vector<vector<int>> &edges) {     vis[cur] = t;     comp_size++;     for (int e : edges[cur]) {       if (vis[e] == -1) {         dfs(e, cur, t + 1, edges);       } else if (e != prev) {         if (cycle_length == 0) cycle_length = t - vis[e] + 1;       }     }   }   long long mod_exp(int a, int b) {     if (b == 0) {       return 1;     } else if (b == 1) {       return a;     }     long long c;     if (b % 2 == 0) {       c = mod_exp(a, b / 2);       c = c * c % MOD;     } else {       c = mod_exp(a, (b - 1) / 2);       c = c * c * a % MOD;     }     return c;   }   int main() {     int n;     memset(vis, -1, sizeof vis);     cin >> n;     vector<vector<int>> edges(n);     for (int i = 0; i < n; i++) {       int a;       cin >> a;       a--;       edges[i].push_back(a);       edges[a].push_back(i);     }     long long ans = 1;     for (int i = 0; i < n; i++) {       if (vis[i] == -1) {         comp_size = 0;         cycle_length = 0;         dfs(i, -1, 1, edges);         cycle_length = cycle_length == 0 ? 2 : cycle_length;         ans = (ans * mod_exp(2, comp_size - cycle_length) % MOD) *               (mod_exp(2, cycle_length) - 2) % MOD;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long pw[16], t[20], na[30], ma[30], ta[30], per[30];   vector<long long> rem;   void calc_pw() {     long long i;     pw[0] = 1;     for (i = 1; i <= 11; i++) pw[i] = 7 * pw[i - 1];   }   long long conv(long long num) {     long long ret = 0, temp;     temp = num;     while (temp > 0) {       t[ret++] = temp % 7;       temp /= 7;     }     return ret;   }   int main() {     long long i, j, n, m, temp, mx_n, mx_m, cur, ans = 0, idx, k, x, t1 = 0,                                                  t2 = 0;     cin >> n >> m;     calc_pw();     for (i = 0; i < 7; i++) rem.push_back(i);     mx_n = max(1LL, conv(n - 1));     mx_m = max(1LL, conv(m - 1));     for (k = 0; k < (1 << rem.size()); k++) {       temp = 0;       for (x = 0; x < rem.size(); x++)         if (k & (1 << x)) temp++;       if (temp == mx_m + mx_n && temp > 0) {         idx = 0;         for (j = 0; j < rem.size(); j++)           if (k & (1 << j)) per[idx++] = rem[j];         do {           idx = 0;           t1 = 0;           t2 = 0;           for (j = mx_m - 1; j >= 0; j--) t1 += pw[j] * per[j];           for (j = mx_m; j < mx_m + mx_n; j++) t2 += pw[idx++] * per[j];           if (t1 < m && t2 < n && (t1 != 0 || t2 != 0)) ans++;         } while (next_permutation(per, per + mx_m + mx_n));       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       long long x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       if (x1 == x2) {         cout << abs(y1 - y2) << endl;       } else if (y1 == y2) {         cout << abs(x1 - x2) << endl;       } else         cout << abs(x1 - x2) + abs(y1 - y2) + 2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, s;       cin >> n >> s;       long long cnt = 0, x = n;       while (x) {         cnt += x % 10;         x /= 10;       }       long long ans = 0, now = 1;       while (1) {         if (cnt <= s) {           cout << ans << endl;           break;         }         long long p = n % 10;         if (!p) p = 10;         ans += (10 - p) * now;         n += 10 - p;         n /= 10;         x = n;         cnt = 0;         while (x) {           cnt += x % 10;           x /= 10;         }         now *= 10;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, count = 0;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       scanf( %1d , &x);       if (x) {         count++;         continue;       }       break;     }     cout << min(count + 1, n);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y;   int work() {     int ans = 0;     int b = x % m;     if (b == 0) b = m;     int a = x / m + (b != m);     int d = y % m;     if (d == 0) d = m;     int c = y / m + (d != m);     if (a == c) return 1;     if (b == 1 && (d == m || y == n)) return 1;     if (x == 1) {       if (c == 1) return 1;       if (d == m) return 1;       if (y == n) return 1;       return 2;     }     if (a + 1 == c) return 2;     if (d + 1 == b) return 2;     if (b == 1) return 2;     if (d == m) return 2;     if (y == n) return 2;     return 3;   }   int main() {     while (cin >> n >> m >> x >> y) printf( %d , work());     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int M[52][52];   int main() {     int n;     cin >> n;     for (int i = 0; i < n * (n - 1) / 2 - 1; i++) {       int a, b;       cin >> a >> b;       a--, b--;       M[a][b]++;     }     int _x = -1, _y = -1, x, y;     bool enc = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         for (int k = 0; k < n; k++) {           if ((M[i][j] == 0) && (M[j][i] == 0) && (i != j)) {             x = i + 1;             y = j + 1;           }           if ((M[i][j] == 1) && (M[j][k] == 1) && (M[i][k] == 0)) {             _x = i + 1;             _y = k + 1;             enc = 1;           }         }       }     }     if (enc) {       cout << _x <<     << _y << endl;     } else {       cout << x <<     << y << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5010;   int n, arr[N], ans[(N << 1)];   vector<int> a[N], b[N];   const int inv2 = 500000004;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &arr[i]);     }     for (int i = n - 2; i >= 0; i--) {       a[i].resize((int)a[i + 1].size() + 1);       for (int j = 1; j < (int)a[i].size(); j++) {         a[i][j] = (long long)a[i + 1][j - 1] * arr[i] % 1000000007;       }       a[i][0] = arr[i];     }     b[0] = a[0];     for (int i = 0; i < (int)b[0].size(); i++) ans[i] = b[0][i];     int cur = arr[0];     for (int i = 1; i < n; i++) {       swap(b[i], b[i - 1]);       b[i][0]--;       if (b[i][0] < 0) b[i][0] += 1000000007;       for (int j = 0; j < (int)a[i].size(); j++) {         b[i][j + 1] -= a[i][j];         if (b[i][j + 1] < 0) b[i][j + 1] += 1000000007;       }       b[i].push_back(0);       for (int j = (int)b[i].size() - 1; j >= 0; j--) {         b[i][j] = b[i][j - 1];       }       b[i][0] = 1;       for (int j = 0; j < (int)a[i].size(); j++) {         b[i][j] += a[i][j];         if (b[i][j] >= 1000000007) b[i][j] -= 1000000007;       }       for (int j = 0; j < (int)b[i].size(); j++) {         ans[j] += (long long)b[i][j] * cur % 1000000007;         if (ans[j] >= 1000000007) ans[j] -= 1000000007;       }       cur = (long long)cur * arr[i] % 1000000007;     }     for (int i = 1; i <= 2 * n - 2; i++) {       if (i > 1) putchar(   );       printf( %d , (long long)ans[i - 1] * inv2 % 1000000007);     }     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100000 + 10;   map<long long, long long> mp;   int main() {     long long n, k, p, x, ans = 0;     scanf( %lld%lld%lld , &n, &p, &k);     for (long long i = 0; i < n; ++i) {       scanf( %lld , &x);       mp[((x * x % p * x % p * x % p - x * k % p) % p + p) % p]++;     }     for (auto e : mp) {       x = e.second;       ans += x * (x - 1) / 2;     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 30;   int main() {     int n, m;     scanf( %d%d , &n, &m);     int maxc = 0;     for (int i = 0; i < n; i++) {       int minc = INF;       for (int j = 0; j < m; j++) {         int cij;         scanf( %d , &cij);         if (minc > cij) minc = cij;       }       if (maxc < minc) maxc = minc;     }     printf( %d n , maxc);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long gcd(long long a, long long b) {     a = ((a) < 0 ? -(a) : (a));     b = ((b) < 0 ? -(b) : (b));     while (b) {       a = a % b;       swap(a, b);     }     return a;   }   const long long inf = 2147383647;   const long long mod = 1000000007;   const double pi = 2 * acos(0.0);   const double eps = 1e-9;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int main() {     int t;     cin >> t;     int x, y, z, a, b, c, ans;     for (int kas = 1; kas <= t; kas++) {       scanf( %d , &x);       scanf( %d , &y);       scanf( %d , &z);       a = min(x, y);       b = max(x, y);       c = b - a;       if (z <= a) {         ans = (x + y + z) / 3;         ans = min(ans, a);       } else         ans = a;       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  9  || s[i] ==  H  || s[i] ==  Q ) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;   }
#include <bits/stdc++.h>   int Pos(int a) {     if (a < 0) {       return 0;     }     return a;   }   int main() {     int n = 0, p1 = 0, t1 = 0, p2 = 0, t2 = 0, p3 = 0;     int energy = 0;     std::vector<int> timeDatal;     std::vector<int> timeDatar;     std::cin >> n >> p1 >> p2 >> p3 >> t1 >> t2;     for (int i = 0; i < n; i++) {       int input;       std::cin >> input;       timeDatal.push_back(input);       std::cin >> input;       timeDatar.push_back(input);       energy += p1 * (timeDatar[i] - timeDatal[i]);       if (i != 0) {         int delta = timeDatal[i] - timeDatar[i - 1];         if (delta - t1 >= 0) {           energy += t1 * p1;         } else if (delta >= 0) {           energy += delta * p1;         }         delta -= t1;         if (delta - t2 >= 0) {           energy += t2 * p2;         } else if (delta >= 0) {           energy += delta * p2;         }         delta -= t2;         if (delta > 0) {           energy += p3 * delta;         }       }     }     std::cout << energy;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010, mod = 998244353;   inline int read() {     int x = 0, f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) f |= ch ==  - , ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return f ? -x : x;   }   int n, fa[maxn], dep[maxn], last = 1, chg, ans[maxn], al, seq[maxn], sl;   set<int> s[maxn];   int main() {     n = read();     for (int i = (2); i <= (n); i++) {       dep[i] = dep[fa[i] = read() + 1] + 1;       s[fa[i]].insert(i);       if (dep[i] > dep[last]) last = i;     }     chg = last;     while (chg != 1) {       while (chg != 1 && s[fa[chg]].size() == 1) chg = fa[chg];       if (chg != 1) {         int was = fa[chg];         set<int>::iterator hhh = s[was].find(chg);         for (set<int>::iterator it = s[was].begin(); it != s[was].end(); it++) {           int f = *it;           if (f == chg) continue;           s[f].insert(chg);           fa[chg] = f;           ans[++al] = chg - 1;           s[was].erase(hhh);           break;         }       }     }     for (int i = last; i; i = fa[i]) seq[++sl] = i - 1;     for (int i = (n); i >= (1); i--) printf( %d  , seq[i]);     printf(  n%d n , al);     for (int i = (al); i >= (1); i--) printf( %d  , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   void solve() {     int n, k;     cin >> n >> k;     vector<int> a(n * k);     for (auto &it : a) cin >> it;     reverse(a.begin(), a.end());     long long ans = 0;     int mid = n / 2, cnt = 0;     for (int i = mid; i < (int)a.size() && cnt++ != k; i += mid + 1) {       ans += a[i];     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       solve();     }   }