E | Eat Walnuts |
每次只能选一个数字(下标为 j,i为它前一个数字下标,k为下一个数字的下标),而且这个数字不能是开头或者结尾数字,把它消耗掉的代价是(a[i]+a[j]+a[k])^2,问最后剩两个数字的时候的最小总花费是多少。
区间DP,其实挺简单,当时憨批了,其实一直是。。。
// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <random>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO \
ios::sync_with_stdio(false); \
// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e3 + 10;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
// int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n;
LL a[maxn];
LL dp[maxn][maxn];
LL sq(LL x)
{
return x * x;
}
int main()
{
#ifdef WXY
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
IO;
while (cin >> n)
{
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int len = 3; len <= n; len++)
{
for (int i = 1;; i++)
{
int j = i + len - 1;
if (j > n)
break;
dp[i][j] = INF;
for (int k = i + 1; k < j; k++)
dp[i][j] = min(dp[i][k] + dp[k][j] + sq(a[i] + a[k] + a[j]), dp[i][j]);
}
}
cout << dp[1][n] << endl;
}
return 0;
}