0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 13:16:26 +00:00

D - Buy Low Sell High

https://codeforces.com/contest/865/submission/175146415
This commit is contained in:
Baoshuo Ren 2022-10-08 22:02:27 +08:00
parent 903e14bb43
commit 8371c97ed1
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

39
Codeforces/865/D/D.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <iostream>
#include <functional>
#include <queue>
#include <vector>
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<int, std::vector<int>, 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;
}