#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define endl "\n"
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
void solve()
{
ll n, k;
cin >> k >> n;
vector<pair<ll, ll>> a;
ll res = 0, sum = 0;
for (ll i = 0; i < n; i++)
{
ll x, y;
char c, d;
cin >> c >> x >> d >> y;
sum += abs(x - y);
if (c != d)
a.push_back(make_pair(min(x, y), max(x, y)));
else
res += abs(x - y);
}
n = a.size();
if (k == 1)
{
vector<ll> l, r;
for (auto i : a)
l.push_back(i.first), r.push_back(i.second);
sort(l.begin(), l.end());
sort(r.begin(), r.end());
ll ans = a.empty() ? sum : 2e18;
ll pref[n], pref1[n];
for (ll i = 0; i < n; i++)
pref[i] = (i == 0 ? 0 : pref[i - 1]) + l[i];
for (ll i = 0; i < n; i++)
pref1[i] = (i == 0 ? 0 : pref1[i - 1]) + r[i];
for (auto _i : a)
{
for (ll x : {_i.first, _i.second})
{
ll pos = upper_bound(l.begin(), l.end(), x) - l.begin(), pos1 = lower_bound(r.begin(), r.end(), x) - r.begin();
ans = min(ans, ((pref[n - 1] - (pos - 1 < 0 ? 0 : pref[pos - 1])) - x * (n - pos) + x * pos1 - (pos1 - 1 < 0 ? 0 : pref1[pos1 - 1])) * 2 + sum + n);
}
}
cout << ans << endl;
return;
}
sort(a.begin(), a.end(), [&](pair<ll, ll> a, pair<ll, ll> b)
{ return a.first + a.second < b.first + b.second; });
ll pref[n], suf[n];
multiset<ll> l, r;
ll suml = 0, sumr = 0;
for (ll i = 0; i < n; i++)
{
l.insert(a[i].first);
r.insert(a[i].second);
suml += a[i].first;
sumr += a[i].second;
while (!l.empty() and !r.empty() and *l.rbegin() > *r.begin())
{
suml -= *l.rbegin();
sumr += *l.rbegin();
r.insert(*l.rbegin());
l.erase(--l.end());
}
while (l.size() > r.size())
{
suml -= *l.rbegin();
sumr += *l.rbegin();
r.insert(*l.rbegin());
l.erase(--l.end());
}
while (r.size() > l.size())
{
suml += *r.begin();
sumr -= *r.begin();
l.insert(*r.begin());
r.erase(r.begin());
}
pref[i] = sumr - suml;
}
l.clear();
r.clear();
suml = sumr = 0;
for (ll i = n - 1; i >= 0; i--)
{
l.insert(a[i].first);
r.insert(a[i].second);
suml += a[i].first;
sumr += a[i].second;
while (!l.empty() and !r.empty() and *l.rbegin() > *r.begin())
{
suml -= *l.rbegin();
sumr += *l.rbegin();
r.insert(*l.rbegin());
l.erase(--l.end());
}
while (l.size() > r.size())
{
suml -= *l.rbegin();
sumr += *l.rbegin();
r.insert(*l.rbegin());
l.erase(--l.end());
}
while (r.size() > l.size())
{
suml += *r.begin();
sumr -= *r.begin();
l.insert(*r.begin());
r.erase(r.begin());
}
suf[i] = sumr - suml;
}
ll ans = pref[n - 1] + res + n;
for (ll i = 0; i + 1 < n; i++) ans = min(ans, pref[i] + suf[i + 1] + res + n);
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// precomp();
// cin >> t;
for (ll cs = 1; cs <= t; cs++)
solve();
// cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# | 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... |