diff --git a/Codeforces/865/D/D.cpp b/Codeforces/865/D/D.cpp new file mode 100644 index 00000000..073fae69 --- /dev/null +++ b/Codeforces/865/D/D.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +int n, p[N]; +long long ans; +std::priority_queue, std::greater<>> q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> p[i]; + } + + for (int i = 1; i <= n; i++) { + if (!q.empty() && q.top() < p[i]) { + ans += p[i] - q.top(); + q.pop(); + q.emplace(p[i]); + } + + q.emplace(p[i]); + } + + cout << ans << endl; + + return 0; +}