diff --git a/Luogu/P4331/P4331.cpp b/Luogu/P4331/P4331.cpp new file mode 100644 index 00000000..2477e9a7 --- /dev/null +++ b/Luogu/P4331/P4331.cpp @@ -0,0 +1,51 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n; +long long a[N], b[N], ans; +std::priority_queue q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + a[i] -= i; + } + + for (int i = 1; i <= n; i++) { + q.emplace(a[i]); + + if (q.top() > a[i]) { + ans += q.top() - a[i]; + q.pop(); + q.emplace(a[i]); + } + + b[i] = q.top(); + } + + for (int i = n - 1; i; i--) { + b[i] = std::min(b[i], b[i + 1]); + } + + cout << ans << endl; + + for (int i = 1; i <= n; i++) { + cout << b[i] + i << ' '; + } + + cout << endl; + + return 0; +}