제출 #699069

#제출 시각아이디문제언어결과실행 시간메모리
699069RanStove (JOI18_stove)C++17
100 / 100
117 ms82612 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 100;

long long n, k, a[N], sum[N], c[N];

long long dp[5020][5020];

void sub2()
{
    sort(a + 1, a + 1 + n);
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++)
        dp[i][0] = 1e18;
    for (int j = 1; j <= k; j++)
    {
        long long ok = a[1] - dp[0][j - 1];
        for (int i = 1; i <= n; i++)
        {
            dp[i][j] = (a[i] + 1) - ok;
            ok = max(ok, a[i + 1] - dp[i][j - 1]);
        }
    }
    cout << dp[n][k];
}

void sub()
{
    sort(a + 1, a + 1 + n);
    int m = 0;
    for (int i = 1; i <= n; i++)
        if (m == 0 || c[m] != a[i])
        {
            m++;
            c[m] = a[i];
        }
    vector <long long> b;
    for (int i = 2; i <= m; i++)
        b.push_back(c[i] - c[i - 1] - 1);
    sort(b.begin(), b.end());
    long long ans = a[n] + 1 - a[1];
    for (int i = b.size() - 1; i >= 0; i--)
    {
        if (k == 1)
            break;
        ans = ans - b[i];
        k--;
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    if (n <= 5000)
        sub2();
    else
        sub();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...