using System; using System.Collections.Generic; using System.Linq; internal class Program { internal class PriorityComparer : IComparer> { public int Compare(Tuple x, Tuple y) { int firstCompared = x.Item1.CompareTo(y.Item1); if (firstCompared == 0) { return x.Item2.CompareTo(y.Item2); } return firstCompared; } } private static readonly PriorityComparer _comparer = new PriorityComparer(); static void PushHeap(List> array, Tuple elem) { int n = array.Count; int left = 0; int right = n - 1; while (right - left > 1) { int middle = (right + left)/2; int compare = _comparer.Compare(elem, array[middle]); if (compare == 0) { array.Insert(middle, elem); return; } if (compare < 0) { right = middle; } else { left = middle; } } for (int i = left; i <= right; i++) { if (_comparer.Compare(elem, array[i]) == 0) { array.Insert(i, elem); return; } if (_comparer.Compare(elem, array[i]) < 0) { array.Insert(i, elem); return; } } array.Add(elem); } static Tuple PopHeap(List> array) { var target = array[0]; array.RemoveAt(0); return target; } public static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var own = Console.ReadLine().Split(' ').Select(element=>new Tuple(int.Parse(element), 0)).ToArray(); var opposites = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var list = new List>(); for (int i = 0; i < n;i++ ) { PushHeap(list, own[i]); } var battleCounts = new int[n]; for (int i = 0; i < n; i++) { var maxBattleCount = 0; var source = new List>(list); for (int k = i; k < i + n; k++) { var first = PopHeap(source); var opposite = opposites[k%n]; int battleCount = first.Item2 + 1; PushHeap(source, new Tuple(first.Item1 + opposite/2, battleCount)); maxBattleCount = Math.Max(maxBattleCount, battleCount); } battleCounts[i] = maxBattleCount; } Console.WriteLine(battleCounts.Min()); } }