#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.first + a.second < b.first + b.second;
});
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;
}
# |
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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
484 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
90 ms |
11176 KB |
Output is correct |
13 |
Correct |
102 ms |
11164 KB |
Output is correct |
14 |
Correct |
95 ms |
10188 KB |
Output is correct |
15 |
Correct |
63 ms |
6736 KB |
Output is correct |
16 |
Correct |
77 ms |
11304 KB |
Output is correct |
17 |
Correct |
94 ms |
11216 KB |
Output is correct |
18 |
Correct |
66 ms |
11368 KB |
Output is correct |
19 |
Correct |
105 ms |
11236 KB |
Output is correct |
20 |
Correct |
97 ms |
11212 KB |
Output is correct |
21 |
Correct |
92 ms |
11204 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 |
1 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 |
424 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 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 |
460 KB |
Output is correct |
12 |
Correct |
1 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 |
600 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 |
460 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 |
492 KB |
Output is correct |
24 |
Correct |
1 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 |
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 |
1 ms |
344 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 |
344 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 |
2 ms |
344 KB |
Output is correct |
20 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
150 ms |
12100 KB |
Output is correct |
26 |
Correct |
196 ms |
12224 KB |
Output is correct |
27 |
Correct |
218 ms |
13148 KB |
Output is correct |
28 |
Correct |
190 ms |
13580 KB |
Output is correct |
29 |
Correct |
190 ms |
13732 KB |
Output is correct |
30 |
Correct |
86 ms |
7588 KB |
Output is correct |
31 |
Correct |
132 ms |
13052 KB |
Output is correct |
32 |
Correct |
139 ms |
13696 KB |
Output is correct |
33 |
Correct |
119 ms |
13356 KB |
Output is correct |
34 |
Correct |
151 ms |
13544 KB |
Output is correct |
35 |
Correct |
171 ms |
13296 KB |
Output is correct |
36 |
Correct |
139 ms |
13360 KB |
Output is correct |
37 |
Correct |
146 ms |
12168 KB |
Output is correct |