Submission #981175

#TimeUsernameProblemLanguageResultExecution timeMemory
981175beabossFeast (NOI19_feast)C++14
100 / 100
141 ms15232 KiB
// Source: https://oj.uz/problem/view/NOI19_feast // #include "bits/stdc++.h" using namespace std; #define FOR(i, a, b) for (ll i = a; i < b; i++) typedef long long ll; typedef pair<ll, ll> pii; #define s second #define f first const ll N = 3e5 + 10; ll a[N]; pii dp[N][2]; ll n, k; pii solve(ll l) { dp[0][0] = {0, 0}; dp[0][1] = {a[0] - l, 1}; FOR(i, 1, n) { dp[i][0] = max(dp[i-1][0], dp[i-1][1]); dp[i][1] = max(make_pair(dp[i-1][0].f + a[i] - l, dp[i-1][0].s + 1), make_pair(dp[i-1][1].f + a[i], dp[i-1][1].s)); } return max(dp[n-1][1], dp[n-1][0]); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; FOR(i, 0 ,n) cin >> a[i]; ll lo = 0; ll hi = 1e18; while (lo < hi) { ll m = (lo + hi + 1)/2; pii res = solve(m); if (res.s >= k) lo = m; else hi = m -1; } // cout << lo << solve(lo).f << endl; cout << solve(lo).f + k * lo << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...