#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
constexpr static int inf = 0x3f3f3f3f;
int Chopsticks[5001];
int K, N;
int dp[5001][1009];
void Input() {
cin >> K >> N;
for (int i = N; i >= 1; --i) {
cin >> Chopsticks[i];
}
K += 8;
}
int Square(const int& x) {
return x * x;
}
int DP() {
memset(dp, 0x3f, sizeof(dp));
for (int i = 1; i <= N; ++i) {
dp[i][0] = 0;
}
for (int i = 3; i <= N; ++i) {
for (int j = 1; j <= K; ++j) {
//筷子不够组成j个三元组
if (i < 3 * j) {
break;
}
dp[i][j] = min(
dp[i - 1][j],
dp[i - 2][j - 1] + Square(Chopsticks[i - 1] - Chopsticks[i])
);
}
}
return dp[N][K];
}
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
Input();
cout << DP() << endl;
}
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容