/*
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma,tune=native")
//*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int ,int > pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 1e5+10;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n , k;
int a[maxn] , sz[maxn];
int l[maxn] , r[maxn];
int par[maxn];
vector < int > e;
bool cmp(int i , int j){
return(a[i + 1] - a[i] > a[j + 1] - a[j]);
}
int getpar(int v){
return((v == par[v]) ? v : par[v] = getpar(par[v]));
}
void merge(int v , int u){
v = getpar(v) , u = getpar(u);
if(sz[v] < sz[u])
swap(u , v);
sz[v] += sz[u];
par[u] = v;
sz[u] = 0;
l[v] = min(l[v] , l[u]);
r[v] = max(r[v] , r[u]);
}
int32_t main(){
migmig;
cin >> n >> k;
for(int i = 1 ; i <= n; i ++)
cin >> a[i] , par[i] = i , l[i] = r[i] = a[i], e.pb(i) , sz[i] = 1;
e.pop_back();
int cnt = n ;
sort(e.begin() , e.end() , cmp);
while(cnt!=k){
cnt --;
int u = e.back();
e.pop_back();
merge(u , u + 1);
}
ll ans = 0;
for(int i = 1 ; i <= n ; i ++)
if(sz[i])ans += r[i] - l[i] + 1;
cout << ans;
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
32 ms |
3196 KB |
Output is correct |
17 |
Correct |
32 ms |
3196 KB |
Output is correct |
18 |
Correct |
31 ms |
3196 KB |
Output is correct |
19 |
Correct |
31 ms |
3196 KB |
Output is correct |
20 |
Correct |
30 ms |
3196 KB |
Output is correct |
21 |
Correct |
31 ms |
3196 KB |
Output is correct |
22 |
Correct |
28 ms |
3196 KB |
Output is correct |
23 |
Correct |
28 ms |
3196 KB |
Output is correct |
24 |
Correct |
28 ms |
3196 KB |
Output is correct |