PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000186. Trà và vỏ quýt (Tea with Tangerines)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trà và vỏ quýt (Tea with Tangerines)

Tea with Tangerines

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn pieces of tangerine peel with positive integer sizes a1≤a2≤⋯≤ana_1\le a_2\le\cdots\le a_n. In one operation, you may split a piece of size zz into exactly two positive integer pieces u,vu,v satisfying u+v=zu+v=z. Your goal is that every pair of final pieces of sizes x,yx,y satisfies max⁡(x,y)<2min⁡(x,y)\max(x,y)<2\min(x,y). Find the minimum number of splitting operations.

Input

Line 1 contains the integer nn, the initial number of peel pieces.

Line 2 contains exactly nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n in nondecreasing order.

Output

Print one integer: the minimum number of splits needed so that the largest final piece is strictly smaller than twice the smallest.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 1≤a1≤a2≤⋯≤an≤1001\le a_1\le a_2\le\cdots\le a_n\le 100.
  • Subtask 2 (30 points): 1≤n≤401\le n\le 40; 1≤a1≤a2≤⋯≤an≤1000001\le a_1\le a_2\le\cdots\le a_n\le 100000.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le 100; 1≤a1≤a2≤⋯≤an≤100000001\le a_1\le a_2\le\cdots\le a_n\le 10000000.

Examples

Example 1

Input

5
1 2 3 4 5

Output

10

Explanation

The smallest piece has size 11 and cannot be split into smaller positive integers. To satisfy M<2⋅1M<2\cdot1, every final piece must have size 11. The original sizes 1,2,3,4,51,2,3,4,5 require respectively 0,1,2,3,40,1,2,3,4 splits, totaling 1010.

Example 2

Input

5
600 900 1300 2000 2550

Output

4

Explanation

Keep the pieces 600600 and 900900. Split 13001300 into 600+700600+700 (one operation), 20002000 into 1000+10001000+1000 (one), and 25502550 into 1000+1000+5501000+1000+550 (two). The smallest final piece is 550550, the largest is 10001000, and 1000<2⋅550=11001000<2\cdot550=1100. This takes four splits, matching the minimum required number of pieces for the larger originals.