This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e18;
const int N = 3e5 + 7;
int n, k, a[N];
pair<int, int> dp[N][2];
pair<int, int> calc(int pen) {
for(int i = 1; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se),
make_pair(dp[i - 1][0].fi + a[i] - pen, dp[i - 1][0].se + 1));
}
return max(dp[n][0], dp[n][1]);
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
dp[0][0] = dp[0][1] = {0, 0};
int l = 0, r = 1e18;
int ans;
while(l <= r) {
int m = (l + r) >> 1;
if(calc(m).se >= k) {
ans = m;
l = m + 1;
} else r = m - 1;
}
cout << calc(ans).fi + k *ans;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:41:30: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | cout << calc(ans).fi + k *ans;
| ~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |