using
System;
using
System.Collections.Generic;
using
System.Linq;
class
GFG
{
List<List<Tuple<
int
,
int
>>> adj =
new
List<List<Tuple<
int
,
int
>>>();
List<
int
> dp =
new
List<
int
>();
List<
bool
> vis =
new
List<
bool
>();
int
ans = 0;
void
dfs(
int
currNode, List<
int
> points)
{
vis[currNode] =
true
;
List<
int
> childNodes =
new
List<
int
>();
foreach
(
var
edge
in
adj[currNode])
{
int
childNode = edge.Item1;
int
edgeLength = edge.Item2;
if
(!vis[childNode])
{
dfs(childNode, points);
}
childNodes.Add(dp[childNode] - edgeLength);
}
childNodes.Sort((a, b) => b.CompareTo(a));
int
max1 = 0, max2 = 0;
if
(childNodes.Count >= 2)
{
max1 = Math.Max(max1, childNodes[0]);
max2 = Math.Max(max2, childNodes[1]);
}
else
if
(childNodes.Count >= 1)
{
max1 = Math.Max(max1, childNodes[0]);
}
ans = Math.Max(ans, max1 + max2 + points[currNode]);
dp[currNode] = max1 + points[currNode];
}
int
MaxPointPath(
int
n, List<
int
> points, List<List<
int
>> edges)
{
adj =
new
List<List<Tuple<
int
,
int
>>>(n + 1);
dp =
new
List<
int
>(n + 1);
vis =
new
List<
bool
>(n + 1);
for
(
int
i = 0; i <= n; i++)
{
adj.Add(
new
List<Tuple<
int
,
int
>>());
dp.Add(0);
vis.Add(
false
);
}
foreach
(
var
edge
in
edges)
{
int
u = edge[0];
int
v = edge[1];
int
w = edge[2];
adj[u].Add(
new
Tuple<
int
,
int
>(v, w));
adj[v].Add(
new
Tuple<
int
,
int
>(u, w));
}
dfs(1, points);
return
ans;
}
static
void
Main()
{
GFG obj =
new
GFG();
int
n = 5;
List<
int
> points =
new
List<
int
>(n + 1)
{
0, 6, 3, 2, 5, 0
};
List<List<
int
>> edges =
new
List<List<
int
>>
{
new
List<
int
> { 1, 2, 10 },
new
List<
int
> { 2, 3, 3 },
new
List<
int
> { 2, 4, 1 },
new
List<
int
> { 1, 5, 11 }
};
Console.WriteLine(obj.MaxPointPath(n, points, edges));
}
}