Language: C++

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;

#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1200500
#define mod 1001
#define eps 1e-9

int Num;

const ll inf=999999999;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<"0"||ch>"9"){if(ch=="-")f=-1;ch=getchar();}
    while(ch>="0"&&ch<="9"){x=x*10+ch-"0";ch=getchar();}
    return x*f;
}
//*************************************************************************************

int n;
int fa[maxn],ind[maxn];
ll sum[maxn];
int lst[maxn],top=0;
int pre[maxn],nxt[maxn],to[maxn],cnt;

void makeedge(int x,int y)
{
    to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
}

int main()
{
    memset(pre,-1,sizeof(pre));
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        fa[i]=y;
        if(y>=0)
        {
            ind[y]++;
            makeedge(y,i);
        }
        sum[i]=x;
    }
    queue<int> q;
    while(!q.empty()) q.pop();
    for(int i=0;i<n;i++)
        if(!ind[i]) q.push(i);
    ll ans=0,tmp=0;
    while(!q.empty())
    {
        int s=q.front();
        q.pop();
        top=0;
        for(int p=pre[s];p!=-1;p=nxt[p])
        {
            lst[++top]=sum[to[p]];
        }
        sort(lst+1,lst+1+top);
        tmp=0;
        for(int i=1;i<top;i++)
        {
            tmp+=lst[i];
            ans+=tmp;
        }
        if(fa[s]>=0)
        {
            --ind[fa[s]];
            sum[fa[s]]+=sum[s];
            if(!ind[fa[s]])
                q.push(fa[s]);
        }
    }
    cout<<ans<<endl;
}

Compiler output:
a.cc: In function 'll read()':
a.cc:30:13: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   30 |     while(ch<"0"||ch>"9"){if(ch=="-")f=-1;ch=getchar();}
      |           ~~^~~~
a.cc:30:21: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   30 |     while(ch<"0"||ch>"9"){if(ch=="-")f=-1;ch=getchar();}
      |                   ~~^~~~
a.cc:30:32: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   30 |     while(ch<"0"||ch>"9"){if(ch=="-")f=-1;ch=getchar();}
      |                              ~~^~~~~
a.cc:31:13: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   31 |     while(ch>="0"&&ch<="9"){x=x*10+ch-"0";ch=getchar();}
      |           ~~^~~~~
a.cc:31:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   31 |     while(ch>="0"&&ch<="9"){x=x*10+ch-"0";ch=getchar();}
      |                    ~~^~~~~
a.cc:31:38: error: invalid operands of types 'll' {aka 'long long int'} and 'const char [2]' to binary 'operator-'
   31 |     while(ch>="0"&&ch<="9"){x=x*10+ch-"0";ch=getchar();}
      |                               ~~~~~~~^~~~
      |                                   |   |
      |                                   |   const char [2]
      |                                   ll {aka long long int}