#if __INCLUDE_LEVEL__ == 0 #include using namespace std; #undef assert #define assert(expr) (expr) || (__builtin_unreachable(), 0) #include __BASE_FILE__ namespace std::ranges::views { namespace { struct S { priority_queue lower; priority_queue, ::greater<>> upper; explicit S(int x) { lower.push(x); } int median() const { return lower.top(); } int size() const { return ssize(lower) + ssize(upper); } void merge(S s) { if (size() < s.size()) { swap(*this, s); } while (ssize(s.lower)) { add(s.lower.top()); s.lower.pop(); } while (ssize(s.upper)) { add(s.upper.top()); s.upper.pop(); } } void add(int x) { if (::empty(upper) || x < upper.top()) { lower.push(x); } else { upper.push(x); } while (ssize(upper) < ssize(lower)) { upper.push(lower.top()); lower.pop(); } while (ssize(lower) < ssize(upper)) { lower.push(upper.top()); upper.pop(); } } int64_t cost() { int m = median(); int64_t cost = 0; while (ssize(lower)) { cost += abs(lower.top() - m); lower.pop(); } while (ssize(upper)) { cost += abs(upper.top() - m); upper.pop(); } return cost; } }; void solve() { int n; cin >> n; vector a(n); cin >> a; sort(a); for (int i : iota(0, n)) { a[i] -= i; } vector s; for (int e : a) { S t(e); while (ssize(s) && t.median() < s.back().median()) { t.merge(::move(s.back())); s.pop_back(); } s.push_back(t); } int64_t ans = 0; for (auto&& e : s) { ans += e.cost(); } cout << ans << '\n'; } } // namespace } // namespace std::ranges::views using views::solve; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); } #else // __INCLUDE_LEVEL__ namespace std { template istream& operator>>(istream& is, pair& p) { return is >> p.first >> p.second; } template istream& operator>>(istream& is, tuple& t) { return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t); } template >* = nullptr> auto operator>>(istream& is, R&& r) -> decltype(is >> *begin(r)) { for (auto&& e : r) { is >> e; } return is; } template ostream& operator<<(ostream& os, const pair& p) { return os << p.first << ' ' << p.second; } template ostream& operator<<(ostream& os, const tuple& t) { auto f = [&os](const auto&... xs) -> ostream& { [[maybe_unused]] auto sep = ""; ((os << exchange(sep, " ") << xs), ...); return os; }; return apply(f, t); } namespace impl { template constexpr int n_dims(char) { return 0; } template constexpr auto n_dims(int) -> decltype(begin(declval()), 0) { return n_dims()))>(0) + 1; } } // namespace impl template >* = nullptr> auto operator<<(ostream& os, R&& r) -> decltype(os << *begin(r)) { static constexpr int D = impl::n_dims(0); static const string SEP = 1 < D ? string(D - 1, '\n') : " "; string sep; for (auto&& e : r) { os << exchange(sep, SEP) << e; } return os; } } // namespace std #endif // __INCLUDE_LEVEL__