#line 1 "test/yukicoder/1077.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1077" #line 2 "template.hpp" // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; // https://xn--kst.jp/blog/2019/08/29/cpp-comp/ // debug methods // usage: debug(x,y); // vector 出力できるように修正 template ostream& debug_print(ostream& os, const vector& v) { os << "["; for (size_t i = 0; i < v.size(); ++i) { os << v[i]; if (i < v.size() - 1) os << ", "; } os << "]"; return os; } template ostream& debug_print(ostream& os, const T& var) { os << var; return os; } #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x #define debug_1(x1) { cout << #x1 << ": "; debug_print(cout, x1) << endl; } #define debug_2(x1, x2) { cout << #x1 << ": "; debug_print(cout, x1) << ", " << #x2 << ": "; debug_print(cout, x2) << endl; } #define debug_3(x1, x2, x3) { cout << #x1 << ": "; debug_print(cout, x1) << ", " << #x2 << ": "; debug_print(cout, x2) << ", " << #x3 << ": "; debug_print(cout, x3) << endl; } #define debug_4(x1, x2, x3, x4) { cout << #x1 << ": "; debug_print(cout, x1) << ", " << #x2 << ": "; debug_print(cout, x2) << ", " << #x3 << ": "; debug_print(cout, x3) << ", " << #x4 << ": "; debug_print(cout, x4) << endl; } #define debug_5(x1, x2, x3, x4, x5) { cout << #x1 << ": "; debug_print(cout, x1) << ", " << #x2 << ": "; debug_print(cout, x2) << ", " << #x3 << ": "; debug_print(cout, x3) << ", " << #x4 << ": "; debug_print(cout, x4) << ", " << #x5 << ": "; debug_print(cout, x5) << endl; } #ifdef LOCAL #define debug(...) CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))(__VA_ARGS__) #else #define debug(...) #endif using ll = long long; using vl = vector; using Graph = vector>; using P = pair; #define all(v) v.begin(), v.end() template inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } #define rep1(i, n) for(ll i = 1; i <= ((ll)n); ++i) // https://trap.jp/post/1224/ template constexpr auto min(T... a) { return min(initializer_list>{a...}); } template constexpr auto max(T... a) { return max(initializer_list>{a...}); } template void input(T &...a) { (cin >> ... >> a); } template void input(vector &a) { for(T &x : a) cin >> x; } void print() { cout << '\n'; } template void print(const T &a, const Ts &...b) { cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template struct is_container : std::false_type {}; template struct is_container().begin()), decltype(std::declval().end())>> : std::true_type {}; template typename enable_if::value>::type print(const Container& x) { if (!x.empty()) { auto it = x.begin(); for (; it != prev(x.end()); ++it) { cout << *it << " "; } cout << *it << "\n"; // 最後の要素を出力して改行 } } #define INT(...) \ int __VA_ARGS__; \ input(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ input(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ input(__VA_ARGS__) #define REP1(a) for(ll i = 0; i < a; i++) #define REP2(i, a) for(ll i = 0; i < a; i++) #define REP3(i, a, b) for(ll i = a; i < b; i++) #define REP4(i, a, b, c) for(ll i = a; i < b; i += c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) ll inf = 3e18; vl dx = {1, -1, 0, 0}; vl dy = {0, 0, 1, -1}; #line 3 "data_structure/slope-trick.hpp" // https://maspypy.com/slope-trick-1-%E8%A7%A3%E8%AA%AC%E7%B7%A8 // https://ei1333.github.io/library/structure/others/slope-trick.hpp struct SlopeTrick { multiset L, R; ll min_f; ll add_L, add_R; SlopeTrick() { L = {-inf}; R = {inf}; add_L = add_R = min_f = 0; } ll size() { return ssize(L) + ssize(R); } // add \____ // f(x) <- f(x) + max(a-x,0) void add_a_minus_x(ll a) { ll r0 = *begin(R); min_f += max(0LL, a - (r0 + add_R)); R.insert(a - add_R); auto itr = begin(R); L.insert(*itr + add_R - add_L); R.erase(itr); } // add ___/ // f(x) <- f(x) + max(x-a,0) void add_x_minus_a(ll a) { ll l0 = *L.rbegin(); min_f += max(0LL, l0 + add_L - a); L.insert(a - add_L); auto itr = prev(end(L)); R.insert(*itr + add_L - add_R); L.erase(itr); } // add \/ // f(x) <- f(x) + |x-a| void add_abs(ll a) { add_a_minus_x(a); add_x_minus_a(a); } // f(x) <- f(x) + a void add_all(ll a) { min_f += a; } // f(x) = min_f をとる閉区間[l,r] P min_range() { return P(*rbegin(L) + add_L, *begin(R) + add_R); } // f(x) <- f(x) + g(x) void merge(SlopeTrick &g) { min_f += g.min_f; for(auto l : g.L) { if(l == -inf) continue; add_a_minus_x(l + g.add_R); } for(auto r : g.R) { if(r == inf) continue; add_x_minus_a(r + g.add_L); } } // https://maspypy.com/slope-trick-1-%E8%A7%A3%E8%AA%AC%E7%B7%A8#toc18:~:text=%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%99%E3%80%82-,%E3%82%B9%E3%83%A9%E3%82%A4%E3%83%89%E6%9C%80%E5%B0%8F%E5%80%A4%E9%96%A2%E6%95%B0,-%EF%BC%9A // f(x) <- min[x-b <= y <= x-a]f(y) // \./ -> \_/ void shift(ll a, ll b) { assert(a <= b); add_L += a; add_R += b; } // f(x) <- f(x-a) void shift(ll a) { shift(a, a); } ll get(ll a) { ll res = min_f; for(auto l : L) { res += max(0LL, l + add_L - a); } for(auto r : R) { res += max(0LL, a - (r + add_R)); } return res; } // \__/ -> \___ // f(x) <- min[y<=x]f(y) void clear_right() { R = {inf}; } // \__/ -> __/ // f(x) <- min[y>=x]f(y) void clear_left() { L = {inf}; } }; #line 3 "test/yukicoder/1077.test.cpp" void solve() { LL(n); SlopeTrick st; while(n--) { LL(y); st.clear_right(); st.add_abs(y); } print(st.min_f); } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); ll t = 1; rep(_, t) solve(); }