Submission #1305956

#TimeUsernameProblemLanguageResultExecution timeMemory
1305956tntStove (JOI18_stove)C++20
100 / 100
33 ms5820 KiB
#include <bits/stdc++.h> using namespace std; //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") #define pb push_back #define ll long long #define int long long //#define sz(v) int(v.size()) #define all(v) v.begin(),v.end() int mod = 1e9 + 7; const int N = 2e5 + 1; const ll inf = 2e18; void solve(){ int n,k; cin >> n >> k; int sum = n; int a[n + 1]; for(int i = 1; i <= n;i++){ cin >> a[i]; if(i != 1) sum += a[i] - a[i - 1] - 1; } multiset <int> mt; int cnt = 0,ans = sum; k--; for(int i = 1; i < n; i++){ if(mt.size() < k){ mt.insert(a[i + 1] - a[i] - 1); cnt += a[i + 1] - a[i] - 1; } else{ if(mt.size() > 0 && *mt.begin() < a[i + 1] - a[i] - 1){ cnt -= *mt.begin(); mt.erase(mt.begin()); cnt += a[i + 1] - a[i] - 1; mt.insert(a[i + 1] - a[i] - 1); } } } ans = min(ans,sum - cnt); cout << ans; } signed main(){ //freopen("time.in", "r", stdin); //freopen("time.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int t1 = 1; while(t1--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...