-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarraySum2.cpp
More file actions
52 lines (43 loc) · 1.03 KB
/
MaximumSubarraySum2.cpp
File metadata and controls
52 lines (43 loc) · 1.03 KB
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
50
51
52
//Maximum Subarray Sum II - https://cses.fi/problemset/task/1644
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n, a, b;
cin >> n >> a >> b;
vector<ll> ps(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ps[i + 1] = ps[i] + x;
}
priority_queue<pair<ll, int>> pq;
for (int i = a; i < b; i++) {
pq.emplace(ps[i], i);
}
ll ans = -INF;
for (int right = b, left = 0; right <= n || left <= n - a; right++, left++) {
if (right <= n) {
pq.emplace(ps[right], right);
}
while (pq.top().second < left + a) {
pq.pop();
}
assert(!pq.empty());
auto [max_sum, index] = pq.top();
ans = max(ans, max_sum - ps[left]);
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}