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>
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using lint=long long;
mt19937 rng(time(NULL));
lint Power(lint A,lint B,lint C){
if(!B) return 1;
lint tmp=Power(A,B>>1,C);
return tmp*tmp%C*(B&1?A:1)%C;
}
int N,K;
int A[100000],Memo[5005][5005];
int DP(int Pos,int Matches){
if(Pos>=N) return 0;
if(Matches==0) return A[N-1]-A[Pos-1]+1;
int &Ret=Memo[Pos][Matches];
if(Ret!=0) return Ret;
Ret=INT_MAX;
for(int i=Pos;i<N;++i)
Ret=min(Ret,DP(i+1,Matches-1)+A[i]-A[Pos]+1);
return Ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>N>>K;
for(int i=0;i<N;++i) cin>>A[i];
cout<<DP(0,K)<<"\n";
return 0;
}
/*
,,aaaaa,,
,dP"'' `""""Ya,
,aP"' `"Yb,
,8"' `"8a,
,8" `"8,
,8" "Yb,
,8" `8,
dP' 8I
,8" bg, ,P'
,8' "Y8"Ya,,,,ad"
,d" a, I8 `"""'
,8' ""888
dP `Yb,
dP' ,d8P::::Y8b, `Ya
,adba8',d88P::;;::;;;:"b:::Ya, Ya
dP":::"Y88P:;P"""YP"""Yb;::::::"Ya, "Y,
8:::::::Yb;d" _ "_ dI:::::::::"Yb, ,,gd88ba,db
Yb:::::::"8(,8P _d8 d8:::::::::::::Y88P"::::::Y8I
`Yb;:::::::""::"":b,,dP::::::::::::::::::;aaa;:::8(
`Y8a;:::::::::::::::::::::;;::::::::::8P""Y8)::8I
8b"ba::::::::::::::::;adP:::::::::::":::dP::;8'
`8b;::::::::::::;aad888P::::::::::::::;dP::;8'
`8b;::::::::""""88" d::::::::::b;:::::;;dP'
"Yb;::::::::::Y8bad::::::::::;"8Paaa""'
`"Y8a;;;:::::::::::::;;aadP""
``""Y88bbbdddd88P""8b,
_,d8"::::::"8b,
,dP8"::::::;;:::"b,
,dP"8:::::::Yb;::::"b,
,8P:dP:::::::::Yb;::::"b,
_,dP:;8":::::::::::Yb;::::"b
,aaaaaa,,d8P:::8":::::::::::;dP:::::;8
,ad":;;:::::"::::8"::::::::::;dP::::::;dI
dP";adP":::::;:;dP;::::aaaad88"::::::adP:8b,
d8:::8;;;aadP"::8'Y8:d8P"::::::::::;dP";d"'`Yb:"b
8I:::;""":::::;dP I8P"::::::::::;a8"a8P" "b:P
Yb::::"8baa8"""' 8;:;d"::::::::d8P"' 8"
"YbaaP::8;P `8;d::;a::;;;;dP ,8
`"Y8P"' Yb;;d::;aadP" ,d'
"YP:::"P' ,d'
"8bdP' _ ,8'
,8"`""Yba,d" ,d"
,P' d"8' ,d"
,8' d'8' ,P'
(b 8 I 8,
Y, Y,Y, `b,
____ "8,__ `Y,Y, `Y""b,
,adP""""b8P""""""""Ybdb, Y,
,dP" ,dP' `"" `8
,8" ,P' ,P
8' 8) ,8'
8, Yb ,aP'
`Ya Yb ,ad"'
"Ya,___ "Ya ,ad"'
``""""""`Yba,,,,,,,adP"'
`"""""""'
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |