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

vector<pair<ll,ll>> edge[1000]; // {id, distance}
ll total;

namespace sensible{
  // O(N)
  pair<ll,ll> dfs(int x,int up=-1){
    pair<ll,ll> res={0,1};

    for (auto y: edge[x]) if (y.first!=up){
      auto rec=dfs(y.first,x);

      rec.first+=y.second*rec.second;

      total+=rec.first*res.second+res.first*rec.second;
      res.second+=rec.second;
      res.first+=rec.first;
    }

    return res;
  }
}

namespace stupid{
  ll depth[1000];
  ll sum[1000];
  ll p[1000][20];

  // O(N)
  void prep(int x,int up=-1){
    for (int i=1; i<20; i++){
      p[x][i]=p[p[x][i-1]][i-1];
    }
    for (auto y: edge[x]) if (y.first!=up){
      sum[y.first]=sum[x]+y.second;
      depth[y.first]=depth[x]+1;
      p[y.first][0]=x;
      prep(y.first,x);
    }
  }

  // O( (logN) * (N^2) )
  void distance(int x,int y){
    total+=sum[x]+sum[y];

    if (depth[x]<depth[y]) swap(x,y);
    int lca=x;
    for (int i=20; i--;) if ((depth[x]-depth[y])&(1<<i)) x=p[x][i];
    for (int i=20; i--;) if (p[x][i]!=p[y][i]) x=p[x][i], y=p[y][i];
    if (x!=y) x=p[x][0];

    total-=sum[x]*2;
  }
}

int main(){
  srand(0x539);

  int const n=1000;
  for (int i=1; i<n; i++){
    int j=rand()%i;
    ll w=rand()%33333;
    edge[i].push_back({j,w});
    edge[j].push_back({i,w});
  }

  total=0;
  sensible::dfs(0);
  cout<<total<<endl;

  total=0;
  stupid::prep(0);
  for (int i=n; i--;) for (int j=i; j--;) stupid::distance(i,j);
  cout<<total<<endl;
}