一本通 1285:最大上升子序列和

最大上升子序列和

最长上升子序列的变式。

状态转移方程: if(ai>aj) fi = max(fi, fj+ai)

#include <iostream>
#include <cstdio>
using namespace std;
//Mystery_Sky
//
#define M 1010
int maxn, n;
int a[M], ans, f[M];
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]), f[i] = a[i];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j < i; j++) {
            if(a[i] > a[j]) f[i] = max(f[i], f[j] + a[i]);
        }
    }
    for(int i = 1; i <= n; i++) ans = max(ans, f[i]);
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Benjamin-cpp/p/10840579.html