用一些 trick 可以轻松 O(n2) 过:162133631
#include <cstdio>
#define maxn 50005
using namespace std;
using LL = long long;
const LL INF = 1e18l;
inline void setmin(LL& x, const LL& y) { if(y < x) x = y; }
inline LL sqr(const LL& x) { return x * x; }
LL s[maxn], f[maxn];
int main()
{
int n, l;
scanf("%d%d", &n, &l);
for(int i=1; i<=n; i++)
{
int x;
scanf("%d", &x);
s[i] = s[i - 1] + x + 1;
}
l ++;
for(int i=1; i<=n; i++)
{
LL t = s[i] - l, f1 = INF, f2 = INF;
int j = 0;
for(; j+1<i; j+=2)
{
setmin(f1, f[j] + sqr(t - s[j]));
setmin(f2, f[j + 1] + sqr(t - s[j + 1]));
}
f[i] = f1 < f2? f1: f2;
if(j + 1 == i)
setmin(f[i], f[j] + sqr(t - s[j]));
}
printf("%lld\n", f[n]);
return 0;
}