#include <bits/stdc++.h>
using namespace std;
#define int long long
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int> > rpq;
int lsum = 0, rsum = 0;
void insrt(int x) {
int med;
if (lpq.size()) med = lpq.top();
else med = 1e9;
if (x <= med) {
lpq.push(x);
lsum += x;
} else {
rpq.push(x);
rsum += x;
}
if (lpq.size() > rpq.size()+1) {
int tmp = lpq.top();
lpq.pop();
rpq.push(tmp);
lsum -= tmp;
rsum += tmp;
}
if (rpq.size() > lpq.size()) {
int tmp = rpq.top();
rpq.pop();
lpq.push(tmp);
rsum -= tmp;
lsum += tmp;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int k, n;
cin >> k >> n;
int m = 0;
vector<tuple<int, int, int>> num;
vector<int> all;
int ans = 0;
for (int i = 0; i < n; i++) {
int a, b;
char c, d;
cin >> c >> a >> d >> b;
if (c == d) {
ans += abs(b-a);
} else {
m++;
ans++;
all.push_back(a);
all.push_back(b);
num.push_back({a+b, a, b});
}
}
sort(all.begin(), all.end());
if (k == 1) {
int x = all[all.size()/2];
for (int i : all) {
ans += abs(i-x);
}
cout << ans << endl;
} else {
sort(num.begin(), num.end());
//find prefixes
vector<int> pref(m);
for (int i = 0; i < m; i++) {
auto [tot, a, b] = num[i];
insrt(a);
insrt(b);
pref[i] = rsum-lsum;
}
//reset arrays
int res = pref[m-1];
rsum = 0, lsum = 0;
while (lpq.size()) lpq.pop();
while (rpq.size()) rpq.pop();
//find suffixes and answer
for (int i = m-1; i >= 0; i--) {
auto [tot, a, b] = num[i];
insrt(a);
insrt(b);
res = min(res, rsum-lsum+pref[i-1]);
}
cout << res+ans << endl;
}
}
//changes infinities higher if wa
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
540 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 |
344 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 |
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 |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
532 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
540 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 |
12 |
Correct |
16 ms |
5464 KB |
Output is correct |
13 |
Correct |
33 ms |
6728 KB |
Output is correct |
14 |
Correct |
22 ms |
5720 KB |
Output is correct |
15 |
Correct |
18 ms |
4184 KB |
Output is correct |
16 |
Correct |
19 ms |
5976 KB |
Output is correct |
17 |
Correct |
21 ms |
6740 KB |
Output is correct |
18 |
Correct |
30 ms |
6348 KB |
Output is correct |
19 |
Correct |
34 ms |
6740 KB |
Output is correct |
20 |
Correct |
23 ms |
6224 KB |
Output is correct |
21 |
Correct |
28 ms |
6484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |