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 eb emplace_back
#define em emplace
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int MAX = 303030;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
struct Node {
priority_queue <ll> L;
priority_queue <ll, vector <ll>, greater <ll> > R;
ll ans = 0, s = 0;
void init(int x) {L.em(x), R.em(x);}
int size() {return (int)(L.size() + R.size());}
void merge(Node &X) {
ans += X.ans;
while(!X.L.empty()) {
ll temp = X.L.top() - X.s;
X.L.pop();
if(temp >= R.top() + s){
ans += temp - R.top() - s;
L.em(R.top() + 2 * s); R.pop();
R.em(temp - s);
}
else L.em(temp + s);
}
while(!X.R.empty()) {
ll temp = X.R.top() + X.s;
X.R.pop();
if(temp <= L.top() - s){
ans += L.top() - temp - s;
R.em(L.top() - 2 * s); L.pop();
L.em(temp + s);
}
else R.em(temp - s);
}
}
} dp[MAX];
int p[MAX], idx[MAX];
ll l[MAX];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n;
ll h;
cin >> n >> h;
for(int i = 1; i <= n; i++) {
cin >> l[i];
dp[i].init(l[i]);
idx[i] = i;
}
for(int i = 2; i <= n; i++) {
p[i] = i - 1;
}
for(int i = n; i > 1; i--) {
dp[idx[i]].s += h;
if(dp[idx[i]].size() < dp[idx[p[i]]].size()) {
dp[idx[p[i]]].merge(dp[idx[i]]);
}
else {
dp[idx[i]].merge(dp[idx[p[i]]]);
idx[p[i]] = idx[i];
}
}
cout << dp[idx[1]].ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |