#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <sstream>
#include <chrono>
#include <random>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define EB emplace_back
#define PoB pop_back
#define LOG log2
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define fch(t, v) for (auto t : v)
#define sz(x) int(x.size())
#define rsz resize
#define gp(x) vector<vector<x>>
#define btree vector<pii>
#define vll vector<ll>
#define Max(a, b, c) max(max(a,b),c)
#define fMax(a, b, c, d) max(Max(a, b, c), dp)
#define Min(a, b, c) min(min(a,b),c)
#define Mid(a, b, c) max(min(a, b), min(max(a, b), c))
#define st(a) set<a>
#define gi greater<int>
#define all(x) (x).begin(),(x).end()
#define tri(x) tuple<x,x,x>
#define pil pair<int, long>
#define ull unsigned long long
#define eps 1e-9
//#define debug(x) cout << '>' << #x << ':' << x << endl;
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
void println() {cerr << ">--------------------<" << endl;}
void printm(vector<vector<int>>& mat) {
cerr << "matrix: " << endl;
for (int i = 0; i<(int)mat.size(); i++) {for (int j = 0; j<(int)mat[0].size(); j++) {cerr << mat[i][j] << " ";} cerr << endl;}
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
// templates
template <class T> bool ckmin(T &a, const T &b) {return b<a ? a = b, 1 : 0;}
template <class T> bool ckmax(T &a, const T &b) {return b>a ? a = b, 1 : 0;}
mt19937_64 rng_ll(chrono::steady_clock::now().time_since_epoch().count());
template <class T> using vc = vector<T>;
template <class T> using gr = greater<T>;
template <class T> using p_q = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vc<T>, gr<T>>;
template <class T1, class T2> using pr = pair<T1, T2>;
int rng(int M) {return (int)(rng_ll()%M);}
constexpr int INF = (int)2e9;
constexpr int MOD = 998244353;
constexpr ll LL_INF = (ll)3e18;
constexpr int mod = (int)1e9 + 7;
constexpr ll inverse = 500000004LL; // inverse of 2 modulo 1e9 + 7
void setIO(const string& str) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (str.empty()) return;
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
int N, K;
vi t;
vc<pii> blocks;
int main() { // TIME YOURSELF !!!
setIO("");
cin >> N >> K;
t.rsz(N);
for (int i = 0; i < N; ++i) {
cin >> t[i];
}
int l = -1, r = -1;
for (int i = 0; i < N; ++i) {
if (t[i] == r) {
r = t[i] + 1;
} else {
if (l != -1) blocks.PB({l, r});
l = t[i], r = t[i] + 1;
}
}
if (l != -1) blocks.PB({l, r});
int sum = 0;
int M = sz(blocks);
for (int i = 0; i < M; ++i) {
sum += (blocks[i].ss - blocks[i].ff);
}
pqg<int> pq;
for (int i = 0; i < M - 1; ++i) {
int dist = blocks[i+1].ff - blocks[i].ss;
pq.push(dist);
}
while (!pq.empty() && M > K) {
sum += pq.top();
pq.pop();
M--;
}
cout << sum;
return 0;
}
// CHECK LONG LONGS, binary search on ans?
// 5000 * 5000 size matrices are kinda big (potential mle)
// Do something, start simpler
// IBM motto: THINK
Compilation message
stove.cpp: In function 'void setIO(const string&)':
stove.cpp:115:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:116:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
20 ms |
3284 KB |
Output is correct |
17 |
Correct |
20 ms |
3288 KB |
Output is correct |
18 |
Correct |
20 ms |
3288 KB |
Output is correct |
19 |
Correct |
21 ms |
3288 KB |
Output is correct |
20 |
Correct |
15 ms |
3288 KB |
Output is correct |
21 |
Correct |
12 ms |
3288 KB |
Output is correct |
22 |
Correct |
11 ms |
3396 KB |
Output is correct |
23 |
Correct |
11 ms |
3320 KB |
Output is correct |
24 |
Correct |
11 ms |
3284 KB |
Output is correct |