#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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
9 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
2068 KB |
Output is correct |
2 |
Correct |
12 ms |
860 KB |
Output is correct |
3 |
Correct |
343 ms |
2084 KB |
Output is correct |
4 |
Correct |
443 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1047 ms |
5204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
7868 KB |
Output is correct |
2 |
Execution timed out |
1052 ms |
8904 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1031 ms |
32460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1059 ms |
2432 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1072 ms |
32084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1020 ms |
7288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1033 ms |
5968 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |