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 fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 5e5 + 5;
ll sum[maxn];
int N;
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N;
for(int i = 1; i <= N; ++i){
int a, b; cin >> a >> b;
sum[i] = sum[i - 1] + a - b;
}
priority_queue<ll> pq;
ll res = 0;
for(int i = 1; i <= N; ++i){
if(sum[i] < 0){
res -= sum[i];
sum[i] = 0;
}
if(sum[i] > sum[N]){
res += sum[i] - sum[N];
sum[i] = sum[N];
}
pq.push(sum[i]);
if(sum[i] < pq.top()){
res += pq.top() - sum[i];
pq.pop();
pq.push(sum[i]);
}
}
cout << res;
}
# | 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... |