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;
#define int long long
struct Function {
multiset<int> s;
int k, p;
Function(int a, int b) { k = a, p = b; }
void reset(int a, int b) { s.clear(), k = a, p = b; }
void prefix() {
if (k <= 0)
return;
if (s.size() < k)
assert(0);
auto it = s.end();
int c = k;
while (c--) {
it--;
k--, p += *it;
s.erase(it);
}
}
void merge(Function x) {
for (auto ele : x.s)
s.insert(ele);
k += x.k, p += x.p;
}
};
int A[500010], B[500010];
void get() {
int n, i, x;
cin >> n;
for (i = 1; i <= n; i++)
cin >> A[i] >> B[i], A[i] += A[i - 1], B[i] += B[i - 1];
Function a(0, 0);
for (i = 1; i <= n * 2; i++)
a.s.insert(0);
Function h(0, 0);
int ext = 0;
for (i = 1; i < n; i++) {
x = A[i] - B[i];
if (x > A[n] - B[n]) {
ext += (x - A[n] + B[n]);
x = A[n] - B[n];
}
// printf(" %lld ", x);
h.reset(1, -x);
h.s.insert(x);
h.s.insert(x);
a.merge(h);
a.prefix();
}
cout << ext + a.p;
return;
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(NULL);
int T;
T = 1;
while (T--)
get();
return 0;
}
Compilation message (stderr)
bulves.cpp: In member function 'void Function::prefix()':
bulves.cpp:12:16: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
12 | if (s.size() < k)
| ~~~~~~~~~^~~
# | 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... |