#include <bits/stdc++.h>
using namespace std;
#define el "\n"
#define FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define ll long long
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 1e5 + 2;
const ll inf = 1e16 + 2;
int n, k, a[N];
void inp()
{
cin >> n >> k; FOR(i, 1, n) cin >> a[i];
}
/* Try your best
No regrets */
namespace subtask_12
{
ll dp[5005][5005];
ll st[4 * N];
void build(int id, int l, int r, int t)
{
if(l == r) {
st[id] = dp[l - 1][t] - a[l];
return;
}
int mid = (r + l) >> 1;
build(id << 1, l, mid, t);
build(id << 1 | 1, mid + 1, r, t);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
ll get(int id, int l, int r, int u, int v)
{
if(r < u || v < l) return inf;
if(u <= l && r <= v) return st[id];
int mid = (r + l) >> 1;
return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
void slv()
{
sort(a + 1, a + n + 1);
FOR(i, 0, n) FOR(j, 0, k) dp[i][j] = inf;
dp[0][0] = 0;
FOR(j, 1, k) {
build(1, 1, n, j - 1);
FOR(i, 1, n) {
dp[i][j] = get(1, 1, n, 1, i) + a[i] + 1;
// FOR(t, 1, i) mini(dp[i][j], dp[t - 1][j - 1] + a[i] + 1 - a[t]);
}
}
ll ans = inf;
FOR(i, 1, k) mini(ans, dp[n][i]);
cout << ans;
}
}
namespace subtask_3
{
void slv()
{
sort(a + 1, a + n + 1);
vector<ll> diff;
diff.push_back(-inf);
FOR(i, 2, n) diff.push_back(a[i] - a[i - 1] - 1);
sort(all(diff));
ll res = n;
FOR(i, 1, n - k) {
res += diff[i];
}
cout << res;
}
}
/* Code slowly, think carefully */
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define __Azul__ "stove"
if(fopen(__Azul__".inp", "r")) {
freopen(__Azul__".inp", "r", stdin);
freopen(__Azul__".out", "w", stdout);
}
bool qs = 0;
int T = 1;
if(qs) cin >> T;
while(T--) {
inp();
subtask_3::slv();
}
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
stove.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main()
| ^~~~
stove.cpp: In function 'int main()':
stove.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(__Azul__".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(__Azul__".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |