[USACO06FEB]奶牛零食Treats for the Cows
思路:
区间DP;
代码:
#include <bits/stdc++.h> using namespace std; #define maxn 2005 #define ll long long ll n,ai[maxn],dp[maxn][maxn],sum[maxn]; inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0')Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } int main() { in(n); for(ll i=1;i<=n;i++) in(ai[i]),sum[i]=sum[i-1]+ai[i],dp[i][i]=ai[i]; for(ll i=1;i<=n;i++) { for(ll v=1;v+i<=n;v++) { ll l=v,r=v+i; dp[l][r]=max(dp[l+1][r],dp[l][r-1])+sum[r]-sum[l-1]; } } cout<<dp[1][n]; return 0; }