#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
const ll OO = 1e18;
ll best = OO, extra = 0;
vector<ll> fi, se;
vector<pii> seg;
multiset<int> big, sml;
int k, n;
void solve(vector<ll> &vc){
vc[0] = 0;
ll sum_big = 0ll;
ll sum_sml = 0ll;
big.clear();
sml.clear();
int mid = 0;
for (int i = 0; i < sz(seg); i++) {
if (seg[i].ft <= mid && seg[i].sd >= mid){
big.insert(seg[i].sd);
sml.insert(seg[i].ft);
sum_big += seg[i].sd;
sum_sml += seg[i].ft;
} else if (seg[i].sd < mid){
sml.insert(seg[i].sd);
sml.insert(seg[i].ft);
sum_sml += seg[i].ft;
sum_sml += seg[i].sd;
ll cur = *(--sml.end());
sum_big += cur;
sum_sml -= cur;
sml.erase(--sml.end());
big.insert(cur);
} else {
big.insert(seg[i].sd);
big.insert(seg[i].ft);
sum_big += seg[i].ft;
sum_big += seg[i].sd;
ll cur = *(big.begin());
sum_sml += cur;
sum_big -= cur;
big.erase(big.begin());
sml.insert(cur);
}
vc[i + 1] = sum_big - sum_sml;
mid = (*big.begin());
}
}
bool cmp(pii x, pii y){
return (x.ft + x.sd) < (y.ft + y.sd);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> k >> n;
for (int i = 0; i < n; i++){
char c1, c2; int x, y;
cin >> c1 >> x >> c2 >> y;
if (c1 == c2){
extra += abs(x - y);
} else {
extra++;
if (x > y) swap(x, y);
seg.PB(MP(x, y));
}
}
sort(all(seg), cmp);
fi.resize(sz(seg) + 1);
se.resize(sz(seg) + 1);
solve(fi);
reverse(all(seg));
solve(se);
reverse(all(se));
int bd = (k == 1 ? 1 : sz(fi));
for (int i = 0; i < bd; i++)
best = min(best, fi[i] + se[i]);
if (best == OO)
best = 0;
cout << extra + best;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
159 ms |
12060 KB |
Output is correct |
13 |
Correct |
370 ms |
12124 KB |
Output is correct |
14 |
Correct |
285 ms |
10992 KB |
Output is correct |
15 |
Correct |
167 ms |
7284 KB |
Output is correct |
16 |
Correct |
160 ms |
12140 KB |
Output is correct |
17 |
Correct |
196 ms |
12240 KB |
Output is correct |
18 |
Correct |
124 ms |
12012 KB |
Output is correct |
19 |
Correct |
197 ms |
12144 KB |
Output is correct |
20 |
Correct |
185 ms |
12288 KB |
Output is correct |
21 |
Correct |
196 ms |
12148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
636 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
388 KB |
Output is correct |
15 |
Correct |
4 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
452 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
162 ms |
13036 KB |
Output is correct |
26 |
Correct |
267 ms |
13088 KB |
Output is correct |
27 |
Correct |
405 ms |
14116 KB |
Output is correct |
28 |
Correct |
361 ms |
14420 KB |
Output is correct |
29 |
Correct |
351 ms |
14504 KB |
Output is correct |
30 |
Correct |
169 ms |
7924 KB |
Output is correct |
31 |
Correct |
171 ms |
13736 KB |
Output is correct |
32 |
Correct |
206 ms |
14448 KB |
Output is correct |
33 |
Correct |
136 ms |
14064 KB |
Output is correct |
34 |
Correct |
202 ms |
14404 KB |
Output is correct |
35 |
Correct |
187 ms |
14160 KB |
Output is correct |
36 |
Correct |
201 ms |
14324 KB |
Output is correct |
37 |
Correct |
157 ms |
13164 KB |
Output is correct |