---
# System prepended metadata

title: d103

---

# d103

```cpp=
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>

using namespace std;

pair<int, int> findMax(int cur, unordered_map<int, unordered_map<int, int>>& edges) {
    int maxLayers = 0, maxDist = 0;
    for (auto it = edges[cur].begin(); it != edges[cur].end(); it++) {
        int to = it->first;
        int w = it->second;
        if (edges[to].find(cur) != edges[to].end()) {
            edges[to].erase(cur);
        }
        pair<int, int> result = findMax(to, edges);
        if (result.first + w > maxDist) {
            maxDist = result.first + w;
        }
        if (result.second + 1 > maxLayers) {
            maxLayers = result.second + 1;
        }
    }
    return pair<int, int>(maxDist, maxLayers);
}

int main() {
    unordered_map<int, unordered_map<int, int>> edges;

    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n - 1; i++) {
        int x, w;
        scanf("%d%d", &x, & w);
        edges[i].insert({{x, w}});
        edges[x].insert({{i, w}});
    }

    pair<int, int> ans = findMax(0, edges);
    printf("%d\n%d\n", ans.first, ans.second);
    return 0;
}

```