#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const ll nx=301, mx=601, inf=1e18;
ll n, mv, h[nx], ch[nx], c[nx], dp[mx][nx][nx], cnt[mx], mn[mx], res, ans=inf;
pair<pair<ll, ll>, ll> fmn={{inf, inf}, inf};
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>mv;
for (int i=1; i<=n; i++) cin>>h[i]>>c[i], fmn=min(fmn, {{h[i], c[i]}, i});
int st=fmn.second;
for (int i=1; i<=n; i++) if (!(i==st||h[i]>h[st])) res+=(h[st]+1-h[i])*mv, h[i]=h[st]+1;
for (int i=0; i<mx; i++) mn[i]=1e18;
for (int i=1; i<=n; i++) mn[h[i]]=min(mn[h[i]], c[i]), cnt[h[i]]++;
for (int i=1; i<mx; i++) mn[i]=min(mn[i], mn[i-1]);
for (int i=0; i<mx-1; i++) for (int j=0; j<nx; j++) for (int k=0; k<nx; k++) dp[i][j][k]=1e18;
for (int i=mx-2; i>h[st]; i--)
{
for (int j=0; j<=n; j++)
{
for (int k=j+1; k<=n; k++) dp[i][j][k]=dp[i+1][cnt[i+1]][k];
for (int k=0; k<=j; k++)
{
for (int l=k; l<=j; l++)
{
dp[i][j][k]=min(dp[i][j][k], dp[i+1][cnt[i+1]+j-l][l]+(l-k)*mn[i-1]+(j-l)*mv);
}
}
//for (int k=0; k<=n; k++) if (i<=4) cout<<i<<' '<<j<<' '<<k<<' '<<dp[i][j][k]<<'\n';
}
}
cout<<res+dp[h[st]+1][cnt[h[st]+1]][1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |