forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
49 lines (45 loc) · 1.14 KB
/
Solution.cpp
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
44
45
46
47
48
49
class BinaryIndexedTree {
private:
int n;
vector<long long> c;
const long long inf = 1e18;
public:
BinaryIndexedTree(int n) {
this->n = n;
c.resize(n + 1, -inf);
}
void update(int x, long long v) {
while (x <= n) {
c[x] = max(c[x], v);
x += x & -x;
}
}
long long query(int x) {
long long mx = -inf;
while (x > 0) {
mx = max(mx, c[x]);
x -= x & -x;
}
return mx;
}
};
class Solution {
public:
long long maxBalancedSubsequenceSum(vector<int>& nums) {
int n = nums.size();
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
arr[i] = nums[i] - i;
}
sort(arr.begin(), arr.end());
arr.erase(unique(arr.begin(), arr.end()), arr.end());
int m = arr.size();
BinaryIndexedTree tree(m);
for (int i = 0; i < n; ++i) {
int j = lower_bound(arr.begin(), arr.end(), nums[i] - i) - arr.begin() + 1;
long long v = max(tree.query(j), 0LL) + nums[i];
tree.update(j, v);
}
return tree.query(m);
}
};