/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll lsum;
ll rsum;
multiset<int> lval;
multiset<int> rval;
void add(int u, int v) {
rsum += u;
rval.insert(u);
rsum += v;
rval.insert(v);
while (rval.size() > lval.size()) {
int x = *rval.begin();
rsum -= x;
rval.erase(rval.find(x));
lsum += x;
lval.insert(x);
}
while (*--lval.end() > *rval.begin()) {
int x = *--lval.end();
int y = *rval.begin();
lsum -= x;
lval.erase(lval.find(x));
lsum += y;
lval.insert(y);
rsum -= y;
rval.erase(rval.find(y));
rsum += x;
rval.insert(x);
}
}
vector<ll> eval(vector<pair<int, int>> vals) {
lsum = 0;
lval.clear();
rsum = 0;
rval.clear();
int n = vals.size();
vector<ll> f(1, 0);
for (int i = 0; i < n; ++i) {
add(vals[i].first, vals[i].second);
f.push_back(rsum - lsum);
}
return f;
}
int main() {
ios_base::sync_with_stdio(false);
int k, n;
cin >> k >> n;
ll ans = 0;
vector<pair<int, int>> vals;
for (int i = 0; i < n; ++i) {
string p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) {
ans += abs(s - t);
} else {
++ans;
vals.emplace_back(s, t);
}
}
sort(vals.begin(), vals.end(), [&](pair<int, int> u, pair<int, int> v) {
return u.first + u.second < v.first + v.second;
});
auto f = eval(vals);
reverse(vals.begin(), vals.end());
auto g = eval(vals);
int m = vals.size();
ll mn = 1e18;
for (int i = 0; i < (k == 1 ? 1 : m + 1); ++i) {
mn = min(mn, f[i] + g[m - i]);
}
cout << ans + mn << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
170 ms |
13252 KB |
Output is correct |
13 |
Correct |
376 ms |
13280 KB |
Output is correct |
14 |
Correct |
349 ms |
12208 KB |
Output is correct |
15 |
Correct |
212 ms |
7972 KB |
Output is correct |
16 |
Correct |
168 ms |
13280 KB |
Output is correct |
17 |
Correct |
232 ms |
13380 KB |
Output is correct |
18 |
Correct |
151 ms |
13284 KB |
Output is correct |
19 |
Correct |
217 ms |
13408 KB |
Output is correct |
20 |
Correct |
205 ms |
13504 KB |
Output is correct |
21 |
Correct |
227 ms |
13380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
428 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
356 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
4 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
528 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
388 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
177 ms |
13920 KB |
Output is correct |
26 |
Correct |
273 ms |
14176 KB |
Output is correct |
27 |
Correct |
463 ms |
14928 KB |
Output is correct |
28 |
Correct |
451 ms |
15580 KB |
Output is correct |
29 |
Correct |
480 ms |
15456 KB |
Output is correct |
30 |
Correct |
155 ms |
8244 KB |
Output is correct |
31 |
Correct |
169 ms |
14816 KB |
Output is correct |
32 |
Correct |
253 ms |
15456 KB |
Output is correct |
33 |
Correct |
139 ms |
15164 KB |
Output is correct |
34 |
Correct |
225 ms |
15456 KB |
Output is correct |
35 |
Correct |
190 ms |
15040 KB |
Output is correct |
36 |
Correct |
239 ms |
15168 KB |
Output is correct |
37 |
Correct |
178 ms |
13912 KB |
Output is correct |