이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// author: erray
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) void(37)
#endif
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
vector<int> H(N), C(N);
for (int i = 0; i < N; ++i) {
cin >> H[i] >> C[i];
}
constexpr int64_t inf = int64_t(1e16);
vector<int64_t> best(N, inf);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i == j) continue;
if (H[j] >= H[i]) {
best[i] = min<int64_t>(best[i], C[j] + 1LL * (H[j] - H[i] + 1) * K);
} else {
best[i] = min<int64_t>(best[i], 0LL + C[j]);
}
}
}
debug(best);
map<int, vector<int>> hs;
for (int i = 0; i < N; ++i) {
hs[H[i]].push_back(i);
}
hs[int(2e9)] = {};
int last = -1;
int64_t ans = 0;
priority_queue<int64_t> pq;
for (auto[pos, v] : hs) {
while (last < pos && (!pq.empty() && pq.top() >= (1LL * last * K))) {
ans += 1LL * last * K - pq.top();
pq.pop();
++last;
}
sort(v.begin(), v.end(), [&](int x, int y) {
return C[x] < C[y];
});
for (int i = 1; i < int(v.size()); ++i) {
int ind = v[i];
ans += best[ind];
pq.push(1LL * pos * K + best[ind]);
}
last = pos + 1;
}
cout << ans << '\n';
}
# | 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... |