|
|
@@ -0,0 +1,77 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+
|
|
|
+namespace D8._2
|
|
|
+{
|
|
|
+ class Program
|
|
|
+ {
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ if (args.Length < 1) return;
|
|
|
+ if (File.Exists(args[0]) == false) return;
|
|
|
+ var file = File.OpenText(args[0]);
|
|
|
+
|
|
|
+ var text = file.ReadToEnd();
|
|
|
+ var all = new List<string>(text.Split(" "));
|
|
|
+
|
|
|
+ var root = getNode(all);
|
|
|
+
|
|
|
+ Console.WriteLine($"Result is : {root.Value}");
|
|
|
+ }
|
|
|
+
|
|
|
+ static Node getNode(List<string> all)
|
|
|
+ {
|
|
|
+ var node = new Node();
|
|
|
+
|
|
|
+ (int children, int metadatas) header = (int.Parse(all[0]), int.Parse(all[1]));
|
|
|
+
|
|
|
+ all.RemoveAt(0);
|
|
|
+ all.RemoveAt(0);
|
|
|
+
|
|
|
+ for (int j = 0; j < header.children; j++)
|
|
|
+ {
|
|
|
+ var childnode = getNode(all);
|
|
|
+ node.Children.Add(childnode);
|
|
|
+ }
|
|
|
+
|
|
|
+ int metasum = 0;
|
|
|
+
|
|
|
+ for (int j = 0; j < header.metadatas; j++)
|
|
|
+ {
|
|
|
+ int meta = int.Parse(all[0]);
|
|
|
+ all.RemoveAt(0);
|
|
|
+ node.Meta.Add(meta);
|
|
|
+
|
|
|
+ if (header.children == 0) metasum += meta;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var i = meta - 1;
|
|
|
+ if (i < 0 || i >= node.Children.Count) continue;
|
|
|
+
|
|
|
+ metasum += node.Children[i].Value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ node.Value = metasum;
|
|
|
+
|
|
|
+ return node;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ class Node
|
|
|
+ {
|
|
|
+ public List<int> Meta { get; set; }
|
|
|
+
|
|
|
+ public List<Node> Children { get; set; }
|
|
|
+
|
|
|
+ public int Value { get; set; }
|
|
|
+
|
|
|
+ public Node()
|
|
|
+ {
|
|
|
+ Meta = new List<int>();
|
|
|
+ Children = new List<Node>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|