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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define LL long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<LL> diff(n + 1);
priority_queue<LL> Q;
int a, b;
LL res = 0;
for (int i = 1; i <= n; ++i)
{
cin >> a >> b;
diff[i] = a - b;
diff[i] += diff[i - 1];
}
for (int i = 1; i < n; ++i)
{
if (diff[i] < 0)
{
res -= diff[i];
diff[i] = 0;
}
else res += diff[i];
Q.push(diff[i]);
Q.push(diff[i]);
Q.pop();
}
while (!Q.empty())
{
LL x = Q.top();
Q.pop();
res -= min(x, diff[n]);
}
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... |