malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = int(3e5) + 228, mod = int(1e9) + 7;   const long long inf = (long long)(1e16);   int n;   pair<int, int> p[N];   pair<long long, long long> t[2 * N], dp[N];   pair<long long, long long> merg(pair<long long, long long> a,                                   pair<long long, long long> b) {     if (a.first > b.first) return b;     if (a.first < b.first) return a;     a.second += b.second;     if (a.second >= mod) a.second -= mod;     return a;   }   void upd(int v, pair<long long, long long> a) {     v += n - 1;     t[v] = merg(t[v], a);     while (v > 1) {       v >>= 1;       t[v] = merg(t[v + v], t[v + v + 1]);     }   }   pair<long long, long long> get(int l, int r) {     l += n - 1;     r += n - 1;     pair<long long, long long> res = make_pair(inf, 0);     while (l <= r) {       if (l & 1) res = merg(res, t[l]);       if (!(r & 1)) res = merg(res, t[r]);       l = (l + 1) >> 1;       r = (r - 1) >> 1;     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &p[i].first, &p[i].second);     }     for (int i = 1; i <= 2 * n; i++) {       t[i] = make_pair(inf, 0);     }     sort(p + 1, p + n + 1);     for (int i = 1; i <= n; i++) {       int l = 0, r = i;       while (r - l > 1) {         int mid = (l + r) >> 1;         if (p[mid].first <= p[i].second)           l = mid;         else           r = mid;       }       if (l == 0) {         dp[i] = make_pair(p[i].second, 1);       } else {         dp[i] = get(1, l);         dp[i].first += p[i].second;       }       upd(i, make_pair(dp[i].first - p[i].first, dp[i].second));     }     int mx = 0;     pair<long long, long long> ans = make_pair(inf, 0);     for (int i = n; i > 0; i--) {       if (mx < p[i].first) ans = merg(ans, dp[i]);       mx = max(mx, p[i].second);     }     printf( %lld , ans.second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios_base::sync_with_stdio(false);     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       while (a[i] % 2 == 0) a[i] /= 2;       while (a[i] % 3 == 0) a[i] /= 3;     }     int flag = 1;     int val = a[0];     for (int i = 1; i < n; i++) {       if (a[i] != val) flag = 0;     }     if (flag)       cout <<  Yes ;     else       cout <<  No ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mx = INT_MIN, n, a[10];   char x;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= 7; j++) {         cin >> x;         a[j] += x - 48;       }     }     for (int i = 1; i <= 7; i++)       if (mx < a[i]) mx = a[i];     cout << mx;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a, b;     int t = n / 2;     if (n % 2 == 1) {       cout << t <<     << t + 1;     } else if (n % 4 == 2) {       cout << t - 2 <<     << t + 2;     } else if (n % 4 == 0) {       cout << t - 1 <<     << t + 1;     }   }
#include <bits/stdc++.h>   using namespace std;   int as[10001];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; ++i) as[i] = 1;     as[n] = 0;     for (int j = 0; j < k; ++j) {       for (int i = 1; i <= n - (1 << (j + 1)); ++i) {         if (as[i] != n - i) {           cout << n - (1 << j) <<    ;           as[i] += as[n - (1 << j)];         } else {           cout << n <<    ;         }       }       int x = 1;       if (n - (1 << (j + 1)) + 1 > 0) {         for (int i = n - (1 << (j + 1)) + 1; i <= n; ++i) {           if (as[i] != n - i) {             cout << n - (1 << j) + x <<    ;             as[i] += as[n - (1 << j) + x];             ++x;           } else {             cout << n <<    ;           }         }       } else {         for (int i = 1; i <= n; ++i) {           if (as[i] != n - i) {             cout << i + as[i] <<    ;             as[i] += as[i + as[i]];           } else {             cout << n <<    ;           }         }       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     long long c[n], s[n];     for (int i = 0; i < n; i++) {       cin >> c[i] >> s[i];     }     long long dp[m + 1];     dp[m] = 0;     for (int i = m - 1; i >= 0; i--) {       dp[i] = m - i;       for (int j = 0; j < n; j++) {         long long l = c[j] - s[j];         l = max(0LL, l);         long long r = c[j] + s[j];         r = min(r, m);         if (i + 1 >= l && i + 1 <= r) {           dp[i] = min(dp[i], dp[i + 1]);           break;         }         if (i < l) {           long long gap = l - i - 1;           long long rr = r + gap;           rr = min(m, rr);           dp[i] = min(dp[i], gap + dp[rr]);         }       }     }     cout << dp[0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[] = {1, 2, 4, 3};   int b[] = {1, 3, 4, 2};   int c[] = {1, 4, 1, 4};   int d[] = {1, 1, 1, 1};   int main() {     string s;     cin >> s;     int n = s.length();     int rem = 0;     for (int i = 0; i < n; i++) {       rem = (rem * 10 + s[i] -  0 ) % 4;     }     int ans = a[rem] + b[rem] + c[rem] + d[rem];     ans %= 5;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1), eps = 1e-6, INF = 1e16;   bool isEqual(long double x, long double y) { return fabs(x - y) < eps; }   int sgn(long double sc) {     if (isEqual(sc, 0)) return 0;     if (sc < eps) return -1;     return 1;   }   struct pnt {     long double x, y;     pnt() {}     pnt(long double _x, long double _y) {       x = _x;       y = _y;     }     pnt(pnt A, pnt B) {       x = B.x - A.x;       y = B.y - A.y;     }     pnt operator+(pnt p) { return pnt(x + p.x, y + p.y); }     pnt operator-(pnt p) { return pnt(x - p.x, y - p.y); }     void operator+=(pnt p) { x += p.x, y += p.y; }     void operator-=(pnt p) { x -= p.x, y -= p.y; }     pnt operator*(long double sc) { return pnt(x * sc, y * sc); }     pnt operator/(long double sc) { return pnt(x / sc, y / sc); }     long double operator*(pnt p) { return x * p.x + y * p.y; }     long double operator^(pnt p) { return x * p.y - p.x * y; }     bool operator==(pnt p) { return isEqual(x, p.x) && isEqual(y, p.y); }     bool operator!=(pnt p) { return !isEqual(x, p.x) || !isEqual(y, p.y); }     void normalize() {       long double sq = sqrt(x * x + y * y);       x /= sq;       y /= sq;       return;     }     long double dist2(pnt p) {       return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);     }     long double dist(pnt p) { return sqrt(dist2(p)); }     bool isParallel(pnt p) { return isEqual(pnt(x, y) ^ p, 0); }   };   struct line {     long double a, b, c;     line() {}     line(long double _a, long double _b, long double _c) {       a = _a, b = _b, c = _c;     }     line(pnt p1, pnt p2) {       a = p1.y - p2.y;       b = p2.x - p1.x;       c = -(a * p1.x + b * p1.y);     }     pnt getPerpendicularVector() { return pnt(a, b); }     pnt getParallelVector() { return pnt(-b, a); }     line getPerpendicularLine() { return line(-b, a, c); }     bool isParallel(line l) {       return getParallelVector().isParallel(l.getParallelVector());     }     pnt intersect(line l) {       if (isParallel(l)) {         return pnt(INF, INF);       }       long double q = a * l.b - b * l.a;       return pnt((b * l.c - c * l.b) / q, (c * l.a - a * l.c) / q);     }     long double dist(pnt p) {       return fabs(a * p.x + b * p.y + c) / sqrt(a * a + b * b);     }     pnt basePerp(pnt p) {       return intersect(line(p, p + getPerpendicularVector()));     }     bool isOnLine(pnt p) { return isEqual(a * p.x + b * p.y + c, 0); }   };   int main() {     int n, d;     cin >> n >> d;     pnt A(0, d), B(d, 0), C(n, n - d), D(n - d, n);     pnt BA(B, A), BC(B, C), CD(C, D), DA(D, A), AC(A, C), DC(D, C);     line BAl(B, A), BCl(B, C), CDl(C, D), DAl(D, A), ACl(A, C);     int m, x, y;     cin >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y;       pnt kuz(x, y), Bkuz(B, kuz), Dkuz(D, kuz);       if ((((BA ^ Bkuz) < 0 && (BC ^ Bkuz) > 0) &&            (((DA ^ Dkuz) > 0) && ((DC ^ Dkuz) < 0))) ||           BAl.isOnLine(kuz) || BCl.isOnLine(kuz) || CDl.isOnLine(kuz) ||           DAl.isOnLine(kuz)) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, k, day, dep[1000010], arr[1000010], cost[1000010], cel, from,       coss;   long long mini[1000010], wylot[1000010], dolot[1000010], sum, inf = 1e7;   vector<pair<long long, int> > tab;   int ptr;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> k;     inf *= inf;     for (int a = 1; a <= m; a++) {       cin >> day >> dep[a] >> arr[a] >> cost[a];       tab.push_back(make_pair(day, a));     }     sort(tab.begin(), tab.end());     ptr = 0;     for (int a = 1; a <= n; a++) mini[a] = inf;     sum = n * inf;     dolot[0] = n * inf;     for (int a = 1; a <= 1e6; a++) {       while (ptr < tab.size() && tab[ptr].first <= a) {         cel = arr[tab[ptr].second];         from = dep[tab[ptr].second];         coss = cost[tab[ptr].second];         if (cel != 0) {           ptr++;           continue;         }         if (coss < mini[from]) {           sum -= mini[from];           mini[from] = coss;           sum += coss;         }         ptr++;       }       dolot[a] = sum;     }     reverse(tab.begin(), tab.end());     ptr = 0;     for (int a = 1; a <= n; a++) mini[a] = inf;     sum = n * inf;     wylot[(int)1e6 + 1] = n * inf;     for (int a = 1e6; a >= 0; a--) {       while (ptr < tab.size() && tab[ptr].first >= a) {         cel = arr[tab[ptr].second];         from = dep[tab[ptr].second];         coss = cost[tab[ptr].second];         if (cel == 0) {           ptr++;           continue;         }         if (coss < mini[cel]) {           sum -= mini[cel];           mini[cel] = coss;           sum += coss;         }         ptr++;       }       wylot[a] = sum;     }     long long ans = inf;     for (int a = 1; a <= 1e6 - k; a++) {       if (dolot[a - 1] < n * inf && wylot[a + k] < n * inf)         ans = min(ans, dolot[a - 1] + wylot[a + k]);     }     if (ans == inf)       cout << -1;     else       cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[3][11];   int sum[11];   int x, len, i;   int main() {     scanf( %d , &n);     for (i = 0; i < n; i++)       for (int j = 0; j < 6; j++) {         scanf( %d , &x);         a[i][x]++;         sum[x]++;       }     for (i = 1; i < 1000; i++) {       if (i < 10) {         if (sum[i])           continue;         else           break;       } else if (i < 100) {         int flag = 0;         for (int j = 0; j < 3; j++)           for (int k = 0; k < 3; k++) {             if (k == j) continue;             if (a[j][i % 10] && a[k][i / 10]) flag = 1;           }         if (flag == 0) break;       } else {         int flag = 0;         for (int j = 0; j < 3; j++) {           for (int k = 0; k < 3; k++) {             if (k == j) continue;             for (int l = 0; l < 3; l++) {               if (l == k || l == j) continue;               if (a[j][i % 10] && a[k][i / 10 % 10] && a[l][i / 100]) flag = 1;             }           }         }         if (flag == 0) break;       }     }     cout << i - 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, mem[100004][28];   char x, y;   string s;   map<pair<int, int>, int> m;   int dp(int i = 0, int j = 27) {     if (i == (int)s.size()) return 0;     int& ret = mem[i][j];     if (~ret) return ret;     if (m[{(int)(s[i] -  a ), j}] != 1) {       return ret = max(dp(i + 1, (int)(s[i] -  a )) + 1, dp(i + 1, j));     } else       return ret = dp(i + 1, j);   }   int main() {     cin >> s;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x >> y;       m[{(int)(x -  a ), (int)(y -  a )}] = 1;       m[{(int)(y -  a ), (int)(x -  a )}] = 1;     }     memset(mem, -1, sizeof mem);     cout << (int)s.size() - dp();     return 0;   }
#include <bits/stdc++.h>   void answer(unsigned long long v) { std::cout << v <<   n ; }   void solve(unsigned n, unsigned k) {     const auto s = n * (n - 1ull) / 2;     if (k > n / 2) return answer(s);     const auto m = n - 2 * k;     const auto d = m * (m - 1ull) / 2;     answer(s - d);   }   int main() {     unsigned n, k;     std::cin >> n >> k;     solve(n, k);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool dp[1005][2005];   pair<long long int, long long int> prv[1005][2005];   char val[1005][2005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int n, k;     cin >> n >> k;     string s;     cin >> s;     s =  *  + s;     dp[0][k] = 1;     for (long long int i = 1; i < n; i++) {       for (long long int j = 0; j <= 2 * k; j++) {         if (s[i] ==  W ) {           if (j == 1 && i != n) continue;           if (j >= 1 && dp[i - 1][j - 1]) {             dp[i][j] = 1;             prv[i][j] = {i - 1, j - 1};             val[i][j] =  W ;           }         }         if (s[i] ==  L ) {           if (j == 2 * k - 1 && i != n) continue;           if (j <= 2 * k - 1 && dp[i - 1][j + 1]) {             dp[i][j] = 1;             prv[i][j] = {i - 1, j + 1};             val[i][j] =  L ;           }         }         if (s[i] ==  D ) {           if ((j == 0 || j == 2 * k) && i != n) continue;           if (dp[i - 1][j]) {             dp[i][j] = 1;             prv[i][j] = {i - 1, j};             val[i][j] =  D ;           }         }         if (s[i] ==  ? ) {           if (!(j == 1 && i != n) && j >= 1 && dp[i - 1][j - 1]) {             dp[i][j] = 1;             prv[i][j] = {i - 1, j - 1};             val[i][j] =  W ;           }           if (!(j == 2 * k - 1 && i != n) && j <= 2 * k - 1 && dp[i - 1][j + 1]) {             dp[i][j] = 1;             prv[i][j] = {i - 1, j + 1};             val[i][j] =  L ;           }           if (!((j == 0 || j == 2 * k) && i != n) && dp[i - 1][j]) {             dp[i][j] = 1;             prv[i][j] = {i - 1, j};             val[i][j] =  D ;           }         }       }     }     if (s[n] ==  W ) {       if (!dp[n - 1][2 * k - 1]) return cout <<  NO , 0;       dp[n][2 * k] = 1;       prv[n][2 * k] = {n - 1, 2 * k - 1};       val[n][2 * k] =  W ;     }     if (s[n] ==  L ) {       if (!dp[n - 1][1]) return cout <<  NO , 0;       dp[n][0] = 1;       prv[n][0] = {n - 1, 1};       val[n][0] =  L ;     }     if (s[n] ==  D ) {       return cout <<  NO , 0;     }     if (s[n] ==  ? ) {       if (dp[n - 1][2 * k - 1]) {         dp[n][2 * k] = 1;         prv[n][2 * k] = {n - 1, 2 * k - 1};         val[n][2 * k] =  W ;       }       if (dp[n - 1][1]) {         dp[n][0] = 1;         prv[n][0] = {n - 1, 1};         val[n][0] =  L ;       }     }     if (dp[n][0] || dp[n][2 * k]) {       long long int x, y;       if (dp[n][0])         x = n, y = 0;       else         x = n, y = 2 * k;       string ans;       while (x) {         ans.push_back(val[x][y]);         pair<long long int, long long int> xx = prv[x][y];         x = xx.first, y = xx.second;       }       reverse(ans.begin(), ans.end());       cout << ans;     } else       return cout <<  NO , 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long gcd(long long a, long long b) {     if (a == 0) return b;     if (b == 0) return a;     if (a > b) return gcd(b, a % b);     return gcd(a, b % a);   }   int main() {     long long n, x, y;     cin >> n >> x >> y;     long long d = gcd(x, y);     long long xd = x / d;     long long yd = y / d;     long long nok = x / d * y;     for (int i = 0; i < n; i++) {       long long a;       cin >> a;       long long l = 0;       long long r = (a / (x + y) + 2) * nok;       while ((r - l) > 1) {         long long m = (r + l) / 2;         long long hits = m / yd + m / xd;         if (hits >= a)           r = m;         else           l = m;       }       if (((r % yd) == 0) && ((r % xd) == 0))         cout <<  Both n ;       else if (r % yd == 0)         cout <<  Vanya n ;       else         cout <<  Vova n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int ans, tot, head[N], ver[N], Next[N], val[N], n, m, deg[N];   queue<int> q[2];   bool vis[N];   inline void add(int x, int y) {     ver[++tot] = y;     Next[tot] = head[x];     head[x] = tot;     deg[y]++;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) scanf( %d , &val[i]);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       x++;       y++;       add(y, x);     }     for (int i = 1; i <= n; i++)       if (!deg[i]) q[val[i]].push(i);     while ((!q[0].empty()) || (!q[1].empty())) {       while (!q[0].empty()) {         int x = q[0].front();         q[0].pop();         vis[x] = true;         for (int i = head[x]; i; i = Next[i]) {           int y = ver[i];           if (vis[y]) continue;           if (!(--deg[y])) q[val[y]].push(y);         }       }       if (!q[1].empty()) {         ans++;         while (!q[1].empty()) {           int x = q[1].front();           q[1].pop();           vis[x] = true;           for (int i = head[x]; i; i = Next[i]) {             int y = ver[i];             if (vis[y]) continue;             if (!(--deg[y])) q[val[y]].push(y);           }         }       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 7050;   int dp[2][maxn];   vector<int> g[2];   int state[2][maxn];   void dfs(int player, int cur, int n) {     if (state[player][cur] == 1) return;     state[player][cur] = 1;     for (int i = 0; i < g[player].size(); i++) {       int v = (cur + g[player][i]) % n;       if (v == 0) {         dp[player][cur] = 1;         return;       }     }     int cnt = 0;     for (int i = 0; i < g[player].size(); i++) {       int v = (cur + g[player][i]) % n;       dfs(player ^ 1, v, n);       if (dp[player ^ 1][v] == (-1)) {         dp[player][cur] = 1;         return;       }       if (dp[player ^ 1][v] == 1) {         cnt++;       }     }     if (cnt == g[player].size()) dp[player][cur] = (-1);     return;   }   void WORK(int n) {     for (int i = 0; i <= n; i++) dp[0][i] = dp[1][i] = 2;     for (int k = 1; k <= 5; k++) {       memset(state, 0, sizeof(state));       for (int i = n - 1; i >= 0; i--) {         dfs(0, i % n, n);         dfs(1, i % n, n);       }     }   }   int main() {     int n;     while (scanf( %d , &n) != EOF) {       for (int i = 0; i < 2; i++) {         g[i].clear();         int cnt;         scanf( %d , &cnt);         while (cnt--) {           int x;           scanf( %d , &x);           g[i].push_back(x);         }       }       WORK(n);       for (int i = 0; i < 2; i++) {         for (int j = 1; j < n; j++) {           if (dp[i][j] == 1)             printf( Win  );           else if (dp[i][j] == (-1))             printf( Lose  );           else             printf( Loop  );         }         printf(  n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void out(T x) {     cout << x << endl;     exit(0);   }   const int maxn = 1e6 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     int m;     cin >> n >> m;     int need = (n + 4 - 1) / 4;     string a =  5 ;     int tota = 5;     while ((int)a.size() < need) {       a +=  5 ;       tota += 5;     }     string b =  5 ;     int totb = 5;     while ((int)b.size() < need) {       b +=  4 ;       totb += 4;     }     reverse(a.begin(), a.end());     reverse(b.begin(), b.end());     cout << a <<   n ;     cout << b <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, cnt;   map<int, int> g[200005];   set<int> s, ns;   void dfs(int x) {     vector<int> v;     v.clear();     ns.clear();     for (set<int>::iterator it = s.begin(); it != s.end(); it++)       if (!g[x][*it])         v.push_back(*it);       else         ns.insert(*it);     s = ns;     for (int i : v) dfs(i);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1, u, v; i <= m; ++i)       scanf( %d%d , &u, &v), g[u][v] = 1, g[v][u] = 1;     for (int i = 1; i <= n; ++i) s.insert(i);     for (; s.size();) {       ++cnt;       int t = *s.begin();       s.erase(t);       dfs(t);     }     printf( %d n , cnt - 1);   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     int t, a, b, c, d, k;     cin >> t;     while (t--) {       cin >> a >> b >> c >> d >> k;       int x = a / c;       int y = b / d;       if (a % c) x++;       if (b % d) y++;       if (x + y <= k) {         cout << x <<     << k - x << endl;       } else         cout << -1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long n, a[300000], b[300000];   long long s;   map<long, long> x, y;   map<pair<long, long>, long> t;   int main() {     cin >> n;     for (long i = 0; i < n; i++) {       cin >> a[i] >> b[i];       x[a[i]]++;       y[b[i]]++;       pair<long, long> p;       p.first = a[i];       p.second = b[i];       t[p]++;     }     for (long i = 0; i < n; i++) {       pair<long, long> p;       p.first = a[i];       p.second = b[i];       s += (x[a[i]] + y[b[i]] - 1 - t[p]);     }     s /= 2;     cout << s;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     scanf( %lld , &n);     scanf( %lld , &k);     long long a[k + 5], mn = -1, ans1, ans2, flag = 0;     for (long long i = 1; i <= k; i++) {       scanf( %lld , &a[i]);       if (a[i] > n) {         continue;       }       flag = 1;       long long r = n % a[i];       if (mn == -1) {         mn = r;         ans1 = i;         ans2 = n / a[i];         continue;       }       if (mn > r) {         mn = r;         ans1 = i;         ans2 = n / a[i];       }     }     if (flag == 0) {       cout <<  1 0 n ;       return 0;     }     cout << ans1 <<     << ans2 <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   vector<int> adj[N];   int n, st[N], sz[N], flat[N], tim = 1;   int dfs(int u) {     int s = 1;     st[u] = tim;     flat[tim] = u;     for (int v : adj[u]) {       tim++;       s += dfs(v);     }     return sz[u] = s;   }   int main(int argc, char const *argv[]) {     int q;     cin >> n >> q;     for (int i = 2; i <= n; ++i) {       int p;       cin >> p;       adj[p].push_back(i);     }     for (int i = 1; i <= n; ++i) {       sort(adj[i].begin(), adj[i].end());     }     dfs(1);     while (q--) {       int u, k;       cin >> u >> k;       if (k > sz[u])         cout << -1 <<   n ;       else         cout << flat[st[u] + k - 1] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d;     cin >> n >> d;     int in[n];     vector<pair<int, int>> c;     in[0] = in[n - 1] = 0;     for (int i = 1; i < n - 1; i++) cin >> in[i];     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       c.push_back({x, y});     }     long long dis[n][n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         dis[i][j] =             d * (abs(c[i].first - c[j].first) + abs(c[i].second - c[j].second));         if (i != 0 && i != n - 1 && i != j) dis[i][j] -= in[i];       }     }     for (int k = 0; k < n; k++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (dis[i][k] + dis[k][j] < dis[i][j])             dis[i][j] = dis[i][k] + dis[k][j];         }       }     }     cout << dis[0][n - 1] << endl;   }