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>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
const int MOD = 1e9+7;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
const int MXN = 5e5+5;
int n;
ll A[MXN], B[MXN];
int main() {
FASTIO();
cin >> n;
for(int i = 1; i <= n; i++) cin >> A[i] >> B[i], A[i] -= B[i], A[i] += A[i-1];
priority_queue<ll> PQ;
ll sy = 0;
for(int i = 1; i < n; i++) {
if(A[i] < 0) sy += -A[i], A[i] = 0;
sy += A[i];
PQ.push(A[i]);
PQ.push(A[i]);
PQ.pop();
}
ll ans = sy;
while(!PQ.empty()) {
ans -= min(A[n], PQ.top());
PQ.pop();
}
cout << ans << "\n";
return 0;
}
/*
*/
# | 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... |