This is a classic DP, but it is obviously difficult to be certain.
In the best grouping scheme, two shorter chopsticks in each group must be adjacent to each other in the sequence of chopsticks.
DP [J] [I] indicates that J groups are picked out from the front I chopsticks (the third group is not considered)
DP [J] [I] = min {
DP [J] [I-1]; (I> J * 2)
DP [J-1] [I-2] + (hash [I]-Hash [I-1]) ^ 2; (n-I> (k-j) * 3)
}
It's so disgusting. I'm so depressed that I can stick my memo to the code.
# Include <iostream>
# Include <algorithm>
# Include <functional>
Using namespace STD;
Long hash [5010];
Long DP [2, 1010] [2, 5010];
Int main ()
{
Long T, k, n;
Scanf ("% lD", & T );
While (t --)
{
Scanf ("% LD % lD", & K, & N );
Long I, J;
For (I = 0; I <n; ++ I)
{
Scanf ("% lD", & hash [I]);
}
Sort (hash, hash + N, greater <long> ());
Long T = hash [1]-Hash [2];
DP [0] [2] = T * t;
For (I = 3; I <n; ++ I)
{
T = hash [I]-Hash [I-1];
T * = T;
DP [0] [I] = DP [0] [I-1]> T? T: DP [0] [I-1];
}
For (I = 1; I <K + 8; ++ I)
{
Long S = 3 * I + 2;
T = hash [s]-Hash [s-1];
T * = T;
DP [I] [s] = DP [I-1] [S-2] + T;
For (j = S + 1; j <n; ++ J)
{
T = hash [J]-Hash [J-1];
T * = T;
T = DP [I-1] [J-2] + T;
DP [I] [J] = DP [I] [J-1]> T? T: DP [I] [J-1];
}
}
Printf ("% LD \ n", DP [K + 7] [N-1]);
}
Return 0;
}