This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const ll inf=(ll)1e18;
int n,k;
int h[305],c[305];
ll dp[2][305][305]; // (pos,bal,unmatch)
vector<int> val[900505];
vector<pii> states[2];
int main(){
n=readint(); k=readint();
for(int i=1;i<=n;i++) h[i]=readint(),c[i]=readint();
vector<int> xs;
for(int i=1;i<=n;i++){
for(int j=0;j<=n;j++){
xs.pb(h[i]+j);
}
}
sort(xs.begin(),xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());
for(int i=1;i<=n;i++){
int p=(int)(lower_bound(xs.begin(),xs.end(),h[i])-xs.begin());
val[p].pb(c[i]);
}
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
dp[0][i][j]=inf;
dp[1][i][j]=inf;
}
}
dp[1][1][0]=0;
states[1].pb(mp(1,0));
int p=1,q=0;
int mn=-1;
for(int x=0;x<(int)xs.size();x++){
swap(p,q);
for(auto st:states[p]){
dp[p][st.fi][st.se]=inf;
}
states[p].clear();
int sz=(int)val[x].size();
for(auto st:states[q]){
int i=st.fi,j=st.se;
if(dp[q][i][j]==inf) continue;
int t=min(i,j+sz);
for(int f=0;f<=sz+j-t;f++){
if(mn==-1&&f>0) break;
if(dp[p][min(n,i+f)][min(n,(sz+j-t)-f)]==inf) states[p].pb(mp(min(n,i+f),min(n,(sz+j-t)-f)));
chkmin(dp[p][min(n,i+f)][min(n,(sz+j-t)-f)],dp[q][i][j]+k*1ll*j+f*1ll*mn);
}
}
for(auto p:val[x]) mn=(mn==-1?p:min(mn,p));
}
ll ans=(ll)1e18;
for(int i=1;i<=n;i++) ans=min(ans,dp[p][i][0]);
printf("%lld\n",ans);
return 0;
}
# | 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... |