这一题,如果直接使用单调队列做计算,在数的覆盖上,不好做取舍,所以这种题目用最原始的DP方法,枚举到的数为a[i],此时再遍历一次从0到i - 1中所有小于i的数a[k],对应的和f[k] + a[i]后的最大值即可,记为f[i]。
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, res;
int a[N], f[N];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
f[i] = a[i];
for (int j = 0; 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;
}