This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define jizz ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef pair<int,int> pii;
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define mkp make_pair
int abs(int x){
    return x>0?x:-x;
}
vector<pii> segs;
vector<int> pos;
int main(){ jizz
    int k, n;
    cin>>k>>n;
    ll ans=0;
    for(int i=0;i<n;i++){
        char p, q;
        int s, t;
        cin>>p>>s>>q>>t;
        if(s>t) swap(s,t);
        ans+=(t-s);
        if(p!=q) {
            ans++;
            segs.pb(mkp(s,t));
            pos.pb(s);
            pos.pb(t);
        }
    }
    sort(ALL(pos));
    int sz=pos.size();
    int bp=pos[sz/2];
    for(int i=0;i<sz/2;i++){
        if(segs[i].S<bp) ans+=2*(bp-segs[i].S);
        else if(segs[i].F>bp) ans+=2*(segs[i].F-bp);
    }
    cout<<ans<<"\n";
    return 0;
}
/*
1 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 | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |