#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
int sumlow = 0, sumhigh = 0;
multiset<int> low, high;
void sinsert(int val) {
int med = low.empty() ? INF : *low.rbegin();
if (val > med) {
high.insert(val);
sumhigh += val;
} else {
low.insert(val);
sumlow += val;
}
if (high.size() > low.size()) {
int tmp = *high.begin();
low.insert(tmp);
sumlow += tmp;
sumhigh -= tmp;
high.erase(high.find(tmp));
} else if (low.size() > high.size() + 1) {
int tmp = *low.rbegin();
high.insert(tmp);
sumhigh += tmp;
sumlow -= tmp;
low.erase(low.find(tmp));
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k, n;
cin >> k >> n;
vector<pair<int, int>> diff;
int same = 0;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) {
same += abs(s - t);
} else {
if (s > t) swap(s, t);
diff.push_back({s, t});
}
}
sort(diff.begin(), diff.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
return a.first + a.second < b.first + b.second;
});
int sz = diff.size();
same += sz; // crossing the river
vector<int> precal(sz);
if (sz == 0) {
cout << same;
return 0;
}
for (int i = 0; i < sz; i++) {
sinsert(diff[i].first);
sinsert(diff[i].second);
int med = *low.rbegin();
precal[i] = (med * (int)low.size() - sumlow) + (sumhigh - med * (int)high.size());
}
if (k == 1) {
int med = *low.rbegin();
int ans = (med * (int)low.size() - sumlow) + (sumhigh - med * (int)high.size());
cout << same + ans;
return 0;
}
sumlow = sumhigh = 0;
low.clear(); high.clear();
int ans = precal[sz - 1];
// since we precalculated from the beginning, we loop back from the end
for (int i = sz - 1; i >= 1; i--) {
sinsert(diff[i].first);
sinsert(diff[i].second);
int med = *low.rbegin();
int val = (med * (int)low.size() - sumlow) + (sumhigh - med * (int)high.size());
ans = min(ans, val + precal[i - 1]);
}
cout << same + ans;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
66 ms |
12008 KB |
Output is correct |
13 |
Correct |
122 ms |
11964 KB |
Output is correct |
14 |
Correct |
100 ms |
10844 KB |
Output is correct |
15 |
Correct |
58 ms |
7120 KB |
Output is correct |
16 |
Correct |
75 ms |
11956 KB |
Output is correct |
17 |
Correct |
82 ms |
11960 KB |
Output is correct |
18 |
Correct |
55 ms |
11992 KB |
Output is correct |
19 |
Correct |
87 ms |
11956 KB |
Output is correct |
20 |
Correct |
86 ms |
11988 KB |
Output is correct |
21 |
Correct |
85 ms |
12020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
135 ms |
12724 KB |
Output is correct |
26 |
Correct |
204 ms |
12952 KB |
Output is correct |
27 |
Correct |
228 ms |
13724 KB |
Output is correct |
28 |
Correct |
230 ms |
14280 KB |
Output is correct |
29 |
Correct |
219 ms |
14272 KB |
Output is correct |
30 |
Correct |
81 ms |
7680 KB |
Output is correct |
31 |
Correct |
107 ms |
13688 KB |
Output is correct |
32 |
Correct |
139 ms |
14284 KB |
Output is correct |
33 |
Correct |
88 ms |
14004 KB |
Output is correct |
34 |
Correct |
151 ms |
14332 KB |
Output is correct |
35 |
Correct |
140 ms |
13876 KB |
Output is correct |
36 |
Correct |
144 ms |
14124 KB |
Output is correct |
37 |
Correct |
145 ms |
12788 KB |
Output is correct |