Submission #1341461

#TimeUsernameProblemLanguageResultExecution timeMemory
1341461alexddSki 2 (JOI24_ski2)C++20
0 / 100
280 ms3408 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int MAXN = 305;
int n,k;
pair<int,int> v[305];
int dp[2][MAXN][MAXN];
int auxdp[MAXN][MAXN], auxdp2[MAXN][MAXN];

map<int, vector<int>> ofh;

void chmin(int&x, int y)
{
    x = min(x, y);
}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>k;
    int maxh = 0;
    vector<int> used;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i].first>>v[i].second;
        maxh = max(maxh, v[i].first);
        ofh[v[i].first].push_back(i);
        used.push_back(v[i].first);

        for(int d=-2;d<=2;d++)
            used.push_back(max(0LL, v[i].first + d));
    }
    for(int i=maxh+1;i<=maxh+n+3;i++)
        used.push_back(i);
    sort(used.begin(), used.end());
    used.erase(unique(used.begin(), used.end()), used.end());


    for(int cnt=0;cnt<=n;cnt++)
        for(int free=0;free<=n+1;free++)
            dp[1][cnt][free] = INF;

    int minc = INF, pas = 1;
    dp[1][0][1] = 0;
    for(int u=1;u+1<used.size();u++)
    {
        pas = 1 - pas;

        int h = used[u], difh = used[u] - used[u-1];

        int cur = ofh[h].size(), mincur = INF;
        for(int id:ofh[h])
            mincur = min(mincur, v[id].second);
        int newmin = min(minc, mincur);
        for(int cnt=0;cnt<=n;cnt++)
            for(int free=0;free<=n+1;free++)
            {
                dp[pas][cnt][free] = INF;

                auxdp2[cnt][free] = dp[1-pas][cnt][free];
                if(cnt >= 1) chmin(auxdp2[cnt][free], auxdp2[cnt-1][free]);

                auxdp[cnt][free] = INF;
                if(minc < INF)
                {
                    if(dp[1-pas][cnt][free] != INF) auxdp[cnt][free] = dp[1-pas][cnt][free] - free * minc;
                    if(free >= 1) auxdp[cnt][free] = min(auxdp[cnt][free], auxdp[cnt][free-1]);
                }
            }

        if(minc < INF)
        {
            for(int x=0;x<=n;x++)
            {
                for(int cnt=0;cnt<=n;cnt++)
                {
                    //x = cnt + dif
                    int dif = x - cnt;
                    assert(-cnt <= dif);
                    int lim = min(cur - dif - 1, n + 1);
                    if(lim >= 0)
                        chmin(dp[pas][x][cur - dif], auxdp[cnt][lim] + (cur - dif) * minc + x * k * difh);
                }
            }
        }


        for(int x=0;x<=n;x++)
        {
            for(int free=0;free<=n+1;free++)
            {
                int lim = min(n, x + free - cur);
                //x - cnt >= cur - free
                //cnt <= x - cur + free
                if(lim >= 0) chmin(dp[pas][x][free], auxdp2[lim][free] + x * k * difh);
            }
        }

        minc = newmin;
    }
    int rez = INF;
    for(int free=0;free<=n+1;free++)
        rez = min(rez, dp[pas][0][free]);
    assert(rez != INF);
    cout<<rez;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...