# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1123475 | btninh | Feast (NOI19_feast) | C++17 | 1096 ms | 10824 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define TASK "A"
const int N = 3e5 + 5;
int n, k, a[N];
pair<ll,ll> dp[N][2];
pair<ll,ll> Calc(ll lmd){
dp[1][0] = {0, 0};
dp[1][1] = {a[1] - lmd, 1};
for(int i = 2; 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] - lmd, dp[i - 1][0].se + 1));
}
return max(dp[n][0], dp[n][1]);
}
signed main()
{
if(fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> a[i];
ll l = 0, r = 1e18;
while (l < r)
{
ll mid = (l + r) >> 1LL;
if (Calc(mid).se >= k) l = mid;
else r = mid - 1;
}
cout << Calc(l).fi + l * k;
}
Compilation message (stderr)
# | 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... |