#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <fstream> #include <map> #include <ext/pb_ds/priority_queue.hpp> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int maxn = 1000010; const int inf = 0x3f3f3f3f; inline ll read() { ll x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') f = (c == '-' ? -1 : 1), c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } __gnu_pbds::priority_queue<pii> Pb[maxn], Pr[maxn]; struct edge { int v, next; } e[maxn]; struct point { double x, y; int w; } p[maxn]; struct query { double the; int pr; }arc[maxn]; int n, k, l, cnt, b[maxn], r[maxn], pr[maxn], head[maxn], fa[maxn], in[maxn], deep[maxn]; ll B[maxn], R[maxn]; void insert(int u, int v) { e[cnt] = (edge) {v, head[u]}, head[u] = cnt++; } void dfs(int u) { Pb[u].push(make_pair(deep[u], b[u])); Pr[u].push(make_pair(deep[u], r[u])); for (int i = head[u]; i != -1; i = e[i].next) { int v = e[i].v; deep[v] = deep[u] + 1, dfs(v); Pb[u].join(Pb[v]), B[u] += B[v]; Pr[u].join(Pr[v]), R[u] += R[v]; } while (Pb[u].top().first - k >= deep[u]) B[u] -= Pb[u].top().second, Pb[u].pop(); while (Pr[u].top().first - k >= deep[u]) R[u] -= Pr[u].top().second, Pr[u].pop(); } double dis(point a, point b) { double x = a.x - b.x, y = a.y - b.y; return sqrt(x * x + y * y); } bool cmp(query a, query b) { return a.the < b.the || a.the == b.the && a.pr > b.pr; } void Max_Circle_Cover() { ll ans = 0; for (int i = 0; i < cnt; i++) { int sum = 0; for (int j = 0; j < cnt; j++) if (i != j && dis(p[i], p[j]) <= 2.0 * l) { double the = atan2((p[i].y - p[j].y), (p[i].x - p[j].x)); double phi = acos(dis(p[i], p[j]) / (2.0 * l)); arc[sum++] = (query) {the - phi, p[j].w}; arc[sum++] = (query) {the + phi, -p[j].w}; } sort(arc, arc + sum, cmp); ll tmp = p[i].w; for (int j = 0; j < sum; j++) { tmp += arc[j].pr; ans = max(ans, tmp); } } cout << ans << endl; } int main() { freopen("sample.in", "r", stdin); n = read(), k = read(), l = read(); memset(head, -1, sizeof head); for (int i = 1; i <= n; i++) { b[i] = B[i] = read(); r[i] = R[i] = read(); pr[i] = read(); fa[i] = read(); in[i] = read(); if (fa[i] != -1) insert(fa[i], i); } dfs(1), cnt = 0; for (int i = 1; i <= n; i++) if (in[i] == 1) p[cnt++] = (point) {B[i], R[i], pr[i]}; Max_Circle_Cover(); return 0; }