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 <vector>
#include <set>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define sz(x) int(x.size())
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vll x(1+N);
ll basicCost = 0;
x[0] = 0;
for(int i = 1; i <= N; i++)
{
ll a, b;
cin >> a >> b;
x[i] = x[i-1] + a - b;
}
for(int i = 1; i < N; i++)
{
if(x[i] < 0)
{
basicCost += 0 - x[i];
x[i] = 0;
}
if(x[i] > x[N])
{
basicCost += x[i] - x[N];
x[i] = x[N];
}
}
ll ans0 = 0;
multiset<ll> points;
for(int i = 1; i <= N; i++)
{
ans0 += x[i];
points.insert(x[i]);
points.insert(x[i]);
points.erase(points.find(*points.rbegin()));
}
ll curr_sl = -N;
ll answer = basicCost + ans0;
points.insert(0);
while(sz(points) > 1)
{
ll u = *points.begin();
points.erase(points.begin());
ll v = *points.begin();
answer += (v - u) * curr_sl;
curr_sl++;
}
cout << answer << '\n';
}
# | 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... |