Computer Network

면접 대비

시간 제한1초메모리 제한2048 MB

요약
배열 a 전체에 +1을 더하거나 2로 나눈 몫을 취하는 연산만으로 a를 b로 바꾸는 최소 횟수를 구하고, 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
수학, 그리디, 이분 탐색, 구현
정답자
아직 제출이 없습니다

문제

The additive-increase/multiplicative-decrease (AIMD) algorithm is a feedback control algorithm best known for its use in TCP congestion control. AIMD combines linear growth of the congestion window when there is no congestion with an exponential reduction when congestion is detected. Multiple flows using AIMD congestion control will eventually converge to an equal usage of a shared link. (from Wikipedia)

You are given two arrays of nn integers: aa and bb. You can perform operations on the array aa. In one operation, you can let a_ia\_i become a_i+1a\_i+1 for all 1≤i≤n1 \leq i \leq n, or let a_ia\_i become ⌊a_i2⌋\left\lfloor \frac{a\_i}{2} \right\rfloor for all 1≤i≤n1 \leq i \leq n.

Find the minimum number of operations that you have to perform to transform aa into bb, or determine that it is impossible.

입력

The first line contains an integer nn (1≤n≤1061 \leq n \leq 10^6).

The second line contains the integer array a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (0≤a_i≤1090 \leq a\_i \leq 10^9).

The third line contains the integer array b_1,b_2,…,b_nb\_1, b\_2, \ldots, b\_n (0≤b_i≤1090 \leq b\_i \leq 10^9).

출력

Print the minimum number of operations needed, or −1-1 if it's impossible to transform aa into bb.

예제3

  1. 예제 1

    입력
    5
    1 2 3 4 5
    6 6 6 6 7
    
    예상 출력
    9
    
  2. 예제 2

    입력
    3
    2 3 4
    1 2 3
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    2
    65536 65537
    1 2
    
    예상 출력
    32