import heapq def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 A = list(map(int, data[idx:idx+N])) idx += N B = list(map(int, data[idx:idx+N])) idx += N # Precompute orders for each starting point k orders = [] for k in range(N): order = [] for i in range(N): pos = (k + i) % N order.append(B[pos]) orders.append(order) min_max = N # Initialize with the worst possible value for k in range(N): current_level = A.copy() current_count = [0] * N heap = [(current_level[i], current_count[i], i) for i in range(N)] heapq.heapify(heap) current_max = 0 early_terminate = False for b in orders[k]: while True: level, cnt, idx = heapq.heappop(heap) if current_level[idx] == level and current_count[idx] == cnt: current_count[idx] += 1 new_level = level + (b // 2) current_level[idx] = new_level heapq.heappush(heap, (new_level, current_count[idx], idx)) if current_count[idx] > current_max: current_max = current_count[idx] if current_max >= min_max: early_terminate = True break if early_terminate: break if not early_terminate and current_max < min_max: min_max = current_max print(min_max) if __name__ == "__main__": main()