#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
long long n,k,fakeres,ps[maxn],suf[maxn];
vector<pair<long long,long long>>all;
bool cmp(pair<long long,long long>a,pair<long long,long long>b){
return a.first+a.second<b.first+b.second;
}
void vorod(){
cin>>k>>n;
for(long long i=0;i<n;i++){
char c,cc;
long long d,dd;
cin>>c>>d>>cc>>dd;
if(d>dd){
swap(d,dd);
}
if(c==cc){
fakeres+=abs(dd-d);
}else{
all.push_back(make_pair(d,dd));
fakeres++;
}
}
n=(long long)all.size();
}
priority_queue<long long>kam,ziad;
long long sumkam=0,sumziad=0;
void ins(int w){
sumkam+=w;
kam.push(w);
while((int)kam.size()>(int)ziad.size()||kam.top()>-ziad.top()){
sumkam-=kam.top();
sumziad+=kam.top();
ziad.push(-kam.top());
kam.pop();
}
while((int)kam.size()<(int)ziad.size()||kam.top()>-ziad.top()){
sumkam+=-ziad.top();
sumziad-=-ziad.top();
kam.push(-ziad.top());
ziad.pop();
}
while((int)kam.size()>(int)ziad.size()||kam.top()>-ziad.top()){
sumkam-=kam.top();
sumziad+=kam.top();
ziad.push(-kam.top());
kam.pop();
}
while((int)kam.size()<(int)ziad.size()||kam.top()>-ziad.top()){
sumkam+=-ziad.top();
sumziad-=-ziad.top();
kam.push(-ziad.top());
ziad.pop();
}
}
void pre(){
sort(all.begin(),all.end(),cmp);
for(long long i=0;i<n;i++){
ins(all[i].first);
ins(all[i].second);
ps[i]=sumziad-sumkam;
}
while((long long)kam.size()>0){
kam.pop();
ziad.pop();
}
sumkam=0,sumziad=0;
for(long long i=n-1;i>=0;i--){
ins(all[i].first);
ins(all[i].second);
suf[i]=sumziad-sumkam;
}
}
void solve(){
if(k==1){
long long mainres=fakeres+ps[n-1];
cout<<mainres<<"\n";
return ;
}
long long mainres=fakeres+suf[0];
for(long long i=0;i<n;i++){
mainres=min(mainres,fakeres+suf[i+1]+ps[i]);
}
cout<<mainres<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("inp.txt","r",stdin);
vorod();
pre();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
360 KB |
Output is correct |
11 |
Correct |
1 ms |
356 KB |
Output is correct |
12 |
Correct |
58 ms |
7264 KB |
Output is correct |
13 |
Correct |
135 ms |
7632 KB |
Output is correct |
14 |
Correct |
118 ms |
6600 KB |
Output is correct |
15 |
Correct |
78 ms |
4572 KB |
Output is correct |
16 |
Correct |
64 ms |
7392 KB |
Output is correct |
17 |
Correct |
120 ms |
7736 KB |
Output is correct |
18 |
Correct |
122 ms |
7576 KB |
Output is correct |
19 |
Correct |
124 ms |
7600 KB |
Output is correct |
20 |
Correct |
71 ms |
7644 KB |
Output is correct |
21 |
Correct |
152 ms |
7784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
61 ms |
7092 KB |
Output is correct |
26 |
Correct |
113 ms |
7372 KB |
Output is correct |
27 |
Correct |
150 ms |
7976 KB |
Output is correct |
28 |
Correct |
136 ms |
8732 KB |
Output is correct |
29 |
Correct |
146 ms |
8800 KB |
Output is correct |
30 |
Correct |
79 ms |
4248 KB |
Output is correct |
31 |
Correct |
64 ms |
7752 KB |
Output is correct |
32 |
Correct |
120 ms |
8384 KB |
Output is correct |
33 |
Correct |
129 ms |
7996 KB |
Output is correct |
34 |
Correct |
124 ms |
8136 KB |
Output is correct |
35 |
Correct |
70 ms |
7792 KB |
Output is correct |
36 |
Correct |
124 ms |
8216 KB |
Output is correct |
37 |
Correct |
72 ms |
7112 KB |
Output is correct |