include [HTML_REMOVED]
include [HTML_REMOVED]
using namespace std;
int main() {
int n;
cin >> n;
int* a = new int[n+1]; //a[0]不使用,a[]表示输入的序列
a[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
//f[i][j]表示前i项固定,且b[i]=j时候的序列b[0]~b[i]的价值的最小可能值,其中f[0][]不使用
int** f = new int*[n + 1];
for (int i = 0; i <= n; i++) {
f[i] = new int[10];
}
for (int i = 0; i <= 9; i++) { //f[0][]初始化
f[0][i] = 0;
}
for (int i = 0; i <= 9; i++) { //f[1][]初始化
f[1][i] = abs(i - a[1]);
}
//为了计算当b[i]==j时f[i][j]的最小值,
//需要找到不同b[i-1]的值(k值)下的f[i-1][b[i-1]] + |a[i]-b[i]| + (b[i]-b[i-1])^2 的最小值
for (int i = 2; i <= n; i++) { //逐行计算f[i][j]
for (int j = 0; j <= 9; j++) { //固定f[i][j]的j值
for (int k = 0; k <= 9; k++) { //逐个递增k值,用于计算固定j值下的f[i][j]的最小值
f[i][j] = min(f[i][j], f[i - 1][k] + abs(a[i] - j) + (j - k) * (j - k));
}
}
}
//测试代码
/*for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 9; j++) {
cout << f[i][j] << " ";
}
}*/
//从f[n][]中找出最小值
int m = f[n][0];
for (int i = 1; i <= 9; i++) {
m = min(f[n][i], m);
}
cout << m;
//清除内存
delete[] a;
for (int i = 0; i <= n + 1; i++) {
delete[] f[i];
}
delete[] f;
return 0;
}