hdu1506

注意最后乘法会超int范围,转换为longlong。

#include 
#include 
using namespace std;

int A[100005];
int l[100005];
int r[100005];

int main()
{
	int n,i;
	while (1)
	{
		scanf("%d",&n);
		if (!n) break;
		for (i=0;i=0 && A[index]>=A[i])
				index=l[index]-1;
			l[i]=index+1;
		}

		r[n-1]=n-1;
		for (i=n-2;i>=0;i--)
		{
			index=i+1;
			while (index=A[i])
				index=r[index]+1;
			r[i]=index-1;
		}

		long long maxn=0;

		for (i=0;imaxn)
				maxn=(long long)(r[i]-l[i]+1)*A[i];

		cout << maxn << endl;
	}
}


你可能感兴趣的:(动态规划DP)