Sum of Maximum Weights

AtCoder
IDabc214_d
Time2000ms
Memory256MB
Difficulty
We have a tree with $N$ vertices numbered $1, 2, \dots, N$. The $i$\-th edge $(1 \leq i \leq N - 1)$ connects Vertex $u_i$ and Vertex $v_i$ and has a weight $w_i$. For different vertices $u$ and $v$, let $f(u, v)$ be the greatest weight of an edge contained in the shortest path from Vertex $u$ to Vertex $v$. Find $\displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j)$. ## Constraints * $2 \leq N \leq 10^5$ * $1 \leq u_i, v_i \leq N$ * $1 \leq w_i \leq 10^7$ * The given graph is a tree. * All values in input are integers. ## Input Input is given from Standard Input in the following format: $N$ $u_1$ $v_1$ $w_1$ $\vdots$ $u_{N - 1}$ $v_{N - 1}$ $w_{N - 1}$ [samples]
Samples
Input #1
3
1 2 10
2 3 20
Output #1
50

We have $f(1, 2) = 10$, $f(2, 3) = 20$, and $f(1, 3) = 20$, so we should print their sum, or $50$.
Input #2
5
1 2 1
2 3 2
4 2 5
3 5 14
Output #2
76
API Response (JSON)
{
  "problem": {
    "name": "Sum of Maximum Weights",
    "description": {
      "content": "We have a tree with $N$ vertices numbered $1, 2, \\dots, N$.   The $i$\\-th edge $(1 \\leq i \\leq N - 1)$ connects Vertex $u_i$ and Vertex $v_i$ and has a weight $w_i$. For different vertices $u$ and $v$",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc214_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "We have a tree with $N$ vertices numbered $1, 2, \\dots, N$.  \nThe $i$\\-th edge $(1 \\leq i \\leq N - 1)$ connects Vertex $u_i$ and Vertex $v_i$ and has a weight $w_i$.\nFor different vertices $u$ and $v$...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments