#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};
ll solve(int i, int j, int k)
{
if (dp[i][j][k]!=-1) return dp[i][j][k];
if (i==mx-1) return max(j-k, 0)*mn[i-1];
dp[i][j][k]=1e18;
if (j<=k) dp[i][j][k]=solve(i+1, cnt[i+1], k);
else
{
for (int l=k; l<=j; l++)
{
dp[i][j][k]=min(dp[i][j][k], solve(i+1, cnt[i+1]+j-l, l)+(l-k)*mn[i-1]+(j-l)*mv);
}
}
return dp[i][j][k];
}
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});
for (int st=fmn.second; st<=fmn.second; st++)
{
res=0;
for (int i=1; i<=n; i++)
{
if (i==st||h[i]>h[st]) ch[i]=h[i];
else ch[i]=h[st]+1, res+=(ch[i]-h[i])*mv;
}
for (int i=0; i<mx; i++) cnt[i]=0, mn[i]=1e18;
for (int i=1; i<=n; i++) mn[ch[i]]=min(mn[ch[i]], c[i]), cnt[ch[i]]++;
for (int i=1; i<mx; i++) mn[i]=min(mn[i], mn[i-1]);
for (int i=0; i<mx; i++) for (int j=0; j<nx; j++) for (int k=0; k<nx; k++) dp[i][j][k]=-1;
ans=min(ans, res+solve(ch[st]+1, cnt[ch[st]+1], 1));
}
cout<<ans;
}
# | 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... |