#include<bits/stdc++.h>
using namespace std;
const int N=200005;
struct fhq
{
multiset<int>s1,s2;
long long sum1,sum2;
void bal()
{
while(s1.size()>s2.size())
{
auto it=(--s1.end());
s2.insert(*it);
sum2+=*it;
sum1-=*it;
s1.erase(it);
}
while(s1.size()<s2.size())
{
auto it=s2.begin();
s1.insert(*it);
sum1+=*it;
sum2-=*it;
s2.erase(it);
}
}
void add(int v)
{
s1.insert(v);
sum1+=v;
bal();
}
void del(int v)
{
if(s1.find(v)!=s1.end())
{
s1.erase(s1.find(v));
sum1-=v;
}
else
{
s2.erase(s2.find(v));
sum2-=v;
}
bal();
}
long long sol()
{
if(!s2.size())
return 0;
long long k=*s2.begin();
return k*s1.size()-sum1+sum2-k*s2.size();
}
}t1,t2;
int n,k;
long long tot,ans;
struct node
{
int l,r;
bool operator<(const node k)const
{
return l+r<k.l+k.r;
}
};
vector<node>a;
int main()
{
ios::sync_with_stdio(false);
cin>>k>>n;
for(int i=1;i<=n;i++)
{
int l,r;
char p,q;
cin>>p>>l>>q>>r;
if(p==q)
tot+=abs(l-r);
else
{
a.push_back({l,r});
t2.add(l);
t2.add(r);
tot++;
}
}
ans=t1.sol()+t2.sol();
if(k==1)
{
ans+=tot;
cout<<ans<<endl;
return 0;
}
sort(a.begin(),a.end());
for(auto i:a)
{
int l=i.l,r=i.r;
t1.add(l);
t1.add(r);
t2.del(l);
t2.del(r);
ans=min(ans,t1.sol()+t2.sol());
}
ans+=tot;
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
95 ms |
10464 KB |
Output is correct |
13 |
Correct |
186 ms |
10464 KB |
Output is correct |
14 |
Correct |
179 ms |
9440 KB |
Output is correct |
15 |
Correct |
94 ms |
6372 KB |
Output is correct |
16 |
Correct |
95 ms |
10464 KB |
Output is correct |
17 |
Correct |
119 ms |
10464 KB |
Output is correct |
18 |
Correct |
122 ms |
10568 KB |
Output is correct |
19 |
Correct |
160 ms |
10464 KB |
Output is correct |
20 |
Correct |
108 ms |
10464 KB |
Output is correct |
21 |
Correct |
111 ms |
10464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
3 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
208 ms |
10548 KB |
Output is correct |
26 |
Correct |
509 ms |
10596 KB |
Output is correct |
27 |
Correct |
587 ms |
12464 KB |
Output is correct |
28 |
Correct |
597 ms |
13024 KB |
Output is correct |
29 |
Correct |
644 ms |
12896 KB |
Output is correct |
30 |
Correct |
211 ms |
7012 KB |
Output is correct |
31 |
Correct |
205 ms |
12256 KB |
Output is correct |
32 |
Correct |
274 ms |
13156 KB |
Output is correct |
33 |
Correct |
228 ms |
12512 KB |
Output is correct |
34 |
Correct |
298 ms |
12896 KB |
Output is correct |
35 |
Correct |
217 ms |
12544 KB |
Output is correct |
36 |
Correct |
235 ms |
12768 KB |
Output is correct |
37 |
Correct |
238 ms |
11360 KB |
Output is correct |