#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;
vector< pair<int, int> > gra[MAXN];
int fa[MAXN], c[MAXN];
int loop[MAXN], loopn, loopcost[MAXN], loopsum;
bool flag[MAXN];
int treecnt[MAXN], treesum[MAXN], sum[MAXN];
int n;

void DFS0(int nowp, int pre)
{

    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre)
    {
        if(fa[iter->first] == 0)
        {
            fa[iter->first] = nowp;
            c[iter->first] = iter->second;
            DFS0(iter->first, nowp);
        }
        else
        {
            if(loopn > 0) return;
            for(int x = nowp; x != iter->first; x = fa[x])
                loop[loopn] = x, loopcost[loopn++] = c[x];
            loop[loopn] = iter->first, loopcost[loopn++] = iter->second;
            return;
        }
    }

    return;
}

void DFS1(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        DFS1(iter->first, nowp);
        treecnt[nowp] += treecnt[iter->first];
        treesum[nowp] += treecnt[iter->first] * iter->second;
    }

    return;
}

void DFS2(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        sum[iter->first] = sum[nowp] - treecnt[iter->first] * iter->second + (n - treecnt[iter->first]) * iter->second;
        DFS2(iter->first, nowp);
    }

    return;
}

int main()
{
    int loopintv, nowindex, temsum, intvcnt;

    // freopen("D:\\1.in", "r", stdin);
    // freopen("D:\\1.out", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        gra[a].push_back(make_pair(b, t));
        gra[b].push_back(make_pair(a, t));
    }

    // Find loop
    fa[1] = -1;
    DFS0(1, 0);
    for(int i = 0; i < loopn; i++) loopsum += loopcost[i];

    // DFS nodes except for the loop
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS1(loop[i], 0);

    // Calculate answers for loop nodes
    loopintv = nowindex = temsum = intvcnt = 0;
    for(nowindex = 0; nowindex < loopn; nowindex++)
    if((loopintv + loopcost[nowindex]) * 2 <= loopsum)
    {
        loopintv += loopcost[nowindex];
        temsum += treesum[loop[(nowindex + 1) % loopn]] + loopintv * treecnt[loop[(nowindex + 1) % loopn]];
        intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
    }
    else break;
    for(int i = nowindex, j = loopsum - loopintv; i < loopn; i++)
    {
        j -= loopcost[i];
        temsum += treesum[loop[(i + 1) % loopn]] + j * treecnt[loop[(i + 1) % loopn]];
    }
    for(int i = 0; i < loopn; i++)
    {
        sum[loop[i]] = temsum;
        temsum += (n - intvcnt) * loopcost[i] - intvcnt * loopcost[i];
        loopintv -= loopcost[i];
        intvcnt -= treecnt[loop[(i + 1) % loopn]];
        while((loopintv + loopcost[nowindex]) * 2 <= loopsum)
        {
            loopintv += loopcost[nowindex];
            temsum += loopintv * treecnt[loop[(nowindex + 1) % loopn]] - (loopsum - loopintv) * treecnt[loop[(nowindex + 1) % loopn]];
            intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
            nowindex = (nowindex + 1) % loopn;
        }
    }

    // Calculate tree nodes
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS2(loop[i], 0);

    // Output
    for(int i = 1; i <= n; i++) printf("%d ", sum[i]);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;
vector< pair<int, int> > gra[MAXN];
int fa[MAXN], c[MAXN];
int loop[MAXN], loopn, loopcost[MAXN], loopsum;
bool flag[MAXN];
int treecnt[MAXN], treesum[MAXN], sum[MAXN];
int n;

void DFS0(int nowp, int pre)
{

    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre)
    {
        if(fa[iter->first] == 0)
        {
            fa[iter->first] = nowp;
            c[iter->first] = iter->second;
            DFS0(iter->first, nowp);
        }
        else
        {
            if(loopn > 0) return;
            for(int x = nowp; x != iter->first; x = fa[x]) loop[loopn] = x, loopcost[loopn++] = c[x];
            loop[loopn] = iter->first, loopcost[loopn++] = c[iter->first];
            return;
        }
    }

    return;
}

void DFS1(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        DFS1(iter->first, nowp);
        treecnt[nowp] += treecnt[iter->first];
        treesum[nowp] += treecnt[iter->first] * iter->second;
    }

    return;
}

void DFS2(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        sum[iter->first] = sum[nowp] - treecnt[iter->first] * iter->second + (n - treecnt[iter->first]) * iter->second;
        DFS1(iter->first, nowp);
    }

    return;
}

int main()
{
    int loopintv, nowindex, temsum, intvcnt;

    // freopen("D:\\1.in", "r", stdin);
    // freopen("D:\\1.out", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        gra[a].push_back(make_pair(b, t));
        gra[b].push_back(make_pair(a, t));
    }

    // Find loop
    DFS0(1, 0);
    for(int i = 0; i < loopn; i++) loopsum += loopcost[i];

    // DFS nodes except for the loop
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS1(loop[i], 0);

    // Calculate answers for loop nodes
    loopintv = nowindex = temsum = intvcnt = 0;
    for(nowindex = 0; nowindex < loopn; nowindex++)
    if((loopintv + loopcost[nowindex]) * 2 <= loopsum)
    {
        loopintv += loopcost[nowindex];
        temsum += treesum[loop[(nowindex + 1) % loopn]] + loopintv * treecnt[loop[(nowindex + 1) % loopn]];
        intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
    }
    else break;
    for(int i = nowindex, j = loopsum - loopintv; i < loopn; i++)
    {
        j -= loopcost[i];
        temsum += treesum[loop[(i + 1) % loopn]] + j * treecnt[loop[(i + 1) % loopn]];
    }
    for(int i = 0; i < loopn; i++)
    {
        sum[loop[i]] = temsum;
        temsum += (n - intvcnt) * loopcost[i] - intvcnt * loopcost[i];
        loopintv -= loopcost[i];
        intvcnt -= treecnt[loop[i]];
        while((loopintv + loopcost[nowindex]) * 2 <= loopsum)
        {
            loopintv += loopcost[nowindex];
            temsum += loopintv * treecnt[loop[(nowindex + 1) % loopn]] - (loopsum - loopintv) * treecnt[loop[(nowindex + 1) % loopn]];
            intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
            nowindex = (nowindex + 1) % loopn;
        }
    }

    // Calculate tree nodes
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS2(loop[i], 0);

    // Output
    for(int i = 1; i <= n; i++) printf("%d ", sum[i]);

    return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 200005
#define inf 0xfffffff
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define x first
#define y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
using namespace std;
vector<int>g[N],f[N];
int c[N],d[N*3],m,q,v[N],w[N*3];
ll e[N],r1[N],r2[N],s[N*3];
void circle(int u,int p,int l)
{
    if(v[u])
    {
        int i,j;
        for(i=l-1;d[i]!=u;i--)c[d[i]]=1;
        for(j=i;j<l;j++)d[j-i]=d[j];
        m=l-i;c[u]=q=1;
        return;
    }
    v[u]=1;d[l]=u;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i];
        if(j!=p)circle(j,u,l+1);
        if(q)break;
    }
}
void dfs(int u,int fa)
{
    r1[u]=0;e[u]=1;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=fa&&!c[j])
        {
            dfs(j,u);
            r1[u]+=r1[j]+w*e[j];
            e[u]+=e[j];
        }
    }
}
void count(int u,int p,ll s,ll t)
{
    r2[u]=s;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=p&&!c[j])
        {
            count(j,u,s+r1[u]-r1[j]+w*(t+e[u]-2*e[j]),t+e[u]-e[j]);
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int i,j,k,n;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)g[i].clear(),f[i].clear();
        for(i=0;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].pb(v),f[u].pb(w);
            g[v].pb(u),f[v].pb(w);
        }
        memset(v,0,sizeof(v));
        q=0;
        memset(c,0,sizeof(c));
        circle(1,0,0);
        for(i=0;i<m;i++)
        {
            dfs(d[i],0);
            d[m+i]=d[m+m+i]=d[i];
            for(j=0;j<g[d[i]].size();j++)
            {
                int x=g[d[i]][j];
                if(x==d[i+1])
                {
                    w[i]=f[d[i]][j];break;
                }
            }
            w[m+i]=w[m+m+i]=w[i];
            s[i+1]=s[i]+w[i];
        }
        for(i=m;i<3*m;i++)s[i+1]=s[i]+w[i];
        ll LR=0,l=0,r=0;
        int x=1,y=m;
        for(i=1;i<m;i++)
        {
            LR+=(s[m]-s[i])*e[d[i]]+r1[d[i]];
            l+=e[d[i]];
        }
        for(i=m;i<m+m;i++)
        {
            while(x<i&&(s[i]-s[x]>s[y+1]-s[i]||y<i))
            {
                LR+=(s[y+1]-s[i]-s[i]+s[x])*e[d[x]];
                l-=e[d[x]];r+=e[d[y+1]];
                x++;y++;
            }
            count(d[i],0,LR,l+r);
            l+=e[d[i]];
            LR+=(l-r)*w[i]+r1[d[i]]-r1[d[i+1]];
            r-=e[d[i+1]];
        }
        for(i=1;i<=n;i++)
        {
            printf("%I64d ",r1[i]+r2[i]);
        }
        puts("");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 600005
#define inf 0xfffffff
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define x first
#define y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
using namespace std;
vector<int>g[N],f[N];
int c[N],d[N],m,q,v[N];
ll e[N],r1[N],r2[N],w[N],s[N];
void circle(int u,int p,int l)
{
    if(v[u])
    {
        int i,j;
        for(i=l-1;d[i]!=u;i--)c[d[i]]=1;
        for(j=i;j<l;j++)d[j-i]=d[j];
        m=l-i;c[u]=q=1;
    }
    v[u]=1;d[l]=u;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i];
        if(j!=p)circle(j,u,l+1);
        if(q)break;
    }
}
void dfs(int u,int fa)
{
    r1[u]=0;e[u]=1;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=fa&&!c[j])
        {
            dfs(j,u);
            r1[u]+=r1[j]+w*e[j];
            e[u]+=e[j];
        }
    }
}
void count(int u,int p,ll s,ll t)
{
    r2[u]=s;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=p&&!c[j])
        {
            count(j,u,s+r1[u]-r1[j]+w*(t+e[u]-2*e[j]),t+e[u]-e[j]);
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int i,j,k,n;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)g[i].clear(),f[i].clear();
        for(i=0;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].pb(v),f[u].pb(w);
            g[v].pb(u),f[v].pb(w);
        }
        memset(v,0,sizeof(v));
        q=0;
        memset(c,0,sizeof(c));
        circle(1,0,0);
        for(i=0;i<m;i++)
        {
            dfs(d[i],0);
            d[m+i]=d[m+m+i]=d[i];
            for(j=0;j<g[d[i]].size();j++)
            {
                int x=g[d[i]][j];
                if(x==d[i+1])
                {
                    w[i]=f[d[i]][j];break;
                }
            }
            w[m+i]=w[m+m+i]=w[i];
            s[i+1]=s[i]+w[i];
        }
        for(i=m;i<3*m;i++)s[i+1]=s[i]+w[i];
        ll LR=0,l=0,r=0;
        int x=1,y=m;
        for(i=1;i<m;i++)
        {
            LR+=(s[m]-s[i])*e[d[i]]+r1[d[i]];
            l+=e[d[i]];
        }
        for(i=m;i<m+m;i++)
        {
            while(x<i&&(s[i]-s[x]>s[y+1]-s[i]||y<i))
            {
                LR+=(s[y+1]-s[i]-s[i]+s[x])*e[d[x]];
                l-=e[d[x]];r+=e[d[y+1]];
                x++;y++;
            }
            count(d[i],0,LR,l+r);
            l+=e[d[i]];
            LR+=(l-r)*w[i]+r1[d[i]]-r1[d[i+1]];
            r-=e[d[i+1]];
        }
        for(i=1;i<=n;i++)
        {
            printf("%I64d ",r1[i]+r2[i]);
        }
        puts("");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 200005
#define inf 0xfffffff
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define x first
#define y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
using namespace std;
vector<int>g[N],f[N];
int c[N],d[N*3],m,q,v[N],w[N*3];
ll e[N],r1[N],r2[N],s[N*3];
void circle(int u,int p,int l)
{
    if(v[u])
    {
        int i,j;
        for(i=l-1;d[i]!=u;i--)c[d[i]]=1;
        for(j=i;j<l;j++)d[j-i]=d[j];
        m=l-i;c[u]=q=1;
    }
    v[u]=1;d[l]=u;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i];
        if(j!=p)circle(j,u,l+1);
        if(q)break;
    }
}
void dfs(int u,int fa)
{
    r1[u]=0;e[u]=1;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=fa&&!c[j])
        {
            dfs(j,u);
            r1[u]+=r1[j]+w*e[j];
            e[u]+=e[j];
        }
    }
}
void count(int u,int p,ll s,ll t)
{
    r2[u]=s;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=p&&!c[j])
        {
            count(j,u,s+r1[u]-r1[j]+w*(t+e[u]-2*e[j]),t+e[u]-e[j]);
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int i,j,k,n;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)g[i].clear(),f[i].clear();
        for(i=0;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].pb(v),f[u].pb(w);
            g[v].pb(u),f[v].pb(w);
        }
        memset(v,0,sizeof(v));
        q=0;
        memset(c,0,sizeof(c));
        circle(1,0,0);
        for(i=0;i<m;i++)
        {
            dfs(d[i],0);
            d[m+i]=d[m+m+i]=d[i];
            for(j=0;j<g[d[i]].size();j++)
            {
                int x=g[d[i]][j];
                if(x==d[i+1])
                {
                    w[i]=f[d[i]][j];break;
                }
            }
            w[m+i]=w[m+m+i]=w[i];
            s[i+1]=s[i]+w[i];
        }
        for(i=m;i<3*m;i++)s[i+1]=s[i]+w[i];
        ll LR=0,l=0,r=0;
        int x=1,y=m;
        for(i=1;i<m;i++)
        {
            LR+=(s[m]-s[i])*e[d[i]]+r1[d[i]];
            l+=e[d[i]];
        }
        for(i=m;i<m+m;i++)
        {
            while(x<i&&(s[i]-s[x]>s[y+1]-s[i]||y<i))
            {
                LR+=(s[y+1]-s[i]-s[i]+s[x])*e[d[x]];
                l-=e[d[x]];r+=e[d[y+1]];
                x++;y++;
            }
            count(d[i],0,LR,l+r);
            l+=e[d[i]];
            LR+=(l-r)*w[i]+r1[d[i]]-r1[d[i+1]];
            r-=e[d[i+1]];
        }
        for(i=1;i<=n;i++)
        {
            printf("%I64d ",r1[i]+r2[i]);
        }
        puts("");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 200005
#define inf 0xfffffff
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define x first
#define y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
using namespace std;
vector<int>g[N],f[N];
int c[N],d[N],m,q,v[N];
ll e[N],r1[N],r2[N],w[N],s[N];
void circle(int u,int p,int l)
{
    if(v[u])
    {
        int i,j;
        for(i=l-1;d[i]!=u;i--)c[d[i]]=1;
        for(j=i;j<l;j++)d[j-i]=d[j];
        m=l-i;c[u]=q=1;
    }
    v[u]=1;d[l]=u;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i];
        if(j!=p)circle(j,u,l+1);
        if(q)break;
    }
}
void dfs(int u,int fa)
{
    r1[u]=0;e[u]=1;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=fa&&!c[j])
        {
            dfs(j,u);
            r1[u]+=r1[j]+w*e[j];
            e[u]+=e[j];
        }
    }
}
void count(int u,int p,ll s,ll t)
{
    r2[u]=s;
    for(int i=0;i<g[u].size();i++)
    {
        int j=g[u][i],w=f[u][i];
        if(j!=p&&!c[j])
        {
            count(j,u,s+r1[u]-r1[j]+w*(t+e[u]-2*e[j]),t+e[u]-e[j]);
        }
    }
}
int main()
{
   // freopen("in.txt","r",stdin);
    int i,j,k,n;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)g[i].clear(),f[i].clear();
        for(i=0;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].pb(v),f[u].pb(w);
            g[v].pb(u),f[v].pb(w);
        }
        memset(v,0,sizeof(v));
        q=0;
        memset(c,0,sizeof(c));
        circle(1,0,0);
        for(i=0;i<m;i++)
        {
            dfs(d[i],0);
            d[m+i]=d[m+m+i]=d[i];
            for(j=0;j<g[d[i]].size();j++)
            {
                int x=g[d[i]][j];
                if(x==d[i+1])
                {
                    w[i]=f[d[i]][j];break;
                }
            }
            w[m+i]=w[m+m+i]=w[i];
            s[i+1]=s[i]+w[i];
        }
        for(i=m;i<3*m;i++)s[i+1]=s[i]+w[i];
        ll LR=0,l=0,r=0;
        int x=1,y=m;
        for(i=1;i<m;i++)
        {
            LR+=(s[m]-s[i])*e[d[i]]+r1[d[i]];
            l+=e[d[i]];
        }
        for(i=m;i<m+m;i++)
        {
            while(x<i&&(s[i]-s[x]>s[y+1]-s[i]||y<i))
            {
                LR+=(s[y+1]-s[i]-s[i]+s[x])*e[d[x]];
                l-=e[d[x]];r+=e[d[y+1]];
                x++;y++;
            }
            count(d[i],0,LR,l+r);
            l+=e[d[i]];
            LR+=(l-r)*w[i]+r1[d[i]]-r1[d[i+1]];
            r-=e[d[i+1]];
        }
        for(i=1;i<=n;i++)
        {
            printf("%I64d ",r1[i]+r2[i]);
        }
        puts("");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 200005
#define inf 0xfffffff
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define x first
#define y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
using namespace std;
vector<pair<int,int> >g[N];
int f[N],v[N],n;
struct node
{
    int u,w;
    node(){}
    node(int _u,int _w):u(_u),w(_w){}
    bool operator <(const node &a)const
    {
        return w>a.w;
    }
};
int spfa(int s)
{
    for(int i=1;i<=n;i++)f[i]=inf;
    priority_queue<node>q;
    q.push(node(s,0));
    memset(v,0,sizeof(v));
    f[s]=0;
    while(!q.empty())
    {
        int u=q.top().u;q.pop();
        v[u]=0;
        for(int i=0;i<g[u].size();i++)
        {
            int j=g[u][i].x,w=g[u][i].y;
            if(f[j]>f[u]+w)
            {
                f[j]=f[u]+w;
                if(!v[j])
                {
                    v[j]=1;q.push(node(j,f[j]));
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    if(i!=s)
    f[s]+=f[i];
    return f[s];
}
int main()
{
    //freopen("in.txt","r",stdin);
    int i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)g[i].clear();
        for(i=0;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].pb(mp(v,w));
            g[v].pb(mp(u,w));
        }
        for(i=1;i<=n;i++)
        printf("%d ",spfa(i));
        puts("");
    }
    return 0;
}


#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#define N 210000
using namespace std;
int head[N],next[N*2],e[N*2],c[N*2],size[N*2],sum[N],i,j,loop[N*2],S,o,bo[N],len,
	x,y,n,fa[N],d[N],tot,k;
long long f[N],g[N],tmp;
void findcircle(int x){
	bo[x]=1;
	for(int i=head[x];i;i=next[i])if(e[i]!=fa[x]){
		if(bo[e[i]]){
			if(tot)return;
			for(int j=x;j!=e[i];j=fa[j])loop[++tot]=j,sum[tot+1]=d[j];
			loop[++tot]=e[i]; sum[tot+1]=c[i];
		}else fa[e[i]]=x,d[e[i]]=c[i],findcircle(e[i]);
	}
}
void dfs(int x){
	bo[x]=1;
	size[x]=1;
	for(int i=head[x];i;i=next[i])if(!bo[e[i]]){
		dfs(e[i]);
		size[x]+=size[e[i]];
		f[x]+=f[e[i]]+1ll*c[i]*size[e[i]];
	}
}
void dfs2(int x){
	bo[x]=1;
	for(int i=head[x];i;i=next[i])if(!bo[e[i]]){
		g[e[i]]=g[x]+f[x]-f[e[i]]+1ll*(n-2*size[e[i]])*c[i];
		dfs2(e[i]);
	}
}
int main(){
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		scanf("%d%d%d",&x,&y,&k);
		next[++len]=head[x]; head[x]=len; e[len]=y; c[len]=k;
		next[++len]=head[y]; head[y]=len; e[len]=x; c[len]=k;
	}
	memset(bo,0,sizeof(bo));
	findcircle(1);
	memset(bo,0,sizeof(bo));
	for(i=1;i<=tot;++i)bo[loop[i]]=1;
	for(i=1;i<=tot;++i)dfs(loop[i]);
	for(i=1;i<=tot;++i)loop[i+tot]=loop[i],sum[i+tot+1]=sum[i+1];
	for(i=1;i<=tot*2;++i)sum[i]+=sum[i-1];
	S=sum[tot+1];	j=1;	tmp=0;	o=0;
	for(i=2;i<=tot;++i)tmp+=f[loop[i]]+1ll*size[loop[i]]*(S-sum[i]);
	for(i=1;i<=tot;++i){
		while((sum[j+1]-sum[i])*2<S){
			++j;
			tmp=tmp+1ll*size[loop[j]]*(2*(sum[j]-sum[i])-S);
			o+=size[loop[j]];
		}
		g[loop[i]]=tmp;
		tmp+=1ll*(sum[i+1]-sum[i])*(n-o-o);
		o-=size[loop[i+1]];
	}
	memset(bo,0,sizeof(bo));
	for(i=1;i<=tot;++i)bo[loop[i]]=1;
	for(i=1;i<=tot;++i)dfs2(loop[i]);
	for(i=1;i<=n;++i)printf("%I64d ",f[i]+g[i]);
}


#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#define N 210000
using namespace std;
int head[N],next[N*2],e[N*2],c[N*2],size[N*2],sum[N],i,j,loop[N*2],S,o,bo[N],len,
	x,y,n,fa[N],d[N],tot,k;
long long f[N],g[N],tmp;
void findcircle(int x){
	bo[x]=1;
	for(int i=head[x];i;i=next[i])if(e[i]!=fa[x]){
		if(bo[e[i]]){
			if(tot)return;
			for(int j=x;j!=e[i];j=fa[j])loop[++tot]=j,sum[tot+1]=d[j];
			loop[++tot]=e[i]; sum[tot+1]=c[i];
		}else fa[e[i]]=x,d[e[i]]=c[i],findcircle(e[i]);
	}
}
void dfs(int x){
	bo[x]=1;
	size[x]=1;
	for(int i=head[x];i;i=next[i])if(!bo[e[i]]){
		dfs(e[i]);
		size[x]+=size[e[i]];
		f[x]+=f[e[i]]+1ll*c[i]*size[e[i]];
	}
}
void dfs2(int x){
	bo[x]=1;
	for(int i=head[x];i;i=next[i])if(!bo[e[i]]){
		g[e[i]]=g[x]+f[x]-f[e[i]]+1ll*(n-2*size[e[i]])*c[i];
		dfs2(e[i]);
	}
}
int main(){
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		scanf("%d%d%d",&x,&y,&k);
		next[++len]=head[x]; head[x]=len; e[len]=y; c[len]=k;
		next[++len]=head[y]; head[y]=len; e[len]=x; c[len]=k;
	}
	memset(bo,0,sizeof(bo));
	findcircle(1);
	memset(bo,0,sizeof(bo));
	for(i=1;i<=tot;++i)bo[loop[i]]=1;
	for(i=1;i<=tot;++i)dfs(loop[i]);
	for(i=1;i<=tot;++i)loop[i+tot]=loop[i],sum[i+tot+1]=sum[i+1];
	for(i=1;i<=tot*2;++i)sum[i]+=sum[i-1];
	S=sum[tot+1];	j=1;	tmp=0;	o=0;
	for(i=2;i<=tot;++i)tmp+=f[loop[i]]+1ll*size[loop[i]]*(S-sum[i]);
	for(i=1;i<=tot;++i){
		while((sum[j+1]-sum[i])*2<S){
			++j;
			tmp=tmp+1ll*size[loop[j]]*(2*(sum[j]-sum[i])-S);
			o+=size[loop[j]];
		}
		g[loop[i]]=tmp;
		tmp+=1ll*(sum[i+1]-sum[i])*(n-o-o)+f[loop[i]]-f[loop[i+1]];
		o-=size[loop[i+1]];
	}
	memset(bo,0,sizeof(bo));
	for(i=1;i<=tot;++i)bo[loop[i]]=1;
	for(i=1;i<=tot;++i)dfs2(loop[i]);
	for(i=1;i<=n;++i)printf("%I64d ",f[i]+g[i]);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,n) for (int i=1;i<=n;++i)
typedef long long ll;
const int N=600005;
int n,m,E,x,y,z,son[N],ed[N],next[N],data[N],fa[N],a[N],c[N],d[N],p[N],q[N];
ll s[N],f[N],g[N];
bool circle(int x)
{
	for (int p=son[x];p;p=next[p]){
		int y=ed[p];
		if (p!=fa[x]){
			if (fa[y]){
				d[++m]=data[p];
				for (int i=x;i!=y;i=ed[fa[i]]) a[m]=i,d[++m]=data[fa[i]];
				a[m]=y; return 1;
			}else fa[y]=p^1; if (circle(y)) return 1;
		}
	}
	return 0;
}
void dp(int x)
{
	c[x]=1,s[x]=0;
	for (int p=son[x];p;p=next[p]){
		int y=ed[p];
		if (!fa[y]) fa[y]=x,dp(y),c[x]+=c[y],s[x]+=s[y]+c[y]*data[p];
	}
}
void dfs(int x)
{
	for (int p=son[x];p;p=next[p]){
		int y=ed[p];
		if (fa[y]>0 && fa[x]!=y)
			s[y]=s[x]+(n-c[y]*2)*data[p],dfs(y);
	}
}
int main()
{
	scanf("%d",&n),E=1;
	rep(i,n) scanf("%d%d%d",&x,&y,&z),
		ed[++E]=y,data[E]=z,next[E]=son[x],son[x]=E,
		ed[++E]=x,data[E]=z,next[E]=son[y],son[y]=E;
	fa[1]=-1,circle(1); rep(i,n) fa[i]=0; rep(i,m) fa[a[i]]=-1;
	rep(i,m) a[m+m+i]=a[m+i]=a[i],d[m+m+i]=d[m+i]=d[i],dp(a[i]);
	for (int i=m*3;i;--i) q[i]=q[i+1]+c[a[i]],g[i]=g[i+1]+s[a[i]]+q[i+1]*d[i+1];
	rep(i,m*3) p[i]=p[i-1]+c[a[i]],f[i]=f[i-1]+s[a[i]]+p[i-1]*d[i],d[i]+=d[i-1];
	int w=1;
	for (int i=m;i<m+m;++i){
		while (d[i]-d[w]>d[w+m]-d[i]) ++w; int l=w-1,r=w+m;
		s[a[i]]=f[i]-f[l]-1LL*p[l]*(d[i]-d[l])+g[i]-g[r]-1LL*q[r]*(d[r]-d[i])-s[a[i]];
	}
	rep(i,m) dfs(a[i]); rep(i,n) printf("%I64d ",s[i]);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long int ll;

const int limite=1000000;

int n;
vector<pair<int,int> > g[limite];
ll numnodos[limite];
ll precomputo[limite];

int nc=0,lenc=0;
int c[limite];
ll d[limite];

int cc[limite];
int dd[limite];
int visto[limite];

bool buscaciclo(int u,int p,int prof)
{
  if (visto[u]) {
    int icc=prof-1;
    while (cc[icc]!=u) icc--;
    for (int i=icc;i<prof;i++) {
      c[i-icc]=cc[i];
      d[i-icc]=dd[i];
      nc++;
      lenc+=dd[i];
    }
    return true;
  }
  visto[u]=1;
  cc[prof]=u;
  vector<pair<int,int> > &ar=g[u];
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    dd[prof]=ar[i].second;
    if (v!=p and buscaciclo(v,u,prof+1))
      return true;
  }
  return false;
}

void eliminaadyacente(int u,int elim)
{
  vector<pair<int,int> > &ar=g[u];
  vector<pair<int,int> > nextar;
  for (int i=0;i<int(ar.size());i++)
    if (elim!=ar[i].first)
      nextar.push_back(ar[i]);
  ar=nextar;
}

void quitapadre(int u,int p)
{
  vector<pair<int,int> > &ar=g[u];
  vector<pair<int,int> > nextar;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    if (v!=p) {
      nextar.push_back(ar[i]);
      quitapadre(v,u);
    }
  }
  ar=nextar;
}

void precomputa(int u)
{
  vector<pair<int,int > > &ar=g[u];
  numnodos[u]=1;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    ll t=ar[i].second;
    precomputa(v);
    numnodos[u]+=numnodos[v];
    precomputo[u]+=precomputo[v]+numnodos[v]*t;
  }
}

ll precomputodebajo(int ic)
{
  int u=c[ic];
  vector<pair<int,int> > &ar=g[u];
  ll pre=0;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    ll t=ar[i].second;
    if (v!=c[(ic+1)%nc] and v!=c[(ic-1+nc)%nc])
      pre+=precomputo[v]+numnodos[v]*t;
  }
  return pre;
}

ll numnodosdebajo(int ic)
{
  int u=c[ic];
  vector<pair<int,int> > &ar=g[u];
  ll num=0;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    if (v!=c[(ic+1)%nc] and v!=c[(ic-1+nc)%nc])
      num+=numnodos[v];
  }
  return num;
}

ll sol[limite];

void calcula(int u,ll computoabove)
{
  //cout<<"calculamos "<<u<<endl;
  vector<pair<int,int> > &ar=g[u];
  sol[u]=computoabove+precomputo[u];
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    ll t=ar[i].second;
    calcula(v,sol[u]-(precomputo[v]+numnodos[v]*t)+(n-numnodos[v])*t);
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin>>n;
  for (int i=0;i<n;i++) {
    int u,v,t;
    cin>>u>>v>>t;
    g[u].push_back(pair<int,int> (v,t));
    g[v].push_back(pair<int,int> (u,t));
  }
  buscaciclo(1,0,0);
  eliminaadyacente(c[0],c[1]);
  eliminaadyacente(c[1],c[0]);
  quitapadre(c[0],0);
  precomputa(c[0]);
  int ri=0;
  int lenri=0;
  ll nodri=numnodosdebajo(0)+1;
  ll computori=precomputodebajo(0);
  ll computole=precomputo[c[0]]-computori;
  //cout<<"inicio "<<nodri<<" "<<computori<<" "<<computole<<endl;
  for (int i=0;i<nc;i++) {
    int u=c[i];
    //cout<<"ciclo "<<i<<" "<<u<<" "<<ri<<" "<<nodri<<" "<<computori<<" "<<computole<<endl;
    while (2*(lenri+d[ri])<=lenc) {
      lenri+=d[ri];
      ri=(ri+1)%nc;
      ll comp=precomputodebajo(ri);
      ll numn=numnodosdebajo(ri);
      nodri+=numn+1;
      computori+=comp+(numn+1)*lenri;
      computole-=comp+(numn+1)*(lenc-lenri);
    }
    //cout<<"despues "<<ri<<" "<<c[ri]<<" "<<nodri<<" "<<computori<<" "<<computole<<endl;
    sol[u]=computori+computole;
    vector<pair<int,int> > &ar=g[u];
    for (int j=0;j<int(ar.size());j++) {
      int v=ar[j].first;
      ll t=ar[j].second;
      if (v!=c[(i+1)%nc] and v!=c[(i-1+nc)%nc])
	calcula(v,computori+computole-(precomputo[v]+numnodos[v]*t)+(n-numnodos[v])*t);
    }
    ll comp=precomputodebajo(i);
    ll numn=numnodosdebajo(i);
    //cout<<"debajo "<<comp<<" "<<numn<<" "<<d[i]<<endl;
    computori-=comp;
    nodri-=numn+1;
    computori-=nodri*d[i];
    computole+=comp+(n-nodri)*d[i];
    lenri-=d[i];
  }
  for (int u=1;u<=n;u++) {
    if (u>1) cout<<" ";
    cout<<sol[u];
  }
  cout<<endl;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long int ll;

const int limite=1000000;

int n;
vector<pair<int,int> > g[limite];
ll numnodos[limite];
ll precomputo[limite];

int nc=0,lenc=0;
int c[limite];
ll d[limite];

int cc[limite];
int dd[limite];
int visto[limite];

bool buscaciclo(int u,int p,int prof)
{
  if (visto[u]) {
    int icc=prof-1;
    while (cc[icc]!=u) icc--;
    for (int i=icc;i<prof;i++) {
      c[i-icc]=cc[i];
      d[i-icc]=dd[i];
      nc++;
      lenc+=dd[i];
    }
    return true;
  }
  visto[u]=1;
  cc[prof]=u;
  vector<pair<int,int> > &ar=g[u];
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    dd[prof]=ar[i].second;
    if (v!=p and buscaciclo(v,u,prof+1))
      return true;
  }
  return false;
}

void eliminaadyacente(int u,int elim)
{
  vector<pair<int,int> > &ar=g[u];
  vector<pair<int,int> > nextar;
  for (int i=0;i<int(ar.size());i++)
    if (elim!=ar[i].first)
      nextar.push_back(ar[i]);
  ar=nextar;
}

void quitapadre(int u,int p)
{
  vector<pair<int,int> > &ar=g[u];
  vector<pair<int,int> > nextar;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    if (v!=p) {
      nextar.push_back(ar[i]);
      quitapadre(v,u);
    }
  }
  ar=nextar;
}

void precomputa(int u)
{
  vector<pair<int,int > > &ar=g[u];
  numnodos[u]=1;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    ll t=ar[i].second;
    precomputa(v);
    numnodos[u]+=numnodos[v];
    precomputo[u]+=precomputo[v]+numnodos[v]*t;
  }
}

ll precomputodebajo(int ic)
{
  int u=c[ic];
  vector<pair<int,int> > &ar=g[u];
  ll pre=0;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    ll t=ar[i].second;
    if (v!=c[(ic+1)%nc] and v!=c[(ic-1+nc)%nc])
      pre+=precomputo[v]+numnodos[v]*t;
  }
  return pre;
}

ll numnodosdebajo(int ic)
{
  int u=c[ic];
  vector<pair<int,int> > &ar=g[u];
  ll num=0;
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    if (v!=c[(ic+1)%nc] and v!=c[(ic-1+nc)%nc])
      num+=numnodos[v];
  }
  return num;
}

ll sol[limite];

void calcula(int u,ll computoabove)
{
  //cout<<"calculamos "<<u<<endl;
  vector<pair<int,int> > &ar=g[u];
  sol[u]=computoabove+precomputo[u];
  for (int i=0;i<int(ar.size());i++) {
    int v=ar[i].first;
    ll t=ar[i].second;
    calcula(v,sol[u]-(precomputo[v]+numnodos[v]*t)+(n-numnodos[v])*t);
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin>>n;
  for (int i=0;i<n;i++) {
    int u,v,t;
    cin>>u>>v>>t;
    g[u].push_back(pair<int,int> (v,t));
    g[v].push_back(pair<int,int> (u,t));
  }
  buscaciclo(1,0,0);
  eliminaadyacente(c[0],c[1]);
  eliminaadyacente(c[1],c[0]);
  quitapadre(c[0],0);
  precomputa(c[0]);
  int ri=0;
  int lenri=0;
  ll nodri=numnodosdebajo(c[0])+1;
  ll computori=precomputodebajo(c[0]);
  ll computole=precomputo[c[0]]-computori;
  //cout<<"inicio "<<nodri<<" "<<computori<<" "<<computole<<endl;
  for (int i=0;i<nc;i++) {
    int u=c[i];
    //cout<<"ciclo "<<i<<" "<<u<<" "<<ri<<" "<<nodri<<" "<<computori<<" "<<computole<<endl;
    while (2*(lenri+d[ri])<=lenc) {
      lenri+=d[ri];
      ri=(ri+1)%nc;
      ll comp=precomputodebajo(ri);
      ll numn=numnodosdebajo(ri);
      nodri+=numn+1;
      computori+=comp+(numn+1)*lenri;
      computole-=comp+(numn+1)*(lenc-lenri);
    }
    //cout<<"despues "<<ri<<" "<<c[ri]<<" "<<nodri<<" "<<computori<<" "<<computole<<endl;
    sol[u]=computori+computole;
    vector<pair<int,int> > &ar=g[u];
    for (int j=0;j<int(ar.size());j++) {
      int v=ar[j].first;
      ll t=ar[j].second;
      if (v!=c[(i+1)%nc] and v!=c[(i-1+nc)%nc])
	calcula(v,computori+computole-(precomputo[v]+numnodos[v]*t)+(n-numnodos[v])*t);
    }
    ll comp=precomputodebajo(i);
    ll numn=numnodosdebajo(i);
    //cout<<"debajo "<<comp<<" "<<numn<<" "<<d[i]<<endl;
    computori-=comp;
    nodri-=numn+1;
    computori-=nodri*d[i];
    computole+=comp+(n-nodri)*d[i];
    lenri-=d[i];
  }
  for (int u=1;u<=n;u++) {
    if (u>1) cout<<" ";
    cout<<sol[u];
  }
  cout<<endl;
}


/*
 * File:   G.cpp
 * Author: yzq110abc
 *
 * Created on 2010年12月14日, 下午7:12
 */

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 220000;
int n;

struct Edge{
    int e, v, wh;
    Edge(){};
    Edge(int a, int b, int c){e = a; v = b; wh = c;}
};

vector <Edge> v[MAXN];

int ins[MAXN];
int oncir[MAXN];
int used[MAXN];
vector < pair <int, int> > circle;

typedef vector<Edge>::iterator VEpoint;
typedef vector< pair<int, int> >::iterator VPpoint;

int Dfs(int pos, int val){

    circle.push_back( make_pair(pos, val));
    if (ins[pos] == 1)
        return 0;
    ins[pos] = 1;
    for (vector<Edge>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
        if (used[it->wh] == 1) continue;
        used[it->wh] = 1;
        if (Dfs(it->e, it->v) == 0) return 0;
    }
    circle.erase(circle.end() - 1);
    ins[pos] = 0;
    return 1;
}

int Find_Circle(){

    memset(ins, 0, sizeof(ins));
    memset(used, 0, sizeof(used));
    Dfs(1, 0);
    reverse(circle.begin(), circle.end());
    for (VPpoint it = circle.end() - 1; ; it--){
        if (it->first == circle[0].first){
            circle.erase(it, circle.end());
            break;
        }
    }
    for (VPpoint it = circle.begin(); it != circle.end(); it++)
        oncir[it->first] = 1;
    return 0;
}

int son[MAXN];
long long ans[MAXN];

int Dfs2(int pos, long long &val, long long len){

    son[pos]++;
    val += len;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        son[pos] += Dfs2(it->e, val, len + it->v);
    }
    ins[pos] = 0;
    return son[pos];
}

int Dfs3(int pos, long long val, int tot){

    ans[pos] = val;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        Dfs3(it->e, val + (tot - son[it->e] - son[it->e]) * it->v, tot);
    }
    ins[pos] = 0;
    return 0;
}

int Cal(int pos){

    long long val = 0;
    Dfs2(pos, val, 0);
    Dfs3(pos, val, son[pos]);
    return 0;
}

int Calculate(){

    memset(ins, 0, sizeof(ins));
    for (int i = 1; i <= n; i++)
        if (oncir[i] == 1) Cal(i);
    return 0;
}

int Dfs4(int pos, long long val, int num, long long len){
    ans[pos] += val + len * num;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (oncir[it->e] == 1) continue;
        if (ins[it->e] == 1) continue;
        Dfs4(it->e, val, num, len + it->v);
    }
    ins[pos] = 0;
    return 0;
}

int Ad(int pos, long long val, int num){
    Dfs4(pos, val, num, 0);
    return 0;
}

int Gocircle(){

    long long totlen = 0;
    int tot = 0;
    for (VPpoint it = circle.begin(); it != circle.end(); it++){
        tot += it->second;
        totlen += ans[it->first];
    }
    int lp = 0, rp = 0;
    long long llen = 0, rlen = 0;
    long long lval = 0, rval = 0;
    int lsum = 0, rsum = 0;
    for (int i = 0; i < (int)circle.size(); i++){
        rval += rlen * son[ circle[i].first ];
        rsum += son[ circle[i].first ];
        if ((rlen + circle[i].second) * 2 > tot){
            rp = i;
            break;
        }
        rlen += circle[i].second;
    }

    rsum -= son[ circle[0].first ];

    lp = rp + 1;

    for (int i = (int)circle.size() - 1; i >= lp; i--){
        llen += circle[i].second;
        lval += llen * son[ circle[i].first ];
        lsum += son[ circle[i].first ];
    }
    lp %= (int)circle.size();

//    for (int i = 0; i < (int)circle.size(); i++)
//        printf("%d\n", circle[i].first);
    for (int i = 0; i < (int)circle.size(); i++){
        Ad(circle[i].first, lval + rval + totlen - ans[circle[i].first], n - son[circle[i].first]);
        lsum += son[ circle[i].first ];
        lval += lsum * circle[i].second;
        llen += circle[i].second;
        rval -= rsum * circle[i].second;
        rsum -= son[ circle[(i + 1) % (int)circle.size()].first ];
        rlen -= circle[i].second;
//        puts("lp, lsum, lval, llen");
//        cout << lp << " " << lsum << " " << lval << " " << llen << endl;
//        puts("rp, rsum, rval, rlen");
//        cout << rp << " " << rsum << " " << rval << " " << rlen << endl;
        while ((circle[rp].second + rlen) * 2 <= tot){

            rlen += circle[rp].second;
            rp++;
            if(rp == (int)circle.size()) rp = 0;
            rval += rlen * son[ circle[rp].first ];
            rsum += son[ circle[rp].first ];

            lval -= llen * son[ circle[lp].first ];
            llen -= circle[lp].second;
            lsum -= son[ circle[lp].first ];
            lp ++;
            if (lp == (int)circle.size()) lp = 0;
        }
    }
    return 0;
}

int main() {

 //   freopen("in", "r", stdin);
    memset(oncir, 0, sizeof(oncir));
    memset(ans, 0, sizeof(ans));
    memset(son, 0, sizeof(son));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        v[a].push_back(Edge(b, c, i));
        v[b].push_back(Edge(a, c, i));
    }
    Find_Circle();
    Calculate();
//    for (int i  = 1; i <= n; i++){
//        cout << son[i] << endl;
//    }
    Gocircle();
    for (int i = 1; i <= n; i++){
        cout << ans[i] << (char)(i == n ? 10 : 32);
//        printf("%I64d%c", ans[i], i == n ? 10 : 32);
    }
    return 0;
}


/*
 * File:   G.cpp
 * Author: yzq110abc
 *
 * Created on 2010年12月14日, 下午7:12
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;

const int MAXN = 220000;
int n;

struct Edge{
    int e, v, wh;
    Edge(){};
    Edge(int a, int b, int c){e = a; v = b; wh = c;}
};

vector <Edge> v[MAXN];

int ins[MAXN];
int oncir[MAXN];
int used[MAXN];
vector < pair <int, int> > circle;

typedef vector<Edge>::iterator VEpoint;
typedef vector< pair<int, int> >::iterator VPpoint;

int Dfs(int pos, int val){

    circle.push_back( make_pair(pos, val));
    if (ins[pos] == 1)
        return 0;
    ins[pos] = 1;
    for (vector<Edge>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
        if (used[it->wh] == 1) continue;
        used[it->wh] = 1;
        if (Dfs(it->e, it->v) == 0) return 0;
    }
    circle.erase(circle.end() - 1);
    ins[pos] = 0;
    return 1;
}

int Find_Circle(){

    memset(ins, 0, sizeof(ins));
    memset(used, 0, sizeof(used));
    Dfs(1, 0);
    reverse(circle.begin(), circle.end());
    for (VPpoint it = circle.end() - 1; ; it--){
        if (it->first == circle[0].first){
            circle.erase(it, circle.end());
            break;
        }
    }
    for (VPpoint it = circle.begin(); it != circle.end(); it++)
        oncir[it->first] = 1;
    return 0;
}

int son[MAXN];
__int64 ans[MAXN];

int Dfs2(int pos, __int64 &val, __int64 len){

    son[pos]++;
    val += len;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        son[pos] += Dfs2(it->e, val, len + it->v);
    }
    ins[pos] = 0;
    return son[pos];
}

int Dfs3(int pos, __int64 val, int tot){

    ans[pos] = val;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        Dfs3(it->e, val + (tot - son[it->e] - son[it->e]) * it->v, tot);
    }
    ins[pos] = 0;
    return 0;
}

int Cal(int pos){

    __int64 val = 0;
    Dfs2(pos, val, 0);
    Dfs3(pos, val, son[pos]);
    return 0;
}

int Calculate(){

    memset(ins, 0, sizeof(ins));
    for (int i = 1; i <= n; i++)
        if (oncir[i] == 1) Cal(i);
    return 0;
}

int Dfs4(int pos, __int64 val, int num, __int64 len){
    ans[pos] += val + len * num;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (oncir[it->e] == 1) continue;
        if (ins[it->e] == 1) continue;
        Dfs4(it->e, val, num, len + it->v);
    }
    ins[pos] = 0;
    return 0;
}

int Ad(int pos, __int64 val, int num){
    Dfs4(pos, val, num, 0);
    return 0;
}

int Gocircle(){

    __int64 totlen = 0;
    int tot = 0;
    for (VPpoint it = circle.begin(); it != circle.end(); it++){
        tot += it->second;
        totlen += ans[it->first];
    }
    int lp = 0, rp = 0;
    __int64 llen = 0, rlen = 0;
    __int64 lval = 0, rval = 0;
    int lsum = 0, rsum = 0;
    for (int i = 0; i < circle.size(); i++){
        rval += rlen * son[ circle[i].first ];
        rsum += son[ circle[i].first ];
        if ((rlen + circle[i].second) * 2 > tot){
            rp = i;
            break;
        }
        rlen += circle[i].second;
    }

    rsum -= son[ circle[0].first ];

    lp = rp + 1;

    for (int i = circle.size() - 1; i >= lp; i--){
        llen += circle[i].second;
        lval += llen * son[ circle[i].first ];
        lsum += son[ circle[i].first ];
    }
    lp %= circle.size();

    for (int i = 0; i < circle.size(); i++){
//        puts("lp, lsum, lval, llen");
//        printf("%d %d %d %d\n", lp, lsum, lval, llen);
//        puts("rp, rsum, rval, rlen");
//        printf("%d %d %d %d\n", rp, rsum, rval, rlen);
        Ad(circle[i].first, lval + rval + totlen - ans[circle[i].first], n - son[circle[i].first]);
        lsum += son[ circle[i].first ];
        lval += lsum * circle[i].second;
        llen += circle[i].second;
        rval -= rsum * circle[i].second;
        rsum -= son[ circle[i].first ];
        rlen -= circle[i].second;
        while ((circle[rp].second + rlen) * 2 <= tot){

            rlen += circle[rp].second;
            rp++;
            if(rp == circle.size()) rp = 0;
            rval += rlen * son[ circle[rp].first ];
            rsum += son[ circle[rp].first ];

            lval -= llen * son[ circle[lp].first ];
            llen -= circle[lp].second;
            lsum -= son[ circle[lp].first ];
            lp ++;
            if (lp == circle.size()) lp = 0;
        }
    }
    return 0;
}

int main() {

//    freopen("in", "r", stdin);
    memset(oncir, 0, sizeof(oncir));
    memset(ans, 0, sizeof(ans));
    memset(son, 0, sizeof(son));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        v[a].push_back(Edge(b, c, i));
        v[b].push_back(Edge(a, c, i));
    }
    Find_Circle();
    Calculate();
    Gocircle();
    for (int i = 1; i <= n; i++){
        printf("%I64d%c", ans[i], i == n ? 10 : 32);
    }
    return 0;
}


/*
 * File:   G.cpp
 * Author: yzq110abc
 *
 * Created on 2010年12月14日, 下午7:12
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;

const int MAXN = 220000;
int n;

struct Edge{
    int e, v, wh;
    Edge(){};
    Edge(int a, int b, int c){e = a; v = b; wh = c;}
};

vector <Edge> v[MAXN];

int ins[MAXN];
int oncir[MAXN];
int used[MAXN];
vector < pair <int, int> > circle;

typedef vector<Edge>::iterator VEpoint;
typedef vector< pair<int, int> >::iterator VPpoint;

int Dfs(int pos, int val){

    circle.push_back( make_pair(pos, val));
    if (ins[pos] == 1)
        return 0;
    ins[pos] = 1;
    for (vector<Edge>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
        if (used[it->wh] == 1) continue;
        used[it->wh] = 1;
        if (Dfs(it->e, it->v) == 0) return 0;
    }
    circle.erase(circle.end() - 1);
    ins[pos] = 0;
    return 1;
}

int Find_Circle(){

    memset(ins, 0, sizeof(ins));
    memset(used, 0, sizeof(used));
    Dfs(1, 0);
    reverse(circle.begin(), circle.end());
    for (VPpoint it = circle.end() - 1; ; it--){
        if (it->first == circle[0].first){
            circle.erase(it, circle.end());
            break;
        }
    }
    for (VPpoint it = circle.begin(); it != circle.end(); it++)
        oncir[it->first] = 1;
    return 0;
}

int son[MAXN];
int ans[MAXN];

int Dfs2(int pos, int &val, int len){

    son[pos]++;
    val += len;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        son[pos] += Dfs2(it->e, val, len + it->v);
    }
    ins[pos] = 0;
    return son[pos];
}

int Dfs3(int pos, int val, int tot){

    ans[pos] = val;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        Dfs3(it->e, val + (tot - son[it->e] - son[it->e]) * it->v, tot);
    }
    ins[pos] = 0;
    return 0;
}

int Cal(int pos){

    int val = 0;
    Dfs2(pos, val, 0);
    Dfs3(pos, val, son[pos]);
    return 0;
}

int Calculate(){

    memset(ins, 0, sizeof(ins));
    for (int i = 1; i <= n; i++)
        if (oncir[i] == 1) Cal(i);
    return 0;
}

int Dfs4(int pos, int val, int num, int len){
    ans[pos] += val + len * num;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (oncir[it->e] == 1) continue;
        if (ins[it->e] == 1) continue;
        Dfs4(it->e, val, num, len + it->v);
    }
    ins[pos] = 0;
    return 0;
}

int Ad(int pos, int val, int num){
    Dfs4(pos, val, num, 0);
    return 0;
}

int Gocircle(){

    int totlen = 0;
    int tot = 0;
    for (VPpoint it = circle.begin(); it != circle.end(); it++){
        tot += it->second;
        totlen += ans[it->first];
    }
    int lp = 0, rp = 0;
    int llen = 0, rlen = 0;
    int lval = 0, rval = 0;
    int lsum = 0, rsum = 0;
    for (int i = 0; i < circle.size(); i++){
        rval += rlen * son[ circle[i].first ];
        rsum += son[ circle[i].first ];
        if ((rlen + circle[i].second) * 2 > tot){
            rp = i;
            break;
        }
        rlen += circle[i].second;
    }

    rsum -= son[ circle[0].first ];

    lp = rp + 1;

    for (int i = circle.size() - 1; i >= lp; i--){
        llen += circle[i].second;
        lval += llen * son[ circle[i].first ];
        lsum += son[ circle[i].first ];
    }
    lp %= circle.size();

    for (int i = 0; i < circle.size(); i++){
//        puts("lp, lsum, lval, llen");
//        printf("%d %d %d %d\n", lp, lsum, lval, llen);
//        puts("rp, rsum, rval, rlen");
//        printf("%d %d %d %d\n", rp, rsum, rval, rlen);
        Ad(circle[i].first, lval + rval + totlen - ans[circle[i].first], n - son[circle[i].first]);
        lsum += son[ circle[i].first ];
        lval += lsum * circle[i].second;
        llen += circle[i].second;
        rval -= rsum * circle[i].second;
        rsum -= son[ circle[i].first ];
        rlen -= circle[i].second;
        while ((circle[rp].second + rlen) * 2 <= tot){

            rlen += circle[rp].second;
            rp++;
            if(rp == circle.size()) rp = 0;
            rval += rlen * son[ circle[rp].first ];
            rsum += son[ circle[rp].first ];

            lval -= llen * son[ circle[lp].first ];
            llen -= circle[lp].second;
            lsum -= son[ circle[lp].first ];
            lp ++;
            if (lp == circle.size()) lp = 0;
        }
    }
    return 0;
}

int main() {

//    freopen("in", "r", stdin);
    memset(oncir, 0, sizeof(oncir));
    memset(ans, 0, sizeof(ans));
    memset(son, 0, sizeof(son));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        v[a].push_back(Edge(b, c, i));
        v[b].push_back(Edge(a, c, i));
    }
    Find_Circle();
    Calculate();
    Gocircle();
    for (int i = 1; i <= n; i++){
        printf("%d%c", ans[i], i == n ? 10 : 32);
    }
    return 0;
}


/*
 * File:   G.cpp
 * Author: yzq110abc
 *
 * Created on 2010年12月14日, 下午7:12
 */

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 220000;
int n;

struct Edge{
    int e, v, wh;
    Edge(){};
    Edge(int a, int b, int c){e = a; v = b; wh = c;}
};

vector <Edge> v[MAXN];

int ins[MAXN];
int oncir[MAXN];
int used[MAXN];
vector < pair <int, int> > circle;

typedef vector<Edge>::iterator VEpoint;
typedef vector< pair<int, int> >::iterator VPpoint;

int Dfs(int pos, int val){

    circle.push_back( make_pair(pos, val));
    if (ins[pos] == 1)
        return 0;
    ins[pos] = 1;
    for (vector<Edge>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
        if (used[it->wh] == 1) continue;
        used[it->wh] = 1;
        if (Dfs(it->e, it->v) == 0) return 0;
    }
    circle.erase(circle.end() - 1);
    ins[pos] = 0;
    return 1;
}

int Find_Circle(){

    memset(ins, 0, sizeof(ins));
    memset(used, 0, sizeof(used));
    Dfs(1, 0);
    reverse(circle.begin(), circle.end());
    for (VPpoint it = circle.end() - 1; ; it--){
        if (it->first == circle[0].first){
            circle.erase(it, circle.end());
            break;
        }
    }
    for (VPpoint it = circle.begin(); it != circle.end(); it++)
        oncir[it->first] = 1;
    return 0;
}

int son[MAXN];
__int64 ans[MAXN];

int Dfs2(int pos, __int64 &val, __int64 len){

    son[pos]++;
    val += len;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        son[pos] += Dfs2(it->e, val, len + it->v);
    }
    ins[pos] = 0;
    return son[pos];
}

int Dfs3(int pos, __int64 val, int tot){

    ans[pos] = val;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        Dfs3(it->e, val + (tot - son[it->e] - son[it->e]) * it->v, tot);
    }
    ins[pos] = 0;
    return 0;
}

int Cal(int pos){

    __int64 val = 0;
    Dfs2(pos, val, 0);
    Dfs3(pos, val, son[pos]);
    return 0;
}

int Calculate(){

    memset(ins, 0, sizeof(ins));
    for (int i = 1; i <= n; i++)
        if (oncir[i] == 1) Cal(i);
    return 0;
}

int Dfs4(int pos, __int64 val, int num, __int64 len){
    ans[pos] += val + len * num;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (oncir[it->e] == 1) continue;
        if (ins[it->e] == 1) continue;
        Dfs4(it->e, val, num, len + it->v);
    }
    ins[pos] = 0;
    return 0;
}

int Ad(int pos, __int64 val, int num){
    Dfs4(pos, val, num, 0);
    return 0;
}

int Gocircle(){

    __int64 totlen = 0;
    int tot = 0;
    for (VPpoint it = circle.begin(); it != circle.end(); it++){
        tot += it->second;
        totlen += ans[it->first];
    }
    int lp = 0, rp = 0;
    __int64 llen = 0, rlen = 0;
    __int64 lval = 0, rval = 0;
    int lsum = 0, rsum = 0;
    for (int i = 0; i < (int)circle.size(); i++){
        rval += rlen * son[ circle[i].first ];
        rsum += son[ circle[i].first ];
        if ((rlen + circle[i].second) * 2 > tot){
            rp = i;
            break;
        }
        rlen += circle[i].second;
    }

    rsum -= son[ circle[0].first ];

    lp = rp + 1;

    for (int i = (int)circle.size() - 1; i >= lp; i--){
        llen += circle[i].second;
        lval += llen * son[ circle[i].first ];
        lsum += son[ circle[i].first ];
    }
    lp %= (int)circle.size();

    for (int i = 0; i < (int)circle.size(); i++){
//        puts("lp, lsum, lval, llen");
//        printf("%d %d %d %d\n", lp, lsum, lval, llen);
//        puts("rp, rsum, rval, rlen");
//        printf("%d %d %d %d\n", rp, rsum, rval, rlen);
        Ad(circle[i].first, lval + rval + totlen - ans[circle[i].first], n - son[circle[i].first]);
        lsum += son[ circle[i].first ];
        lval += lsum * circle[i].second;
        llen += circle[i].second;
        rval -= rsum * circle[i].second;
        rsum -= son[ circle[i].first ];
        rlen -= circle[i].second;
        while ((circle[rp].second + rlen) * 2 <= tot){

            rlen += circle[rp].second;
            rp++;
            if(rp == (int)circle.size()) rp = 0;
            rval += rlen * son[ circle[rp].first ];
            rsum += son[ circle[rp].first ];

            lval -= llen * son[ circle[lp].first ];
            llen -= circle[lp].second;
            lsum -= son[ circle[lp].first ];
            lp ++;
            if (lp == (int)circle.size()) lp = 0;
        }
    }
    return 0;
}

int main() {

//    freopen("in", "r", stdin);
    memset(oncir, 0, sizeof(oncir));
    memset(ans, 0, sizeof(ans));
    memset(son, 0, sizeof(son));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        v[a].push_back(Edge(b, c, i));
        v[b].push_back(Edge(a, c, i));
    }
    Find_Circle();
    Calculate();
    Gocircle();
    for (int i = 1; i <= n; i++){
        printf("%I64d%c", ans[i], i == n ? 10 : 32);
    }
    return 0;
}


/*
 * File:   G.cpp
 * Author: yzq110abc
 *
 * Created on 2010年12月14日, 下午7:12
 */

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 220000;
int n;

struct Edge{
    int e, v, wh;
    Edge(){};
    Edge(int a, int b, int c){e = a; v = b; wh = c;}
};

vector <Edge> v[MAXN];

int ins[MAXN];
int oncir[MAXN];
int used[MAXN];
vector < pair <int, int> > circle;

typedef vector<Edge>::iterator VEpoint;
typedef vector< pair<int, int> >::iterator VPpoint;

int Dfs(int pos, int val){

    circle.push_back( make_pair(pos, val));
    if (ins[pos] == 1)
        return 0;
    ins[pos] = 1;
    for (vector<Edge>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
        if (used[it->wh] == 1) continue;
        used[it->wh] = 1;
        if (Dfs(it->e, it->v) == 0) return 0;
    }
    circle.erase(circle.end() - 1);
    ins[pos] = 0;
    return 1;
}

int Find_Circle(){

    memset(ins, 0, sizeof(ins));
    memset(used, 0, sizeof(used));
    Dfs(1, 0);
    reverse(circle.begin(), circle.end());
    for (VPpoint it = circle.end() - 1; ; it--){
        if (it->first == circle[0].first){
            circle.erase(it, circle.end());
            break;
        }
    }
    for (VPpoint it = circle.begin(); it != circle.end(); it++)
        oncir[it->first] = 1;
    return 0;
}

int son[MAXN];
long long ans[MAXN];

int Dfs2(int pos, long long &val, long long len){

    son[pos]++;
    val += len;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        son[pos] += Dfs2(it->e, val, len + it->v);
    }
    ins[pos] = 0;
    return son[pos];
}

int Dfs3(int pos, long long val, int tot){

    ans[pos] = val;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (ins[it->e] == 1) continue;
        if (oncir[it->e] == 1) continue;
        Dfs3(it->e, val + (tot - son[it->e] - son[it->e]) * it->v, tot);
    }
    ins[pos] = 0;
    return 0;
}

int Cal(int pos){

    long long val = 0;
    Dfs2(pos, val, 0);
    Dfs3(pos, val, son[pos]);
    return 0;
}

int Calculate(){

    memset(ins, 0, sizeof(ins));
    for (int i = 1; i <= n; i++)
        if (oncir[i] == 1) Cal(i);
    return 0;
}

int Dfs4(int pos, long long val, int num, long long len){
    ans[pos] += val + len * num;
    ins[pos] = 1;
    for (VEpoint it = v[pos].begin(); it != v[pos].end(); it++){
        if (oncir[it->e] == 1) continue;
        if (ins[it->e] == 1) continue;
        Dfs4(it->e, val, num, len + it->v);
    }
    ins[pos] = 0;
    return 0;
}

int Ad(int pos, long long val, int num){
    Dfs4(pos, val, num, 0);
    return 0;
}

int Gocircle(){

    long long totlen = 0;
    int tot = 0;
    for (VPpoint it = circle.begin(); it != circle.end(); it++){
        tot += it->second;
        totlen += ans[it->first];
    }
    int lp = 0, rp = 0;
    long long llen = 0, rlen = 0;
    long long lval = 0, rval = 0;
    int lsum = 0, rsum = 0;
    for (int i = 0; i < (int)circle.size(); i++){
        rval += rlen * son[ circle[i].first ];
        rsum += son[ circle[i].first ];
        if ((rlen + circle[i].second) * 2 > tot){
            rp = i;
            break;
        }
        rlen += circle[i].second;
    }

    rsum -= son[ circle[0].first ];

    lp = rp + 1;

    for (int i = (int)circle.size() - 1; i >= lp; i--){
        llen += circle[i].second;
        lval += llen * son[ circle[i].first ];
        lsum += son[ circle[i].first ];
    }
    lp %= (int)circle.size();

//    for (int i = 0; i < (int)circle.size(); i++)
//        printf("%d\n", circle[i].first);
    for (int i = 0; i < (int)circle.size(); i++){
        Ad(circle[i].first, lval + rval + totlen - ans[circle[i].first], n - son[circle[i].first]);
        lsum += son[ circle[i].first ];
        lval += lsum * circle[i].second;
        llen += circle[i].second;
        rval -= rsum * circle[i].second;
        rsum -= son[ circle[(i + 1) % (int)circle.size()].first ];
        rlen -= circle[i].second;
//        puts("lp, lsum, lval, llen");
//        cout << lp << " " << lsum << " " << lval << " " << llen << endl;
//        puts("rp, rsum, rval, rlen");
//        cout << rp << " " << rsum << " " << rval << " " << rlen << endl;
        while ((circle[rp].second + rlen) * 2 <= tot){

            rlen += circle[rp].second;
            rp++;
            if(rp == (int)circle.size()) rp = 0;
            rval += rlen * son[ circle[rp].first ];
            rsum += son[ circle[rp].first ];

            lval -= llen * son[ circle[lp].first ];
            llen -= circle[lp].second;
            lsum -= son[ circle[lp].first ];
            lp ++;
            if (lp == (int)circle.size()) lp = 0;
        }
    }
    return 0;
}

int main() {

//    freopen("in", "r", stdin);
    memset(oncir, 0, sizeof(oncir));
    memset(ans, 0, sizeof(ans));
    memset(son, 0, sizeof(son));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        v[a].push_back(Edge(b, c, i));
        v[b].push_back(Edge(a, c, i));
    }
    Find_Circle();
    Calculate();
//    for (int i  = 1; i <= n; i++){
//        cout << son[i] << endl;
//    }
    Gocircle();
    for (int i = 1; i <= n; i++){
 //       cout << ans[i] << (char)(i == n ? 10 : 32);
        printf("%I64d%c", ans[i], i == n ? 10 : 32);
    }
    return 0;
}


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (c<'0' || c>'9')
	{
		if (c=='-')	f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9')
	{
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*f;
}
const int MAXN=200010;
struct edge
{
	int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].val=w;
	head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
int from[MAXN];
vector <int> cir;
void dfs_cl(int x,int fa)
{
	visit[x]=1;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (v==fa) continue;
		if (visit[v])
		{
			int tmp=x;
			while (tmp!=v)
			{
				cir.push_back(tmp);
				in_circle[tmp]=1;
				tmp=from[tmp];
			}
			Found=1;
			in_circle[v]=1;
			cir.push_back(v);
			continue;
		}
		from[v]=x;
		dfs_cl(v,x);
		if (Found) return ;
	}
}
int sz[MAXN],fa[MAXN],root[MAXN];
ll f[MAXN],dis[MAXN];
void dfs(int x,int rt)
{
	sz[x]++;
	root[x]=rt;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (in_circle[v] || fa[v]) continue;
		fa[v]=x;
		dis[v]=dis[x]+e[i].val;
		dfs(v,rt);
		sz[x]+=sz[v];
		f[x]+=f[v]+sz[v]*e[i].val;
	}
}
ll ans[MAXN];
int n;
void dfs2(int x,int dist,int size)
{
	visit[x]=1;
	ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (visit[v] || in_circle[v]) continue;
		int szv=size+sz[x]-sz[v];
		int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
		dfs2(v,disv,szv);
	}
}
ll cst[MAXN*3],Ans[MAXN];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	for (int i=1;i<=n;i++)
		if (!Found)
			dfs_cl(i,-1);
	int num=cir.size();
	for (int i=0;i<num;i++)
		dfs(cir[i],cir[i]);
	memset(visit,0,sizeof(visit));
	for (int i=0;i<num;i++)
		dfs2(cir[i],0,0),cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
		cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
	{
		for (int j=head[cir[i+1]];j;j=e[j].next)
		{
			int v=e[j].to;
			if (v==cir[i])
				cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
		}
	}
	partial_sum(cst,cst+3*num,cst);
	ll anss=0;
	for (int i=1;i<num;i++)
		anss+=(cst[num]-cst[i])*sz[cir[i]];
	int j=num+1,ncnt=sz[cir[num]]
	ll tot=cst[num];
	ll all=0;
	for (int i=0;i<num;i++)
		all+=ans[cir[i]];
	for (int i=num;i<2*num;i++)
	{
		while (cst[j]-cst[i]<=tot/2)
		{
			ncnt+=sz[cir[j]];
			anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
			j++;
		}
		Ans[cir[i]]=anss;
		ncnt-=sz[cir[i]];
		anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
	}
	for (int i=1;i<=n;i++)
		printf("%I64d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
	return 0;
}
/*
6
1 5 2
1 2 8
2 3 4
1 4 1
2 6 3
3 4 2

4
1 2 3
2 4 4
2 3 5
3 4 8
*/


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (c<'0' || c>'9')
	{
		if (c=='-')	f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9')
	{
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*f;
}
const int MAXN=200010;
struct edge
{
	int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].val=w;
	head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
int from[MAXN];
vector <int> cir;
void dfs_cl(int x,int fa)
{
	visit[x]=1;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (v==fa) continue;
		if (visit[v])
		{
			int tmp=x;
			while (tmp!=v)
			{
				cir.push_back(tmp);
				in_circle[tmp]=1;
				tmp=from[tmp];
			}
			Found=1;
			in_circle[v]=1;
			cir.push_back(v);
			continue;
		}
		from[v]=x;
		dfs_cl(v,x);
		if (Found) return ;
	}
}
int sz[MAXN],fa[MAXN],root[MAXN];
ll f[MAXN],dis[MAXN];
void dfs(int x,int rt)
{
	sz[x]++;
	root[x]=rt;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (in_circle[v] || fa[v]) continue;
		fa[v]=x;
		dis[v]=dis[x]+e[i].val;
		dfs(v,rt);
		sz[x]+=sz[v];
		f[x]+=f[v]+sz[v]*e[i].val;
	}
}
ll ans[MAXN];
int n;
void dfs2(int x,int dist,int size)
{
	visit[x]=1;
	ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (visit[v] || in_circle[v]) continue;
		int szv=size+sz[x]-sz[v];
		int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
		dfs2(v,disv,szv);
	}
}
ll cst[MAXN*3],Ans[MAXN];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	for (int i=1;i<=n;i++)
		if (!Found)
			dfs_cl(i,-1);
	int num=cir.size();
	for (int i=0;i<num;i++)
		dfs(cir[i],cir[i]);
	memset(visit,0,sizeof(visit));
	for (int i=0;i<num;i++)
		dfs2(cir[i],0,0),cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
		cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
	{
		for (int j=head[cir[i+1]];j;j=e[j].next)
		{
			int v=e[j].to;
			if (v==cir[i])
				cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
		}
	}
	//partial_sum(cst,cst+3*num,cst);
	for (int i=1;i<3*num;i++)
		cst[i]+=cst[i-1];
	ll anss=0;
	for (int i=1;i<num;i++)
		anss+=(cst[num]-cst[i])*sz[cir[i]];
	int j=num+1,ncnt=sz[cir[num]];
	ll tot=cst[num];
	ll all=0;
	for (int i=0;i<num;i++)
		all+=ans[cir[i]];
	for (int i=num;i<2*num;i++)
	{
		while (cst[j]-cst[i]<=tot/2)
		{
			ncnt+=sz[cir[j]];
			anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
			j++;
		}
		Ans[cir[i]]=anss;
		ncnt-=sz[cir[i]];
		anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
	}
	for (int i=1;i<=n;i++)
		printf("%I64d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
	return 0;
}
/*
6
1 5 2
1 2 8
2 3 4
1 4 1
2 6 3
3 4 2

4
1 2 3
2 4 4
2 3 5
3 4 8
*/


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (c<'0' || c>'9')
	{
		if (c=='-')	f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9')
	{
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*f;
}
const int MAXN=200010;
struct edge
{
	int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].val=w;
	head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
int from[MAXN];
vector <int> cir;
void dfs_cl(int x,int fa)
{
	visit[x]=1;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (v==fa) continue;
		if (visit[v])
		{
			int tmp=x;
			while (tmp!=v)
			{
				cir.push_back(tmp);
				in_circle[tmp]=1;
				tmp=from[tmp];
			}
			Found=1;
			in_circle[v]=1;
			cir.push_back(v);
			continue;
		}
		from[v]=x;
		dfs_cl(v,x);
		if (Found) return ;
	}
}
int dis[MAXN],sz[MAXN],fa[MAXN],root[MAXN];
int f[MAXN];
void dfs(int x,int rt)
{
	sz[x]++;
	root[x]=rt;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (in_circle[v] || fa[v]) continue;
		fa[v]=x;
		dis[v]=dis[x]+e[i].val;
		dfs(v,rt);
		sz[x]+=sz[v];
		f[x]+=f[v]+sz[v]*e[i].val;
	}
}
int ans[MAXN],n;
void dfs2(int x,int dist,int size)
{
	visit[x]=1;
	ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (visit[v] || in_circle[v]) continue;
		int szv=size+sz[x]-sz[v];
		int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
		dfs2(v,disv,szv);
	}
}
int cst[MAXN*3];
int Ans[MAXN];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	for (int i=1;i<=n;i++)
		if (!Found)
			dfs_cl(i,-1);
	int num=cir.size();
	for (int i=0;i<num;i++)
		dfs(cir[i],cir[i]);
	memset(visit,0,sizeof(visit));
	for (int i=0;i<num;i++)
		dfs2(cir[i],0,0),cir.push_back(cir[i]);
	for (int i=1;i<=n;i++)
		cout<<ans[i]<<' ';
	cout<<endl;
	for (int i=0;i<num;i++)
		cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
	{
		for (int j=head[cir[i+1]];j;j=e[j].next)
		{
			int v=e[j].to;
			if (v==cir[i])
				cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
		}
	}
	partial_sum(cst,cst+3*num,cst);
	int anss=0;
	for (int i=1;i<num;i++)
		anss+=(cst[num]-cst[i])*sz[cir[i]];
	int j=num+1,tot=cst[num],ncnt=sz[cir[num]];
	int all=0;
	for (int i=0;i<num;i++)
		all+=ans[cir[i]];
	for (int i=num;i<2*num;i++)
	{
		while (cst[j]-cst[i]<=tot/2)
		{
			ncnt+=sz[cir[j]];
			anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
			j++;
		}
		Ans[cir[i]]=anss;
		ncnt-=sz[cir[i]];
		anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
	}
	for (int i=1;i<=n;i++)
		printf("%d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
	return 0;
}
/*
6
1 5 2
1 2 8
2 3 4
1 4 1
2 6 3
3 4 2

4
1 2 3
2 4 4
2 3 5
3 4 8
*/


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (c<'0' || c>'9')
	{
		if (c=='-')	f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9')
	{
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*f;
}
const int MAXN=200010;
struct edge
{
	int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].val=w;
	head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
int from[MAXN];
vector <int> cir;
void dfs_cl(int x,int fa)
{
	visit[x]=1;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (v==fa) continue;
		if (visit[v])
		{
			int tmp=x;
			while (tmp!=v)
			{
				cir.push_back(tmp);
				in_circle[tmp]=1;
				tmp=from[tmp];
			}
			Found=1;
			in_circle[v]=1;
			cir.push_back(v);
			continue;
		}
		from[v]=x;
		dfs_cl(v,x);
		if (Found) return ;
	}
}
int dis[MAXN],sz[MAXN],fa[MAXN],root[MAXN];
int f[MAXN];
void dfs(int x,int rt)
{
	sz[x]++;
	root[x]=rt;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (in_circle[v] || fa[v]) continue;
		fa[v]=x;
		dis[v]=dis[x]+e[i].val;
		dfs(v,rt);
		sz[x]+=sz[v];
		f[x]+=f[v]+sz[v]*e[i].val;
	}
}
int ans[MAXN],n;
void dfs2(int x,int dist,int size)
{
	visit[x]=1;
	ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (visit[v] || in_circle[v]) continue;
		int szv=size+sz[x]-sz[v];
		int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
		dfs2(v,disv,szv);
	}
}
int cst[MAXN*3];
int Ans[MAXN];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	for (int i=1;i<=n;i++)
		if (!Found)
			dfs_cl(i,-1);
	int num=cir.size();
	for (int i=0;i<num;i++)
		dfs(cir[i],cir[i]);
	memset(visit,0,sizeof(visit));
	for (int i=0;i<num;i++)
		dfs2(cir[i],0,0),cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
		cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
	{
		for (int j=head[cir[i+1]];j;j=e[j].next)
		{
			int v=e[j].to;
			if (v==cir[i])
				cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
		}
	}
	partial_sum(cst,cst+3*num,cst);
	int anss=0;
	for (int i=1;i<num;i++)
		anss+=(cst[num]-cst[i])*sz[cir[i]];
	int j=num+1,tot=cst[num],ncnt=sz[cir[num]];
	int all=0;
	for (int i=0;i<num;i++)
		all+=ans[cir[i]];
	for (int i=num;i<2*num;i++)
	{
		while (cst[j]-cst[i]<=tot/2)
		{
			ncnt+=sz[cir[j]];
			anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
			j++;
		}
		Ans[cir[i]]=anss;
		ncnt-=sz[cir[i]];
		anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
	}
	for (int i=1;i<=n;i++)
		printf("%d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
	return 0;
}
/*
6
1 5 2
1 2 8
2 3 4
1 4 1
2 6 3
3 4 2

4
1 2 3
2 4 4
2 3 5
3 4 8
*/


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (c<'0' || c>'9')
	{
		if (c=='-')	f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9')
	{
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*f;
}
const int MAXN=200010;
struct edge
{
	int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].val=w;
	head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
vector <int> cir;
void dfs_cl(int x,int faid)
{
	visit[x]=1;
	cir.push_back(x);
	in_circle[x]=1;
	for (int i=head[x];i;i=e[i].next)
	{
		if (i==(faid^1)) continue;
		int v=e[i].to;
		if (visit[v])
		{
			Found=1;
			return ;
		}
		dfs_cl(v,i);
		if (Found) return ;
	}
	cir.pop_back();
	in_circle[x]=0;
}
int dis[MAXN],sz[MAXN],fa[MAXN],root[MAXN];
int f[MAXN];
void dfs(int x,int rt)
{
	sz[x]++;
	root[x]=rt;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (in_circle[v] || fa[v]) continue;
		fa[v]=x;
		dis[v]=dis[x]+e[i].val;
		dfs(v,rt);
		sz[x]+=sz[v];
		f[x]+=f[v]+sz[v]*e[i].val;
	}
}
int ans[MAXN],n;
void dfs2(int x,int dist,int size)
{
	visit[x]=1;
	ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (visit[v] || in_circle[v]) continue;
		int szv=size+sz[x]-sz[v];
		int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
		dfs2(v,szv,disv);
	}
}
int cst[MAXN*3];
int Ans[MAXN];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	for (int i=1;i<=n;i++)
		if (!Found)
			dfs_cl(i,-1);
	int num=cir.size();
	for (int i=0;i<num;i++)
		dfs(cir[i],cir[i]);
	memset(visit,0,sizeof(visit));
	for (int i=0;i<num;i++)
		dfs2(cir[i],0,0),cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
		cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
	{
		for (int j=head[cir[i+1]];j;j=e[j].next)
		{
			int v=e[j].to;
			if (v==cir[i])
				cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
		}
	}
	partial_sum(cst,cst+3*num,cst);
	int anss=0;
	for (int i=1;i<num;i++)
		anss+=(cst[num]-cst[i])*sz[cir[i]];
	int j=num+1,tot=cst[num],ncnt=sz[cir[num]];
	int all=0;
	for (int i=0;i<num;i++)
		all+=ans[cir[i]];
	for (int i=num;i<2*num;i++)
	{
		while (cst[j]-cst[i]<=tot/2)
		{
			ncnt+=sz[cir[j]];
			anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
			j++;
		}
		Ans[cir[i]]=anss;
		ncnt-=sz[cir[i]];
		anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
	}
	for (int i=1;i<=n;i++)
		printf("%d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
	return 0;
}


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (c<'0' || c>'9')
	{
		if (c=='-')	f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9')
	{
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*f;
}
const int MAXN=200010;
struct edge
{
	int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].val=w;
	head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
vector <int> cir;
void dfs_cl(int x,int faid)
{
	visit[x]=1;
	cir.push_back(x);
	in_circle[x]=1;
	for (int i=head[x];i;i=e[i].next)
	{
		if (i==(faid^1)) continue;
		int v=e[i].to;
		if (visit[v])
		{
			Found=1;
			return ;
		}
		dfs_cl(v,i);
		if (Found) return ;
	}
	cir.pop_back();
	in_circle[x]=0;
}
int dis[MAXN],sz[MAXN],fa[MAXN],root[MAXN];
int f[MAXN];
void dfs(int x,int rt)
{
	sz[x]++;
	root[x]=rt;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (in_circle[v] || fa[v]) continue;
		fa[v]=x;
		dis[v]=dis[x]+e[i].val;
		dfs(v,rt);
		sz[x]+=sz[v];
		f[x]+=f[v]+sz[v]*e[i].val;
	}
}
int ans[MAXN],n;
void dfs2(int x,int dist,int size)
{
	visit[x]=1;
	ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		if (visit[v] || in_circle[v]) continue;
		int szv=size+sz[x]-sz[v];
		int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
		dfs2(v,disv,szv);
	}
}
int cst[MAXN*3];
int Ans[MAXN];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	for (int i=1;i<=n;i++)
		if (!Found)
			dfs_cl(i,-1);
	int num=cir.size();
	for (int i=0;i<num;i++)
		dfs(cir[i],cir[i]);
	memset(visit,0,sizeof(visit));
	for (int i=0;i<num;i++)
		dfs2(cir[i],0,0),cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
		cir.push_back(cir[i]);
	for (int i=0;i<num;i++)
	{
		for (int j=head[cir[i+1]];j;j=e[j].next)
		{
			int v=e[j].to;
			if (v==cir[i])
				cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
		}
	}
	partial_sum(cst,cst+3*num,cst);
	int anss=0;
	for (int i=1;i<num;i++)
		anss+=(cst[num]-cst[i])*sz[cir[i]];
	int j=num+1,tot=cst[num],ncnt=sz[cir[num]];
	int all=0;
	for (int i=0;i<num;i++)
		all+=ans[cir[i]];
	for (int i=num;i<2*num;i++)
	{
		while (cst[j]-cst[i]<=tot/2)
		{
			ncnt+=sz[cir[j]];
			anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
			j++;
		}
		Ans[cir[i]]=anss;
		ncnt-=sz[cir[i]];
		anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
	}
	for (int i=1;i<=n;i++)
		printf("%d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
	return 0;
}
/*
6
1 5 2
1 2 8
2 3 4
1 4 1
2 6 3
3 4 2
*/


#include <bits/stdc++.h>
#include <chrono>
typedef long long ll;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned long long ull;
typedef double db;
typedef unsigned char uchar;
#define vec vector
using namespace std;
using namespace std::chrono;
#define var auto
#define rep(i,a,b) for(decltype(a) i=a, lim__lim=(b); i<=lim__lim; i++)
#define repr(i,a,b) for(decltype(b) i=b,lim__lim=(a); i>=lim__lim; i--)
#define fst first
#define sec second
inline int getint()
{
   char c = getchar(); int x = 0, mi = 1;
   while(c != EOF && c != '-' && !isdigit(c)) c = getchar();
   if(c == '-') { mi = -1; c = getchar(); }
   while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
   return x * mi;
}
inline ll getll() { ll x; scanf("%lld",&x); return x; }
inline db getdb() { static double x; scanf("%lf",&x); return x; }
void pr(const char* s = " ") { printf("%s", s); }
void pr(char const& x) { putchar(x); }
void pr(bool const& x) { printf("%d", x); }
void pr(int const& x) { printf("%d", x); }
void pr(ll const& x) { printf("%lld", x); }
void pr(double const& x) { printf("%.2f", x); }
void pr(long double const& x) { printf("%.2f", (double)x); }
#define sp pr(' ')
#define ln pr('\n')
template<typename T, typename R> void pr(pair<T, R> x) { pr("("); pr(x.fst); pr(","); pr(x.sec); pr(")"); }
template<typename T> void pr(T const t[], int l, int r, const char* s = " ", const char* h = "", const char* c = "\n")
{ printf("%s", h); rep(i, l, r) pr(t[i]), pr(s); printf("%s", c); }
#define prarr(arr,l,r) pr(arr, l, r, " ", #arr ": ")
template<typename T> void updmax(T& a, T const& b, function<void(void)> f = [](){}) { if(a < b) { a = b; f(); } }
template<typename T> void updmin(T& a, T const& b, function<void(void)> f = [](){}) { if(a > b) { a = b; f(); } }
template<typename T> int descr(T* from, T* to){ sort(from, to); return (int)(unique(from, to) - from); }
template<typename T> int lBound(T* from, T* to, T const& val) { return (int)(lower_bound(from, to, val) - from); }
template<typename T> int uBound(T* from, T* to, T const& val) { return (int)(upper_bound(from, to, val) - from); }
time_point<system_clock> timing;
void TBegin() { timing = system_clock::now(); }
double TEnd(const char* c = "time: ") {
    var end = system_clock::now(); var d = duration_cast<microseconds>(end - timing);
    var dt = double(d.count()) * microseconds::period::num / microseconds::period::den;
    printf("%s%.10f", c, dt); return dt; }
void TTick(const char* c = "time:") { TEnd(c); TBegin(); }
#define gcd __gcd
template<typename T> void fill(T a[], uchar v, int l, int r) { memset(&a[l], v, sizeof(a[0]) * (r - l + 1)); }
// ======================================================================================

const int mxn = 1e4 + 50;
int d[mxn];
int ans[mxn]; int cnt;

int main()
{
    int n = getint();
    int m = getint();
    int k = getint();

    rep(i, 1, k) ans[cnt++] = 1;
    ans[cnt++] = n;
    if(k != 1) rep(i, 2, k) ans[cnt++] = n + 1;
    else if(n == m) ans[cnt++] = n + m - 1;
    else ans[cnt++] = n + m;
    pr(cnt), ln;
    pr(ans, 0, cnt-1);

    return 0;
}

/*
1 1 1 0 0 0 1 1 1
0 0 1 1 1 0 0 0 1
0 0 0 0 1 1 1 0 0

1 1 1 1 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0

1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0

*/


#include <bits/stdc++.h>
#include <chrono>
typedef long long ll;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned long long ull;
typedef double db;
typedef unsigned char uchar;
#define vec vector
using namespace std;
using namespace std::chrono;
#define var auto
#define rep(i,a,b) for(decltype(a) i=a, lim__lim=(b); i<=lim__lim; i++)
#define repr(i,a,b) for(decltype(b) i=b,lim__lim=(a); i>=lim__lim; i--)
#define fst first
#define sec second
inline int getint()
{
   char c = getchar(); int x = 0, mi = 1;
   while(c != EOF && c != '-' && !isdigit(c)) c = getchar();
   if(c == '-') { mi = -1; c = getchar(); }
   while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
   return x * mi;
}
inline ll getll() { ll x; scanf("%lld",&x); return x; }
inline db getdb() { static double x; scanf("%lf",&x); return x; }
void pr(const char* s = " ") { printf("%s", s); }
void pr(char const& x) { putchar(x); }
void pr(bool const& x) { printf("%d", x); }
void pr(int const& x) { printf("%d", x); }
void pr(ll const& x) { printf("%lld", x); }
void pr(double const& x) { printf("%.2f", x); }
void pr(long double const& x) { printf("%.2f", (double)x); }
#define sp pr(' ')
#define ln pr('\n')
template<typename T, typename R> void pr(pair<T, R> x) { pr("("); pr(x.fst); pr(","); pr(x.sec); pr(")"); }
template<typename T> void pr(T const t[], int l, int r, const char* s = " ", const char* h = "", const char* c = "\n")
{ printf("%s", h); rep(i, l, r) pr(t[i]), pr(s); printf("%s", c); }
#define prarr(arr,l,r) pr(arr, l, r, " ", #arr ": ")
template<typename T> void updmax(T& a, T const& b, function<void(void)> f = [](){}) { if(a < b) { a = b; f(); } }
template<typename T> void updmin(T& a, T const& b, function<void(void)> f = [](){}) { if(a > b) { a = b; f(); } }
template<typename T> int descr(T* from, T* to){ sort(from, to); return (int)(unique(from, to) - from); }
template<typename T> int lBound(T* from, T* to, T const& val) { return (int)(lower_bound(from, to, val) - from); }
template<typename T> int uBound(T* from, T* to, T const& val) { return (int)(upper_bound(from, to, val) - from); }
time_point<system_clock> timing;
void TBegin() { timing = system_clock::now(); }
double TEnd(const char* c = "time: ") {
    var end = system_clock::now(); var d = duration_cast<microseconds>(end - timing);
    var dt = double(d.count()) * microseconds::period::num / microseconds::period::den;
    printf("%s%.10f", c, dt); return dt; }
void TTick(const char* c = "time:") { TEnd(c); TBegin(); }
#define gcd __gcd
template<typename T> void fill(T a[], uchar v, int l, int r) { memset(&a[l], v, sizeof(a[0]) * (r - l + 1)); }
// ======================================================================================

const int mxn = 1e4 + 50;
int d[mxn];
int ans[mxn]; int c;

int main()
{
    int n = getint();
    int m = getint();
    int k = getint();

    if(k == 1)
    {
        ans[c++] = 1;
        ans[c++] = n;
        if(m + 1 >= n && m != 1) ans[c++] = n + m - 1;
        if(n - 2 <= 0) ans[c++] = n + m;
    }
    else
    {
        rep(i, 1, k) ans[c++] = 1;
        ans[c++] = n;
        rep(i, 2, k) ans[c++] = n + 1;
        if(n - 2 <= 0) ans[c++] = n + m;
    }

    pr(c), ln;
    pr(ans, 0, c-1);

    return 0;
}

/*

1 1 0 1 1
0 1 1 0 0

1 1 1 0 0 0 1 1 1
0 0 1 1 1 0 0 0 0

1 1 0 0 1 1
1 1 0 0 1 1
0 1 1 0 0 1
0 0 1 1 0 0
0 0 0 1 1 0

1 1 1 1 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0

1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0

*/


#include <bits/stdc++.h>
#include <chrono>
typedef long long ll;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned long long ull;
typedef double db;
typedef unsigned char uchar;
#define vec vector
using namespace std;
using namespace std::chrono;
#define var auto
#define rep(i,a,b) for(decltype(a) i=a, lim__lim=(b); i<=lim__lim; i++)
#define repr(i,a,b) for(decltype(b) i=b,lim__lim=(a); i>=lim__lim; i--)
#define fst first
#define sec second
inline int getint()
{
   char c = getchar(); int x = 0, mi = 1;
   while(c != EOF && c != '-' && !isdigit(c)) c = getchar();
   if(c == '-') { mi = -1; c = getchar(); }
   while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
   return x * mi;
}
inline ll getll() { ll x; scanf("%lld",&x); return x; }
inline db getdb() { static double x; scanf("%lf",&x); return x; }
void pr(const char* s = " ") { printf("%s", s); }
void pr(char const& x) { putchar(x); }
void pr(bool const& x) { printf("%d", x); }
void pr(int const& x) { printf("%d", x); }
void pr(ll const& x) { printf("%lld", x); }
void pr(double const& x) { printf("%.2f", x); }
void pr(long double const& x) { printf("%.2f", (double)x); }
#define sp pr(' ')
#define ln pr('\n')
template<typename T, typename R> void pr(pair<T, R> x) { pr("("); pr(x.fst); pr(","); pr(x.sec); pr(")"); }
template<typename T> void pr(T const t[], int l, int r, const char* s = " ", const char* h = "", const char* c = "\n")
{ printf("%s", h); rep(i, l, r) pr(t[i]), pr(s); printf("%s", c); }
#define prarr(arr,l,r) pr(arr, l, r, " ", #arr ": ")
template<typename T> void updmax(T& a, T const& b, function<void(void)> f = [](){}) { if(a < b) { a = b; f(); } }
template<typename T> void updmin(T& a, T const& b, function<void(void)> f = [](){}) { if(a > b) { a = b; f(); } }
template<typename T> int descr(T* from, T* to){ sort(from, to); return (int)(unique(from, to) - from); }
template<typename T> int lBound(T* from, T* to, T const& val) { return (int)(lower_bound(from, to, val) - from); }
template<typename T> int uBound(T* from, T* to, T const& val) { return (int)(upper_bound(from, to, val) - from); }
time_point<system_clock> timing;
void TBegin() { timing = system_clock::now(); }
double TEnd(const char* c = "time: ") {
    var end = system_clock::now(); var d = duration_cast<microseconds>(end - timing);
    var dt = double(d.count()) * microseconds::period::num / microseconds::period::den;
    printf("%s%.10f", c, dt); return dt; }
void TTick(const char* c = "time:") { TEnd(c); TBegin(); }
#define gcd __gcd
template<typename T> void fill(T a[], uchar v, int l, int r) { memset(&a[l], v, sizeof(a[0]) * (r - l + 1)); }
// ======================================================================================

const int mxn = 1e4 + 50;
int d[mxn];
int ans[mxn]; int cnt;

int main()
{
    int n = getint();
    int m = getint();
    int k = getint();

    rep(i, 1, k) ans[cnt++] = 1;
    ans[cnt++] = n;
    rep(i, 2, max(2, k)) ans[cnt++] = n + 1;
    pr(cnt), ln;
    pr(ans, 0, cnt-1);

    return 0;
}

/*
1 1 1 1 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0

1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0

*/


#include <bits/stdc++.h>
#include <chrono>
typedef long long ll;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned long long ull;
typedef double db;
typedef unsigned char uchar;
#define vec vector
using namespace std;
using namespace std::chrono;
#define var auto
#define rep(i,a,b) for(decltype(a) i=a, lim__lim=(b); i<=lim__lim; i++)
#define repr(i,a,b) for(decltype(b) i=b,lim__lim=(a); i>=lim__lim; i--)
#define fst first
#define sec second
inline int getint()
{
   char c = getchar(); int x = 0, mi = 1;
   while(c != EOF && c != '-' && !isdigit(c)) c = getchar();
   if(c == '-') { mi = -1; c = getchar(); }
   while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
   return x * mi;
}
inline ll getll() { ll x; scanf("%lld",&x); return x; }
inline db getdb() { static double x; scanf("%lf",&x); return x; }
void pr(const char* s = " ") { printf("%s", s); }
void pr(char const& x) { putchar(x); }
void pr(bool const& x) { printf("%d", x); }
void pr(int const& x) { printf("%d", x); }
void pr(ll const& x) { printf("%lld", x); }
void pr(double const& x) { printf("%.2f", x); }
void pr(long double const& x) { printf("%.2f", (double)x); }
#define sp pr(' ')
#define ln pr('\n')
template<typename T, typename R> void pr(pair<T, R> x) { pr("("); pr(x.fst); pr(","); pr(x.sec); pr(")"); }
template<typename T> void pr(T const t[], int l, int r, const char* s = " ", const char* h = "", const char* c = "\n")
{ printf("%s", h); rep(i, l, r) pr(t[i]), pr(s); printf("%s", c); }
#define prarr(arr,l,r) pr(arr, l, r, " ", #arr ": ")
template<typename T> void updmax(T& a, T const& b, function<void(void)> f = [](){}) { if(a < b) { a = b; f(); } }
template<typename T> void updmin(T& a, T const& b, function<void(void)> f = [](){}) { if(a > b) { a = b; f(); } }
template<typename T> int descr(T* from, T* to){ sort(from, to); return (int)(unique(from, to) - from); }
template<typename T> int lBound(T* from, T* to, T const& val) { return (int)(lower_bound(from, to, val) - from); }
template<typename T> int uBound(T* from, T* to, T const& val) { return (int)(upper_bound(from, to, val) - from); }
time_point<system_clock> timing;
void TBegin() { timing = system_clock::now(); }
double TEnd(const char* c = "time: ") {
    var end = system_clock::now(); var d = duration_cast<microseconds>(end - timing);
    var dt = double(d.count()) * microseconds::period::num / microseconds::period::den;
    printf("%s%.10f", c, dt); return dt; }
void TTick(const char* c = "time:") { TEnd(c); TBegin(); }
#define gcd __gcd
template<typename T> void fill(T a[], uchar v, int l, int r) { memset(&a[l], v, sizeof(a[0]) * (r - l + 1)); }
// ======================================================================================

const int mxn = 1e4 + 50;
int d[mxn];
int ans[mxn]; int cnt;

int main()
{
    int n = getint();
    int m = getint();
    int k = getint();

    rep(i, 1, k) ans[cnt++] = 1;
    ans[cnt++] = n;
    rep(i, 2, k) ans[cnt++] = n + 1;
    if(n == m || (n == m + 1 && k == 1)) ans[cnt++] = n + m - 1;

    pr(cnt), ln;
    pr(ans, 0, cnt-1);

    return 0;
}

/*
1 1 1 1 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0

1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 0 0 0 0

*/


#include<bits/stdc++.h>
using namespace std;

#define boost ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

#define li long long int
#define mod 1000000007
int ss[4000], tt[4000];

int main() {
	boost
	int m, n, kk;
	cin >> m >> n >> kk;
	memset(tt, 0, sizeof(tt));
	memset(ss, 0, sizeof(ss));
	int mn = m + n;
	int p = mn;
	int i, j, k = 0;
	while (tt[1] < kk) {
		ss[k++] = 1, tt[1]++;
	}
	for (j = 2; j <= p + 1; j++)
		if ((j - 1) % mn < m)
			tt[j] = tt[1];
	for (i = 2; i <= p; i++) {
		int ff = 0;
		if (tt[i] == 0) {
			i--;
			ff++;
			tt[i]++;
			ss[k++] = i;
		}
		while (tt[i] < kk) {
			ss[k++] = i, tt[i]++;
			ff++;
		}
		if (ff)
			for (j = i + 1; j <= p + 1; j++)
				if ((j - i) % mn < m)
					tt[j] += ff;
	}
	if (tt[p + 1] == tt[1])
		ss[k++] = p;
	cout << k << "\n" << ss[0] << " ";

	for (i = 1; i < k; i++)
		cout << ss[i] << " ";
	cout << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);
		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		cout << m << " ";

		for(int i=1;i<=(k-1);i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);

		if(k == 1 && m == n-1)
			num++;

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		if(k == 1 && m == n-1)
			k++;

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		if(n == 2)
			num++;

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		cout << m << " ";

		if(n == 2)
			k++;

		for(int i=1;i<=(k-1);i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);
		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		for(int i=1;i<=k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);

		if(k == 1)
			num++;

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		if(k == 1)
			k++;

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		cout << m << " ";

		for(int i=1;i<=(k-1);i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);
		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		for(int i=1;i<=k;i++)
			cout << (m) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);

		if(k == 1 && m == n-1)
			num++;

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		if(k == 1 && m == n-1)
			k++;

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		if(n == 2 && k == 1)
			num++;

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		cout << m << " ";

		if(n == 2 && k == 1)
			k++;

		for(int i=1;i<=(k-1);i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

	int n,m,k;

	cin >> n >> m >> k;

	if(m < n){

		int num = (2*k);

		if(k == 1 && m == n-1)
			num++;

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		if(k == 1 && m == n-1)
			k++;

		for(int i=1;i<k;i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";
	}
	else{

		int num = (2*k + 1);

		cout << num << endl;

		for(int i=1;i<=k;i++)
			cout << "1 ";

		cout << m << " ";

		for(int i=1;i<=(k-1);i++)
			cout << (m+1) << " ";

		cout << (m+2) << " ";

	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
typedef long long ll;
#define MAX 1005
using namespace std;
#define INF 2147483647
#define inf 1<<30
#define MOD 1000000007
#define EPS 1e-9
#define G 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
bool debug = false;
int n,m, k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};

vector<int> ans;
int mm[MAX];
int main(){
    scanf("%d%d%d",&n,&m,&k);
    int sk=0,l=1;
    for(int day=1;day<1+m+n;day++) {
        sk -= mm[day];
        while(sk<k) {
            ans.push_back(day); sk++; l=day+n-1; mm[day+n]++;
        }
        if (day==l) {
            ans.push_back(day); sk++; l=day+n-1; mm[day+n]++;
        }
    }
    printf("%d\n", ans.size());
    for(int c:ans) printf("%d ", c);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
typedef long long ll;
#define MAX 1005
using namespace std;
#define INF 2147483647
#define inf 1<<30
#define MOD 1000000007
#define EPS 1e-9
#define G 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
bool debug = false;
int n,m, k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};

vector<int> ans;
int mm[MAX*2];
int main(){
    scanf("%d%d%d",&n,&m,&k);
    int sk=0,l=1;
    for(int day=1;day<1+m+n;day++) {
        sk -= mm[day];
        while(sk<k) {
            ans.push_back(day); sk++; l=day+n-1; mm[day+n]++;
        }
        if (day==l) {
            ans.push_back(day); sk++; l=day+n-1; mm[day+n]++;
        }
    }
    printf("%d\n", ans.size());
    for(int c:ans) printf("%d ", c);
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
#define carpe_diem ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define endl '\n'
#define S second
#define F first
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define M 1000000007

int main()
{
    carpe_diem
    int n,m,k; cin>>n>>m>>k;
    vector<int> ans;
    for(int i=0; i<k; i++) ans.pb(1);
    int t=n;
    while(t<=n+m)
    {
        ans.pb(t);
        if(t+1<=n+m)
        {
            for(int i=0; i<k-1; i++) ans.pb(t+1);
        }
        t+=n;
        if(k==1) t--;
        // cout<<t<<endl;
    }
    // if(t==n+m+1 && k==1){ans.pb(n+m);}
    cout<<ans.size()<<endl;
    for(int i=0; i<ans.size(); i++) cout<<ans[i]<<" ";
}