提交时间:2022-07-20 11:50:37

运行 ID: 52700

#include <iostream> #include <vector> #include <algorithm> #define ll long long using namespace std; int a[1000010]; int main() { int n,top = 0; ll ans = 0; cin>>n; while(n--) { int x; cin>>x; if(x > a[top]) a[++top] = x; else { int pos = upper_bound(a + 1,a + top + 1,x) - a; ans+=a[pos] - x; } } cout<<ans; return 0; }