제출 #1337413

#제출 시각아이디문제언어결과실행 시간메모리
1337413nagorn_phStove (JOI18_stove)C++20
100 / 100
19 ms2372 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset <int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update>

#define int long long
#define double long double
#define pii pair <int, int>
#define tiii tuple <int, int, int>
#define tiiii tuple <int, int, int, int>
#define emb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define iShowSpeed cin.tie(NULL)->sync_with_stdio(false)
#define matrix vector <vector <int>>
#define mat(n, m) vector <vector <int>> (n, vector <int> (m));

const int mod = 1e9 + 7;
const int inf = 1e18;
const matrix II = {{1, 0}, {0, 1}};

void solve(){
    int n, k; cin >> n >> k;
    vector <int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i];
    priority_queue <int> pq;
    for (int i = 1; i < n; i++) pq.emplace(a[i + 1] - a[i] - 1);
    int sum = a[n] + 1 - a[1];
    while (k-- > 1) sum -= pq.top(), pq.pop();
    cout << sum;
}

int32_t main(){
    iShowSpeed;
    // int q; cin >> q; while (q--) 
    solve();
}

/*
O(nk) : dp[i] = min(dp[j] + a[i] + 1 - a[j + 1])
              = a[i] + 1 + min(dp[j] - a[j + 1]) ; j < i
how to optimize to O(n)???
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...