#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ins insert
//cout<<fixed<<setprecision(3); 3 decimalke brez fixed pa 3 zanesljiva mesta
const int MAXN=1e6+3;
const long long linf=1e18;
const int inf=1e9;
const int mod=1e9+7;
void solve(){
int k,n;cin>>k>>n;
ll res=0;
vector<int>house;
for(int i=0;i<n;i++){
char t,tt;int p,pp;
cin>>t>>p>>tt>>pp;
if(tt==t){
res+=abs(pp-p);
}
else{
res++;
house.pb(p);house.pb(pp);
}
}
sort(house.begin(),house.end());
if(k==1){
int m=house.size();
ll mn1=res;
for(int i=0;i<m;i++){
mn1+=abs(house[i]-house[m/2]);
}
ll mn2=res;
for(int i=0;i<m;i++){
mn2+=abs(house[i]-house[m/2-1]);
}
cout<<min(mn1,mn2)<<endl;
}
else{
cout<<-1<<endl;
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
int t=1;
while(t--) solve();
}
# | 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... |