CF BUDDY
← Problems·

1397B · Power Sequence

1500 · brute force, math, number theory

Problem: Let's call a list of positive integers a0,a1,...,an1a_0, a_1, ..., a_{n-1} a power sequence if there is a positive integer cc, so that for every 0in10 \le i \le n-1 then ai=cia_i = c^i.

Given a list of nn positive integers a0,a1,...,an1a_0, a_1, ..., a_{n-1}, you are allowed to:

  • Reorder the list (i.e. pick a permutation pp of {0,1,...,n1}\{0,1,...,n - 1\} and change aia_i to apia_{p_i}), then
  • Do the following operation any number of times: pick an index ii and change aia_i to ai1a_i - 1 or ai+1a_i + 1 (i.e. increment or decrement aia_i by 11) with a cost of 11.

Find the minimum cost to transform a0,a1,...,an1a_0, a_1, ..., a_{n-1} into a power sequence.

Input Format: The first line contains an integer nn (3n1053 \le n \le 10^5).

The second line contains nn integers a0,a1,...,an1a_0, a_1, ..., a_{n-1} (1ai1091 \le a_i \le 10^9).

Output Format: Print the minimum cost to transform a0,a1,...,an1a_0, a_1, ..., a_{n-1} into a power sequence.

Note: In the first example, we first reorder {1,3,2}\{1, 3, 2\} into {1,2,3}\{1, 2, 3\}, then increment a2a_2 to 44 with cost 11 to get a power sequence {1,2,4}\{1, 2, 4\}.

Sample Cases

Case 1

Input

3
1 3 2

Output

1

Case 2

Input

3
1000000000 1000000000 1000000000

Output

1999982505

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback