This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;
mt19937 _rand(time(NULL));
const int mxn = 5e5+5;
int n;
ll d[mxn];
void solve(){
cin >> n;
fr(i, 1, n+1){
int a, b;
cin >> a >> b;
d[i] = d[i-1] + a - b;
}
pq<int> Q;
ll ans = 0;
fr(i, 1, n+1){
int vinrange = min(max(d[i], 0LL), d[n]);
Q.push(vinrange);
if(Q.top() > d[i]){
Q.push(vinrange);
ans += Q.top()-d[i];
Q.pop();
}
else if(d[i] > d[n]){
ans += d[i]-d[n];
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |