Skip to content

Commit 83809d0

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent ed7702e commit 83809d0

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

content/6_Advanced/Lagrange.mdx

+36-36
Original file line numberDiff line numberDiff line change
@@ -120,42 +120,42 @@ using namespace std;
120120

121121
#define ll long long
122122

123-
int main(){
124-
int n, k;
125-
cin >> n >> k;
126-
127-
int a[n];
128-
for (int &i : a) cin >> i;
129-
130-
/*
131-
* Returns {maximum sum, subarrays used (max if ties)}
132-
* if creating a subarray penalizes the sum by "lmb" and
133-
* there is no limit to the number of subarrays you can create
134-
*/
135-
auto solveLambda = [&](ll lmb){
136-
pair<ll, ll> dp[n][2];
137-
138-
dp[0][0] = {0, 0};
139-
dp[0][1] = {a[0] - lmb, 1};
140-
141-
for (int i = 1; i < n; i++){
142-
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
143-
144-
dp[i][1] = max(
145-
make_pair(dp[i - 1][0].first + a[i] - lmb, dp[i - 1][0].second + 1),
146-
make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second)
147-
);
148-
}
149-
return max(dp[n - 1][0], dp[n - 1][1]);
150-
};
151-
152-
ll lo = 0, hi = 1e18;
153-
154-
while (lo < hi){
155-
ll mid = (lo + hi) / 2;
156-
solveLambda(mid).second <= k ? hi = mid : lo = mid + 1;
157-
}
158-
cout << solveLambda(lo).first + lo * k << endl;
123+
int main() {
124+
int n, k;
125+
cin >> n >> k;
126+
127+
int a[n];
128+
for (int &i : a) cin >> i;
129+
130+
/*
131+
* Returns {maximum sum, subarrays used (max if ties)}
132+
* if creating a subarray penalizes the sum by "lmb" and
133+
* there is no limit to the number of subarrays you can create
134+
*/
135+
auto solveLambda = [&](ll lmb) {
136+
pair<ll, ll> dp[n][2];
137+
138+
dp[0][0] = {0, 0};
139+
dp[0][1] = {a[0] - lmb, 1};
140+
141+
for (int i = 1; i < n; i++) {
142+
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
143+
144+
dp[i][1] =
145+
max(make_pair(dp[i - 1][0].first + a[i] - lmb,
146+
dp[i - 1][0].second + 1),
147+
make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second));
148+
}
149+
return max(dp[n - 1][0], dp[n - 1][1]);
150+
};
151+
152+
ll lo = 0, hi = 1e18;
153+
154+
while (lo < hi) {
155+
ll mid = (lo + hi) / 2;
156+
solveLambda(mid).second <= k ? hi = mid : lo = mid + 1;
157+
}
158+
cout << solveLambda(lo).first + lo * k << endl;
159159
}
160160
```
161161

0 commit comments

Comments
 (0)