AcWing1016 最大上升子序列和
原题链接
其实就是在原本的Lis的基础上,将求的数量变成了求最大的和
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int) (x).size()
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> VI;
typedef double db;
const int N = 1010;
int n, a[N], f[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int res = -1;
for (int i = 1; i <= n; i++) {
f[i] = a[i];
for (int j = 1; j < i; j++) {
if (a[i] > a[j]) f[i] = max(f[i], f[j]+a[i]);
}
res = max(res, f[i]);
}
cout << res << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
浙公网安备 33010602011771号