题解 | 最大上升子序列和

最大上升子序列和

https://www.nowcoder.com/practice/dcb97b18715141599b64dbdb8cdea3bd

#include <iostream>

using namespace std;
#define N 1010
int a[N];   // 存储原数据
int f[N];   // f[i] : 以a[i]结尾的最长上升子序列和
int n;

int main()
{
    int res = -1e8;
    cin >> n;
    for(int i = 1; i <= n; ++i){
        scanf("%d",&a[i]);
    }
    for(int i = 1; i <= n; ++i){
        // 遍历1 ~ i-1, 并求出以a[i]结尾的最大上升序列和
        f[i] = a[i];
        for(int j = 1; j < i; ++j){
            if(a[j] < a[i]){
                f[i] = max(f[i],f[j] + a[i]);
            }
        }
        res = max(res,f[i]);
    }
    cout << res << endl;

    return 0;
}


全部评论

相关推荐

05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务