#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 17;
int k, n;
long long s[N], ans, ls, rs;
priority_queue <int> lpq, rpq;
vector <pair <int, int>> vt;
bool cmp (pair <int, int> x, pair <int, int> y)
{
return x.first + x.second < y.first + y.second;
}
void add (int x)
{
int mid;
if (lpq.size())
{
mid = lpq.top();
}
else
{
mid = 1e9 + 17;
}
if (x <= mid)
{
lpq.push(x);
ls += x;
}
else
{
rpq.push(-x);
rs += x;
}
if (rpq.size() + 1 < lpq.size()) {
int nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
ls -= nxt;
rs += nxt;
} else if (lpq.size() < rpq.size()) {
int nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
rs -= nxt;
ls += nxt;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> k >> n;
long long z = 0;
vt.push_back ({0, 0});
for (int i = 1; i <= n; ++i)
{
int a, b;
char x, y;
cin >> x >> a >> y >> b;
if (x == y)
{
z += abs (a - b);
continue;
}
vt.push_back ({a, b});
}
sort (vt.begin(), vt.end(), cmp);
n = vt.size() - 1;
z += n;
for (int i = 1; i <= n; ++i)
{
add (vt[i].first);
add (vt[i].second);
s[i] = rs - ls;
}
ans = s[n];
cout << ans + z;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |