#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#include<random>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
vector<pair<int,int>>arr;
ll eval(int br){
ll res=0;
for(auto[i,j]:arr)
res+=2*max(0,max(i-br,br-j));
return res;
}
void solve(){
int k,n;
cin>>k>>n;
ll res1=0;
for(int i=0;i<n;i++){
char c1,c2;
int a1,a2;
cin>>c1>>a1>>c2>>a2;
res1+=abs(a1-a2);
if(c1!=c2){
res1+=k;
if(a1>a2)
swap(a1,a2);
arr.emplace_back(a1,a2);
}
}
vector<int>pos;
for(auto[i,j]:arr){
pos.push_back(i);
pos.push_back(j);
}
sort(pos.begin(),pos.end());
ll res2=1e18;
if(k==1){
for(int i:pos)
res2=min(res2,eval(i));
}else{ // k == 2
}
cout<<res1+res2<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("/Users/jakob/cp_testing/test.txt","r",stdin);//freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
24
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |