solution.cpp 645 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;

int numv[maxn];
int v[maxn];

int dfs(int idx)
{
	if (idx == 0)
		return 0;

	if (idx == 1)
		return v[0] * v[1];

	int max_, a = 0x3f3f3f, b = 0x3f3f3f3f, c = 0;
	if (idx >= 2)
		a = (v[idx] * v[idx - 1] + dfs(idx - 2) + numv[idx - 2] * (v[idx] + v[idx - 1])); //与左边粘
	b = v[idx] * numv[idx - 1] + dfs(idx - 1);											  //与左边不粘
	c = min(a, b);
	return c;
}

int main()
{

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &v[i]);
		if (i == 0)
			numv[i] = v[i];
		numv[i] = numv[i - 1] + v[i];
	}

	printf("%d", dfs(n - 1));
}