树形dp
dp[i][j]=max(dp[i][j],dp[lch][k]+dp[rch][j-k])
#includeusing namespace std;int t, n, m, dp[1005][605];struct node { int tim, pic; }tr[5005];inline int read() { int red = 0, f_f = 1; char ch = getchar(); while(ch>'9' || ch <'0') {if(ch=='-') f_f = -1; ch = getchar();} while(ch>='0' &&ch<='9') red = red*10+ch-'0', ch = getchar(); return red*f_f;}void dfs_read(int a) { tr[a].tim = read() * 2, tr[a].pic = read(); if(!tr[a].pic) { dfs_read(a << 1); dfs_read(a << 1| 1); }}void dfs(int a, int r) { if(dp[a][r] || !r) return; if(tr[a].pic) { dp[a][r] = min(tr[a].pic, (r - tr[a].tim) / 5); return; } for (int i = 0; i <= r - tr[a].tim; i++) { dfs(a << 1, i); dfs(a << 1 | 1, r - tr[a].tim - i); dp[a][r] = max(dp[a][r], dp[a << 1][i] + dp[a << 1 | 1][r - tr[a].tim - i]); }}int main(){ memset(dp, 0, sizeof(dp)); t = read() - 1; dfs_read(1); dfs(1, t); printf("%d\n", dp[1][t]); return 0;}