AcWing 1016. 最大上升子序列和(线性DP)
题目链接
题目描述
一个数的序列 bi,当 b1
这些子序列中和最大为18,为子序列(1,3,5,9)的和。
你的任务,就是对于给定的序列,求出最大上升子序列和。
注意,最长的上升子序列的和不一定是最大的,比如序列(100,1,2,3)的最大上升子序列和为100,而最长上升子序列为(1,2,3)。
题目模型
- 最长上升子序列
题目代码
#include
#include
#include
using namespace std;
const int N = 1010;
int n;
int w[N];
int f[N];
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++ ) scanf("%d", &w[i]);
int res = 0;
for(int i = 0; i < n; i ++ )
{
f[i] = w[i];
for(int j = 0; j < i; j ++ )
if(w[i] > w[j])
f[i] = max(f[i], f[j] + w[i]);
res = max(res, f[i]);
}
printf("%d\n", res);
return 0;
}