NC213445. EatWalnuts(评测)
描述
输入描述
Input contains multiple test cases.The first line of each test case contains a integer n(3 ≤ n ≤ 100), the number of walnuts at the beginning. The second line contains n positive integers separated by spaces, representing the size of each walnut. Each positive integer does not exceed 1,000.
For 50% of the testcases, n ≤ 50.
For 90% of the testcases, n ≤ 90.
For 100% of the testcases, n ≤ 100.
The number of the testcases does not exceed 1000.
输出描述
For each test case, print a integer–the minimum price Mr.Watermelon will pay.
示例1
输入:
5 3 1 50 20 15
输出:
6698
C++(clang++11) 解法, 执行用时: 63ms, 内存消耗: 432K, 提交时间: 2020-10-31 14:32:57
#include <bits/stdc++.h> using namespace std; int f[105][105],a[1005]; inline int mul(int x){return x*x;} int main (){ int n,d,i,k; while (scanf ("%d",&n)!=EOF) {for (i=1;i<=n;i++) {scanf ("%d",&a[i]);} for (d=3;d<=n;d++) {for (i=1;i+d-1<=n;i++) {int j=i+d-1; f[i][j]=2147483647; for (k=i+1;k<j;k++) {f[i][j]=min(f[i][j],f[i][k]+f[k][j]+mul(a[i]+a[j]+a[k]));} } } printf ("%d\n",f[1][n]); } return 0; }