malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   int main(void) {     int n, i, j;     scanf( %d , &n);     int a[n][2], b[n + 1];     for (i = 0; i < n + 1; i++) {       b[i] = 0;     }     for (i = 0; i < n; i++) a[i][1] = 0;     for (i = 0; i < n; i++) {       scanf( %d , &a[i][0]);       if (a[i][0] <= n && a[i][0] >= 1) {         if (b[a[i][0]] != 1) {           b[a[i][0]] = 1;           a[i][1] = 1;         }       }     }     int k = 1;     for (i = 0; i < n; i++) {       if (a[i][1] != 1) {         for (j = k; j < n + 1; j++) {           if (b[j] == 0) {             a[i][0] = j;             b[j] = 1;             k = j;             break;           }         }       }     }     for (i = 0; i < n; i++) printf( %d  , a[i][0]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int a1[200005];   long long int a2[29];   vector<long long int> v, v1;   bool p[100000008];   void seive() {     long long int i, j;     for (i = 1; i * i <= 100; i++) {       if (p[i] == false) {         for (j = i * i; j <= 100; j += i) {           p[j] = true;         }       }     }     v.push_back(2);     for (i = 3; i <= 100; i += 2) {       if (p[i] == false) {         v.push_back(i);         cout << i << endl;       }     }   }   int main() {     long long int n, d = 0, k = 0, c = 0, x = 0, i = 0, m, f = 0, j = 0, j1,                      sum = 0, s = 0, mx = 100000000000000000;     cin >> n;     while (n--) {       cin >> m;       k = 1;       sum = 0;       for (i = 3; i <= m; i += 2) {         d = (i * 2) + (i - 2) * 2;         sum += (d * k);         k++;       }       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long read() {     long long r = 0, t = 1, c = getchar();     while (c <  0  || c >  9 ) {       t = c ==  -  ? -1 : 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       r = r * 10 + c - 48;       c = getchar();     }     return r * t;   }   const int N = 6010, mod = 998244353;   long long n, m, ans, r;   vector<pair<long long, long long> > c, a[N], b[N];   set<pair<long long, long long> > s;   void insert(set<pair<long long, long long> > &s, pair<long long, long long> a) {     auto it = s.upper_bound(pair<long long, long long>(a.first, (~0ull) >> 1));     if (it != s.begin()) {       it--;       if (it->first <= a.first && it->second >= a.second) return;     }     it = s.lower_bound(pair<long long, long long>(a.first, 0));     while (it != s.end() && a.first <= it->first && a.second >= it->second)       it = s.erase(it);     s.insert(a);     assert(s.size() <= 200000);   }   int main() {     n = read();     for (int i = (1), end_i = (n); i <= end_i; i++) {       long long l = read(), r = read();       while (r - l >= (l & (-l)) - 1) {         a[i].push_back(pair<long long, long long>(l, l + (l & (-l)) - 1));         l += l & (-l);       }       for (int j = (60), end_j = (0); j >= end_j; j--)         if (r - l + 1 >= (1ll << j)) {           a[i].push_back(pair<long long, long long>(l, l + (1ll << j) - 1));           l += (1ll << j);         }     }     m = read();     for (int i = (1), end_i = (m); i <= end_i; i++) {       long long l = read(), r = read();       while (l + (l & (-l)) - 1 <= r) {         b[i].push_back(pair<long long, long long>(l, l + (l & (-l)) - 1));         l += l & (-l);       }       for (int j = (60), end_j = (0); j >= end_j; j--)         if (r - l + 1 >= (1ll << j)) {           b[i].push_back(pair<long long, long long>(l, l + (1ll << j) - 1));           l += (1ll << j);         }     }     for (int i = (1), end_i = (n); i <= end_i; i++)       for (int j = (1), end_j = (m); j <= end_j; j++) {         s.clear();         for (auto A : a[i])           for (auto B : b[j]) {             long long w = max(A.second - A.first, B.second - B.first);             insert(s, pair<long long, long long>((A.first ^ B.first) & (~w),                                                  (A.first ^ B.first) | w));           }         for (auto S : s) c.push_back(S);       }     sort(c.begin(), c.end(),          [](pair<long long, long long> a, pair<long long, long long> b) {            return a.first == b.first ? a.second > b.second : a.first < b.first;          });     for (auto C : c)       if (C.second > r) {         ans = (ans + ((C.second - C.first + 1) % mod) *                          ((C.first + C.second) % mod) % mod * ((mod + 1) / 2)) %               mod;         r = C.second;       }     printf( %d n , (int)ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   const int mod = 1e9 + 7;   const long long inf = 1e18;   const double PI = acos(-1);   const double eps = 1e-8;   long long qkmod(long long base, long long cnt) {     long long res = 1;     while (cnt) {       if (cnt & 1) res = res * base % mod;       base = base * base % mod;       cnt >>= 1;     }     return res;   }   int a[maxn];   int main() {     int T;     scanf( %d , &T);     int n, p;     while (T--) {       scanf( %d %d , &n, &p);       for (int i = 0; i < n; i++) cin >> a[i];       if (p == 1) {         printf(n & 1 ?  1 n  :  0 n );         continue;       }       sort(a, a + n);       long long res = 0, sum = 0, pre = 1e6 + 7;       bool flag = false;       int i = n - 1, j = n - 1;       while (~i) {         int k = a[i--];         int d = pre - k;         pre = k;         res = res * qkmod(p, d) % mod;         for (int i = 0; i < min(20, d); i++) {           sum *= p;           if (sum > maxn) flag = true;         }         while (~j && a[j] == k) {           j--;           if (flag || sum > 0) {             sum--;             res += mod - 1;           } else {             sum++;             res++;           }         }         res %= mod;       }       printf( %d n , (res * qkmod(p, pre)) % mod);     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   vector<int> edge[1005];   pair<int, int> ans[1005];   set<int> eQQ;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, a, b;     cin >> n;     for (int i = 1; i < n; ++i) {       cin >> a >> b;       if (b != n) return cout <<  NO n , 0;       edge[a].push_back(i);     }     for (int i = n - 1; i > 0; --i)       if (edge[i].empty()) eQQ.insert(i);     for (int i = n - 1, l; i >= 0; --i) {       l = n;       for (auto x : edge[i])         if (x == edge[i].back())           ans[x] = make_pair(l, i);         else {           auto p = eQQ.lower_bound(i);           if (p == eQQ.begin())             return cout <<  NO n , 0;           else             --p, ans[x] = make_pair(l, *p), l = *p, eQQ.erase(p);         }     }     cout <<  YES n ;     for (int i = 1; i < n; ++i)       cout << ans[i].first <<     << ans[i].second <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[100 * 100 + 5];   pair<int, int> as[100 * 10000 + 5];   pair<int, int> ass[100 * 1000 * 7 + 5];   int p = 0;   int badbakhti(int l, int r) {     if (r - l < 2) {       return 1;     }     int h = -10000 * 100000, e = (r + l) / 2;     h = a[e].first;     for (int i = l; i < r; i++) {       if (i != e) {         as[p].first = a[e].first;         as[p].second = a[i].second;         p++;       }     }     badbakhti(l, e);     badbakhti(e, r);     return 1;   }   int main() {     int n;     cin >> n;     int d, q;     for (int i = 0; i < n; i++) {       cin >> d >> q;       a[i] = make_pair(d, q);       as[p] = make_pair(d, q);       p++;     }     sort(a, a + n);     badbakhti(0, n);     sort(as, as + p);     int u = 0, er = 0;     for (int i = 0; i < p; i++) {       u = i;       while (as[i].first == as[u].first && as[i].second == as[u].second) u++;       ass[er].first = as[i].first;       ass[er].second = as[i].second;       er++;       i = u - 1;     }     cout << er << endl;     for (int i = 0; i < er; i++) {       cout << ass[i].first <<     << ass[i].second << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000100;   const int M = 1001000;   const int P = 1000000007;   int fac[M], phi[M];   vector<int> prime;   long long val[M], sum[M], res[M];   long long ans[M], vec[M], rec[M];   void init_() {     fac[1] = 1;     for (int i = 2; i <= N; i++) {       if (!fac[i]) {         fac[i] = i;         prime.push_back(i);       }       for (int j = 0; j <= (int)prime.size() - 1; j++) {         int p = prime[j];         if (p * i > N) break;         fac[p * i] = p;         if (fac[i] == p) break;       }     }     phi[1] = 1;     for (int i = 2; i <= N; i++) {       int p = fac[i];       int j = i / p;       int p_ = fac[j];       if (p == p_)         phi[i] = phi[j] * p;       else         phi[i] = phi[j] * (p - 1);     }     for (int i = 1; i <= N; i++) val[i] = (val[i - 1] + i) % P;     for (int i = 1; i <= N; i++) sum[i] = (sum[i - 1] + (long long)i * i) % P;     for (int i = 1; i <= N; i++)       for (int j = 1; j <= N / i; j++) (res[i * j] += (long long)i * phi[j]) %= P;     for (int i = 1; i <= N; i++)       ans[i] =           (sum[i] + sum[i - 1] + 2 * (2 * P - val[i] - val[i - 1] + res[i])) % P;     for (int i = 1; i <= N; i++) vec[i] = (vec[i - 1] + ans[i] * i) % P;     for (int i = 1; i <= N; i++) rec[i] = (rec[i - 1] + ans[i] * i % P * i) % P;     for (int i = 1; i <= N; i++) ans[i] = (ans[i - 1] + ans[i]) % P;   }   int main() {     init_();     int _;     cin >> _;     while (_--) {       int n, m;       scanf( %d%d , &n, &m);       if (n > m) swap(n, m);       n++, m++;       int o = (ans[n] * n % P * m % P - (n + m) * vec[n] % P + rec[n] + P) % P;       printf( %d n , o);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e6 + 3;   int nxt[mxN], vis[mxN];   void get(string s) {     nxt[0] = -1;     int i = 0, k = -1, len = s.size();     while (i < len) {       if (k == -1 or s[i] == s[k])         nxt[++i] = ++k;       else         k = nxt[k];     }   }   int main() {     int t;     int n, m, i, j, c, a, b, k, cn = 0, sm = 0, mx = INT_MIN, mn = INT_MAX;     string s, ss, sr;     bool f = false, ff = true;     cin >> s;     get(s);     nxt[0] = 0;     n = nxt[s.size()];     for (i = 0; i < s.size(); i++) vis[nxt[i]] = 1;     while (n) {       if (vis[n]) break;       n = nxt[n];     }     if (!n)       return cout <<  Just a legend                    <<   n ,              0;     for (i = 0; i < n; i++) cout << s[i];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[10] = {};   int fac[13] = {1};   inline void p(vector<int> q, vector<int> w, int cnt, long long pr) {     if (q.size() == 0) {       a[cnt] += pr;       return;     }     int qq = q[q.size() - 1], ww = w[w.size() - 1];     q.pop_back();     w.pop_back();     p(q, w, cnt, pr * qq);     p(q, w, cnt + 1, pr * ww);   }   inline int c(int a, int b) { return fac[a] / fac[b] / fac[a - b]; }   inline int cal(int r, int n) {     if (r <= 0) return 0;     if (n == 6) return 1;     long long ret = 0;     for (int i = 0; i < r; ++i) {       if (a[i] <= 0) continue;       --a[i];       ret = (ret + cal(r - i, n + 1) * (a[i] + 1)) % 1000000007;       ++a[i];     }     return ret;   }   int main() {     char m[11];     int cnt = 0, t = 1;     long long ans = 0, tmp = 1;     vector<int> q, w;     for (int i = 1; i < 13; ++i) fac[i] = fac[i - 1] * i;     scanf( %s , m);     for (int i = 0; i < strlen(m); ++i) {       if (m[i] ==  0 ) continue;       q.clear();       w.clear();       t = 1;       if (m[i] <=  4 )         t = m[i] -  0 ;       else if (m[i] <=  7 ) {         q.push_back(m[i] -  0  - 1);         w.push_back(1);       } else {         q.push_back(m[i] -  0  - 2);         w.push_back(2);       }       for (int j = i + 1; j < strlen(m); ++j) {         q.push_back(8);         w.push_back(2);       }       p(q, w, cnt, t);       if (m[i] ==  4  || m[i] ==  7 ) ++cnt;     }     ++a[cnt];     --a[0];     for (int i = 1; i < 10 && a[i] > 0; ++i) {       --a[i];       ans = (ans + (long long)cal(i, 0) * (a[i] + 1)) % 1000000007;       ++a[i];     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     string s;     cin >> s;     long long n = s.length();     map<char, long long> m;     m[ B ] = 1;     m[ u ] = 1;     m[ l ] = 1;     m[ b ] = 1;     m[ a ] = 1;     m[ s ] = 1;     m[ r ] = 1;     for (long long i = 0; i < n; i++) {       if (m[s[i]] > 0) {         m[s[i]]++;       }     }     long long c1 = min(m[ u ] - 1, m[ a ] - 1);     long long c11 = min(m[ B ] - 1, m[ b ] - 1);     long long c22 = min(m[ l ] - 1, m[ r ] - 1);     c22 = min(m[ s ] - 1, c22);     long long c3 = min(c11, c22);     long long num1 = c1 / 2;     long long num2 = c3;     cout << min(num1, num2) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, pair<long long, long long>>> v;   long long a, b, w, cnt;   long long parent[100005], sz[100005];   void init(long long n) {     for (long long i = 1; i <= n; i++) parent[i] = i, sz[i] = 1;   }   long long root(long long x) {     if (parent[x] == x) return x;     return parent[x] = root(parent[x]);   }   long long mst(long long n, long long k) {     long long ans = 0;     for (long long i = 0; i < n; i++) {       long long x = v[i].second.first, y = v[i].second.second, z = v[i].first;       if (x == a && y == b) continue;       long long px = root(x), py = root(y);       if (px != py) {         if (sz[px] < sz[py]) swap(px, py);         ans += z;         sz[px] += sz[py];         parent[py] = px;         cnt++;       }     }     if (cnt != k) return -1;     return ans;   }   int main() {     long long t, n, i, j, m, ans = 0;     cin >> n >> m;     for (i = 1; i <= m; i++) {       long long x, y, z;       cin >> x >> y >> z;       if (x > y) swap(x, y);       v.push_back({z, {x, y}});     }     a = v[0].second.first, b = v[0].second.second, w = v[0].first;     sort(v.begin(), v.end());     init(n);     cnt = 2;     sz[a] = 2;     parent[b] = a;     long long ans1 = w + mst(m, n);     init(n);     cnt = 1;     long long ans2 = mst(m, n);     if (ans2 >= 0)       cout << w - (ans1 - ans2);     else       cout << 1000000000;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000001;   int n;   int l[MAXN];   int r[MAXN];   int num[MAXN];   int izq[MAXN];   int der[MAXN];   long long int ans[MAXN];   long long int bit[2][MAXN];   vector<pair<int, int> > L[MAXN];   vector<pair<int, int> > R[MAXN];   void update(int id, int a, int v) {     while (a <= n) {       bit[id][a] += v;       a += a & -a;     }   }   void update_range(int a, int b, int v) {     a++, b++;     update(0, a, v);     update(0, b + 1, -v);     update(1, a, v * (a - 1));     update(1, b + 1, -v * b);   }   long long int query(int id, int a) {     long long int ans = 0;     while (a > 0) {       ans += bit[id][a];       a -= a & -a;     }     return ans;   }   long long int query(int b) { return query(0, b) * b - query(1, b); }   long long int query_range(int a, int b) {     if (a > b) return 0ll;     a++, b++;     return query(b) - query(a - 1);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int q;     cin >> n >> q;     vector<pair<int, int> > A;     for (int i = 0; i < n; ++i) {       cin >> num[i];       A.emplace_back(num[i], i);     }     sort(A.begin(), A.end());     reverse(A.begin(), A.end());     set<int> s;     s.insert(-1);     s.insert(n);     for (int i = 0; i < n; ++i) {       auto it = s.lower_bound(A[i].second);       der[A[i].second] = *it;       it--;       izq[A[i].second] = *it;       s.insert(A[i].second);     }     for (int i = 0; i < q; ++i) cin >> l[i];     for (int i = 0; i < q; ++i) cin >> r[i];     for (int i = 0; i < q; ++i) {       l[i]--, r[i]--;       L[l[i]].emplace_back(r[i], i);       R[r[i] + 1].emplace_back(l[i], i);     }     for (int i = n - 1; i >= 0; i--) {       update_range(izq[i] + 1, der[i] - 1, 1);       for (int j = 0; j < R[i].size(); ++j)         ans[R[i][j].second] -= query_range(R[i][j].first, i - 1);       for (int j = 0; j < L[i].size(); ++j)         ans[L[i][j].second] += query_range(i, L[i][j].first);     }     for (int i = 0; i < q; ++i) cout << ans[i] <<    ;     cout <<   n ;     return 0;   }
#include<bits/stdc++.h>   #define LL long long   #define SZ(x) (int)x.size()-1   #define F(i,a,b) for (int i=a;i<=b;++i)   #define DF(i,a,b) for (int i=a;i>=b;--i)   #define pb push_back   #define ms(a,b) memset(a,b,sizeof a)   using namespace std;   int read(){       char ch=getchar(); int w=1,c=0;       for (;!isdigit(ch);ch=getchar()) if (ch== - ) w=-1;       for (;isdigit(ch);ch=getchar()) c=(c<<3)+(c<<1)+(ch^48);       return w*c;   }   const int M=2e4+10;   int n,a[M],m;   #define lowbit(x) (x&-x)   #define pii pair<int,int>   #define mp make_pair   pii d[M];   void add(int x,pii y){    for (;x<=n;x+=lowbit(x)) d[x]=max(d[x],y);   }   pii qry(int x){    pii ret=mp(0,0);    for (;x;x-=lowbit(x)) ret=max(ret,d[x]);    return ret;   }   pii f[15][M][33];   int g[15][M][33],b[M],tw[33],ted[33];   pii nw[33];   int main(){    n=read(); m=read();    F(i,1,n) a[i]=read(),b[i]=min(a[i]+i,n);    DF(i,n,1){     F(j,0,30) g[0][i][j]=min(n,b[i]+j);      add(i,mp(b[i],i));     f[0][i][0]=qry(b[i]);     int tmp=f[0][i][0].second;     F(j,1,30){      f[0][i][j]=max(f[0][i][j-1],mp(b[min(b[i]+j,n)],min(b[i]+j,n)));     }    }    F(t,1,14){     F(i,1,n){      F(j,0,30){       F(jj,0,j){        int ps=f[t-1][i][jj].second;        f[t][i][j]=max(f[t][i][j],f[t-1][ps][j-jj]);        g[t][i][j]=max(g[t][i][j],g[t-1][ps][j-jj]);       }      }     }    }    F(T,1,m){     int l=read(),r=read(),k=read();     if (l==r){      cout<<0<<  n ;      continue;     }     F(i,0,k) nw[i]=mp(b[l],l);     int ans=1e9,cur=0;     DF(i,14,0){      F(j,0,k) tw[j]=nw[j].second,ted[j]=0;      F(j,0,k){       F(jj,0,j){        ted[j]=max(ted[j],g[i][tw[jj]][j-jj]);        }       }      int mx=0;      F(j,0,k) mx=max(mx,ted[j]);      if (mx>=r){       ans=min(ans,cur|(1<<i));      }      else{       F(j,0,k){        F(jj,0,j){         nw[j]=max(nw[j],f[i][tw[jj]][j-jj]);         }        }       cur|=(1<<i);      }     }     cout<<ans<<  n ;    }       return 0;   }   /*   100 1   4 6 5 1 6 6 6 5 3 6 4 4 2 1 6 4 2 1 2 1 4 6 6 5 4 5 5 4 4 5 5 4 1 2 3 1 3 5 4 4 5 3 6 5 5 1 4 4 1 5 5 4 5 1 1 5 3 4 4 2 6 6 4 4 5 2 6 6 2 3 1 6 3 4 4 1 2 1 2 2 3 4 1 3 3 4 6 3 2 5 6 6 6 6 4 6 3 2 1 5   19 84 14   */
#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e8 + 5e4 + 1;   const double error = 1e-7;   const double PI = acos(-1);   mt19937 rng(chrono::system_clock::now().time_since_epoch().count());   inline long long int MOD(long long int x, long long int m = mod) {     long long int y = x % m;     return (y >= 0) ? y : y + m;   }   const int inf = 0x3f3f3f3f;   const unsigned long long int infl = 1e18 + 1;   const int nmax = 2e3 + 5;   typedef long long int T;   bool lexcomp(const complex<T> &p, const complex<T> &q) {     return make_pair(p.real(), p.imag()) < make_pair(q.real(), q.imag());   }   complex<T> origin, ray;   inline bool half(complex<T> p) {     return ((conj(ray) * (p)).imag()) < 0 ||            (((conj(ray) * (p)).imag()) == 0 && ((conj(ray) * (p)).real()) > 0);   }   bool polarComp(complex<T> p, complex<T> q) {     p -= origin;     q -= origin;     return make_tuple(half(p), 0) < make_tuple(half(q), ((conj(p) * (q)).imag()));   }   vector<complex<T> > points;   vector<int> ans;   vector<int> input;   char str[nmax];   int main() {     int n;     cin >> n;     points.resize(n + 1);     for (int i = 1; i <= n; i++) {       int x, y;       cin >> x >> y;       points[i] = complex<T>(x, y);       input.push_back(i);     }     cin >> str;     int pid =         min_element(input.begin(), input.end(),                     [&](int i, int j) { return lexcomp(points[i], points[j]); }) -         input.begin();     ans.push_back(input[pid]);     origin = points[input[pid]];     ray = complex<T>(0, -1);     input.erase(input.begin() + pid);     for (int i = 0; i < n - 2; i++) {       if (str[i] ==  L )         pid = min_element(                   input.begin(), input.end(),                   [&](int i, int j) { return polarComp(points[i], points[j]); }) -               input.begin();       else         pid = max_element(                   input.begin(), input.end(),                   [&](int i, int j) { return polarComp(points[i], points[j]); }) -               input.begin();       ray = -points[ans.back()] + points[input[pid]];       ans.push_back(input[pid]);       origin = points[input[pid]];       input.erase(input.begin() + pid);     }     ans.push_back(input[0]);     for (int x : ans) cout << x <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int N;   vector<int> arr;   int main() {     cin >> N;     for (int i = 0; i < N; i++) {       int num;       cin >> num;       arr.push_back(num);     }     vector<int> cur(1000, 0);     int toto = 0;     for (int i = 0; i < N; i++) {       int zoto = toto;       for (int k = 0; k < cur.size(); k++) {         zoto -= cur[k];         int remo = (arr[i] - zoto) / 9 + ((arr[i] - zoto) % 9 ? 1 : 0);         if (arr[i] - zoto < 0) {           continue;         }         if (k < remo - 1 || cur[k] == 9)           continue;         else if (k == remo - 1) {           int montar = (arr[i] - zoto) % 9;           if (montar == 0) montar = 9;           if (zoto + montar + 9 * k > arr[i]) continue;           montar = max(montar, cur[k] + 1);           if (zoto + montar > arr[i]) continue;           toto -= cur[k];           cur[k] = montar;           toto += cur[k];         } else {           if (zoto + cur[k] + 1 > arr[i]) continue;           cur[k]++;           toto++;         }         int remy = arr[i] - (zoto + cur[k]);         for (int hu = 0; hu < k; hu++) {           if (remy >= 9) {             toto -= cur[hu];             cur[hu] = 9;             toto += cur[hu];             remy -= cur[hu];           } else if (remy >= 0) {             toto -= cur[hu];             cur[hu] = remy;             toto += cur[hu];             remy -= cur[hu];           } else             break;         }         break;       }       int maxx = 0;       for (int x = 0; x < cur.size(); x++) {         if (cur[x] != 0) maxx = x;       }       for (int x = maxx; x >= 0; x--) {         cout << cur[x];       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int he[100005], np;   int n;   long long v[100005], f2[100005], fl[100005], ms[100005];   long long h[100005], g[100005];   int read() {     int x = 0;     char ch;     ch = getchar();     while (ch <  0  || ch >  9 ) ch = getchar();     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x;   }   struct edg {     int to, nxt;   } e[200005];   void ad(int x, int y) {     np++;     e[np].to = y;     e[np].nxt = he[x];     he[x] = np;   }   void dfs(int x, int fa) {     f2[x] = fl[x] = ms[x] = g[x] = v[x];     for (int i = he[x]; i; i = e[i].nxt) {       int y = e[i].to;       if (y == fa) continue;       dfs(y, x);       f2[x] = max(f2[x], f2[y]);       f2[x] = max(f2[x], g[x] + ms[y]);       f2[x] = max(f2[x], ms[x] + g[y]);       f2[x] = max(f2[x], fl[x] + fl[y]);       fl[x] = max(fl[x], ms[x] + ms[y]);       fl[x] = max(fl[x], fl[y]);       g[x] = max(g[x], v[x] + g[y]);       g[x] = max(g[x], ms[y] + v[x] + h[x]);       g[x] = max(g[x], ms[x] + fl[y]);       h[x] = max(h[x], fl[y]);       ms[x] = max(ms[x], v[x] + ms[y]);     }   }   int main() {     n = read();     for (int i = 1; i <= n; i++) {       v[i] = read();     }     for (int i = 1; i <= n - 1; i++) {       int x = read(), y = read();       ad(x, y);       ad(y, x);     }     dfs(1, 0);     cout << f2[1];     return 0;   }