#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,
tree_order_statistics_node_update> oset;
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back
#define sp <<" "<<
#define cont(x) for(auto el:x) cout<<el<<' ';cout<<endl;
#define contp(x) for(auto el:x) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;
#define DEBUG(x) cout<<#x sp x<<endl;
#define carp(x,y) ((x%MOD)*(y%MOD))%MOD
#define topla(x,y) ((x%MOD)+(y%MOD))%MOD
#define mid (l+r)/2
const int MAXN=3e4+5;
const int MOD=1e9+7;
const int INF=1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,k;
cin>>k>>n;
vector<char> p(n+1), q(n+1);
vi s(n+1), t(n+1);
oset bir;
int top,tane,ekle;
top=tane=ekle=0;
FORE(i,1,n+1){
cin>>p[i]>>s[i]>>q[i]>>t[i];
if(p[i]!=q[i]){
bir.insert({t[i],n+i});
bir.insert({s[i],i});
top+=s[i]+t[i];
tane+=2;
}
else ekle+=abs(t[i]-s[i]);
}
ekle+=tane/2;
int res=INF;
if(k<=2){
int sz=bir.size();
auto med=bir.find_by_order(sz/2);
int deg=0;
for(auto el:bir){
if(el.fi>med->fi) deg+=el.fi-med->fi;
else deg+=med->fi-el.fi;
}
res=min(res,deg);
}
if(k==2){
vii pref(2*n+1,{0,0});
int once=-1;
FOR(i,bir.size()){
auto deg=*bir.find_by_order(i);
pref[deg.se].fi=(once>=0?pref[once].fi:0)+deg.fi;
pref[deg.se].se=(once>=0?pref[once].se:0)+1;
once=deg.se;
}
vi tut(2*n+1);
int ind=(int) bir.size()-1;
int ekle2=0;
int ekle3=0;
oset iki;
map<int,int> say;
for(auto el:bir){
say[(el.se<=n?el.se:el.se-n)]++;
iki.insert(el);
}
while(!bir.empty()){
int sz=bir.size();
auto med=bir.find_by_order(sz/2);
int tane1=pref[med->se].se;
int tane2=tane-tane1;
int top1=pref[med->se].fi;
int top2=top-top1;
tut[ind]+=(med->fi*tane1-top1)+(top2+ekle2-med->fi*(tane2+ekle3));
auto deg=*bir.rbegin();
top-=deg.fi;
tane--;
int ind2=deg.se;
if(ind2>n) ind2-=n;
if(say[ind2]==2){ //baş içerde son dışarda
ekle2+=max(t[ind2],s[ind2]);
ekle3++;
}
else if(say[ind2]==1){
ekle2-=max(t[ind2],s[ind2]);
ekle3--;
}
say[ind2]--;
bir.erase(deg);
ind--;
}
top=tane=0;
ind=iki.size()-1;
ekle2=0;
ekle3=0;
int dur=iki.size();
while(dur!=bir.size()+1){
auto deg=*iki.rbegin();
int ind2=deg.se;
if(ind2>n) ind2-=n;
bir.insert(deg);
int sz=bir.size();
auto med=bir.find_by_order((sz-1)/2);
if(say[ind2]==0){ //baş içerde son dışarda
ekle2-=max(t[ind2],s[ind2]);
ekle3--;
}
else if(say[ind2]==1){
ekle2+=max(t[ind2],s[ind2]);
ekle3++;
}
say[ind2]++;
top+=deg.fi;
tane++;
int tane2=pref.back().se-pref[med->se].se;
int tane1=tane-tane2;
int top2=pref.back().fi-pref[med->se].fi;
int top1=top-top2;
tut[ind-1]+=(med->fi*tane1-top1)+(top2+ekle2-med->fi*(tane2+ekle3));
iki.erase(deg);
ind--;
}
FOR(i,n) res=min(res,tut[i]);
}
cout<<ekle+res<<endl;
}
# | 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... |