【dp每日一题】POJ 3186 Treats for the Cows
大意:
给出n个数,每次只能从剩下的数里面取第一个数或者最后一个数,价值是\(a[i]*k\),k为第几次取,问最大价值和是多少
思路
区间dp,\(dp[i][j]\)代表从剩下第i个数和到第j个数时,能取到的最大值
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int N = 2e3 + 5;
int n, a[N], dp[N][N], res;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], dp[i][i] = n * a[i];
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
dp[i][j] = max(dp[i + 1][j] + (n - (j - i)) * a[i],
dp[i][j - 1] + (n - (j - i)) * a[j]);
}
}
cout << dp[1][n] << endl;
}