Language: C++

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.7.4"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;

// import dcomp.datastructure.unionfind;

int main() {
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    struct E { int a, b; long c; }
    E[] elist;
    foreach (i; 0..n-1) {
        int a, b; long c;
        sc.read(a, b, c); a--; b--;
        elist ~= E(a, b, c);
    }
    elist.sort!"a.c > b.c";

    int[][] g = new int[][2*n-1];
    int[] gi = new int[n];
    foreach (i; 0..n) gi[i] = i;
    long[] sm = new long[2*n-1];

    auto uf = UnionFind(n);
    int z = n;
    foreach (e; elist) {
        int a = e.a, b = e.b;
        int x = gi[uf.id[a]], y = gi[uf.id[b]];
        long as = uf.group(a).length.to!long;
        long bs = uf.group(b).length.to!long;

        g[z] ~= x; g[z] ~= y;
//        writeln("add ", e.c*bs, " ", e.c*as);
//        writeln("g ", z, " ", x, " ", y);
        sm[x] = e.c * bs;
        sm[y] = e.c * as;
        uf.merge(a, b);
        gi[uf.id[a]] = z;
        z++;
    }

    long[] res = new long[n];
    void dfs(int p, long s) {
        s += sm[p];
        if (p < n) {
            res[p] = s;
        }
        foreach (d; g[p]) {
            dfs(d, s);
        }
    }
    dfs(z-1, 0);

    writeln(res.map!(to!string).join("\n"));
    return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.array;

 
class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                 
                 
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                FastAppender!(E[]) buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}


 
 

 
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;

 
T[N] fixed(T, size_t N)(T[N] a) {return a;}

 
 

 
struct FastAppender(A, size_t MIN = 4) {
    import std.algorithm : max;
    import std.conv;
    import std.range.primitives : ElementEncodingType;
    import core.stdc.string : memcpy;

    private alias T = ElementEncodingType!A;
    private T* _data;
    private uint len, cap;
     
    @property size_t length() const {return len;}
    bool empty() const { return len == 0; }
     
    void reserve(size_t nlen) {
        import core.memory : GC;
        if (nlen <= cap) return;
        
        void* nx = GC.malloc(nlen * T.sizeof);

        cap = nlen.to!uint;
        if (len) memcpy(nx, _data, len * T.sizeof);
        _data = cast(T*)(nx);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }
     
    void opOpAssign(string op : "~")(T item) {
        if (len == cap) {
            reserve(max(MIN, cap*2));
        }
        _data[len++] = item;
    }
     
    void insertBack(T item) {
        this ~= item;
    }
     
    void removeBack() {
        len--;
    }
     
    void clear() {
        len = 0;
    }
    ref inout(T) back() inout { assert(len); return _data[len-1]; }
    ref inout(T) opIndex(size_t i) inout { return _data[i]; }
     
    T[] data() {
        return (_data) ? _data[0..len] : null;
    }
}

 
 
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/datastructure/unionfind.d */
// module dcomp.datastructure.unionfind;

 
struct UnionFind {
    import std.algorithm : map, swap, each;
    import std.range : iota, array;
    import std.conv : to;
    int[] id;  
    int[][] groups;  
    int count;  
     
    this(size_t n) {
        id = iota(n.to!int).array;
        groups = iota(n.to!int).map!(a => [a]).array;
        count = n.to!int;
    }
     
    void merge(size_t a, size_t b) {
        if (same(a, b)) return;
        count--;
        int x = id[a], y = id[b];
        if (groups[x].length < groups[y].length) swap(x, y);
        groups[y].each!(a => id[a] = x);
        groups[x] ~= groups[y];
        groups[y] = [];
    }
     
    int[] group(size_t i) {
        return groups[id[i]];
    }
     
    bool same(size_t a, size_t b) {
        return id[a] == id[b];
    }
}

 
 

 
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
 
static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }
     
}

import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
    public import core.bitop : popcnt;
    int popcnt(ulong v) {
        return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
    }
}

bool poppar(ulong v) {
    v^=v>>1;
    v^=v>>2;
    v&=0x1111111111111111UL;
    v*=0x1111111111111111UL;
    return ((v>>60) & 1) != 0;
}

/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/


Compiler output:
a.cc:17:17: error: too many decimal points in number
   17 |     foreach (i; 0..n-1) {
      |                 ^~~~
a.cc:26:17: error: too many decimal points in number
   26 |     foreach (i; 0..n) gi[i] = i;
      |                 ^~~~
a.cc:118:29: error: too many decimal points in number
  118 |                 foreach (i; 0..T.length) {
      |                             ^~~~~~~~~~~
a.cc:170:5: error: stray '@' in program
  170 |     @property size_t length() const {return len;}
      |     ^
a.cc:210:32: error: too many decimal points in number
  210 |         return (_data) ? _data[0..len] : null;
      |                                ^~~~~~
a.cc:1:1: error: expected unqualified-id before '/' token
    1 | /+ dub.sdl:
      | ^
a.cc: In function 'int main()':
a.cc:12:5: error: 'Scanner' was not declared in this scope
   12 |     Scanner sc = new Scanner(stdin);
      |     ^~~~~~~
a.cc:14:5: error: 'sc' was not declared in this scope
   14 |     sc.read(n);
      |     ^~
a.cc:16:9: error: expected initializer before 'elist'
   16 |     E[] elist;
      |         ^~~~~
a.cc:17:14: error: 'i' was not declared in this scope
   17 |     foreach (i; 0..n-1) {
      |              ^
a.cc:22:5: error: 'elist' was not declared in this scope
   22 |     elist.sort!"a.c > b.c";
      |     ^~~~~
a.cc:24:8: error: structured binding declaration cannot have type 'int'
   24 |     int[][] g = new int[][2*n-1];
      |        ^~
a.cc:24:8: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:24:8: error: empty structured binding declaration
a.cc:24:10: error: expected initializer before '[' token
   24 |     int[][] g = new int[][2*n-1];
      |          ^
a.cc:25:8: error: structured binding declaration cannot have type 'int'
   25 |     int[] gi = new int[n];
      |        ^~
a.cc:25:8: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:25:8: error: empty structured binding declaration
a.cc:25:11: error: expected initializer before 'gi'
   25 |     int[] gi = new int[n];
      |           ^~
a.cc:26:15: error: expected ')' before ';' token
   26 |     foreach (i; 0..n) gi[i] = i;
      |             ~ ^
      |               )
a.cc:27:9: error: structured binding declaration cannot have type 'long int'
   27 |     long[] sm = new long[2*n-1];
      |         ^~
a.cc:27:9: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:27:9: error: empty structured binding declaration
a.cc:27:12: error: expected initializer before 'sm'
   27 |     long[] sm = new long[2*n-1];
      |            ^~
a.cc:29:15: error: 'UnionFind' was not declared in this scope
   29 |     auto uf = UnionFind(n);
      |               ^~~~~~~~~
a.cc:31:14: error: 'e' was not declared in this scope
   31 |     foreach (e; elist) {
      |              ^
a.cc:31:22: error: expected ';' before ')' token
   31 |     foreach (e; elist) {
      |                      ^
      |                      ;
a.cc:47:9: error: structured binding declaration cannot have type 'long int'
   47 |     long[] res = new long[n];
      |         ^~
a.cc:47:9: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:47:9: error: empty structured binding declaration
a.cc:47:12: error: expected initializer before 'res'
   47 |     long[] res = new long[n];
      |            ^~~
a.cc:48:29: error: a function-definition is not allowed here before '{' token
   48 |     void dfs(int p, long s) {
      |                             ^
a.cc:57:5: error: 'dfs' was not declared in this scope
   57 |     dfs(z-1, 0);
      |     ^~~
a.cc:59:13: error: 'res' was not declared in this scope
   59 |     writeln(res.map!(to!string).join("\n"));
      |             ^~~
a.cc:59:5: error: 'writeln' was not declared in this scope
   59 |     writeln(res.map!(to!string).join("\n"));
      |     ^~~~~~~
a.cc: At global scope:
a.cc:69:5: error: 'import' does not name a type
   69 |     import std.stdio : File;
      |     ^~~~~~
a.cc:69:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:70:5: error: 'import' does not name a type
   70 |     import std.conv : to;
      |     ^~~~~~
a.cc:70:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:71:5: error: 'import' does not name a type
   71 |     import std.range : front, popFront, array, ElementType;
      |     ^~~~~~
a.cc:71:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:72:5: error: 'import' does not name a type
   72 |     import std.array : split;
      |     ^~~~~~
a.cc:72:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:73:5: error: 'import' does not name a type
   73 |     import std.traits : isSomeChar, isStaticArray, isArray;
      |     ^~~~~~
a.cc:73:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:74:5: error: 'import' does not name a type
   74 |     import std.algorithm : map;
      |     ^~~~~~
a.cc:74:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:75:5: error: 'File' does not name a type
   75 |     File f;
      |     ^~~~
a.cc:76:5: error: expected unqualified-id before 'this'
   76 |     this(File f) {
      |     ^~~~
a.cc:79:9: error: expected unqualified-id before '[' token
   79 |     char[512] lineBuf;
      |         ^
a.cc:80:9: error: expected unqualified-id before '[' token
   80 |     char[] line;
      |         ^
a.cc:81:12: error: expected ':' before 'bool'
   81 |     private bool succW() {
      |            ^~~~~
      |            :
a.cc:89:12: error: expected ':' before 'bool'
   89 |     private bool succ() {
      |            ^~~~~
      |            :
a.cc:104:12: error: expected ':' before 'bool'
  104 |     private bool readSingle(T)(ref T x) {
      |            ^~~~~
      |            :
a.cc:104:29: error: 'T' has not been declared
  104 |     private bool readSingle(T)(ref T x) {
      |                             ^
a.cc:104:32: error: 'ref' has not been declared
  104 |     private bool readSingle(T)(ref T x) {
      |                                ^~~
a.cc:104:38: error: expected ',' or '...' before 'x'
  104 |     private bool readSingle(T)(ref T x) {
      |                                      ^
a.cc:104:13: error: 'readSingle' declared as function returning a function
  104 |     private bool readSingle(T)(ref T x) {
      |             ^~~~
a.cc:135:14: error: 'T' has not been declared
  135 |     int read(T, Args...)(ref T x, auto ref Args args) {
      |              ^
a.cc:135:17: error: 'Args' has not been declared
  135 |     int read(T, Args...)(ref T x, auto ref Args args) {
      |                 ^~~~
a.cc:135:26: error: 'ref' has not been declared
  135 |     int read(T, Args...)(ref T x, auto ref Args args) {
      |                          ^~~
a.cc:135:32: error: expected ',' or '...' before 'x'
  135 |     int read(T, Args...)(ref T x, auto ref Args args) {
      |                                ^
a.cc:135:5: error: 'read' declared as function returning a function
  135 |     int read(T, Args...)(ref T x, auto ref Args args) {
      |     ^~~
a.cc: In member function 'bool Scanner::succW()':
a.cc:82:9: error: 'import' was not declared in this scope
   82 |         import std.range.primitives : empty, front, popFront;
      |         ^~~~~~
a.cc:83:16: error: expected ';' before 'std'
   83 |         import std.ascii : isWhite;
      |                ^~~
a.cc:84:17: error: 'line' was not declared in this scope
   84 |         while (!line.empty && line.front.isWhite) {
      |                 ^~~~
a.cc:87:17: error: 'line' was not declared in this scope
   87 |         return !line.empty;
      |                 ^~~~
a.cc: In member function 'bool Scanner::succ()':
a.cc:90:9: error: 'import' was not declared in this scope
   90 |         import std.range.primitives : empty, front, popFront;
      |         ^~~~~~
a.cc:91:16: error: expected ';' before 'std'
   91 |         import std.ascii : isWhite;
      |                ^~~
a.cc:93:21: error: 'line' was not declared in this scope
   93 |             while (!line.empty && line.front.isWhite) {
      |                     ^~~~
a.cc:96:18: error: 'line' was not declared in this scope
   96 |             if (!line.empty) break;
      |                  ^~~~
a.cc:97:13: error: 'line' was not declared in this scope
   97 |             line = lineBuf[];
      |             ^~~~
a.cc:97:20: error: 'lineBuf' was not declared in this scope; did you mean 'linux'?
   97 |             line = lineBuf[];
      |                    ^~~~~~~
      |                    linux
a.cc:97:28: error: expected primary-expression before ']' token
   97 |             line = lineBuf[];
      |                            ^
a.cc:98:13: error: 'f' was not declared in this scope
   98 |             f.readln(line);
      |             ^
a.cc: At global scope:
a.cc:154:3: error: 'N' was not declared in this scope
  154 | T[N] fixed(T, size_t N)(T[N] a) {return a;}
      |   ^
a.cc:154:6: error: expected initializer before 'fixed'
  154 | T[N] fixed(T, size_t N)(T[N] a) {return a;}
      |      ^~~~~
a.cc:160:22: error: expected ')' before ',' token
  160 | struct FastAppender(A, size_t MIN = 4) {
      |                    ~ ^
      |                      )
a.cc:160:31: error: expected initializer before 'MIN'
  160 | struct FastAppender(A, size_t MIN = 4) {
      |                               ^~~
a.cc:221:5: error: 'import' does not name a type
  221 |     import std.algorithm : map, swap, each;
      |     ^~~~~~
a.cc:221:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:222:5: error: 'import' does not name a type
  222 |     import std.range : iota, array;
      |     ^~~~~~
a.cc:222:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:223:5: error: 'import' does not name a type
  223 |     import std.conv : to;
      |     ^~~~~~
a.cc:223:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:224:8: error: expected unqualified-id before '[' token
  224 |     int[] id;
      |        ^
a.cc:225:8: error: expected unqualified-id before '[' token
  225 |     int[][] groups;
      |        ^
a.cc:228:5: error: expected unqualified-id before 'this'
  228 |     this