#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e5+10;
struct st{
int a,b,sr;
};
st t[N];
multiset<pair<int,int> > mu;
multiset<pair<int,int> >::iterator it;
ll summ,sumw;
int ilem,ilew,curr;
ll val1[N],val2[N];
void add(int x,int ind){
mu.insert({x,ind});
if(x>=curr) ilew++,sumw+=(ll)x;
else summ+=(ll)x,ilem++;
if(ilew>ilem){
ilew--,ilem++;
summ+=(ll)curr;
it++;
curr=(*it).fi;
sumw-=(ll)curr;
}else{
if(ilem-1>ilew){
ilem--,ilew++;
sumw+=(ll)curr;
it--;
curr=(*it).fi;
summ-=(ll)curr;
}
}
}
void solve(){
int n,k;
cin>>k>>n;
ll res=0;
int l=0;
for(int i=1;i<=n;i++){
char a,c;
int b,d;
cin>>a>>b>>c>>d;
if(a==c) res+=(ll)abs(d-b);
else{
res++;
st pom;
pom.a=b,pom.b=d,pom.sr=(b+d);
t[++l]=pom;
}
}
n=l;
if(n==0){
cout<<res<<"\n";
return;
}
if(n==1){
cout<<res+(ll)abs(t[1].a-t[1].b)<<"\n";
return;
}
sort(t+1,t+1+n,[](st a,st b){
return a.sr<b.sr;
});
mu.insert({t[1].a,1});
curr=t[1].a;
it=mu.begin();
add(t[1].b,2);
if(k==1){
for(int i=2;i<=n;i++){
add(t[i].a,i*2-1);
add(t[i].b,i*2);
}
cout<<(ll)curr*(ll)ilem-summ+sumw-(ll)curr*(ll)ilew+res<<"\n";
}else{
val1[1]=(ll)curr*(ll)ilem-summ+sumw-(ll)curr*(ll)ilew;
for(int i=2;i<=n;i++){
add(t[i].a,i*2-1);
add(t[i].b,i*2);
val1[i]=(ll)curr*(ll)ilem-summ+sumw-(ll)curr*(ll)ilew;
}
sumw=0,summ=0,ilem=0,ilew=0,curr=0;
mu.clear();
mu.insert({t[n].a,1});
curr=t[n].a;
it=mu.begin();
add(t[n].b,2);
val2[n]=(ll)curr*(ll)ilem-summ+sumw-(ll)curr*(ll)ilew;
for(int i=n-1;i>=1;i--){
add(t[i].a,(n-i+1)*2-1);
add(t[i].b,(n-i+1)*2);
val2[i]=(ll)curr*(ll)ilem-summ+sumw-(ll)curr*(ll)ilew;
}
ll res2=LLONG_MAX;
for(int i=1;i<=n-1;i++) res2=min(res2,val1[i]+val2[i+1]);
cout<<res2+res<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
58 ms |
11592 KB |
Output is correct |
13 |
Correct |
106 ms |
13144 KB |
Output is correct |
14 |
Correct |
90 ms |
10920 KB |
Output is correct |
15 |
Correct |
52 ms |
7828 KB |
Output is correct |
16 |
Correct |
77 ms |
12488 KB |
Output is correct |
17 |
Correct |
60 ms |
13128 KB |
Output is correct |
18 |
Correct |
60 ms |
12796 KB |
Output is correct |
19 |
Correct |
65 ms |
13144 KB |
Output is correct |
20 |
Correct |
66 ms |
12684 KB |
Output is correct |
21 |
Correct |
63 ms |
12892 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 |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 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 |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 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 |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
324 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 |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
356 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 |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
472 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 |
472 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
95 ms |
13192 KB |
Output is correct |
26 |
Correct |
155 ms |
13380 KB |
Output is correct |
27 |
Correct |
209 ms |
14248 KB |
Output is correct |
28 |
Correct |
198 ms |
14656 KB |
Output is correct |
29 |
Correct |
192 ms |
14688 KB |
Output is correct |
30 |
Correct |
84 ms |
7880 KB |
Output is correct |
31 |
Correct |
108 ms |
13984 KB |
Output is correct |
32 |
Correct |
90 ms |
14660 KB |
Output is correct |
33 |
Correct |
92 ms |
14264 KB |
Output is correct |
34 |
Correct |
106 ms |
14656 KB |
Output is correct |
35 |
Correct |
98 ms |
14276 KB |
Output is correct |
36 |
Correct |
93 ms |
14444 KB |
Output is correct |
37 |
Correct |
94 ms |
13120 KB |
Output is correct |