using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int monsterCount = int.Parse(Reader.ReadLine()); List mikata = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).OrderBy(a=>a).ToList(); TreeNode root = this.CreateTree(mikata); List teki = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToList(); long ans = long.MaxValue; for(int i=0; i src) { Queue task = new Queue(); TreeNode root = new TreeNode(); root.Item = new Mikata(src[0]); task.Enqueue(root); for(int i=0; i= src.Count) { break; } tr.Right = new TreeNode(); tr.Right.Parent = tr; tr.Right.Item = new Mikata(src[i+1]); task.Enqueue(tr.Right); } return root; } public class TreeNode { public TreeNode Parent; public TreeNode Left; public TreeNode Right; public Mikata Item; public int MaxCount() { int ans = this.Item.Count; if(this.Left != null) { ans = Math.Max(ans, this.Left.MaxCount()); } if(this.Right != null) { ans = Math.Max(ans, this.Right.MaxCount()); } return ans; } public TreeNode Duplicate() { TreeNode tn = new TreeNode(); if(this.Left != null) { tn.Left = this.Left.Duplicate(); tn.Left.Parent = tn; } if(this.Right != null) { tn.Right = this.Right.Duplicate(); tn.Right.Parent = tn; } tn.Item = this.Item.Duplicate(); return tn; } private bool IsSmaller(TreeNode target) { if(this.Item.Level < target.Item.Level) { return true; } else if(this.Item.Level > target.Item.Level) { return false; } else if(this.Item.Count < target.Item.Count) { return true; } return false; } public void Swap() { // 小さいものが上にくる if(this.Left == null && this.Right == null) { return; } bool isLeft = true; bool mustSwap = false; if(this.Left != null & this.Right != null) { if(this.Right.IsSmaller(this.Left)) { if(this.Right.IsSmaller(this)) { isLeft = false; mustSwap = true; } } else { if(this.Left.IsSmaller(this)) { isLeft = true; mustSwap = true; } } } else if(this.Left != null && this.Left.IsSmaller(this)) { isLeft = true; mustSwap = true; } else if(this.Right != null && this.Right.IsSmaller(this)) { isLeft = false; mustSwap = true; } if(mustSwap) { if(isLeft) { Mikata tmp = this.Item; this.Item = this.Left.Item; this.Left.Item = tmp; this.Left.Swap(); } else { Mikata tmp = this.Item; this.Item = this.Right.Item; this.Right.Item = tmp; this.Right.Swap(); } } } } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { Program prg = new Program(); prg.Proc(); } }