/************************************************
*Author :jibancanyang
*Created Time :二 4/ 5 09:33:33 2016
*题目类型:*最直观的思维是发现搜索很好写,但是仔细一想是可以转化为递推的.递推的时候要找
*矩阵中的转移路线然后确定递推顺序.本题也是一直二叉树来记忆化逐步优化为递推二维dp的典型例子
*************************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
vector<int> vi;
#define xx first
#define yy second
#define sa(n) scanf("%d", &(n))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++)
const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 2e3 + 12;
int a[maxn];
int dp[maxn][maxn];
int main(void)
{
#ifdef LOCAL
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int n;
while (~sa(n)) {
rep (i, 1, n + 1) sa(a[i]), dp[i][i] = a[i] * n;
for (int i = n - 1; i > 0; i--) {
for (int j = i + 1; j <= n; j++) {
int t = n - j + i;
dp[i][j] = max(dp[i][j - 1] + t * a[j], dp[i + 1][j] + t * a[i]);
}
}
cout << dp[1][n] << endl;
}
return 0;
}
poj3186 dp
最新推荐文章于 2020-04-01 11:59:59 发布