C # implementation of the approach
using System ; using System . Collections . Generic ; class GFG { static int maximum = int . MinValue , x , ans = int . MaxValue ; static List < List < int > > graph = new List < List < int > > ( ) ; static List < int > weight = new List < int > ( ) ;