This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Grzegorz Ryn
// V LO Kraków
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define st first
#define nd second
#define FOR(__VAR, __START, __END) for(int __VAR=__START; __VAR<=__END; __VAR++)
#define FORB(__VAR, __START, __END) for(int __VAR=__START; __VAR>=__END; __VAR--)
#define FORK(__VAR, __START, __END, __DIFF) for(int __VAR=__START; __VAR<=END; __VAR+=__DIFF)
#define all(__VAR) (__VAR).begin(), (__VAR).end()
#define rall(__VAR) (__VAR).rbegin(), (__VAR).rend()
#define DEBUG(__VAR) cout << #__VAR << ": " << __VAR << endl;
template<typename __T1, typename __T2>
ostream & operator<<(ostream &out, pair<__T1, __T2> &__VAR) {
cout << "[" << __VAR.st << ", " << __VAR.nd << "]";
return out;
}
template<typename __T>
ostream & operator<<(ostream &out, vector<__T> &__VAR) {
cout << "[";
FOR(i, 0, (int)__VAR.size()-2)
cout << __VAR[i] << ", ";
if(__VAR.size()>0)
cout << __VAR[__VAR.size()-1];
cout << "]" << endl;
return out;
}
const int INF=1e9;
void print(priority_queue<ll, vector<ll>, greater<ll>> Q, ll trans) {
cout << "Q: " << endl;
while(!Q.empty()) {
cout << Q.top()-trans << ' ';
Q.pop();
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<ll> a(N), b(N);
for(int i=0; i<N; i++) {
cin >> a[i] >> b[i];
}
priority_queue<ll, vector<ll>, greater<ll>> Q;
for(int i=0; i<N+1; i++) {
Q.push(0);
}
ll trans=0;
ll result=0;
for(int i=0; i<N; i++) {
trans += b[i]-a[i];
ll st = Q.top();
result += max(trans-st, 0LL);
Q.push(trans);
Q.push(trans);
Q.pop();
//cout << result << endl;
//print(Q, trans);
}
ll slope=0, prev=-1e16;
while(!Q.empty()) {
ll cur=Q.top();
Q.pop();
result += slope * (cur-prev);
prev = cur;
slope++;
if(cur-trans == 0) {
break;
}
}
cout << result << '\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... |