建图贼难想
树上跑多重背包
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 610
using namespace std;
int n, cnt;
int f[MAXN][MAXN];
struct N {
int ti, v, lc, rc;
} ns[MAXN];
int read() {
int f = 1, k = 0;
char c = getchar();
while(c > '9' || c < '0') {
if(c == '-') {
f = -1;
}
c = getchar();
}
while(c >= '0' && c <= '9') {
k = k * 10 + c - '0';
c = getchar();
}
return f * k;
}
int sread() {
int now = ++cnt;
ns[now].ti = read();
ns[now].ti *= 2;
ns[now].v = read();
if(ns[now].v == 0) {
ns[now].lc = sread();
ns[now].rc = sread();
for(int i = ns[now].ti; i <= n; i ++) {
for(int k = 0; k <= i - ns[now].ti; k ++) {
f[now][i] = max(f[now][i], f[ns[now].lc][k] + f[ns[now].rc][i - k - ns[now].ti]);
}
}
} else {
for(int i = ns[now].ti; i <= n; i ++) {
f[now][i] = min((i - ns[now].ti) / 5, ns[now].v);
}
}
return now;
}
int main() {
n = read();
sread();
n--;
printf("%d", f[1][n]);
return 0;
}