using
System;
using
System.Collections.Generic;
class
Program
{
static
List<List<
int
>> adj;
static
int
ans;
static
List<
int
> DFS(
int
par,
int
node, List<
int
> wei)
{
int
su = 0;
int
miOdd =
int
.MaxValue;
int
miEven =
int
.MaxValue;
foreach
(
int
i
in
adj[node])
{
if
(i != par)
{
List<
int
> li = DFS(node, i, wei);
su += li[0];
miOdd = Math.Min(miOdd, li[1]);
miEven = Math.Min(miEven, li[2]);
}
}
su += wei[node];
if
(wei[node] % 2 == 0)
{
miEven = Math.Min(miEven, wei[node]);
}
else
{
miOdd = Math.Min(miOdd, wei[node]);
}
if
(su % 2 == 0)
{
if
(miOdd !=
int
.MaxValue)
{
ans = Math.Max(ans, su - miOdd);
}
}
else
{
if
(miEven < 0)
{
ans = Math.Max(ans, su - miEven);
}
else
{
ans = Math.Max(ans, su);
}
}
return
new
List<
int
> { su, miOdd, miEven };
}
static
int
MaximumOddSum(List<
int
> par, List<
int
> wei)
{
ans = 0;
int
n = par.Count;
adj =
new
List<List<
int
>>();
for
(
int
i = 0; i < n + 10; i++)
{
adj.Add(
new
List<
int
>());
}
ans =
int
.MinValue;
for
(
int
i = 1; i < n; i++)
{
int
u = i;
int
v = par[i];
adj[u].Add(v);
adj[v].Add(u);
}
DFS(-1, 0, wei);
if
(ans ==
int
.MinValue)
{
ans = -1;
}
return
ans;
}
public
static
void
Main(
string
[] args)
{
List<
int
> par =
new
List<
int
> { -1, 0, 0, 1, 1, 2 };
List<
int
> wei =
new
List<
int
> { 5, 3, 1, 1, 3, 3 };
Console.WriteLine(MaximumOddSum(par, wei));
}
}