#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
const ll INF=1e18;
int main(){
ll k,n;
cin>>k>>n;
ll l[n+5],r[n+5];
ll dist=0,cur=0;
vector<ll>v;
for(ll i=1;i<=n;i++){
char a,b;
ll p,q;
cin>>a>>p>>b>>q;
dist+=abs(p-q);
if(a!=b){
l[cur]=min(p,q);
r[cur]=max(p,q);
cur++;
v.pb(p);
v.pb(q);
}
}
dist+=cur;
if(v.size()==0){
cout<<dist;
return 0;
}
sort(l,l+cur);
sort(r,r+cur);
sort(all(v));
ll suml=0,sumr=0;
for(ll i=0;i<cur;i++){
sumr+=l[i];
}
ll c=0,d=0;
ll ans=INF;
for(auto x:v){
while(c<cur&&r[c]<x){
suml+=r[c];
c++;
}
while(d<cur&&l[d]<=x){
sumr-=l[d];
d++;
}
ans=min(ans,dist+(x*c-suml)*2+(sumr-x*(cur-d))*2);
}
cout<<ans;
}
# |
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 |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
356 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
440 KB |
Output is correct |
11 |
Correct |
2 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 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 |
312 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
49 ms |
4780 KB |
Output is correct |
13 |
Correct |
116 ms |
5792 KB |
Output is correct |
14 |
Correct |
75 ms |
4932 KB |
Output is correct |
15 |
Correct |
65 ms |
3516 KB |
Output is correct |
16 |
Correct |
79 ms |
5184 KB |
Output is correct |
17 |
Correct |
95 ms |
5828 KB |
Output is correct |
18 |
Correct |
91 ms |
5544 KB |
Output is correct |
19 |
Correct |
110 ms |
5812 KB |
Output is correct |
20 |
Correct |
81 ms |
5436 KB |
Output is correct |
21 |
Correct |
109 ms |
5564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |