This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
long long n,m,i,j,k,t[5001],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 main()
{
//freopen("stove.inp","r",stdin);
//freopen("stove.out","w",stdout);
cin>>n>>m;
for(i=1;i<=n;i++) cin>>t[i];
sub2(); return 0;
if(n<=20) sub1();
else sub2();
}
Compilation message (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |