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

#BS0000315. Lễ hội đồ chơi gỗ (Wooden Toy Festival)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lễ hội đồ chơi gỗ (Wooden Toy Festival)

Wooden Toy Festival

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A town is preparing for a wooden toy festival. Its workshop has three carvers and expects NN customers. Customer ii requests a toy with integer pattern aia_i.

Before the festival, each carver selects an integer pattern xx in [1,109][1,10^9] to practise. Producing pattern aia_i then takes ∣x−ai∣|x-a_i| units of time. A carver can handle several orders simultaneously, so a customer's waiting time depends only on the requested and practised patterns.

Choose the three practised patterns and assign each customer to one carver to minimize the maximum waiting time among all NN customers.

Input

  • The first line contains the integer NN, the number of customers.
  • The second line contains NN integers a1,…,aNa_1,\ldots,a_N, the requested patterns.

Output

Print one integer: the minimum possible maximum waiting time.

Subtasks

  • Subtask 1 (20 points): 1≤N≤121\le N\le12; 1≤ai≤501\le a_i\le50.
  • Subtask 2 (30 points): 1≤N≤2⋅1051\le N\le2\cdot10^5; 1≤ai≤1091\le a_i\le10^9; a1≤⋯≤aNa_1\le\cdots\le a_N.
  • Subtask 3 (50 points): 1≤N≤2⋅1051\le N\le2\cdot10^5; 1≤ai≤1091\le a_i\le10^9.

Examples

Input 1

6
5 4 2 1 30 60

Output 1

2

Explanation

Select patterns 3, 30 and 60. Requests 5, 4, 2 and 1 wait 2, 1, 1 and 2 units respectively; the other two wait zero. A limit of 1 requires separate carvers for 1, 5, 30 and 60. Three carvers cannot achieve it, so the answer is 2.

Input 2

6
1 7 7 9 9 9

Output 2

0

Explanation

Choose patterns 1, 7 and 9. Every customer is assigned to a carver who has practised the requested pattern, so all waiting times are zero, which is the lower bound.

Input 3

9
14 19 37 59 1 4 4 98 73

Output 3

13

Explanation

Choose patterns 14, 50 and 85 for groups (1,4,4,14,19), (37,59) and (73,98); each group's maximum wait is 13. With a limit of 12, requests 1, 37, 73 and 98 have pairwise distances of at least 25, so they need four different carvers.

Input 4

1
2

Output 4

0

Explanation

The only customer requests pattern 2. One carver practises pattern 2, so the customer waits zero time.

Input 5

6
3 10 1 17 15 11

Output 5

1

Explanation

Choose patterns 2, 10 and 16 for groups (1,3), (10,11) and (15,17). Each group has maximum wait 1. Zero waiting would require six carvers for six distinct patterns.