-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum_Subarray_Sum_II.cpp
More file actions
33 lines (31 loc) · 1.02 KB
/
Maximum_Subarray_Sum_II.cpp
File metadata and controls
33 lines (31 loc) · 1.02 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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i, k, e) for (int i = k; i < e; i++)
#define dbg(x) cout << #x << " = " << x << endl
/*
goal = maximize the sum of elements in a window
when storing prefix sums : optimal to subtract prefix sum smallest in the window(sorted set)
storing of prefix sums in window implies we are always calculating subarray sum
in contrast to storing of values in window(here we may not be calculating sum of a contiguous sub array)
*/
int32_t main() {
int n, a, b;
cin >> n >> a >> b;
vector<int> nums(n + 1);
forn(i, 1, n + 1) cin >> nums[i];
vector<int> pref(n + 1, 0);
forn(i, 1, n + 1) pref[i] += nums[i] + pref[i - 1];
int ans = LLONG_MIN;
set<pair<int, int>> window;
int left = 0;
int start = 0;
forn(i, a, n + 1) {
window.insert({pref[left], left++});
ans = max(ans, pref[i] - window.begin()->first);
if (i - start >= b)
window.erase({pref[start], start++});
}
cout << ans;
return 0;
}