题目链接:Click here
题意:给定一串数字,求其上升子序列和的最大值。
思路:DP
状态转移方程:dp[j] = max(dp[i], dp[j]+a[i])(其中,j < i, a[j] < a[i])
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
using namespace std;
int main()
{
int n, dp[1005], a[1005], ans;
while(scanf("%d", &n) != EOF)
{
if(n == 0)break;
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
dp[i] = a[i]; //初始化
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if(a[i] > a[j])
dp[i] = max(dp[i], dp[j] + a[i]);
}
}
ans = -100000000;
for(int i = 0; i < n; i++)
ans = max(ans, dp[i]);
printf("%d\n", ans);
}
return 0;
}