杭电acm1087

dp问题: ans[i] = max(ans[j]) + d[i],循环条件:d[j] < d[i] && temp < ans[j]

#include <stdio.h>
#include <string.h>

int main() {
	
	int d[1001], ans[1001];
	int n, i, j, flag, rst;
	while(~scanf("%d", &n) && n) {
		for(i=0; i<n; i++)
			scanf("%d", &d[i]);
		memset(ans, 0, sizeof(ans));
		for(i=0; i<n; i++) {
			flag = 0;
			int temp = -1;//记录最大值
			for(j=0; j<i; j++)
				if(d[i]>d[j] && temp<ans[j]) {
					temp = ans[j];
					flag = 1;
				}// end if
			if(flag)
				ans[i] = temp + d[i];
			else
				ans[i] += d[i];
		}//end for
		
		rst = -1;
		for(i=0; i<n; i++)
			if(rst<ans[i])
				rst = ans[i];
		
		printf("%d\n", rst);
	}//end while
	
	return 0;
}



posted @ 2015-11-15 13:49  StevenLuke  阅读(97)  评论(0编辑  收藏  举报