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

#BS0000334. Tưới cây đồng đều (Water the Trees)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tưới cây đồng đều (Water the Trees)

Water the Trees

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn trees of initial heights hih_i, on each day starting at day 1 either skip the day or water one tree. Watering adds one unit on odd days and two units on even days. Minimize the number of days until all heights are equal.

Input

First line: nn. Second line: nn integers hih_i.

Output

Print the minimum number of days.

Subtasks

  • Subtask 1 (20 points): 1≤n≤41\le n\le4; 1≤hi≤51\le h_i\le5.
  • Subtask 2 (30 points): 1≤n≤3⋅1051\le n\le3\cdot10^5; 1≤hi≤1091\le h_i\le10^9; all heights have the same parity.
  • Subtask 3 (50 points): 1≤n≤3⋅1051\le n\le3\cdot10^5; 1≤hi≤1091\le h_i\le10^9.

Examples

Input 1

3
1 2 4

Output 1

4

Explanation

Water tree 1 on day 1, tree 2 on day 2, skip day 3, and water tree 1 on day 4. Three days supply at most 4 units, less than the required 5.

Input 2

2
7 7

Output 2

0

Explanation

Both trees already have equal heights.