Language: C++

stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

typedef vector<int> Edges;
typedef vector<Edges> Graph;

int n;
ll len[200000];
bool root[200000];
Graph g;
vector<int> order;

int main() {
  while (scanf("%d", &n) > 0) {
    MEMSET(root, false);
    g = Graph(n);
    REP(i, n) {
      int p;
      scanf("%lld %d", &len[i], &p);
      if (p != -1) {
        g[p].push_back(i);
      } else {
        root[i] = true;
      }
    }
    REP(i, n) {
      if (!root[i]) { continue; }
      queue<int> que;
      que.push(i);
      while (!que.empty()) {
        int from = que.front();
        que.pop();
        order.push_back(from);
        FORIT(it, g[from]) {
          int to = *it;
          que.push(to);
        }
      }
    }
    reverse(order.begin(), order.end());
    ll ans = 0;
    REP(i, n) {
      int from = order[i];
      vector<int> lens;
      FORIT(it, g[from]) {
        lens.push_back(len[*it]);
      }
      sort(lens.begin(), lens.end());
      ll sum = 0;
      FORIT(it, lens) {
        ans += sum;
        sum += *it;
      }
      len[from] += sum;
    }
    printf("%lld\n", ans);
  }
}

Compiler output:
a.cc:1:1: error: 'stdio' does not name a type; did you mean 'stdin'?
    1 | stdio.h>
      | ^~~~~
      | stdin
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/bits/stl_algo.h:71,
                 from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/stdlib.h:98:8: error: 'size_t' does not name a type
   98 | extern size_t __ctype_get_mb_cur_max (void) __THROW __wur;
      |        ^~~~~~
/usr/include/stdlib.h:57:1: note: 'size_t' is defined in header '<cstddef>'; this is probably fixable by adding '#include <cstddef>'
   56 | #include <bits/floatn.h>
  +++ |+#include <cstddef>
   57 | 
/usr/include/stdlib.h:278:36: error: 'size_t' has not been declared
  278 | extern int strfromd (char *__dest, size_t __size, const char *__format,
      |                                    ^~~~~~
/usr/include/stdlib.h:282:36: error: 'size_t' has not been declared
  282 | extern int strfromf (char *__dest, size_t __size, const char *__format,
      |                                    ^~~~~~
/usr/include/stdlib.h:286:36: error: 'size_t' has not been declared
  286 | extern int strfroml (char *__dest, size_t __size, const char *__format,
      |                                    ^~~~~~
/usr/include/stdlib.h:298:38: error: 'size_t' has not been declared
  298 | extern int strfromf32 (char *__dest, size_t __size, const char * __format,
      |                                      ^~~~~~
/usr/include/stdlib.h:304:38: error: 'size_t' has not been declared
  304 | extern int strfromf64 (char *__dest, size_t __size, const char * __format,
      |                                      ^~~~~~
/usr/include/stdlib.h:310:39: error: 'size_t' has not been declared
  310 | extern int strfromf128 (char *__dest, size_t __size, const char * __format,
      |                                       ^~~~~~
/usr/include/stdlib.h:316:39: error: 'size_t' has not been declared
  316 | extern int strfromf32x (char *__dest, size_t __size, const char * __format,
      |                                       ^~~~~~
/usr/include/stdlib.h:322:39: error: 'size_t' has not been declared
  322 | extern int strfromf64x (char *__dest, size_t __size, const char * __format,
      |                                       ^~~~~~
/usr/include/stdlib.h:342:27: error: 'locale_t' has not been declared
  342 |                           locale_t __loc) __THROW __nonnull ((1, 4));
      |                           ^~~~~~~~
/usr/include/stdlib.h:346:49: error: 'locale_t' has not been declared
  346 |                                     int __base, locale_t __loc)
      |                                                 ^~~~~~~~
/usr/include/stdlib.h:352:33: error: 'locale_t' has not been declared
  352 |                                 locale_t __loc)
      |                                 ^~~~~~~~
/usr/include/stdlib.h:358:55: error: 'locale_t' has not been declared
  358 |                                           int __base, locale_t __loc)
      |                                                       ^~~~~~~~
In file included from /usr/include/features.h:523,
                 from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
                 from /usr/include/string.h:26,
                 from a.cc:2:
/usr/include/stdlib.h:365:17: error: 'locale_t' has not been declared
  365 | extern long int __REDIRECT_NTH (strtol_l, (const char *__restrict __nptr,
      |                 ^~~~~~~~~~~~~~
/usr/include/stdlib.h:370:26: error: 'locale_t' has not been declared
  370 | extern unsigned long int __REDIRECT_NTH (strtoul_l,
      |                          ^~~~~~~~~~~~~~
/usr/include/stdlib.h:377:22: error: 'locale_t' has not been declared
  377 | extern long long int __REDIRECT_NTH (strtoll_l, (const char *__restrict __nptr,
      |                      ^~~~~~~~~~~~~~
/usr/include/stdlib.h:384:31: error: 'locale_t' has not been declared
  384 | extern unsigned long long int __REDIRECT_NTH (strtoull_l,
      |                               ^~~~~~~~~~~~~~
/usr/include/stdlib.h:416:53: error: 'locale_t' has not been declared
  416 |                         char **__restrict __endptr, locale_t __loc)
      |                                                     ^~~~~~~~
/usr/include/stdlib.h:420:52: error: 'locale_t' has not been declared
  420 |                        char **__restrict __endptr, locale_t __loc)
      |                                                    ^~~~~~~~
/usr/include/stdlib.h:425:31: error: 'locale_t' has not been declared
  425 |                               locale_t __loc)
      |                               ^~~~~~~~
/usr/include/stdlib.h:438:29: error: 'locale_t' has not been declared
  438 |                             locale_t __loc)
      |                             ^~~~~~~~
/usr/include/stdlib.h:445:29: error: 'locale_t' has not been declared
  445 |                             locale_t __loc)
      |                             ^~~~~~~~
/usr/include/stdlib.h:452:31: error: 'locale_t' has not been declared
  452 |                               locale_t __loc)
      |                               ^~~~~~~~
/usr/include/stdlib.h:459:31: error: 'locale_t' has not been declared
  459 |                               locale_t __loc)
      |                               ^~~~~~~~
/usr/include/stdlib.h:466:31: error: 'locale_t' has not been declared
  466 |                               locale_t __loc)
      |                               ^~~~~~~~
/usr/include/stdlib.h:531:25: error: 'size_t' has not been declared
  531 |                         size_t __statelen) __THROW __nonnull ((2));
      |                         ^~~~~~
/usr/include/stdlib.h:561:25: error: 'size_t' has not been declared
  561 |                         size_t __statelen,
      |                         ^~~~~~
/usr/include/stdlib.h:661:42: error: 'size_t' has not been declared
  661 | extern void arc4random_buf (void *__buf, size_t __size)
      |                                          ^~~~~~
/usr/include/stdlib.h:672:22: error: 'size_t' was not declared in this scope; did you mean 'std::size_t'?
  672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
      |                      ^~~~~~
      |                      std::size_t
In file included from /usr/include/c++/14/bits/stl_algobase.h:59,
                 from /usr/include/c++/14/algorithm:60:
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:310:33: note: 'std::size_t' declared here
  310 |   typedef __SIZE_TYPE__         size_t;
      |                                 ^~~~~~
/usr/include/stdlib.h:675:22: error: 'size_t' was not declared in this scope; did you mean 'std::size_t'?
  675 | extern void *calloc (size_t __nmemb, size_t __size)
      |                      ^~~~~~
      |                      std::size_t
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:310:33: note: 'std::size_t' declared here
  310 |   typedef __SIZE_TYPE__         size_t;
      |                                 ^~~~~~
/usr/include/stdlib.h:675:38: error: 'size_t' was not declared in this scope; did you mean 'std::size_t'?
  675 | extern void *calloc (size_t __nmemb, size_t __size)
      |                                      ^~~~~~
      |                                      std::size_t
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:310:33: note: 'std::size_t' declared here
  310 |   typedef __SIZE_TYPE__         size_t;
      |                                 ^~~~~~
/usr/include/stdlib.h:675:51: error: expression list treated as compound expression in initializer [-fpermissive]
  675 | extern void *calloc (size_t __nmemb, size_t __size)
      |                                                   ^
/usr/include/stdlib.h:683:36: error: 'size_t' has not been declared
  683 | extern void *realloc (void *__ptr, size_t __size)
      |                                    ^~~~~~
/usr/include/stdlib.h:695:41: error: 'size_t' has not been declared
  695 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                         ^~~~~~
/usr/include/stdlib.h:695:57: error: 'size_t' has not been declared
  695 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                                         ^~~~~~
/usr/include/stdlib.h:701:41: error: 'size_t' has not been declared
  701 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                         ^~~~~~
/usr/include/stdlib.h:701:57: error: 'size_t' has not been declared
  701 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                                         ^~~~~~
In file included from /usr/include/stdlib.h:706:
/usr/include/alloca.h:32:22: error: 'size_t' was not declared in this scope; did you mean 'std::size_t'?
   32 | extern void *alloca (size_t __size) __THROW;
      |                      ^~~~~~
      |                      std::size_t
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:310:33: note: 'std::size_t' declared here
  310 |   typedef __SIZE_TYPE__         size_t;
      |                                 ^~~~~~
/usr/include/stdlib.h:712:22: error: 'size_t' was not declared in this scope; did you mean 'std::size_t'?
  712 | extern void *valloc (size_t __size) __THROW __attribute_malloc__
      |                      ^~~~~~
      |                      std::size_t
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:310:33: note: 'std::size_t' declared here
  310 |   typedef __SIZE_TYPE__         size_t;
      |                                 ^~~~~~
/usr/include/stdlib.h:718:45: error: 'size_t' has not been declared
  718 | extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
      |                                             ^~~~~~
/usr/include/stdlib.h:718:65: error: 'size_t' has not been declared
  718 | extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
      |                                                                 ^~~~~~
/usr/include/stdlib.h:724:29: error: 'size_t' was not declared in this scope; did you mean 'std::size_t'?
  724 | extern void *a