#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
int main(){
int k, n;
cin >> k >> n;
vector<pair<ll, ll>> deals;
ll ans = 0;
for (int i = 0; i < n; i++){
char a, b;
ll c, d;
cin >> a >> c >> b >> d;
if (a == b) ans += max(d,c)-min(d,c);
else deals.push_back({min(c,d), max(c,d)});
}
ans += deals.size();
if (k == 1){
vector<ll> v;
for (auto x : deals) v.push_back(x.first), v.push_back(x.second);
sort(v.begin(), v.end());
ll med = v[v.size()/2];
for (ll x : v) ans += abs(x-med);
}else{
sort(deals.begin(), deals.end(), [&](pair<ll,ll> x, pair<ll,ll> y){
return (x.first+x.second)<(y.first+y.second);
});
n = (int) deals.size();
ll prefix[n+1];
ll suffix[n+1];
prefix[0] = suffix[n] = 0;
ll sum = 0;
ll other = 0;
multiset<ll> values;
for (int i = 0; i < n; i++){
sum += deals[i].first + deals[i].second;
values.insert(deals[i].first);
values.insert(deals[i].second);
other += *values.begin();
values.erase(values.begin());
prefix[i+1] = sum - other - other;
}
values.clear();
sum = other = 0;
for (int i = (n-1); i > -1; i--){
sum += deals[i].first + deals[i].second;
values.insert(deals[i].first);
values.insert(deals[i].second);
other += *--values.end();
values.erase(--values.end());
suffix[i] = other + other - sum;
}
ll best = prefix[0]+suffix[0];
for (int i = 1; i <= n; i++) best = min(best, prefix[i]+suffix[i]);
ans += best;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
448 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 |
# |
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 |
464 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 |
32 ms |
5312 KB |
Output is correct |
13 |
Correct |
67 ms |
5056 KB |
Output is correct |
14 |
Correct |
48 ms |
4796 KB |
Output is correct |
15 |
Correct |
39 ms |
2832 KB |
Output is correct |
16 |
Correct |
62 ms |
5060 KB |
Output is correct |
17 |
Correct |
65 ms |
5052 KB |
Output is correct |
18 |
Correct |
55 ms |
5060 KB |
Output is correct |
19 |
Correct |
65 ms |
5008 KB |
Output is correct |
20 |
Correct |
51 ms |
5060 KB |
Output is correct |
21 |
Correct |
59 ms |
5052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
344 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
344 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 |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
440 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
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 |
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 |
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 |
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 |
0 ms |
432 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 |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
62 ms |
8792 KB |
Output is correct |
26 |
Correct |
97 ms |
9164 KB |
Output is correct |
27 |
Correct |
114 ms |
9676 KB |
Output is correct |
28 |
Correct |
127 ms |
10340 KB |
Output is correct |
29 |
Correct |
128 ms |
10456 KB |
Output is correct |
30 |
Correct |
65 ms |
5576 KB |
Output is correct |
31 |
Correct |
73 ms |
9668 KB |
Output is correct |
32 |
Correct |
90 ms |
10260 KB |
Output is correct |
33 |
Correct |
77 ms |
10016 KB |
Output is correct |
34 |
Correct |
104 ms |
10256 KB |
Output is correct |
35 |
Correct |
81 ms |
9920 KB |
Output is correct |
36 |
Correct |
92 ms |
10076 KB |
Output is correct |
37 |
Correct |
71 ms |
8884 KB |
Output is correct |