-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2355.cpp
More file actions
25 lines (24 loc) · 798 Bytes
/
2355.cpp
File metadata and controls
25 lines (24 loc) · 798 Bytes
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
class Solution {
public:
long long maximumBooks(vector<int>& books) {
int n = books.size();
vector<long long> dp(n, 0);
stack<int> stk;
long long res = 0;
for (int i = 0; i < n; ++i) {
while (!stk.empty() && books[stk.top()] > books[i] - (i - stk.top())) stk.pop();
if (!stk.empty()) {
long long j = stk.top();
long long L = i - j;
dp[i] = dp[j] + ((long long)books[i] + (books[i] - L + 1)) * L / 2;
}
else {
long long L = min(i + 1, books[i]);
dp[i] = ((long long)books[i] + (books[i] - L + 1)) * L / 2;
}
stk.push(i);
res = max(res, dp[i]);
}
return res;
}
};