malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   long long add(long long a, long long b) {     long long ret = a + b;     if (ret >= 1000000007) ret -= 1000000007;     return ret;   }   long long subtract(long long a, long long b) {     long long ret = a - b;     if (ret < 0) ret += 1000000007;     return ret;   }   long long mult(long long a, long long b) {     long long ret = a * b;     if (ret >= 1000000007) ret %= 1000000007;     return ret;   }   long long bigmod(long long a, long long b) {     long long ret = 1;     while (b) {       if (b & 1) ret = mult(ret, a);       b >>= 1;       a = mult(a, a);     }     return ret;   }   long long inverse(long long n) { return bigmod(n, 1000000007 - 2); }   bool base[1000010];   vector<int> primes;   void sieve(int mx) {     mx += 10;     int x = sqrt(mx);     for (int i = 3; i <= x; i += 2)       if (base[i] == 0)         for (int j = i * i, k = i << 1; j < mx; j += k) base[j] = 1;     primes.push_back(2);     for (int i = 3; i < mx; i += 2)       if (base[i] == 0) primes.push_back(i);   }   bool checkBit(int n, int i) { return (n & (1 << i)); }   int setBit(int n, int i) { return (n | (1 << i)); }   int resetBit(int n, int i) { return (n & (~(1 << i))); }   char a[22], b[22];   int c[22];   struct node {     int end;     node *next[2];     node() {       end = 0;       next[0] = next[1] = NULL;     }   } * root;   void insert(int val) {     node *curr = root;     for (int i = 0; i < 18; ++i) {       int id = c[i];       if (curr->next[id] == NULL) curr->next[id] = new node;       curr = curr->next[id];     }     curr->end += val;   }   int query() {     node *curr = root;     for (int i = 0; i < 18; ++i) {       int id = c[i];       if (curr->next[id] == NULL) return 0;       curr = curr->next[id];     }     return curr->end;   }   int main() {     int q;     scanf( %d , &q);     root = new node;     while (q--) {       scanf( %s , a);       scanf( %s , b);       int n = strlen(b);       for (int i = 0; i < 18 - n; ++i) c[i] = 0;       for (int i = 0; i < n; ++i) c[18 - n + i] = ((b[i] -  0 ) & 1);       if (a[0] ==  + )         insert(1);       else if (a[0] ==  - )         insert(-1);       else {         printf( %d n , query());       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int64_t dp[2004], nx[2004];   struct pt {     int64_t v, c;   };   int main() {     ios::sync_with_stdio(false);     int n, m, q;     cin >> n >> m >> q;     vector<vector<pair<int, int>>> G(n + 1);     vector<int> val(n + 1, 0);     vector<int64_t> cost(n + 1, 0);     for (int i = 0; i < m; ++i) {       int a, b, c;       cin >> a >> b >> c;       G[a].push_back({b, c});       G[b].push_back({a, c});       val[a] = max(val[a], c);       val[b] = max(val[b], c);     }     int64_t ans = 0, md = 1000000007;     for (int j = 2; j <= n; ++j) dp[j] = -1e12;     for (int i = 0; i < m; ++i) {       int64_t tm = 0;       for (int j = 1; j <= n; ++j) {         nx[j] = -1e12;         for (auto to : G[j]) {           nx[j] = max(nx[j], dp[to.first] + to.second);         }         tm = max(tm, nx[j]);       }       ans = (ans + tm) % md;       swap(dp, nx);     }     for (int i = 1; i <= n; ++i) {       cost[i] = val[i] * 1ll * m - dp[i];     }     vector<pt> v;     for (int i = 1; i <= n; ++i) {       v.push_back({val[i], cost[i]});     }     int64_t nowq = m + 1;     int64_t mx = 0;     int mxi = 0;     for (int i = 0; i < v.size(); ++i) {       if (nowq * v[i].v - v[i].c > mx) {         mx = nowq * v[i].v - v[i].c;         mxi = i;       }     }     while (nowq <= q) {       int64_t nxq = q + 1;       for (int i = 0; i < v.size(); ++i) {         if (v[i].v > v[mxi].v) {           nxq = min(nxq, 1 + (v[i].c - v[mxi].c) / (v[i].v - v[mxi].v));         }       }       int64_t q2 = (nxq - nowq) * (nxq - nowq - 1) / 2;       q2 %= md;       ans += (nxq - nowq) * (mx % md) + (v[mxi].v * q2) % md;       ans %= md;       mx = 0;       for (int i = 0; i < v.size(); ++i) {         if (nxq * v[i].v - v[i].c > mx) {           mx = nxq * v[i].v - v[i].c;           mxi = i;         }       }       nowq = nxq;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int A[3] = {0};     int num;     cin >> num;     long ip;     while (num--) {       cin >> ip;       if (ip % 3 == 0)         A[0]++;       else if (ip % 3 == 1)         A[1]++;       else if (ip % 3 == 2)         A[2]++;     }     cout << A[0] / 2 + min(A[1], A[2]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, ret, f[26];   vector<pair<pair<int, int>, int> > ss;   int call(char c) { return int(c) - int( a ); }   int main() {     cin >> n;     string s;     for (int i = 1; i <= n; i++) {       cin >> s;       ss.push_back(make_pair(make_pair(call(s[0]), call(s[long(s.length()) - 1])),                              long(s.length())));     }     ret = 0;     for (int c = 0; c < 26; c++) {       fill(f + 0, f + 26, 0);       for (int i = 0; i < n; i++) {         if ((ss[i].first.first == c) || (f[ss[i].first.first] > 0)) {           f[ss[i].first.second] =               max(f[ss[i].first.second], f[ss[i].first.first] + ss[i].second);         }       }       ret = max(ret, f[c]);     }     printf( %d , ret);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, m, t, cnt, r[105];   int main() {     scanf( %d , &n);     for (int i = (0); i <= (n); i++) {       ++cnt;       if (i < n) scanf( %d , &x);       if (x < 0) ++t;       if (t == 3 || i == n) {         r[m++] = cnt - 1;         cnt = t = 1;       }     }     printf( %d n , m);     for (int i = (0); i < (m); i++) printf( %d  , r[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i = 0;     string a, b;     vector<pair<string, string> > vec;     cin >> n;     while (n--) {       cin >> a >> b;       vec.push_back(make_pair(a, b));     }     n = 0;     sort(vec.begin(), vec.end());     while (i < vec.size()) {       n++;       while (i < vec.size() - 1 && vec[i].first == vec[i + 1].first &&              vec[i].second == vec[i + 1].second)         i++;       i++;     }     cout << n << endl;   }
#include <bits/stdc++.h>   using namespace std;   int m, n;   int main() {     cin >> n >> m;     if (m * n % 2 || ((min(m, n) == 1) && max(m, n) > 2)) {       cout << 1 <<   n ;       cout << n <<     << m <<     << 1 <<     << 1 <<   n ;       for (int i = 1; i <= m; ++i) {         if (i % 2) {           for (int j = 1; j <= n; ++j) cout << j <<     << i <<   n ;         } else {           for (int j = n; j >= 1; --j) cout << j <<     << i <<   n ;         }       }       cout << 1 <<     << 1;     } else {       cout << 0 <<   n ;       if (n % 2 == 0) {         for (int i = 1; i <= m; ++i) cout << 1 <<     << i <<   n ;         for (int i = 2; i <= n; ++i) {           if (i % 2) {             for (int j = 2; j <= m; ++j) cout << i <<     << j <<   n ;           } else {             for (int j = m; j >= 2; --j) cout << i <<     << j <<   n ;           }         }         for (int i = n; i >= 1; --i) cout << i <<     << 1 <<   n ;       } else {         for (int i = 1; i <= n; ++i) cout << i <<     << 1 <<   n ;         for (int i = 2; i <= m; ++i) {           if (i % 2) {             for (int j = 2; j <= n; ++j) cout << j <<     << i <<   n ;           } else {             for (int j = n; j >= 2; --j) cout << j <<     << i <<   n ;           }         }         for (int i = m; i >= 1; --i) cout << 1 <<     << i <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int vowel(char a) {     if (a ==  a  || a ==  i  || a ==  e  || a ==  u  || a ==  o ) {       return 1;     }     return 0;   }   int main() {     char s[1005];     char t[1005];     int i;     scanf( %s , s);     scanf( %s , t);     if (strlen(s) != strlen(t)) {       printf( NO n );       return 0;     }     for (i = 0; i < strlen(s); i++) {       if (vowel(s[i]) != vowel(t[i])) {         printf( NO n );         return 0;       }     }     printf( YES n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int A[50][50];   int main() {     long long int T;     cin >> T;     while (T--) {       int n;       cin >> n;       for (int i = 0; i < n; i++) {         string s;         cin >> s;         for (int j = 0; j < n; j++) {           A[i][j] = s[j] -  0 ;         }       }       bool f = true;       for (int i = 0; i < n - 1; i++) {         for (int j = 0; j < n - 1; j++) {           if (A[i][j] && !A[i + 1][j] && !A[i][j + 1]) {             f = false;             i = n;             break;           }         }       }       cout << ((f) ?  YES  :  NO ) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, p[500500], pp[500500], t = 1;   vector<long long> s, f;   int fd(long long x) {     if (x == pp[x]) {       return x;     }     return pp[x] = fd(pp[x]);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       p[i] = i;       pp[i] = i;     }     for (int i = 1; i <= m; i++) {       long long x;       cin >> x;       if (x == 1) {         long long l, r;         cin >> l >> r;         p[l] = r;         pp[l] = r;       }       if (x == 2) {         long long r;         cin >> r;         s.push_back(r);         f.push_back(fd(r));       }       if (x == 3) {         long long r, y;         cin >> r >> y;         int st = s[y - 1], fn = f[y - 1];         while (st != fn) {           if (st == r) {             break;           }           st = p[st];         }         if (st == r) {           cout <<  YES  << endl;           continue;         }         cout <<  NO  << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long t, n, a[100010], b[100010], c[100], w, e;   long long ans = 0;   void init() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= 90; i++) c[i] = 0;     sort(a + 1, a + n + 1);     w = 1;     for (int i = 1; i <= n; i++) {       while (a[i] > ((1 << w) - 1)) w++;       b[i] = w;       c[w]++;     }     ans = 0;     for (int i = 1; i <= w; i++) ans = ans + (c[i] - 1) * c[i] / 2;     cout << ans << endl;   }   int main() {     cin >> t;     for (int q = 1; q <= t; q++) init();     return 0;   }
#include<bits/stdc++.h>   using namespace std;   const int N=10,M=405,mod=998244353;   int n,m,k,t,v[M],id[M][M],ch[M][30],l[N];   char s[N][N];   struct mat{int a[M][M];};   mat operator*(mat x,mat y)   {    mat z;    for(int i=1;i<=t;i++)    {     for(int j=1;j<=t;j++)     {      z.a[i][j]=0;      for(int k=1;k<=t;k++)       z.a[i][j]=(z.a[i][j]+1ll*x.a[i][k]*y.a[k][j]%mod)%mod;     }    }    return z;   }   mat qp(mat a,int b)   {    mat ans=a;    b--;    while(b)    {     if(b&1)      ans=ans*a;     a=a*a;     b>>=1;    }    return ans;   }   mat tr;   int sol(int x,int y)   {    if(x>y)     swap(x,y);    if(id[x][y])     return id[x][y];    int u=++t;    id[x][y]=u;    for(int i=0;i<26;i++)    {     if(ch[x][i]&&ch[y][i])     {      int nx1=ch[x][i],nx2=ch[y][i];      tr.a[u][sol(nx1,nx2)]++;      if(v[nx1])       tr.a[u][sol(1,nx2)]++;      if(v[nx2])       tr.a[u][sol(nx1,1)]++;      if(v[nx1]&&v[nx2])       tr.a[u][sol(1,1)]++;     }    }    return u;   }   int main()   {    scanf( %d%d ,&n,&m);    k=1;    for(int i=1;i<=n;i++)    {     scanf( %s ,s[i]+1),l[i]=strlen(s[i]+1);     int u=1;     for(int j=1;j<=l[i];j++)     {      int x=s[i][j]- a ;      if(!ch[u][x])       ch[u][x]=++k;      u=ch[u][x];     }     v[u]=1;    }    memset(tr.a,0,sizeof(tr.a));    int b=sol(1,1);    tr=qp(tr,m);    printf( %d n ,tr.a[b][b]);    return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int a[4005];   long long int dp[4005][4005];   int m;   int dfs(int l, int r) {     if (l > r) return 0;     long long int mi = 1e18;     long long int root = -1;     for (int i = l; i <= r; i++) {       if (a[i] < mi) {         mi = a[i];         root = i;       }     }     int root_l = dfs(l, root - 1);     int root_r = dfs(root + 1, r);     int sz = r - l + 1;     for (int i = 0; i <= sz; i++) {       int sz_l = root - l;       int sz_r = r - root;       dp[root][i] = -1e18;       for (int j = max(0, i - sz_r - 1); j <= min(sz_l, i); j++) {         dp[root][i] = max(dp[root][i], dp[root_l][j] + dp[root_r][i - j] -                                            2ll * j * (i - j) * a[root]);         if (i - 1 - j >= 0)           dp[root][i] = max(dp[root][i], dp[root_l][j] + dp[root_r][i - 1 - j] -                                              2ll * (j + 1) * (i - j) * a[root] +                                              a[root] + 1ll * m * a[root]);       }     }     return root;   }   int main(void) {     int n;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);     for (int i = 0; i <= n; i++) {       dp[0][i] = -1e18;     }     dp[0][0] = 0;     int root = dfs(1, n);     printf( %lld n , dp[root][m]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5010;   const int mod = 1e9 + 7;   const int INF = 0x3f3f3f3f;   const double pi = acos(-1.0);   const double eps = 1e-4;   int main() {     double d, l, v1, v2;     cin >> d >> l >> v1 >> v2;     printf( %.6lf n , (l - d) / (v1 + v2));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& p) {     return o <<  (  << p.first <<  ,  << p.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& vc) {     o <<  sz =   << vc.size() << endl <<  [ ;     for (const T& v : vc) o << v <<  , ;     o <<  ] ;     return o;   }   int cnt;   vector<int> ask(int v) {     cout <<  ?   << v << endl;     int K;     cin >> K;     vector<int> ret;     for (int i = 0; i < (int)(K); i++) {       int x;       cin >> x;       ret.push_back(x);     }     cnt++;     assert(cnt <= 16);     return ret;   }   void ans(int v) { cout <<  !   << v << endl; }   bool ToLeaf(int v, int p, vector<int>& vs, vector<vector<int> >& vis) {     while (true) {       vector<int> vi = ask(v);       if (vi.size() == 2) {         ans(v);         return 1;       }       vs.push_back(v);       vis.push_back(vi);       if (vi.size() == 1) break;       int nv = -1;       for (int i = 0; i < (int)(3); i++)         if (vi[i] != p) nv = vi[i];       p = v, v = nv;     }     return 0;   }   bool Leaf2Leaf(vector<int>& vs, vector<vector<int> >& vis) {     int s = 1;     if (ToLeaf(s, -1, vs, vis)) return 1;     if (vs.size() == 1) return 0;     int nxt = -1;     for (int nv : vis[0])       if (nv != vs[1]) nxt = nv;     reverse(vs.begin(), vs.end());     reverse(vis.begin(), vis.end());     if (ToLeaf(nxt, s, vs, vis)) return 1;     return 0;   }   bool dfs(int v, int p, int d = 0) {     vector<int> vi = ask(v);     if (vi.size() == 2) {       ans(v);       return 1;     }     if (d == 2) {       return 0;     }     for (int u : vi)       if (u != p) {         if (cnt < 16) {           if (dfs(u, v, d + 1)) return 1;         } else {           ans(u);           return 1;         }       }     return 0;   }   void solve() {     cnt = 0;     int H;     cin >> H;     vector<int> vs;     vector<vector<int> > vis;     if (Leaf2Leaf(vs, vis)) {       return;     }     int K = vs.size();     while (K <= 5) {       int top = vs[K / 2];       int nxt = -1;       if (vis[K / 2].size() == 1)         nxt = vis[K / 2][0];       else         nxt = vis[K / 2][0] ^ vis[K / 2][1] ^ vis[K / 2][2] ^ vs[K / 2 - 1] ^               vs[K / 2 + 1];       for (int i = 0; i < (int)(K / 2); i++) vs.pop_back(), vis.pop_back();       if (ToLeaf(nxt, top, vs, vis)) {         return;       }       K = vs.size();     }     int top = vs[K / 2];     int nxt = vis[K / 2][0] ^ vis[K / 2][1] ^ vis[K / 2][2] ^ vs[K / 2 - 1] ^               vs[K / 2 + 1];     assert(dfs(nxt, top));   }   int main() {     int T;     cin >> T;     for (int t = 0; t < (int)(T); t++) solve();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long n;     cin >> n;     long long d = 0;     vector<long long> v;     vector<long long> a;     long long val = 0;     map<long long, long long> m;     long long start = 0, end = -1, count = 0;     for (long long i = 0; i < (long long)n; i++) {       cin >> val;       if (i == 0) {         if (val < 0) {           count++;           break;         } else {           v.push_back(val);           m[val]++;         }       } else {         if (val > 0) {           if (m[val] >= m[-val] && m[val] == 0) {             v.push_back(val);             m[val]++;           } else {             count++;             break;           }         } else {           if (v.size() == 0 && start != end) {             d++;             end = i;             a.push_back(end - start + 1);             start = i + 1;             m.clear();           }           if (m[val] >= m[-val]) {             count++;             break;           } else {             m[val]++;             for (long long j = 0; j < v.size(); j++) {               if (v[j] == (-val)) {                 v.erase(v.begin() + j);                 break;               }             }           }           if (v.size() == 0 && start != end) {             d++;             end = i;             a.push_back(end - start + 1);             start = i + 1;             m.clear();           }         }       }     }     if (v.size() != 0 || count > 0) {       cout << -1 <<   n ;     } else {       cout << d <<   n ;       for (long long i = 0; i < a.size(); i++) {         cout << a[i] <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     long long ar[n + 1];     for (long long i = 1; i <= n; i++) {       cin >> ar[i];     }     ar[0] = 0;     long long ans = 0;     for (long long i = 0; i < n; i++) {       long long x = ar[i];       long long y = ar[i + 1];       if (x > y) {         ans += y * (x - y);       }       if (x < y) {         ans += (y - x) * (n - y + 1);       }     }     cout << ans;   }
#include <iostream>   #include <vector>   #include <algorithm>   #include <math.h>      using namespace std;      #define fi first   #define se second   #define all(x) x.begin(), x.end()   #define sz(x) x.size()   #define pb push_back   #define mk make_pair   #define tests int t; cin >> t; while(t--)   #define forn(i, a, b) for (int i = a; i < b; i++)      typedef long long ll;   typedef long double ld;      const int Z = (int)1e5 + 123;   const int inf = (int)2e9 + 123;   const ll llinf = (ll)9e18 + 123;      int main() {       tests {           int n;           cin >> n;           ll a = 0, b = n % 10;           bool flag = false;           while ((b * 2021) + (a * 2020) <= n) {               if ((n - (b * 2021)) % 2020 > 0) {                   b += 10;                   continue;               } else {                   flag = true;                   break;               }           }           cout << (flag ?  YES n  :  NO n );       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     while (y >= 1) {       if (y % 2) res = (res * x) % p;       x = ((x % p) * (x % p)) % p;       y /= 2;     }     return res;   }   void solve() {     long long int n, m;     cin >> n >> m;     cout << power(power(2, m, 1000000007) - 1, n, 1000000007) <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t;     t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d;     cin >> a >> b >> c >> d;     int m = max(3 * a / 10, a - a / 250 * c);     int v = max(3 * b / 10, b - b / 250 * d);     if (m == v)       cout <<  Tie ;     else if (m > v)       cout <<  Misha ;     else       cout <<  Vasya ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e6 + 6;   long long int arr[2000005], brr[2000005], crr[2000005];   void solve(int n, long long int arr[]) {     int cnt = n + arr[0];     for (int i = 1; i < n; i++) {       cnt += abs(arr[i] - arr[i - 1]) + 1;     }     cout << cnt;     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     solve(n, arr);     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int INFTY = 20000000;   const int MAX = 500100;   const int MOD = 10000000;   void coutTab(int* tab, int n) {     for (int i = 0; i < n; i++) {       cout << tab[i] <<    ;     }     cout <<   n ;   }   string create(int a, int b, int x, int start) {     if (start == 0)       a--;     else       b--;     string s;     s.push_back(start +  0 );     char l;     for (int i = 0; i < x - 1; i++) {       l = s[s.size() - 1];       if (l ==  1 ) {         s.push_back( 0 );         a--;       } else {         s.push_back( 1 );         b--;       }     }     l = s[s.size() - 1];     if (l ==  0 ) {       while (a--) s.push_back( 0 );       while (b--) s.push_back( 1 );     } else {       while (b--) s.push_back( 1 );       while (a--) s.push_back( 0 );     }     return s;   }   bool check(string s, int x) {     int j = 0;     for (int i = 1; i < s.size(); i++) {       if (s[i - 1] != s[i]) j++;     }     return x == j;   }   int main() {     ios_base::sync_with_stdio(0);     int a, b, x;     cin >> a >> b >> x;     string res = create(a, b, x, 0);     if (!check(res, x)) res = create(a, b, x, 1);     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   struct NODE {     int to, next;   } node[200000 + 10];   long long dp[100000 + 10], k[100000 + 10];   int n, tol;   int head[100000 + 10];   void addedge(int from, int to) {     node[tol].to = to;     node[tol].next = head[from];     head[from] = tol++;   }   void tree_dp(int now, int fa) {     priority_queue<long long, vector<long long>, less<long long> > que;     while (!que.empty()) que.pop();     for (int i = head[now]; i != -1; i = node[i].next) {       int v = node[i].to;       if (v != fa) {         k[v]--;         tree_dp(v, now);         que.push(dp[v]);       }     }     while (k[now] && !que.empty()) {       k[now]--;       int p = que.top();       dp[now] += que.top() + 2;       que.pop();     }     for (int i = head[now]; i != -1; i = node[i].next) {       if (k[now] == 0) break;       int v = node[i].to;       if (v != fa) {         long long t = min(k[now], k[v]);         dp[now] += t * 2;         k[now] -= t;       }     }   }   int main() {     while (scanf( %d , &n) != EOF) {       memset(head, -1, sizeof(head));       tol = 0;       for (int i = 1; i <= n; i++) scanf( %I64d , &k[i]);       for (int i = 1; i <= n - 1; i++) {         int u, v;         scanf( %d %d , &u, &v);         addedge(u, v);         addedge(v, u);       }       int root;       scanf( %d , &root);       memset(dp, 0, sizeof(dp));       tree_dp(root, 0);       printf( %I64d n , dp[root]);     }     return 0;   }