mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 13:56:26 +00:00
D - Buy Low Sell High
https://codeforces.com/contest/865/submission/175146415
This commit is contained in:
parent
903e14bb43
commit
8371c97ed1
39
Codeforces/865/D/D.cpp
Normal file
39
Codeforces/865/D/D.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user