#include <bits/stdc++.h>
using namespace std;
const int DIM = 100005;
long long lst1[DIM], lst2[DIM];
vector<pair<int, int>> arr;
void insert(auto &lef, auto &rig, long long &lsum, long long &rsum, int x) {
if (!lef.size() or x <= lef.top()) {
lsum += x; lef.push(x); }
else {
rsum += x; rig.push(x); } }
void balance(auto &lef, auto &rig, long long &lsum, long long &rsum) {
while ((int) rig.size() - (int) lef.size() > 1) {
lsum += rig.top(); rsum -= rig.top(); lef.push(rig.top()); rig.pop(); }
while ((int) lef.size() - (int) rig.size() > 1) {
rsum += lef.top(); lsum -= lef.top(); rig.push(lef.top()); lef.pop(); } }
void solve(long long lst[DIM]) {
long long lsum = 0, rsum = 0;
priority_queue<int, vector<int>, less<int>> lef;
priority_queue<int, vector<int>, greater<int>> rig;
for (int i = 1; i <= (int) arr.size(); ++i) {
insert(lef, rig, lsum, rsum, arr[i - 1].first);
insert(lef, rig, lsum, rsum, arr[i - 1].second);
balance(lef, rig, lsum, rsum);
lst[i] = 1LL * lef.top() * (lef.size() - rig.size()) - lsum + rsum; } }
int main(void) {
#ifdef HOME
freopen("bridge.in", "r", stdin);
freopen("bridge.out", "w", stdout);
#endif
int k, n; cin >> k >> n;
long long ans = 0;
for (int i = 1; i <= n; ++i) {
char c1, c2; int p1, p2;
cin >> c1 >> p1 >> c2 >> p2;
if (c1 == c2) {
ans += abs(p2 - p1); }
else {
arr.emplace_back(min(p1, p2), max(p1, p2)); } }
sort(arr.begin(), arr.end(),
[](const pair<int, int> &p1, const pair<int, int> &p2) {
return p1.first + p1.second < p2.first + p2.second; });
solve(lst1); reverse(arr.begin(), arr.end()); solve(lst2);
long long cst = (1LL << 50);
for (int i = 0; i <= (int) arr.size(); ++i) {
cst = min(cst, lst1[i] + lst2[arr.size() - i]); }
if (k == 1) {
cst = lst1[arr.size()]; }
cout << ans + arr.size() + cst << endl;
return 0; }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
636 KB |
Output is correct |
3 |
Correct |
3 ms |
700 KB |
Output is correct |
4 |
Correct |
4 ms |
700 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
4 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
892 KB |
Output is correct |
9 |
Correct |
4 ms |
912 KB |
Output is correct |
10 |
Correct |
4 ms |
936 KB |
Output is correct |
11 |
Correct |
4 ms |
956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
984 KB |
Output is correct |
2 |
Correct |
2 ms |
984 KB |
Output is correct |
3 |
Correct |
3 ms |
1156 KB |
Output is correct |
4 |
Correct |
3 ms |
1164 KB |
Output is correct |
5 |
Correct |
4 ms |
1184 KB |
Output is correct |
6 |
Correct |
3 ms |
1208 KB |
Output is correct |
7 |
Correct |
4 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
1252 KB |
Output is correct |
9 |
Correct |
4 ms |
1272 KB |
Output is correct |
10 |
Correct |
3 ms |
1316 KB |
Output is correct |
11 |
Correct |
4 ms |
1336 KB |
Output is correct |
12 |
Correct |
75 ms |
5736 KB |
Output is correct |
13 |
Correct |
162 ms |
8132 KB |
Output is correct |
14 |
Correct |
109 ms |
9032 KB |
Output is correct |
15 |
Correct |
97 ms |
9268 KB |
Output is correct |
16 |
Correct |
107 ms |
12216 KB |
Output is correct |
17 |
Correct |
148 ms |
14728 KB |
Output is correct |
18 |
Correct |
119 ms |
16584 KB |
Output is correct |
19 |
Correct |
165 ms |
18972 KB |
Output is correct |
20 |
Correct |
130 ms |
20752 KB |
Output is correct |
21 |
Correct |
154 ms |
23040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
23040 KB |
Output is correct |
2 |
Correct |
3 ms |
23040 KB |
Output is correct |
3 |
Correct |
4 ms |
23040 KB |
Output is correct |
4 |
Correct |
3 ms |
23040 KB |
Output is correct |
5 |
Correct |
2 ms |
23040 KB |
Output is correct |
6 |
Correct |
3 ms |
23040 KB |
Output is correct |
7 |
Correct |
4 ms |
23040 KB |
Output is correct |
8 |
Correct |
2 ms |
23040 KB |
Output is correct |
9 |
Correct |
3 ms |
23040 KB |
Output is correct |
10 |
Correct |
2 ms |
23040 KB |
Output is correct |
11 |
Correct |
2 ms |
23040 KB |
Output is correct |
12 |
Correct |
3 ms |
23040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
23040 KB |
Output is correct |
2 |
Correct |
2 ms |
23040 KB |
Output is correct |
3 |
Correct |
3 ms |
23040 KB |
Output is correct |
4 |
Correct |
3 ms |
23040 KB |
Output is correct |
5 |
Correct |
3 ms |
23040 KB |
Output is correct |
6 |
Correct |
3 ms |
23040 KB |
Output is correct |
7 |
Correct |
3 ms |
23040 KB |
Output is correct |
8 |
Correct |
3 ms |
23040 KB |
Output is correct |
9 |
Correct |
3 ms |
23040 KB |
Output is correct |
10 |
Correct |
3 ms |
23040 KB |
Output is correct |
11 |
Correct |
3 ms |
23040 KB |
Output is correct |
12 |
Correct |
3 ms |
23040 KB |
Output is correct |
13 |
Correct |
3 ms |
23040 KB |
Output is correct |
14 |
Correct |
3 ms |
23040 KB |
Output is correct |
15 |
Correct |
4 ms |
23040 KB |
Output is correct |
16 |
Correct |
2 ms |
23040 KB |
Output is correct |
17 |
Correct |
3 ms |
23040 KB |
Output is correct |
18 |
Correct |
3 ms |
23040 KB |
Output is correct |
19 |
Correct |
3 ms |
23040 KB |
Output is correct |
20 |
Correct |
3 ms |
23040 KB |
Output is correct |
21 |
Correct |
3 ms |
23040 KB |
Output is correct |
22 |
Correct |
4 ms |
23040 KB |
Output is correct |
23 |
Correct |
4 ms |
23040 KB |
Output is correct |
24 |
Correct |
4 ms |
23040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
23040 KB |
Output is correct |
2 |
Correct |
2 ms |
23040 KB |
Output is correct |
3 |
Correct |
3 ms |
23040 KB |
Output is correct |
4 |
Correct |
3 ms |
23040 KB |
Output is correct |
5 |
Correct |
3 ms |
23040 KB |
Output is correct |
6 |
Correct |
2 ms |
23040 KB |
Output is correct |
7 |
Correct |
3 ms |
23040 KB |
Output is correct |
8 |
Correct |
2 ms |
23040 KB |
Output is correct |
9 |
Correct |
2 ms |
23040 KB |
Output is correct |
10 |
Correct |
3 ms |
23040 KB |
Output is correct |
11 |
Correct |
3 ms |
23040 KB |
Output is correct |
12 |
Correct |
3 ms |
23040 KB |
Output is correct |
13 |
Correct |
3 ms |
23040 KB |
Output is correct |
14 |
Correct |
3 ms |
23040 KB |
Output is correct |
15 |
Correct |
4 ms |
23040 KB |
Output is correct |
16 |
Correct |
2 ms |
23040 KB |
Output is correct |
17 |
Correct |
3 ms |
23040 KB |
Output is correct |
18 |
Correct |
3 ms |
23040 KB |
Output is correct |
19 |
Correct |
3 ms |
23040 KB |
Output is correct |
20 |
Correct |
4 ms |
23040 KB |
Output is correct |
21 |
Correct |
3 ms |
23040 KB |
Output is correct |
22 |
Correct |
4 ms |
23040 KB |
Output is correct |
23 |
Correct |
3 ms |
23040 KB |
Output is correct |
24 |
Correct |
4 ms |
23040 KB |
Output is correct |
25 |
Correct |
82 ms |
24292 KB |
Output is correct |
26 |
Correct |
105 ms |
25252 KB |
Output is correct |
27 |
Correct |
137 ms |
26968 KB |
Output is correct |
28 |
Correct |
159 ms |
29288 KB |
Output is correct |
29 |
Correct |
161 ms |
31664 KB |
Output is correct |
30 |
Correct |
86 ms |
31664 KB |
Output is correct |
31 |
Correct |
102 ms |
34492 KB |
Output is correct |
32 |
Correct |
151 ms |
36928 KB |
Output is correct |
33 |
Correct |
116 ms |
38804 KB |
Output is correct |
34 |
Correct |
161 ms |
41332 KB |
Output is correct |
35 |
Correct |
129 ms |
43084 KB |
Output is correct |
36 |
Correct |
151 ms |
45136 KB |
Output is correct |
37 |
Correct |
70 ms |
45988 KB |
Output is correct |