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>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 3e5 + 5;
int a[maxn], N, K;
pair<ll, int> f[maxn][2];
pair<ll, int> best(pair<ll, int> a, pair<ll, int> b)
{
if(a.fi == b.fi) return a.se < b.se ? a : b;
return a.fi > b.fi ? a : b;
}
pair<ll, int> operator - (pair<ll, int> a, ll x)
{
return mp(a.fi - x, a.se + 1);
}
pair<ll, int> operator + (pair<ll, int> a, ll x)
{
return mp(a.fi + x, a.se);
}
pair<ll, int> solve(ll cost)
{
for(int i = 1; i <= N; ++i){
f[i][0] = best(f[i - 1][0], f[i - 1][1] - cost);
f[i][1] = best(f[i - 1][0], f[i - 1][1]) + a[i];
}
return best(f[N][0], f[N][1] - cost);
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N >> K;
for(int i = 1; i <= N; ++i){
cin >> a[i];
}
ll l = 0, r = 1e15, mid;
while(l <= r){
mid = (l + r) / 2;
if(solve(mid).se > K) l = mid + 1;
else r = mid - 1;
}
cout << solve(l).fi + l * K;
}
# | 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... |