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;
using ll = long long;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
map<int, Tree<int>> plc;
const ll one = 1;
pair<pair<ll, ll>, Tree<int>> solve()
{
int lf;
cin >> lf;
if (lf > 0)
{
Tree<int> ans = plc[0];
ans.insert(lf);
return { {0, 1}, ans };
}
else
{
pair<pair<ll, ll>, Tree<int>> lft = solve();
pair<pair<ll, ll>, Tree<int>> rgt = solve();
if (lft.second.size() < rgt.second.size()) swap(lft, rgt);
pair<pair<ll, ll>, Tree<int>> ans = lft;
ans.first.first += rgt.first.first, ans.first.second += rgt.first.second;
ll tadd = 0;
for (int tins : rgt.second) tadd += one * lft.second.order_of_key(tins), ans.second.insert(tins);
tadd = fmin(tadd, lft.first.second * rgt.first.second - tadd);
ans.first.first += tadd;
return ans;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
cout << solve().first.first;
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |