#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int k,n;
cin>>k>>n;
vector <char> t1(n),t2(n);
vector <int> x(n),y(n);
int ans=0;
vector <vector <int> > v;
for(int i=0;i<n;i++){
cin>>t1[i]>>x[i]>>t2[i]>>y[i];
if(t1[i]==t2[i]){
ans+=abs(x[i]-y[i]);
}
else{
v.pb({x[i]+y[i],x[i],y[i]});
ans++;
}
}
sort(all(v));
multiset <int> l,r;
vector <int> pr;
int suml=0,sumr=0;
for(int i=0;i<v.size();i++){
if(l.size()!=0 && *l.rbegin()>v[i][1]){
l.insert(v[i][1]);
suml+=v[i][1];
}
else{
r.insert(v[i][1]);
sumr+=v[i][1];
}
if(l.size()!=0 && *l.rbegin()>v[i][2]){
l.insert(v[i][2]);
suml+=v[i][2];
}
else{
r.insert(v[i][2]);
sumr+=v[i][2];
}
while(r.size()>l.size()){
int cur=*r.begin();
r.erase(r.begin());
sumr-=cur;
l.insert(cur);
suml+=cur;
}
while(l.size()>r.size()+1){
int cur=*l.rbegin();
auto it=l.end();it--;
l.erase(it);
suml-=cur;
r.insert(cur);
sumr+=cur;
}
int med=*l.rbegin();
int x=l.size(),y=r.size();
pr.pb((med*x-suml)+(sumr-y*med));
}
if(k==1){
if(pr.size()!=0)ans+=pr.back();
cout<<ans<<"\n";return 0;
}
l.clear();r.clear();
suml=0;sumr=0;
int mn=1e18;
for(int i=v.size()-1;i>=0;i--){
if(l.size()!=0 && *l.rbegin()>v[i][1]){
l.insert(v[i][1]);
suml+=v[i][1];
}
else{
r.insert(v[i][1]);
sumr+=v[i][1];
}
if(l.size()!=0 && *l.rbegin()>v[i][2]){
l.insert(v[i][2]);
suml+=v[i][2];
}
else{
r.insert(v[i][2]);
sumr+=v[i][2];
}
while(r.size()>l.size()){
int cur=*r.begin();
r.erase(r.begin());
sumr-=cur;
l.insert(cur);
suml+=cur;
}
while(l.size()>r.size()+1){
int cur=*l.rbegin();
auto it=l.end();it--;
l.erase(it);
suml-=cur;
r.insert(cur);
sumr+=cur;
}
int med=*l.rbegin();
int x=l.size(),y=r.size();
int sf=(med*x-suml)+(sumr-y*med);
if(i-1>=0)sf+=pr[i-1];
mn=min(mn,sf);
}
if(mn==1e18)mn=0;
cout<<mn+ans<<"\n";
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:31:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
448 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
1 ms |
448 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
644 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
92 ms |
18528 KB |
Output is correct |
13 |
Correct |
155 ms |
19972 KB |
Output is correct |
14 |
Correct |
117 ms |
17200 KB |
Output is correct |
15 |
Correct |
82 ms |
11976 KB |
Output is correct |
16 |
Correct |
105 ms |
19428 KB |
Output is correct |
17 |
Correct |
114 ms |
20016 KB |
Output is correct |
18 |
Correct |
105 ms |
19716 KB |
Output is correct |
19 |
Correct |
133 ms |
20180 KB |
Output is correct |
20 |
Correct |
143 ms |
19620 KB |
Output is correct |
21 |
Correct |
115 ms |
19856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
432 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
125 ms |
18440 KB |
Output is correct |
26 |
Correct |
137 ms |
18656 KB |
Output is correct |
27 |
Correct |
210 ms |
19464 KB |
Output is correct |
28 |
Correct |
238 ms |
19996 KB |
Output is correct |
29 |
Correct |
234 ms |
20072 KB |
Output is correct |
30 |
Correct |
101 ms |
10760 KB |
Output is correct |
31 |
Correct |
141 ms |
19456 KB |
Output is correct |
32 |
Correct |
150 ms |
19972 KB |
Output is correct |
33 |
Correct |
125 ms |
19600 KB |
Output is correct |
34 |
Correct |
166 ms |
20180 KB |
Output is correct |
35 |
Correct |
147 ms |
19544 KB |
Output is correct |
36 |
Correct |
155 ms |
19908 KB |
Output is correct |
37 |
Correct |
130 ms |
18432 KB |
Output is correct |