#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define bl ' '
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define comp(v) (v).erase(unique(all(v)), (v).end())
#define lbd(v,x) lower_bound(all(v), (x))-(v).begin()
#define ubd(v,x) upper_bound(all(v), (x))-(v).begin()
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<int, pi> ipi;
typedef pair<pi, pi> pipi;
typedef pair<ll, ll> pll;
const int MAXN = 330;//100100*20;
const ll MOD = 1e9+7;
const ll INF = 2e18;
struct Point {
ll h, c;
}arr[MAXN];
vector<int> v;
ll dp[MAXN][MAXN][MAXN], mn[MAXN], k;
int cnt[MAXN];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n>>k;
for(int i=0; i<n; i++) cin>>arr[i].h>>arr[i].c;
sort(arr, arr+n, [&](Point &a, Point &b) {
return a.h!=b.h?a.h<b.h:a.c<b.c;
});
ll h = arr[0].h; mn[0] = arr[0].c; int x = 0;
for(int i = 0, idx = 0; i < n; i++) {
v.push_back(h);
while(idx < n && arr[idx].h == h) x++, idx++;
x--;
h = max(arr[i+1].h, h+1);
}
mn[0] = INT_MAX;
for(int i = 0, idx = 0; i < n; i++) {
while(idx < n && arr[idx].h == v[i]) {
cnt[i]++; mn[i]=min(mn[i], arr[idx].c);
idx++;
}
mn[i+1] = mn[i];
}
memset(dp, -1, sizeof dp);
for(int j=0; j<=n; j++) for(int k=0; k<=n; k++) dp[v.size()][j][k]=0;
for(int idx=(int)v.size()-1; idx>0; idx--) {
for(int h=0; h<=n; h++) {
for(int rem=0; rem<=n; rem++) {
if(h <= rem) dp[idx][h][rem] = dp[idx+1][cnt[idx+1]][rem];
else {
dp[idx][h][rem] = LLONG_MAX;
for(int i=0; i<=h-rem; i++)
dp[idx][h][rem] = min(dp[idx][h][rem], dp[idx+1][cnt[idx+1]+i][h-i]+k*i+mn[idx-1]*(h-rem-i));
}
}
}
}
cout<<dp[1][cnt[1]+cnt[0]-1][1]+k*(cnt[0]-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... |