/* code by the cute PixelCat owo */
/* as cute as nacho neko (aka. my wife)! */
//#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#define int LL //__int128
#define double long double
using namespace std;
using LL=long long;
using uLL=unsigned long long;
using pii=pair<LL,LL>;
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Fors(i,a,b,s) for(int i=a;i<=b;i+=s)
#define Forr(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define L(id) (id*2+1)
#define R(id) (id*2+2)
#define LO(x) (x&(-x))
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair
#define MOD (int)(1000000007)
#define INF (int)(1000000000000000ll) //1e15
#define EPS (1e-6)
#ifdef NYAOWO
#include "debug.h"
inline void USACO(const string &s) { cerr<<"USACO: "<<s<<"\n"; }
#else
#define debug(...)
inline void timer(){}
inline void USACO(const string &s){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
#endif
inline void NYA(){ ios::sync_with_stdio(false); cin.tie(0); }
inline int gcd(int a,int b) { return __gcd(a,b); }
inline int lcm(int a,int b) { return a/gcd(a,b)*b; }
int fpow(int b,int p,const int &mod){
int ans=1;
while(p){
if(p&1) ans=ans*b%mod;
p/=2; b=b*b%mod;
}
return ans;
}
int fpow(int b,int p) { return fpow(b,p,MOD); }
template<typename T> inline void chmin(T &_a,const T &_b) { if(_b<_a) _a=_b; }
template<typename T> inline void chmax(T &_a,const T &_b) { if(_b>_a) _a=_b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct DynamicMed{
int sl,sh;
multiset<int> lo,hi;
void init(){
lo.clear();
hi.clear();
sl=sh=0;
}
void bal(){
if(sz(lo)>sz(hi)+1){
int a=*lo.rbegin();
sl-=a; sh+=a;
hi.insert(a);
lo.erase(lo.find(a));
}
if(sz(lo)<sz(hi)){
int a=*hi.begin();
sl+=a; sh-=a;
hi.erase(hi.find(a));
lo.insert(a);
}
}
void insert(int k){
if(sz(hi) && k>*hi.begin()){
sh+=k;
hi.insert(k);
}else{
sl+=k;
lo.insert(k);
}
bal();
}
void remove(int k){
if(sz(hi) && k>=*hi.begin()){
sh-=k;
hi.erase(hi.find(k));
}else{
sl-=k;
lo.erase(lo.find(k));
}
bal();
}
int rape(){
int ans=sh-sl;
if(sz(lo)>sz(hi)) ans+=*lo.rbegin();
return ans;
}
void fuck(){
debug(sh,sl,hi,lo);
}
}lo,li;
int32_t main(){
NYA();
//nyaacho >/////<
// weird emojis credit to uglyfeng
// OwO
// OwQ
// QwO
// QwQ
int k,n; cin>>k>>n;
int add=0;
vector<pii> v;
For(i,1,n){
char z1,z2;
int p1,p2;
cin>>z1>>p1>>z2>>p2;
if(z1==z2){
add+=abs(p1-p2);
}else{
add++;
v.eb(p1,p2);
li.insert(p1);
li.insert(p2);
}
}
sort(all(v),[](const pii &a,const pii &b){
return a.F+a.S<b.F+b.S;
});
int ans=add+li.rape();
if(k==1){
cout<<ans<<"\n";
return 0;
}
For(i,0,sz(v)-1){
li.remove(v[i].F);
li.remove(v[i].S);
lo.insert(v[i].F);
lo.insert(v[i].S);
chmin(ans,add+lo.rape()+li.rape());
debug(i,add,lo.rape(),li.rape());
lo.fuck();
li.fuck();
}
cout<<ans<<"\n";
return 0;
}
// OwO
// UwU
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
388 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
68 ms |
12052 KB |
Output is correct |
13 |
Correct |
117 ms |
13524 KB |
Output is correct |
14 |
Correct |
105 ms |
11292 KB |
Output is correct |
15 |
Correct |
65 ms |
8116 KB |
Output is correct |
16 |
Correct |
80 ms |
12848 KB |
Output is correct |
17 |
Correct |
81 ms |
13476 KB |
Output is correct |
18 |
Correct |
76 ms |
13100 KB |
Output is correct |
19 |
Correct |
98 ms |
13488 KB |
Output is correct |
20 |
Correct |
84 ms |
13104 KB |
Output is correct |
21 |
Correct |
85 ms |
13264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
166 ms |
12016 KB |
Output is correct |
26 |
Correct |
308 ms |
12212 KB |
Output is correct |
27 |
Correct |
337 ms |
12980 KB |
Output is correct |
28 |
Correct |
361 ms |
13656 KB |
Output is correct |
29 |
Correct |
349 ms |
13572 KB |
Output is correct |
30 |
Correct |
137 ms |
7224 KB |
Output is correct |
31 |
Correct |
176 ms |
12932 KB |
Output is correct |
32 |
Correct |
177 ms |
13532 KB |
Output is correct |
33 |
Correct |
142 ms |
13156 KB |
Output is correct |
34 |
Correct |
207 ms |
13540 KB |
Output is correct |
35 |
Correct |
175 ms |
13100 KB |
Output is correct |
36 |
Correct |
161 ms |
13288 KB |
Output is correct |
37 |
Correct |
199 ms |
11996 KB |
Output is correct |