#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 INF = 1E18;
char ch[2];
int x[2];
template <typename T>
ostream& operator<< (ostream &out, vector <T> vec) {
out << "{";
for(int i = 0; i < int(vec.size()); i++) {
out << vec[i];
if(i +1 < int(vec.size())) {
out << ", ";
}
}
return out << "}";
}
#define ONLINE_JUDGE
i64 suml = 0, sumr = 0;
multiset <int> low, up;
void balance() {
while(int(low.size()) > int(up.size())) {
int x = *low.rbegin();
up.emplace(x);
suml -= x;
sumr += x;
low.erase(low.find(x));
}
while(int(up.size()) > int(low.size())) {
int x = *up.begin();
low.emplace(x);
suml += x;
sumr -= x;
up.erase(up.find(x));
}
}
void add(int x) {
low.emplace(x);
suml += x;
balance();
}
void solve() {
int k, n;
cin >> k >> n;
i64 total = 0;
vector <pair <int, int>> a;
for(int i = 0; i < n; i++) {
cin >> ch[0] >> x[0] >> ch[1] >> x[1];
if(ch[0] != ch[1]) {
a.emplace_back(x[0], x[1]);
} else {
total += abs(x[0] - x[1]);
}
}
n = int(a.size());
i64 ans = INF;
sort(a.begin(), a.end(), [&](pair <int, int> a, pair <int, int> b) -> bool {
return a.second + a.first < b.second + b.first;
});
vector <i64> pref(n +1);
for(int i = 0; i < n; i++) {
add(a[i].first);
add(a[i].second);
//cerr << a[i].first << " " << a[i].second << " :: " << sumr << " " << suml << "\n";
pref[i +1] = sumr - suml;
}
ans = min(ans, pref[n]);
if(k == 2) {
low.clear();
up.clear();
suml = sumr = 0;
for(int i = n; i >= 1; i--) {
add(a[i -1].first);
add(a[i -1].second);
//cerr << a[i].first << " " << a[i].second << " :: " << sumr << " " << suml << " | " << pref[i -1] << "\n";
ans = min(ans, pref[i -1] + sumr - suml);
}
}
cout << ans + total + n;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 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 |
12 |
Correct |
87 ms |
11168 KB |
Output is correct |
13 |
Correct |
107 ms |
11320 KB |
Output is correct |
14 |
Correct |
92 ms |
10184 KB |
Output is correct |
15 |
Correct |
56 ms |
6860 KB |
Output is correct |
16 |
Correct |
74 ms |
11224 KB |
Output is correct |
17 |
Correct |
88 ms |
11380 KB |
Output is correct |
18 |
Correct |
66 ms |
11196 KB |
Output is correct |
19 |
Correct |
100 ms |
11212 KB |
Output is correct |
20 |
Correct |
93 ms |
11328 KB |
Output is correct |
21 |
Correct |
94 ms |
11336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
1 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 |
348 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
161 ms |
11380 KB |
Output is correct |
26 |
Correct |
205 ms |
11216 KB |
Output is correct |
27 |
Correct |
210 ms |
11468 KB |
Output is correct |
28 |
Correct |
224 ms |
11232 KB |
Output is correct |
29 |
Correct |
195 ms |
11404 KB |
Output is correct |
30 |
Correct |
89 ms |
6060 KB |
Output is correct |
31 |
Correct |
127 ms |
11212 KB |
Output is correct |
32 |
Correct |
138 ms |
11464 KB |
Output is correct |
33 |
Correct |
121 ms |
11408 KB |
Output is correct |
34 |
Correct |
159 ms |
11412 KB |
Output is correct |
35 |
Correct |
180 ms |
11356 KB |
Output is correct |
36 |
Correct |
140 ms |
11208 KB |
Output is correct |
37 |
Correct |
146 ms |
11628 KB |
Output is correct |