Language: C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
#define N 1000005
#define ll __int64
int w[N],in[N],vis[N];
ll mx[N],sum[N];
vector<int>gra[N];
vector<ll>g[N];
queue<int>q;
int main()
{
//    freopen("c.in","r",stdin);
//    freopen("c.out","w",stdout);
     int n,i,a,t,v;
     while(scanf("%d",&n)==1){
         for(i=0;i<=n;i++)gra[i].clear(),in[i]=0,vis[i]=0,mx[i]=0,g[i].clear();
         for(i=0;i<n;i++){
             scanf("%d%d",&w[i],&a);sum[i]=w[i];
             if(a==-1)continue;
             gra[i].push_back(a);in[a]++;
         }

         ll ans=0,tot;
         for(i=0;i<n;i++)
         if(in[i]==0)q.push(i);
         else if(in[i]>1)vis[i]=1;
         while(!q.empty()){
             t=q.front();q.pop();
             for(i=0;i<gra[t].size();i++){
                v=gra[t][i];in[v]--;
                if(!vis[v])sum[v]+=sum[t];
                else g[v].push_back(sum[t]);
                if(in[v]==0){
                    if(vis[v]){
//                        sort(g[v],g[v]+g[v].size());tot=0;
                        sort(g[v].begin(), g[v].end()); tot=0;
                        for(int j=0;j<g[v].size();j++)
                        tot+=(g[v][j]*(g[v].size()-j-1));
                        ans+=tot,sum[v]+=tot;
                        //printf("ans=%I64d,tot=%I64d\n",ans,tot);
                    }
                    q.push(v);
                }
             }
         }
         cout<<ans<<endl;
         //printf("%I64d\n",ans);
     }
     return 0;
}
/*

3
3 -1
4 0
5 0

3
10 -1
10 0
10 1

4
1 -1
1 0
1 1
1 0

8
1 -1
1 0
2 0
3 0
1 6
1 6
6 -1
7 6
*/

Compiler output:
a.cc:17:12: error: '__int64' does not name a type; did you mean '__int64_t'?
   17 | #define ll __int64
      |            ^~~~~~~
a.cc:19:1: note: in expansion of macro 'll'
   19 | ll mx[N],sum[N];
      | ^~
a.cc:17:12: error: '__int64' was not declared in this scope; did you mean '__ynf64'?
   17 | #define ll __int64
      |            ^~~~~~~
a.cc:21:8: note: in expansion of macro 'll'
   21 | vector<ll>g[N];
      |        ^~
a.cc:21:10: error: template argument 1 is invalid
   21 | vector<ll>g[N];
      |          ^
a.cc:21:10: error: template argument 2 is invalid
a.cc: In function 'int main()':
a.cc:29:59: error: 'mx' was not declared in this scope
   29 |          for(i=0;i<=n;i++)gra[i].clear(),in[i]=0,vis[i]=0,mx[i]=0,g[i].clear();
      |                                                           ^~
a.cc:29:72: error: request for member 'clear' in 'g[i]', which is of non-class type 'int'
   29 |          for(i=0;i<=n;i++)gra[i].clear(),in[i]=0,vis[i]=0,mx[i]=0,g[i].clear();
      |                                                                        ^~~~~
a.cc:31:37: error: 'sum' was not declared in this scope
   31 |              scanf("%d%d",&w[i],&a);sum[i]=w[i];
      |                                     ^~~
a.cc:17:12: error: '__int64' was not declared in this scope; did you mean '__ynf64'?
   17 | #define ll __int64
      |            ^~~~~~~
a.cc:36:10: note: in expansion of macro 'll'
   36 |          ll ans=0,tot;
      |          ^~
a.cc:44:28: error: 'sum' was not declared in this scope
   44 |                 if(!vis[v])sum[v]+=sum[t];
      |                            ^~~
a.cc:45:27: error: request for member 'push_back' in 'g[v]', which is of non-class type 'int'
   45 |                 else g[v].push_back(sum[t]);
      |                           ^~~~~~~~~
a.cc:45:37: error: 'sum' was not declared in this scope
   45 |                 else g[v].push_back(sum[t]);
      |                                     ^~~
a.cc:49:35: error: request for member 'begin' in 'g[v]', which is of non-class type 'int'
   49 |                         sort(g[v].begin(), g[v].end()); tot=0;
      |                                   ^~~~~
a.cc:49:49: error: request for member 'end' in 'g[v]', which is of non-class type 'int'
   49 |                         sort(g[v].begin(), g[v].end()); tot=0;
      |                                                 ^~~
a.cc:49:57: error: 'tot' was not declared in this scope
   49 |                         sort(g[v].begin(), g[v].end()); tot=0;
      |                                                         ^~~
a.cc:50:44: error: request for member 'size' in 'g[v]', which is of non-class type 'int'
   50 |                         for(int j=0;j<g[v].size();j++)
      |                                            ^~~~
a.cc:51:35: error: invalid types 'int[int]' for array subscript
   51 |                         tot+=(g[v][j]*(g[v].size()-j-1));
      |                                   ^
a.cc:51:45: error: request for member 'size' in 'g[v]', which is of non-class type 'int'
   51 |                         tot+=(g[v][j]*(g[v].size()-j-1));
      |                                             ^~~~
a.cc:52:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
   52 |                         ans+=tot,sum[v]+=tot;
      |                         ^~~
      |                         abs
a.cc:52:34: error: 'sum' was not declared in this scope
   52 |                         ans+=tot,sum[v]+=tot;
      |                                  ^~~
a.cc:59:16: error: 'ans' was not declared in this scope; did you mean 'abs'?
   59 |          cout<<ans<<endl;
      |                ^~~
      |                abs