**

D. Buy Low Sell High

**

原题链接
solution:利用优先队列的思路可以很好的求出答案,有一个坑点就是res用int会溢出,必须用long long表示

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n, x;
	long long res = 0;
	cin >> n;
	priority_queue<int, vector<int>, greater<int> > q;
	for (int i = 0; i < n; ++i){
		cin >> x;
		q.push(x);
		if (q.top() < x){
			res += x - q.top();
			q.pop();
			q.push(x);
		}
	}
	cout << res << endl;
	return 0;
}