#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;
num.push_back({0, 0, 0});
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+1);
for (int i = 1; i <= m; i++) {
auto [tot, a, b] = num[i];
insrt(a);
insrt(b);
pref[i] = rsum-lsum;
}
//reset arrays
int res = pref[m];
while (lpq.size()) lpq.pop();
while (rpq.size()) rpq.pop();
rsum = 0, lsum = 0;
//find suffixes and answer
for (int i = m; 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;
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:78:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | auto [tot, a, b] = num[i];
| ^
bridge.cpp:90:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | 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 |
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 |
356 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 |
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 |
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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
18 ms |
5344 KB |
Output is correct |
13 |
Correct |
31 ms |
6876 KB |
Output is correct |
14 |
Correct |
22 ms |
5860 KB |
Output is correct |
15 |
Correct |
18 ms |
4324 KB |
Output is correct |
16 |
Correct |
20 ms |
6108 KB |
Output is correct |
17 |
Correct |
22 ms |
6864 KB |
Output is correct |
18 |
Correct |
25 ms |
6364 KB |
Output is correct |
19 |
Correct |
31 ms |
6864 KB |
Output is correct |
20 |
Correct |
23 ms |
6612 KB |
Output is correct |
21 |
Correct |
28 ms |
6620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
400 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
528 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
33 ms |
8660 KB |
Output is correct |
26 |
Correct |
61 ms |
9180 KB |
Output is correct |
27 |
Correct |
75 ms |
9684 KB |
Output is correct |
28 |
Correct |
80 ms |
10436 KB |
Output is correct |
29 |
Correct |
77 ms |
9876 KB |
Output is correct |
30 |
Correct |
40 ms |
5604 KB |
Output is correct |
31 |
Correct |
34 ms |
9680 KB |
Output is correct |
32 |
Correct |
52 ms |
10420 KB |
Output is correct |
33 |
Correct |
67 ms |
9928 KB |
Output is correct |
34 |
Correct |
63 ms |
10296 KB |
Output is correct |
35 |
Correct |
39 ms |
9948 KB |
Output is correct |
36 |
Correct |
61 ms |
9928 KB |
Output is correct |
37 |
Correct |
27 ms |
8668 KB |
Output is correct |