#include <bits/stdc++.h>
using namespace std;
using ll=long long;
struct pii {
int l, r;
bool operator<(pii b) const { return l+r<b.l+b.r; }
};
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int K, N, M=0;
cin>>K>>N;
pii A[N];
ll B[N], s=0, w=0;
for(int i=0; i<N; i++) {
char a, b;
int l, r;
cin>>a>>l>>b>>r;
if(l>r) swap(l, r);
if(a!=b) A[M++]={l, r};
else s+=r-l;
}
s+=N=M;
sort(A, A+N);
priority_queue<int> L;
priority_queue<int, vector<int>, greater<int>> R;
auto add=[&](pii v) {
L.push(v.l), R.push(v.r);
w+=v.r-v.l;
if(L.top()>R.top()) {
int l=L.top(), r=R.top();
L.pop(), R.pop();
L.push(r), R.push(l);
w+=(l-r)*2;
}
return w;
};
for(int i=0; i<N; i++) B[i]=add(A[i]);
w=0;
decltype(L)().swap(L);
decltype(R)().swap(R);
ll t=N ? B[N-1] : 0;
if(K==2 && N) for(int i=N; --i;) t=min(t, add(A[i])+B[i-1]);
cout<<s+t<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
30 ms |
3008 KB |
Output is correct |
13 |
Correct |
57 ms |
2756 KB |
Output is correct |
14 |
Correct |
45 ms |
2596 KB |
Output is correct |
15 |
Correct |
33 ms |
1860 KB |
Output is correct |
16 |
Correct |
33 ms |
2752 KB |
Output is correct |
17 |
Correct |
54 ms |
2864 KB |
Output is correct |
18 |
Correct |
34 ms |
2752 KB |
Output is correct |
19 |
Correct |
61 ms |
2756 KB |
Output is correct |
20 |
Correct |
43 ms |
2780 KB |
Output is correct |
21 |
Correct |
62 ms |
2864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
34 ms |
3116 KB |
Output is correct |
26 |
Correct |
59 ms |
3108 KB |
Output is correct |
27 |
Correct |
75 ms |
3100 KB |
Output is correct |
28 |
Correct |
80 ms |
3100 KB |
Output is correct |
29 |
Correct |
81 ms |
3104 KB |
Output is correct |
30 |
Correct |
41 ms |
1824 KB |
Output is correct |
31 |
Correct |
34 ms |
3128 KB |
Output is correct |
32 |
Correct |
78 ms |
3116 KB |
Output is correct |
33 |
Correct |
37 ms |
3120 KB |
Output is correct |
34 |
Correct |
84 ms |
3088 KB |
Output is correct |
35 |
Correct |
54 ms |
3108 KB |
Output is correct |
36 |
Correct |
82 ms |
3048 KB |
Output is correct |
37 |
Correct |
27 ms |
3116 KB |
Output is correct |