AcWing 3662. 最大上升子序列和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
using LL = long long;
const int N = 100010;
int n, w[N];
LL tr[N], f[N];
vector<int> xs;
int get(int x) {
return lower_bound(xs.begin(), xs.end(), x) - xs.begin();
}
int lowbit(int x) {
return x & -x;
}
void add(int i, LL v) {
for (; i <= n; i += lowbit(i))
tr[i] = max(tr[i], v);
}
LL query(int i) {
LL res = 0;
for (; i; i -= lowbit(i))
res = max(res, tr[i]);
return res;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &w[i]);
xs.push_back(w[i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
LL ans = 0;
for (int i = 0; i < n; ++i) {
int k = get(w[i]);
f[i] = query(k) + w[i];
ans = max(ans, f[i]);
add(k + 1, f[i]);
}
printf("%lld\n", ans);
return 0;
}