#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 (lpq.size() > rpq.size() + 1)
{
int t = lpq.top();
lpq.pop();
ls -= t;
rpq.push(-t);
rs += t;
}
if (rpq.size() > lpq.size())
{
int t = -rpq.top();
rpq.pop();
rs -= t;
lpq.push(t);
ls += t;
}
}
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];
if (k == 2)
{
while (lpq.size())
{
lpq.pop();
}
while (rpq.size())
{
rpq.pop();
}
ls = rs = 0;
for (int i = n; i > 0; i--)
{
add (vt[i].first);
add (vt[i].second);
ans = min (ans, rs - ls + s[i - 1]);
}
}
cout << ans + z;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
23 ms |
3024 KB |
Output is correct |
13 |
Correct |
51 ms |
3180 KB |
Output is correct |
14 |
Correct |
34 ms |
3536 KB |
Output is correct |
15 |
Correct |
29 ms |
2260 KB |
Output is correct |
16 |
Correct |
23 ms |
3284 KB |
Output is correct |
17 |
Correct |
32 ms |
3024 KB |
Output is correct |
18 |
Correct |
30 ms |
3544 KB |
Output is correct |
19 |
Correct |
42 ms |
3152 KB |
Output is correct |
20 |
Correct |
27 ms |
3532 KB |
Output is correct |
21 |
Correct |
39 ms |
3224 KB |
Output is correct |
# |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
396 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
516 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
548 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
35 ms |
4048 KB |
Output is correct |
26 |
Correct |
64 ms |
4616 KB |
Output is correct |
27 |
Correct |
69 ms |
5072 KB |
Output is correct |
28 |
Correct |
73 ms |
5580 KB |
Output is correct |
29 |
Correct |
93 ms |
5484 KB |
Output is correct |
30 |
Correct |
37 ms |
2796 KB |
Output is correct |
31 |
Correct |
36 ms |
4504 KB |
Output is correct |
32 |
Correct |
67 ms |
5064 KB |
Output is correct |
33 |
Correct |
56 ms |
4816 KB |
Output is correct |
34 |
Correct |
58 ms |
5068 KB |
Output is correct |
35 |
Correct |
37 ms |
5072 KB |
Output is correct |
36 |
Correct |
63 ms |
5240 KB |
Output is correct |
37 |
Correct |
22 ms |
3796 KB |
Output is correct |