#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);
if (i > 0) res = min(res, rsum-lsum+pref[i-1]);
else res = min(res, rsum-lsum);
}
cout << res+ans << endl;
}
}
//changes infinities higher if wa
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:77:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | auto [tot, a, b] = num[i];
| ^
bridge.cpp:89:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
89 | auto [tot, a, b] = num[i];
| ^
# |
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 |
344 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 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
680 KB |
Output is correct |
12 |
Correct |
17 ms |
5464 KB |
Output is correct |
13 |
Correct |
31 ms |
6712 KB |
Output is correct |
14 |
Correct |
26 ms |
5720 KB |
Output is correct |
15 |
Correct |
18 ms |
4280 KB |
Output is correct |
16 |
Correct |
19 ms |
5976 KB |
Output is correct |
17 |
Correct |
23 ms |
6740 KB |
Output is correct |
18 |
Correct |
28 ms |
6232 KB |
Output is correct |
19 |
Correct |
31 ms |
6728 KB |
Output is correct |
20 |
Correct |
21 ms |
6376 KB |
Output is correct |
21 |
Correct |
29 ms |
6472 KB |
Output is correct |
# |
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 |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |