#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);
using namespace std;
struct line{
char x,z;
long long y,t;
}a[100009];
struct lena{
long long y,t;
}b[100009];
long long pf[100009];
vector<long long>c;
bool cmp(const lena &p, const lena &q){
return p.y+p.t<q.y+q.t;
}
int main()
{
fastio
int k,n;
cin>>k>>n;
int cnt=0;
long long ans=0;
for (int i=1;i<=n;i++){
cin>>a[i].x>>a[i].y>>a[i].z>>a[i].t;
if (a[i].x!=a[i].z){
b[++cnt]={a[i].y,a[i].t};
c.push_back(a[i].y);
c.push_back(a[i].t);
ans++;
}
else {
ans+=abs(a[i].y-a[i].t);
}
}
if (k==1){
sort(c.begin(),c.end());
int d=(int)c.size();
d/=2;
for (int i=0;i<(int)c.size();i++){
if (c[i]<=c[d]){
ans=ans+c[d]-c[i];
}
else ans=ans+c[i]-c[d];
}
cout<<ans;
return 0;
}
else {
if (cnt==0){
cout<<ans;
return 0;
}
sort(b+1,b+1+cnt,cmp);
for (int i=1;i<cnt;i++){
vector<long long>d;
for (int j=1;j<=i;j++){
d.push_back(b[j].y);
d.push_back(b[j].t);
}
sort(d.begin(),d.end());
int r=(int)d.size();
long long gm=0;
r/=2;
for (auto v:d){
if (d[r]>=v)gm+=d[r]-v;
else gm+=v-d[r];
}
long long lgm=0;
r--;
for (auto v:d){
if (d[r]>=v)lgm+=d[r]-v;
else lgm+=v-d[r];
}
pf[i]=min(lgm,gm);
}
long long cal=1e16;
for (int i=cnt;i>1;i--){
vector<long long>d;
for (int j=cnt;j>=i;j--){
d.push_back(b[j].y);
d.push_back(b[j].t);
}
sort(d.begin(),d.end());
int r=(int)d.size();
long long gm=0;
r/=2;
for (auto v:d){
if (d[r]>=v)gm+=d[r]-v;
else gm+=v-d[r];
}
long long lgm=0;
r--;
for (auto v:d){
if (d[r]>=v)lgm+=d[r]-v;
else lgm+=v-d[r];
}
cal=min(cal,min(gm,lgm)+pf[i-1]+ans);
}
cout<<cal;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
400 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
20 ms |
6600 KB |
Output is correct |
13 |
Correct |
42 ms |
7680 KB |
Output is correct |
14 |
Correct |
28 ms |
6540 KB |
Output is correct |
15 |
Correct |
26 ms |
4732 KB |
Output is correct |
16 |
Correct |
27 ms |
7512 KB |
Output is correct |
17 |
Correct |
29 ms |
7652 KB |
Output is correct |
18 |
Correct |
34 ms |
7364 KB |
Output is correct |
19 |
Correct |
40 ms |
8212 KB |
Output is correct |
20 |
Correct |
28 ms |
7232 KB |
Output is correct |
21 |
Correct |
38 ms |
7412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
24 ms |
420 KB |
Output is correct |
14 |
Correct |
75 ms |
428 KB |
Output is correct |
15 |
Correct |
84 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
25 ms |
340 KB |
Output is correct |
18 |
Correct |
12 ms |
396 KB |
Output is correct |
19 |
Correct |
28 ms |
340 KB |
Output is correct |
20 |
Correct |
31 ms |
460 KB |
Output is correct |
21 |
Correct |
76 ms |
448 KB |
Output is correct |
22 |
Correct |
31 ms |
448 KB |
Output is correct |
23 |
Correct |
22 ms |
340 KB |
Output is correct |
24 |
Correct |
30 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
24 ms |
340 KB |
Output is correct |
14 |
Correct |
73 ms |
432 KB |
Output is correct |
15 |
Correct |
83 ms |
440 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
22 ms |
324 KB |
Output is correct |
18 |
Correct |
11 ms |
340 KB |
Output is correct |
19 |
Correct |
33 ms |
340 KB |
Output is correct |
20 |
Correct |
30 ms |
460 KB |
Output is correct |
21 |
Correct |
70 ms |
344 KB |
Output is correct |
22 |
Correct |
30 ms |
340 KB |
Output is correct |
23 |
Correct |
22 ms |
340 KB |
Output is correct |
24 |
Correct |
31 ms |
344 KB |
Output is correct |
25 |
Execution timed out |
2065 ms |
7272 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |