제출 #1033699

#제출 시각아이디문제언어결과실행 시간메모리
1033699vjudge1Stove (JOI18_stove)C++17
100 / 100
16 ms2376 KiB
#ifdef LOCAL
#include "D:\debug.h"
#else
#define cebug(...) "anHiepORZ"
#endif

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> 

//less: set
//less_equal: multiset (ko dung dc ham ...)

#define fi first
#define se second
#define pb push_back

//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left
//COUNTDOWN: 9 days left


#define int long long

const int N =  1e5 + 10;
const int mod = 1e9 + 7;
const int oo = 1e18;

int n, k;
int a[N];

void solve(){
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    int s = 1;
    int ans = 0;
    vector<int> blk;
    int sm = 0;
    for(int i = 1; i <= n ; i++){
        if(a[i] + 1 != a[i + 1]){
            ans += (i - s + 1);
            sm++;
            if(i != n) blk.push_back(a[i + 1] - a[i] - 1);
            s = i + 1;
        }
    }
    int need = sm - k;
    if(need <= 0) cout << ans;
    else{
        sort(blk.begin(), blk.end());
        for(int i = 0; i < need; i++) ans+= blk[i];
        cout << ans; 
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int tc; tc = 1;
    while(tc--)
        solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...