Submission #41091

#TimeUsernameProblemLanguageResultExecution timeMemory
41091IvanCStove (JOI18_stove)C++14
100 / 100
35 ms3056 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> ii; const int MAXN = 1e5 + 10; ll vetor[MAXN],N,K; ii dp[MAXN]; ii solve(ll W){ dp[0] = ii(0,0); ii best = ii(-vetor[1] + 1,0); for(ll i = 1;i<=N;i++){ dp[i] = ii(best.first + vetor[i] + W,best.second - 1); best = min(best, ii(-vetor[i+1] + 1 + dp[i].first,dp[i].second) ); } return dp[N]; } int main(){ cin.tie(0);ios_base::sync_with_stdio(0); cin >> N >> K; for(ll i = 1;i<=N;i++) cin >> vetor[i]; ll ini = 0, fim = (ll)2*1e9, resp = -1,meio; while(ini <= fim){ meio = (ini+fim)/2; if(abs(solve(meio).second) >= K){ resp = meio; ini = meio + 1; } else{ fim = meio - 1; } } cout << solve(resp).first - K*resp << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...