#include <bits/stdc++.h>
using namespace std;
long long n,m,i,j,k,t[100001],kq,d[1001],f[5001][5001];
void vet(int i,int sl)
{
if(sl>m) return ;
if(i>n)
{
long long sum=0,vt;
for(int j=1;j<=n;j++)
{
if(d[j]==1) vt=t[j];
if(j==n || d[j+1]==1) sum+=t[j]+1-vt;
}
kq=min(kq,sum);
return ;
}
if(i==1) { d[i]=1; vet(i+1,sl+1); }
else
{
for(int j=0;j<=1;j++)
{
d[i]=j;
vet(i+1,sl+j);
}
}
}
void sub1()
{
kq=round(1e9);
vet(1,0);
cout<<kq;
}
//////////////////////////////////////////////////////////////
const long long oo=round(1e18);
void sub2()
{
for(i=0;i<=n;i++)
for(k=0;k<=m;k++) f[i][j]=oo;
f[0][0]=0;
for(k=1;k<=n;k++)
{
long long mi=oo;
for(i=0;i<=n;i++)
{
if(i>0 && mi<oo) f[i][k]=mi+t[i]+1;
if(i<n && f[i][k-1]<oo) mi=min(mi,f[i][k-1]-t[i+1]);
}
}
cout<<f[n][m];
}
/////////////////////////////////////////////////////////////////////
int b[100001];
void sub3()
{
int sl=0;
for(i=2;i<=n;i++) b[++sl]=t[i]-(t[i-1]+1);
sort(b+1,b+sl+1);
long long sum=0;
for(i=sl;i>=sl-(m-1)+1;i--) sum+=b[i];
cout<<t[n]+1-t[1]-sum;
}
int main()
{
//freopen("stove.inp","r",stdin);
//freopen("stove.out","w",stdout);
cin>>n>>m;
for(i=1;i<=n;i++) cin>>t[i];
if(n<=20) sub1();
else if(n<=5000) sub2();
else sub3();
}
Compilation message
stove.cpp: In function 'void vet(int, int)':
stove.cpp:13:46: warning: 'vt' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(j==n || d[j+1]==1) sum+=t[j]+1-vt;
~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
424 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
24 ms |
372 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
424 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
24 ms |
372 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
256 ms |
82968 KB |
Output is correct |
11 |
Correct |
264 ms |
82936 KB |
Output is correct |
12 |
Correct |
247 ms |
82936 KB |
Output is correct |
13 |
Correct |
261 ms |
83052 KB |
Output is correct |
14 |
Correct |
281 ms |
82812 KB |
Output is correct |
15 |
Correct |
235 ms |
82932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
424 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
24 ms |
372 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
256 ms |
82968 KB |
Output is correct |
11 |
Correct |
264 ms |
82936 KB |
Output is correct |
12 |
Correct |
247 ms |
82936 KB |
Output is correct |
13 |
Correct |
261 ms |
83052 KB |
Output is correct |
14 |
Correct |
281 ms |
82812 KB |
Output is correct |
15 |
Correct |
235 ms |
82932 KB |
Output is correct |
16 |
Correct |
82 ms |
1528 KB |
Output is correct |
17 |
Correct |
82 ms |
1484 KB |
Output is correct |
18 |
Correct |
89 ms |
1500 KB |
Output is correct |
19 |
Correct |
82 ms |
1528 KB |
Output is correct |
20 |
Correct |
89 ms |
1520 KB |
Output is correct |
21 |
Correct |
81 ms |
1528 KB |
Output is correct |
22 |
Correct |
82 ms |
1496 KB |
Output is correct |
23 |
Correct |
82 ms |
1528 KB |
Output is correct |
24 |
Correct |
84 ms |
1436 KB |
Output is correct |