Submission #545505

#TimeUsernameProblemLanguageResultExecution timeMemory
545505MohamedAliSaidaneStove (JOI18_stove)C++14
50 / 100
124 ms100172 KiB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

#define pb push_back
#define popb pop_back
#define pf push_front
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define ff first
#define ss second

int nx[4] = {0,0,1,-1}, ny[4] = {1,-1,0,0};
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);}

const int MAX_N = 5004;
int n, k;
vll A;
int dp[MAX_N][MAX_N];
int f(int i, int j)
{
    if(dp[i][j] != -1)
        return dp[i][j];
    if(i == n-1)
        return dp[i][j] = 0;
    int ans = f(i+1,j) + A[i+1] - A[i];
    if(j > 0)
        return dp[i][j] = min(ans,f(i+1,j-1)+1);
    return dp[i][j] = ans;
}
void solve()
{
    memset(dp,-1,sizeof(dp));
    cin >> n >> k;
    A.assign(n,0);
    for(int i = 0; i < n ; i ++)
        cin >> A[i];
    sort(all(A));
    cout << f(0,k-1) + 1;

}

int main()
{
    ios::sync_with_stdio(false);
    int tt  = 1;
    while(tt--)
        solve();
}



#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...