#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
int n,k;
const int maxn=35;
pii arr[maxn];
vector<int>storage[maxn];
int memo[2*maxn][maxn][maxn][2];
int dp(int index, int take, int cur,bool amos){
if(index>2*n+5){
if(cur==0) return 0;
else return 1e18;
}
if(memo[index][take][cur][amos]!=-1) return memo[index][take][cur][amos];
int ans=1e18;
//establish a connecting point
if(!amos&&(!storage[index].empty()&&take)){
int nx=cur+storage[index].size();
int hold=min(nx,take);
nx-=hold;
//show(nx,nx);
//show(index,index);
ans=min(ans,dp(index,take,nx,1)+nx*k);
}
//get more connection
if(amos){
if(take<n)ans=min(ans,dp(index,take+1,cur,1)+storage[index][0]);
ans=min(ans,dp(index+1,take,cur,0));
}
else{
//bring everything up
int nx=cur+storage[index].size();
int hold=min(nx,take);
ans=min(ans,dp(index+1,take,nx-hold,0)+(nx-hold)*k);
}
//cout << index << " " << take << " " << cur << " " << ans << "\n";
return memo[index][take][cur][amos]=ans;
}
void solve(){
cin >> n >> k;
for(int x=0;x<n;x++){
cin >> arr[x].first >> arr[x].second;
storage[arr[x].first].push_back(arr[x].second);
}
for(int x=0;x<=n;x++) sort(storage[x].begin(),storage[x].end());
memset(memo,-1,sizeof(memo));
cout << dp(0,1,0,0);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | 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... |