malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   void fastio() {     cin.tie(nullptr);     cin.sync_with_stdio(false);   }   using LL = long long;   using LD = long double;   const LL MOD = 1e9 + 7;   const LL INF = LLONG_MAX;   const LL N = 2e5 + 1;   int main() {     fastio();     LL n;     cin >> n;     vector<LL> a(n);     for (auto &e : a) {       cin >> e;     }     vector<LL> ans(n, 0);     sort(a.begin(), a.end(), greater<LL>());     LL p = 0;     for (LL i = 0; i < n; i += 2) {       ans[i] = a[p++];     }     for (LL i = 0; i < n; ++i) {       if (ans[i] == 0) {         ans[i] = a[p++];       }     }     cout << (n - 1) / 2 <<   n ;     for (auto &e : ans) {       cout << e <<    ;     }     cout <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int q, n, sum, a;     cin >> q;     while (q--) {       sum = 0;       cin >> n;       for (int i = 0; i < n; i++) {         cin >> a;         sum += a;       }       cout << (sum + n - 1) / n << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5000 + 20;   const int maxn = 1e5 + 20;   const int mod = 998244353;   int cnt, head[maxn], pos[maxn];   long long ksm(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       b >>= 1, a = a * a % mod;     }     return res % mod;   }   void read(int &v) {     int k = 1;     v = 0;     int c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) k = 0;       c = getchar();     }     while (c >=  0  && c <=  9 )       v = (v << 3) + (v << 1) + (c - 48), c = getchar();     if (k == 0) v = -v;   }   int d[maxn], dp[maxn], a[maxn], same[maxn];   int main() {     int n, q;     read(n), read(q);     for (int i = 1; i <= n; i++) {       read(a[i]);       if (a[i] == a[i - 1]) same[i] = same[i - 1] + 1;       if (a[i] >= a[i - 1])         d[i] = 0;       else         d[i] = 1;     }     dp[0] = 0;     for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] + d[i];     while (q--) {       int l, r;       read(l), read(r);       if (dp[r] - dp[l] == 0)         puts( Yes );       else if (dp[r] - dp[l] == r - l)         puts( Yes );       else {         int x = dp[r] - dp[l];         r = r - same[r];         if (dp[r] - dp[r - x] == x)           puts( Yes );         else           puts( No );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int query(int x, int y) {     if (x == -1) return 0;     cout << 1 <<     << x <<     << y << endl;     string ret;     cin >> ret;     return ( TAK  == ret);   }   int get(int l, int r) {     if (l > r) return -1;     while (l < r) {       int m = (l + r) >> 1;       if (query(m, m + 1)) {         r = m;       } else {         l = m + 1;       }     }     return l;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, k;     cin >> n >> k;     int x = get(1, n);     int y = get(1, x - 1);     if (!query(y, x)) y = get(x + 1, n);     cout << 2 <<     << x <<     << y << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, d, done[300005] = {0};   vector<int> graph[300005];   map<pair<int, int>, int> edge;   set<int> p;   void go() {     queue<pair<int, int> > q;     for (auto it : p) q.push({it, 0});     while (!q.empty()) {       int x = q.front().first, P = q.front().second;       q.pop();       if (done[x]) continue;       done[x] = 1;       for (auto it : graph[x]) {         if (done[it] && it != P)           printf( %d  , edge[{min(x, it), max(x, it)}]);         else if (it != P)           q.push({it, x});       }     }   }   int main() {     scanf( %d , &n);     scanf( %d , &k);     scanf( %d , &d);     for (__typeof(k) i = 0 - (0 > k); i != k - (0 > k); i += 1 - 2 * (0 > k)) {       int x;       scanf( %d , &x);       p.insert(x);     }     for (__typeof(n - 1) i = 0 - (0 > n - 1); i != n - 1 - (0 > n - 1);          i += 1 - 2 * (0 > n - 1)) {       int u, v;       scanf( %d , &u);       scanf( %d , &v);       graph[u].push_back(v);       graph[v].push_back(u);       edge[{min(u, v), max(u, v)}] = i + 1;     }     printf( %d n , (int)p.size() - 1);     go();     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 88;   const int M = 100100;   int dp[N][M][2];   pair<int, int> a[N];   int n, m;   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %d%d , &a[i].first, &a[i].second);     sort(a, a + n);     for (int i = 0; i <= n; i++)       for (int j = 0; j <= m; j++)         for (int f = 0; f < 2; f++) dp[i][j][f] = M;     dp[0][0][1] = 0;     for (int i = 0; i < n; i++) {       for (int x = 0; x <= m; x++)         dp[i + 1][x][0] = min(dp[i + 1][x][0], min(dp[i][x][0], dp[i][x][1]));       for (int x = 0; x <= m; x++) {         int r = a[i].first - x - 1;         r = max(r, x - a[i].first);         r = max(r, a[i].second);         int y = min(m, a[i].first + r);         dp[i + 1][y][1] =             min(dp[i + 1][y][1], min(dp[i][x][0], dp[i][x][1]) + r - a[i].second);       }       for (int x = 1; x <= m; x++)         dp[i + 1][x][1] = min(dp[i + 1][x][1], dp[i + 1][x - 1][1] + 1);     }     printf( %d n , min(dp[n][m][0], dp[n][m][1]));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[200010];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     int p = 2e9 + 7, ans = 0;     for (int i = 1; i < n; i++) p = min(p, a[i + 1] - a[i]);     for (int i = 1; i < n; i++) ans += (a[i + 1] - a[i] == p);     return printf( %d %d , p, ans), 0;   }
#include <bits/stdc++.h>   const long long mod = 1000000007;   const long long Inf = 1e9;   using namespace std;   bool valid(string s) {     for (int i = 0; i < s.length(); ++i) {       if (s[i] ==     || s[i] ==  # ) {         if (s[i] ==  # ) return true;       } else         return false;     }     return false;   }   string wtSpace(string s) {     string res =   ;     for (int i = 0; i < s.length(); ++i) {       if (s[i] !=    ) res += s[i];     }     return res;   }   int main() {     int n, a, d;     scanf( %d %d %d , &n, &a, &d);     vector<double> ans(n);     for (int i = 0; i < n; i++) {       int t, v;       scanf( %d %d , &t, &v);       double time = pow(2 * d / (double)a, 0.5);       double time2 = v / (double)a;       if (time <= time2)         ans[i] = t + time;       else {         double x = a * pow(time2, 2) / 2.0;         ans[i] = t + time2 + (d - x) / (double)v;       }     }     for (int i = 0; i < n - 1; i++) {       if (ans[i + 1] < ans[i]) ans[i + 1] = ans[i];       printf( %.6f n , ans[i]);     }     printf( %.6f n , ans[n - 1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (a < b) a = b;   }   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T, class T1>   inline void gn(T &first, T1 &second) {     gn(first);     gn(second);   }   template <class T, class T1, class T2>   inline void gn(T &first, T1 &second, T2 &z) {     gn(first);     gn(second);     gn(z);   }   template <class T>   inline void print(T first) {     if (first < 0) {       putchar( - );       return print(-first);     }     if (first < 10) {       putchar( 0  + first);       return;     }     print(first / 10);     putchar(first % 10 +  0 );   }   template <class T>   inline void printsp(T first) {     print(first);     putchar(   );   }   template <class T>   inline void println(T first) {     print(first);     putchar(  n );   }   template <class T, class U>   inline void print(T first, U second) {     printsp(first);     println(second);   }   template <class T, class U, class V>   inline void print(T first, U second, V z) {     printsp(first);     printsp(second);     println(z);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   const int B = 2222222;   int n, u, cnt[B << 1], flag[B];   long long s[B << 1], first, second;   long long calc(int p) {     long long ans = 0;     int r = max(1, p - (int)(first / second));     for (int t = 0; t + p < B; t += p) {       ans += second * ((long long)(cnt[t + p - 1] - cnt[t + r - 1]) * (t + p) -                        s[t + p - 1] + s[t + r - 1]);       ans += first * (cnt[t + r - 1] - cnt[t]);     }     return ans;   }   void solve_B() {     gn(n, first, second);     for (int i = 0; i < n; i++) {       gn(u);       s[u] += u;       cnt[u]++;     }     for (int i = 1; i < B; i++) s[i] += s[i - 1], cnt[i] += cnt[i - 1];     long long ans = n * first;     for (int i = 2; i < B; i++)       if (!flag[i]) {         smin(ans, calc(i));         if ((long long)i * i >= B) continue;         for (int j = i * i; j < B; j += i) flag[j] = 1;       }     println(ans);     exit(0);   }   int main() {     solve_B();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int ii = 0; ii < t; ii++) {       int n;       cin >> n;       bool A[52];       char Aa[52][52];       for (int i = 0; i < n; i++) {         char t;         cin >> t;         if (t ==  1 )           A[i] = 0;         else           A[i] = 1;       }       int k = 0;       int t = -1;       for (int i = n - 1; i >= 0; i--) {         if (A[i] == 0) {           k++;         } else {           t = i;         }       }       if (n - k > 2 || n - k == 0)         cout <<  YES  << endl;       else {         cout <<  NO  << endl;         continue;       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           Aa[i][j] =  = ;         }       }       for (int i = 0; i < n; i++) Aa[i][i] =  X ;       int c1 = t;       if (n - k > 2) {         for (int i = t + 1; i < n; i++) {           if (A[i] == 1) {             Aa[c1][i] =  + ;             Aa[i][c1] =  - ;             c1 = i;           }         }         Aa[c1][t] =  + ;         Aa[t][c1] =  - ;       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           cout << Aa[i][j];         }         cout << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long T;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> T;     int a, b, c, d;     while (T--) {       cin >> a >> b >> c >> d;       bool ok = 0;       if (a == c) {         if (d + b == a) ok = 1;       }       if (a == d) {         if (c + b == a) ok = 1;       }       if (b == c) {         if (a + d == b) ok = 1;       }       if (b == d) {         if (a + c == b) ok = 1;       }       if (ok)         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> adjc[100005];   vector<int> cadj[100005];   struct edge {     int u, v, c;   };   class dsu {    public:     dsu(vector<int> vert) : vert(vert) {       n = vert.size();       par.resize(n);       iota(par.begin(), par.end(), 0);     }     int root(int u) {       if (par[u] == u) {         return u;       }       return (par[u] = root(par[u]));     }     void merge(int u, int v) {       int ui = lower_bound(vert.begin(), vert.end(), u) - vert.begin();       int vi = lower_bound(vert.begin(), vert.end(), v) - vert.begin();       int ru = root(ui), rv = root(vi);       if (ru != rv) {         if (rand() % 2) {           swap(ru, rv);         }         par[ru] = rv;       }     }     bool ced(int u, int v) {       int ui = lower_bound(vert.begin(), vert.end(), u) - vert.begin();       int vi = lower_bound(vert.begin(), vert.end(), v) - vert.begin();       if ((ui == n || (vert[ui] != u)) || (vi == n || (vert[vi] != v))) return 0;       return (root(ui) == root(vi));     }     int n;     vector<int> vert, par;   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<edge> e;     for (int i = 0; i < m; i++) {       int u, v, c;       cin >> u >> v >> c;       e.push_back({u, v, c});       cadj[c].push_back(u);       cadj[c].push_back(v);       adjc[u].push_back(c);       adjc[v].push_back(c);     }     for (int i = 1; i <= n; i++) {       sort(adjc[i].begin(), adjc[i].end());       adjc[i].resize(unique(adjc[i].begin(), adjc[i].end()) - adjc[i].begin());     }     vector<dsu> cdsu;     for (int i = 1; i <= m; i++) {       sort(cadj[i].begin(), cadj[i].end());       cadj[i].resize(unique(cadj[i].begin(), cadj[i].end()) - cadj[i].begin());       cdsu.emplace_back(cadj[i]);     }     for (edge ed : e) {       cdsu[ed.c - 1].merge(ed.u, ed.v);     }     map<pair<int, int>, int> dp;     int q;     cin >> q;     while (q--) {       int u, v;       cin >> u >> v;       if (adjc[u].size() > adjc[v].size()) {         swap(u, v);       }       if (dp.find({u, v}) != dp.end()) {         cout << dp[{u, v}] << endl;         continue;       }       int ans = 0;       for (auto c : adjc[u]) {         ans += cdsu[c - 1].ced(u, v);       }       cout << ans << endl;       dp[{u, v}] = ans;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 20 + 5;   const long long INF = 3e9;   const char *cc =  0123456789abcdef ;   int c[maxn][maxn], used[maxn];   long long dp[maxn][maxn];   char s[maxn];   long long solve(int t, int m, bool flag) {     memset(dp[0], 0, sizeof(dp[0]));     if (flag) {       for (int i = 0; i < m; ++i) {         for (int j = 0; j + used[0] <= t && j + i + 1 <= m; ++j) {           dp[0][i + j] = min(INF, dp[0][i + j] + c[m - i - 1][j]);         }       }     } else       for (int i = 0; i + used[0] <= t && i <= m; ++i) dp[0][i] = c[m][i];     for (int i = 1; i < 16; ++i) {       memset(dp[i], 0, sizeof(dp[i]));       for (int j = 0; j <= m; ++j) {         if (!dp[i - 1][j]) continue;         for (int k = 0; k + used[i] <= t && k + j <= m; ++k) {           dp[i][j + k] = min(INF, dp[i][j + k] + dp[i - 1][j] * c[m - j][k]);         }       }     }     return dp[15][m];   }   int main() {     for (int i = 0; i < maxn; ++i) {       c[i][0] = c[i][i] = 1;       for (int j = 1; j < i; ++j) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];     }     int k, t;     cin >> k >> t;     bool flag = true;     for (int i = 0; i < 20; ++i) {       long long sum = 0;       for (int j = 0; j < 16; ++j) {         if (used[j] >= t) continue;         if (!flag || j != 0) ++used[j];         long long tmp = solve(t, 20 - i - 1, flag && j == 0);         sum += tmp;         if (sum >= k) {           s[i] = cc[j];           k -= sum - tmp;           if (j > 0) flag = false;           break;         }         if (!flag || j != 0) --used[j];       }     }     s[20] = 0;     int p = 0;     while (s[p] ==  0 ) ++p;     cout << s + p << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > inc[100010];   priority_queue<pair<double, int> > Q;   long long A[100010];   int N, M, K;   vector<pair<int, int> > mul;   int sel[100010];   int typ[100010];   void compute() {     int k;     while (M-- && !(Q.empty() && mul.empty())) {       k = Q.empty() ? -1 : Q.top().second;       if (mul.empty() || (k >= 0 && Q.top().first >= mul.back().first)) {         A[k] += inc[k].back().first;         sel[inc[k].back().second] = 1;         inc[k].pop_back();         Q.pop();         if (!inc[k].empty())           Q.push(make_pair((A[k] + inc[k].back().first) / (1.0 * A[k]), k));         continue;       }       sel[mul.back().second] = 1;       mul.pop_back();     }   }   pair<int, int> typ1[100010];   int main() {     int i, b, t;     cin >> K >> N >> M;     for (i = 0; i < K; ++i) cin >> A[i];     for (int idx = 0; idx < N; ++idx) {       cin >> t >> i >> b;       typ[idx] = t;       if (t == 3) {         mul.push_back(make_pair(b, idx));         continue;       }       --i;       if (t == 1) {         typ1[i] = max(typ1[i], make_pair(b - (int)A[i], idx));         continue;       }       inc[i].push_back(make_pair(b, idx));     }     for (i = 0; i < K; ++i) {       if (!typ1[i].first) continue;       inc[i].push_back(typ1[i]);     }     sort(mul.begin(), mul.end());     for (i = 0; i < K; ++i) {       if (inc[i].empty()) continue;       sort(inc[i].begin(), inc[i].end());       Q.push(make_pair((A[i] + inc[i].back().first) / (1.0 * A[i]), i));     }     compute();     cout << accumulate(sel, sel + N, 0) <<   n ;     for (i = 0; i < N; ++i) {       if (!sel[i] || typ[i] > 1) continue;       cout << i + 1 <<    ;     }     for (i = 0; i < N; ++i) {       if (!sel[i] || typ[i] != 2) continue;       cout << i + 1 <<    ;     }     for (i = 0; i < N; ++i) {       if (!sel[i] || typ[i] < 3) continue;       cout << i + 1 <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char a[102];   char b[102];   char c[102];   int main() {     gets(a);     gets(b);     int l = strlen(a);     int i;     for (i = l - 1; i >= 0; i--) {       if (a[i] ==  z ) {         a[i] =  a ;       } else {         a[i] = a[i] + 1;         break;       }     }     if (!strcmp(a, b))       cout <<  No such string ;     else       cout << a;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   int a[N];   int main() {     int n;     while (scanf( %d , &n) != EOF) {       memset(a, 0, sizeof(a));       for (int i = 0; i < n; i++) {         scanf( %d , &a[i]);       }       sort(a, a + n);       int mid = (n - 1) / 2;       int big = n - 1;       int small = mid;       int ok = 0;       while (big > mid && small >= 0) {         if (a[small] * 2 <= a[big]) {           ok++;           small--;           big--;         } else {           small--;         }       }       printf( %d n , n - ok);     }   }
#include <bits/stdc++.h>   using namespace std;   int n, sol;   multiset<int> S;   multiset<int>::iterator p;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int val;       scanf( %d , &val);       S.insert(val);     }     long long s = 0;     for (int i = 1; i <= n; i++) {       p = S.lower_bound(s);       if (p == S.end()) break;       if (*p < s) p++;       sol++;       s += *p;       S.erase(p);     }     printf( %d n , sol);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long bz = -10000000000;   long long t[50001], ll[50001], rr[50001], dd[50001];   long long tu[50001];   int main() {     long long n, m;     cin >> n >> m;     for (long long a = 1; a <= n; a++) tu[a] = bz;     for (long long a = 1; a <= m; a++) cin >> t[a] >> ll[a] >> rr[a] >> dd[a];     for (long long a = 1; a <= m; a++) {       if (t[a] == 1) {         for (long long b = ll[a]; b <= rr[a]; b++) {           if (tu[b] == bz)             continue;           else             tu[b] += dd[a];         }       } else {         long long jc = 1;         for (long long b = ll[a]; b <= rr[a]; b++) {           if (tu[b] == bz) {             tu[b] = dd[a];             jc = 0;           } else {             if (tu[b] >= dd[a]) {               tu[b] = dd[a];               jc = 0;             }           }         }         if (jc) {           cout <<  NO ;           return 0;         }       }     }     for (long long a = 1; a <= n; a++)       if (tu[a] == bz) tu[a] = 0;     for (long long a = m; a >= 1; a--) {       if (t[a] == 2) {         long long jc = 1;         for (long long b = ll[a]; b <= rr[a]; b++) {           if (tu[b] == dd[a]) jc = 0;         }         if (jc) {           cout <<  NO ;           return 0;         }         continue;       }       for (long long b = ll[a]; b <= rr[a]; b++)         if (tu[a] != bz) tu[b] -= dd[a];     }     cout <<  YES  << endl;     long long jc = 0;     for (long long a = 1; a <= n; a++) {       if (tu[a] == bz) tu[a] = 0;       if (!jc) {         cout << tu[a];         jc = 1;       } else {         cout <<     << tu[a];       }     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   const int N = 4040;   using namespace std;   int n, m;   int l[N], s[N];   int cost[N];   int inf = -1e9;   int d[N][N];   int f[N][N];   vector<int> v[N];   vector<int> pos[N];   vector<int> fut[N][14];   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> l[i];     for (int i = 1; i <= n; i++) cin >> s[i];     reverse(l + 1, l + n + 1);     reverse(s + 1, s + n + 1);     for (int i = 1; i <= n + m; i++) cin >> cost[i];     for (int i = 1; i <= n; i++) v[l[i]].push_back(i);     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++) d[i][j] = inf;     for (int i = 1; i <= n + m; i++) {       for (int j = 0; j < 13; j++) {         fut[i][j].resize((1 << 13 - j));         while (fut[i][j].size() >= N - 4) fut[i][j].pop_back();         for (auto &x : fut[i][j]) x = inf;       }     }     for (int i = 0; i < N; i++) {       for (int j = 1; j <= n + m; j++) {         f[i][j] = cost[j];         for (int h = 0; h < 13; h++) {           if (i & (1 << h))             f[i][j] += cost[j + h + 1];           else             break;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= l[i]; j++) {         int dif = min(12, l[i] - j);         for (int p = 0; p < fut[j][dif].size(); p++) {           d[p + 1][i] = max(d[p + 1][i], fut[j][dif][p] + f[p][l[i]] - s[i]);         }       }       d[1][i] = max(d[1][i], cost[l[i]] - s[i]);       for (int j = 0; j < N; j++) {         int nj = j, x = l[i];         for (int h = 0; h < 13; h++) {           fut[x][h][nj] = max(fut[x][h][nj], d[j][i]);           nj >>= 1;         }       }     }     int res = 0;     for (int i = 0; i < N; i++) {       for (int j = 1; j <= n; j++) {         res = max(res, d[i][j]);       }     }     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long int XpowerY(long long int x, long long int y, long long int m) {     long long int ans = 1;     x = x % m;     while (y > 0) {       if (y % 2 == 1) ans = (ans * x) % m;       x = ((x % m) * (x % m)) % m;       y = y >> 1;     }     return ans % m;   }   vector<long long int> gr[100023];   long long int s[100023], val[100023];   long long int fl, minP;   long long int dfs(long long int node, long long int prev, long long int lev) {     long long int mx = 1e12;     if (lev % 2 == 0) {       for (int i = 0; i < gr[node].size(); i++) {         if (s[gr[node][i]] < prev) fl = 1;         mx = min(mx, s[gr[node][i]] - prev);       };       val[node] = mx;       if (gr[node].size() == 0) val[node] = 0;     }     for (int i = 0; i < gr[node].size(); i++) {       val[gr[node][i]] = s[gr[node][i]] - mx - prev;       long long int temp;       if (lev % 2 == 0)         temp = prev + val[node];       else         temp = s[node];       dfs(gr[node][i], temp, lev + 1);     }     return 0;   }   int main() {     long long int n, x;     cin >> n;     for (long long int i = 0; i < n - 1; i++) {       cin >> x;       gr[x].push_back(i + 2);     }     for (long long int i = 1; i <= n; i++) cin >> s[i];     dfs(1, -1, 1);     val[1] = s[1];     if (fl) return cout << -1 << endl, 0;     for (long long int i = 0; i < n; i++) {       ;       minP += val[i + 1];     }     cout << minP << endl;     return 0;   }