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>
using namespace std;
long long dp[301][601][301];
long long wow[301][601][301];
deque<pair<long long,long long>> troll[601][301];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n,br,h,c,ans = LLONG_MAX;
cin >> n >> br;
vector<pair<long long,long long>> haha(n+1,{-1,-1});
vector<long long> idk(601,LLONG_MAX/10);
for(long long i = 0; i < n; i++) {
cin >> h >> c;
idk[h] = min(idk[h],c);
haha[i+1] = {h,c};
}
for(long long i = 1; i <= 600; i++) {
idk[i] = min(idk[i],idk[i-1]);
}
sort(haha.begin(),haha.end());
for(long long i = 0; i < 301; i++) {
for(long long j = 0; j < 601; j++) {
for(long long k = 0; k < 301; k++) {
dp[i][j][k] = LLONG_MAX/10;
wow[i][j][k] = LLONG_MAX/10;
}
}
}
for(long long i = haha[1].first; i <= 600; i++) {
dp[1][i][1] = haha[1].first*br;
}
for(long long j = 0; j <= 600; j++) {
for(long long y = 0; y <= 300; y++) {
long long c = dp[1][j][y]-br*(j+1);
troll[j][y].push_back({c,1});
}
}
for(long long y = 1; y <= 300; y++) {
for(long long j = 0; j <= 600; j++) {
wow[1][j][y] = min(wow[1][j][y-1],dp[1][j][y]-y*idk[j]);
}
}
for(long long i = 2; i <= n; i++) {
for(long long j = max(1LL,haha[i].first); j <= 600; j++) {
for(long long k = i-1; k >= 1; k--) {
dp[i][j][i-k] = min(dp[i][j][i-k],wow[k][j-1][i-k]+(i-k)*br*j+(i-k)*idk[j-1]);
}
}
for(long long j = 0; j < 600; j++) {
for(long long y = 0; y <= i; y++) {
if(j+1 >= haha[i].first) {
dp[i][j+1][y] = min(dp[i][j+1][y],troll[j][y][0].first+i*(j+1)*br);
}
}
}
for(long long j = 1; j <= 600; j++) {
for(long long y = 0; y <= n; y++) {
dp[i][j][y] = min(dp[i][j][y],dp[i][j-1][y]);
}
}
for(long long j = 0; j <= 600; j++) {
for(long long y = 1; y <= i; y++) {
if(troll[j][y][0].second <= i-y) {
troll[j][y].pop_front();
}
long long c = dp[i][j][y]-i*br*(j+1);
while(!troll[j][y].empty() && troll[j][y][troll[j][y].size()-1].first > c) {
troll[j][y].pop_back();
}
troll[j][y].push_back({c,i});
}
}
for(long long y = 1; y <= 300; y++) {
for(long long j = 0; j <= 600; j++) {
wow[i][j][y] = min(wow[i][j][y-1],dp[i][j][y]-y*idk[j]);
}
}
}
for(long long i = 0; i <= n; i++) {
ans = min(ans,dp[n][600][i]);
}
for(long long i = 1; i <= n; i++) {
ans-=haha[i].first*br;
}
cout << ans << endl;
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... |