求Subtask #1
查看原帖
求Subtask #1
1339889
pjh0625楼主2025/1/15 12:14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5+5; 

ll n, a[N], ans;
priority_queue<ll> q;
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

int main() {
    scanf("%lld", &n);
    for(ll i=1;i<=n;i++) {
        scanf("%lld", &a[i]);
        if(!q.empty() && q.top() > a[i]) {
            ans += q.top() - a[i];
            q.pop();
            q.push(a[i]);
        }
        q.push(a[i]);
    }
    printf("%lld\n", ans);
    return 0;
}

2025/1/15 12:14
加载中...