Type: Default 1000ms 256MiB

完全平方数组

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Description

我们将任意两个相邻数相加均为完全平方数的数组称为完全平方数组,即如果长度为 NN 的数组 AA 为完全平方数组,则 i[1,N),A[i]+A[i+1] \forall i \in [1,N) , A[i]+A[i+1]为完全平方数(完全平方数是指一个正整数,如果它可以表示为某个整数的平方,则称该正整数为完全平方数)

给你一个长度为 n n 的数组 a a ,以及如下三种操作:

  • 将数组中任意一个数字 a[i]a[i] 修改为 a[i]+1a[i]+1a[i]1a[i]-1,代价为 11
  • 将数组所有数字修改为任意正整数 x x , 代价为 x2 x^2
  • 对数组求前缀和操作,即对于1in 1 \leq i \leq n a[i]变为1ia[i] a[i] 变为 \sum_1^i a[i],代价为 0 0

请问将该数组修改为完全平方数组的最小代价是多少?

Format

Input

每个测试点只有一组测试数据

第一行一个正整数 n(2n1e5) n(2 \leq n \leq 1e5) ,代表数组大小
第二行 nn 个整数 ai(0ai5e5)a_i(0 \leq a_i \leq 5e5),代表数组第 ii 个元素

Output

输出将给定数组修改为完全平方数组的最小代价

Samples

5
1 2 7 18 7

1

SDNU_ACM_ICPC_2025新生月赛

Not Attended
Status
Done
Rule
ACM/ICPC
Problem
13
Start at
2025-11-16 12:00
End at
2025-11-16 17:00
Duration
5 hour(s)
Host
Partic.
38