http://acm.hdu.edu.cn/showproblem.php?pid=1087
题意:给你一段序列,求最长上升子序列和。
思路:跟poj2533一个思路,只不过那题求长度+1,这题求和加上当前元素即可。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int dp[N], a[N];
int main()
{
// freopen("in.txt", "r", stdin);
int n;
while(~scanf("%d", &n))
{
if(n == 0) break;
for(int i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
}
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i ++)
{
int sum = 0;
for(int j = 0; j < i; j ++)
{
if(a[i] > a[j])
{
sum = max(sum, dp[j]);
}
}
dp[i] = sum+a[i];
}
int maxx = -INF;
for(int i = 0; i < n; i ++)
{
maxx = max(maxx, dp[i]);
}
printf("%d\n", maxx);
}
return 0;
}