malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 5;   struct edg {     int u, v, l, r;     edg(int u = 0, int v = 0, int l = 0, int r = 0) : u(u), v(v), l(l), r(r) {}     bool operator<(const edg &n1) const { return l > n1.l; }   };   priority_queue<edg> q;   vector<edg> adj[maxn][2];   int n, m;   int dp[maxn][2];   inline void add(int u, int v, int l, int r) {     int op = l % 2;     if (dp[u][op] >= l) {       if (v == n) {         cout << l + 1 << endl;         exit(0);       }       if (r + 1 > dp[v][op ^ 1]) {         dp[v][op ^ 1] = r + 1;         for (int i = 0; i < adj[v][op ^ 1].size(); i++) {           edg t = adj[v][op ^ 1][i];           if (l + 1 <= t.r) q.push(edg(v, t.v, l + 1, t.r));         }         adj[v][op ^ 1].clear();       }     } else {       adj[u][op].push_back(edg(u, v, l, r));     }   }   int main() {     cin >> n >> m;     memset(dp, 0xf3, sizeof(dp));     dp[1][0] = 0;     for (int i = 1; i <= m; i++) {       int u, v, l, r;       scanf( %d%d%d%d , &u, &v, &l, &r);       if ((r - l + 1) % 2 == 0) {         q.push(edg(u, v, l, r - 1));         q.push(edg(v, u, l, r - 1));         if (l + 1 <= r - 2) {           q.push(edg(u, v, l + 1, r - 2));           q.push(edg(v, u, l + 1, r - 2));         }       } else {         if (l <= r - 2) {           q.push(edg(u, v, l, r - 2));           q.push(edg(v, u, l, r - 2));         }         q.push(edg(u, v, l + 1, r - 1));         q.push(edg(v, u, l + 1, r - 1));       }     }     if (n == 1) {       cout <<  0  << endl;       return 0;     }     while (!q.empty()) {       edg t = q.top();       q.pop();       add(t.u, t.v, t.l, t.r);     }     cout <<  -1  << endl;     return 0;   }
#include<iostream>   #include <bits/stdc++.h>   using namespace std;   #define ll long long   #define mod 1000000007   #define INF 100000007   #define MAX 1000000   #define nl   n    #define F first   #define S second   #define pb(a) push_back(a)   typedef vector<ll> vi;   typedef priority_queue<ll , vector<ll> , greater<ll>()> pqmx;   typedef priority_queue<ll> pqmn;   typedef pair<ll , ll> pi;   typedef vector< pair<int , int> > vpi;   #define fr(i,n) for(i=0;i<n;i++)   #define rep(i,a,n) for(i=a;i<n;i++)   int n , t , m , temp , temp2, root , p ,q , k , i , j;      struct comp {        bool operator()( pair<int, int> const& a, pair<int, int> const& b) {            return a.first < b.first || (a.first == b.first && a.second > b.second);       }    };       void func(){       ll A , B , C;       cin>>A>>B>>C;       if((A + B + C)%9 != 0){           cout<< NO <<  n ;           return;       }       ll p = (A + B + C) / 9;       if(p > A || p > B || p > C){           cout<< NO <<  n ;           return;       }       cout<< YES <<  n ;   }            int main(){   #ifndef ONLINE_JUDGE       freopen( input.txt  ,  r  , stdin);       freopen( output.txt  ,  w  , stdout);   #endif       cin>>t;       while(t--){           func();       }   }
#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   template <class T, class R>   ostream& operator<<(ostream& os, pair<T, R>& v) {     os <<  (  << v.first <<     << v.second <<  )  <<    ;     return os;   }   template <class T>   ostream& operator<<(ostream& os, vector<T>& v) {     for (auto i : v) os << i <<    ;     return os;   }   template <class T>   ostream& operator<<(ostream& os, set<T>& v) {     for (auto i : v) os << i <<    ;     return os;   }   unordered_map<string, string*> some;   struct Node {     Node* parent;     vector<Node*> children;     int wordsCount;     string* word;     Node(Node* parent = nullptr) : parent(parent), wordsCount(0), word(nullptr) {       children.resize(26, nullptr);     }     Node* getChild(char c) {       if (children[c -  a ] == nullptr) {         children[c -  a ] = new Node(this);       }       return children[c -  a ];     }   };   struct Bor {     unordered_set<string> words;     Node* root;     Bor() : root(new Node()) {}     int addWord(string& s) {       bool was = (words.find(s) != words.end());       Node* now = root;       int ans = 0;       int step = 1;       int stepForStep = 1e9;       int len = 0;       bool gg = false;       for (char& x : s) {         len++;         now = now->getChild(x);         stepForStep--;         if (stepForStep == 0) step = 1;         ans += step;         if (!gg && now->wordsCount == 1) {           if (was && len != s.size()) {             ans++;             step = 0;             stepForStep = 1e9;           } else if (now->word != nullptr && now->word->size() != len &&                      *now->word == s.substr(0, now->word->size())) {             ans++;             step = 0;             stepForStep = now->word->size() - len + 1;           }           gg = true;         }       }       if (step != 0) {         if (now->word == nullptr) now->word = new string(s);         auto kek = now->word;         while (now != root) {           now->wordsCount += 1;           if (now->wordsCount == 1) now->word = kek;           now = now->parent;         }         if (now->wordsCount == 1) now->word = kek;         now->wordsCount += 1;       }       words.insert(s);       return ans;     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     string s;     int ans = 0;     int count = 0;     Bor bor;     int gl = 0;     int vsego = 0;     while (getline(cin, s)) {       vsego += s.size() + 1;       ans += 1;       string now;       for (char x : s) {         if (x >=  a  && x <=  z  || x >=  A  && x <=  Z ) {           now += x;         } else {           ans++;           if (now !=   ) {             int cnt = bor.addWord(now);             ans += cnt;             gl += cnt;             now =   ;           }         }       }       if (now !=   ) {         int cnt = bor.addWord(now);         ans += cnt;         gl += cnt;       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1000;   int n, m;   int cnt[MaxN + 5][MaxN + 5];   bool Check(int x, int y) {     if (x < 0) return false;     if (y < 0) return false;     if (x > n) return false;     if (y > n) return false;     return true;   }   int main() {     while (~scanf( %d%d , &n, &m)) {       memset(cnt, 0, sizeof(cnt));       for (int i = 1; i <= m; i++) {         int x, y;         scanf( %d%d , &x, &y);         for (int nx = x - 2; nx <= x; nx++)           for (int ny = y - 2; ny <= y; ny++)             if (Check(nx, ny)) {               cnt[nx][ny]++;               if (cnt[nx][ny] == 9) {                 printf( %d n , i);                 return 0;               }             }       }       printf( -1 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int www = 9;   const int Size = 10000;   char buffer[Size];   int dr[] = {-1, 0, 1, 0};   int dc[] = {0, 1, 0, -1};   const int inf = 0x0fffffff;   const int size = 1000;   int d[size];   int solution(int nTest) {     int k, a, b;     scanf( %d%d%d , &k, &a, &b);     scanf( %s , buffer);     string s = buffer;     int n = s.size();     if (n / k >= a && (n + k - 1) / k <= b) {       vector<string> res;       int t = n / k;       int tt = (n + k - 1) / k;       int r = k - (n % k);       for (int i = 0; i < k - (n % k); i++) {         res.push_back(s.substr(i * t, t));       }       for (int i = 0; i < n % k; i++) {         res.push_back(s.substr(t * r + i * tt, tt));       }       for (int i = 0; i < res.size(); i++) {         puts(res[i].c_str());       }       return 0;     }     puts( No solution );     return 0;   }   int main() {     int i = 0, n = 9999;     while (i < n && solution(i)) {       i++;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int radix_len = 1e7 + 5;   long long cnt[radix_len];   int main() {     int n, k, a;     ios_base::sync_with_stdio(false);     cin >> n >> k;     long long tot = 0;     for (int i = (0), i_end_ = (n); i < i_end_; ++i) {       cin >> a;       cnt[a]++;       tot += a;     }     if (tot < k) {       cout << -1 << endl;       return 0;     }     auto bound = 1;     auto sum = 0LL;     for (int i = radix_len - 1; i > 0; i--) {       sum += cnt[i];       if (sum >= k) {         bound = i;         break;       }     }     for (int i = radix_len - 1; i > 1; i--) {       if (i / 2 < bound) break;       cnt[i / 2] += cnt[i];       cnt[i - i / 2] += cnt[i];       sum += cnt[i];       cnt[i] = 0;       while (sum - cnt[bound] >= k) {         sum -= cnt[bound];         bound++;       }     }     cout << bound << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     cin >> n >> k;     if (k < n / 2) {       cout << -1;       return 0;     }     if (n == 1) {       if (k)         cout << -1;       else         cout << 1;       return 0;     }     int x = k - (n - 2) / 2;     cout << x <<     << 2 * x <<    ;     for (int i = 2, j = 2 * x + 1; i < n; i++, j++) cout << j <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, u, v, i, j, c[505], ca = 0, cb = 0, cc = 0, cnt, nt;   int main() {     cin >> n >> m;     vector<int> g[505];     for (i = 0; i < 505; i++) c[i] = 0;     for (i = 0; i < m; i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);       if (g[u].size() == n - 1) {         c[u] = 2;         cb++;       }       if (g[v].size() == n - 1) {         c[v] = 2;         cb++;       }     }     for (i = 1; i <= n; i++) {       if (c[i] == 0) break;     }     if (i != n + 1) {       c[i] = 1;       ca++;       for (j = 0; j < g[i].size(); j++) {         v = g[i][j];         if (c[v] == 0) {           c[v] = 1;           ca++;         }       }     }     while (i <= n && c[i] != 0) i++;     if (i != n + 1) {       c[i] = 3;       cc++;       for (j = 0; j < g[i].size(); j++) {         v = g[i][j];         if (c[v] == 2) continue;         if (c[v] == 1) {           cout <<  NO  << endl;           return 0;         } else {           c[v] = 3;           cc++;         }       }     }     while (i <= n) {       if (c[i] == 0) {         cout <<  NO  << endl;         return 0;       }       i++;     }     for (i = 1; i <= n; i++) {       u = i;       if (c[i] == 1) {         cnt = ca - 1 + cb;         nt = 3;       } else if (c[i] == 2)         cnt = n - 1;       else {         cnt = cc - 1 + cb;         nt = 1;       }       if (g[u].size() == n - 1) continue;       if (g[u].size() != cnt) {         cout <<  NO  << endl;         return 0;       }       for (j = 0; j < g[u].size(); j++) {         if (c[g[u][j]] == nt) {           cout <<  NO  << endl;           return 0;         }       }     }     cout <<  YES  << endl;     for (i = 1; i <= n; i++) {       if (c[i] == 1)         cout <<  a ;       else if (c[i] == 2)         cout <<  b ;       else         cout <<  c ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> Map;   string s[181], sn;   int n, i;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> sn;     cin >> n;     s[1] =  January ;     s[2] =  February ;     s[3] =  March ;     s[4] =  April ;     s[5] =  May ;     s[6] =  June ;     s[7] =  July ;     s[8] =  August ;     s[9] =  September ;     s[10] =  October ;     s[11] =  November ;     s[12] =  December ;     for (i = 13; i <= 24; i++) s[i] = s[i - 12];     for (i = 1; i <= 12; i++) Map[s[i]] = i;     cout << s[Map[sn] + n % 12] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, h;   struct Node {     int m, v;     int id;     bool operator<(const Node &x) const {       if (m != x.m) return m < x.m;       return v < x.v;     }   } q[100005];   bool ok(double times) {     int i, j;     for (j = 1, i = 0; i < n; i++) {       if (q[i].v * times - (double)j * h >= -1e-9) j++;       if (j > k) return true;     }     return false;   }   int main() {     int i, j;     cin >> n >> k >> h;     for (i = 0; i < n; i++) cin >> q[i].m;     for (i = 0; i < n; i++) {       cin >> q[i].v;       q[i].id = i + 1;     }     sort(q, q + n);     double l, r, mid;     l = 0, r = 1e20;     for (int i = 0; i < 200; i++) {       mid = (l + r) * 0.5;       if (!ok(mid))         l = mid;       else         r = mid;     }     j = 1;     for (i = 0; i < n; i++) {       if (q[i].v * r - (double)j * h >= -1e-9) {         cout << q[i].id <<    ;         j++;       }       if (j > k) break;     }   }
#include <bits/stdc++.h>   int n, a[1000][1000];   int main() {     for (int i = 1; i <= 100; i++)       for (int j = 1; j <= 100; j++) a[i][j] = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int t, v;       scanf( %d , &t);       for (int j = 1; j <= t; j++) {         scanf( %d , &v);         a[i][v] = 1;       }     }     bool f1;     for (int i = 1; i <= n; i++) {       f1 = true;       bool f;       for (int j = 1; j <= n; j++)         if (i != j) {           f = false;           for (int k = 1; k <= 100; k++)             if (a[j][k] == 1 && a[i][k] == 0) {               f = true;               break;             }           if (f == false) {             f1 = false;             break;           }         }       if (f1 == true)         printf( YES );       else         printf( NO );       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N[10];   string shirt[5] = { S ,  M ,  L ,  XL ,  XXL };   map<string, int> mp;   int main() {     int K, i, d;     for ((i) = 0; (i) < (int)(5); (i)++) cin >> N[i];     cin >> K;     for ((i) = 0; (i) < (int)(5); (i)++) mp[shirt[i]] = i;     for ((i) = 0; (i) < (int)(K); (i)++) {       string s;       cin >> s;       int x = mp[s];       int ans = -1;       for (d = 0;; d++) {         if (x + d >= 0 && x + d < 5 && N[x + d] > 0) {           ans = x + d;           break;         }         if (x - d >= 0 && x - d < 5 && N[x - d] > 0) {           ans = x - d;           break;         }       }       N[ans]--;       cout << shirt[ans] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T Abs(T a) {     return a > 0 ? a : -a;   }   template <typename T>   inline T sqr(T a) {     return a * a;   }   template <typename T>   inline void relaxMin(T &a, T b) {     if (b < a) a = b;   }   template <typename T>   inline void relaxMax(T &a, T b) {     if (b > a) a = b;   }   const int INF = (int)1E9;   const long double EPS = 1E-6;   const long double PI = 3.1415926535897932384626433832795;   int dp[2000][2000];   int mod = 1000000007;   void solve() {     int n;     cin >> n;     int k;     int x = 0, y = 0;     for (int i(0), _bb(n); i < _bb; ++i) {       cin >> k;       if (k == 1)         x++;       else         y++;     }     dp[0][0] = 1;     dp[1][0] = dp[1][1] = 1;     for (int i(2), _bb(1001); i < _bb; ++i) {       dp[i][0] = (dp[i - 1][0] + (i - 1) * 1LL * dp[i - 2][0]) % mod;       for (int j(1), _bb(1001); j < _bb; ++j)         dp[i][j] = (dp[i - 1][j - 1] * 1LL * i) % mod;     }     cout << dp[n][y] << endl;   }   int main() {     srand(0xA1B2C3D4);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   void update(int *c, int x, int v) {     while (x <= m) {       c[x] += v;       x += x & -x;     }   }   long long sum(int *c, int x) {     long long ret = 0;     while (x > 0) {       ret += c[x];       x -= x & -x;     }     return ret;   }   long long calc(int *c, int L, int R) { return sum(c, R) - sum(c, L - 1); }   int c[3060 + 3060][3060], L[3060][3060], R[3060][3060], Ld[3060][3060];   char s[3060][3060];   vector<pair<int, int> > g[3060];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         if (s[i][j] ==  . )           L[i][j] = 0;         else           L[i][j] = L[i][j - 1] + 1;       }     for (int i = n; i > 0; --i)       for (int j = m; j > 0; --j) {         if (s[i][j] ==  . )           R[i][j] = 0;         else           R[i][j] = R[i][j + 1] + 1;         if (s[i][j] ==  . )           Ld[i][j] = 0;         else           Ld[i][j] = Ld[i + 1][j - 1] + 1;       }     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) g[j + R[i][j] - 1].push_back(make_pair(i, j));     long long ans = 0;     for (int j = m; j > 0; --j) {       for (int i = 0; i < g[j].size(); ++i) {         int x = g[j][i].first;         int y = g[j][i].second;         update(c[x + y], y, 1);       }       for (int i = 1; i <= n; ++i) {         int mi = min(L[i][j], Ld[i][j]);         if (!mi) continue;         ans += calc(c[i + j], j - mi + 1, j);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     int A[t];     for (int i = 0; i < t; i++) {       scanf( %d , &A[i]);     }     int count = 1, max = 1;     for (int i = 0; i < t - 1; i++) {       if (A[i] <= A[i + 1]) {         count++;         if (max <= count) max = count;       } else {         if (max < count) {           max = count;         }         count = 1;       }     }     printf( %d , max);   }
#include <bits/stdc++.h>   using namespace std;   long long pow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) {         res = (res * a) % 1000000007;       }       b >>= 1;       a = (a * a) % 1000000007;     }     return res;   }   int main() {     long long n, k;     cin >> n >> k;     long long ans = (pow(k, k - 1) * pow(n - k, n - k)) % 1000000007;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n + 1];     int b[n + 1];     for (int i = 1; i <= n; i++) {       cin >> a[i];       b[a[i]] = i;     }     for (int i = 1; i <= n; i++) {       cout << b[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void Yahia74() {     ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);   }   const int N = 3e5 + 74, OO = 0x3f3f3f3f, MOD = (int)1e9 + 7;   int main() {     Yahia74();     long long n, d;     cin >> n >> d;     long long ans = n, c = 9, t = 10;     while (c <= n) {       long long st = 0, ed = 1e18 / t;       while (st < ed) {         long long m = st + (ed - st + 1) / 2;         if (m * t + c <= n)           st = m;         else           ed = m - 1;       }       if (st * t + c <= n && st * t + c >= n - d) ans = st * t + c;       t *= 10;       c = c * 10 + 9;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool isopen(char ch) {     if (ch ==  <  || ch ==  {  || ch ==  (  || ch ==  [ ) return true;     return false;   }   int main() {     int k, sum = 0, ans = 0;     string s;     cin >> k >> s;     int c[10] = {0};     for (long long int i = 0; i < s.size(); i++) {       sum = sum + s[i] -  0 ;       int d = s[i] -  0 ;       c[d]++;     }     for (int i = 0; i < 9; i++)       for (int j = 0; j < c[i]; j++) {         if (k > sum) {           sum = sum + 9 - i;           ans++;         }       }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   void scan(int &x) {     bool neg = false;     register int c;     x = 0;     c = getchar();     if (c ==  - ) {       neg = true;       c = getchar();     }     for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48;     if (neg) x *= -1;   }   int main() {     string s;     cin >> s;     int k;     cin >> k;     int n = s.size();     if (k > n)       cout <<  impossible ;     else {       bool ch[26] = {false};       int cnt = 0;       for (int i = 0; i < n; i++) {         if (!ch[s[i] -  a ]) {           cnt++;           ch[s[i] -  a ] = true;         }       }       if (k - cnt >= 0)         cout << k - cnt;       else         cout <<  0 ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dy[] = {-1, 1, 0, 0};   int dx[] = {0, 0, -1, 1};   int n, m;   char mp[1005][1005];   int k;   pair<int, int> p[30];   int row[1005][1005];   int col[1005][1005];   pair<int, int> d[100005];   int return_dir(char dir) {     if (dir ==  N )       return 0;     else if (dir ==  S )       return 1;     else if (dir ==  W )       return 2;     else       return 3;   }   int main() {     for (int i = 0; i < 30; i++) p[i].first = -1;     char ch;     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         ch = getchar();         while (ch == 32 || ch ==   n ) ch = getchar();         mp[i][j] = ch;         if (mp[i][j] >=  A  && mp[i][j] <=  Z )           p[mp[i][j] -  A ] = make_pair(i, j);       }     }     int c = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (mp[i][j] ==  # )           c++;         else           row[i][j] = c;       }     }     c = 0;     for (int j = 0; j < m; j++) {       for (int i = 0; i < n; i++) {         if (mp[i][j] ==  # )           c++;         else           col[i][j] = c;       }     }     scanf( %d , &k);     for (int i = 0; i < k; i++) {       string s;       cin >> s;       int a;       cin >> a;       d[i].first = return_dir(s[0]);       d[i].second = a;     }     bool was = false;     for (int i = 0; i < 30; i++) {       if (p[i].first == -1) continue;       bool ok = true;       int si = p[i].first;       int sj = p[i].second;       for (int j = 0; j < k; j++) {         int ni = si + dy[d[j].first] * d[j].second;         int nj = sj + dx[d[j].first] * d[j].second;         if (ni >= n || nj >= m || ni < 0 || nj < 0) {           ok = false;           break;         }         if (d[j].first < 2) {           if (col[ni][nj] != col[si][sj] || mp[ni][nj] ==  # ) ok = false;         } else {           if (row[ni][nj] != row[si][sj] || mp[ni][nj] ==  # ) ok = false;         }         si = ni;         sj = nj;       }       if (ok) {         was = true;         putchar( A  + i);       }     }     if (!was) cout <<  no solution ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, b;     cin >> n >> b;     long long a[n + 1];     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     a[n] = b;     sort(a, a + n + 1);     if (a[0] == b) {       cout << a[n - 1] - a[0];     } else {       if (a[n] == b) {         cout << a[n] - a[1];       } else {         long long s1, s2, s3, s4, ans;         s1 = (b - a[0]) + (a[n - 1] - a[0]);         ans = s1;         s2 = (a[n] - b) + (a[n] - a[1]);         ans = min(ans, s2);         s3 = (b - a[1]) + (a[n] - a[1]);         ans = min(ans, s3);         s4 = (a[n - 1] - b) + (a[n - 1] - a[0]);         ans = min(ans, s4);         cout << ans;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   long long int n, a[100005], dis[100005] = {0}, vis[100050] = {0}, cnt = 0;   vector<pair<long long int, long long int> > v[100005];   void dfs(long long int s, long long int f) {     vis[s] = 1;     long long int i;     for (i = 0; i < v[s].size(); i++) {       long long int x = v[s][i].first, y = v[s][i].second;       if (!vis[x]) {         if (f == 0) {           dis[x] = max(y + dis[s], y);           if (dis[x] > a[x]) {             cnt++;             dfs(x, 1);           } else             dfs(x, 0);         } else {           cnt++;           dfs(x, f);         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     memset(dis, 0, sizeof(dis[0]));     memset(vis, 0, sizeof(vis[0]));     long long int i, j, x, y;     cin >> n;     for (i = 1; i <= n; i++) cin >> a[i];     for (i = 2; i <= n; i++) {       cin >> x >> y;       v[i].push_back({x, y});       v[x].push_back({i, y});     }     for (i = 1; i <= n; i++) {       if (!vis[i]) dfs(i, 0);     }     cout << cnt;     return 0;   }