Ejemplo n.º 1
0
        private static void Sum(Item2 i1, IList <ICollection <Item2> > sorted)
        {
            sum += i1.Value;
            if (i1.Dependencies.Count == 0)
            {
                if (sum > maxSum)
                {
                    maxSum = sum;
                }

                return;
            }


            foreach (var dep in i1.Dependencies)
            {
                Sum(unsorted[dep - 1], sorted);

                sum -= unsorted[dep - 1].Value;
            }

            return;
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            unsorted = new List <Item2>();


            n = int.Parse(Console.ReadLine());

            var times = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

            for (int j = 0; j < times.Count(); j++)
            {
                var item = new Item2(j + 1, times[j]);
                unsorted.Add(item);
            }

            //var a = new Item2("1", 5);
            //var b = new Item2("2", 7, "1", "3");
            //var c = new Item2("3", 1);
            //var d = new Item2("4", 2, "2", "5");
            //var e = new Item2("5", 6, "1", "3");

            //unsorted = new List<Item2>() { a, b, c, d, e };



            sum    = 0;
            maxSum = 0;

            try
            {
                for (int i = 0; i < n; i++)
                {
                    var data = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                    if (data.Count() == 1 && data[0] == 0)
                    {
                        unsorted[i].Dependencies = new List <int>();
                        continue;
                    }
                    else
                    {
                        unsorted[i].Dependencies = new List <int>();


                        foreach (var i1 in data)
                        {
                            unsorted[i1 - 1].DependenciesCount++;

                            unsorted[i].Dependencies.Add(i1);
                        }
                    }
                }


                var sorted = TopologicalSort.Group(unsorted, x => x.Dependencies, x => x.Name);


                var independant       = sorted[0].Where(i => i.DependenciesCount == 0).ToList();
                var maxIndependantSum = 0;
                if (independant.Count > 0)
                {
                    maxIndependantSum = independant.Max(x => x.Value);
                }

                sorted[0] = sorted[0].Where(i => i.DependenciesCount != 0).ToList();

                sum = 0;



                for (int i = sorted[sorted.Count - 1].Count - 1; i >= 0; i--)
                {
                    sum = 0;
                    Sum(sorted[sorted.Count - 1].ToList()[i], sorted);
                }



                Console.WriteLine(Math.Max(maxSum, maxIndependantSum));
                //Print(sorted);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(-1);
            }
        }