From 4706bdfdc45bf0c8dd0c592a6aa0409ed4edc0d6 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 8 Jan 2023 16:53:34 +0800 Subject: [PATCH] =?UTF-8?q?P4597=20=E5=BA=8F=E5=88=97=20sequence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/99038877 --- Luogu/P4597/P4597.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Luogu/P4597/P4597.cpp diff --git a/Luogu/P4597/P4597.cpp b/Luogu/P4597/P4597.cpp new file mode 100644 index 00000000..1be37a77 --- /dev/null +++ b/Luogu/P4597/P4597.cpp @@ -0,0 +1,35 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n; +long long ans; +std::priority_queue q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + q.emplace(x); + + if (x < q.top()) { + ans += q.top() - x; + q.pop(); + q.emplace(x); + } + } + + cout << ans << endl; + + return 0; +}