malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, n, a, b, c, ret = 0, t;     scanf( %d %d %d %d , &n, &a, &b, &c);     n *= 10;     for (i = 0; i <= a; i++)       for (j = 0; j <= b; j++) {         t = (i * 5);         t += (j * 10);         if (n - t < 0) continue;         if ((n - t) % 20 == 0 && (n - t) / 20 <= c) ret++;       }     printf( %d n , ret);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long parse(string s) {     long long k = 1, ret = 0;     for (int i = 0; i < s.size(); i++)       ret += k * (long long)(s[s.size() - i - 1] -  0 ), k *= 4;     return ret;   }   string change(string s) {     string ret = s;     for (int i = 0; i < ret.size(); i++) {       if (ret[i] ==  1 )         ret[i] =  2 ;       else if (ret[i] ==  2 )         ret[i] =  3 ;       else if (ret[i] ==  3 )         ret[i] =  1 ;     }     return ret;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long idx = (n + 2) / 3, m = n % 3;       long long k = 0, sum = 0;       while (sum < idx) sum += (1ll << (2 * k)), k++;       k--;       long long buf = (1ll << (2 * k)), c = sum - buf + 1;       string ans =  1 ;       for (int i = k - 1; i >= 0; i--) {         long long div = (1ll << (2 * i));         long long d = (idx - c) / div;         ans += (char)( 0  + d);         c += div * d;       }       long long f = parse(ans);       long long s = parse(change(ans));       if (m == 1)         cout << f <<   n ;       else if (m == 2)         cout << s <<   n ;       else         cout << (f ^ s) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   inline void sub(long long &a, long long b) {     a -= b;     if (a < 0) a += 1000000007;   }   inline void add(long long &a, long long b) {     a += b;     if (a >= 1000000007) a -= 1000000007;   }   template <typename T>   inline T const &MAX(T const &a, T const &b) {     return a > b ? a : b;   }   template <typename T>   inline T const &MIN(T const &a, T const &b) {     return a < b ? a : b;   }   inline long long qp(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % 1000000007;       a = a * a % 1000000007, b >>= 1;     }     return ans;   }   inline long long qp(long long a, long long b, long long c) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % c;       a = a * a % c, b >>= 1;     }     return ans;   }   using namespace std;   const unsigned long long ba = 233;   const double eps = 1e-6;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const int N = 100000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f;   long long h[N], a[N];   int cnt, pre[N];   struct sgt {     pair<int, int> ma[N << 2];     void update(int pos, pair<int, int> v, int l, int r, int rt) {       if (l == r) {         ma[rt] = v;         return;       }       int m = (l + r) >> 1;       if (pos <= m)         update(pos, v, l, m, rt << 1);       else         update(pos, v, m + 1, r, rt << 1 | 1);       ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]);     }     pair<int, int> query(int L, int R, int l, int r, int rt) {       if (L <= l && r <= R) return ma[rt];       int m = (l + r) >> 1;       pair<int, int> ans = make_pair(0, 0);       if (L <= m) ans = max(ans, query(L, R, l, m, rt << 1));       if (m < R) ans = max(ans, query(L, R, m + 1, r, rt << 1 | 1));       return ans;     }   } sg;   int main() {     int n, d;     scanf( %d%d , &n, &d);     for (int i = 1; i <= n; i++) {       scanf( %lld , &h[i]);       a[cnt++] = h[i];     }     sort(a, a + cnt);     cnt = unique(a, a + cnt) - a;     for (int i = 1; i <= n; i++) {       pair<int, int> x = make_pair(0, 0), y;       int p = upper_bound(a, a + cnt, h[i] - d) - a;       p--;       if (p >= 0) x = max(x, sg.query(1, p + 1, 1, cnt, 1));       p = lower_bound(a, a + cnt, h[i] + d) - a;       if (p < cnt) x = max(x, sg.query(p + 1, cnt, 1, cnt, 1));       p = lower_bound(a, a + cnt, h[i]) - a;       y = sg.query(p + 1, p + 1, 1, cnt, 1);       if (x.first + 1 > y.first) {         sg.update(p + 1, make_pair(x.first + 1, i), 1, cnt, 1);         pre[i] = x.second;       }     }     pair<int, int> x = sg.query(1, cnt, 1, cnt, 1);     printf( %d n , x.first);     stack<int> st;     for (int now = x.second; now != 0; now = pre[now]) st.push(now);     while (!st.empty()) printf( %d  , st.top()), st.pop();     return 0;   }
#include <bits/stdc++.h>   const int N = 1e5 + 10;   const int MOD = 1e9 + 7;   const int INF = 2e9;   const long long INFLL = 1e18;   const double PI = acos((double)-1);   inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }   inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }   inline int fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   using namespace std;   int main() {     ios::sync_with_stdio(false);     string s;     cin >> s;     int n = s.length();     int x = 0, y = 0;     int ans = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  - ) {         y++;         if (x > 0)           x--;         else           ans++;       } else {         x++;         if (y > 0)           y--;         else           ans++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     if (n == 1 and k == 10) {       cout <<  -1 ;     } else if (k == 10) {       cout <<  1 ;       for (int i = 1; i < n; i++) {         cout <<  0 ;       }     } else {       for (int i = 0; i < n; i++) {         cout << k;       }     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int in() {     int x;     scanf( %d , &x);     return x;   }   int main() {     ios::sync_with_stdio(false);     long long k, d, t;     cin >> k >> d >> t;     t *= 2;     long long p = (k + d - 1) / d * d;     long double res = t / (p + k) * p;     t %= (p + k);     if (t > 2 * k) {       res += k;       t -= 2 * k;       res += t;     } else       res += (long double)t / 2;     cout << setprecision(10) << fixed << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 23;   long long f[maxn][3][2];   int a[maxn], n;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     if (n == 1) {       return 0 * printf( %d , a[1]);     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j < 3; j++) f[i][j][0] = f[i][j][1] = -1e18;     }     f[0][0][0] = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 3; j++) {         for (int k = 0; k < 2; k++) {           f[i + 1][(j + 1) % 3][k] =               max(f[i + 1][(j + 1) % 3][k], f[i][j][k] - a[i + 1]);           f[i + 1][j][k] = max(f[i + 1][j][k], f[i][j][k] + a[i + 1]);           f[i + 2][j][k | 1] =               max(f[i + 2][j][k | 1], f[i][j][k] + a[i + 1] + a[i + 2]);           f[i + 2][(j + 2) % 3][k | 1] =               max(f[i + 2][(j + 2) % 3][k | 1], f[i][j][k] - a[i + 1] - a[i + 2]);         }       }     }     cout << f[n][(1 - n % 3 + 3) % 3][1];   }
#include <bits/stdc++.h>   using namespace std;   int n;   double arr[550];   int m;   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> arr[i];     double ans = 0;     for (int i = 1; i <= m; i++) {       int u, v;       double w;       cin >> u >> v;       cin >> w;       double rr = (arr[u] + arr[v]) / w;       if (rr > ans) ans = rr;     }     printf( %.9lf n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   const long double PI = acos(-1.0);   long long diff[200010];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     ;     int T, j, t, i, buckN = 1;     int N = 1;     scanf( %d , &T);     long long sum = 0, a, x, lE = 0;     while (T--) {       scanf( %d , &t);       if (t == 1) {         scanf( %lld%lld , &a, &x);         sum += a * x;         diff[a] -= x;         if (a == N) {           diff[a] += x;           lE += x;         }       } else if (t == 2) {         scanf( %lld , &a);         diff[N] = a - lE;         lE = a;         N++;         sum += a;       } else {         sum -= lE;         --N;         lE -= diff[N];         diff[N] = 0;       }       printf( %.10f n , (1.0 * sum) / N);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string arr[3];     for (int i = 0; i < 3; i++) {       cin >> arr[i];       for (int j = 0; j < arr[i].size(); j++)         if (arr[i][j] ==  ;  || arr[i][j] ==  -  || arr[i][j] ==  _ ) {           arr[i].erase(j, 1);           j--;         }       for (int j = 0; j < arr[i].size(); j++)         if (arr[i][j] >=  A  && arr[i][j] <=  Z ) arr[i][j] += 32;     }     string s1 = arr[0] + arr[1] + arr[2], s2 = arr[0] + arr[2] + arr[1],            s3 = arr[1] + arr[0] + arr[2], s4 = arr[1] + arr[2] + arr[0],            s5 = arr[2] + arr[0] + arr[1], s6 = arr[2] + arr[1] + arr[0];     string s;     int n;     cin >> n;     while (n--) {       cin >> s;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  ;  || s[i] ==  -  || s[i] ==  _ ) {           s.erase(i, 1);           i--;         }         if (s[i] >=  A  && s[i] <=  Z ) s[i] += 32;       }       if (s == s1 || s == s2 || s == s3 || s == s4 || s == s5 || s == s6)         cout <<  ACC  << endl;       else         cout <<  WA  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int fir[201], tot, in[201], In[201];   int nxt[400201], to[400201];   inline void add_edge(int u, int v) {     to[++tot] = v;     nxt[tot] = fir[u];     fir[u] = tot;   }   int n, c[201], cc[4][201];   queue<int> q[5];   inline int solve(int x) {     int ans = 0;     for (register int i = 1; i <= 3; i++) {       for (register int j = 1; j <= cc[i][0]; j++) {         if (!in[cc[i][j]]) q[i].push(cc[i][j]);       }     }     int now = x - 1;     while (1) {       now++;       if (now == 4) now = 1;       while (!q[now].empty()) {         ans++;         int u = q[now].front();         q[now].pop();         for (register int i = fir[u]; i; i = nxt[i]) {           int v = to[i];           in[v]--;           if (!in[v]) {             q[c[v]].push(v);           }         }       }       if (q[1].empty() && q[2].empty() && q[3].empty()) return ans;       ans++;     }   }   int main() {     scanf( %d , &n);     for (register int i = 1; i <= n; i++) {       scanf( %d , c + i);       cc[c[i]][++cc[c[i]][0]] = i;     }     for (register int i = 1; i <= n; i++) {       int sze = 0, tmp = 0;       scanf( %d , &sze);       for (register int j = 1; j <= sze; j++) {         scanf( %d , &tmp);         add_edge(tmp, i);         In[i]++;         in[i]++;       }     }     int ans = 0x7fffffff;     ans = min(ans, solve(1));     for (register int i = 1; i <= n; i++) in[i] = In[i];     ans = min(ans, solve(2));     for (register int i = 1; i <= n; i++) in[i] = In[i];     ans = min(ans, solve(3));     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   const int inf = 0x3f3f3f3f;   int main() {     long long n, m, k, p[maxn], ans = 0, sub = 0;     cin >> n >> m >> k;     for (int i = 1; i <= m; i++) {       scanf( %lld , &p[i]);       p[i]--;     }     for (int i = 1; i <= m; i++) {       long long now = (p[i] - sub) / k;       for (int j = 1;; j++) {         long long a = (p[i + j] - sub) / k;         if (i + j > m || a != now) {           sub += j;           i += j - 1;           ans++;           break;         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1 * 1000 + 10;   int ans[MAXN][MAXN];   int main() {     ios::sync_with_stdio(false);     int n, d, k;     cin >> n;     vector<int> a(n), b(n);     for (int i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(a.begin(), a.end(), [&](int a, int b) { return a <= b; });     int ct = 0;     for (int i = 0; i < n; i++) ct = (a[i] == b[i]) ? ct : ct + 1;     if (ct == 0 || ct == 2)       cout <<  YES ;     else       cout <<  NO ;   }
#include <bits/stdc++.h>   using namespace std;   int dp[2010][2010], a[2010];   int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     int n, i, j, ans, mn, cnt;     scanf( %d , &n);     cnt = 0;     for (i = 0; i < n; i++) {       scanf( %d , a + i);       if (a[i] == 1) cnt++;     }     if (cnt) {       ans = n - cnt;     } else {       mn = 0x3fffffff;       for (i = 0; i < n; i++) {         for (j = i; j < n; j++) {           dp[i][j] = (i == j ? a[i] : gcd(dp[i][j - 1], a[j]));           if (dp[i][j] == 1) mn = min(mn, j - i);         }       }       if (mn == 0x3fffffff)         ans = -1;       else         ans = mn + n - 1;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[1000005];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     sort(a + 1, a + 1 + n);     int x = 1;     int y = n;     long long ans = 0;     while (x < y) {       ans += abs(a[y] - a[x]) * 2;       x += m;       y -= m;     }     printf( %lld , ans);     return 0;   }
#include <bits/stdc++.h>   const int inf = 1e9 + 7;   const int mod = 998244353;   namespace __________ {   namespace _in {   template <class T>   inline T read() {     char ch;     bool flag = 0;     T x = 0;     while (ch = getchar(), !isdigit(ch))       if (ch ==  - ) flag = 1;     while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();     return flag ? -x : x;   }   struct {     inline operator long long() { return read<long long>(); }     inline operator int() { return read<int>(); }     inline operator bool() { return read<bool>(); }     template <class T>     inline void operator()(T &x) {       x = *this;     }     template <class T, class... A>     inline void operator()(T &x, A &...a) {       x = *this, this->operator()(a...);     }   } IN;   }  // namespace _in   namespace _std {   template <class T>   inline T abs(T x) {     if (x < 0) x = -x;     return x;   }   template <class T>   inline T max(T x, T y) {     if (x < y) x = y;     return x;   }   template <class T>   inline T min(T x, T y) {     if (x > y) x = y;     return x;   }   template <class T>   inline void swap(T &x, T &y) {     x ^= y ^= x ^= y;   }   template <class T>   inline void chkmax(T &x, T y) {     if (x < y) x = y;   }   template <class T>   inline void chkmin(T &x, T y) {     if (x > y) x = y;   }   }  // namespace _std   namespace _mod {   inline int mul(int x, int y) { return 1ll * x * y % mod; }   inline int dec(int x, int y) {     x -= y;     if (x < 0) x += mod;     return x;   }   inline int add(int x, int y) {     x += y;     if (x >= mod) x -= mod;     return x;   }   inline void pls(int &x, int y) {     x += y;     if (x >= mod) x -= mod;   }   inline void sub(int &x, int y) {     x -= y;     if (x < 0) x += mod;   }   inline int modpow(int x, int y, int res = 1) {     if (y == -1) y = mod - 2;     for (; y; y >>= 1, x = mul(x, x))       if (y & 1) res = mul(res, x);     return res;   }   }  // namespace _mod   using namespace _in;   using namespace _std;   using namespace _mod;   }  // namespace __________   using namespace __________;   const int N = 2e3 + 5;   int n, k, L[N], R[N], a[N];   long long dp[N], sum;   inline void calc(int &tot, int p, int &tim) {     int _, a = ::a[p], L = ::L[p];     if (a <= tot)       tot -= a, tim = L;     else       _ = a - tot, tot = (k - (_ % k)) % k, tim = L + (_ / k - (_ % k == 0)) + 1;   }   inline void solve() {     IN(n, k);     for (int i = 1; i <= n; ++i) IN(L[i], R[i], a[i]), sum += a[i];     dp[n + 1] = 0;     for (int i = n; i >= 1; --i) {       dp[i] = 1e18;       int tot = k, tim;       for (int j = i; j <= n; ++j) {         calc(tot, j, tim);         if (tim > R[j]) break;         if (j == n) dp[i] = 0;         if (L[j + 1] > tim) chkmin(dp[i], dp[j + 1] + tot);       }     }     printf( %lld n , dp[1] > 1e14 ? -1 : dp[1] + sum);   }   int main() {     int T = 1;     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     long long T, N, M;     cin >> T;     while (T--) {       cin >> N >> M;       N /= M;       long long Sum = 0, Ans = 0;       long long digits[20];       for (int i = 1; i <= 10; i++) Sum += (digits[i] = (i * M % 10));       for (int i = 1; i <= N % 10; i++) Ans += digits[i];       Ans += N / 10 * Sum;       cout << Ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   const int NINF = -INF - 1;   const int N = 2e5 + 5;   using namespace std;   char a[N];   int c[N];   int b[26];   int main() {     int n, ans = 0, maxn = -1000000;     scanf( %d , &n);     scanf( %s , a);     for (int i = 0; i < n; i++) {       ans = 1;       for (int j = a[i] -  a  + 1; j < 26; j++) {         ans = max(ans, b[j] + 1);       }       b[a[i] -  a ] = ans;       c[i] = ans;       maxn = max(maxn, c[i]);     }     cout << maxn << endl;     for (int i = 0; i < n; i++) printf( %d  , c[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, finall = 0, t1 = 0;     vector<string> v;     cin >> n;     int cop = n;     while (n--) {       string s;       cin >> s;       v.push_back(s);     }     for (int j = 0; j < 7; j++) {       t1 = 0;       for (int k = 0; k < cop; k++) t1 += v[k][j] -  0 ;       if (t1 > finall) finall = t1;     }     cout << finall;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f, N = 2e5 + 5;   inline int read() {     int sum = 0, f = 1;     char c = getchar();     while (c >  9  || c <  0 ) {       if (c ==  - ) f = -f;       c = getchar();     }     while (c >=  0  && c <=  9 ) sum = sum * 10 + c - 48, c = getchar();     return sum * f;   }   int n, d, x[N], minn = INF;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> d;     for (int i = 1; i <= n; i++) cin >> x[i];     for (int i = 1; i <= 100; i++) {       int j = i + d, cnt = 0;       for (int k = 1; k <= n; k++) {         if (x[k] < i || x[k] > j) cnt++;       }       minn = min(minn, cnt);       ;     }     cout << minn <<   n ;     return 0;   }
#include <bits/stdc++.h>   int main() {     char abc[105];     scanf( %s , abc);     int n = strlen(abc);     int i = 0;     int max = 1, count = 0;     while (i < n) {       if (abc[i] ==  A  || abc[i] ==  E  || abc[i] ==  I  || abc[i] ==  O  ||           abc[i] ==  U  || abc[i] ==  Y ) {         if (max < count + 1) max = count + 1;         count = 0;       } else         count++;       i++;     }     if (max < count + 1) max = count + 1;     printf( %d , max);   }
#include <bits/stdc++.h>   using namespace std;   int find(int a[], int n) {     int max = 1;     int m = a[1];     for (int i = 2; i < n; i++) {       if (m <= a[i]) {         max = i;         m = a[i];       }     }     return max;   }   int main() {     int c = 0;     int n;     cin >> n;     int a[n];     cin >> a[0];     for (int i = 1; i < n; i++) {       int temp;       cin >> a[i];     }     int m = 0;     do {       m = find(a, n);       if (a[m] >= a[0]) {         a[m]--;         a[0]++;         c++;       } else         break;     } while (true);     cout << c <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     while (cin >> n >> m) {       int i;       for (i = 1; i <= n; i++) {         int k = m - 2, t = m;         if (i % 2)           while (t--) cout <<  # ;         else {           cout << (i % 4 ?  .  :  # );           while (k--) cout <<  . ;           cout << (i % 4 ?  #  :  . );         }         cout << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 400005;   vector<int> adj[N];   vector<int> st;   int cTime = 1;   int in[N];   int low[N];   vector<vector<int>> comps;   map<array<int, 2>, int> edgeToI;   array<int, 2> res[N];   int edgeToIndex(int a, int b) { return edgeToI[{min(a, b), max(a, b)}]; }   void addRes(int a, int b) { res[edgeToIndex(a, b)] = {a, b}; }   void dfs(int node, int par) {     in[node] = cTime++;     low[node] = in[node];     st.push_back(node);     for (int next : adj[node]) {       if (next == par) continue;       if (in[next]) {         low[node] = min(low[next], low[node]);       } else {         dfs(next, node);         low[node] = min(low[node], low[next]);       }     }     if (in[node] == low[node]) {       vector<int> cur;       while (true) {         int last = st.back();         st.pop_back();         cur.push_back(last);         if (last == node) break;       }       comps.push_back(cur);     }   }   bool onCircle[N];   bool vis[N];   void getEdges(int node) {     vis[node] = true;     for (int next : adj[node]) {       if (res[edgeToIndex(node, next)][0] != 0) continue;       addRes(next, node);       if (!vis[next]) getEdges(next);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);       edgeToI[{min(a, b), max(a, b)}] = i;     }     dfs(1, -1);     vector<int> best;     for (auto comp : comps) {       if (comp.size() > best.size()) best = comp;     }     for (int i : best) onCircle[i] = true;     vector<array<int, 2>> edges;     cout << best.size() <<   n ;     if (best.size() != 1) {     }     getEdges(best[0]);     for (int i = 0; i < m; i++) {       cout << res[i][0] <<     << res[i][1] <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int hed[600010], nxt[600010], to[600010], cnt;   inline void addedge(int u, int v) {     nxt[++cnt] = hed[u];     hed[u] = cnt;     to[cnt] = v;   }   int siz[300010], root[300010], ans[300010];   int mx[300010];   int id[6000010], son[6000010][2], tot;   int n, q;   int merge(int x, int y) {     if (!x) return y;     if (!y) return x;     if (mx[id[y]] < mx[id[x]]) id[x] = id[y];     son[x][0] = merge(son[x][0], son[y][0]);     son[x][1] = merge(son[x][1], son[y][1]);     return x;   }   void update(int &x, int l, int r, int p, int v, int i) {     if (!x) x = ++tot;     if (v < mx[id[x]]) id[x] = i;     if (l == r) return;     int mid = (l + r) >> 1;     if (p <= mid)       update(son[x][0], l, mid, p, v, i);     else       update(son[x][1], mid + 1, r, p, v, i);   }   int query(int a, int b, int k, int l, int r) {     if (a > b || l > r || !k) return 0;     if (a == l && b == r) return id[k];     int mid = (l + r) >> 1;     if (b <= mid)       return query(a, b, son[k][0], l, mid);     else if (a > mid)       return query(a, b, son[k][1], mid + 1, r);     else {       int L = query(a, mid, son[k][0], l, mid),           R = query(mid + 1, b, son[k][1], mid + 1, r);       if (!L) return R;       if (!R) return L;       if (mx[L] < mx[R])         return L;       else         return R;     }   }   void dfs(int x) {     siz[x] = 1;     for (int i = hed[x]; i; i = nxt[i]) {       int y = to[i];       dfs(y);       siz[x] += siz[y];       root[x] = merge(root[x], root[y]);       mx[x] = max(mx[x], siz[y]);     }     update(root[x], 1, n, siz[x], mx[x], x);     ans[x] = query((siz[x] + 1) / 2, siz[x], root[x], 1, n);   }   int main() {     scanf( %d%d , &n, &q);     mx[0] = 1e9;     for (int i = 2; i <= n; i++) {       int p;       scanf( %d , &p);       addedge(p, i);     }     dfs(1);     while (q--) {       int x;       scanf( %d , &x);       printf( %d n , ans[x]);     }     return 0;   }