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;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt")
using ll = long long int;
using ull = unsigned long long int;
using vi = vector<ll>;
using ii = pair<ll,ll>;
using vii = vector<ii>;
using ld = long double;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree < T, null_type, less<T>,
rb_tree_tag,
tree_order_statistics_node_update >;
#ifdef SA_DEBUG
template <class T>
void print(T a) {cerr << a << endl;}
template <class T, class... V>
void print(T a, V... b) {cerr << a << ", "; print(b...);}
#define dbg(...) cerr << "[" << __LINE__ << "] " << #__VA_ARGS__ << " :: ", print(__VA_ARGS__)
#else
#define dbg(...) 7
#endif
#define eb emplace_back
#define fi first
#define se second
const ll INFL = 1e18;
const int INF = 1e9;
const double PI = acos(-1);
const ll mod = 1e9+7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T, class V>
ostream& operator << (ostream &s, pair<T, V> a){
s << a.fi << ' ' << a.se; return s;
}
template<class T, class V>
istream& operator >> (istream &s, pair<T, V> &a){
s >> a.fi >> a.se; return s;
}
template<class T>
ostream& operator << (ostream &s, vector<T> a){
for(int i = 0; i < (int)a.size(); i++){
if(i > 0)s << ' ' ;
s << a[i];
} return s;
}
template<class T>
istream& operator >> (istream &s, vector<T> &a){
for(T &x : a)s >> x;
return s;
}
template<class T>
void input(T a[], int l, int r, istream &s = cin){
while(l <= r)s >> a[l++];
}
template<class T>
void output(T a[], int l, int r, bool en = true, ostream &s = cout){
while(l <= r){ s << a[l++];
if(l <= r) s << ' ';
} if(en)s << "\n";
}
const int N = 2e3+3, K = 17, M = 2e5 + 5;
//====================================================================//
template< typename T >
struct SlopeTrick {
T INF = numeric_limits< T >::max() / 3;
T min_f, add_l, add_r;
priority_queue< T, vector< T >, less<> > L;
priority_queue< T, vector< T >, greater<> > R;
void push_R(const T &a) { R.push(a - add_r); }
T top_R() const { return R.top() + add_r; }
T pop_R() { T val=top_R(); R.pop(); return val;}
void push_L(const T &a) { L.push(a - add_l); }
T top_L() const { return L.top() + add_l; }
T pop_L() { T val=top_L(); L.pop(); return val;}
public:
SlopeTrick() : min_f(0), add_l(0), add_r(0) {
L.push(-INF); R.push(INF);
}
// f(x) += a
void add_all(const T &a) {
min_f += a;
}
// add \_ ; f(x) += max(a - x, 0)
void add_a_minus_x(const T &a) {
if (a > top_R()) {
min_f+=a-top_R(); push_L(pop_R());push_R(a);
} else {
push_L(a);
}
}
// add _/ ; f(x) += max(x - a, 0)
void add_x_minus_a(const T &a) {
if (top_L() > a) {
min_f+=top_L()-a;push_R(pop_L());push_L(a);
} else {
push_R(a);
}
}
// add \/ ; f(x) += max(x - a, 0)
void add_abs(const T &a) {
add_a_minus_x(a); add_x_minus_a(a);
}
// \/ -> \_ ; f_{new} (x) = min f(y) (y <= x)
void clear_right(){ while(R.size()>=2)R.pop(); }
// \/ -> _/ ; f_{new} (x) = min f(y) (y >= x)
void clear_left(){ while(L.size()>=2) L.pop(); }
// \/. -> .\/ ; f_{new} (x) = f(x - a)
void shift(const T &a) { add_l+=a; add_r+=a; }
T get(const T &x) {
T ret = min_f;
if (!L.empty() && x < top_L()) {
while(!L.empty())ret += max(T(0),pop_L()-x);
}
if (!R.empty() && top_R() < x) {
while(!R.empty())ret += max(T(0),x-pop_R());
}
return ret;
}
};
void testcase(){
SlopeTrick<ll> s;
int n, h;
cin >> n >> h;
for(int i = 0; i < n; i++){
ll x;
cin >> x;
s.add_l-=h;
s.add_r+=h;
s.add_abs(x);
}
cout << s.min_f << "\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
//cin >> T;
for(int qq = 1; qq <= T; ++qq){
//cout << "Case #" << qq << ": ";
testcase();
}
return 0;
}
/*
6 1597352862016328480
*/
# | 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... |