#include<bits/stdc++.h>
#define int long long
#define pii pair<int,ll>
#define fi first
#define se second
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")*/
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const ll N = (int)1e9 + 1;
const int maxN = (int)1e6 + 1;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int infty = 1e18 + 7;
const int base = (int)4e5;
void InputFile()
{
//freopen("scrivener.inp","r",stdin);
//freopen("scrivener.out","w",stdout);
//freopen("test.out","r",stdin);
}
void FastInput()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
int k,n;
char P[maxN],Q[maxN];
int S[maxN],T[maxN];
vector<int> pnt;
vector<int> vc;
vector<int> vc1;
void Read()
{
cin >> k >> n;
int su = 0;
int res = infty;
int dak = 0;
for(int i = 1;i <= n;i++)
{
cin >> P[i] >> S[i] >> Q[i] >> T[i];
pnt.push_back(S[i]);
pnt.push_back(T[i]);
if(P[i] != Q[i])
{
vc.push_back(S[i]);
vc.push_back(T[i]);
dak++;
}
else
{
su += abs(S[i]-T[i]);
}
}
if(vc.empty())
{
cout << su;
return;
}
sort(vc.begin(),vc.end());
vc1.resize(vc.size());
for(int i = 0;i < vc.size();i++)
{
if(i == 0)
{
vc1[i] = vc[i];
}
else
{
vc1[i] = vc[i] + vc1[i-1];
}
}
for(int i : pnt)
{
int check = 0;
int id = upper_bound(vc.begin(),vc.end(),i) - vc.begin();
int chigh = vc.size() - id;
int clow = id;
if(clow > 0) check += i * clow - vc1[clow-1];
int h = vc1[(int)vc1.size()-1];
if(id > 0) h -= vc1[id-1];
check += h - i * chigh;
res = min(res,check + su + dak);
}
cout << res;
}
void Solve()
{
}
void Debug()
{
}
int32_t main()
{
FastInput();
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve();
//Prepare();
int test;
//cin >> test;
test = 1;
while(test--)
//for(int prc = 1; prc <= test; prc++)
{
Read();
Solve();
//Debug();
}
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
politicaldevelopment.cpp: In function 'void Read()':
politicaldevelopment.cpp:73:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0;i < vc.size();i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |