why am i getting wrong answer . I have used DP and filling the table bottom up.
http://www.spoj.com/problems/TRT/
for(int i=0;i<n;i++)
{
dp[i][i]=n*a[i];
}
for(int i=n-2;i>=0;i--)
{
for(int j=i+1;j<n;j++)
{
dp[i][j]=max((i+1)*a[i]+dp[i+1][j],dp[i][j-1]+(i+1)*a[j]);//(i+1) is the age
}
}
printf("%d\n",dp[0][n-1]);
The age will not be i+1. It will be n+ij.
dp[i][j] = max( (n+i-j)*a[i]+dp[i+1][j], dp[i][j-1] + (n+i-j)*a[j] );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.