#P10989. [蓝桥杯 2023 国 Python A/Java A] 等腰三角形

[蓝桥杯 2023 国 Python A/Java A] 等腰三角形

Background

Python users are recommended to submit this problem using PyPy3.

Problem Description

Given a sequence AiA_i containing nn numbers, in each operation you may choose any number and change it by +1+1 or 1-1.

We want to make this sequence satisfy the following condition: no matter which three numbers you choose from it, the three corresponding lengths can always form an isosceles triangle. Find the minimum number of operations needed to make the sequence satisfy this condition.

Input Format

The first line contains an integer nn.

The second line contains nn integers, representing A1,A2,,AnA_1, A_2, \cdots, A_n, separated by a single space.

Output Format

Output one line containing an integer, representing the minimum number of operations.

5
3 3 5 7 7

3

Hint

For 40%40\% of the test cases, n5000,Ai5000n \le 5000, A_i \le 5000.

For 70%70\% of the test cases, n2×105,Ai106n \le 2 \times 10^5, A_i \le 10^6.

For all test cases, 1n2×105,1Ai1091 \le n \le 2 \times 10^5, 1 \le A_i \le 10^9.

Sample Explanation

You can modify the original sequence to 4,4,4,7,74, 4, 4, 7, 7.

Translated by ChatGPT 5